2010-11-11 02:54:27 +03:00
|
|
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
2010-04-19 05:17:06 +04:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// This webpage shows layout of YV12 and other YUV formats
|
|
|
|
// http://www.fourcc.org/yuv.php
|
|
|
|
// The actual conversion is best described here
|
|
|
|
// http://en.wikipedia.org/wiki/YUV
|
|
|
|
// An article on optimizing YUV conversion using tables instead of multiplies
|
|
|
|
// http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
|
|
|
|
//
|
|
|
|
// YV12 is a full plane of Y and a half height, half width chroma planes
|
|
|
|
// YV16 is a full plane of Y and a full height, half width chroma planes
|
2010-04-21 07:05:34 +04:00
|
|
|
// YV24 is a full plane of Y and a full height, full width chroma planes
|
2010-04-19 05:17:06 +04:00
|
|
|
//
|
|
|
|
// ARGB pixel format is output, which on little endian is stored as BGRA.
|
|
|
|
// The alpha is set to 255, allowing the application to use RGBA or RGB32.
|
|
|
|
|
|
|
|
#include "yuv_convert.h"
|
|
|
|
|
2016-06-09 04:46:42 +03:00
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "libyuv.h"
|
2016-08-05 05:30:01 +03:00
|
|
|
#include "scale_yuv_argb.h"
|
2010-04-19 05:17:06 +04:00
|
|
|
// Header for low level row functions.
|
|
|
|
#include "yuv_row.h"
|
|
|
|
#include "mozilla/SSE.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace gfx {
|
2016-03-10 12:58:00 +03:00
|
|
|
|
2010-11-11 02:54:27 +03:00
|
|
|
// 16.16 fixed point arithmetic
|
|
|
|
const int kFractionBits = 16;
|
|
|
|
const int kFractionMax = 1 << kFractionBits;
|
|
|
|
const int kFractionMask = ((1 << kFractionBits) - 1);
|
2010-04-19 05:17:06 +04:00
|
|
|
|
2016-03-10 12:58:00 +03:00
|
|
|
YUVType TypeFromSize(int ywidth,
|
2016-08-05 05:30:01 +03:00
|
|
|
int yheight,
|
|
|
|
int cbcrwidth,
|
|
|
|
int cbcrheight)
|
2011-07-05 06:52:00 +04:00
|
|
|
{
|
|
|
|
if (ywidth == cbcrwidth && yheight == cbcrheight) {
|
|
|
|
return YV24;
|
|
|
|
}
|
2016-03-10 12:58:00 +03:00
|
|
|
else if ((ywidth + 1) / 2 == cbcrwidth && yheight == cbcrheight) {
|
2011-07-05 06:52:00 +04:00
|
|
|
return YV16;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return YV12;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 05:30:01 +03:00
|
|
|
libyuv::FourCC FourCCFromYUVType(YUVType aYUVType)
|
|
|
|
{
|
|
|
|
if (aYUVType == YV24) {
|
|
|
|
return libyuv::FOURCC_I444;
|
|
|
|
} else if (aYUVType == YV16) {
|
|
|
|
return libyuv::FOURCC_I422;
|
|
|
|
} else if (aYUVType == YV12) {
|
|
|
|
return libyuv::FOURCC_I420;
|
|
|
|
} else {
|
|
|
|
return libyuv::FOURCC_ANY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-19 05:17:06 +04:00
|
|
|
// Convert a frame of YUV to 32 bit ARGB.
|
2015-09-08 00:53:36 +03:00
|
|
|
void ConvertYCbCrToRGB32(const uint8* y_buf,
|
|
|
|
const uint8* u_buf,
|
|
|
|
const uint8* v_buf,
|
|
|
|
uint8* rgb_buf,
|
|
|
|
int pic_x,
|
|
|
|
int pic_y,
|
|
|
|
int pic_width,
|
|
|
|
int pic_height,
|
|
|
|
int y_pitch,
|
|
|
|
int uv_pitch,
|
|
|
|
int rgb_pitch,
|
2016-10-12 05:46:28 +03:00
|
|
|
YUVType yuv_type,
|
|
|
|
YUVColorSpace yuv_color_space) {
|
2016-06-09 04:46:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Deprecated function's conversion is accurate.
|
|
|
|
// libyuv converion is a bit inaccurate to get performance. It dynamically
|
|
|
|
// calculates RGB from YUV to use simd. In it, signed byte is used for conversion's
|
|
|
|
// coefficient, but it requests 129. libyuv cut 129 to 127. And only 6 bits are
|
|
|
|
// used for a decimal part during the dynamic calculation.
|
|
|
|
//
|
|
|
|
// The function is still fast on some old intel chips.
|
|
|
|
// See Bug 1256475.
|
|
|
|
bool use_deprecated = gfxPrefs::YCbCrAccurateConversion() ||
|
2016-10-12 05:46:28 +03:00
|
|
|
(supports_mmx() && supports_sse() && !supports_sse3() &&
|
|
|
|
yuv_color_space == YUVColorSpace::BT601);
|
|
|
|
// The deprecated function only support BT601.
|
|
|
|
// See Bug 1210357.
|
|
|
|
if (yuv_color_space != YUVColorSpace::BT601) {
|
|
|
|
use_deprecated = false;
|
|
|
|
}
|
2016-06-09 04:46:42 +03:00
|
|
|
if (use_deprecated) {
|
|
|
|
ConvertYCbCrToRGB32_deprecated(y_buf, u_buf, v_buf, rgb_buf,
|
|
|
|
pic_x, pic_y, pic_width, pic_height,
|
|
|
|
y_pitch, uv_pitch, rgb_pitch, yuv_type);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-10 04:09:19 +03:00
|
|
|
|
2016-06-09 04:46:42 +03:00
|
|
|
if (yuv_type == YV24) {
|
|
|
|
const uint8* src_y = y_buf + y_pitch * pic_y + pic_x;
|
|
|
|
const uint8* src_u = u_buf + uv_pitch * pic_y + pic_x;
|
|
|
|
const uint8* src_v = v_buf + uv_pitch * pic_y + pic_x;
|
2017-02-24 22:01:56 +03:00
|
|
|
if (yuv_color_space == mozilla::YUVColorSpace::BT709) {
|
|
|
|
DebugOnly<int> err = libyuv::H444ToARGB(src_y, y_pitch,
|
|
|
|
src_u, uv_pitch,
|
|
|
|
src_v, uv_pitch,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
pic_width, pic_height);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
} else {
|
|
|
|
DebugOnly<int> err = libyuv::I444ToARGB(src_y, y_pitch,
|
|
|
|
src_u, uv_pitch,
|
|
|
|
src_v, uv_pitch,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
pic_width, pic_height);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
}
|
2016-06-09 04:46:42 +03:00
|
|
|
} else if (yuv_type == YV16) {
|
|
|
|
const uint8* src_y = y_buf + y_pitch * pic_y + pic_x;
|
|
|
|
const uint8* src_u = u_buf + uv_pitch * pic_y + pic_x / 2;
|
|
|
|
const uint8* src_v = v_buf + uv_pitch * pic_y + pic_x / 2;
|
2017-02-24 22:01:56 +03:00
|
|
|
if (yuv_color_space == mozilla::YUVColorSpace::BT709) {
|
|
|
|
DebugOnly<int> err = libyuv::H422ToARGB(src_y, y_pitch,
|
|
|
|
src_u, uv_pitch,
|
|
|
|
src_v, uv_pitch,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
pic_width, pic_height);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
} else {
|
|
|
|
DebugOnly<int> err = libyuv::I422ToARGB(src_y, y_pitch,
|
|
|
|
src_u, uv_pitch,
|
|
|
|
src_v, uv_pitch,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
pic_width, pic_height);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
}
|
2016-06-09 04:46:42 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(yuv_type == YV12);
|
|
|
|
const uint8* src_y = y_buf + y_pitch * pic_y + pic_x;
|
|
|
|
const uint8* src_u = u_buf + (uv_pitch * pic_y + pic_x) / 2;
|
|
|
|
const uint8* src_v = v_buf + (uv_pitch * pic_y + pic_x) / 2;
|
2017-02-24 22:01:56 +03:00
|
|
|
if (yuv_color_space == mozilla::YUVColorSpace::BT709) {
|
|
|
|
DebugOnly<int> err = libyuv::H420ToARGB(src_y, y_pitch,
|
|
|
|
src_u, uv_pitch,
|
|
|
|
src_v, uv_pitch,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
pic_width, pic_height);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
} else {
|
|
|
|
DebugOnly<int> err = libyuv::I420ToARGB(src_y, y_pitch,
|
|
|
|
src_u, uv_pitch,
|
|
|
|
src_v, uv_pitch,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
pic_width, pic_height);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
}
|
2016-06-09 04:46:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a frame of YUV to 32 bit ARGB.
|
|
|
|
void ConvertYCbCrToRGB32_deprecated(const uint8* y_buf,
|
|
|
|
const uint8* u_buf,
|
|
|
|
const uint8* v_buf,
|
|
|
|
uint8* rgb_buf,
|
|
|
|
int pic_x,
|
|
|
|
int pic_y,
|
|
|
|
int pic_width,
|
|
|
|
int pic_height,
|
|
|
|
int y_pitch,
|
|
|
|
int uv_pitch,
|
|
|
|
int rgb_pitch,
|
|
|
|
YUVType yuv_type) {
|
2010-04-21 07:05:34 +04:00
|
|
|
unsigned int y_shift = yuv_type == YV12 ? 1 : 0;
|
|
|
|
unsigned int x_shift = yuv_type == YV24 ? 0 : 1;
|
2010-08-02 07:32:14 +04:00
|
|
|
// Test for SSE because the optimized code uses movntq, which is not part of MMX.
|
|
|
|
bool has_sse = supports_mmx() && supports_sse();
|
|
|
|
// There is no optimized YV24 SSE routine so we check for this and
|
2010-04-21 07:05:34 +04:00
|
|
|
// fall back to the C code.
|
2010-08-02 07:32:14 +04:00
|
|
|
has_sse &= yuv_type != YV24;
|
2010-04-21 07:05:34 +04:00
|
|
|
bool odd_pic_x = yuv_type != YV24 && pic_x % 2 != 0;
|
2010-04-19 05:17:06 +04:00
|
|
|
int x_width = odd_pic_x ? pic_width - 1 : pic_width;
|
|
|
|
|
|
|
|
for (int y = pic_y; y < pic_height + pic_y; ++y) {
|
|
|
|
uint8* rgb_row = rgb_buf + (y - pic_y) * rgb_pitch;
|
|
|
|
const uint8* y_ptr = y_buf + y * y_pitch + pic_x;
|
2010-04-21 07:05:34 +04:00
|
|
|
const uint8* u_ptr = u_buf + (y >> y_shift) * uv_pitch + (pic_x >> x_shift);
|
|
|
|
const uint8* v_ptr = v_buf + (y >> y_shift) * uv_pitch + (pic_x >> x_shift);
|
2010-04-19 05:17:06 +04:00
|
|
|
|
|
|
|
if (odd_pic_x) {
|
|
|
|
// Handle the single odd pixel manually and use the
|
|
|
|
// fast routines for the remaining.
|
|
|
|
FastConvertYUVToRGB32Row_C(y_ptr++,
|
|
|
|
u_ptr++,
|
|
|
|
v_ptr++,
|
|
|
|
rgb_row,
|
2010-04-21 07:05:34 +04:00
|
|
|
1,
|
|
|
|
x_shift);
|
2010-04-19 05:17:06 +04:00
|
|
|
rgb_row += 4;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:54:27 +03:00
|
|
|
if (has_sse) {
|
2010-04-19 05:17:06 +04:00
|
|
|
FastConvertYUVToRGB32Row(y_ptr,
|
|
|
|
u_ptr,
|
|
|
|
v_ptr,
|
|
|
|
rgb_row,
|
|
|
|
x_width);
|
2010-11-11 02:54:27 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-04-19 05:17:06 +04:00
|
|
|
FastConvertYUVToRGB32Row_C(y_ptr,
|
|
|
|
u_ptr,
|
|
|
|
v_ptr,
|
|
|
|
rgb_row,
|
2010-04-21 07:05:34 +04:00
|
|
|
x_width,
|
|
|
|
x_shift);
|
2010-11-11 02:54:27 +03:00
|
|
|
}
|
2010-04-19 05:17:06 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:54:27 +03:00
|
|
|
// MMX used for FastConvertYUVToRGB32Row requires emms instruction.
|
2010-08-02 07:32:14 +04:00
|
|
|
if (has_sse)
|
2010-04-19 05:17:06 +04:00
|
|
|
EMMS();
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:54:27 +03:00
|
|
|
// C version does 8 at a time to mimic MMX code
|
2010-12-09 20:59:21 +03:00
|
|
|
static void FilterRows_C(uint8* ybuf, const uint8* y0_ptr, const uint8* y1_ptr,
|
|
|
|
int source_width, int source_y_fraction) {
|
2010-11-11 02:54:27 +03:00
|
|
|
int y1_fraction = source_y_fraction;
|
|
|
|
int y0_fraction = 256 - y1_fraction;
|
|
|
|
uint8* end = ybuf + source_width;
|
|
|
|
do {
|
|
|
|
ybuf[0] = (y0_ptr[0] * y0_fraction + y1_ptr[0] * y1_fraction) >> 8;
|
|
|
|
ybuf[1] = (y0_ptr[1] * y0_fraction + y1_ptr[1] * y1_fraction) >> 8;
|
|
|
|
ybuf[2] = (y0_ptr[2] * y0_fraction + y1_ptr[2] * y1_fraction) >> 8;
|
|
|
|
ybuf[3] = (y0_ptr[3] * y0_fraction + y1_ptr[3] * y1_fraction) >> 8;
|
|
|
|
ybuf[4] = (y0_ptr[4] * y0_fraction + y1_ptr[4] * y1_fraction) >> 8;
|
|
|
|
ybuf[5] = (y0_ptr[5] * y0_fraction + y1_ptr[5] * y1_fraction) >> 8;
|
|
|
|
ybuf[6] = (y0_ptr[6] * y0_fraction + y1_ptr[6] * y1_fraction) >> 8;
|
|
|
|
ybuf[7] = (y0_ptr[7] * y0_fraction + y1_ptr[7] * y1_fraction) >> 8;
|
|
|
|
y0_ptr += 8;
|
|
|
|
y1_ptr += 8;
|
|
|
|
ybuf += 8;
|
|
|
|
} while (ybuf < end);
|
|
|
|
}
|
2010-12-09 20:59:21 +03:00
|
|
|
|
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_MMX
|
|
|
|
void FilterRows_MMX(uint8* ybuf, const uint8* y0_ptr, const uint8* y1_ptr,
|
|
|
|
int source_width, int source_y_fraction);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_SSE2
|
|
|
|
void FilterRows_SSE2(uint8* ybuf, const uint8* y0_ptr, const uint8* y1_ptr,
|
|
|
|
int source_width, int source_y_fraction);
|
2010-11-11 02:54:27 +03:00
|
|
|
#endif
|
|
|
|
|
2010-12-09 20:59:21 +03:00
|
|
|
static inline void FilterRows(uint8* ybuf, const uint8* y0_ptr,
|
|
|
|
const uint8* y1_ptr, int source_width,
|
|
|
|
int source_y_fraction) {
|
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
|
|
|
FilterRows_SSE2(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_MMX
|
|
|
|
if (mozilla::supports_mmx()) {
|
|
|
|
FilterRows_MMX(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FilterRows_C(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-26 07:11:13 +04:00
|
|
|
// Scale a frame of YUV to 32 bit ARGB.
|
2015-09-08 00:53:36 +03:00
|
|
|
void ScaleYCbCrToRGB32(const uint8* y_buf,
|
|
|
|
const uint8* u_buf,
|
|
|
|
const uint8* v_buf,
|
|
|
|
uint8* rgb_buf,
|
|
|
|
int source_width,
|
|
|
|
int source_height,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int y_pitch,
|
|
|
|
int uv_pitch,
|
|
|
|
int rgb_pitch,
|
|
|
|
YUVType yuv_type,
|
2016-10-12 05:46:28 +03:00
|
|
|
YUVColorSpace yuv_color_space,
|
2015-09-08 00:53:36 +03:00
|
|
|
ScaleFilter filter) {
|
2016-08-05 05:30:01 +03:00
|
|
|
|
|
|
|
bool use_deprecated = gfxPrefs::YCbCrAccurateConversion() ||
|
2016-08-19 16:18:08 +03:00
|
|
|
#if defined(XP_WIN) && defined(_M_X64)
|
|
|
|
// libyuv does not support SIMD scaling on win 64bit. See Bug 1295927.
|
|
|
|
supports_sse3() ||
|
|
|
|
#endif
|
2016-08-05 05:30:01 +03:00
|
|
|
(supports_mmx() && supports_sse() && !supports_sse3());
|
2016-10-12 05:46:28 +03:00
|
|
|
// The deprecated function only support BT601.
|
|
|
|
// See Bug 1210357.
|
|
|
|
if (yuv_color_space != YUVColorSpace::BT601) {
|
|
|
|
use_deprecated = false;
|
|
|
|
}
|
2016-08-05 05:30:01 +03:00
|
|
|
if (use_deprecated) {
|
|
|
|
ScaleYCbCrToRGB32_deprecated(y_buf, u_buf, v_buf,
|
|
|
|
rgb_buf,
|
|
|
|
source_width, source_height,
|
|
|
|
width, height,
|
|
|
|
y_pitch, uv_pitch,
|
|
|
|
rgb_pitch,
|
|
|
|
yuv_type,
|
|
|
|
ROTATE_0,
|
|
|
|
filter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugOnly<int> err =
|
|
|
|
libyuv::YUVToARGBScale(y_buf, y_pitch,
|
|
|
|
u_buf, uv_pitch,
|
|
|
|
v_buf, uv_pitch,
|
|
|
|
FourCCFromYUVType(yuv_type),
|
2016-10-12 05:46:28 +03:00
|
|
|
yuv_color_space,
|
2016-08-05 05:30:01 +03:00
|
|
|
source_width, source_height,
|
|
|
|
rgb_buf, rgb_pitch,
|
|
|
|
width, height,
|
|
|
|
libyuv::kFilterBilinear);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale a frame of YUV to 32 bit ARGB.
|
|
|
|
void ScaleYCbCrToRGB32_deprecated(const uint8* y_buf,
|
|
|
|
const uint8* u_buf,
|
|
|
|
const uint8* v_buf,
|
|
|
|
uint8* rgb_buf,
|
|
|
|
int source_width,
|
|
|
|
int source_height,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int y_pitch,
|
|
|
|
int uv_pitch,
|
|
|
|
int rgb_pitch,
|
|
|
|
YUVType yuv_type,
|
|
|
|
Rotate view_rotate,
|
|
|
|
ScaleFilter filter) {
|
2010-10-26 07:11:13 +04:00
|
|
|
bool has_mmx = supports_mmx();
|
2010-11-11 02:54:27 +03:00
|
|
|
|
|
|
|
// 4096 allows 3 buffers to fit in 12k.
|
|
|
|
// Helps performance on CPU with 16K L1 cache.
|
|
|
|
// Large enough for 3830x2160 and 30" displays which are 2560x1600.
|
|
|
|
const int kFilterBufferSize = 4096;
|
|
|
|
// Disable filtering if the screen is too big (to avoid buffer overflows).
|
|
|
|
// This should never happen to regular users: they don't have monitors
|
|
|
|
// wider than 4096 pixels.
|
|
|
|
// TODO(fbarchard): Allow rotated videos to filter.
|
|
|
|
if (source_width > kFilterBufferSize || view_rotate)
|
|
|
|
filter = FILTER_NONE;
|
|
|
|
|
|
|
|
unsigned int y_shift = yuv_type == YV12 ? 1 : 0;
|
2010-10-26 07:11:13 +04:00
|
|
|
// Diagram showing origin and direction of source sampling.
|
|
|
|
// ->0 4<-
|
|
|
|
// 7 3
|
|
|
|
//
|
|
|
|
// 6 5
|
|
|
|
// ->1 2<-
|
|
|
|
// Rotations that start at right side of image.
|
|
|
|
if ((view_rotate == ROTATE_180) ||
|
|
|
|
(view_rotate == ROTATE_270) ||
|
|
|
|
(view_rotate == MIRROR_ROTATE_0) ||
|
|
|
|
(view_rotate == MIRROR_ROTATE_90)) {
|
2010-11-11 02:54:27 +03:00
|
|
|
y_buf += source_width - 1;
|
|
|
|
u_buf += source_width / 2 - 1;
|
|
|
|
v_buf += source_width / 2 - 1;
|
|
|
|
source_width = -source_width;
|
2010-10-26 07:11:13 +04:00
|
|
|
}
|
|
|
|
// Rotations that start at bottom of image.
|
|
|
|
if ((view_rotate == ROTATE_90) ||
|
|
|
|
(view_rotate == ROTATE_180) ||
|
|
|
|
(view_rotate == MIRROR_ROTATE_90) ||
|
|
|
|
(view_rotate == MIRROR_ROTATE_180)) {
|
2010-11-11 02:54:27 +03:00
|
|
|
y_buf += (source_height - 1) * y_pitch;
|
|
|
|
u_buf += ((source_height >> y_shift) - 1) * uv_pitch;
|
|
|
|
v_buf += ((source_height >> y_shift) - 1) * uv_pitch;
|
|
|
|
source_height = -source_height;
|
2010-10-26 07:11:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle zero sized destination.
|
2010-11-11 02:54:27 +03:00
|
|
|
if (width == 0 || height == 0)
|
2010-10-26 07:11:13 +04:00
|
|
|
return;
|
2010-11-11 02:54:27 +03:00
|
|
|
int source_dx = source_width * kFractionMax / width;
|
|
|
|
int source_dy = source_height * kFractionMax / height;
|
|
|
|
int source_dx_uv = source_dx;
|
2010-10-26 07:11:13 +04:00
|
|
|
|
|
|
|
if ((view_rotate == ROTATE_90) ||
|
|
|
|
(view_rotate == ROTATE_270)) {
|
2010-11-11 02:54:27 +03:00
|
|
|
int tmp = height;
|
2010-10-26 07:11:13 +04:00
|
|
|
height = width;
|
|
|
|
width = tmp;
|
2010-11-11 02:54:27 +03:00
|
|
|
tmp = source_height;
|
|
|
|
source_height = source_width;
|
|
|
|
source_width = tmp;
|
|
|
|
int original_dx = source_dx;
|
|
|
|
int original_dy = source_dy;
|
|
|
|
source_dx = ((original_dy >> kFractionBits) * y_pitch) << kFractionBits;
|
|
|
|
source_dx_uv = ((original_dy >> kFractionBits) * uv_pitch) << kFractionBits;
|
|
|
|
source_dy = original_dx;
|
2010-10-26 07:11:13 +04:00
|
|
|
if (view_rotate == ROTATE_90) {
|
|
|
|
y_pitch = -1;
|
|
|
|
uv_pitch = -1;
|
2010-11-11 02:54:27 +03:00
|
|
|
source_height = -source_height;
|
2010-10-26 07:11:13 +04:00
|
|
|
} else {
|
|
|
|
y_pitch = 1;
|
|
|
|
uv_pitch = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:54:27 +03:00
|
|
|
// Need padding because FilterRows() will write 1 to 16 extra pixels
|
|
|
|
// after the end for SSE2 version.
|
|
|
|
uint8 yuvbuf[16 + kFilterBufferSize * 3 + 16];
|
|
|
|
uint8* ybuf =
|
2012-09-15 00:09:52 +04:00
|
|
|
reinterpret_cast<uint8*>(reinterpret_cast<uintptr_t>(yuvbuf + 15) & ~15);
|
2010-11-11 02:54:27 +03:00
|
|
|
uint8* ubuf = ybuf + kFilterBufferSize;
|
|
|
|
uint8* vbuf = ubuf + kFilterBufferSize;
|
|
|
|
// TODO(fbarchard): Fixed point math is off by 1 on negatives.
|
|
|
|
int yscale_fixed = (source_height << kFractionBits) / height;
|
|
|
|
|
|
|
|
// TODO(fbarchard): Split this into separate function for better efficiency.
|
|
|
|
for (int y = 0; y < height; ++y) {
|
2010-10-26 07:11:13 +04:00
|
|
|
uint8* dest_pixel = rgb_buf + y * rgb_pitch;
|
2010-11-11 02:54:27 +03:00
|
|
|
int source_y_subpixel = (y * yscale_fixed);
|
|
|
|
if (yscale_fixed >= (kFractionMax * 2)) {
|
|
|
|
source_y_subpixel += kFractionMax / 2; // For 1/2 or less, center filter.
|
|
|
|
}
|
|
|
|
int source_y = source_y_subpixel >> kFractionBits;
|
|
|
|
|
|
|
|
const uint8* y0_ptr = y_buf + source_y * y_pitch;
|
|
|
|
const uint8* y1_ptr = y0_ptr + y_pitch;
|
|
|
|
|
|
|
|
const uint8* u0_ptr = u_buf + (source_y >> y_shift) * uv_pitch;
|
|
|
|
const uint8* u1_ptr = u0_ptr + uv_pitch;
|
|
|
|
const uint8* v0_ptr = v_buf + (source_y >> y_shift) * uv_pitch;
|
|
|
|
const uint8* v1_ptr = v0_ptr + uv_pitch;
|
2010-10-26 07:11:13 +04:00
|
|
|
|
2010-11-11 02:54:27 +03:00
|
|
|
// vertical scaler uses 16.8 fixed point
|
|
|
|
int source_y_fraction = (source_y_subpixel & kFractionMask) >> 8;
|
|
|
|
int source_uv_fraction =
|
|
|
|
((source_y_subpixel >> y_shift) & kFractionMask) >> 8;
|
|
|
|
|
|
|
|
const uint8* y_ptr = y0_ptr;
|
|
|
|
const uint8* u_ptr = u0_ptr;
|
|
|
|
const uint8* v_ptr = v0_ptr;
|
|
|
|
// Apply vertical filtering if necessary.
|
|
|
|
// TODO(fbarchard): Remove memcpy when not necessary.
|
|
|
|
if (filter & mozilla::gfx::FILTER_BILINEAR_V) {
|
|
|
|
if (yscale_fixed != kFractionMax &&
|
|
|
|
source_y_fraction && ((source_y + 1) < source_height)) {
|
|
|
|
FilterRows(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
|
|
|
|
} else {
|
|
|
|
memcpy(ybuf, y0_ptr, source_width);
|
|
|
|
}
|
|
|
|
y_ptr = ybuf;
|
|
|
|
ybuf[source_width] = ybuf[source_width-1];
|
|
|
|
int uv_source_width = (source_width + 1) / 2;
|
|
|
|
if (yscale_fixed != kFractionMax &&
|
|
|
|
source_uv_fraction &&
|
|
|
|
(((source_y >> y_shift) + 1) < (source_height >> y_shift))) {
|
|
|
|
FilterRows(ubuf, u0_ptr, u1_ptr, uv_source_width, source_uv_fraction);
|
|
|
|
FilterRows(vbuf, v0_ptr, v1_ptr, uv_source_width, source_uv_fraction);
|
|
|
|
} else {
|
|
|
|
memcpy(ubuf, u0_ptr, uv_source_width);
|
|
|
|
memcpy(vbuf, v0_ptr, uv_source_width);
|
|
|
|
}
|
|
|
|
u_ptr = ubuf;
|
|
|
|
v_ptr = vbuf;
|
|
|
|
ubuf[uv_source_width] = ubuf[uv_source_width - 1];
|
|
|
|
vbuf[uv_source_width] = vbuf[uv_source_width - 1];
|
|
|
|
}
|
|
|
|
if (source_dx == kFractionMax) { // Not scaled
|
|
|
|
FastConvertYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width);
|
2010-12-09 20:59:21 +03:00
|
|
|
} else if (filter & FILTER_BILINEAR_H) {
|
2010-11-11 02:54:27 +03:00
|
|
|
LinearScaleYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width, source_dx);
|
|
|
|
} else {
|
|
|
|
// Specialized scalers and rotation.
|
2010-12-09 20:59:21 +03:00
|
|
|
#if defined(MOZILLA_MAY_SUPPORT_SSE) && defined(_MSC_VER) && defined(_M_IX86)
|
|
|
|
if(mozilla::supports_sse()) {
|
2010-11-11 02:54:27 +03:00
|
|
|
if (width == (source_width * 2)) {
|
2010-12-09 20:59:21 +03:00
|
|
|
DoubleYUVToRGB32Row_SSE(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width);
|
2010-11-11 02:54:27 +03:00
|
|
|
} else if ((source_dx & kFractionMask) == 0) {
|
|
|
|
// Scaling by integer scale factor. ie half.
|
2010-12-09 20:59:21 +03:00
|
|
|
ConvertYUVToRGB32Row_SSE(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width,
|
|
|
|
source_dx >> kFractionBits);
|
2010-11-11 02:54:27 +03:00
|
|
|
} else if (source_dx_uv == source_dx) { // Not rotated.
|
|
|
|
ScaleYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width, source_dx);
|
|
|
|
} else {
|
2010-12-09 20:59:21 +03:00
|
|
|
RotateConvertYUVToRGB32Row_SSE(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width,
|
|
|
|
source_dx >> kFractionBits,
|
|
|
|
source_dx_uv >> kFractionBits);
|
2010-10-26 07:11:13 +04:00
|
|
|
}
|
2010-12-09 20:59:21 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ScaleYUVToRGB32Row_C(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width, source_dx);
|
|
|
|
}
|
2010-10-26 07:11:13 +04:00
|
|
|
#else
|
2012-03-22 09:21:16 +04:00
|
|
|
(void)source_dx_uv;
|
2010-12-09 20:59:21 +03:00
|
|
|
ScaleYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
|
|
|
|
dest_pixel, width, source_dx);
|
2010-11-11 02:54:27 +03:00
|
|
|
#endif
|
|
|
|
}
|
2010-10-26 07:11:13 +04:00
|
|
|
}
|
2010-11-11 02:54:27 +03:00
|
|
|
// MMX used for FastConvertYUVToRGB32Row and FilterRows requires emms.
|
2010-10-26 07:11:13 +04:00
|
|
|
if (has_mmx)
|
|
|
|
EMMS();
|
|
|
|
}
|
2016-12-21 05:00:46 +03:00
|
|
|
void ConvertYCbCrAToARGB32(const uint8* y_buf,
|
|
|
|
const uint8* u_buf,
|
|
|
|
const uint8* v_buf,
|
|
|
|
const uint8* a_buf,
|
|
|
|
uint8* argb_buf,
|
|
|
|
int pic_width,
|
|
|
|
int pic_height,
|
|
|
|
int ya_pitch,
|
|
|
|
int uv_pitch,
|
|
|
|
int argb_pitch) {
|
|
|
|
|
|
|
|
// The downstream graphics stack expects an attenuated input, hence why the
|
|
|
|
// attenuation parameter is set.
|
|
|
|
DebugOnly<int> err = libyuv::I420AlphaToARGB(y_buf, ya_pitch,
|
|
|
|
u_buf, uv_pitch,
|
|
|
|
v_buf, uv_pitch,
|
|
|
|
a_buf, ya_pitch,
|
|
|
|
argb_buf, argb_pitch,
|
|
|
|
pic_width, pic_height, 1);
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
}
|
2010-10-26 07:11:13 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|