Source code

Revision control

Other Tools

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/planar_functions.h"
12
13
#include <string.h> // for memset()
14
15
#include "libyuv/cpu_id.h"
16
#ifdef HAVE_JPEG
17
#include "libyuv/mjpeg_decoder.h"
18
#endif
19
#include "libyuv/row.h"
20
#include "libyuv/scale_row.h" // for ScaleRowDown2
21
22
#ifdef __cplusplus
23
namespace libyuv {
24
extern "C" {
25
#endif
26
27
// Copy a plane of data
28
LIBYUV_API
29
void CopyPlane(const uint8* src_y, int src_stride_y,
30
uint8* dst_y, int dst_stride_y,
31
int width, int height) {
32
int y;
33
void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
34
// Negative height means invert the image.
35
if (height < 0) {
36
height = -height;
37
dst_y = dst_y + (height - 1) * dst_stride_y;
38
dst_stride_y = -dst_stride_y;
39
}
40
// Coalesce rows.
41
if (src_stride_y == width &&
42
dst_stride_y == width) {
43
width *= height;
44
height = 1;
45
src_stride_y = dst_stride_y = 0;
46
}
47
// Nothing to do.
48
if (src_y == dst_y && src_stride_y == dst_stride_y) {
49
return;
50
}
51
#if defined(HAS_COPYROW_SSE2)
52
if (TestCpuFlag(kCpuHasSSE2)) {
53
CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
54
}
55
#endif
56
#if defined(HAS_COPYROW_AVX)
57
if (TestCpuFlag(kCpuHasAVX)) {
58
CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
59
}
60
#endif
61
#if defined(HAS_COPYROW_ERMS)
62
if (TestCpuFlag(kCpuHasERMS)) {
63
CopyRow = CopyRow_ERMS;
64
}
65
#endif
66
#if defined(HAS_COPYROW_NEON)
67
if (TestCpuFlag(kCpuHasNEON)) {
68
CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
69
}
70
#endif
71
#if defined(HAS_COPYROW_MIPS)
72
if (TestCpuFlag(kCpuHasMIPS)) {
73
CopyRow = CopyRow_MIPS;
74
}
75
#endif
76
77
// Copy plane
78
for (y = 0; y < height; ++y) {
79
CopyRow(src_y, dst_y, width);
80
src_y += src_stride_y;
81
dst_y += dst_stride_y;
82
}
83
}
84
85
// TODO(fbarchard): Consider support for negative height.
86
LIBYUV_API
87
void CopyPlane_16(const uint16* src_y, int src_stride_y,
88
uint16* dst_y, int dst_stride_y,
89
int width, int height) {
90
int y;
91
void (*CopyRow)(const uint16* src, uint16* dst, int width) = CopyRow_16_C;
92
// Coalesce rows.
93
if (src_stride_y == width &&
94
dst_stride_y == width) {
95
width *= height;
96
height = 1;
97
src_stride_y = dst_stride_y = 0;
98
}
99
#if defined(HAS_COPYROW_16_SSE2)
100
if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32)) {
101
CopyRow = CopyRow_16_SSE2;
102
}
103
#endif
104
#if defined(HAS_COPYROW_16_ERMS)
105
if (TestCpuFlag(kCpuHasERMS)) {
106
CopyRow = CopyRow_16_ERMS;
107
}
108
#endif
109
#if defined(HAS_COPYROW_16_NEON)
110
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) {
111
CopyRow = CopyRow_16_NEON;
112
}
113
#endif
114
#if defined(HAS_COPYROW_16_MIPS)
115
if (TestCpuFlag(kCpuHasMIPS)) {
116
CopyRow = CopyRow_16_MIPS;
117
}
118
#endif
119
120
// Copy plane
121
for (y = 0; y < height; ++y) {
122
CopyRow(src_y, dst_y, width);
123
src_y += src_stride_y;
124
dst_y += dst_stride_y;
125
}
126
}
127
128
// Copy I422.
129
LIBYUV_API
130
int I422Copy(const uint8* src_y, int src_stride_y,
131
const uint8* src_u, int src_stride_u,
132
const uint8* src_v, int src_stride_v,
133
uint8* dst_y, int dst_stride_y,
134
uint8* dst_u, int dst_stride_u,
135
uint8* dst_v, int dst_stride_v,
136
int width, int height) {
137
int halfwidth = (width + 1) >> 1;
138
if (!src_u || !src_v ||
139
!dst_u || !dst_v ||
140
width <= 0 || height == 0) {
141
return -1;
142
}
143
// Negative height means invert the image.
144
if (height < 0) {
145
height = -height;
146
src_y = src_y + (height - 1) * src_stride_y;
147
src_u = src_u + (height - 1) * src_stride_u;
148
src_v = src_v + (height - 1) * src_stride_v;
149
src_stride_y = -src_stride_y;
150
src_stride_u = -src_stride_u;
151
src_stride_v = -src_stride_v;
152
}
153
154
if (dst_y) {
155
CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
156
}
157
CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height);
158
CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height);
159
return 0;
160
}
161
162
// Copy I444.
163
LIBYUV_API
164
int I444Copy(const uint8* src_y, int src_stride_y,
165
const uint8* src_u, int src_stride_u,
166
const uint8* src_v, int src_stride_v,
167
uint8* dst_y, int dst_stride_y,
168
uint8* dst_u, int dst_stride_u,
169
uint8* dst_v, int dst_stride_v,
170
int width, int height) {
171
if (!src_u || !src_v ||
172
!dst_u || !dst_v ||
173
width <= 0 || height == 0) {
174
return -1;
175
}
176
// Negative height means invert the image.
177
if (height < 0) {
178
height = -height;
179
src_y = src_y + (height - 1) * src_stride_y;
180
src_u = src_u + (height - 1) * src_stride_u;
181
src_v = src_v + (height - 1) * src_stride_v;
182
src_stride_y = -src_stride_y;
183
src_stride_u = -src_stride_u;
184
src_stride_v = -src_stride_v;
185
}
186
187
if (dst_y) {
188
CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
189
}
190
CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height);
191
CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height);
192
return 0;
193
}
194
195
// Copy I400.
196
LIBYUV_API
197
int I400ToI400(const uint8* src_y, int src_stride_y,
198
uint8* dst_y, int dst_stride_y,
199
int width, int height) {
200
if (!src_y || !dst_y || width <= 0 || height == 0) {
201
return -1;
202
}
203
// Negative height means invert the image.
204
if (height < 0) {
205
height = -height;
206
src_y = src_y + (height - 1) * src_stride_y;
207
src_stride_y = -src_stride_y;
208
}
209
CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
210
return 0;
211
}
212
213
// Convert I420 to I400.
214
LIBYUV_API
215
int I420ToI400(const uint8* src_y, int src_stride_y,
216
const uint8* src_u, int src_stride_u,
217
const uint8* src_v, int src_stride_v,
218
uint8* dst_y, int dst_stride_y,
219
int width, int height) {
220
if (!src_y || !dst_y || width <= 0 || height == 0) {
221
return -1;
222
}
223
// Negative height means invert the image.
224
if (height < 0) {
225
height = -height;
226
src_y = src_y + (height - 1) * src_stride_y;
227
src_stride_y = -src_stride_y;
228
}
229
230
CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
231
return 0;
232
}
233
234
// Support function for NV12 etc UV channels.
235
// Width and height are plane sizes (typically half pixel width).
236
LIBYUV_API
237
void SplitUVPlane(const uint8* src_uv, int src_stride_uv,
238
uint8* dst_u, int dst_stride_u,
239
uint8* dst_v, int dst_stride_v,
240
int width, int height) {
241
int y;
242
void (*SplitUVRow)(const uint8* src_uv, uint8* dst_u, uint8* dst_v,
243
int width) = SplitUVRow_C;
244
// Negative height means invert the image.
245
if (height < 0) {
246
height = -height;
247
dst_u = dst_u + (height - 1) * dst_stride_u;
248
dst_v = dst_v + (height - 1) * dst_stride_v;
249
dst_stride_u = -dst_stride_u;
250
dst_stride_v = -dst_stride_v;
251
}
252
// Coalesce rows.
253
if (src_stride_uv == width * 2 &&
254
dst_stride_u == width &&
255
dst_stride_v == width) {
256
width *= height;
257
height = 1;
258
src_stride_uv = dst_stride_u = dst_stride_v = 0;
259
}
260
#if defined(HAS_SPLITUVROW_SSE2)
261
if (TestCpuFlag(kCpuHasSSE2)) {
262
SplitUVRow = SplitUVRow_Any_SSE2;
263
if (IS_ALIGNED(width, 16)) {
264
SplitUVRow = SplitUVRow_SSE2;
265
}
266
}
267
#endif
268
#if defined(HAS_SPLITUVROW_AVX2)
269
if (TestCpuFlag(kCpuHasAVX2)) {
270
SplitUVRow = SplitUVRow_Any_AVX2;
271
if (IS_ALIGNED(width, 32)) {
272
SplitUVRow = SplitUVRow_AVX2;
273
}
274
}
275
#endif
276
#if defined(HAS_SPLITUVROW_NEON)
277
if (TestCpuFlag(kCpuHasNEON)) {
278
SplitUVRow = SplitUVRow_Any_NEON;
279
if (IS_ALIGNED(width, 16)) {
280
SplitUVRow = SplitUVRow_NEON;
281
}
282
}
283
#endif
284
#if defined(HAS_SPLITUVROW_DSPR2)
285
if (TestCpuFlag(kCpuHasDSPR2) &&
286
IS_ALIGNED(dst_u, 4) && IS_ALIGNED(dst_stride_u, 4) &&
287
IS_ALIGNED(dst_v, 4) && IS_ALIGNED(dst_stride_v, 4)) {
288
SplitUVRow = SplitUVRow_Any_DSPR2;
289
if (IS_ALIGNED(width, 16)) {
290
SplitUVRow = SplitUVRow_DSPR2;
291
}
292
}
293
#endif
294
295
for (y = 0; y < height; ++y) {
296
// Copy a row of UV.
297
SplitUVRow(src_uv, dst_u, dst_v, width);
298
dst_u += dst_stride_u;
299
dst_v += dst_stride_v;
300
src_uv += src_stride_uv;
301
}
302
}
303
304
LIBYUV_API
305
void MergeUVPlane(const uint8* src_u, int src_stride_u,
306
const uint8* src_v, int src_stride_v,
307
uint8* dst_uv, int dst_stride_uv,
308
int width, int height) {
309
int y;
310
void (*MergeUVRow)(const uint8* src_u, const uint8* src_v, uint8* dst_uv,
311
int width) = MergeUVRow_C;
312
// Coalesce rows.
313
// Negative height means invert the image.
314
if (height < 0) {
315
height = -height;
316
dst_uv = dst_uv + (height - 1) * dst_stride_uv;
317
dst_stride_uv = -dst_stride_uv;
318
}
319
// Coalesce rows.
320
if (src_stride_u == width &&
321
src_stride_v == width &&
322
dst_stride_uv == width * 2) {
323
width *= height;
324
height = 1;
325
src_stride_u = src_stride_v = dst_stride_uv = 0;
326
}
327
#if defined(HAS_MERGEUVROW_SSE2)
328
if (TestCpuFlag(kCpuHasSSE2)) {
329
MergeUVRow = MergeUVRow_Any_SSE2;
330
if (IS_ALIGNED(width, 16)) {
331
MergeUVRow = MergeUVRow_SSE2;
332
}
333
}
334
#endif
335
#if defined(HAS_MERGEUVROW_AVX2)
336
if (TestCpuFlag(kCpuHasAVX2)) {
337
MergeUVRow = MergeUVRow_Any_AVX2;
338
if (IS_ALIGNED(width, 32)) {
339
MergeUVRow = MergeUVRow_AVX2;
340
}
341
}
342
#endif
343
#if defined(HAS_MERGEUVROW_NEON)
344
if (TestCpuFlag(kCpuHasNEON)) {
345
MergeUVRow = MergeUVRow_Any_NEON;
346
if (IS_ALIGNED(width, 16)) {
347
MergeUVRow = MergeUVRow_NEON;
348
}
349
}
350
#endif
351
352
for (y = 0; y < height; ++y) {
353
// Merge a row of U and V into a row of UV.
354
MergeUVRow(src_u, src_v, dst_uv, width);
355
src_u += src_stride_u;
356
src_v += src_stride_v;
357
dst_uv += dst_stride_uv;
358
}
359
}
360
361
// Mirror a plane of data.
362
void MirrorPlane(const uint8* src_y, int src_stride_y,
363
uint8* dst_y, int dst_stride_y,
364
int width, int height) {
365
int y;
366
void (*MirrorRow)(const uint8* src, uint8* dst, int width) = MirrorRow_C;
367
// Negative height means invert the image.
368
if (height < 0) {
369
height = -height;
370
src_y = src_y + (height - 1) * src_stride_y;
371
src_stride_y = -src_stride_y;
372
}
373
#if defined(HAS_MIRRORROW_NEON)
374
if (TestCpuFlag(kCpuHasNEON)) {
375
MirrorRow = MirrorRow_Any_NEON;
376
if (IS_ALIGNED(width, 16)) {
377
MirrorRow = MirrorRow_NEON;
378
}
379
}
380
#endif
381
#if defined(HAS_MIRRORROW_SSSE3)
382
if (TestCpuFlag(kCpuHasSSSE3)) {
383
MirrorRow = MirrorRow_Any_SSSE3;
384
if (IS_ALIGNED(width, 16)) {
385
MirrorRow = MirrorRow_SSSE3;
386
}
387
}
388
#endif
389
#if defined(HAS_MIRRORROW_AVX2)
390
if (TestCpuFlag(kCpuHasAVX2)) {
391
MirrorRow = MirrorRow_Any_AVX2;
392
if (IS_ALIGNED(width, 32)) {
393
MirrorRow = MirrorRow_AVX2;
394
}
395
}
396
#endif
397
// TODO(fbarchard): Mirror on mips handle unaligned memory.
398
#if defined(HAS_MIRRORROW_DSPR2)
399
if (TestCpuFlag(kCpuHasDSPR2) &&
400
IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
401
IS_ALIGNED(dst_y, 4) && IS_ALIGNED(dst_stride_y, 4)) {
402
MirrorRow = MirrorRow_DSPR2;
403
}
404
#endif
405
406
// Mirror plane
407
for (y = 0; y < height; ++y) {
408
MirrorRow(src_y, dst_y, width);
409
src_y += src_stride_y;
410
dst_y += dst_stride_y;
411
}
412
}
413
414
// Convert YUY2 to I422.
415
LIBYUV_API
416
int YUY2ToI422(const uint8* src_yuy2, int src_stride_yuy2,
417
uint8* dst_y, int dst_stride_y,
418
uint8* dst_u, int dst_stride_u,
419
uint8* dst_v, int dst_stride_v,
420
int width, int height) {
421
int y;
422
void (*YUY2ToUV422Row)(const uint8* src_yuy2,
423
uint8* dst_u, uint8* dst_v, int width) =
424
YUY2ToUV422Row_C;
425
void (*YUY2ToYRow)(const uint8* src_yuy2, uint8* dst_y, int width) =
426
YUY2ToYRow_C;
427
// Negative height means invert the image.
428
if (height < 0) {
429
height = -height;
430
src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2;
431
src_stride_yuy2 = -src_stride_yuy2;
432
}
433
// Coalesce rows.
434
if (src_stride_yuy2 == width * 2 &&
435
dst_stride_y == width &&
436
dst_stride_u * 2 == width &&
437
dst_stride_v * 2 == width) {
438
width *= height;
439
height = 1;
440
src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0;
441
}
442
#if defined(HAS_YUY2TOYROW_SSE2)
443
if (TestCpuFlag(kCpuHasSSE2)) {
444
YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2;
445
YUY2ToYRow = YUY2ToYRow_Any_SSE2;
446
if (IS_ALIGNED(width, 16)) {
447
YUY2ToUV422Row = YUY2ToUV422Row_SSE2;
448
YUY2ToYRow = YUY2ToYRow_SSE2;
449
}
450
}
451
#endif
452
#if defined(HAS_YUY2TOYROW_AVX2)
453
if (TestCpuFlag(kCpuHasAVX2)) {
454
YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2;
455
YUY2ToYRow = YUY2ToYRow_Any_AVX2;
456
if (IS_ALIGNED(width, 32)) {
457
YUY2ToUV422Row = YUY2ToUV422Row_AVX2;
458
YUY2ToYRow = YUY2ToYRow_AVX2;
459
}
460
}
461
#endif
462
#if defined(HAS_YUY2TOYROW_NEON)
463
if (TestCpuFlag(kCpuHasNEON)) {
464
YUY2ToYRow = YUY2ToYRow_Any_NEON;
465
if (width >= 16) {
466
YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON;
467
}
468
if (IS_ALIGNED(width, 16)) {
469
YUY2ToYRow = YUY2ToYRow_NEON;
470
YUY2ToUV422Row = YUY2ToUV422Row_NEON;
471
}
472
}
473
#endif
474
475
for (y = 0; y < height; ++y) {
476
YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width);
477
YUY2ToYRow(src_yuy2, dst_y, width);
478
src_yuy2 += src_stride_yuy2;
479
dst_y += dst_stride_y;
480
dst_u += dst_stride_u;
481
dst_v += dst_stride_v;
482
}
483
return 0;
484
}
485
486
// Convert UYVY to I422.
487
LIBYUV_API
488
int UYVYToI422(const uint8* src_uyvy, int src_stride_uyvy,
489
uint8* dst_y, int dst_stride_y,
490
uint8* dst_u, int dst_stride_u,
491
uint8* dst_v, int dst_stride_v,
492
int width, int height) {
493
int y;
494
void (*UYVYToUV422Row)(const uint8* src_uyvy,
495
uint8* dst_u, uint8* dst_v, int width) =
496
UYVYToUV422Row_C;
497
void (*UYVYToYRow)(const uint8* src_uyvy,
498
uint8* dst_y, int width) = UYVYToYRow_C;
499
// Negative height means invert the image.
500
if (height < 0) {
501
height = -height;
502
src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy;
503
src_stride_uyvy = -src_stride_uyvy;
504
}
505
// Coalesce rows.
506
if (src_stride_uyvy == width * 2 &&
507
dst_stride_y == width &&
508
dst_stride_u * 2 == width &&
509
dst_stride_v * 2 == width) {
510
width *= height;
511
height = 1;
512
src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0;
513
}
514
#if defined(HAS_UYVYTOYROW_SSE2)
515
if (TestCpuFlag(kCpuHasSSE2)) {
516
UYVYToUV422Row = UYVYToUV422Row_Any_SSE2;
517
UYVYToYRow = UYVYToYRow_Any_SSE2;
518
if (IS_ALIGNED(width, 16)) {
519
UYVYToUV422Row = UYVYToUV422Row_SSE2;
520
UYVYToYRow = UYVYToYRow_SSE2;
521
}
522
}
523
#endif
524
#if defined(HAS_UYVYTOYROW_AVX2)
525
if (TestCpuFlag(kCpuHasAVX2)) {
526
UYVYToUV422Row = UYVYToUV422Row_Any_AVX2;
527
UYVYToYRow = UYVYToYRow_Any_AVX2;
528
if (IS_ALIGNED(width, 32)) {
529
UYVYToUV422Row = UYVYToUV422Row_AVX2;
530
UYVYToYRow = UYVYToYRow_AVX2;
531
}
532
}
533
#endif
534
#if defined(HAS_UYVYTOYROW_NEON)
535
if (TestCpuFlag(kCpuHasNEON)) {
536
UYVYToYRow = UYVYToYRow_Any_NEON;
537
if (width >= 16) {
538
UYVYToUV422Row = UYVYToUV422Row_Any_NEON;
539
}
540
if (IS_ALIGNED(width, 16)) {
541
UYVYToYRow = UYVYToYRow_NEON;
542
UYVYToUV422Row = UYVYToUV422Row_NEON;
543
}
544
}
545
#endif
546
547
for (y = 0; y < height; ++y) {
548
UYVYToUV422Row(src_uyvy, dst_u, dst_v, width);
549
UYVYToYRow(src_uyvy, dst_y, width);
550
src_uyvy += src_stride_uyvy;
551
dst_y += dst_stride_y;
552
dst_u += dst_stride_u;
553
dst_v += dst_stride_v;
554
}
555
return 0;
556
}
557
558
// Mirror I400 with optional flipping
559
LIBYUV_API
560
int I400Mirror(const uint8* src_y, int src_stride_y,
561
uint8* dst_y, int dst_stride_y,
562
int width, int height) {
563
if (!src_y || !dst_y ||
564
width <= 0 || height == 0) {
565
return -1;
566
}
567
// Negative height means invert the image.
568
if (height < 0) {
569
height = -height;
570
src_y = src_y + (height - 1) * src_stride_y;
571
src_stride_y = -src_stride_y;
572
}
573
574
MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
575
return 0;
576
}
577
578
// Mirror I420 with optional flipping
579
LIBYUV_API
580
int I420Mirror(const uint8* src_y, int src_stride_y,
581
const uint8* src_u, int src_stride_u,
582
const uint8* src_v, int src_stride_v,
583
uint8* dst_y, int dst_stride_y,
584
uint8* dst_u, int dst_stride_u,
585
uint8* dst_v, int dst_stride_v,
586
int width, int height) {
587
int halfwidth = (width + 1) >> 1;
588
int halfheight = (height + 1) >> 1;
589
if (!src_y || !src_u || !src_v || !dst_y || !dst_u || !dst_v ||
590
width <= 0 || height == 0) {
591
return -1;
592
}
593
// Negative height means invert the image.
594
if (height < 0) {
595
height = -height;
596
halfheight = (height + 1) >> 1;
597
src_y = src_y + (height - 1) * src_stride_y;
598
src_u = src_u + (halfheight - 1) * src_stride_u;
599
src_v = src_v + (halfheight - 1) * src_stride_v;
600
src_stride_y = -src_stride_y;
601
src_stride_u = -src_stride_u;
602
src_stride_v = -src_stride_v;
603
}
604
605
if (dst_y) {
606
MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
607
}
608
MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight);
609
MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight);
610
return 0;
611
}
612
613
// ARGB mirror.
614
LIBYUV_API
615
int ARGBMirror(const uint8* src_argb, int src_stride_argb,
616
uint8* dst_argb, int dst_stride_argb,
617
int width, int height) {
618
int y;
619
void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
620
ARGBMirrorRow_C;
621
if (!src_argb || !dst_argb || width <= 0 || height == 0) {
622
return -1;
623
}
624
// Negative height means invert the image.
625
if (height < 0) {
626
height = -height;
627
src_argb = src_argb + (height - 1) * src_stride_argb;
628
src_stride_argb = -src_stride_argb;
629
}
630
#if defined(HAS_ARGBMIRRORROW_NEON)
631
if (TestCpuFlag(kCpuHasNEON)) {
632
ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
633
if (IS_ALIGNED(width, 4)) {
634
ARGBMirrorRow = ARGBMirrorRow_NEON;
635
}
636
}
637
#endif
638
#if defined(HAS_ARGBMIRRORROW_SSE2)
639
if (TestCpuFlag(kCpuHasSSE2)) {
640
ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
641
if (IS_ALIGNED(width, 4)) {
642
ARGBMirrorRow = ARGBMirrorRow_SSE2;
643
}
644
}
645
#endif
646
#if defined(HAS_ARGBMIRRORROW_AVX2)
647
if (TestCpuFlag(kCpuHasAVX2)) {
648
ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
649
if (IS_ALIGNED(width, 8)) {
650
ARGBMirrorRow = ARGBMirrorRow_AVX2;
651
}
652
}
653
#endif
654
655
// Mirror plane
656
for (y = 0; y < height; ++y) {
657
ARGBMirrorRow(src_argb, dst_argb, width);
658
src_argb += src_stride_argb;
659
dst_argb += dst_stride_argb;
660
}
661
return 0;
662
}
663
664
// Get a blender that optimized for the CPU and pixel count.
665
// As there are 6 blenders to choose from, the caller should try to use
666
// the same blend function for all pixels if possible.
667
LIBYUV_API
668
ARGBBlendRow GetARGBBlend() {
669
void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1,
670
uint8* dst_argb, int width) = ARGBBlendRow_C;
671
#if defined(HAS_ARGBBLENDROW_SSSE3)
672
if (TestCpuFlag(kCpuHasSSSE3)) {
673
ARGBBlendRow = ARGBBlendRow_SSSE3;
674
return ARGBBlendRow;
675
}
676
#endif
677
#if defined(HAS_ARGBBLENDROW_NEON)
678
if (TestCpuFlag(kCpuHasNEON)) {
679
ARGBBlendRow = ARGBBlendRow_NEON;
680
}
681
#endif
682
return ARGBBlendRow;
683
}
684
685
// Alpha Blend 2 ARGB images and store to destination.
686
LIBYUV_API
687
int ARGBBlend(const uint8* src_argb0, int src_stride_argb0,
688
const uint8* src_argb1, int src_stride_argb1,
689
uint8* dst_argb, int dst_stride_argb,
690
int width, int height) {
691
int y;
692
void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1,
693
uint8* dst_argb, int width) = GetARGBBlend();
694
if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
695
return -1;
696
}
697
// Negative height means invert the image.
698
if (height < 0) {
699
height = -height;
700
dst_argb = dst_argb + (height - 1) * dst_stride_argb;
701
dst_stride_argb = -dst_stride_argb;
702
}
703
// Coalesce rows.
704
if (src_stride_argb0 == width * 4 &&
705
src_stride_argb1 == width * 4 &&
706
dst_stride_argb == width * 4) {
707
width *= height;
708
height = 1;
709
src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
710
}
711
712
for (y = 0; y < height; ++y) {
713
ARGBBlendRow(src_argb0, src_argb1, dst_argb, width);
714
src_argb0 += src_stride_argb0;
715
src_argb1 += src_stride_argb1;
716
dst_argb += dst_stride_argb;
717
}
718
return 0;
719
}
720
721
// Alpha Blend plane and store to destination.
722
LIBYUV_API
723
int BlendPlane(const uint8* src_y0, int src_stride_y0,
724
const uint8* src_y1, int src_stride_y1,
725
const uint8* alpha, int alpha_stride,
726
uint8* dst_y, int dst_stride_y,
727
int width, int height) {
728
int y;
729
void (*BlendPlaneRow)(const uint8* src0, const uint8* src1,
730
const uint8* alpha, uint8* dst, int width) = BlendPlaneRow_C;
731
if (!src_y0 || !src_y1 || !alpha || !dst_y || width <= 0 || height == 0) {
732
return -1;
733
}
734
// Negative height means invert the image.
735
if (height < 0) {
736
height = -height;
737
dst_y = dst_y + (height - 1) * dst_stride_y;
738
dst_stride_y = -dst_stride_y;
739
}
740
741
// Coalesce rows for Y plane.
742
if (src_stride_y0 == width &&
743
src_stride_y1 == width &&
744
alpha_stride == width &&
745
dst_stride_y == width) {
746
width *= height;
747
height = 1;
748
src_stride_y0 = src_stride_y1 = alpha_stride = dst_stride_y = 0;
749
}
750
751
#if defined(HAS_BLENDPLANEROW_SSSE3)
752
if (TestCpuFlag(kCpuHasSSSE3)) {
753
BlendPlaneRow = BlendPlaneRow_Any_SSSE3;
754
if (IS_ALIGNED(width, 8)) {
755
BlendPlaneRow = BlendPlaneRow_SSSE3;
756
}
757
}
758
#endif
759
#if defined(HAS_BLENDPLANEROW_AVX2)
760
if (TestCpuFlag(kCpuHasAVX2)) {
761
BlendPlaneRow = BlendPlaneRow_Any_AVX2;
762
if (IS_ALIGNED(width, 32)) {
763
BlendPlaneRow = BlendPlaneRow_AVX2;
764
}
765
}
766
#endif
767
768
for (y = 0; y < height; ++y) {
769
BlendPlaneRow(src_y0, src_y1, alpha, dst_y, width);
770
src_y0 += src_stride_y0;
771
src_y1 += src_stride_y1;
772
alpha += alpha_stride;
773
dst_y += dst_stride_y;
774
}
775
return 0;
776
}
777
778
#define MAXTWIDTH 2048
779
// Alpha Blend YUV images and store to destination.
780
LIBYUV_API
781
int I420Blend(const uint8* src_y0, int src_stride_y0,
782
const uint8* src_u0, int src_stride_u0,
783
const uint8* src_v0, int src_stride_v0,
784
const uint8* src_y1, int src_stride_y1,
785
const uint8* src_u1, int src_stride_u1,
786
const uint8* src_v1, int src_stride_v1,
787
const uint8* alpha, int alpha_stride,
788
uint8* dst_y, int dst_stride_y,
789
uint8* dst_u, int dst_stride_u,
790
uint8* dst_v, int dst_stride_v,
791
int width, int height) {
792
int y;
793
// Half width/height for UV.
794
int halfwidth = (width + 1) >> 1;
795
void (*BlendPlaneRow)(const uint8* src0, const uint8* src1,
796
const uint8* alpha, uint8* dst, int width) = BlendPlaneRow_C;
797
void (*ScaleRowDown2)(const uint8* src_ptr, ptrdiff_t src_stride,
798
uint8* dst_ptr, int dst_width) = ScaleRowDown2Box_C;
799
if (!src_y0 || !src_u0 || !src_v0 || !src_y1 || !src_u1 || !src_v1 ||
800
!alpha || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) {
801
return -1;
802
}
803
804
// Negative height means invert the image.
805
if (height < 0) {
806
height = -height;
807
dst_y = dst_y + (height - 1) * dst_stride_y;
808
dst_stride_y = -dst_stride_y;
809
}
810
811
// Blend Y plane.
812
BlendPlane(src_y0, src_stride_y0,
813
src_y1, src_stride_y1,
814
alpha, alpha_stride,
815
dst_y, dst_stride_y,
816
width, height);
817
818
#if defined(HAS_BLENDPLANEROW_SSSE3)
819
if (TestCpuFlag(kCpuHasSSSE3)) {
820
BlendPlaneRow = BlendPlaneRow_Any_SSSE3;
821
if (IS_ALIGNED(halfwidth, 8)) {
822
BlendPlaneRow = BlendPlaneRow_SSSE3;
823
}
824
}
825
#endif
826
#if defined(HAS_BLENDPLANEROW_AVX2)
827
if (TestCpuFlag(kCpuHasAVX2)) {
828
BlendPlaneRow = BlendPlaneRow_Any_AVX2;
829
if (IS_ALIGNED(halfwidth, 32)) {
830
BlendPlaneRow = BlendPlaneRow_AVX2;
831
}
832
}
833
#endif
834
if (!IS_ALIGNED(width, 2)) {
835
ScaleRowDown2 = ScaleRowDown2Box_Odd_C;
836
}
837
#if defined(HAS_SCALEROWDOWN2_NEON)
838
if (TestCpuFlag(kCpuHasNEON)) {
839
ScaleRowDown2 = ScaleRowDown2Box_Odd_NEON;
840
if (IS_ALIGNED(width, 2)) {
841
ScaleRowDown2 = ScaleRowDown2Box_Any_NEON;
842
if (IS_ALIGNED(halfwidth, 16)) {
843
ScaleRowDown2 = ScaleRowDown2Box_NEON;
844
}
845
}
846
}
847
#endif
848
#if defined(HAS_SCALEROWDOWN2_SSSE3)
849
if (TestCpuFlag(kCpuHasSSSE3)) {
850
ScaleRowDown2 = ScaleRowDown2Box_Odd_SSSE3;
851
if (IS_ALIGNED(width, 2)) {
852
ScaleRowDown2 = ScaleRowDown2Box_Any_SSSE3;
853
if (IS_ALIGNED(halfwidth, 16)) {
854
ScaleRowDown2 = ScaleRowDown2Box_SSSE3;
855
}
856
}
857
}
858
#endif
859
#if defined(HAS_SCALEROWDOWN2_AVX2)
860
if (TestCpuFlag(kCpuHasAVX2)) {
861
ScaleRowDown2 = ScaleRowDown2Box_Odd_AVX2;
862
if (IS_ALIGNED(width, 2)) {
863
ScaleRowDown2 = ScaleRowDown2Box_Any_AVX2;
864
if (IS_ALIGNED(halfwidth, 32)) {
865
ScaleRowDown2 = ScaleRowDown2Box_AVX2;
866
}
867
}
868
}
869
#endif
870
871
// Row buffer for intermediate alpha pixels.
872
align_buffer_64(halfalpha, halfwidth);
873
for (y = 0; y < height; y += 2) {
874
// last row of odd height image use 1 row of alpha instead of 2.
875
if (y == (height - 1)) {
876
alpha_stride = 0;
877
}
878
// Subsample 2 rows of UV to half width and half height.
879
ScaleRowDown2(alpha, alpha_stride, halfalpha, halfwidth);
880
alpha += alpha_stride * 2;
881
BlendPlaneRow(src_u0, src_u1, halfalpha, dst_u, halfwidth);
882
BlendPlaneRow(src_v0, src_v1, halfalpha, dst_v, halfwidth);
883
src_u0 += src_stride_u0;
884
src_u1 += src_stride_u1;
885
dst_u += dst_stride_u;
886
src_v0 += src_stride_v0;
887
src_v1 += src_stride_v1;
888
dst_v += dst_stride_v;
889
}
890
free_aligned_buffer_64(halfalpha);
891
return 0;
892
}
893
894
// Multiply 2 ARGB images and store to destination.
895
LIBYUV_API
896
int ARGBMultiply(const uint8* src_argb0, int src_stride_argb0,
897
const uint8* src_argb1, int src_stride_argb1,
898
uint8* dst_argb, int dst_stride_argb,
899
int width, int height) {
900
int y;
901
void (*ARGBMultiplyRow)(const uint8* src0, const uint8* src1, uint8* dst,
902
int width) = ARGBMultiplyRow_C;
903
if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
904
return -1;
905
}
906
// Negative height means invert the image.
907
if (height < 0) {
908
height = -height;
909
dst_argb = dst_argb + (height - 1) * dst_stride_argb;
910
dst_stride_argb = -dst_stride_argb;
911
}
912
// Coalesce rows.
913
if (src_stride_argb0 == width * 4 &&
914
src_stride_argb1 == width * 4 &&
915
dst_stride_argb == width * 4) {
916
width *= height;
917
height = 1;
918
src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
919
}
920
#if defined(HAS_ARGBMULTIPLYROW_SSE2)
921
if (TestCpuFlag(kCpuHasSSE2)) {
922
ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2;
923
if (IS_ALIGNED(width, 4)) {
924
ARGBMultiplyRow = ARGBMultiplyRow_SSE2;
925
}
926
}
927
#endif
928
#if defined(HAS_ARGBMULTIPLYROW_AVX2)
929
if (TestCpuFlag(kCpuHasAVX2)) {
930
ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2;
931
if (IS_ALIGNED(width, 8)) {
932
ARGBMultiplyRow = ARGBMultiplyRow_AVX2;
933
}
934
}
935
#endif
936
#if defined(HAS_ARGBMULTIPLYROW_NEON)
937
if (TestCpuFlag(kCpuHasNEON)) {
938
ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON;
939
if (IS_ALIGNED(width, 8)) {
940
ARGBMultiplyRow = ARGBMultiplyRow_NEON;
941
}
942
}
943
#endif
944
945
// Multiply plane
946
for (y = 0; y < height; ++y) {
947
ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width);
948
src_argb0 += src_stride_argb0;
949
src_argb1 += src_stride_argb1;
950
dst_argb += dst_stride_argb;
951
}
952
return 0;
953
}
954
955
// Add 2 ARGB images and store to destination.
956
LIBYUV_API
957
int ARGBAdd(const uint8* src_argb0, int src_stride_argb0,
958
const uint8* src_argb1, int src_stride_argb1,
959
uint8* dst_argb, int dst_stride_argb,
960
int width, int height) {
961
int y;
962
void (*ARGBAddRow)(const uint8* src0, const uint8* src1, uint8* dst,
963
int width) = ARGBAddRow_C;
964
if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
965
return -1;
966
}
967
// Negative height means invert the image.
968
if (height < 0) {
969
height = -height;
970
dst_argb = dst_argb + (height - 1) * dst_stride_argb;
971
dst_stride_argb = -dst_stride_argb;
972
}
973
// Coalesce rows.
974
if (src_stride_argb0 == width * 4 &&
975
src_stride_argb1 == width * 4 &&
976
dst_stride_argb == width * 4) {
977
width *= height;
978
height = 1;
979
src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
980
}
981
#if defined(HAS_ARGBADDROW_SSE2) && (defined(_MSC_VER) && !defined(__clang__))
982
if (TestCpuFlag(kCpuHasSSE2)) {
983
ARGBAddRow = ARGBAddRow_SSE2;
984
}
985
#endif
986
#if defined(HAS_ARGBADDROW_SSE2) && !(defined(_MSC_VER) && !defined(__clang__))
987
if (TestCpuFlag(kCpuHasSSE2)) {
988
ARGBAddRow = ARGBAddRow_Any_SSE2;
989
if (IS_ALIGNED(width, 4)) {
990
ARGBAddRow = ARGBAddRow_SSE2;
991
}
992
}
993
#endif
994
#if defined(HAS_ARGBADDROW_AVX2)
995
if (TestCpuFlag(kCpuHasAVX2)) {
996
ARGBAddRow = ARGBAddRow_Any_AVX2;
997
if (IS_ALIGNED(width, 8)) {
998
ARGBAddRow = ARGBAddRow_AVX2;
999
}
1000
}
1001
#endif
1002
#if defined(HAS_ARGBADDROW_NEON)
1003
if (TestCpuFlag(kCpuHasNEON)) {
1004
ARGBAddRow = ARGBAddRow_Any_NEON;
1005
if (IS_ALIGNED(width, 8)) {
1006
ARGBAddRow = ARGBAddRow_NEON;
1007
}
1008
}
1009
#endif
1010
1011
// Add plane
1012
for (y = 0; y < height; ++y) {
1013
ARGBAddRow(src_argb0, src_argb1, dst_argb, width);
1014
src_argb0 += src_stride_argb0;
1015
src_argb1 += src_stride_argb1;
1016
dst_argb += dst_stride_argb;
1017
}
1018
return 0;
1019
}
1020
1021
// Subtract 2 ARGB images and store to destination.
1022
LIBYUV_API
1023
int ARGBSubtract(const uint8* src_argb0, int src_stride_argb0,
1024
const uint8* src_argb1, int src_stride_argb1,
1025
uint8* dst_argb, int dst_stride_argb,
1026
int width, int height) {
1027
int y;
1028
void (*ARGBSubtractRow)(const uint8* src0, const uint8* src1, uint8* dst,
1029
int width) = ARGBSubtractRow_C;
1030
if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
1031
return -1;
1032
}
1033
// Negative height means invert the image.
1034
if (height < 0) {
1035
height = -height;
1036
dst_argb = dst_argb + (height - 1) * dst_stride_argb;
1037
dst_stride_argb = -dst_stride_argb;
1038
}
1039
// Coalesce rows.
1040
if (src_stride_argb0 == width * 4 &&
1041
src_stride_argb1 == width * 4 &&
1042
dst_stride_argb == width * 4) {
1043
width *= height;
1044
height = 1;
1045
src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
1046
}
1047
#if defined(HAS_ARGBSUBTRACTROW_SSE2)
1048
if (TestCpuFlag(kCpuHasSSE2)) {
1049
ARGBSubtractRow = ARGBSubtractRow_Any_SSE2;
1050
if (IS_ALIGNED(width, 4)) {
1051
ARGBSubtractRow = ARGBSubtractRow_SSE2;
1052
}
1053
}
1054
#endif
1055
#if defined(HAS_ARGBSUBTRACTROW_AVX2)
1056
if (TestCpuFlag(kCpuHasAVX2)) {
1057
ARGBSubtractRow = ARGBSubtractRow_Any_AVX2;
1058
if (IS_ALIGNED(width, 8)) {
1059
ARGBSubtractRow = ARGBSubtractRow_AVX2;
1060
}
1061
}
1062
#endif
1063
#if defined(HAS_ARGBSUBTRACTROW_NEON)
1064
if (TestCpuFlag(kCpuHasNEON)) {
1065
ARGBSubtractRow = ARGBSubtractRow_Any_NEON;
1066
if (IS_ALIGNED(width, 8)) {
1067
ARGBSubtractRow = ARGBSubtractRow_NEON;
1068
}
1069
}
1070
#endif
1071
1072
// Subtract plane
1073
for (y = 0; y < height; ++y) {
1074
ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width);
1075
src_argb0 += src_stride_argb0;
1076
src_argb1 += src_stride_argb1;
1077
dst_argb += dst_stride_argb;
1078
}
1079
return 0;
1080
}
1081
// Convert I422 to RGBA with matrix
1082
static int I422ToRGBAMatrix(const uint8* src_y, int src_stride_y,
1083
const uint8* src_u, int src_stride_u,
1084
const uint8* src_v, int src_stride_v,
1085
uint8* dst_rgba, int dst_stride_rgba,
1086
const struct YuvConstants* yuvconstants,
1087
int width, int height) {
1088
int y;
1089
void (*I422ToRGBARow)(const uint8* y_buf,
1090
const uint8* u_buf,
1091
const uint8* v_buf,
1092
uint8* rgb_buf,
1093
const struct YuvConstants* yuvconstants,
1094
int width) = I422ToRGBARow_C;
1095
if (!src_y || !src_u || !src_v || !dst_rgba ||
1096
width <= 0 || height == 0) {
1097
return -1;
1098
}
1099
// Negative height means invert the image.
1100
if (height < 0) {
1101
height = -height;
1102
dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba;
1103
dst_stride_rgba = -dst_stride_rgba;
1104
}
1105
#if defined(HAS_I422TORGBAROW_SSSE3)
1106
if (TestCpuFlag(kCpuHasSSSE3)) {
1107
I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
1108
if (IS_ALIGNED(width, 8)) {
1109
I422ToRGBARow = I422ToRGBARow_SSSE3;
1110
}
1111
}
1112
#endif
1113
#if defined(HAS_I422TORGBAROW_AVX2)
1114
if (TestCpuFlag(kCpuHasAVX2)) {
1115
I422ToRGBARow = I422ToRGBARow_Any_AVX2;
1116
if (IS_ALIGNED(width, 16)) {
1117
I422ToRGBARow = I422ToRGBARow_AVX2;
1118
}
1119
}
1120
#endif
1121
#if defined(HAS_I422TORGBAROW_NEON)
1122
if (TestCpuFlag(kCpuHasNEON)) {
1123
I422ToRGBARow = I422ToRGBARow_Any_NEON;
1124
if (IS_ALIGNED(width, 8)) {
1125
I422ToRGBARow = I422ToRGBARow_NEON;
1126
}
1127
}
1128
#endif
1129
#if defined(HAS_I422TORGBAROW_DSPR2)
1130
if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(width, 4) &&
1131
IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
1132
IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
1133
IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) &&
1134
IS_ALIGNED(dst_rgba, 4) && IS_ALIGNED(dst_stride_rgba, 4)) {
1135
I422ToRGBARow = I422ToRGBARow_DSPR2;
1136
}
1137
#endif
1138
1139
for (y = 0; y < height; ++y) {
1140
I422ToRGBARow(src_y, src_u, src_v, dst_rgba, yuvconstants, width);
1141
dst_rgba += dst_stride_rgba;
1142
src_y += src_stride_y;
1143
src_u += src_stride_u;
1144
src_v += src_stride_v;
1145
}
1146
return 0;
1147
}
1148
1149
// Convert I422 to RGBA.
1150
LIBYUV_API
1151
int I422ToRGBA(const uint8* src_y, int src_stride_y,
1152
const uint8* src_u, int src_stride_u,
1153
const uint8* src_v, int src_stride_v,
1154
uint8* dst_rgba, int dst_stride_rgba,
1155
int width, int height) {
1156
return I422ToRGBAMatrix(src_y, src_stride_y,
1157
src_u, src_stride_u,
1158
src_v, src_stride_v,
1159
dst_rgba, dst_stride_rgba,
1160
&kYuvI601Constants,
1161
width, height);
1162
}
1163
1164
// Convert I422 to BGRA.
1165
LIBYUV_API
1166
int I422ToBGRA(const uint8* src_y, int src_stride_y,
1167
const uint8* src_u, int src_stride_u,
1168
const uint8* src_v, int src_stride_v,
1169
uint8* dst_bgra, int dst_stride_bgra,
1170
int width, int height) {
1171
return I422ToRGBAMatrix(src_y, src_stride_y,
1172
src_v, src_stride_v, // Swap U and V
1173
src_u, src_stride_u,
1174
dst_bgra, dst_stride_bgra,
1175
&kYvuI601Constants, // Use Yvu matrix
1176
width, height);
1177
}
1178
1179
// Convert NV12 to RGB565.
1180
LIBYUV_API
1181
int NV12ToRGB565(const uint8* src_y, int src_stride_y,
1182
const uint8* src_uv, int src_stride_uv,
1183
uint8* dst_rgb565, int dst_stride_rgb565,
1184
int width, int height) {
1185
int y;
1186
void (*NV12ToRGB565Row)(const uint8* y_buf,
1187
const uint8* uv_buf,
1188
uint8* rgb_buf,
1189
const struct YuvConstants* yuvconstants,
1190
int width) = NV12ToRGB565Row_C;
1191
if (!src_y || !src_uv || !dst_rgb565 ||
1192
width <= 0 || height == 0) {
1193
return -1;
1194
}
1195
// Negative height means invert the image.
1196
if (height < 0) {
1197
height = -height;
1198
dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
1199
dst_stride_rgb565 = -dst_stride_rgb565;
1200
}
1201
#if defined(HAS_NV12TORGB565ROW_SSSE3)
1202
if (TestCpuFlag(kCpuHasSSSE3)) {
1203
NV12ToRGB565Row = NV12ToRGB565Row_Any_SSSE3;
1204
if (IS_ALIGNED(width, 8)) {
1205
NV12ToRGB565Row = NV12ToRGB565Row_SSSE3;
1206
}
1207
}
1208
#endif
1209
#if defined(HAS_NV12TORGB565ROW_AVX2)
1210
if (TestCpuFlag(kCpuHasAVX2)) {
1211
NV12ToRGB565Row = NV12ToRGB565Row_Any_AVX2;
1212
if (IS_ALIGNED(width, 16)) {
1213
NV12ToRGB565Row = NV12ToRGB565Row_AVX2;
1214
}
1215
}
1216
#endif
1217
#if defined(HAS_NV12TORGB565ROW_NEON)
1218
if (TestCpuFlag(kCpuHasNEON)) {
1219
NV12ToRGB565Row = NV12ToRGB565Row_Any_NEON;
1220
if (IS_ALIGNED(width, 8)) {
1221
NV12ToRGB565Row = NV12ToRGB565Row_NEON;
1222
}
1223
}
1224
#endif
1225
1226
for (y = 0; y < height; ++y) {
1227
NV12ToRGB565Row(src_y, src_uv, dst_rgb565, &kYuvI601Constants, width);
1228
dst_rgb565 += dst_stride_rgb565;
1229
src_y += src_stride_y;
1230
if (y & 1) {
1231
src_uv += src_stride_uv;
1232
}
1233
}
1234
return 0;
1235
}
1236
1237
// Convert RAW to RGB24.
1238
LIBYUV_API
1239
int RAWToRGB24(const uint8* src_raw, int src_stride_raw,
1240
uint8* dst_rgb24, int dst_stride_rgb24,
1241
int width, int height) {
1242
int y;
1243
void (*RAWToRGB24Row)(const uint8* src_rgb, uint8* dst_rgb24, int width) =
1244
RAWToRGB24Row_C;
1245
if (!src_raw || !dst_rgb24 ||
1246
width <= 0 || height == 0) {
1247
return -1;
1248
}
1249
// Negative height means invert the image.
1250
if (height < 0) {
1251
height = -height;
1252
src_raw = src_raw + (height - 1) * src_stride_raw;
1253
src_stride_raw = -src_stride_raw;
1254
}
1255
// Coalesce rows.
1256
if (src_stride_raw == width * 3 &&
1257
dst_stride_rgb24 == width * 3) {
1258
width *= height;
1259
height = 1;
1260
src_stride_raw = dst_stride_rgb24 = 0;
1261
}
1262
#if defined(HAS_RAWTORGB24ROW_SSSE3)
1263
if (TestCpuFlag(kCpuHasSSSE3)) {
1264
RAWToRGB24Row = RAWToRGB24Row_Any_SSSE3;
1265
if (IS_ALIGNED(width, 8)) {
1266
RAWToRGB24Row = RAWToRGB24Row_SSSE3;
1267
}
1268
}
1269
#endif
1270
#if defined(HAS_RAWTORGB24ROW_NEON)
1271
if (TestCpuFlag(kCpuHasNEON)) {
1272
RAWToRGB24Row = RAWToRGB24Row_Any_NEON;
1273
if (IS_ALIGNED(width, 8)) {
1274
RAWToRGB24Row = RAWToRGB24Row_NEON;
1275
}
1276
}
1277
#endif
1278
1279
for (y = 0; y < height; ++y) {
1280
RAWToRGB24Row(src_raw, dst_rgb24, width);
1281
src_raw += src_stride_raw;
1282
dst_rgb24 += dst_stride_rgb24;
1283
}
1284
return 0;
1285
}
1286
1287
LIBYUV_API
1288
void SetPlane(uint8* dst_y, int dst_stride_y,
1289
int width, int height,
1290
uint32 value) {
1291
int y;
1292
void (*SetRow)(uint8* dst, uint8 value, int width) = SetRow_C;
1293
if (height < 0) {
1294
height = -height;
1295
dst_y = dst_y + (height - 1) * dst_stride_y;
1296
dst_stride_y = -dst_stride_y;
1297
}
1298
// Coalesce rows.
1299
if (dst_stride_y == width) {
1300
width *= height;
1301
height = 1;
1302
dst_stride_y = 0;
1303
}
1304
#if defined(HAS_SETROW_NEON)
1305
if (TestCpuFlag(kCpuHasNEON)) {
1306
SetRow = SetRow_Any_NEON;
1307
if (IS_ALIGNED(width, 16)) {
1308
SetRow = SetRow_NEON;
1309
}
1310
}
1311
#endif
1312
#if defined(HAS_SETROW_X86)
1313
if (TestCpuFlag(kCpuHasX86)) {
1314
SetRow = SetRow_Any_X86;
1315
if (IS_ALIGNED(width, 4)) {
1316
SetRow = SetRow_X86;
1317
}
1318
}
1319
#endif
1320
#if defined(HAS_SETROW_ERMS)
1321
if (TestCpuFlag(kCpuHasERMS)) {
1322
SetRow = SetRow_ERMS;
1323
}
1324
#endif
1325
1326
// Set plane
1327
for (y = 0; y < height; ++y) {
1328
SetRow(dst_y, value, width);
1329
dst_y += dst_stride_y;
1330
}
1331
}
1332
1333
// Draw a rectangle into I420
1334
LIBYUV_API
1335
int I420Rect(uint8* dst_y, int dst_stride_y,
1336
uint8* dst_u, int dst_stride_u,
1337
uint8* dst_v, int dst_stride_v,
1338
int x, int y,
1339
int width, int height,
1340
int value_y, int value_u, int value_v) {
1341
int halfwidth = (width + 1) >> 1;
1342
int halfheight = (height + 1) >> 1;
1343
uint8* start_y = dst_y + y * dst_stride_y + x;
1344
uint8* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2);
1345
uint8* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2);
1346
if (!dst_y || !dst_u || !dst_v ||
1347
width <= 0 || height == 0 ||
1348
x < 0 || y < 0 ||
1349
value_y < 0 || value_y > 255 ||
1350
value_u < 0 || value_u > 255 ||
1351
value_v < 0 || value_v > 255) {
1352
return -1;
1353
}
1354
1355
SetPlane(start_y, dst_stride_y, width, height, value_y);
1356
SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u);
1357
SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v);
1358
return 0;
1359
}
1360
1361
// Draw a rectangle into ARGB
1362
LIBYUV_API
1363
int ARGBRect(uint8* dst_argb, int dst_stride_argb,
1364
int dst_x, int dst_y,
1365
int width, int height,
1366
uint32 value) {
1367
int y;
1368
void (*ARGBSetRow)(uint8* dst_argb, uint32 value, int width) = ARGBSetRow_C;
1369
if (!dst_argb ||
1370
width <= 0 || height == 0 ||
1371
dst_x < 0 || dst_y < 0) {
1372
return -1;
1373
}
1374
if (height < 0) {
1375
height = -height;
1376
dst_argb = dst_argb + (height - 1) * dst_stride_argb;
1377
dst_stride_argb = -dst_stride_argb;
1378
}
1379
dst_argb += dst_y * dst_stride_argb + dst_x * 4;
1380
// Coalesce rows.
1381
if (dst_stride_argb == width * 4) {
1382
width *= height;
1383
height = 1;
1384
dst_stride_argb = 0;
1385
}
1386
1387
#if defined(HAS_ARGBSETROW_NEON)
1388
if (TestCpuFlag(kCpuHasNEON)) {
1389
ARGBSetRow = ARGBSetRow_Any_NEON;
1390
if (IS_ALIGNED(width, 4)) {
1391
ARGBSetRow = ARGBSetRow_NEON;
1392
}
1393
}
1394
#endif
1395
#if defined(HAS_ARGBSETROW_X86)
1396
if (TestCpuFlag(kCpuHasX86)) {
1397
ARGBSetRow = ARGBSetRow_X86;
1398
}
1399
#endif
1400
1401
// Set plane
1402
for (y = 0; y < height; ++y) {
1403
ARGBSetRow(dst_argb, value, width);
1404
dst_argb += dst_stride_argb;
1405
}
1406
return 0;
1407
}
1408
1409
// Convert unattentuated ARGB to preattenuated ARGB.
1410
// An unattenutated ARGB alpha blend uses the formula
1411
// p = a * f + (1 - a) * b
1412
// where
1413
// p is output pixel
1414
// f is foreground pixel
1415
// b is background pixel
1416
// a is alpha value from foreground pixel
1417
// An preattenutated ARGB alpha blend uses the formula
1418
// p = f + (1 - a) * b
1419
// where
1420
// f is foreground pixel premultiplied by alpha
1421
1422
LIBYUV_API
1423
int ARGBAttenuate(const uint8* src_argb, int src_stride_argb,
1424
uint8* dst_argb, int dst_stride_argb,
1425
int width, int height) {
1426
int y;
1427
void (*ARGBAttenuateRow)(const uint8* src_argb, uint8* dst_argb,
1428
int width) = ARGBAttenuateRow_C;
1429
if (!src_argb || !dst_argb || width <= 0 || height == 0) {
1430
return -1;
1431
}
1432
if (height < 0) {
1433
height = -height;
1434
src_argb = src_argb + (height - 1) * src_stride_argb;
1435
src_stride_argb = -src_stride_argb;
1436
}
1437
// Coalesce rows.
1438
if (src_stride_argb == width * 4 &&
1439
dst_stride_argb == width * 4) {
1440
width *= height;
1441
height = 1;
1442
src_stride_argb = dst_stride_argb = 0;
1443
}
1444
#if defined(HAS_ARGBATTENUATEROW_SSSE3)
1445
if (TestCpuFlag(kCpuHasSSSE3)) {
1446
ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3;
1447
if (IS_ALIGNED(width, 4)) {
1448
ARGBAttenuateRow = ARGBAttenuateRow_SSSE3;
1449
}
1450
}
1451
#endif
1452
#if defined(HAS_ARGBATTENUATEROW_AVX2)
1453
if (TestCpuFlag(kCpuHasAVX2)) {
1454
ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2;
1455
if (IS_ALIGNED(width, 8)) {
1456
ARGBAttenuateRow = ARGBAttenuateRow_AVX2;
1457
}
1458
}
1459
#endif
1460
#if defined(HAS_ARGBATTENUATEROW_NEON)
1461
if (TestCpuFlag(kCpuHasNEON)) {
1462
ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON;
1463
if (IS_ALIGNED(width, 8)) {
1464
ARGBAttenuateRow = ARGBAttenuateRow_NEON;
1465
}
1466
}
1467
#endif
1468
1469
for (y = 0; y < height; ++y) {
1470
ARGBAttenuateRow(src_argb, dst_argb, width);
1471
src_argb += src_stride_argb;
1472
dst_argb += dst_stride_argb;
1473
}
1474
return 0;
1475
}
1476
1477
// Convert preattentuated ARGB to unattenuated ARGB.
1478
LIBYUV_API
1479
int ARGBUnattenuate(const uint8* src_argb, int src_stride_argb,
1480
uint8* dst_argb, int dst_stride_argb,
1481
int width, int height) {
1482
int y;
1483
void (*ARGBUnattenuateRow)(const uint8* src_argb, uint8* dst_argb,
1484
int width) = ARGBUnattenuateRow_C;
1485
if (!src_argb || !dst_argb || width <= 0 || height == 0) {
1486
return -1;
1487
}
1488
if (height < 0) {
1489
height = -height;
1490
src_argb = src_argb + (height - 1) * src_stride_argb;
1491
src_stride_argb = -src_stride_argb;
1492
}
1493
// Coalesce rows.
1494
if (src_stride_argb == width * 4 &&
1495
dst_stride_argb == width * 4) {
1496
width *= height;
1497
height = 1;
1498
src_stride_argb = dst_stride_argb = 0;
1499
}
1500
#if defined(HAS_ARGBUNATTENUATEROW_SSE2)
1501
if (TestCpuFlag(kCpuHasSSE2)) {
1502
ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2;
1503
if (IS_ALIGNED(width, 4)) {
1504
ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2;
1505
}
1506
}
1507
#endif
1508
#if defined(HAS_ARGBUNATTENUATEROW_AVX2)
1509
if (TestCpuFlag(kCpuHasAVX2)) {
1510
ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2;
1511
if (IS_ALIGNED(width, 8)) {
1512
ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2;
1513
}
1514
}
1515
#endif
1516
// TODO(fbarchard): Neon version.
1517
1518
for (y = 0; y < height; ++y) {
1519
ARGBUnattenuateRow(src_argb, dst_argb, width);
1520
src_argb += src_stride_argb;
1521
dst_argb += dst_stride_argb;
1522
}
1523
return 0;
1524
}
1525
1526
// Convert ARGB to Grayed ARGB.
1527
LIBYUV_API
1528
int ARGBGrayTo(const uint8* src_argb, int src_stride_argb,
1529
uint8* dst_argb, int dst_stride_argb,
1530
int width, int height) {
1531
int y;
1532
void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb,
1533
int width) = ARGBGrayRow_C;
1534
if (!src_argb || !dst_argb || width <= 0 || height == 0) {
1535
return -1;
1536
}
1537
if (height < 0) {
1538
height = -height;
1539
src_argb = src_argb + (height - 1) * src_stride_argb;
1540
src_stride_argb = -src_stride_argb;
1541
}
1542
// Coalesce rows.
1543
if (src_stride_argb == width * 4 &&
1544
dst_stride_argb == width * 4) {
1545
width *= height;
1546
height = 1;
1547
src_stride_argb = dst_stride_argb = 0;
1548
}
1549
#if defined(HAS_ARGBGRAYROW_SSSE3)
1550
if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1551
ARGBGrayRow = ARGBGrayRow_SSSE3;
1552
}
1553
#endif
1554
#if defined(HAS_ARGBGRAYROW_NEON)
1555
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
1556
ARGBGrayRow = ARGBGrayRow_NEON;
1557
}
1558
#endif
1559
1560
for (y = 0; y < height; ++y) {
1561
ARGBGrayRow(src_argb, dst_argb, width);
1562
src_argb += src_stride_argb;
1563
dst_argb += dst_stride_argb;
1564
}
1565
return 0;
1566
}
1567
1568
// Make a rectangle of ARGB gray scale.
1569
LIBYUV_API
1570
int ARGBGray(uint8* dst_argb, int dst_stride_argb,
1571
int dst_x, int dst_y,
1572
int width, int height) {
1573
int y;
1574
void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb,
1575
int width) = ARGBGrayRow_C;
1576
uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1577
if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
1578
return -1;
1579
}
1580
// Coalesce rows.
1581
if (dst_stride_argb == width * 4) {
1582
width *= height;
1583
height = 1;
1584
dst_stride_argb = 0;
1585
}
1586
#if defined(HAS_ARGBGRAYROW_SSSE3)
1587
if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1588
ARGBGrayRow = ARGBGrayRow_SSSE3;
1589
}
1590
#endif
1591
#if defined(HAS_ARGBGRAYROW_NEON)
1592
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
1593
ARGBGrayRow = ARGBGrayRow_NEON;
1594
}
1595
#endif
1596
for (y = 0; y < height; ++y) {
1597
ARGBGrayRow(dst, dst, width);
1598
dst += dst_stride_argb;
1599
}
1600
return 0;
1601
}
1602
1603
// Make a rectangle of ARGB Sepia tone.
1604
LIBYUV_API
1605
int ARGBSepia(uint8* dst_argb, int dst_stride_argb,
1606
int dst_x, int dst_y, int width, int height) {
1607
int y;
1608
void (*ARGBSepiaRow)(uint8* dst_argb, int width) = ARGBSepiaRow_C;
1609
uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1610
if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
1611
return -1;
1612
}
1613
// Coalesce rows.
1614
if (dst_stride_argb == width * 4) {
1615
width *= height;
1616
height = 1;
1617
dst_stride_argb = 0;
1618
}
1619
#if defined(HAS_ARGBSEPIAROW_SSSE3)
1620
if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1621
ARGBSepiaRow = ARGBSepiaRow_SSSE3;
1622
}
1623
#endif
1624
#if defined(HAS_ARGBSEPIAROW_NEON)
1625
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
1626
ARGBSepiaRow = ARGBSepiaRow_NEON;
1627
}
1628
#endif
1629
for (y = 0; y < height; ++y) {
1630
ARGBSepiaRow(dst, width);
1631
dst += dst_stride_argb;
1632
}
1633
return 0;
1634
}
1635
1636
// Apply a 4x4 matrix to each ARGB pixel.
1637
// Note: Normally for shading, but can be used to swizzle or invert.
1638
LIBYUV_API
1639
int ARGBColorMatrix(const uint8* src_argb, int src_stride_argb,
1640
uint8* dst_argb, int dst_stride_argb,
1641
const int8* matrix_argb,
1642
int width, int height) {
1643
int y;
1644
void (*ARGBColorMatrixRow)(const uint8* src_argb, uint8* dst_argb,
1645
const int8* matrix_argb, int width) = ARGBColorMatrixRow_C;
1646
if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) {
1647
return -1;
1648
}
1649
if (height < 0) {
1650
height = -height;
1651
src_argb = src_argb + (height - 1) * src_stride_argb;
1652
src_stride_argb = -src_stride_argb;
1653
}
1654
// Coalesce rows.
1655
if (src_stride_argb == width * 4 &&
1656
dst_stride_argb == width * 4) {
1657
width *= height;
1658
height = 1;
1659
src_stride_argb = dst_stride_argb = 0;
1660
}
1661
#if defined(HAS_ARGBCOLORMATRIXROW_SSSE3)
1662
if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1663
ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3;
1664
}
1665
#endif
1666
#if defined(HAS_ARGBCOLORMATRIXROW_NEON)
1667
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
1668
ARGBColorMatrixRow = ARGBColorMatrixRow_NEON;
1669
}
1670
#endif
1671
for (y = 0; y < height; ++y) {
1672
ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width);
1673
src_argb += src_stride_argb;
1674
dst_argb += dst_stride_argb;
1675
}
1676
return 0;
1677
}
1678
1679
// Apply a 4x3 matrix to each ARGB pixel.
1680
// Deprecated.
1681
LIBYUV_API
1682
int RGBColorMatrix(uint8* dst_argb, int dst_stride_argb,
1683
const int8* matrix_rgb,
1684
int dst_x, int dst_y, int width, int height) {
1685
SIMD_ALIGNED(int8 matrix_argb[16]);
1686
uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1687
if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 ||
1688
dst_x < 0 || dst_y < 0) {
1689
return -1;
1690
}
1691
1692
// Convert 4x3 7 bit matrix to 4x4 6 bit matrix.
1693
matrix_argb[0] = matrix_rgb[0] / 2;
1694
matrix_argb[1] = matrix_rgb[1] / 2;
1695
matrix_argb[2] = matrix_rgb[2] / 2;
1696
matrix_argb[3] = matrix_rgb[3] / 2;
1697
matrix_argb[4] = matrix_rgb[4] / 2;
1698
matrix_argb[5] = matrix_rgb[5] / 2;
1699
matrix_argb[6] = matrix_rgb[6] / 2;
1700
matrix_argb[7] = matrix_rgb[7] / 2;
1701
matrix_argb[8] = matrix_rgb[8] / 2;
1702
matrix_argb[9] = matrix_rgb[9] / 2;
1703
matrix_argb[10] = matrix_rgb[10] / 2;
1704
matrix_argb[11] = matrix_rgb[11] / 2;
1705
matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0;
1706
matrix_argb[15] = 64; // 1.0
1707
1708
return ARGBColorMatrix((const uint8*)(dst), dst_stride_argb,
1709
dst, dst_stride_argb,
1710
&matrix_argb[0], width, height);
1711
}
1712
1713
// Apply a color table each ARGB pixel.
1714
// Table contains 256 ARGB values.
1715
LIBYUV_API
1716
int ARGBColorTable(uint8* dst_argb, int dst_stride_argb,
1717
const uint8* table_argb,
1718
int dst_x, int dst_y, int width, int height) {
1719
int y;
1720
void (*ARGBColorTableRow)(uint8* dst_argb, const uint8* table_argb,
1721
int width) = ARGBColorTableRow_C;
1722
uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1723
if (!dst_argb || !table_argb || width <= 0 || height <= 0 ||
1724
dst_x < 0 || dst_y < 0) {
1725
return -1;
1726
}
1727
// Coalesce rows.
1728
if (dst_stride_argb == width * 4) {
1729
width *= height;
1730
height = 1;
1731
dst_stride_argb = 0;
1732
}
1733
#if defined(HAS_ARGBCOLORTABLEROW_X86)
1734
if (TestCpuFlag(kCpuHasX86)) {
1735
ARGBColorTableRow = ARGBColorTableRow_X86;
1736
}
1737
#endif
1738
for (y = 0; y < height; ++y) {
1739
ARGBColorTableRow(dst, table_argb, width);
1740
dst += dst_stride_argb;
1741
}
1742
return 0;
1743
}
1744
1745
// Apply a color table each ARGB pixel but preserve destination alpha.
1746
// Table contains 256 ARGB values.
1747
LIBYUV_API
1748
int RGBColorTable(uint8* dst_argb, int dst_stride_argb,
1749
const uint8* table_argb,
1750
int dst_x, int dst_y, int width, int height) {
1751
int y;
1752
void (*RGBColorTableRow)(uint8* dst_argb, const uint8* table_argb,
1753
int width) = RGBColorTableRow_C;
1754
uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1755
if (!dst_argb || !table_argb || width <= 0 || height <= 0 ||
1756
dst_x < 0 || dst_y < 0) {
1757
return -1;
1758
}
1759
// Coalesce rows.
1760
if (dst_stride_argb == width * 4) {
1761
width *= height;
1762
height = 1;
1763
dst_stride_argb = 0;
1764
}
1765
#if defined(HAS_RGBCOLORTABLEROW_X86)
1766
if (TestCpuFlag(kCpuHasX86)) {
1767
RGBColorTableRow = RGBColorTableRow_X86;
1768
}
1769
#endif
1770
for (y = 0; y < height; ++y) {
1771
RGBColorTableRow(dst, table_argb, width);
1772
dst += dst_stride_argb;
1773
}
1774
return 0;
1775
}
1776
1777
// ARGBQuantize is used to posterize art.
1778
// e.g. rgb / qvalue * qvalue + qvalue / 2
1779
// But the low levels implement efficiently with 3 parameters, and could be
1780
// used for other high level operations.
1781
// dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset;
1782
// where scale is 1 / interval_size as a fixed point value.
1783
// The divide is replaces with a multiply by reciprocal fixed point multiply.
1784
// Caveat - although SSE2 saturates, the C function does not and should be used
1785
// with care if doing anything but quantization.
1786
LIBYUV_API
1787
int ARGBQuantize(uint8* dst_argb, int dst_stride_argb,
1788
int scale, int interval_size, int interval_offset,
1789
int dst_x, int dst_y, int width, int height) {
1790
int y;
1791
void (*ARGBQuantizeRow)(uint8* dst_argb, int scale, int interval_size,
1792
int interval_offset, int width) = ARGBQuantizeRow_C;
1793
uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1794
if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 ||
1795
interval_size < 1 || interval_size > 255) {
1796
return -1;
1797
}
1798
// Coalesce rows.
1799
if (dst_stride_argb == width * 4) {
1800
width *= height;
1801
height = 1;
1802
dst_stride_argb = 0;
1803
}
1804
#if defined(HAS_ARGBQUANTIZEROW_SSE2)
1805
if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) {
1806
ARGBQuantizeRow = ARGBQuantizeRow_SSE2;
1807
}
1808
#endif
1809
#if defined(HAS_ARGBQUANTIZEROW_NEON)
1810
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
1811
ARGBQuantizeRow = ARGBQuantizeRow_NEON;
1812
}
1813
#endif
1814
for (y = 0; y < height; ++y) {
1815
ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width);
1816
dst += dst_stride_argb;
1817
}
1818
return 0;
1819
}
1820
1821
// Computes table of cumulative sum for image where the value is the sum
1822
// of all values above and to the left of the entry. Used by ARGBBlur.
1823
LIBYUV_API
1824
int ARGBComputeCumulativeSum(const uint8* src_argb, int src_stride_argb,
1825
int32* dst_cumsum, int dst_stride32_cumsum,
1826
int width, int height) {
1827
int y;
1828
void (*ComputeCumulativeSumRow)(const uint8* row, int32* cumsum,
1829
const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C;
1830
int32* previous_cumsum = dst_cumsum;
1831
if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) {
1832
return -1;
1833
}
1834
#if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
1835
if (TestCpuFlag(kCpuHasSSE2)) {
1836
ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
1837
}
1838
#endif
1839
memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4); // 4 int per pixel.
1840
for (y = 0; y < height; ++y) {
1841
ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width);
1842
previous_cumsum = dst_cumsum;
1843
dst_cumsum += dst_stride32_cumsum;
1844
src_argb += src_stride_argb;
1845
}
1846
return 0;
1847
}
1848
1849
// Blur ARGB image.
1850
// Caller should allocate CumulativeSum table of width * height * 16 bytes
1851
// aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory
1852
// as the buffer is treated as circular.
1853
LIBYUV_API
1854
int ARGBBlur(const uint8* src_argb, int src_stride_argb,
1855
uint8* dst_argb, int dst_stride_argb,
1856
int32* dst_cumsum, int dst_stride32_cumsum,
1857
int width, int height, int radius) {
1858
int y;
1859
void (*ComputeCumulativeSumRow)(const uint8 *row, int32 *cumsum,
1860
const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C;
1861
void (*CumulativeSumToAverageRow)(const int32* topleft, const int32* botleft,
1862
int width, int area, uint8* dst, int count) = CumulativeSumToAverageRow_C;
1863
int32* cumsum_bot_row;
1864
int32* max_cumsum_bot_row;
1865
int32* cumsum_top_row;
1866
1867
if (!src_argb || !dst_argb || width <= 0 || height == 0) {
1868
return -1;
1869
}
1870
if (height < 0) {
1871
height = -height;
1872
src_argb = src_argb + (height - 1) * src_stride_argb;
1873
src_stride_argb = -src_stride_argb;
1874
}
1875
if (radius > height) {
1876
radius = height;
1877
}
1878
if (radius > (width / 2 - 1)) {
1879
radius = width / 2 - 1;
1880
}
1881
if (radius <= 0) {
1882
return -1;
1883
}
1884
#if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
1885
if (TestCpuFlag(kCpuHasSSE2)) {
1886
ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
1887
CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2;
1888
}
1889
#endif
1890
// Compute enough CumulativeSum for first row to be blurred. After this
1891
// one row of CumulativeSum is updated at a time.
1892
ARGBComputeCumulativeSum(src_argb, src_stride_argb,
1893
dst_cumsum, dst_stride32_cumsum,
1894
width, radius);
1895
1896
src_argb = src_argb + radius * src_stride_argb;
1897
cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum];
1898
1899
max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum];
1900
cumsum_top_row = &dst_cumsum[0];
1901
1902
for (y = 0; y < height; ++y) {
1903
int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0;
1904
int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1);
1905
int area = radius * (bot_y - top_y);
1906
int boxwidth = radius * 4;
1907
int x;
1908
int n;
1909
1910
// Increment cumsum_top_row pointer with circular buffer wrap around.
1911
if (top_y) {
1912
cumsum_top_row += dst_stride32_cumsum;
1913
if (cumsum_top_row >= max_cumsum_bot_row) {
1914
cumsum_top_row = dst_cumsum;
1915
}
1916
}
1917
// Increment cumsum_bot_row pointer with circular buffer wrap around and
1918
// then fill in a row of CumulativeSum.
1919
if ((y + radius) < height) {
1920
const int32* prev_cumsum_bot_row = cumsum_bot_row;
1921
cumsum_bot_row += dst_stride32_cumsum;
1922
if (cumsum_bot_row >= max_cumsum_bot_row) {
1923
cumsum_bot_row = dst_cumsum;
1924
}
1925
ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row,
1926
width);
1927
src_argb += src_stride_argb;
1928
}
1929
1930
// Left clipped.
1931
for (x = 0; x < radius + 1; ++x) {
1932
CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row,
1933
boxwidth, area, &dst_argb[x * 4], 1);
1934
area += (bot_y - top_y);
1935
boxwidth += 4;
1936
}
1937
1938
// Middle unclipped.
1939
n = (width - 1) - radius - x + 1;
1940
CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row,
1941
boxwidth, area, &dst_argb[x * 4], n);
1942
1943
// Right clipped.
1944
for (x += n; x <= width - 1; ++x) {
1945
area -= (bot_y - top_y);
1946
boxwidth -= 4;
1947
CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4,
1948
cumsum_bot_row + (x - radius - 1) * 4,
1949
boxwidth, area, &dst_argb[x * 4], 1);
1950
}
1951
dst_argb += dst_stride_argb;
1952
}
1953
return 0;
1954
}
1955
1956
// Multiply ARGB image by a specified ARGB value.
1957
LIBYUV_API
1958
int ARGBShade(const uint8* src_argb, int src_stride_argb,
1959
uint8* dst_argb, int dst_stride_argb,
1960
int width, int height, uint32 value) {
1961
int y;
1962
void (*ARGBShadeRow)(const uint8* src_argb, uint8* dst_argb,
1963
int width, uint32 value) = ARGBShadeRow_C;
1964
if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) {
1965
return -1;
1966
}
1967
if (height < 0) {
1968
height = -height;
1969
src_argb = src_argb + (height - 1) * src_stride_argb;
1970
src_stride_argb = -src_stride_argb;
1971
}
1972
// Coalesce rows.
1973
if (src_stride_argb == width * 4 &&
1974
dst_stride_argb == width * 4) {
1975
width *= height;
1976
height = 1;
1977
src_stride_argb = dst_stride_argb = 0;
1978
}
1979
#if defined(HAS_ARGBSHADEROW_SSE2)
1980
if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) {
1981
ARGBShadeRow = ARGBShadeRow_SSE2;
1982
}
1983
#endif
1984
#if defined(HAS_ARGBSHADEROW_NEON)
1985
if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
1986
ARGBShadeRow = ARGBShadeRow_NEON;
1987
}
1988
#endif
1989
1990
for (y = 0; y < height; ++y) {
1991
ARGBShadeRow(src_argb, dst_argb, width, value);
1992
src_argb += src_stride_argb;
1993
dst_argb += dst_stride_argb;
1994
}
1995
return 0;
1996
}
1997
1998
// Interpolate 2 planes by specified amount (0 to 255).
1999
LIBYUV_API
2000
int InterpolatePlane(const uint8* src0, int src_stride0,
2001
const uint8* src1, int src_stride1,
2002
uint8* dst, int dst_stride,
2003
int width, int height, int interpolation) {
2004
int y;
2005
void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr,
2006
ptrdiff_t src_stride, int dst_width,
2007
int source_y_fraction) = InterpolateRow_C;
2008
if (!src0 || !src1 || !dst || width <= 0 || height == 0) {
2009
return -1;
2010
}
2011
// Negative height means invert the image.
2012
if (height < 0) {
2013
height = -height;
2014
dst = dst + (height - 1) * dst_stride;
2015
dst_stride = -dst_stride;
2016
}
2017
// Coalesce rows.
2018
if (src_stride0 == width &&
2019
src_stride1 == width &&
2020
dst_stride == width) {
2021
width *= height;
2022
height = 1;
2023
src_stride0 = src_stride1 = dst_stride = 0;
2024
}
2025
#if defined(HAS_INTERPOLATEROW_SSSE3)
2026
if (TestCpuFlag(kCpuHasSSSE3)) {
2027
InterpolateRow = InterpolateRow_Any_SSSE3;
2028
if (IS_ALIGNED(width, 16)) {
2029
InterpolateRow = InterpolateRow_SSSE3;
2030
}
2031
}
2032
#endif
2033
#if defined(HAS_INTERPOLATEROW_AVX2)
2034
if (TestCpuFlag(kCpuHasAVX2)) {
2035
InterpolateRow = InterpolateRow_Any_AVX2;
2036
if (IS_ALIGNED(width, 32)) {
2037
InterpolateRow = InterpolateRow_AVX2;
2038
}
2039
}
2040
#endif
2041
#if defined(HAS_INTERPOLATEROW_NEON)
2042
if (TestCpuFlag(kCpuHasNEON)) {
2043
InterpolateRow = InterpolateRow_Any_NEON;
2044
if (IS_ALIGNED(width, 16)) {
2045
InterpolateRow = InterpolateRow_NEON;
2046
}
2047
}
2048
#endif
2049
#if defined(HAS_INTERPOLATEROW_DSPR2)
2050
if (TestCpuFlag(kCpuHasDSPR2) &&
2051
IS_ALIGNED(src0, 4) && IS_ALIGNED(src_stride0, 4) &&
2052
IS_ALIGNED(src1, 4) && IS_ALIGNED(src_stride1, 4) &&
2053
IS_ALIGNED(dst, 4) && IS_ALIGNED(dst_stride, 4) &&
2054
IS_ALIGNED(width, 4)) {
2055
InterpolateRow = InterpolateRow_DSPR2;
2056
}
2057
#endif
2058
2059
for (y = 0; y < height; ++y) {
2060
InterpolateRow(dst, src0, src1 - src0, width, interpolation);
2061
src0 += src_stride0;
2062
src1 += src_stride1;
2063
dst += dst_stride;
2064
}
2065
return 0;
2066
}
2067
2068
// Interpolate 2 ARGB images by specified amount (0 to 255).
2069
LIBYUV_API
2070
int ARGBInterpolate(const uint8* src_argb0, int src_stride_argb0,
2071
const uint8* src_argb1, int src_stride_argb1,
2072
uint8* dst_argb, int dst_stride_argb,
2073
int width, int height, int interpolation) {
2074
return InterpolatePlane(src_argb0, src_stride_argb0,
2075
src_argb1, src_stride_argb1,
2076
dst_argb, dst_stride_argb,
2077
width * 4, height, interpolation);
2078
}
2079
2080
// Interpolate 2 YUV images by specified amount (0 to 255).
2081
LIBYUV_API
2082
int I420Interpolate(const uint8* src0_y, int src0_stride_y,
2083
const uint8* src0_u, int src0_stride_u,
2084
const uint8* src0_v, int src0_stride_v,
2085
const uint8* src1_y, int src1_stride_y,
2086
const uint8* src1_u, int src1_stride_u,
2087
const uint8* src1_v, int src1_stride_v,
2088
uint8* dst_y, int dst_stride_y,
2089
uint8* dst_u, int dst_stride_u,
2090
uint8* dst_v, int dst_stride_v,
2091
int width, int height, int interpolation) {
2092
int halfwidth = (width + 1) >> 1;
2093
int halfheight = (height + 1) >> 1;
2094
if (!src0_y || !src0_u || !src0_v ||
2095
!src1_y || !src1_u || !src1_v ||
2096
!dst_y || !dst_u || !dst_v ||
2097
width <= 0 || height == 0) {
2098
return -1;
2099
}
2100
InterpolatePlane(src0_y, src0_stride_y,
2101
src1_y, src1_stride_y,
2102
dst_y, dst_stride_y,
2103
width, height, interpolation);
2104
InterpolatePlane(src0_u, src0_stride_u,
2105
src1_u, src1_stride_u,
2106
dst_u, dst_stride_u,
2107
halfwidth, halfheight, interpolation);
2108
InterpolatePlane(src0_v, src0_stride_v,
2109
src1_v, src1_stride_v,
2110
dst_v, dst_stride_v,
2111
halfwidth, halfheight, interpolation);
2112
return 0;
2113
}
2114
2115
// Shuffle ARGB channel order. e.g. BGRA to ARGB.
2116
LIBYUV_API
2117
int ARGBShuffle(const uint8* src_bgra, int src_stride_bgra,
2118
uint8* dst_argb, int dst_stride_argb,
2119
const uint8* shuffler, int width, int height) {
2120
int y;
2121
void (*ARGBShuffleRow)(const uint8* src_bgra, uint8* dst_argb,
2122
const uint8* shuffler, int width) = ARGBShuffleRow_C;
2123
if (!src_bgra || !dst_argb ||
2124
width <= 0 || height == 0) {
2125
return -1;
2126
}
2127
// Negative height means invert the image.
2128
if (height < 0) {
2129
height = -height;
2130
src_bgra = src_bgra + (height - 1) * src_stride_bgra;
2131
src_stride_bgra = -src_stride_bgra;
2132
}
2133
// Coalesce rows.
2134
if (src_stride_bgra == width * 4 &&
2135
dst_stride_argb == width * 4) {
2136
width *= height;
2137
height = 1;
2138
src_stride_bgra = dst_stride_argb = 0;
2139
}
2140
#if defined(HAS_ARGBSHUFFLEROW_SSE2)
2141
if (TestCpuFlag(kCpuHasSSE2)) {
2142
ARGBShuffleRow = ARGBShuffleRow_Any_SSE2;
2143
if (IS_ALIGNED(width, 4)) {
2144
ARGBShuffleRow = ARGBShuffleRow_SSE2;
2145
}
2146
}
2147
#endif
2148
#if defined(HAS_ARGBSHUFFLEROW_SSSE3)
2149
if (TestCpuFlag(kCpuHasSSSE3)) {
2150
ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3;
2151
if (IS_ALIGNED(width, 8)) {
2152
ARGBShuffleRow = ARGBShuffleRow_SSSE3;
2153
}
2154
}
2155
#endif
2156
#if defined(HAS_ARGBSHUFFLEROW_AVX2)
2157
if (TestCpuFlag(kCpuHasAVX2)) {
2158
ARGBShuffleRow = ARGBShuffleRow_Any_AVX2;
2159
if (IS_ALIGNED(width, 16)) {
2160
ARGBShuffleRow = ARGBShuffleRow_AVX2;
2161
}
2162
}
2163
#endif
2164
#if defined(HAS_ARGBSHUFFLEROW_NEON)
2165
if (TestCpuFlag(kCpuHasNEON)) {
2166
ARGBShuffleRow = ARGBShuffleRow_Any_NEON;
2167
if (IS_ALIGNED(width, 4)) {
2168
ARGBShuffleRow = ARGBShuffleRow_NEON;
2169
}
2170
}
2171
#endif
2172
2173
for (y = 0; y < height; ++y) {
2174
ARGBShuffleRow(src_bgra, dst_argb, shuffler, width);
2175
src_bgra += src_stride_bgra;
2176
dst_argb += dst_stride_argb;
2177
}
2178
return 0;
2179
}
2180
2181
// Sobel ARGB effect.
2182
static int ARGBSobelize(const uint8* src_argb, int src_stride_argb,
2183
uint8* dst_argb, int dst_stride_argb,
2184
int width, int height,
2185
void (*SobelRow)(const uint8* src_sobelx,
2186
const uint8* src_sobely,
2187
uint8* dst, int width)) {
2188
int y;
2189
void (*ARGBToYJRow)(const uint8* src_argb, uint8* dst_g, int width) =
2190
ARGBToYJRow_C;
2191
void (*SobelYRow)(const uint8* src_y0, const uint8* src_y1,
2192
uint8* dst_sobely, int width) = SobelYRow_C;
2193
void (*SobelXRow)(const uint8* src_y0, const uint8* src_y1,
2194
const uint8* src_y2, uint8* dst_sobely, int width) =
2195
SobelXRow_C;
2196
const int kEdge = 16; // Extra pixels at start of row for extrude/align.
2197
if (!src_argb || !dst_argb || width <= 0 || height == 0) {
2198
return -1;
2199
}
2200
// Negative height means invert the image.
2201
if (height < 0) {
2202
height = -height;
2203
src_argb = src_argb + (height - 1) * src_stride_argb;
2204
src_stride_argb = -src_stride_argb;
2205
}
2206
2207
#if defined(HAS_ARGBTOYJROW_SSSE3)
2208
if (TestCpuFlag(kCpuHasSSSE3)) {
2209
ARGBToYJRow = ARGBToYJRow_Any_SSSE3;
2210
if (IS_ALIGNED(width, 16)) {
2211
ARGBToYJRow = ARGBToYJRow_SSSE3;
2212
}
2213
}
2214
#endif
2215
#if defined(HAS_ARGBTOYJROW_AVX2)
2216
if (TestCpuFlag(kCpuHasAVX2)) {
2217
ARGBToYJRow = ARGBToYJRow_Any_AVX2;
2218
if (IS_ALIGNED(width, 32)) {
2219
ARGBToYJRow = ARGBToYJRow_AVX2;
2220
}
2221
}
2222
#endif
2223
#if defined(HAS_ARGBTOYJROW_NEON)
2224
if (TestCpuFlag(kCpuHasNEON)) {
2225
ARGBToYJRow = ARGBToYJRow_Any_NEON;
2226
if (IS_ALIGNED(width, 8)) {
2227
ARGBToYJRow = ARGBToYJRow_NEON;
2228
}
2229
}
2230
#endif
2231
2232
#if defined(HAS_SOBELYROW_SSE2)
2233
if (TestCpuFlag(kCpuHasSSE2)) {
2234
SobelYRow = SobelYRow_SSE2;
2235
}
2236
#endif
2237
#if defined(HAS_SOBELYROW_NEON)
2238
if (TestCpuFlag(kCpuHasNEON)) {
2239
SobelYRow = SobelYRow_NEON;
2240
}
2241
#endif
2242
#if defined(HAS_SOBELXROW_SSE2)
2243
if (TestCpuFlag(kCpuHasSSE2)) {
2244
SobelXRow = SobelXRow_SSE2;
2245
}
2246
#endif
2247
#if defined(HAS_SOBELXROW_NEON)
2248
if (TestCpuFlag(kCpuHasNEON)) {
2249
SobelXRow = SobelXRow_NEON;
2250
}
2251
#endif
2252
{
2253
// 3 rows with edges before/after.
2254
const int kRowSize = (width + kEdge + 31) & ~31;
2255
align_buffer_64(rows, kRowSize * 2 + (kEdge + kRowSize * 3 + kEdge));
2256
uint8* row_sobelx = rows;
2257
uint8* row_sobely = rows + kRowSize;
2258
uint8* row_y = rows + kRowSize * 2;
2259
2260
// Convert first row.
2261
uint8* row_y0 = row_y + kEdge;
2262
uint8* row_y1 = row_y0 + kRowSize;
2263
uint8* row_y2 = row_y1 + kRowSize;
2264
ARGBToYJRow(src_argb, row_y0, width);
2265
row_y0[-1] = row_y0[0];
2266
memset(row_y0 + width, row_y0[width - 1], 16); // Extrude 16 for valgrind.
2267
ARGBToYJRow(src_argb, row_y1, width);
2268
row_y1[-1] = row_y1[0];
2269
memset(row_y1 + width, row_y1[width - 1], 16);
2270
memset(row_y2 + width, 0, 16);
2271
2272
for (y = 0; y < height; ++y) {
2273
// Convert next row of ARGB to G.
2274
if (y < (height - 1)) {
2275
src_argb += src_stride_argb;
2276
}
2277
ARGBToYJRow(src_argb, row_y2, width);
2278
row_y2[-1] = row_y2[0];
2279
row_y2[width] = row_y2[width - 1];
2280
2281
SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width);
2282
SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width);
2283
SobelRow(row_sobelx, row_sobely, dst_argb, width);
2284
2285
// Cycle thru circular queue of 3 row_y buffers.
2286
{
2287
uint8* row_yt = row_y0;
2288
row_y0 = row_y1;
2289
row_y1 = row_y2;
2290
row_y2 = row_yt;
2291
}
2292
2293
dst_argb += dst_stride_argb;
2294
}
2295
free_aligned_buffer_64(rows);
2296
}
2297
return 0;
2298
}
2299
2300
// Sobel ARGB effect.
2301
LIBYUV_API
2302
int ARGBSobel(const uint8* src_argb, int src_stride_argb,
2303
uint8* dst_argb, int dst_stride_argb,
2304
int width, int height) {
2305
void (*SobelRow)(const uint8* src_sobelx, const uint8* src_sobely,
2306
uint8* dst_argb, int width) = SobelRow_C;
2307
#if defined(HAS_SOBELROW_SSE2)
2308
if (TestCpuFlag(kCpuHasSSE2)) {
2309
SobelRow = SobelRow_Any_SSE2;
2310
if (IS_ALIGNED(width, 16)) {
2311
SobelRow = SobelRow_SSE2;
2312
}
2313
}
2314
#endif
2315
#if defined(HAS_SOBELROW_NEON)
2316
if (TestCpuFlag(kCpuHasNEON)) {
2317
SobelRow = SobelRow_Any_NEON;
2318
if (IS_ALIGNED(width, 8)) {
2319
SobelRow = SobelRow_NEON;
2320
}
2321
}
2322
#endif
2323
return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
2324
width, height, SobelRow);
2325
}
2326
2327
// Sobel ARGB effect with planar output.
2328
LIBYUV_API
2329
int ARGBSobelToPlane(const uint8* src_argb, int src_stride_argb,
2330
uint8* dst_y, int dst_stride_y,
2331
int width, int height) {
2332
void (*SobelToPlaneRow)(const uint8* src_sobelx, const uint8* src_sobely,
2333
uint8* dst_, int width) = SobelToPlaneRow_C;
2334
#if defined(HAS_SOBELTOPLANEROW_SSE2)
2335
if (TestCpuFlag(kCpuHasSSE2)) {
2336
SobelToPlaneRow = SobelToPlaneRow_Any_SSE2;
2337
if (IS_ALIGNED(width, 16)) {
2338
SobelToPlaneRow = SobelToPlaneRow_SSE2;
2339
}
2340
}
2341
#endif
2342
#if defined(HAS_SOBELTOPLANEROW_NEON)
2343
if (TestCpuFlag(kCpuHasNEON)) {
2344
SobelToPlaneRow = SobelToPlaneRow_Any_NEON;
2345
if (IS_ALIGNED(width, 16)) {
2346
SobelToPlaneRow = SobelToPlaneRow_NEON;
2347
}
2348
}
2349
#endif
2350
return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y,
2351
width, height, SobelToPlaneRow);
2352
}
2353
2354
// SobelXY ARGB effect.
2355
// Similar to Sobel, but also stores Sobel X in R and Sobel Y in B. G = Sobel.
2356
LIBYUV_API
2357
int ARGBSobelXY(const uint8* src_argb, int src_stride_argb,
2358
uint8* dst_argb, int dst_stride_argb,
2359
int width, int height) {
2360
void (*SobelXYRow)(const uint8* src_sobelx, const uint8* src_sobely,
2361
uint8* dst_argb, int width) = SobelXYRow_C;
2362
#if defined(HAS_SOBELXYROW_SSE2)
2363
if (TestCpuFlag(kCpuHasSSE2)) {
2364
SobelXYRow = SobelXYRow_Any_SSE2;
2365
if (IS_ALIGNED(width, 16)) {
2366
SobelXYRow = SobelXYRow_SSE2;
2367
}
2368
}
2369
#endif
2370
#if defined(HAS_SOBELXYROW_NEON)
2371
if (TestCpuFlag(kCpuHasNEON)) {
2372
SobelXYRow = SobelXYRow_Any_NEON;
2373
if (IS_ALIGNED(width, 8)) {
2374
SobelXYRow = SobelXYRow_NEON;
2375
}
2376
}
2377
#endif
2378
return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
2379
width, height, SobelXYRow);
2380
}
2381
2382
// Apply a 4x4 polynomial to each ARGB pixel.
2383
LIBYUV_API
2384
int ARGBPolynomial(const uint8* src_argb, int src_stride_argb,
2385
uint8* dst_argb, int dst_stride_argb,
2386
const float* poly,
2387
int width, int height) {
2388
int y;
2389
void (*ARGBPolynomialRow)(const uint8* src_argb,
2390
uint8* dst_argb, const float* poly,
2391
int width) = ARGBPolynomialRow_C;
2392
if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) {
2393
return -1;
2394
}
2395
// Negative height means invert the image.
2396
if (height < 0) {
2397
height = -height;
2398
src_argb = src_argb + (height - 1) * src_stride_argb;
2399
src_stride_argb = -src_stride_argb;
2400
}
2401
// Coalesce rows.
2402
if (src_stride_argb == width * 4 &&
2403
dst_stride_argb == width * 4) {
2404
width *= height;
2405
height = 1;
2406
src_stride_argb = dst_stride_argb = 0;
2407
}
2408
#if defined(HAS_ARGBPOLYNOMIALROW_SSE2)
2409
if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) {
2410
ARGBPolynomialRow = ARGBPolynomialRow_SSE2;
2411
}
2412
#endif
2413
#if defined(HAS_ARGBPOLYNOMIALROW_AVX2)
2414
if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) &&
2415
IS_ALIGNED(width, 2)) {
2416
ARGBPolynomialRow = ARGBPolynomialRow_AVX2;