Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / media / base / yuv_convert.cc
blobd4c1d900b2cc5decb8095fcb46566d7662a92fad
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"
20 #include "base/cpu.h"
21 #include "base/lazy_instance.h"
22 #include "base/logging.h"
23 #include "base/macros.h"
24 #include "base/memory/aligned_memory.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
27 #include "build/build_config.h"
28 #include "media/base/simd/convert_rgb_to_yuv.h"
29 #include "media/base/simd/convert_yuv_to_rgb.h"
30 #include "media/base/simd/filter_yuv.h"
32 #if defined(ARCH_CPU_X86_FAMILY)
33 #if defined(COMPILER_MSVC)
34 #include <intrin.h>
35 #else
36 #include <mmintrin.h>
37 #endif
38 #endif
40 // Assembly functions are declared without namespace.
41 extern "C" { void EmptyRegisterState_MMX(); } // extern "C"
43 namespace media {
45 typedef void (*FilterYUVRowsProc)(uint8*,
46 const uint8*,
47 const uint8*,
48 int,
49 uint8);
51 typedef void (*ConvertRGBToYUVProc)(const uint8*,
52 uint8*,
53 uint8*,
54 uint8*,
55 int,
56 int,
57 int,
58 int,
59 int);
61 typedef void (*ConvertYUVToRGB32Proc)(const uint8*,
62 const uint8*,
63 const uint8*,
64 uint8*,
65 int,
66 int,
67 int,
68 int,
69 int,
70 YUVType);
72 typedef void (*ConvertYUVAToARGBProc)(const uint8*,
73 const uint8*,
74 const uint8*,
75 const uint8*,
76 uint8*,
77 int,
78 int,
79 int,
80 int,
81 int,
82 int,
83 YUVType);
85 typedef void (*ConvertYUVToRGB32RowProc)(const uint8*,
86 const uint8*,
87 const uint8*,
88 uint8*,
89 ptrdiff_t,
90 const int16*);
92 typedef void (*ConvertYUVAToARGBRowProc)(const uint8*,
93 const uint8*,
94 const uint8*,
95 const uint8*,
96 uint8*,
97 ptrdiff_t,
98 const int16*);
100 typedef void (*ScaleYUVToRGB32RowProc)(const uint8*,
101 const uint8*,
102 const uint8*,
103 uint8*,
104 ptrdiff_t,
105 ptrdiff_t,
106 const int16*);
108 static FilterYUVRowsProc g_filter_yuv_rows_proc_ = NULL;
109 static ConvertYUVToRGB32RowProc g_convert_yuv_to_rgb32_row_proc_ = NULL;
110 static ScaleYUVToRGB32RowProc g_scale_yuv_to_rgb32_row_proc_ = NULL;
111 static ScaleYUVToRGB32RowProc g_linear_scale_yuv_to_rgb32_row_proc_ = NULL;
112 static ConvertRGBToYUVProc g_convert_rgb32_to_yuv_proc_ = NULL;
113 static ConvertRGBToYUVProc g_convert_rgb24_to_yuv_proc_ = NULL;
114 static ConvertYUVToRGB32Proc g_convert_yuv_to_rgb32_proc_ = NULL;
115 static ConvertYUVAToARGBProc g_convert_yuva_to_argb_proc_ = NULL;
117 static const int kYUVToRGBTableSize = 256 * 4 * 4 * sizeof(int16);
119 // base::AlignedMemory has a private operator new(), so wrap it in a struct so
120 // that we can put it in a LazyInstance::Leaky.
121 struct YUVToRGBTableWrapper {
122 base::AlignedMemory<kYUVToRGBTableSize, 16> table;
125 typedef base::LazyInstance<YUVToRGBTableWrapper>::Leaky
126 YUVToRGBTable;
127 static YUVToRGBTable g_table_rec601 = LAZY_INSTANCE_INITIALIZER;
128 static YUVToRGBTable g_table_jpeg = LAZY_INSTANCE_INITIALIZER;
129 static YUVToRGBTable g_table_rec709 = LAZY_INSTANCE_INITIALIZER;
130 static const int16* g_table_rec601_ptr = NULL;
131 static const int16* g_table_jpeg_ptr = NULL;
132 static const int16* g_table_rec709_ptr = NULL;
134 // Empty SIMD registers state after using them.
135 void EmptyRegisterStateStub() {}
136 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
137 void EmptyRegisterStateIntrinsic() { _mm_empty(); }
138 #endif
139 typedef void (*EmptyRegisterStateProc)();
140 static EmptyRegisterStateProc g_empty_register_state_proc_ = NULL;
142 // Get the appropriate value to bitshift by for vertical indices.
143 int GetVerticalShift(YUVType type) {
144 switch (type) {
145 case YV16:
146 return 0;
147 case YV12:
148 case YV12J:
149 case YV12HD:
150 return 1;
152 NOTREACHED();
153 return 0;
156 const int16* GetLookupTable(YUVType type) {
157 switch (type) {
158 case YV12:
159 case YV16:
160 return g_table_rec601_ptr;
161 case YV12J:
162 return g_table_jpeg_ptr;
163 case YV12HD:
164 return g_table_rec709_ptr;
166 NOTREACHED();
167 return NULL;
170 // Populates a pre-allocated lookup table from a YUV->RGB matrix.
171 const int16* PopulateYUVToRGBTable(const double matrix[3][3],
172 bool full_range,
173 int16* table) {
174 // We'll have 4 sub-tables that lie contiguous in memory, one for each of Y,
175 // U, V and A.
176 const int kNumTables = 4;
177 // Each table has 256 rows (for all possible 8-bit values).
178 const int kNumRows = 256;
179 // Each row has 4 columns, for contributions to each of R, G, B and A.
180 const int kNumColumns = 4;
181 // Each element is a fixed-point (10.6) 16-bit signed value.
182 const int kElementSize = sizeof(int16);
184 // Sanity check that our constants here match the size of the statically
185 // allocated tables.
186 COMPILE_ASSERT(
187 kNumTables * kNumRows * kNumColumns * kElementSize == kYUVToRGBTableSize,
188 "YUV lookup table size doesn't match expectation.");
190 // Y needs an offset of -16 for color ranges that ignore the lower 16 values,
191 // U and V get -128 to put them in [-128, 127] from [0, 255].
192 int offsets[3] = {(full_range ? 0 : -16), -128, -128};
194 for (int i = 0; i < kNumRows; ++i) {
195 // Y, U, and V contributions to each of R, G, B and A.
196 for (int j = 0; j < 3; ++j) {
197 #if defined(OS_ANDROID)
198 // Android is RGBA.
199 table[(j * kNumRows + i) * kNumColumns + 0] =
200 matrix[j][0] * 64 * (i + offsets[j]) + 0.5;
201 table[(j * kNumRows + i) * kNumColumns + 1] =
202 matrix[j][1] * 64 * (i + offsets[j]) + 0.5;
203 table[(j * kNumRows + i) * kNumColumns + 2] =
204 matrix[j][2] * 64 * (i + offsets[j]) + 0.5;
205 #else
206 // Other platforms are BGRA.
207 table[(j * kNumRows + i) * kNumColumns + 0] =
208 matrix[j][2] * 64 * (i + offsets[j]) + 0.5;
209 table[(j * kNumRows + i) * kNumColumns + 1] =
210 matrix[j][1] * 64 * (i + offsets[j]) + 0.5;
211 table[(j * kNumRows + i) * kNumColumns + 2] =
212 matrix[j][0] * 64 * (i + offsets[j]) + 0.5;
213 #endif
214 // Alpha contributions from Y and V are always 0. U is set such that
215 // all values result in a full '255' alpha value.
216 table[(j * kNumRows + i) * kNumColumns + 3] = (j == 1) ? 256 * 64 - 1 : 0;
218 // And YUVA alpha is passed through as-is.
219 for (int k = 0; k < kNumTables; ++k)
220 table[((kNumTables - 1) * kNumRows + i) * kNumColumns + k] = i;
223 return table;
226 void InitializeCPUSpecificYUVConversions() {
227 CHECK(!g_filter_yuv_rows_proc_);
228 CHECK(!g_convert_yuv_to_rgb32_row_proc_);
229 CHECK(!g_scale_yuv_to_rgb32_row_proc_);
230 CHECK(!g_linear_scale_yuv_to_rgb32_row_proc_);
231 CHECK(!g_convert_rgb32_to_yuv_proc_);
232 CHECK(!g_convert_rgb24_to_yuv_proc_);
233 CHECK(!g_convert_yuv_to_rgb32_proc_);
234 CHECK(!g_convert_yuva_to_argb_proc_);
235 CHECK(!g_empty_register_state_proc_);
237 g_filter_yuv_rows_proc_ = FilterYUVRows_C;
238 g_convert_yuv_to_rgb32_row_proc_ = ConvertYUVToRGB32Row_C;
239 g_scale_yuv_to_rgb32_row_proc_ = ScaleYUVToRGB32Row_C;
240 g_linear_scale_yuv_to_rgb32_row_proc_ = LinearScaleYUVToRGB32Row_C;
241 g_convert_rgb32_to_yuv_proc_ = ConvertRGB32ToYUV_C;
242 g_convert_rgb24_to_yuv_proc_ = ConvertRGB24ToYUV_C;
243 g_convert_yuv_to_rgb32_proc_ = ConvertYUVToRGB32_C;
244 g_convert_yuva_to_argb_proc_ = ConvertYUVAToARGB_C;
245 g_empty_register_state_proc_ = EmptyRegisterStateStub;
247 // Assembly code confuses MemorySanitizer.
248 #if defined(ARCH_CPU_X86_FAMILY) && !defined(MEMORY_SANITIZER)
249 g_convert_yuva_to_argb_proc_ = ConvertYUVAToARGB_MMX;
251 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
252 g_empty_register_state_proc_ = EmptyRegisterStateIntrinsic;
253 #else
254 g_empty_register_state_proc_ = EmptyRegisterState_MMX;
255 #endif
257 g_convert_yuv_to_rgb32_row_proc_ = ConvertYUVToRGB32Row_SSE;
258 g_convert_yuv_to_rgb32_proc_ = ConvertYUVToRGB32_SSE;
260 g_filter_yuv_rows_proc_ = FilterYUVRows_SSE2;
261 g_convert_rgb32_to_yuv_proc_ = ConvertRGB32ToYUV_SSE2;
263 #if defined(ARCH_CPU_X86_64)
264 g_scale_yuv_to_rgb32_row_proc_ = ScaleYUVToRGB32Row_SSE2_X64;
266 // Technically this should be in the MMX section, but MSVC will optimize out
267 // the export of LinearScaleYUVToRGB32Row_MMX, which is required by the unit
268 // tests, if that decision can be made at compile time. Since all X64 CPUs
269 // have SSE2, we can hack around this by making the selection here.
270 g_linear_scale_yuv_to_rgb32_row_proc_ = LinearScaleYUVToRGB32Row_MMX_X64;
271 #else
272 g_scale_yuv_to_rgb32_row_proc_ = ScaleYUVToRGB32Row_SSE;
273 g_linear_scale_yuv_to_rgb32_row_proc_ = LinearScaleYUVToRGB32Row_SSE;
274 #endif
276 base::CPU cpu;
277 if (cpu.has_ssse3()) {
278 g_convert_rgb24_to_yuv_proc_ = &ConvertRGB24ToYUV_SSSE3;
280 // TODO(hclam): Add ConvertRGB32ToYUV_SSSE3 when the cyan problem is solved.
281 // See: crbug.com/100462
283 #endif
285 // Initialize YUV conversion lookup tables.
287 // SD Rec601 YUV->RGB matrix, see http://www.fourcc.org/fccyvrgb.php
288 const double kRec601ConvertMatrix[3][3] = {
289 {1.164, 1.164, 1.164}, {0.0, -0.391, 2.018}, {1.596, -0.813, 0.0},
292 // JPEG table, values from above link.
293 const double kJPEGConvertMatrix[3][3] = {
294 {1.0, 1.0, 1.0}, {0.0, -0.34414, 1.772}, {1.402, -0.71414, 0.0},
297 // Rec709 "HD" color space, values from:
298 // http://www.equasys.de/colorconversion.html
299 const double kRec709ConvertMatrix[3][3] = {
300 {1.164, 1.164, 1.164}, {0.0, -0.213, 2.112}, {1.793, -0.533, 0.0},
303 PopulateYUVToRGBTable(kRec601ConvertMatrix, false,
304 g_table_rec601.Get().table.data_as<int16>());
305 PopulateYUVToRGBTable(kJPEGConvertMatrix, true,
306 g_table_jpeg.Get().table.data_as<int16>());
307 PopulateYUVToRGBTable(kRec709ConvertMatrix, false,
308 g_table_rec709.Get().table.data_as<int16>());
309 g_table_rec601_ptr = g_table_rec601.Get().table.data_as<int16>();
310 g_table_rec709_ptr = g_table_rec709.Get().table.data_as<int16>();
311 g_table_jpeg_ptr = g_table_jpeg.Get().table.data_as<int16>();
314 // Empty SIMD registers state after using them.
315 void EmptyRegisterState() { g_empty_register_state_proc_(); }
317 // 16.16 fixed point arithmetic
318 const int kFractionBits = 16;
319 const int kFractionMax = 1 << kFractionBits;
320 const int kFractionMask = ((1 << kFractionBits) - 1);
322 // Scale a frame of YUV to 32 bit ARGB.
323 void ScaleYUVToRGB32(const uint8* y_buf,
324 const uint8* u_buf,
325 const uint8* v_buf,
326 uint8* rgb_buf,
327 int source_width,
328 int source_height,
329 int width,
330 int height,
331 int y_pitch,
332 int uv_pitch,
333 int rgb_pitch,
334 YUVType yuv_type,
335 Rotate view_rotate,
336 ScaleFilter filter) {
337 // Handle zero sized sources and destinations.
338 if ((yuv_type == YV12 && (source_width < 2 || source_height < 2)) ||
339 (yuv_type == YV16 && (source_width < 2 || source_height < 1)) ||
340 width == 0 || height == 0)
341 return;
343 const int16* lookup_table = GetLookupTable(yuv_type);
345 // 4096 allows 3 buffers to fit in 12k.
346 // Helps performance on CPU with 16K L1 cache.
347 // Large enough for 3830x2160 and 30" displays which are 2560x1600.
348 const int kFilterBufferSize = 4096;
349 // Disable filtering if the screen is too big (to avoid buffer overflows).
350 // This should never happen to regular users: they don't have monitors
351 // wider than 4096 pixels.
352 // TODO(fbarchard): Allow rotated videos to filter.
353 if (source_width > kFilterBufferSize || view_rotate)
354 filter = FILTER_NONE;
356 unsigned int y_shift = GetVerticalShift(yuv_type);
357 // Diagram showing origin and direction of source sampling.
358 // ->0 4<-
359 // 7 3
361 // 6 5
362 // ->1 2<-
363 // Rotations that start at right side of image.
364 if ((view_rotate == ROTATE_180) || (view_rotate == ROTATE_270) ||
365 (view_rotate == MIRROR_ROTATE_0) || (view_rotate == MIRROR_ROTATE_90)) {
366 y_buf += source_width - 1;
367 u_buf += source_width / 2 - 1;
368 v_buf += source_width / 2 - 1;
369 source_width = -source_width;
371 // Rotations that start at bottom of image.
372 if ((view_rotate == ROTATE_90) || (view_rotate == ROTATE_180) ||
373 (view_rotate == MIRROR_ROTATE_90) || (view_rotate == MIRROR_ROTATE_180)) {
374 y_buf += (source_height - 1) * y_pitch;
375 u_buf += ((source_height >> y_shift) - 1) * uv_pitch;
376 v_buf += ((source_height >> y_shift) - 1) * uv_pitch;
377 source_height = -source_height;
380 int source_dx = source_width * kFractionMax / width;
382 if ((view_rotate == ROTATE_90) || (view_rotate == ROTATE_270)) {
383 int tmp = height;
384 height = width;
385 width = tmp;
386 tmp = source_height;
387 source_height = source_width;
388 source_width = tmp;
389 int source_dy = source_height * kFractionMax / height;
390 source_dx = ((source_dy >> kFractionBits) * y_pitch) << kFractionBits;
391 if (view_rotate == ROTATE_90) {
392 y_pitch = -1;
393 uv_pitch = -1;
394 source_height = -source_height;
395 } else {
396 y_pitch = 1;
397 uv_pitch = 1;
401 // Need padding because FilterRows() will write 1 to 16 extra pixels
402 // after the end for SSE2 version.
403 uint8 yuvbuf[16 + kFilterBufferSize * 3 + 16];
404 uint8* ybuf =
405 reinterpret_cast<uint8*>(reinterpret_cast<uintptr_t>(yuvbuf + 15) & ~15);
406 uint8* ubuf = ybuf + kFilterBufferSize;
407 uint8* vbuf = ubuf + kFilterBufferSize;
409 // TODO(fbarchard): Fixed point math is off by 1 on negatives.
411 // We take a y-coordinate in [0,1] space in the source image space, and
412 // transform to a y-coordinate in [0,1] space in the destination image space.
413 // Note that the coordinate endpoints lie on pixel boundaries, not on pixel
414 // centers: e.g. a two-pixel-high image will have pixel centers at 0.25 and
415 // 0.75. The formula is as follows (in fixed-point arithmetic):
416 // y_dst = dst_height * ((y_src + 0.5) / src_height)
417 // dst_pixel = clamp([0, dst_height - 1], floor(y_dst - 0.5))
418 // Implement this here as an accumulator + delta, to avoid expensive math
419 // in the loop.
420 int source_y_subpixel_accum =
421 ((kFractionMax / 2) * source_height) / height - (kFractionMax / 2);
422 int source_y_subpixel_delta = ((1 << kFractionBits) * source_height) / height;
424 // TODO(fbarchard): Split this into separate function for better efficiency.
425 for (int y = 0; y < height; ++y) {
426 uint8* dest_pixel = rgb_buf + y * rgb_pitch;
427 int source_y_subpixel = source_y_subpixel_accum;
428 source_y_subpixel_accum += source_y_subpixel_delta;
429 if (source_y_subpixel < 0)
430 source_y_subpixel = 0;
431 else if (source_y_subpixel > ((source_height - 1) << kFractionBits))
432 source_y_subpixel = (source_height - 1) << kFractionBits;
434 const uint8* y_ptr = NULL;
435 const uint8* u_ptr = NULL;
436 const uint8* v_ptr = NULL;
437 // Apply vertical filtering if necessary.
438 // TODO(fbarchard): Remove memcpy when not necessary.
439 if (filter & media::FILTER_BILINEAR_V) {
440 int source_y = source_y_subpixel >> kFractionBits;
441 y_ptr = y_buf + source_y * y_pitch;
442 u_ptr = u_buf + (source_y >> y_shift) * uv_pitch;
443 v_ptr = v_buf + (source_y >> y_shift) * uv_pitch;
445 // Vertical scaler uses 16.8 fixed point.
446 uint8 source_y_fraction = (source_y_subpixel & kFractionMask) >> 8;
447 if (source_y_fraction != 0) {
448 g_filter_yuv_rows_proc_(
449 ybuf, y_ptr, y_ptr + y_pitch, source_width, source_y_fraction);
450 } else {
451 memcpy(ybuf, y_ptr, source_width);
453 y_ptr = ybuf;
454 ybuf[source_width] = ybuf[source_width - 1];
456 int uv_source_width = (source_width + 1) / 2;
457 uint8 source_uv_fraction;
459 // For formats with half-height UV planes, each even-numbered pixel row
460 // should not interpolate, since the next row to interpolate from should
461 // be a duplicate of the current row.
462 if (y_shift && (source_y & 0x1) == 0)
463 source_uv_fraction = 0;
464 else
465 source_uv_fraction = source_y_fraction;
467 if (source_uv_fraction != 0) {
468 g_filter_yuv_rows_proc_(
469 ubuf, u_ptr, u_ptr + uv_pitch, uv_source_width, source_uv_fraction);
470 g_filter_yuv_rows_proc_(
471 vbuf, v_ptr, v_ptr + uv_pitch, uv_source_width, source_uv_fraction);
472 } else {
473 memcpy(ubuf, u_ptr, uv_source_width);
474 memcpy(vbuf, v_ptr, uv_source_width);
476 u_ptr = ubuf;
477 v_ptr = vbuf;
478 ubuf[uv_source_width] = ubuf[uv_source_width - 1];
479 vbuf[uv_source_width] = vbuf[uv_source_width - 1];
480 } else {
481 // Offset by 1/2 pixel for center sampling.
482 int source_y = (source_y_subpixel + (kFractionMax / 2)) >> kFractionBits;
483 y_ptr = y_buf + source_y * y_pitch;
484 u_ptr = u_buf + (source_y >> y_shift) * uv_pitch;
485 v_ptr = v_buf + (source_y >> y_shift) * uv_pitch;
487 if (source_dx == kFractionMax) { // Not scaled
488 g_convert_yuv_to_rgb32_row_proc_(y_ptr, u_ptr, v_ptr, dest_pixel, width,
489 lookup_table);
490 } else {
491 if (filter & FILTER_BILINEAR_H) {
492 g_linear_scale_yuv_to_rgb32_row_proc_(y_ptr, u_ptr, v_ptr, dest_pixel,
493 width, source_dx,
494 lookup_table);
495 } else {
496 g_scale_yuv_to_rgb32_row_proc_(y_ptr, u_ptr, v_ptr, dest_pixel, width,
497 source_dx, lookup_table);
502 g_empty_register_state_proc_();
505 // Scale a frame of YV12 to 32 bit ARGB for a specific rectangle.
506 void ScaleYUVToRGB32WithRect(const uint8* y_buf,
507 const uint8* u_buf,
508 const uint8* v_buf,
509 uint8* rgb_buf,
510 int source_width,
511 int source_height,
512 int dest_width,
513 int dest_height,
514 int dest_rect_left,
515 int dest_rect_top,
516 int dest_rect_right,
517 int dest_rect_bottom,
518 int y_pitch,
519 int uv_pitch,
520 int rgb_pitch) {
521 // This routine doesn't currently support up-scaling.
522 CHECK_LE(dest_width, source_width);
523 CHECK_LE(dest_height, source_height);
525 // Sanity-check the destination rectangle.
526 DCHECK(dest_rect_left >= 0 && dest_rect_right <= dest_width);
527 DCHECK(dest_rect_top >= 0 && dest_rect_bottom <= dest_height);
528 DCHECK(dest_rect_right > dest_rect_left);
529 DCHECK(dest_rect_bottom > dest_rect_top);
531 const int16* lookup_table = GetLookupTable(YV12);
533 // Fixed-point value of vertical and horizontal scale down factor.
534 // Values are in the format 16.16.
535 int y_step = kFractionMax * source_height / dest_height;
536 int x_step = kFractionMax * source_width / dest_width;
538 // Determine the coordinates of the rectangle in 16.16 coords.
539 // NB: Our origin is the *center* of the top/left pixel, NOT its top/left.
540 // If we're down-scaling by more than a factor of two, we start with a 50%
541 // fraction to avoid degenerating to point-sampling - we should really just
542 // fix the fraction at 50% for all pixels in that case.
543 int source_left = dest_rect_left * x_step;
544 int source_right = (dest_rect_right - 1) * x_step;
545 if (x_step < kFractionMax * 2) {
546 source_left += ((x_step - kFractionMax) / 2);
547 source_right += ((x_step - kFractionMax) / 2);
548 } else {
549 source_left += kFractionMax / 2;
550 source_right += kFractionMax / 2;
552 int source_top = dest_rect_top * y_step;
553 if (y_step < kFractionMax * 2) {
554 source_top += ((y_step - kFractionMax) / 2);
555 } else {
556 source_top += kFractionMax / 2;
559 // Determine the parts of the Y, U and V buffers to interpolate.
560 int source_y_left = source_left >> kFractionBits;
561 int source_y_right =
562 std::min((source_right >> kFractionBits) + 2, source_width + 1);
564 int source_uv_left = source_y_left / 2;
565 int source_uv_right = std::min((source_right >> (kFractionBits + 1)) + 2,
566 (source_width + 1) / 2);
568 int source_y_width = source_y_right - source_y_left;
569 int source_uv_width = source_uv_right - source_uv_left;
571 // Determine number of pixels in each output row.
572 int dest_rect_width = dest_rect_right - dest_rect_left;
574 // Intermediate buffer for vertical interpolation.
575 // 4096 bytes allows 3 buffers to fit in 12k, which fits in a 16K L1 cache,
576 // and is bigger than most users will generally need.
577 // The buffer is 16-byte aligned and padded with 16 extra bytes; some of the
578 // FilterYUVRowsProcs have alignment requirements, and the SSE version can
579 // write up to 16 bytes past the end of the buffer.
580 const int kFilterBufferSize = 4096;
581 const bool kAvoidUsingOptimizedFilter = source_width > kFilterBufferSize;
582 uint8 yuv_temp[16 + kFilterBufferSize * 3 + 16];
583 // memset() yuv_temp to 0 to avoid bogus warnings when running on Valgrind.
584 if (RunningOnValgrind())
585 memset(yuv_temp, 0, sizeof(yuv_temp));
586 uint8* y_temp = reinterpret_cast<uint8*>(
587 reinterpret_cast<uintptr_t>(yuv_temp + 15) & ~15);
588 uint8* u_temp = y_temp + kFilterBufferSize;
589 uint8* v_temp = u_temp + kFilterBufferSize;
591 // Move to the top-left pixel of output.
592 rgb_buf += dest_rect_top * rgb_pitch;
593 rgb_buf += dest_rect_left * 4;
595 // For each destination row perform interpolation and color space
596 // conversion to produce the output.
597 for (int row = dest_rect_top; row < dest_rect_bottom; ++row) {
598 // Round the fixed-point y position to get the current row.
599 int source_row = source_top >> kFractionBits;
600 int source_uv_row = source_row / 2;
601 DCHECK(source_row < source_height);
603 // Locate the first row for each plane for interpolation.
604 const uint8* y0_ptr = y_buf + y_pitch * source_row + source_y_left;
605 const uint8* u0_ptr = u_buf + uv_pitch * source_uv_row + source_uv_left;
606 const uint8* v0_ptr = v_buf + uv_pitch * source_uv_row + source_uv_left;
607 const uint8* y1_ptr = NULL;
608 const uint8* u1_ptr = NULL;
609 const uint8* v1_ptr = NULL;
611 // Locate the second row for interpolation, being careful not to overrun.
612 if (source_row + 1 >= source_height) {
613 y1_ptr = y0_ptr;
614 } else {
615 y1_ptr = y0_ptr + y_pitch;
617 if (source_uv_row + 1 >= (source_height + 1) / 2) {
618 u1_ptr = u0_ptr;
619 v1_ptr = v0_ptr;
620 } else {
621 u1_ptr = u0_ptr + uv_pitch;
622 v1_ptr = v0_ptr + uv_pitch;
625 if (!kAvoidUsingOptimizedFilter) {
626 // Vertical scaler uses 16.8 fixed point.
627 uint8 fraction = (source_top & kFractionMask) >> 8;
628 g_filter_yuv_rows_proc_(
629 y_temp + source_y_left, y0_ptr, y1_ptr, source_y_width, fraction);
630 g_filter_yuv_rows_proc_(
631 u_temp + source_uv_left, u0_ptr, u1_ptr, source_uv_width, fraction);
632 g_filter_yuv_rows_proc_(
633 v_temp + source_uv_left, v0_ptr, v1_ptr, source_uv_width, fraction);
635 // Perform horizontal interpolation and color space conversion.
636 // TODO(hclam): Use the MMX version after more testing.
637 LinearScaleYUVToRGB32RowWithRange_C(y_temp, u_temp, v_temp, rgb_buf,
638 dest_rect_width, source_left, x_step,
639 lookup_table);
640 } else {
641 // If the frame is too large then we linear scale a single row.
642 LinearScaleYUVToRGB32RowWithRange_C(y0_ptr, u0_ptr, v0_ptr, rgb_buf,
643 dest_rect_width, source_left, x_step,
644 lookup_table);
647 // Advance vertically in the source and destination image.
648 source_top += y_step;
649 rgb_buf += rgb_pitch;
652 g_empty_register_state_proc_();
655 void ConvertRGB32ToYUV(const uint8* rgbframe,
656 uint8* yplane,
657 uint8* uplane,
658 uint8* vplane,
659 int width,
660 int height,
661 int rgbstride,
662 int ystride,
663 int uvstride) {
664 g_convert_rgb32_to_yuv_proc_(rgbframe,
665 yplane,
666 uplane,
667 vplane,
668 width,
669 height,
670 rgbstride,
671 ystride,
672 uvstride);
675 void ConvertRGB24ToYUV(const uint8* rgbframe,
676 uint8* yplane,
677 uint8* uplane,
678 uint8* vplane,
679 int width,
680 int height,
681 int rgbstride,
682 int ystride,
683 int uvstride) {
684 g_convert_rgb24_to_yuv_proc_(rgbframe,
685 yplane,
686 uplane,
687 vplane,
688 width,
689 height,
690 rgbstride,
691 ystride,
692 uvstride);
695 void ConvertYUY2ToYUV(const uint8* src,
696 uint8* yplane,
697 uint8* uplane,
698 uint8* vplane,
699 int width,
700 int height) {
701 for (int i = 0; i < height / 2; ++i) {
702 for (int j = 0; j < (width / 2); ++j) {
703 yplane[0] = src[0];
704 *uplane = src[1];
705 yplane[1] = src[2];
706 *vplane = src[3];
707 src += 4;
708 yplane += 2;
709 uplane++;
710 vplane++;
712 for (int j = 0; j < (width / 2); ++j) {
713 yplane[0] = src[0];
714 yplane[1] = src[2];
715 src += 4;
716 yplane += 2;
721 void ConvertNV21ToYUV(const uint8* src,
722 uint8* yplane,
723 uint8* uplane,
724 uint8* vplane,
725 int width,
726 int height) {
727 int y_plane_size = width * height;
728 memcpy(yplane, src, y_plane_size);
730 src += y_plane_size;
731 int u_plane_size = y_plane_size >> 2;
732 for (int i = 0; i < u_plane_size; ++i) {
733 *vplane++ = *src++;
734 *uplane++ = *src++;
738 void ConvertYUVToRGB32(const uint8* yplane,
739 const uint8* uplane,
740 const uint8* vplane,
741 uint8* rgbframe,
742 int width,
743 int height,
744 int ystride,
745 int uvstride,
746 int rgbstride,
747 YUVType yuv_type) {
748 g_convert_yuv_to_rgb32_proc_(yplane,
749 uplane,
750 vplane,
751 rgbframe,
752 width,
753 height,
754 ystride,
755 uvstride,
756 rgbstride,
757 yuv_type);
760 void ConvertYUVAToARGB(const uint8* yplane,
761 const uint8* uplane,
762 const uint8* vplane,
763 const uint8* aplane,
764 uint8* rgbframe,
765 int width,
766 int height,
767 int ystride,
768 int uvstride,
769 int astride,
770 int rgbstride,
771 YUVType yuv_type) {
772 g_convert_yuva_to_argb_proc_(yplane,
773 uplane,
774 vplane,
775 aplane,
776 rgbframe,
777 width,
778 height,
779 ystride,
780 uvstride,
781 astride,
782 rgbstride,
783 yuv_type);
786 } // namespace media