1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This webpage shows layout of YV12 and other YUV formats
6 // http://www.fourcc.org/yuv.php
7 // The actual conversion is best described here
8 // http://en.wikipedia.org/wiki/YUV
9 // An article on optimizing YUV conversion using tables instead of multiplies
10 // http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
12 // YV12 is a full plane of Y and a half height, half width chroma planes
13 // YV16 is a full plane of Y and a full height, half width chroma planes
15 // ARGB pixel format is output, which on little endian is stored as BGRA.
16 // The alpha is set to 255, allowing the application to use RGBA or RGB32.
18 #include "media/base/yuv_convert.h"
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
24 #include "build/build_config.h"
25 #include "media/base/simd/convert_rgb_to_yuv.h"
26 #include "media/base/simd/convert_yuv_to_rgb.h"
27 #include "media/base/simd/filter_yuv.h"
29 #if defined(ARCH_CPU_X86_FAMILY)
30 #if defined(COMPILER_MSVC)
37 // Assembly functions are declared without namespace.
38 extern "C" { void EmptyRegisterState_MMX(); } // extern "C"
42 typedef void (*FilterYUVRowsProc
)(uint8
*, const uint8
*, const uint8
*, int, int);
44 typedef void (*ConvertRGBToYUVProc
)(const uint8
*,
54 typedef void (*ConvertYUVToRGB32Proc
)(const uint8
*,
65 typedef void (*ConvertYUVAToARGBProc
)(const uint8
*,
78 typedef void (*ConvertYUVToRGB32RowProc
)(const uint8
*,
84 typedef void (*ConvertYUVAToARGBRowProc
)(const uint8
*,
91 typedef void (*ScaleYUVToRGB32RowProc
)(const uint8
*,
98 static FilterYUVRowsProc g_filter_yuv_rows_proc_
= NULL
;
99 static ConvertYUVToRGB32RowProc g_convert_yuv_to_rgb32_row_proc_
= NULL
;
100 static ScaleYUVToRGB32RowProc g_scale_yuv_to_rgb32_row_proc_
= NULL
;
101 static ScaleYUVToRGB32RowProc g_linear_scale_yuv_to_rgb32_row_proc_
= NULL
;
102 static ConvertRGBToYUVProc g_convert_rgb32_to_yuv_proc_
= NULL
;
103 static ConvertRGBToYUVProc g_convert_rgb24_to_yuv_proc_
= NULL
;
104 static ConvertYUVToRGB32Proc g_convert_yuv_to_rgb32_proc_
= NULL
;
105 static ConvertYUVAToARGBProc g_convert_yuva_to_argb_proc_
= NULL
;
107 // Empty SIMD registers state after using them.
108 void EmptyRegisterStateStub() {}
109 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
110 void EmptyRegisterStateIntrinsic() { _mm_empty(); }
112 typedef void (*EmptyRegisterStateProc
)();
113 static EmptyRegisterStateProc g_empty_register_state_proc_
= NULL
;
115 void InitializeCPUSpecificYUVConversions() {
116 CHECK(!g_filter_yuv_rows_proc_
);
117 CHECK(!g_convert_yuv_to_rgb32_row_proc_
);
118 CHECK(!g_scale_yuv_to_rgb32_row_proc_
);
119 CHECK(!g_linear_scale_yuv_to_rgb32_row_proc_
);
120 CHECK(!g_convert_rgb32_to_yuv_proc_
);
121 CHECK(!g_convert_rgb24_to_yuv_proc_
);
122 CHECK(!g_convert_yuv_to_rgb32_proc_
);
123 CHECK(!g_convert_yuva_to_argb_proc_
);
124 CHECK(!g_empty_register_state_proc_
);
126 g_filter_yuv_rows_proc_
= FilterYUVRows_C
;
127 g_convert_yuv_to_rgb32_row_proc_
= ConvertYUVToRGB32Row_C
;
128 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_C
;
129 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_C
;
130 g_convert_rgb32_to_yuv_proc_
= ConvertRGB32ToYUV_C
;
131 g_convert_rgb24_to_yuv_proc_
= ConvertRGB24ToYUV_C
;
132 g_convert_yuv_to_rgb32_proc_
= ConvertYUVToRGB32_C
;
133 g_convert_yuva_to_argb_proc_
= ConvertYUVAToARGB_C
;
134 g_empty_register_state_proc_
= EmptyRegisterStateStub
;
136 // Assembly code confuses MemorySanitizer.
137 #if defined(ARCH_CPU_X86_FAMILY) && !defined(MEMORY_SANITIZER)
140 g_convert_yuv_to_rgb32_row_proc_
= ConvertYUVToRGB32Row_MMX
;
141 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_MMX
;
142 g_convert_yuv_to_rgb32_proc_
= ConvertYUVToRGB32_MMX
;
143 g_convert_yuva_to_argb_proc_
= ConvertYUVAToARGB_MMX
;
144 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_MMX
;
146 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
147 g_filter_yuv_rows_proc_
= FilterYUVRows_MMX
;
148 g_empty_register_state_proc_
= EmptyRegisterStateIntrinsic
;
150 g_empty_register_state_proc_
= EmptyRegisterState_MMX
;
155 g_convert_yuv_to_rgb32_row_proc_
= ConvertYUVToRGB32Row_SSE
;
156 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_SSE
;
157 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_SSE
;
158 g_convert_yuv_to_rgb32_proc_
= ConvertYUVToRGB32_SSE
;
161 if (cpu
.has_sse2()) {
162 g_filter_yuv_rows_proc_
= FilterYUVRows_SSE2
;
163 g_convert_rgb32_to_yuv_proc_
= ConvertRGB32ToYUV_SSE2
;
165 #if defined(ARCH_CPU_X86_64)
166 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_SSE2_X64
;
168 // Technically this should be in the MMX section, but MSVC will optimize out
169 // the export of LinearScaleYUVToRGB32Row_MMX, which is required by the unit
170 // tests, if that decision can be made at compile time. Since all X64 CPUs
171 // have SSE2, we can hack around this by making the selection here.
172 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_MMX_X64
;
176 if (cpu
.has_ssse3()) {
177 g_convert_rgb24_to_yuv_proc_
= &ConvertRGB24ToYUV_SSSE3
;
179 // TODO(hclam): Add ConvertRGB32ToYUV_SSSE3 when the cyan problem is solved.
180 // See: crbug.com/100462
185 // Empty SIMD registers state after using them.
186 void EmptyRegisterState() { g_empty_register_state_proc_(); }
188 // 16.16 fixed point arithmetic
189 const int kFractionBits
= 16;
190 const int kFractionMax
= 1 << kFractionBits
;
191 const int kFractionMask
= ((1 << kFractionBits
) - 1);
193 // Scale a frame of YUV to 32 bit ARGB.
194 void ScaleYUVToRGB32(const uint8
* y_buf
,
207 ScaleFilter filter
) {
208 // Handle zero sized sources and destinations.
209 if ((yuv_type
== YV12
&& (source_width
< 2 || source_height
< 2)) ||
210 (yuv_type
== YV16
&& (source_width
< 2 || source_height
< 1)) ||
211 width
== 0 || height
== 0)
214 // 4096 allows 3 buffers to fit in 12k.
215 // Helps performance on CPU with 16K L1 cache.
216 // Large enough for 3830x2160 and 30" displays which are 2560x1600.
217 const int kFilterBufferSize
= 4096;
218 // Disable filtering if the screen is too big (to avoid buffer overflows).
219 // This should never happen to regular users: they don't have monitors
220 // wider than 4096 pixels.
221 // TODO(fbarchard): Allow rotated videos to filter.
222 if (source_width
> kFilterBufferSize
|| view_rotate
)
223 filter
= FILTER_NONE
;
225 unsigned int y_shift
= yuv_type
;
226 // Diagram showing origin and direction of source sampling.
232 // Rotations that start at right side of image.
233 if ((view_rotate
== ROTATE_180
) || (view_rotate
== ROTATE_270
) ||
234 (view_rotate
== MIRROR_ROTATE_0
) || (view_rotate
== MIRROR_ROTATE_90
)) {
235 y_buf
+= source_width
- 1;
236 u_buf
+= source_width
/ 2 - 1;
237 v_buf
+= source_width
/ 2 - 1;
238 source_width
= -source_width
;
240 // Rotations that start at bottom of image.
241 if ((view_rotate
== ROTATE_90
) || (view_rotate
== ROTATE_180
) ||
242 (view_rotate
== MIRROR_ROTATE_90
) || (view_rotate
== MIRROR_ROTATE_180
)) {
243 y_buf
+= (source_height
- 1) * y_pitch
;
244 u_buf
+= ((source_height
>> y_shift
) - 1) * uv_pitch
;
245 v_buf
+= ((source_height
>> y_shift
) - 1) * uv_pitch
;
246 source_height
= -source_height
;
249 int source_dx
= source_width
* kFractionMax
/ width
;
251 if ((view_rotate
== ROTATE_90
) || (view_rotate
== ROTATE_270
)) {
256 source_height
= source_width
;
258 int source_dy
= source_height
* kFractionMax
/ height
;
259 source_dx
= ((source_dy
>> kFractionBits
) * y_pitch
) << kFractionBits
;
260 if (view_rotate
== ROTATE_90
) {
263 source_height
= -source_height
;
270 // Need padding because FilterRows() will write 1 to 16 extra pixels
271 // after the end for SSE2 version.
272 uint8 yuvbuf
[16 + kFilterBufferSize
* 3 + 16];
274 reinterpret_cast<uint8
*>(reinterpret_cast<uintptr_t>(yuvbuf
+ 15) & ~15);
275 uint8
* ubuf
= ybuf
+ kFilterBufferSize
;
276 uint8
* vbuf
= ubuf
+ kFilterBufferSize
;
278 // TODO(fbarchard): Fixed point math is off by 1 on negatives.
280 // We take a y-coordinate in [0,1] space in the source image space, and
281 // transform to a y-coordinate in [0,1] space in the destination image space.
282 // Note that the coordinate endpoints lie on pixel boundaries, not on pixel
283 // centers: e.g. a two-pixel-high image will have pixel centers at 0.25 and
284 // 0.75. The formula is as follows (in fixed-point arithmetic):
285 // y_dst = dst_height * ((y_src + 0.5) / src_height)
286 // dst_pixel = clamp([0, dst_height - 1], floor(y_dst - 0.5))
287 // Implement this here as an accumulator + delta, to avoid expensive math
289 int source_y_subpixel_accum
=
290 ((kFractionMax
/ 2) * source_height
) / height
- (kFractionMax
/ 2);
291 int source_y_subpixel_delta
= ((1 << kFractionBits
) * source_height
) / height
;
293 // TODO(fbarchard): Split this into separate function for better efficiency.
294 for (int y
= 0; y
< height
; ++y
) {
295 uint8
* dest_pixel
= rgb_buf
+ y
* rgb_pitch
;
296 int source_y_subpixel
= source_y_subpixel_accum
;
297 source_y_subpixel_accum
+= source_y_subpixel_delta
;
298 if (source_y_subpixel
< 0)
299 source_y_subpixel
= 0;
300 else if (source_y_subpixel
> ((source_height
- 1) << kFractionBits
))
301 source_y_subpixel
= (source_height
- 1) << kFractionBits
;
303 const uint8
* y_ptr
= NULL
;
304 const uint8
* u_ptr
= NULL
;
305 const uint8
* v_ptr
= NULL
;
306 // Apply vertical filtering if necessary.
307 // TODO(fbarchard): Remove memcpy when not necessary.
308 if (filter
& media::FILTER_BILINEAR_V
) {
309 int source_y
= source_y_subpixel
>> kFractionBits
;
310 y_ptr
= y_buf
+ source_y
* y_pitch
;
311 u_ptr
= u_buf
+ (source_y
>> y_shift
) * uv_pitch
;
312 v_ptr
= v_buf
+ (source_y
>> y_shift
) * uv_pitch
;
314 // Vertical scaler uses 16.8 fixed point.
315 int source_y_fraction
= (source_y_subpixel
& kFractionMask
) >> 8;
316 if (source_y_fraction
!= 0) {
317 g_filter_yuv_rows_proc_(
318 ybuf
, y_ptr
, y_ptr
+ y_pitch
, source_width
, source_y_fraction
);
320 memcpy(ybuf
, y_ptr
, source_width
);
323 ybuf
[source_width
] = ybuf
[source_width
- 1];
325 int uv_source_width
= (source_width
+ 1) / 2;
326 int source_uv_fraction
;
328 // For formats with half-height UV planes, each even-numbered pixel row
329 // should not interpolate, since the next row to interpolate from should
330 // be a duplicate of the current row.
331 if (y_shift
&& (source_y
& 0x1) == 0)
332 source_uv_fraction
= 0;
334 source_uv_fraction
= source_y_fraction
;
336 if (source_uv_fraction
!= 0) {
337 g_filter_yuv_rows_proc_(
338 ubuf
, u_ptr
, u_ptr
+ uv_pitch
, uv_source_width
, source_uv_fraction
);
339 g_filter_yuv_rows_proc_(
340 vbuf
, v_ptr
, v_ptr
+ uv_pitch
, uv_source_width
, source_uv_fraction
);
342 memcpy(ubuf
, u_ptr
, uv_source_width
);
343 memcpy(vbuf
, v_ptr
, uv_source_width
);
347 ubuf
[uv_source_width
] = ubuf
[uv_source_width
- 1];
348 vbuf
[uv_source_width
] = vbuf
[uv_source_width
- 1];
350 // Offset by 1/2 pixel for center sampling.
351 int source_y
= (source_y_subpixel
+ (kFractionMax
/ 2)) >> kFractionBits
;
352 y_ptr
= y_buf
+ source_y
* y_pitch
;
353 u_ptr
= u_buf
+ (source_y
>> y_shift
) * uv_pitch
;
354 v_ptr
= v_buf
+ (source_y
>> y_shift
) * uv_pitch
;
356 if (source_dx
== kFractionMax
) { // Not scaled
357 g_convert_yuv_to_rgb32_row_proc_(y_ptr
, u_ptr
, v_ptr
, dest_pixel
, width
);
359 if (filter
& FILTER_BILINEAR_H
) {
360 g_linear_scale_yuv_to_rgb32_row_proc_(
361 y_ptr
, u_ptr
, v_ptr
, dest_pixel
, width
, source_dx
);
363 g_scale_yuv_to_rgb32_row_proc_(
364 y_ptr
, u_ptr
, v_ptr
, dest_pixel
, width
, source_dx
);
369 g_empty_register_state_proc_();
372 // Scale a frame of YV12 to 32 bit ARGB for a specific rectangle.
373 void ScaleYUVToRGB32WithRect(const uint8
* y_buf
,
384 int dest_rect_bottom
,
388 // This routine doesn't currently support up-scaling.
389 CHECK_LE(dest_width
, source_width
);
390 CHECK_LE(dest_height
, source_height
);
392 // Sanity-check the destination rectangle.
393 DCHECK(dest_rect_left
>= 0 && dest_rect_right
<= dest_width
);
394 DCHECK(dest_rect_top
>= 0 && dest_rect_bottom
<= dest_height
);
395 DCHECK(dest_rect_right
> dest_rect_left
);
396 DCHECK(dest_rect_bottom
> dest_rect_top
);
398 // Fixed-point value of vertical and horizontal scale down factor.
399 // Values are in the format 16.16.
400 int y_step
= kFractionMax
* source_height
/ dest_height
;
401 int x_step
= kFractionMax
* source_width
/ dest_width
;
403 // Determine the coordinates of the rectangle in 16.16 coords.
404 // NB: Our origin is the *center* of the top/left pixel, NOT its top/left.
405 // If we're down-scaling by more than a factor of two, we start with a 50%
406 // fraction to avoid degenerating to point-sampling - we should really just
407 // fix the fraction at 50% for all pixels in that case.
408 int source_left
= dest_rect_left
* x_step
;
409 int source_right
= (dest_rect_right
- 1) * x_step
;
410 if (x_step
< kFractionMax
* 2) {
411 source_left
+= ((x_step
- kFractionMax
) / 2);
412 source_right
+= ((x_step
- kFractionMax
) / 2);
414 source_left
+= kFractionMax
/ 2;
415 source_right
+= kFractionMax
/ 2;
417 int source_top
= dest_rect_top
* y_step
;
418 if (y_step
< kFractionMax
* 2) {
419 source_top
+= ((y_step
- kFractionMax
) / 2);
421 source_top
+= kFractionMax
/ 2;
424 // Determine the parts of the Y, U and V buffers to interpolate.
425 int source_y_left
= source_left
>> kFractionBits
;
427 std::min((source_right
>> kFractionBits
) + 2, source_width
+ 1);
429 int source_uv_left
= source_y_left
/ 2;
430 int source_uv_right
= std::min((source_right
>> (kFractionBits
+ 1)) + 2,
431 (source_width
+ 1) / 2);
433 int source_y_width
= source_y_right
- source_y_left
;
434 int source_uv_width
= source_uv_right
- source_uv_left
;
436 // Determine number of pixels in each output row.
437 int dest_rect_width
= dest_rect_right
- dest_rect_left
;
439 // Intermediate buffer for vertical interpolation.
440 // 4096 bytes allows 3 buffers to fit in 12k, which fits in a 16K L1 cache,
441 // and is bigger than most users will generally need.
442 // The buffer is 16-byte aligned and padded with 16 extra bytes; some of the
443 // FilterYUVRowProcs have alignment requirements, and the SSE version can
444 // write up to 16 bytes past the end of the buffer.
445 const int kFilterBufferSize
= 4096;
446 const bool kAvoidUsingOptimizedFilter
= source_width
> kFilterBufferSize
;
447 uint8 yuv_temp
[16 + kFilterBufferSize
* 3 + 16];
448 // memset() yuv_temp to 0 to avoid bogus warnings when running on Valgrind.
449 if (RunningOnValgrind())
450 memset(yuv_temp
, 0, sizeof(yuv_temp
));
451 uint8
* y_temp
= reinterpret_cast<uint8
*>(
452 reinterpret_cast<uintptr_t>(yuv_temp
+ 15) & ~15);
453 uint8
* u_temp
= y_temp
+ kFilterBufferSize
;
454 uint8
* v_temp
= u_temp
+ kFilterBufferSize
;
456 // Move to the top-left pixel of output.
457 rgb_buf
+= dest_rect_top
* rgb_pitch
;
458 rgb_buf
+= dest_rect_left
* 4;
460 // For each destination row perform interpolation and color space
461 // conversion to produce the output.
462 for (int row
= dest_rect_top
; row
< dest_rect_bottom
; ++row
) {
463 // Round the fixed-point y position to get the current row.
464 int source_row
= source_top
>> kFractionBits
;
465 int source_uv_row
= source_row
/ 2;
466 DCHECK(source_row
< source_height
);
468 // Locate the first row for each plane for interpolation.
469 const uint8
* y0_ptr
= y_buf
+ y_pitch
* source_row
+ source_y_left
;
470 const uint8
* u0_ptr
= u_buf
+ uv_pitch
* source_uv_row
+ source_uv_left
;
471 const uint8
* v0_ptr
= v_buf
+ uv_pitch
* source_uv_row
+ source_uv_left
;
472 const uint8
* y1_ptr
= NULL
;
473 const uint8
* u1_ptr
= NULL
;
474 const uint8
* v1_ptr
= NULL
;
476 // Locate the second row for interpolation, being careful not to overrun.
477 if (source_row
+ 1 >= source_height
) {
480 y1_ptr
= y0_ptr
+ y_pitch
;
482 if (source_uv_row
+ 1 >= (source_height
+ 1) / 2) {
486 u1_ptr
= u0_ptr
+ uv_pitch
;
487 v1_ptr
= v0_ptr
+ uv_pitch
;
490 if (!kAvoidUsingOptimizedFilter
) {
491 // Vertical scaler uses 16.8 fixed point.
492 int fraction
= (source_top
& kFractionMask
) >> 8;
493 g_filter_yuv_rows_proc_(
494 y_temp
+ source_y_left
, y0_ptr
, y1_ptr
, source_y_width
, fraction
);
495 g_filter_yuv_rows_proc_(
496 u_temp
+ source_uv_left
, u0_ptr
, u1_ptr
, source_uv_width
, fraction
);
497 g_filter_yuv_rows_proc_(
498 v_temp
+ source_uv_left
, v0_ptr
, v1_ptr
, source_uv_width
, fraction
);
500 // Perform horizontal interpolation and color space conversion.
501 // TODO(hclam): Use the MMX version after more testing.
502 LinearScaleYUVToRGB32RowWithRange_C(y_temp
,
510 // If the frame is too large then we linear scale a single row.
511 LinearScaleYUVToRGB32RowWithRange_C(y0_ptr
,
520 // Advance vertically in the source and destination image.
521 source_top
+= y_step
;
522 rgb_buf
+= rgb_pitch
;
525 g_empty_register_state_proc_();
528 void ConvertRGB32ToYUV(const uint8
* rgbframe
,
537 g_convert_rgb32_to_yuv_proc_(rgbframe
,
548 void ConvertRGB24ToYUV(const uint8
* rgbframe
,
557 g_convert_rgb24_to_yuv_proc_(rgbframe
,
568 void ConvertYUY2ToYUV(const uint8
* src
,
574 for (int i
= 0; i
< height
/ 2; ++i
) {
575 for (int j
= 0; j
< (width
/ 2); ++j
) {
585 for (int j
= 0; j
< (width
/ 2); ++j
) {
594 void ConvertNV21ToYUV(const uint8
* src
,
600 int y_plane_size
= width
* height
;
601 memcpy(yplane
, src
, y_plane_size
);
604 int u_plane_size
= y_plane_size
>> 2;
605 for (int i
= 0; i
< u_plane_size
; ++i
) {
611 void ConvertYUVToRGB32(const uint8
* yplane
,
621 g_convert_yuv_to_rgb32_proc_(yplane
,
633 void ConvertYUVAToARGB(const uint8
* yplane
,
645 g_convert_yuva_to_argb_proc_(yplane
,