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 "build/build_config.h"
24 #include "media/base/simd/convert_rgb_to_yuv.h"
25 #include "media/base/simd/convert_yuv_to_rgb.h"
26 #include "media/base/simd/filter_yuv.h"
28 #if defined(ARCH_CPU_X86_FAMILY)
29 #if defined(COMPILER_MSVC)
36 // Assembly functions are declared without namespace.
37 extern "C" { void EmptyRegisterState_MMX(); } // extern "C"
41 typedef void (*FilterYUVRowsProc
)(uint8
*, const uint8
*, const uint8
*, int, int);
43 typedef void (*ConvertRGBToYUVProc
)(const uint8
*,
53 typedef void (*ConvertYUVToRGB32Proc
)(const uint8
*,
64 typedef void (*ConvertYUVAToARGBProc
)(const uint8
*,
77 typedef void (*ConvertYUVToRGB32RowProc
)(const uint8
*,
83 typedef void (*ConvertYUVAToARGBRowProc
)(const uint8
*,
90 typedef void (*ScaleYUVToRGB32RowProc
)(const uint8
*,
97 static FilterYUVRowsProc g_filter_yuv_rows_proc_
= NULL
;
98 static ConvertYUVToRGB32RowProc g_convert_yuv_to_rgb32_row_proc_
= NULL
;
99 static ScaleYUVToRGB32RowProc g_scale_yuv_to_rgb32_row_proc_
= NULL
;
100 static ScaleYUVToRGB32RowProc g_linear_scale_yuv_to_rgb32_row_proc_
= NULL
;
101 static ConvertRGBToYUVProc g_convert_rgb32_to_yuv_proc_
= NULL
;
102 static ConvertRGBToYUVProc g_convert_rgb24_to_yuv_proc_
= NULL
;
103 static ConvertYUVToRGB32Proc g_convert_yuv_to_rgb32_proc_
= NULL
;
104 static ConvertYUVAToARGBProc g_convert_yuva_to_argb_proc_
= NULL
;
106 // Empty SIMD registers state after using them.
107 void EmptyRegisterStateStub() {}
108 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
109 void EmptyRegisterStateIntrinsic() { _mm_empty(); }
111 typedef void (*EmptyRegisterStateProc
)();
112 static EmptyRegisterStateProc g_empty_register_state_proc_
= NULL
;
114 void InitializeCPUSpecificYUVConversions() {
115 CHECK(!g_filter_yuv_rows_proc_
);
116 CHECK(!g_convert_yuv_to_rgb32_row_proc_
);
117 CHECK(!g_scale_yuv_to_rgb32_row_proc_
);
118 CHECK(!g_linear_scale_yuv_to_rgb32_row_proc_
);
119 CHECK(!g_convert_rgb32_to_yuv_proc_
);
120 CHECK(!g_convert_rgb24_to_yuv_proc_
);
121 CHECK(!g_convert_yuv_to_rgb32_proc_
);
122 CHECK(!g_convert_yuva_to_argb_proc_
);
123 CHECK(!g_empty_register_state_proc_
);
125 g_filter_yuv_rows_proc_
= FilterYUVRows_C
;
126 g_convert_yuv_to_rgb32_row_proc_
= ConvertYUVToRGB32Row_C
;
127 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_C
;
128 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_C
;
129 g_convert_rgb32_to_yuv_proc_
= ConvertRGB32ToYUV_C
;
130 g_convert_rgb24_to_yuv_proc_
= ConvertRGB24ToYUV_C
;
131 g_convert_yuv_to_rgb32_proc_
= ConvertYUVToRGB32_C
;
132 g_convert_yuva_to_argb_proc_
= ConvertYUVAToARGB_C
;
133 g_empty_register_state_proc_
= EmptyRegisterStateStub
;
135 #if defined(ARCH_CPU_X86_FAMILY)
138 g_convert_yuv_to_rgb32_row_proc_
= ConvertYUVToRGB32Row_MMX
;
139 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_MMX
;
140 g_convert_yuv_to_rgb32_proc_
= ConvertYUVToRGB32_MMX
;
141 g_convert_yuva_to_argb_proc_
= ConvertYUVAToARGB_MMX
;
142 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_MMX
;
144 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
145 g_filter_yuv_rows_proc_
= FilterYUVRows_MMX
;
146 g_empty_register_state_proc_
= EmptyRegisterStateIntrinsic
;
148 g_empty_register_state_proc_
= EmptyRegisterState_MMX
;
153 g_convert_yuv_to_rgb32_row_proc_
= ConvertYUVToRGB32Row_SSE
;
154 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_SSE
;
155 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_SSE
;
156 g_convert_yuv_to_rgb32_proc_
= ConvertYUVToRGB32_SSE
;
159 if (cpu
.has_sse2()) {
160 g_filter_yuv_rows_proc_
= FilterYUVRows_SSE2
;
161 g_convert_rgb32_to_yuv_proc_
= ConvertRGB32ToYUV_SSE2
;
163 #if defined(ARCH_CPU_X86_64)
164 g_scale_yuv_to_rgb32_row_proc_
= ScaleYUVToRGB32Row_SSE2_X64
;
166 // Technically this should be in the MMX section, but MSVC will optimize out
167 // the export of LinearScaleYUVToRGB32Row_MMX, which is required by the unit
168 // tests, if that decision can be made at compile time. Since all X64 CPUs
169 // have SSE2, we can hack around this by making the selection here.
170 g_linear_scale_yuv_to_rgb32_row_proc_
= LinearScaleYUVToRGB32Row_MMX_X64
;
174 if (cpu
.has_ssse3()) {
175 g_convert_rgb24_to_yuv_proc_
= &ConvertRGB24ToYUV_SSSE3
;
177 // TODO(hclam): Add ConvertRGB32ToYUV_SSSE3 when the cyan problem is solved.
178 // See: crbug.com/100462
183 // Empty SIMD registers state after using them.
184 void EmptyRegisterState() { g_empty_register_state_proc_(); }
186 // 16.16 fixed point arithmetic
187 const int kFractionBits
= 16;
188 const int kFractionMax
= 1 << kFractionBits
;
189 const int kFractionMask
= ((1 << kFractionBits
) - 1);
191 // Scale a frame of YUV to 32 bit ARGB.
192 void ScaleYUVToRGB32(const uint8
* y_buf
,
205 ScaleFilter filter
) {
206 // Handle zero sized sources and destinations.
207 if ((yuv_type
== YV12
&& (source_width
< 2 || source_height
< 2)) ||
208 (yuv_type
== YV16
&& (source_width
< 2 || source_height
< 1)) ||
209 width
== 0 || height
== 0)
212 // 4096 allows 3 buffers to fit in 12k.
213 // Helps performance on CPU with 16K L1 cache.
214 // Large enough for 3830x2160 and 30" displays which are 2560x1600.
215 const int kFilterBufferSize
= 4096;
216 // Disable filtering if the screen is too big (to avoid buffer overflows).
217 // This should never happen to regular users: they don't have monitors
218 // wider than 4096 pixels.
219 // TODO(fbarchard): Allow rotated videos to filter.
220 if (source_width
> kFilterBufferSize
|| view_rotate
)
221 filter
= FILTER_NONE
;
223 unsigned int y_shift
= yuv_type
;
224 // Diagram showing origin and direction of source sampling.
230 // Rotations that start at right side of image.
231 if ((view_rotate
== ROTATE_180
) || (view_rotate
== ROTATE_270
) ||
232 (view_rotate
== MIRROR_ROTATE_0
) || (view_rotate
== MIRROR_ROTATE_90
)) {
233 y_buf
+= source_width
- 1;
234 u_buf
+= source_width
/ 2 - 1;
235 v_buf
+= source_width
/ 2 - 1;
236 source_width
= -source_width
;
238 // Rotations that start at bottom of image.
239 if ((view_rotate
== ROTATE_90
) || (view_rotate
== ROTATE_180
) ||
240 (view_rotate
== MIRROR_ROTATE_90
) || (view_rotate
== MIRROR_ROTATE_180
)) {
241 y_buf
+= (source_height
- 1) * y_pitch
;
242 u_buf
+= ((source_height
>> y_shift
) - 1) * uv_pitch
;
243 v_buf
+= ((source_height
>> y_shift
) - 1) * uv_pitch
;
244 source_height
= -source_height
;
247 int source_dx
= source_width
* kFractionMax
/ width
;
249 if ((view_rotate
== ROTATE_90
) || (view_rotate
== ROTATE_270
)) {
254 source_height
= source_width
;
256 int source_dy
= source_height
* kFractionMax
/ height
;
257 source_dx
= ((source_dy
>> kFractionBits
) * y_pitch
) << kFractionBits
;
258 if (view_rotate
== ROTATE_90
) {
261 source_height
= -source_height
;
268 // Need padding because FilterRows() will write 1 to 16 extra pixels
269 // after the end for SSE2 version.
270 uint8 yuvbuf
[16 + kFilterBufferSize
* 3 + 16];
272 reinterpret_cast<uint8
*>(reinterpret_cast<uintptr_t>(yuvbuf
+ 15) & ~15);
273 uint8
* ubuf
= ybuf
+ kFilterBufferSize
;
274 uint8
* vbuf
= ubuf
+ kFilterBufferSize
;
276 // TODO(fbarchard): Fixed point math is off by 1 on negatives.
278 // We take a y-coordinate in [0,1] space in the source image space, and
279 // transform to a y-coordinate in [0,1] space in the destination image space.
280 // Note that the coordinate endpoints lie on pixel boundaries, not on pixel
281 // centers: e.g. a two-pixel-high image will have pixel centers at 0.25 and
282 // 0.75. The formula is as follows (in fixed-point arithmetic):
283 // y_dst = dst_height * ((y_src + 0.5) / src_height)
284 // dst_pixel = clamp([0, dst_height - 1], floor(y_dst - 0.5))
285 // Implement this here as an accumulator + delta, to avoid expensive math
287 int source_y_subpixel_accum
=
288 ((kFractionMax
/ 2) * source_height
) / height
- (kFractionMax
/ 2);
289 int source_y_subpixel_delta
= ((1 << kFractionBits
) * source_height
) / height
;
291 // TODO(fbarchard): Split this into separate function for better efficiency.
292 for (int y
= 0; y
< height
; ++y
) {
293 uint8
* dest_pixel
= rgb_buf
+ y
* rgb_pitch
;
294 int source_y_subpixel
= source_y_subpixel_accum
;
295 source_y_subpixel_accum
+= source_y_subpixel_delta
;
296 if (source_y_subpixel
< 0)
297 source_y_subpixel
= 0;
298 else if (source_y_subpixel
> ((source_height
- 1) << kFractionBits
))
299 source_y_subpixel
= (source_height
- 1) << kFractionBits
;
301 const uint8
* y_ptr
= NULL
;
302 const uint8
* u_ptr
= NULL
;
303 const uint8
* v_ptr
= NULL
;
304 // Apply vertical filtering if necessary.
305 // TODO(fbarchard): Remove memcpy when not necessary.
306 if (filter
& media::FILTER_BILINEAR_V
) {
307 int source_y
= source_y_subpixel
>> kFractionBits
;
308 y_ptr
= y_buf
+ source_y
* y_pitch
;
309 u_ptr
= u_buf
+ (source_y
>> y_shift
) * uv_pitch
;
310 v_ptr
= v_buf
+ (source_y
>> y_shift
) * uv_pitch
;
312 // Vertical scaler uses 16.8 fixed point.
313 int source_y_fraction
= (source_y_subpixel
& kFractionMask
) >> 8;
314 if (source_y_fraction
!= 0) {
315 g_filter_yuv_rows_proc_(
316 ybuf
, y_ptr
, y_ptr
+ y_pitch
, source_width
, source_y_fraction
);
318 memcpy(ybuf
, y_ptr
, source_width
);
321 ybuf
[source_width
] = ybuf
[source_width
- 1];
323 int uv_source_width
= (source_width
+ 1) / 2;
324 int source_uv_fraction
;
326 // For formats with half-height UV planes, each even-numbered pixel row
327 // should not interpolate, since the next row to interpolate from should
328 // be a duplicate of the current row.
329 if (y_shift
&& (source_y
& 0x1) == 0)
330 source_uv_fraction
= 0;
332 source_uv_fraction
= source_y_fraction
;
334 if (source_uv_fraction
!= 0) {
335 g_filter_yuv_rows_proc_(
336 ubuf
, u_ptr
, u_ptr
+ uv_pitch
, uv_source_width
, source_uv_fraction
);
337 g_filter_yuv_rows_proc_(
338 vbuf
, v_ptr
, v_ptr
+ uv_pitch
, uv_source_width
, source_uv_fraction
);
340 memcpy(ubuf
, u_ptr
, uv_source_width
);
341 memcpy(vbuf
, v_ptr
, uv_source_width
);
345 ubuf
[uv_source_width
] = ubuf
[uv_source_width
- 1];
346 vbuf
[uv_source_width
] = vbuf
[uv_source_width
- 1];
348 // Offset by 1/2 pixel for center sampling.
349 int source_y
= (source_y_subpixel
+ (kFractionMax
/ 2)) >> kFractionBits
;
350 y_ptr
= y_buf
+ source_y
* y_pitch
;
351 u_ptr
= u_buf
+ (source_y
>> y_shift
) * uv_pitch
;
352 v_ptr
= v_buf
+ (source_y
>> y_shift
) * uv_pitch
;
354 if (source_dx
== kFractionMax
) { // Not scaled
355 g_convert_yuv_to_rgb32_row_proc_(y_ptr
, u_ptr
, v_ptr
, dest_pixel
, width
);
357 if (filter
& FILTER_BILINEAR_H
) {
358 g_linear_scale_yuv_to_rgb32_row_proc_(
359 y_ptr
, u_ptr
, v_ptr
, dest_pixel
, width
, source_dx
);
361 g_scale_yuv_to_rgb32_row_proc_(
362 y_ptr
, u_ptr
, v_ptr
, dest_pixel
, width
, source_dx
);
367 g_empty_register_state_proc_();
370 // Scale a frame of YV12 to 32 bit ARGB for a specific rectangle.
371 void ScaleYUVToRGB32WithRect(const uint8
* y_buf
,
382 int dest_rect_bottom
,
386 // This routine doesn't currently support up-scaling.
387 CHECK_LE(dest_width
, source_width
);
388 CHECK_LE(dest_height
, source_height
);
390 // Sanity-check the destination rectangle.
391 DCHECK(dest_rect_left
>= 0 && dest_rect_right
<= dest_width
);
392 DCHECK(dest_rect_top
>= 0 && dest_rect_bottom
<= dest_height
);
393 DCHECK(dest_rect_right
> dest_rect_left
);
394 DCHECK(dest_rect_bottom
> dest_rect_top
);
396 // Fixed-point value of vertical and horizontal scale down factor.
397 // Values are in the format 16.16.
398 int y_step
= kFractionMax
* source_height
/ dest_height
;
399 int x_step
= kFractionMax
* source_width
/ dest_width
;
401 // Determine the coordinates of the rectangle in 16.16 coords.
402 // NB: Our origin is the *center* of the top/left pixel, NOT its top/left.
403 // If we're down-scaling by more than a factor of two, we start with a 50%
404 // fraction to avoid degenerating to point-sampling - we should really just
405 // fix the fraction at 50% for all pixels in that case.
406 int source_left
= dest_rect_left
* x_step
;
407 int source_right
= (dest_rect_right
- 1) * x_step
;
408 if (x_step
< kFractionMax
* 2) {
409 source_left
+= ((x_step
- kFractionMax
) / 2);
410 source_right
+= ((x_step
- kFractionMax
) / 2);
412 source_left
+= kFractionMax
/ 2;
413 source_right
+= kFractionMax
/ 2;
415 int source_top
= dest_rect_top
* y_step
;
416 if (y_step
< kFractionMax
* 2) {
417 source_top
+= ((y_step
- kFractionMax
) / 2);
419 source_top
+= kFractionMax
/ 2;
422 // Determine the parts of the Y, U and V buffers to interpolate.
423 int source_y_left
= source_left
>> kFractionBits
;
425 std::min((source_right
>> kFractionBits
) + 2, source_width
+ 1);
427 int source_uv_left
= source_y_left
/ 2;
428 int source_uv_right
= std::min((source_right
>> (kFractionBits
+ 1)) + 2,
429 (source_width
+ 1) / 2);
431 int source_y_width
= source_y_right
- source_y_left
;
432 int source_uv_width
= source_uv_right
- source_uv_left
;
434 // Determine number of pixels in each output row.
435 int dest_rect_width
= dest_rect_right
- dest_rect_left
;
437 // Intermediate buffer for vertical interpolation.
438 // 4096 bytes allows 3 buffers to fit in 12k, which fits in a 16K L1 cache,
439 // and is bigger than most users will generally need.
440 // The buffer is 16-byte aligned and padded with 16 extra bytes; some of the
441 // FilterYUVRowProcs have alignment requirements, and the SSE version can
442 // write up to 16 bytes past the end of the buffer.
443 const int kFilterBufferSize
= 4096;
444 const bool kAvoidUsingOptimizedFilter
= source_width
> kFilterBufferSize
;
445 uint8 yuv_temp
[16 + kFilterBufferSize
* 3 + 16];
446 uint8
* y_temp
= reinterpret_cast<uint8
*>(
447 reinterpret_cast<uintptr_t>(yuv_temp
+ 15) & ~15);
448 uint8
* u_temp
= y_temp
+ kFilterBufferSize
;
449 uint8
* v_temp
= u_temp
+ kFilterBufferSize
;
451 // Move to the top-left pixel of output.
452 rgb_buf
+= dest_rect_top
* rgb_pitch
;
453 rgb_buf
+= dest_rect_left
* 4;
455 // For each destination row perform interpolation and color space
456 // conversion to produce the output.
457 for (int row
= dest_rect_top
; row
< dest_rect_bottom
; ++row
) {
458 // Round the fixed-point y position to get the current row.
459 int source_row
= source_top
>> kFractionBits
;
460 int source_uv_row
= source_row
/ 2;
461 DCHECK(source_row
< source_height
);
463 // Locate the first row for each plane for interpolation.
464 const uint8
* y0_ptr
= y_buf
+ y_pitch
* source_row
+ source_y_left
;
465 const uint8
* u0_ptr
= u_buf
+ uv_pitch
* source_uv_row
+ source_uv_left
;
466 const uint8
* v0_ptr
= v_buf
+ uv_pitch
* source_uv_row
+ source_uv_left
;
467 const uint8
* y1_ptr
= NULL
;
468 const uint8
* u1_ptr
= NULL
;
469 const uint8
* v1_ptr
= NULL
;
471 // Locate the second row for interpolation, being careful not to overrun.
472 if (source_row
+ 1 >= source_height
) {
475 y1_ptr
= y0_ptr
+ y_pitch
;
477 if (source_uv_row
+ 1 >= (source_height
+ 1) / 2) {
481 u1_ptr
= u0_ptr
+ uv_pitch
;
482 v1_ptr
= v0_ptr
+ uv_pitch
;
485 if (!kAvoidUsingOptimizedFilter
) {
486 // Vertical scaler uses 16.8 fixed point.
487 int fraction
= (source_top
& kFractionMask
) >> 8;
488 g_filter_yuv_rows_proc_(
489 y_temp
+ source_y_left
, y0_ptr
, y1_ptr
, source_y_width
, fraction
);
490 g_filter_yuv_rows_proc_(
491 u_temp
+ source_uv_left
, u0_ptr
, u1_ptr
, source_uv_width
, fraction
);
492 g_filter_yuv_rows_proc_(
493 v_temp
+ source_uv_left
, v0_ptr
, v1_ptr
, source_uv_width
, fraction
);
495 // Perform horizontal interpolation and color space conversion.
496 // TODO(hclam): Use the MMX version after more testing.
497 LinearScaleYUVToRGB32RowWithRange_C(y_temp
,
505 // If the frame is too large then we linear scale a single row.
506 LinearScaleYUVToRGB32RowWithRange_C(y0_ptr
,
515 // Advance vertically in the source and destination image.
516 source_top
+= y_step
;
517 rgb_buf
+= rgb_pitch
;
520 g_empty_register_state_proc_();
523 void ConvertRGB32ToYUV(const uint8
* rgbframe
,
532 g_convert_rgb32_to_yuv_proc_(rgbframe
,
543 void ConvertRGB24ToYUV(const uint8
* rgbframe
,
552 g_convert_rgb24_to_yuv_proc_(rgbframe
,
563 void ConvertYUY2ToYUV(const uint8
* src
,
569 for (int i
= 0; i
< height
/ 2; ++i
) {
570 for (int j
= 0; j
< (width
/ 2); ++j
) {
580 for (int j
= 0; j
< (width
/ 2); ++j
) {
589 void ConvertNV21ToYUV(const uint8
* src
,
595 int y_plane_size
= width
* height
;
596 memcpy(yplane
, src
, y_plane_size
);
599 int u_plane_size
= y_plane_size
>> 2;
600 for (int i
= 0; i
< u_plane_size
; ++i
) {
606 void ConvertYUVToRGB32(const uint8
* yplane
,
616 g_convert_yuv_to_rgb32_proc_(yplane
,
628 void ConvertYUVAToARGB(const uint8
* yplane
,
640 g_convert_yuva_to_argb_proc_(yplane
,