1 /*
2 * Copyright 2012 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/convert_from.h"
12
13 #include "libyuv/basic_types.h"
14 #include "libyuv/convert.h" // For I420Copy
15 #include "libyuv/cpu_id.h"
16 #include "libyuv/planar_functions.h"
17 #include "libyuv/rotate.h"
18 #include "libyuv/row.h"
19 #include "libyuv/scale.h" // For ScalePlane()
20 #include "libyuv/video_common.h"
21
22 #ifdef __cplusplus
23 namespace libyuv {
24 extern "C" {
25 #endif
26
27 #define SUBSAMPLE(v, a, s) (v < 0) ? (-((-v + a) >> s)) : ((v + a) >> s)
Abs(int v)28 static __inline int Abs(int v) {
29 return v >= 0 ? v : -v;
30 }
31
32 // I420 To any I4xx YUV format with mirroring.
I420ToI4xx(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_y,int dst_stride_y,uint8 * dst_u,int dst_stride_u,uint8 * dst_v,int dst_stride_v,int src_y_width,int src_y_height,int dst_uv_width,int dst_uv_height)33 static int I420ToI4xx(const uint8* src_y,
34 int src_stride_y,
35 const uint8* src_u,
36 int src_stride_u,
37 const uint8* src_v,
38 int src_stride_v,
39 uint8* dst_y,
40 int dst_stride_y,
41 uint8* dst_u,
42 int dst_stride_u,
43 uint8* dst_v,
44 int dst_stride_v,
45 int src_y_width,
46 int src_y_height,
47 int dst_uv_width,
48 int dst_uv_height) {
49 const int dst_y_width = Abs(src_y_width);
50 const int dst_y_height = Abs(src_y_height);
51 const int src_uv_width = SUBSAMPLE(src_y_width, 1, 1);
52 const int src_uv_height = SUBSAMPLE(src_y_height, 1, 1);
53 if (src_y_width == 0 || src_y_height == 0 || dst_uv_width <= 0 ||
54 dst_uv_height <= 0) {
55 return -1;
56 }
57 if (dst_y) {
58 ScalePlane(src_y, src_stride_y, src_y_width, src_y_height, dst_y,
59 dst_stride_y, dst_y_width, dst_y_height, kFilterBilinear);
60 }
61 ScalePlane(src_u, src_stride_u, src_uv_width, src_uv_height, dst_u,
62 dst_stride_u, dst_uv_width, dst_uv_height, kFilterBilinear);
63 ScalePlane(src_v, src_stride_v, src_uv_width, src_uv_height, dst_v,
64 dst_stride_v, dst_uv_width, dst_uv_height, kFilterBilinear);
65 return 0;
66 }
67
68 // 420 chroma is 1/2 width, 1/2 height
69 // 422 chroma is 1/2 width, 1x height
70 LIBYUV_API
I420ToI422(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_y,int dst_stride_y,uint8 * dst_u,int dst_stride_u,uint8 * dst_v,int dst_stride_v,int width,int height)71 int I420ToI422(const uint8* src_y,
72 int src_stride_y,
73 const uint8* src_u,
74 int src_stride_u,
75 const uint8* src_v,
76 int src_stride_v,
77 uint8* dst_y,
78 int dst_stride_y,
79 uint8* dst_u,
80 int dst_stride_u,
81 uint8* dst_v,
82 int dst_stride_v,
83 int width,
84 int height) {
85 const int dst_uv_width = (Abs(width) + 1) >> 1;
86 const int dst_uv_height = Abs(height);
87 return I420ToI4xx(src_y, src_stride_y, src_u, src_stride_u, src_v,
88 src_stride_v, dst_y, dst_stride_y, dst_u, dst_stride_u,
89 dst_v, dst_stride_v, width, height, dst_uv_width,
90 dst_uv_height);
91 }
92
93 // 420 chroma is 1/2 width, 1/2 height
94 // 444 chroma is 1x width, 1x height
95 LIBYUV_API
I420ToI444(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_y,int dst_stride_y,uint8 * dst_u,int dst_stride_u,uint8 * dst_v,int dst_stride_v,int width,int height)96 int I420ToI444(const uint8* src_y,
97 int src_stride_y,
98 const uint8* src_u,
99 int src_stride_u,
100 const uint8* src_v,
101 int src_stride_v,
102 uint8* dst_y,
103 int dst_stride_y,
104 uint8* dst_u,
105 int dst_stride_u,
106 uint8* dst_v,
107 int dst_stride_v,
108 int width,
109 int height) {
110 const int dst_uv_width = Abs(width);
111 const int dst_uv_height = Abs(height);
112 return I420ToI4xx(src_y, src_stride_y, src_u, src_stride_u, src_v,
113 src_stride_v, dst_y, dst_stride_y, dst_u, dst_stride_u,
114 dst_v, dst_stride_v, width, height, dst_uv_width,
115 dst_uv_height);
116 }
117
118 // Copy to I400. Source can be I420,422,444,400,NV12,NV21
119 LIBYUV_API
I400Copy(const uint8 * src_y,int src_stride_y,uint8 * dst_y,int dst_stride_y,int width,int height)120 int I400Copy(const uint8* src_y,
121 int src_stride_y,
122 uint8* dst_y,
123 int dst_stride_y,
124 int width,
125 int height) {
126 if (!src_y || !dst_y || width <= 0 || height == 0) {
127 return -1;
128 }
129 // Negative height means invert the image.
130 if (height < 0) {
131 height = -height;
132 src_y = src_y + (height - 1) * src_stride_y;
133 src_stride_y = -src_stride_y;
134 }
135 CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
136 return 0;
137 }
138
139 LIBYUV_API
I422ToYUY2(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_yuy2,int dst_stride_yuy2,int width,int height)140 int I422ToYUY2(const uint8* src_y,
141 int src_stride_y,
142 const uint8* src_u,
143 int src_stride_u,
144 const uint8* src_v,
145 int src_stride_v,
146 uint8* dst_yuy2,
147 int dst_stride_yuy2,
148 int width,
149 int height) {
150 int y;
151 void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
152 const uint8* src_v, uint8* dst_yuy2, int width) =
153 I422ToYUY2Row_C;
154 if (!src_y || !src_u || !src_v || !dst_yuy2 || width <= 0 || height == 0) {
155 return -1;
156 }
157 // Negative height means invert the image.
158 if (height < 0) {
159 height = -height;
160 dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2;
161 dst_stride_yuy2 = -dst_stride_yuy2;
162 }
163 // Coalesce rows.
164 if (src_stride_y == width && src_stride_u * 2 == width &&
165 src_stride_v * 2 == width && dst_stride_yuy2 == width * 2) {
166 width *= height;
167 height = 1;
168 src_stride_y = src_stride_u = src_stride_v = dst_stride_yuy2 = 0;
169 }
170 #if defined(HAS_I422TOYUY2ROW_SSE2)
171 if (TestCpuFlag(kCpuHasSSE2)) {
172 I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
173 if (IS_ALIGNED(width, 16)) {
174 I422ToYUY2Row = I422ToYUY2Row_SSE2;
175 }
176 }
177 #endif
178 #if defined(HAS_I422TOYUY2ROW_NEON)
179 if (TestCpuFlag(kCpuHasNEON)) {
180 I422ToYUY2Row = I422ToYUY2Row_Any_NEON;
181 if (IS_ALIGNED(width, 16)) {
182 I422ToYUY2Row = I422ToYUY2Row_NEON;
183 }
184 }
185 #endif
186
187 for (y = 0; y < height; ++y) {
188 I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
189 src_y += src_stride_y;
190 src_u += src_stride_u;
191 src_v += src_stride_v;
192 dst_yuy2 += dst_stride_yuy2;
193 }
194 return 0;
195 }
196
197 LIBYUV_API
I420ToYUY2(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_yuy2,int dst_stride_yuy2,int width,int height)198 int I420ToYUY2(const uint8* src_y,
199 int src_stride_y,
200 const uint8* src_u,
201 int src_stride_u,
202 const uint8* src_v,
203 int src_stride_v,
204 uint8* dst_yuy2,
205 int dst_stride_yuy2,
206 int width,
207 int height) {
208 int y;
209 void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
210 const uint8* src_v, uint8* dst_yuy2, int width) =
211 I422ToYUY2Row_C;
212 if (!src_y || !src_u || !src_v || !dst_yuy2 || width <= 0 || height == 0) {
213 return -1;
214 }
215 // Negative height means invert the image.
216 if (height < 0) {
217 height = -height;
218 dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2;
219 dst_stride_yuy2 = -dst_stride_yuy2;
220 }
221 #if defined(HAS_I422TOYUY2ROW_SSE2)
222 if (TestCpuFlag(kCpuHasSSE2)) {
223 I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
224 if (IS_ALIGNED(width, 16)) {
225 I422ToYUY2Row = I422ToYUY2Row_SSE2;
226 }
227 }
228 #endif
229 #if defined(HAS_I422TOYUY2ROW_NEON)
230 if (TestCpuFlag(kCpuHasNEON)) {
231 I422ToYUY2Row = I422ToYUY2Row_Any_NEON;
232 if (IS_ALIGNED(width, 16)) {
233 I422ToYUY2Row = I422ToYUY2Row_NEON;
234 }
235 }
236 #endif
237 #if defined(HAS_I422TOYUY2ROW_MSA)
238 if (TestCpuFlag(kCpuHasMSA)) {
239 I422ToYUY2Row = I422ToYUY2Row_Any_MSA;
240 if (IS_ALIGNED(width, 32)) {
241 I422ToYUY2Row = I422ToYUY2Row_MSA;
242 }
243 }
244 #endif
245
246 for (y = 0; y < height - 1; y += 2) {
247 I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
248 I422ToYUY2Row(src_y + src_stride_y, src_u, src_v,
249 dst_yuy2 + dst_stride_yuy2, width);
250 src_y += src_stride_y * 2;
251 src_u += src_stride_u;
252 src_v += src_stride_v;
253 dst_yuy2 += dst_stride_yuy2 * 2;
254 }
255 if (height & 1) {
256 I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
257 }
258 return 0;
259 }
260
261 LIBYUV_API
I422ToUYVY(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_uyvy,int dst_stride_uyvy,int width,int height)262 int I422ToUYVY(const uint8* src_y,
263 int src_stride_y,
264 const uint8* src_u,
265 int src_stride_u,
266 const uint8* src_v,
267 int src_stride_v,
268 uint8* dst_uyvy,
269 int dst_stride_uyvy,
270 int width,
271 int height) {
272 int y;
273 void (*I422ToUYVYRow)(const uint8* src_y, const uint8* src_u,
274 const uint8* src_v, uint8* dst_uyvy, int width) =
275 I422ToUYVYRow_C;
276 if (!src_y || !src_u || !src_v || !dst_uyvy || width <= 0 || height == 0) {
277 return -1;
278 }
279 // Negative height means invert the image.
280 if (height < 0) {
281 height = -height;
282 dst_uyvy = dst_uyvy + (height - 1) * dst_stride_uyvy;
283 dst_stride_uyvy = -dst_stride_uyvy;
284 }
285 // Coalesce rows.
286 if (src_stride_y == width && src_stride_u * 2 == width &&
287 src_stride_v * 2 == width && dst_stride_uyvy == width * 2) {
288 width *= height;
289 height = 1;
290 src_stride_y = src_stride_u = src_stride_v = dst_stride_uyvy = 0;
291 }
292 #if defined(HAS_I422TOUYVYROW_SSE2)
293 if (TestCpuFlag(kCpuHasSSE2)) {
294 I422ToUYVYRow = I422ToUYVYRow_Any_SSE2;
295 if (IS_ALIGNED(width, 16)) {
296 I422ToUYVYRow = I422ToUYVYRow_SSE2;
297 }
298 }
299 #endif
300 #if defined(HAS_I422TOUYVYROW_NEON)
301 if (TestCpuFlag(kCpuHasNEON)) {
302 I422ToUYVYRow = I422ToUYVYRow_Any_NEON;
303 if (IS_ALIGNED(width, 16)) {
304 I422ToUYVYRow = I422ToUYVYRow_NEON;
305 }
306 }
307 #endif
308 #if defined(HAS_I422TOUYVYROW_MSA)
309 if (TestCpuFlag(kCpuHasMSA)) {
310 I422ToUYVYRow = I422ToUYVYRow_Any_MSA;
311 if (IS_ALIGNED(width, 32)) {
312 I422ToUYVYRow = I422ToUYVYRow_MSA;
313 }
314 }
315 #endif
316
317 for (y = 0; y < height; ++y) {
318 I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
319 src_y += src_stride_y;
320 src_u += src_stride_u;
321 src_v += src_stride_v;
322 dst_uyvy += dst_stride_uyvy;
323 }
324 return 0;
325 }
326
327 LIBYUV_API
I420ToUYVY(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_uyvy,int dst_stride_uyvy,int width,int height)328 int I420ToUYVY(const uint8* src_y,
329 int src_stride_y,
330 const uint8* src_u,
331 int src_stride_u,
332 const uint8* src_v,
333 int src_stride_v,
334 uint8* dst_uyvy,
335 int dst_stride_uyvy,
336 int width,
337 int height) {
338 int y;
339 void (*I422ToUYVYRow)(const uint8* src_y, const uint8* src_u,
340 const uint8* src_v, uint8* dst_uyvy, int width) =
341 I422ToUYVYRow_C;
342 if (!src_y || !src_u || !src_v || !dst_uyvy || width <= 0 || height == 0) {
343 return -1;
344 }
345 // Negative height means invert the image.
346 if (height < 0) {
347 height = -height;
348 dst_uyvy = dst_uyvy + (height - 1) * dst_stride_uyvy;
349 dst_stride_uyvy = -dst_stride_uyvy;
350 }
351 #if defined(HAS_I422TOUYVYROW_SSE2)
352 if (TestCpuFlag(kCpuHasSSE2)) {
353 I422ToUYVYRow = I422ToUYVYRow_Any_SSE2;
354 if (IS_ALIGNED(width, 16)) {
355 I422ToUYVYRow = I422ToUYVYRow_SSE2;
356 }
357 }
358 #endif
359 #if defined(HAS_I422TOUYVYROW_NEON)
360 if (TestCpuFlag(kCpuHasNEON)) {
361 I422ToUYVYRow = I422ToUYVYRow_Any_NEON;
362 if (IS_ALIGNED(width, 16)) {
363 I422ToUYVYRow = I422ToUYVYRow_NEON;
364 }
365 }
366 #endif
367 #if defined(HAS_I422TOUYVYROW_MSA)
368 if (TestCpuFlag(kCpuHasMSA)) {
369 I422ToUYVYRow = I422ToUYVYRow_Any_MSA;
370 if (IS_ALIGNED(width, 32)) {
371 I422ToUYVYRow = I422ToUYVYRow_MSA;
372 }
373 }
374 #endif
375
376 for (y = 0; y < height - 1; y += 2) {
377 I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
378 I422ToUYVYRow(src_y + src_stride_y, src_u, src_v,
379 dst_uyvy + dst_stride_uyvy, width);
380 src_y += src_stride_y * 2;
381 src_u += src_stride_u;
382 src_v += src_stride_v;
383 dst_uyvy += dst_stride_uyvy * 2;
384 }
385 if (height & 1) {
386 I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
387 }
388 return 0;
389 }
390
391 // TODO(fbarchard): test negative height for invert.
392 LIBYUV_API
I420ToNV12(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_y,int dst_stride_y,uint8 * dst_uv,int dst_stride_uv,int width,int height)393 int I420ToNV12(const uint8* src_y,
394 int src_stride_y,
395 const uint8* src_u,
396 int src_stride_u,
397 const uint8* src_v,
398 int src_stride_v,
399 uint8* dst_y,
400 int dst_stride_y,
401 uint8* dst_uv,
402 int dst_stride_uv,
403 int width,
404 int height) {
405 if (!src_y || !src_u || !src_v || !dst_y || !dst_uv || width <= 0 ||
406 height == 0) {
407 return -1;
408 }
409 int halfwidth = (width + 1) / 2;
410 int halfheight = height > 0 ? (height + 1) / 2 : (height - 1) / 2;
411 if (dst_y) {
412 CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
413 }
414 MergeUVPlane(src_u, src_stride_u, src_v, src_stride_v, dst_uv, dst_stride_uv,
415 halfwidth, halfheight);
416 return 0;
417 }
418
419 LIBYUV_API
I420ToNV21(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_y,int dst_stride_y,uint8 * dst_vu,int dst_stride_vu,int width,int height)420 int I420ToNV21(const uint8* src_y,
421 int src_stride_y,
422 const uint8* src_u,
423 int src_stride_u,
424 const uint8* src_v,
425 int src_stride_v,
426 uint8* dst_y,
427 int dst_stride_y,
428 uint8* dst_vu,
429 int dst_stride_vu,
430 int width,
431 int height) {
432 return I420ToNV12(src_y, src_stride_y, src_v, src_stride_v, src_u,
433 src_stride_u, dst_y, dst_stride_y, dst_vu, dst_stride_vu,
434 width, height);
435 }
436
437 // Convert I422 to RGBA with matrix
I420ToRGBAMatrix(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgba,int dst_stride_rgba,const struct YuvConstants * yuvconstants,int width,int height)438 static int I420ToRGBAMatrix(const uint8* src_y,
439 int src_stride_y,
440 const uint8* src_u,
441 int src_stride_u,
442 const uint8* src_v,
443 int src_stride_v,
444 uint8* dst_rgba,
445 int dst_stride_rgba,
446 const struct YuvConstants* yuvconstants,
447 int width,
448 int height) {
449 int y;
450 void (*I422ToRGBARow)(const uint8* y_buf, const uint8* u_buf,
451 const uint8* v_buf, uint8* rgb_buf,
452 const struct YuvConstants* yuvconstants, int width) =
453 I422ToRGBARow_C;
454 if (!src_y || !src_u || !src_v || !dst_rgba || width <= 0 || height == 0) {
455 return -1;
456 }
457 // Negative height means invert the image.
458 if (height < 0) {
459 height = -height;
460 dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba;
461 dst_stride_rgba = -dst_stride_rgba;
462 }
463 #if defined(HAS_I422TORGBAROW_SSSE3)
464 if (TestCpuFlag(kCpuHasSSSE3)) {
465 I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
466 if (IS_ALIGNED(width, 8)) {
467 I422ToRGBARow = I422ToRGBARow_SSSE3;
468 }
469 }
470 #endif
471 #if defined(HAS_I422TORGBAROW_AVX2)
472 if (TestCpuFlag(kCpuHasAVX2)) {
473 I422ToRGBARow = I422ToRGBARow_Any_AVX2;
474 if (IS_ALIGNED(width, 16)) {
475 I422ToRGBARow = I422ToRGBARow_AVX2;
476 }
477 }
478 #endif
479 #if defined(HAS_I422TORGBAROW_NEON)
480 if (TestCpuFlag(kCpuHasNEON)) {
481 I422ToRGBARow = I422ToRGBARow_Any_NEON;
482 if (IS_ALIGNED(width, 8)) {
483 I422ToRGBARow = I422ToRGBARow_NEON;
484 }
485 }
486 #endif
487 #if defined(HAS_I422TORGBAROW_DSPR2)
488 if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(width, 4) &&
489 IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
490 IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
491 IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) &&
492 IS_ALIGNED(dst_rgba, 4) && IS_ALIGNED(dst_stride_rgba, 4)) {
493 I422ToRGBARow = I422ToRGBARow_DSPR2;
494 }
495 #endif
496 #if defined(HAS_I422TORGBAROW_MSA)
497 if (TestCpuFlag(kCpuHasMSA)) {
498 I422ToRGBARow = I422ToRGBARow_Any_MSA;
499 if (IS_ALIGNED(width, 8)) {
500 I422ToRGBARow = I422ToRGBARow_MSA;
501 }
502 }
503 #endif
504
505 for (y = 0; y < height; ++y) {
506 I422ToRGBARow(src_y, src_u, src_v, dst_rgba, yuvconstants, width);
507 dst_rgba += dst_stride_rgba;
508 src_y += src_stride_y;
509 if (y & 1) {
510 src_u += src_stride_u;
511 src_v += src_stride_v;
512 }
513 }
514 return 0;
515 }
516
517 // Convert I420 to RGBA.
518 LIBYUV_API
I420ToRGBA(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgba,int dst_stride_rgba,int width,int height)519 int I420ToRGBA(const uint8* src_y,
520 int src_stride_y,
521 const uint8* src_u,
522 int src_stride_u,
523 const uint8* src_v,
524 int src_stride_v,
525 uint8* dst_rgba,
526 int dst_stride_rgba,
527 int width,
528 int height) {
529 return I420ToRGBAMatrix(src_y, src_stride_y, src_u, src_stride_u, src_v,
530 src_stride_v, dst_rgba, dst_stride_rgba,
531 &kYuvI601Constants, width, height);
532 }
533
534 // Convert I420 to BGRA.
535 LIBYUV_API
I420ToBGRA(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_bgra,int dst_stride_bgra,int width,int height)536 int I420ToBGRA(const uint8* src_y,
537 int src_stride_y,
538 const uint8* src_u,
539 int src_stride_u,
540 const uint8* src_v,
541 int src_stride_v,
542 uint8* dst_bgra,
543 int dst_stride_bgra,
544 int width,
545 int height) {
546 return I420ToRGBAMatrix(src_y, src_stride_y, src_v,
547 src_stride_v, // Swap U and V
548 src_u, src_stride_u, dst_bgra, dst_stride_bgra,
549 &kYvuI601Constants, // Use Yvu matrix
550 width, height);
551 }
552
553 // Convert I420 to RGB24 with matrix
I420ToRGB24Matrix(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb24,int dst_stride_rgb24,const struct YuvConstants * yuvconstants,int width,int height)554 static int I420ToRGB24Matrix(const uint8* src_y,
555 int src_stride_y,
556 const uint8* src_u,
557 int src_stride_u,
558 const uint8* src_v,
559 int src_stride_v,
560 uint8* dst_rgb24,
561 int dst_stride_rgb24,
562 const struct YuvConstants* yuvconstants,
563 int width,
564 int height) {
565 int y;
566 void (*I422ToRGB24Row)(const uint8* y_buf, const uint8* u_buf,
567 const uint8* v_buf, uint8* rgb_buf,
568 const struct YuvConstants* yuvconstants, int width) =
569 I422ToRGB24Row_C;
570 if (!src_y || !src_u || !src_v || !dst_rgb24 || width <= 0 || height == 0) {
571 return -1;
572 }
573 // Negative height means invert the image.
574 if (height < 0) {
575 height = -height;
576 dst_rgb24 = dst_rgb24 + (height - 1) * dst_stride_rgb24;
577 dst_stride_rgb24 = -dst_stride_rgb24;
578 }
579 #if defined(HAS_I422TORGB24ROW_SSSE3)
580 if (TestCpuFlag(kCpuHasSSSE3)) {
581 I422ToRGB24Row = I422ToRGB24Row_Any_SSSE3;
582 if (IS_ALIGNED(width, 8)) {
583 I422ToRGB24Row = I422ToRGB24Row_SSSE3;
584 }
585 }
586 #endif
587 #if defined(HAS_I422TORGB24ROW_AVX2)
588 if (TestCpuFlag(kCpuHasAVX2)) {
589 I422ToRGB24Row = I422ToRGB24Row_Any_AVX2;
590 if (IS_ALIGNED(width, 16)) {
591 I422ToRGB24Row = I422ToRGB24Row_AVX2;
592 }
593 }
594 #endif
595 #if defined(HAS_I422TORGB24ROW_NEON)
596 if (TestCpuFlag(kCpuHasNEON)) {
597 I422ToRGB24Row = I422ToRGB24Row_Any_NEON;
598 if (IS_ALIGNED(width, 8)) {
599 I422ToRGB24Row = I422ToRGB24Row_NEON;
600 }
601 }
602 #endif
603 #if defined(HAS_I422TORGB24ROW_MSA)
604 if (TestCpuFlag(kCpuHasMSA)) {
605 I422ToRGB24Row = I422ToRGB24Row_Any_MSA;
606 if (IS_ALIGNED(width, 16)) {
607 I422ToRGB24Row = I422ToRGB24Row_MSA;
608 }
609 }
610 #endif
611
612 for (y = 0; y < height; ++y) {
613 I422ToRGB24Row(src_y, src_u, src_v, dst_rgb24, yuvconstants, width);
614 dst_rgb24 += dst_stride_rgb24;
615 src_y += src_stride_y;
616 if (y & 1) {
617 src_u += src_stride_u;
618 src_v += src_stride_v;
619 }
620 }
621 return 0;
622 }
623
624 // Convert I420 to RGB24.
625 LIBYUV_API
I420ToRGB24(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb24,int dst_stride_rgb24,int width,int height)626 int I420ToRGB24(const uint8* src_y,
627 int src_stride_y,
628 const uint8* src_u,
629 int src_stride_u,
630 const uint8* src_v,
631 int src_stride_v,
632 uint8* dst_rgb24,
633 int dst_stride_rgb24,
634 int width,
635 int height) {
636 return I420ToRGB24Matrix(src_y, src_stride_y, src_u, src_stride_u, src_v,
637 src_stride_v, dst_rgb24, dst_stride_rgb24,
638 &kYuvI601Constants, width, height);
639 }
640
641 // Convert I420 to RAW.
642 LIBYUV_API
I420ToRAW(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_raw,int dst_stride_raw,int width,int height)643 int I420ToRAW(const uint8* src_y,
644 int src_stride_y,
645 const uint8* src_u,
646 int src_stride_u,
647 const uint8* src_v,
648 int src_stride_v,
649 uint8* dst_raw,
650 int dst_stride_raw,
651 int width,
652 int height) {
653 return I420ToRGB24Matrix(src_y, src_stride_y, src_v,
654 src_stride_v, // Swap U and V
655 src_u, src_stride_u, dst_raw, dst_stride_raw,
656 &kYvuI601Constants, // Use Yvu matrix
657 width, height);
658 }
659
660 // Convert I420 to ARGB1555.
661 LIBYUV_API
I420ToARGB1555(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_argb1555,int dst_stride_argb1555,int width,int height)662 int I420ToARGB1555(const uint8* src_y,
663 int src_stride_y,
664 const uint8* src_u,
665 int src_stride_u,
666 const uint8* src_v,
667 int src_stride_v,
668 uint8* dst_argb1555,
669 int dst_stride_argb1555,
670 int width,
671 int height) {
672 int y;
673 void (*I422ToARGB1555Row)(const uint8* y_buf, const uint8* u_buf,
674 const uint8* v_buf, uint8* rgb_buf,
675 const struct YuvConstants* yuvconstants,
676 int width) = I422ToARGB1555Row_C;
677 if (!src_y || !src_u || !src_v || !dst_argb1555 || width <= 0 ||
678 height == 0) {
679 return -1;
680 }
681 // Negative height means invert the image.
682 if (height < 0) {
683 height = -height;
684 dst_argb1555 = dst_argb1555 + (height - 1) * dst_stride_argb1555;
685 dst_stride_argb1555 = -dst_stride_argb1555;
686 }
687 #if defined(HAS_I422TOARGB1555ROW_SSSE3)
688 if (TestCpuFlag(kCpuHasSSSE3)) {
689 I422ToARGB1555Row = I422ToARGB1555Row_Any_SSSE3;
690 if (IS_ALIGNED(width, 8)) {
691 I422ToARGB1555Row = I422ToARGB1555Row_SSSE3;
692 }
693 }
694 #endif
695 #if defined(HAS_I422TOARGB1555ROW_AVX2)
696 if (TestCpuFlag(kCpuHasAVX2)) {
697 I422ToARGB1555Row = I422ToARGB1555Row_Any_AVX2;
698 if (IS_ALIGNED(width, 16)) {
699 I422ToARGB1555Row = I422ToARGB1555Row_AVX2;
700 }
701 }
702 #endif
703 #if defined(HAS_I422TOARGB1555ROW_NEON)
704 if (TestCpuFlag(kCpuHasNEON)) {
705 I422ToARGB1555Row = I422ToARGB1555Row_Any_NEON;
706 if (IS_ALIGNED(width, 8)) {
707 I422ToARGB1555Row = I422ToARGB1555Row_NEON;
708 }
709 }
710 #endif
711 #if defined(HAS_I422TOARGB1555ROW_DSPR2)
712 if (TestCpuFlag(kCpuHasDSPR2)) {
713 I422ToARGB1555Row = I422ToARGB1555Row_Any_DSPR2;
714 if (IS_ALIGNED(width, 4)) {
715 I422ToARGB1555Row = I422ToARGB1555Row_DSPR2;
716 }
717 }
718 #endif
719 #if defined(HAS_I422TOARGB1555ROW_MSA)
720 if (TestCpuFlag(kCpuHasMSA)) {
721 I422ToARGB1555Row = I422ToARGB1555Row_Any_MSA;
722 if (IS_ALIGNED(width, 8)) {
723 I422ToARGB1555Row = I422ToARGB1555Row_MSA;
724 }
725 }
726 #endif
727
728 for (y = 0; y < height; ++y) {
729 I422ToARGB1555Row(src_y, src_u, src_v, dst_argb1555, &kYuvI601Constants,
730 width);
731 dst_argb1555 += dst_stride_argb1555;
732 src_y += src_stride_y;
733 if (y & 1) {
734 src_u += src_stride_u;
735 src_v += src_stride_v;
736 }
737 }
738 return 0;
739 }
740
741 // Convert I420 to ARGB4444.
742 LIBYUV_API
I420ToARGB4444(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_argb4444,int dst_stride_argb4444,int width,int height)743 int I420ToARGB4444(const uint8* src_y,
744 int src_stride_y,
745 const uint8* src_u,
746 int src_stride_u,
747 const uint8* src_v,
748 int src_stride_v,
749 uint8* dst_argb4444,
750 int dst_stride_argb4444,
751 int width,
752 int height) {
753 int y;
754 void (*I422ToARGB4444Row)(const uint8* y_buf, const uint8* u_buf,
755 const uint8* v_buf, uint8* rgb_buf,
756 const struct YuvConstants* yuvconstants,
757 int width) = I422ToARGB4444Row_C;
758 if (!src_y || !src_u || !src_v || !dst_argb4444 || width <= 0 ||
759 height == 0) {
760 return -1;
761 }
762 // Negative height means invert the image.
763 if (height < 0) {
764 height = -height;
765 dst_argb4444 = dst_argb4444 + (height - 1) * dst_stride_argb4444;
766 dst_stride_argb4444 = -dst_stride_argb4444;
767 }
768 #if defined(HAS_I422TOARGB4444ROW_SSSE3)
769 if (TestCpuFlag(kCpuHasSSSE3)) {
770 I422ToARGB4444Row = I422ToARGB4444Row_Any_SSSE3;
771 if (IS_ALIGNED(width, 8)) {
772 I422ToARGB4444Row = I422ToARGB4444Row_SSSE3;
773 }
774 }
775 #endif
776 #if defined(HAS_I422TOARGB4444ROW_AVX2)
777 if (TestCpuFlag(kCpuHasAVX2)) {
778 I422ToARGB4444Row = I422ToARGB4444Row_Any_AVX2;
779 if (IS_ALIGNED(width, 16)) {
780 I422ToARGB4444Row = I422ToARGB4444Row_AVX2;
781 }
782 }
783 #endif
784 #if defined(HAS_I422TOARGB4444ROW_NEON)
785 if (TestCpuFlag(kCpuHasNEON)) {
786 I422ToARGB4444Row = I422ToARGB4444Row_Any_NEON;
787 if (IS_ALIGNED(width, 8)) {
788 I422ToARGB4444Row = I422ToARGB4444Row_NEON;
789 }
790 }
791 #endif
792 #if defined(HAS_I422TOARGB4444ROW_DSPR2)
793 if (TestCpuFlag(kCpuHasDSPR2)) {
794 I422ToARGB4444Row = I422ToARGB4444Row_Any_DSPR2;
795 if (IS_ALIGNED(width, 4)) {
796 I422ToARGB4444Row = I422ToARGB4444Row_DSPR2;
797 }
798 }
799 #endif
800 #if defined(HAS_I422TOARGB4444ROW_MSA)
801 if (TestCpuFlag(kCpuHasMSA)) {
802 I422ToARGB4444Row = I422ToARGB4444Row_Any_MSA;
803 if (IS_ALIGNED(width, 8)) {
804 I422ToARGB4444Row = I422ToARGB4444Row_MSA;
805 }
806 }
807 #endif
808
809 for (y = 0; y < height; ++y) {
810 I422ToARGB4444Row(src_y, src_u, src_v, dst_argb4444, &kYuvI601Constants,
811 width);
812 dst_argb4444 += dst_stride_argb4444;
813 src_y += src_stride_y;
814 if (y & 1) {
815 src_u += src_stride_u;
816 src_v += src_stride_v;
817 }
818 }
819 return 0;
820 }
821
822 // Convert I420 to RGB565 with specified color matrix.
823 LIBYUV_API
I420ToRGB565Matrix(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb565,int dst_stride_rgb565,const struct YuvConstants * yuvconstants,int width,int height)824 int I420ToRGB565Matrix(const uint8* src_y,
825 int src_stride_y,
826 const uint8* src_u,
827 int src_stride_u,
828 const uint8* src_v,
829 int src_stride_v,
830 uint8* dst_rgb565,
831 int dst_stride_rgb565,
832 const struct YuvConstants* yuvconstants,
833 int width,
834 int height) {
835 int y;
836 void (*I422ToRGB565Row)(const uint8* y_buf, const uint8* u_buf,
837 const uint8* v_buf, uint8* rgb_buf,
838 const struct YuvConstants* yuvconstants, int width) =
839 I422ToRGB565Row_C;
840 if (!src_y || !src_u || !src_v || !dst_rgb565 || width <= 0 || height == 0) {
841 return -1;
842 }
843 // Negative height means invert the image.
844 if (height < 0) {
845 height = -height;
846 dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
847 dst_stride_rgb565 = -dst_stride_rgb565;
848 }
849 #if defined(HAS_I422TORGB565ROW_SSSE3)
850 if (TestCpuFlag(kCpuHasSSSE3)) {
851 I422ToRGB565Row = I422ToRGB565Row_Any_SSSE3;
852 if (IS_ALIGNED(width, 8)) {
853 I422ToRGB565Row = I422ToRGB565Row_SSSE3;
854 }
855 }
856 #endif
857 #if defined(HAS_I422TORGB565ROW_AVX2)
858 if (TestCpuFlag(kCpuHasAVX2)) {
859 I422ToRGB565Row = I422ToRGB565Row_Any_AVX2;
860 if (IS_ALIGNED(width, 16)) {
861 I422ToRGB565Row = I422ToRGB565Row_AVX2;
862 }
863 }
864 #endif
865 #if defined(HAS_I422TORGB565ROW_NEON)
866 if (TestCpuFlag(kCpuHasNEON)) {
867 I422ToRGB565Row = I422ToRGB565Row_Any_NEON;
868 if (IS_ALIGNED(width, 8)) {
869 I422ToRGB565Row = I422ToRGB565Row_NEON;
870 }
871 }
872 #endif
873 #if defined(HAS_I422TORGB565ROW_MSA)
874 if (TestCpuFlag(kCpuHasMSA)) {
875 I422ToRGB565Row = I422ToRGB565Row_Any_MSA;
876 if (IS_ALIGNED(width, 8)) {
877 I422ToRGB565Row = I422ToRGB565Row_MSA;
878 }
879 }
880 #endif
881
882 for (y = 0; y < height; ++y) {
883 I422ToRGB565Row(src_y, src_u, src_v, dst_rgb565, yuvconstants, width);
884 dst_rgb565 += dst_stride_rgb565;
885 src_y += src_stride_y;
886 if (y & 1) {
887 src_u += src_stride_u;
888 src_v += src_stride_v;
889 }
890 }
891 return 0;
892 }
893
894 // Convert I420 to RGB565.
895 LIBYUV_API
I420ToRGB565(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb565,int dst_stride_rgb565,int width,int height)896 int I420ToRGB565(const uint8* src_y,
897 int src_stride_y,
898 const uint8* src_u,
899 int src_stride_u,
900 const uint8* src_v,
901 int src_stride_v,
902 uint8* dst_rgb565,
903 int dst_stride_rgb565,
904 int width,
905 int height) {
906 return I420ToRGB565Matrix(src_y,
907 src_stride_y,
908 src_u,
909 src_stride_u,
910 src_v,
911 src_stride_v,
912 dst_rgb565,
913 dst_stride_rgb565,
914 &kYuvI601Constants,
915 width,
916 height);
917 }
918
919 // Convert J420 to RGB565.
920 LIBYUV_API
J420ToRGB565(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb565,int dst_stride_rgb565,int width,int height)921 int J420ToRGB565(const uint8* src_y,
922 int src_stride_y,
923 const uint8* src_u,
924 int src_stride_u,
925 const uint8* src_v,
926 int src_stride_v,
927 uint8* dst_rgb565,
928 int dst_stride_rgb565,
929 int width,
930 int height) {
931 return I420ToRGB565Matrix(src_y,
932 src_stride_y,
933 src_u,
934 src_stride_u,
935 src_v,
936 src_stride_v,
937 dst_rgb565,
938 dst_stride_rgb565,
939 &kYuvJPEGConstants,
940 width,
941 height);
942 }
943
944 // Convert H420 to RGB565.
945 LIBYUV_API
H420ToRGB565(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb565,int dst_stride_rgb565,int width,int height)946 int H420ToRGB565(const uint8* src_y,
947 int src_stride_y,
948 const uint8* src_u,
949 int src_stride_u,
950 const uint8* src_v,
951 int src_stride_v,
952 uint8* dst_rgb565,
953 int dst_stride_rgb565,
954 int width,
955 int height) {
956 return I420ToRGB565Matrix(src_y,
957 src_stride_y,
958 src_u,
959 src_stride_u,
960 src_v,
961 src_stride_v,
962 dst_rgb565,
963 dst_stride_rgb565,
964 &kYuvH709Constants,
965 width,
966 height);
967 }
968
969 // Convert I422 to RGB565.
970 LIBYUV_API
I422ToRGB565(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb565,int dst_stride_rgb565,int width,int height)971 int I422ToRGB565(const uint8* src_y,
972 int src_stride_y,
973 const uint8* src_u,
974 int src_stride_u,
975 const uint8* src_v,
976 int src_stride_v,
977 uint8* dst_rgb565,
978 int dst_stride_rgb565,
979 int width,
980 int height) {
981 int y;
982 void (*I422ToRGB565Row)(const uint8* y_buf, const uint8* u_buf,
983 const uint8* v_buf, uint8* rgb_buf,
984 const struct YuvConstants* yuvconstants, int width) =
985 I422ToRGB565Row_C;
986 if (!src_y || !src_u || !src_v || !dst_rgb565 || width <= 0 || height == 0) {
987 return -1;
988 }
989 // Negative height means invert the image.
990 if (height < 0) {
991 height = -height;
992 dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
993 dst_stride_rgb565 = -dst_stride_rgb565;
994 }
995 #if defined(HAS_I422TORGB565ROW_SSSE3)
996 if (TestCpuFlag(kCpuHasSSSE3)) {
997 I422ToRGB565Row = I422ToRGB565Row_Any_SSSE3;
998 if (IS_ALIGNED(width, 8)) {
999 I422ToRGB565Row = I422ToRGB565Row_SSSE3;
1000 }
1001 }
1002 #endif
1003 #if defined(HAS_I422TORGB565ROW_AVX2)
1004 if (TestCpuFlag(kCpuHasAVX2)) {
1005 I422ToRGB565Row = I422ToRGB565Row_Any_AVX2;
1006 if (IS_ALIGNED(width, 16)) {
1007 I422ToRGB565Row = I422ToRGB565Row_AVX2;
1008 }
1009 }
1010 #endif
1011 #if defined(HAS_I422TORGB565ROW_NEON)
1012 if (TestCpuFlag(kCpuHasNEON)) {
1013 I422ToRGB565Row = I422ToRGB565Row_Any_NEON;
1014 if (IS_ALIGNED(width, 8)) {
1015 I422ToRGB565Row = I422ToRGB565Row_NEON;
1016 }
1017 }
1018 #endif
1019 #if defined(HAS_I422TORGB565ROW_MSA)
1020 if (TestCpuFlag(kCpuHasMSA)) {
1021 I422ToRGB565Row = I422ToRGB565Row_Any_MSA;
1022 if (IS_ALIGNED(width, 8)) {
1023 I422ToRGB565Row = I422ToRGB565Row_MSA;
1024 }
1025 }
1026 #endif
1027
1028 for (y = 0; y < height; ++y) {
1029 I422ToRGB565Row(src_y, src_u, src_v, dst_rgb565, &kYuvI601Constants, width);
1030 dst_rgb565 += dst_stride_rgb565;
1031 src_y += src_stride_y;
1032 src_u += src_stride_u;
1033 src_v += src_stride_v;
1034 }
1035 return 0;
1036 }
1037
1038 // Ordered 8x8 dither for 888 to 565. Values from 0 to 7.
1039 static const uint8 kDither565_4x4[16] = {
1040 0, 4, 1, 5, 6, 2, 7, 3, 1, 5, 0, 4, 7, 3, 6, 2,
1041 };
1042
1043 // Convert I420 to RGB565 with dithering.
1044 LIBYUV_API
I420ToRGB565Dither(const uint8 * src_y,int src_stride_y,const uint8 * src_u,int src_stride_u,const uint8 * src_v,int src_stride_v,uint8 * dst_rgb565,int dst_stride_rgb565,const uint8 * dither4x4,int width,int height)1045 int I420ToRGB565Dither(const uint8* src_y,
1046 int src_stride_y,
1047 const uint8* src_u,
1048 int src_stride_u,
1049 const uint8* src_v,
1050 int src_stride_v,
1051 uint8* dst_rgb565,
1052 int dst_stride_rgb565,
1053 const uint8* dither4x4,
1054 int width,
1055 int height) {
1056 int y;
1057 void (*I422ToARGBRow)(const uint8* y_buf, const uint8* u_buf,
1058 const uint8* v_buf, uint8* rgb_buf,
1059 const struct YuvConstants* yuvconstants, int width) =
1060 I422ToARGBRow_C;
1061 void (*ARGBToRGB565DitherRow)(const uint8* src_argb, uint8* dst_rgb,
1062 const uint32 dither4, int width) =
1063 ARGBToRGB565DitherRow_C;
1064 if (!src_y || !src_u || !src_v || !dst_rgb565 || width <= 0 || height == 0) {
1065 return -1;
1066 }
1067 // Negative height means invert the image.
1068 if (height < 0) {
1069 height = -height;
1070 dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
1071 dst_stride_rgb565 = -dst_stride_rgb565;
1072 }
1073 if (!dither4x4) {
1074 dither4x4 = kDither565_4x4;
1075 }
1076 #if defined(HAS_I422TOARGBROW_SSSE3)
1077 if (TestCpuFlag(kCpuHasSSSE3)) {
1078 I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
1079 if (IS_ALIGNED(width, 8)) {
1080 I422ToARGBRow = I422ToARGBRow_SSSE3;
1081 }
1082 }
1083 #endif
1084 #if defined(HAS_I422TOARGBROW_AVX2)
1085 if (TestCpuFlag(kCpuHasAVX2)) {
1086 I422ToARGBRow = I422ToARGBRow_Any_AVX2;
1087 if (IS_ALIGNED(width, 16)) {
1088 I422ToARGBRow = I422ToARGBRow_AVX2;
1089 }
1090 }
1091 #endif
1092 #if defined(HAS_I422TOARGBROW_NEON)
1093 if (TestCpuFlag(kCpuHasNEON)) {
1094 I422ToARGBRow = I422ToARGBRow_Any_NEON;
1095 if (IS_ALIGNED(width, 8)) {
1096 I422ToARGBRow = I422ToARGBRow_NEON;
1097 }
1098 }
1099 #endif
1100 #if defined(HAS_I422TOARGBROW_DSPR2)
1101 if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(width, 4) &&
1102 IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
1103 IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
1104 IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2)) {
1105 I422ToARGBRow = I422ToARGBRow_DSPR2;
1106 }
1107 #endif
1108 #if defined(HAS_I422TOARGBROW_MSA)
1109 if (TestCpuFlag(kCpuHasMSA)) {
1110 I422ToARGBRow = I422ToARGBRow_Any_MSA;
1111 if (IS_ALIGNED(width, 8)) {
1112 I422ToARGBRow = I422ToARGBRow_MSA;
1113 }
1114 }
1115 #endif
1116 #if defined(HAS_ARGBTORGB565DITHERROW_SSE2)
1117 if (TestCpuFlag(kCpuHasSSE2)) {
1118 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_SSE2;
1119 if (IS_ALIGNED(width, 4)) {
1120 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_SSE2;
1121 }
1122 }
1123 #endif
1124 #if defined(HAS_ARGBTORGB565DITHERROW_AVX2)
1125 if (TestCpuFlag(kCpuHasAVX2)) {
1126 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_AVX2;
1127 if (IS_ALIGNED(width, 8)) {
1128 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_AVX2;
1129 }
1130 }
1131 #endif
1132 #if defined(HAS_ARGBTORGB565DITHERROW_NEON)
1133 if (TestCpuFlag(kCpuHasNEON)) {
1134 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_NEON;
1135 if (IS_ALIGNED(width, 8)) {
1136 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_NEON;
1137 }
1138 }
1139 #endif
1140 #if defined(HAS_ARGBTORGB565DITHERROW_MSA)
1141 if (TestCpuFlag(kCpuHasMSA)) {
1142 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_MSA;
1143 if (IS_ALIGNED(width, 8)) {
1144 ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_MSA;
1145 }
1146 }
1147 #endif
1148 {
1149 // Allocate a row of argb.
1150 align_buffer_64(row_argb, width * 4);
1151 for (y = 0; y < height; ++y) {
1152 I422ToARGBRow(src_y, src_u, src_v, row_argb, &kYuvI601Constants, width);
1153 ARGBToRGB565DitherRow(row_argb, dst_rgb565,
1154 *(uint32*)(dither4x4 + ((y & 3) << 2)),
1155 width); // NOLINT
1156 dst_rgb565 += dst_stride_rgb565;
1157 src_y += src_stride_y;
1158 if (y & 1) {
1159 src_u += src_stride_u;
1160 src_v += src_stride_v;
1161 }
1162 }
1163 free_aligned_buffer_64(row_argb);
1164 }
1165 return 0;
1166 }
1167
1168 // Convert I420 to specified format
1169 LIBYUV_API
ConvertFromI420(const uint8 * y,int y_stride,const uint8 * u,int u_stride,const uint8 * v,int v_stride,uint8 * dst_sample,int dst_sample_stride,int width,int height,uint32 fourcc)1170 int ConvertFromI420(const uint8* y,
1171 int y_stride,
1172 const uint8* u,
1173 int u_stride,
1174 const uint8* v,
1175 int v_stride,
1176 uint8* dst_sample,
1177 int dst_sample_stride,
1178 int width,
1179 int height,
1180 uint32 fourcc) {
1181 uint32 format = CanonicalFourCC(fourcc);
1182 int r = 0;
1183 if (!y || !u || !v || !dst_sample || width <= 0 || height == 0) {
1184 return -1;
1185 }
1186 switch (format) {
1187 // Single plane formats
1188 case FOURCC_YUY2:
1189 r = I420ToYUY2(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1190 dst_sample_stride ? dst_sample_stride : width * 2, width,
1191 height);
1192 break;
1193 case FOURCC_UYVY:
1194 r = I420ToUYVY(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1195 dst_sample_stride ? dst_sample_stride : width * 2, width,
1196 height);
1197 break;
1198 case FOURCC_RGBP:
1199 r = I420ToRGB565(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1200 dst_sample_stride ? dst_sample_stride : width * 2, width,
1201 height);
1202 break;
1203 case FOURCC_RGBO:
1204 r = I420ToARGB1555(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1205 dst_sample_stride ? dst_sample_stride : width * 2,
1206 width, height);
1207 break;
1208 case FOURCC_R444:
1209 r = I420ToARGB4444(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1210 dst_sample_stride ? dst_sample_stride : width * 2,
1211 width, height);
1212 break;
1213 case FOURCC_24BG:
1214 r = I420ToRGB24(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1215 dst_sample_stride ? dst_sample_stride : width * 3, width,
1216 height);
1217 break;
1218 case FOURCC_RAW:
1219 r = I420ToRAW(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1220 dst_sample_stride ? dst_sample_stride : width * 3, width,
1221 height);
1222 break;
1223 case FOURCC_ARGB:
1224 r = I420ToARGB(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1225 dst_sample_stride ? dst_sample_stride : width * 4, width,
1226 height);
1227 break;
1228 case FOURCC_BGRA:
1229 r = I420ToBGRA(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1230 dst_sample_stride ? dst_sample_stride : width * 4, width,
1231 height);
1232 break;
1233 case FOURCC_ABGR:
1234 r = I420ToABGR(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1235 dst_sample_stride ? dst_sample_stride : width * 4, width,
1236 height);
1237 break;
1238 case FOURCC_RGBA:
1239 r = I420ToRGBA(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1240 dst_sample_stride ? dst_sample_stride : width * 4, width,
1241 height);
1242 break;
1243 case FOURCC_I400:
1244 r = I400Copy(y, y_stride, dst_sample,
1245 dst_sample_stride ? dst_sample_stride : width, width,
1246 height);
1247 break;
1248 case FOURCC_NV12: {
1249 uint8* dst_uv = dst_sample + width * height;
1250 r = I420ToNV12(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1251 dst_sample_stride ? dst_sample_stride : width, dst_uv,
1252 dst_sample_stride ? dst_sample_stride : width, width,
1253 height);
1254 break;
1255 }
1256 case FOURCC_NV21: {
1257 uint8* dst_vu = dst_sample + width * height;
1258 r = I420ToNV21(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1259 dst_sample_stride ? dst_sample_stride : width, dst_vu,
1260 dst_sample_stride ? dst_sample_stride : width, width,
1261 height);
1262 break;
1263 }
1264 // TODO(fbarchard): Add M420.
1265 // Triplanar formats
1266 case FOURCC_I420:
1267 case FOURCC_YV12: {
1268 dst_sample_stride = dst_sample_stride ? dst_sample_stride : width;
1269 int halfstride = (dst_sample_stride + 1) / 2;
1270 int halfheight = (height + 1) / 2;
1271 uint8* dst_u;
1272 uint8* dst_v;
1273 if (format == FOURCC_YV12) {
1274 dst_v = dst_sample + dst_sample_stride * height;
1275 dst_u = dst_v + halfstride * halfheight;
1276 } else {
1277 dst_u = dst_sample + dst_sample_stride * height;
1278 dst_v = dst_u + halfstride * halfheight;
1279 }
1280 r = I420Copy(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1281 dst_sample_stride, dst_u, halfstride, dst_v, halfstride,
1282 width, height);
1283 break;
1284 }
1285 case FOURCC_I422:
1286 case FOURCC_YV16: {
1287 dst_sample_stride = dst_sample_stride ? dst_sample_stride : width;
1288 int halfstride = (dst_sample_stride + 1) / 2;
1289 uint8* dst_u;
1290 uint8* dst_v;
1291 if (format == FOURCC_YV16) {
1292 dst_v = dst_sample + dst_sample_stride * height;
1293 dst_u = dst_v + halfstride * height;
1294 } else {
1295 dst_u = dst_sample + dst_sample_stride * height;
1296 dst_v = dst_u + halfstride * height;
1297 }
1298 r = I420ToI422(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1299 dst_sample_stride, dst_u, halfstride, dst_v, halfstride,
1300 width, height);
1301 break;
1302 }
1303 case FOURCC_I444:
1304 case FOURCC_YV24: {
1305 dst_sample_stride = dst_sample_stride ? dst_sample_stride : width;
1306 uint8* dst_u;
1307 uint8* dst_v;
1308 if (format == FOURCC_YV24) {
1309 dst_v = dst_sample + dst_sample_stride * height;
1310 dst_u = dst_v + dst_sample_stride * height;
1311 } else {
1312 dst_u = dst_sample + dst_sample_stride * height;
1313 dst_v = dst_u + dst_sample_stride * height;
1314 }
1315 r = I420ToI444(y, y_stride, u, u_stride, v, v_stride, dst_sample,
1316 dst_sample_stride, dst_u, dst_sample_stride, dst_v,
1317 dst_sample_stride, width, height);
1318 break;
1319 }
1320 // Formats not supported - MJPG, biplanar, some rgb formats.
1321 default:
1322 return -1; // unknown fourcc - return failure code.
1323 }
1324 return r;
1325 }
1326
1327 #ifdef __cplusplus
1328 } // extern "C"
1329 } // namespace libyuv
1330 #endif
1331