/* * Copyright 2011 The LibYuv Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
#include"libyuv/row.h"
#include <assert.h> #include <string.h> // For memcpy and memset.
#include"libyuv/basic_types.h" #include"libyuv/convert_argb.h"// For kYuvI601Constants
// This macro controls YUV to RGB using unsigned math to extend range of // YUV to RGB coefficients to 0 to 4 instead of 0 to 2 for more accuracy on B: // LIBYUV_UNLIMITED_DATA
// Macros to enable unlimited data for each colorspace // LIBYUV_UNLIMITED_BT601 // LIBYUV_UNLIMITED_BT709 // LIBYUV_UNLIMITED_BT2020
// The following macro from row_win makes the C code match the row_win code, // which is 7 bit fixed point for ARGBToI420: #if !defined(LIBYUV_BIT_EXACT) && !defined(LIBYUV_DISABLE_X86) && \ defined(_MSC_VER) && !defined(__clang__) && \
(defined(_M_IX86) || defined(_M_X64)) #define LIBYUV_RGB7 1 #endif
#ifdefined(LIBYUV_ARGBTOUV_PAVGB)
uint8_t ab = AVGB(b0, b2);
uint8_t ag = AVGB(g0, g2);
uint8_t ar = AVGB(r0, r2);
dst_u[0] = RGBToU(ar, ag, ab);
dst_v[0] = RGBToV(ar, ag, ab); #else
uint16_t b = b0 + b2;
uint16_t g = g0 + g2;
uint16_t r = r0 + r2;
dst_u[0] = RGB2xToU(r, g, b);
dst_v[0] = RGB2xToV(r, g, b); #endif
}
}
void ARGBToUV444Row_C(const uint8_t* src_argb,
uint8_t* dst_u,
uint8_t* dst_v, int width) { int x; for (x = 0; x < width; ++x) {
uint8_t ab = src_argb[0];
uint8_t ag = src_argb[1];
uint8_t ar = src_argb[2];
dst_u[0] = RGBToU(ar, ag, ab);
dst_v[0] = RGBToV(ar, ag, ab);
src_argb += 4;
dst_u += 1;
dst_v += 1;
}
}
void ARGBGrayRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) { int x; for (x = 0; x < width; ++x) {
uint8_t y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]);
dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
dst_argb[3] = src_argb[3];
dst_argb += 4;
src_argb += 4;
}
}
// Convert a row of image to Sepia tone. void ARGBSepiaRow_C(uint8_t* dst_argb, int width) { int x; for (x = 0; x < width; ++x) { int b = dst_argb[0]; int g = dst_argb[1]; int r = dst_argb[2]; int sb = (b * 17 + g * 68 + r * 35) >> 7; int sg = (b * 22 + g * 88 + r * 45) >> 7; int sr = (b * 24 + g * 98 + r * 50) >> 7; // b does not over flow. a is preserved from original.
dst_argb[0] = STATIC_CAST(uint8_t, sb);
dst_argb[1] = STATIC_CAST(uint8_t, clamp255(sg));
dst_argb[2] = STATIC_CAST(uint8_t, clamp255(sr));
dst_argb += 4;
}
}
// Apply color matrix to a row of image. Matrix is signed. // TODO(fbarchard): Consider adding rounding (+32). void ARGBColorMatrixRow_C(const uint8_t* src_argb,
uint8_t* dst_argb, const int8_t* matrix_argb, int width) { int x; for (x = 0; x < width; ++x) { int b = src_argb[0]; int g = src_argb[1]; int r = src_argb[2]; int a = src_argb[3]; int sb = (b * matrix_argb[0] + g * matrix_argb[1] + r * matrix_argb[2] +
a * matrix_argb[3]) >>
6; int sg = (b * matrix_argb[4] + g * matrix_argb[5] + r * matrix_argb[6] +
a * matrix_argb[7]) >>
6; int sr = (b * matrix_argb[8] + g * matrix_argb[9] + r * matrix_argb[10] +
a * matrix_argb[11]) >>
6; int sa = (b * matrix_argb[12] + g * matrix_argb[13] + r * matrix_argb[14] +
a * matrix_argb[15]) >>
6;
dst_argb[0] = STATIC_CAST(uint8_t, Clamp(sb));
dst_argb[1] = STATIC_CAST(uint8_t, Clamp(sg));
dst_argb[2] = STATIC_CAST(uint8_t, Clamp(sr));
dst_argb[3] = STATIC_CAST(uint8_t, Clamp(sa));
src_argb += 4;
dst_argb += 4;
}
}
// Apply color table to a row of image. void ARGBColorTableRow_C(uint8_t* dst_argb, const uint8_t* table_argb, int width) { int x; for (x = 0; x < width; ++x) { int b = dst_argb[0]; int g = dst_argb[1]; int r = dst_argb[2]; int a = dst_argb[3];
dst_argb[0] = table_argb[b * 4 + 0];
dst_argb[1] = table_argb[g * 4 + 1];
dst_argb[2] = table_argb[r * 4 + 2];
dst_argb[3] = table_argb[a * 4 + 3];
dst_argb += 4;
}
}
// Apply color table to a row of image. void RGBColorTableRow_C(uint8_t* dst_argb, const uint8_t* table_argb, int width) { int x; for (x = 0; x < width; ++x) { int b = dst_argb[0]; int g = dst_argb[1]; int r = dst_argb[2];
dst_argb[0] = table_argb[b * 4 + 0];
dst_argb[1] = table_argb[g * 4 + 1];
dst_argb[2] = table_argb[r * 4 + 2];
dst_argb += 4;
}
}
void ARGBQuantizeRow_C(uint8_t* dst_argb, int scale, int interval_size, int interval_offset, int width) { int x; for (x = 0; x < width; ++x) { int b = dst_argb[0]; int g = dst_argb[1]; int r = dst_argb[2];
dst_argb[0] = STATIC_CAST(
uint8_t, (b * scale >> 16) * interval_size + interval_offset);
dst_argb[1] = STATIC_CAST(
uint8_t, (g * scale >> 16) * interval_size + interval_offset);
dst_argb[2] = STATIC_CAST(
uint8_t, (r * scale >> 16) * interval_size + interval_offset);
dst_argb += 4;
}
}
void ARGBAddRow_C(const uint8_t* src_argb, const uint8_t* src_argb1,
uint8_t* dst_argb, int width) { int i; for (i = 0; i < width; ++i) { constint b = src_argb[0]; constint g = src_argb[1]; constint r = src_argb[2]; constint a = src_argb[3]; constint b_add = src_argb1[0]; constint g_add = src_argb1[1]; constint r_add = src_argb1[2]; constint a_add = src_argb1[3];
dst_argb[0] = STATIC_CAST(uint8_t, SHADE(b, b_add));
dst_argb[1] = STATIC_CAST(uint8_t, SHADE(g, g_add));
dst_argb[2] = STATIC_CAST(uint8_t, SHADE(r, r_add));
dst_argb[3] = STATIC_CAST(uint8_t, SHADE(a, a_add));
src_argb += 4;
src_argb1 += 4;
dst_argb += 4;
}
} #undef SHADE
#define SHADE(f, v) clamp0(f - v)
void ARGBSubtractRow_C(const uint8_t* src_argb, const uint8_t* src_argb1,
uint8_t* dst_argb, int width) { int i; for (i = 0; i < width; ++i) { constint b = src_argb[0]; constint g = src_argb[1]; constint r = src_argb[2]; constint a = src_argb[3]; constint b_sub = src_argb1[0]; constint g_sub = src_argb1[1]; constint r_sub = src_argb1[2]; constint a_sub = src_argb1[3];
dst_argb[0] = STATIC_CAST(uint8_t, SHADE(b, b_sub));
dst_argb[1] = STATIC_CAST(uint8_t, SHADE(g, g_sub));
dst_argb[2] = STATIC_CAST(uint8_t, SHADE(r, r_sub));
dst_argb[3] = STATIC_CAST(uint8_t, SHADE(a, a_sub));
src_argb += 4;
src_argb1 += 4;
dst_argb += 4;
}
} #undef SHADE
// Sobel functions which mimics SSSE3. void SobelXRow_C(const uint8_t* src_y0, const uint8_t* src_y1, const uint8_t* src_y2,
uint8_t* dst_sobelx, int width) { int i; for (i = 0; i < width; ++i) { int a = src_y0[i]; int b = src_y1[i]; int c = src_y2[i]; int a_sub = src_y0[i + 2]; int b_sub = src_y1[i + 2]; int c_sub = src_y2[i + 2]; int a_diff = a - a_sub; int b_diff = b - b_sub; int c_diff = c - c_sub; int sobel = Abs(a_diff + b_diff * 2 + c_diff);
dst_sobelx[i] = (uint8_t)(clamp255(sobel));
}
}
void SobelYRow_C(const uint8_t* src_y0, const uint8_t* src_y1,
uint8_t* dst_sobely, int width) { int i; for (i = 0; i < width; ++i) { int a = src_y0[i + 0]; int b = src_y0[i + 1]; int c = src_y0[i + 2]; int a_sub = src_y1[i + 0]; int b_sub = src_y1[i + 1]; int c_sub = src_y1[i + 2]; int a_diff = a - a_sub; int b_diff = b - b_sub; int c_diff = c - c_sub; int sobel = Abs(a_diff + b_diff * 2 + c_diff);
dst_sobely[i] = (uint8_t)(clamp255(sobel));
}
}
void SobelRow_C(const uint8_t* src_sobelx, const uint8_t* src_sobely,
uint8_t* dst_argb, int width) { int i; for (i = 0; i < width; ++i) { int r = src_sobelx[i]; int b = src_sobely[i]; int s = clamp255(r + b);
dst_argb[0] = (uint8_t)(s);
dst_argb[1] = (uint8_t)(s);
dst_argb[2] = (uint8_t)(s);
dst_argb[3] = (uint8_t)(255u);
dst_argb += 4;
}
}
void SobelToPlaneRow_C(const uint8_t* src_sobelx, const uint8_t* src_sobely,
uint8_t* dst_y, int width) { int i; for (i = 0; i < width; ++i) { int r = src_sobelx[i]; int b = src_sobely[i]; int s = clamp255(r + b);
dst_y[i] = (uint8_t)(s);
}
}
void SobelXYRow_C(const uint8_t* src_sobelx, const uint8_t* src_sobely,
uint8_t* dst_argb, int width) { int i; for (i = 0; i < width; ++i) { int r = src_sobelx[i]; int b = src_sobely[i]; int g = clamp255(r + b);
dst_argb[0] = (uint8_t)(b);
dst_argb[1] = (uint8_t)(g);
dst_argb[2] = (uint8_t)(r);
dst_argb[3] = (uint8_t)(255u);
dst_argb += 4;
}
}
void J400ToARGBRow_C(const uint8_t* src_y, uint8_t* dst_argb, int width) { // Copy a Y to RGB. int x; for (x = 0; x < width; ++x) {
uint8_t y = src_y[0];
dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
dst_argb[3] = 255u;
dst_argb += 4;
++src_y;
}
}
// Macros to create SIMD specific yuv to rgb conversion constants.
// BT.601 full range YUV to RGB reference (aka JPEG) // * R = Y + V * 1.40200 // * G = Y - U * 0.34414 - V * 0.71414 // * B = Y + U * 1.77200 // KR = 0.299; KB = 0.114
// U and V contributions to R,G,B. #define UB 113 /* round(1.77200 * 64) */ #define UG 22 /* round(0.34414 * 64) */ #define VG 46 /* round(0.71414 * 64) */ #define VR 90 /* round(1.40200 * 64) */
// Y contribution to R,G,B. Scale and bias. #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */ #define YB 32 /* 64 / 2 */
// BT.2020 full range YUV to RGB reference // R = Y + V * 1.474600 // G = Y - U * 0.164553 - V * 0.571353 // B = Y + U * 1.881400 // KR = 0.2627; KB = 0.0593
#ifdefined(__aarch64__) || defined(__arm__) || defined(__riscv) #define LOAD_YUV_CONSTANTS \ int ub = yuvconstants->kUVCoeff[0]; \ int vr = yuvconstants->kUVCoeff[1]; \ int ug = yuvconstants->kUVCoeff[2]; \ int vg = yuvconstants->kUVCoeff[3]; \ int yg = yuvconstants->kRGBCoeffBias[0]; \ int bb = yuvconstants->kRGBCoeffBias[1]; \ int bg = yuvconstants->kRGBCoeffBias[2]; \ int br = yuvconstants->kRGBCoeffBias[3]
#define CALC_RGB16 \
int32_t y1 = (uint32_t)(y32 * yg) >> 16; \ int b16 = y1 + (u * ub) - bb; \ int g16 = y1 + bg - (u * ug + v * vg); \ int r16 = y1 + (v * vr) - br #else #define LOAD_YUV_CONSTANTS \ int ub = yuvconstants->kUVToB[0]; \ int ug = yuvconstants->kUVToG[0]; \ int vg = yuvconstants->kUVToG[1]; \ int vr = yuvconstants->kUVToR[1]; \ int yg = yuvconstants->kYToRgb[0]; \ int yb = yuvconstants->kYBiasToRgb[0]
#define CALC_RGB16 \
int32_t y1 = ((uint32_t)(y32 * yg) >> 16) + yb; \
int8_t ui = (int8_t)u; \
int8_t vi = (int8_t)v; \
ui -= 0x80; \
vi -= 0x80; \ int b16 = y1 + (ui * ub); \ int g16 = y1 - (ui * ug + vi * vg); \ int r16 = y1 + (vi * vr) #endif
// C reference code that mimics the YUV assembly. // Reads 8 bit YUV and leaves result as 16 bit. static __inlinevoid YuvPixel(uint8_t y,
uint8_t u,
uint8_t v,
uint8_t* b,
uint8_t* g,
uint8_t* r, conststruct YuvConstants* yuvconstants) {
LOAD_YUV_CONSTANTS;
uint32_t y32 = y * 0x0101;
CALC_RGB16;
*b = STATIC_CAST(uint8_t, Clamp((int32_t)(b16) >> 6));
*g = STATIC_CAST(uint8_t, Clamp((int32_t)(g16) >> 6));
*r = STATIC_CAST(uint8_t, Clamp((int32_t)(r16) >> 6));
}
// Reads 8 bit YUV and leaves result as 16 bit. static __inlinevoid YuvPixel8_16(uint8_t y,
uint8_t u,
uint8_t v, int* b, int* g, int* r, conststruct YuvConstants* yuvconstants) {
LOAD_YUV_CONSTANTS;
uint32_t y32 = y * 0x0101;
CALC_RGB16;
*b = b16;
*g = g16;
*r = r16;
}
// C reference code that mimics the YUV 16 bit assembly. // Reads 10 bit YUV and leaves result as 16 bit. static __inlinevoid YuvPixel10_16(uint16_t y,
uint16_t u,
uint16_t v, int* b, int* g, int* r, conststruct YuvConstants* yuvconstants) {
LOAD_YUV_CONSTANTS;
uint32_t y32 = (y << 6) | (y >> 4);
u = STATIC_CAST(uint8_t, clamp255(u >> 2));
v = STATIC_CAST(uint8_t, clamp255(v >> 2));
CALC_RGB16;
*b = b16;
*g = g16;
*r = r16;
}
// C reference code that mimics the YUV 16 bit assembly. // Reads 12 bit YUV and leaves result as 16 bit. static __inlinevoid YuvPixel12_16(int16_t y,
int16_t u,
int16_t v, int* b, int* g, int* r, conststruct YuvConstants* yuvconstants) {
LOAD_YUV_CONSTANTS;
uint32_t y32 = (y << 4) | (y >> 8);
u = STATIC_CAST(uint8_t, clamp255(u >> 4));
v = STATIC_CAST(uint8_t, clamp255(v >> 4));
CALC_RGB16;
*b = b16;
*g = g16;
*r = r16;
}
// C reference code that mimics the YUV 10 bit assembly. // Reads 10 bit YUV and clamps down to 8 bit RGB. static __inlinevoid YuvPixel10(uint16_t y,
uint16_t u,
uint16_t v,
uint8_t* b,
uint8_t* g,
uint8_t* r, conststruct YuvConstants* yuvconstants) { int b16; int g16; int r16;
YuvPixel10_16(y, u, v, &b16, &g16, &r16, yuvconstants);
*b = STATIC_CAST(uint8_t, Clamp(b16 >> 6));
*g = STATIC_CAST(uint8_t, Clamp(g16 >> 6));
*r = STATIC_CAST(uint8_t, Clamp(r16 >> 6));
}
// C reference code that mimics the YUV 12 bit assembly. // Reads 12 bit YUV and clamps down to 8 bit RGB. static __inlinevoid YuvPixel12(uint16_t y,
uint16_t u,
uint16_t v,
uint8_t* b,
uint8_t* g,
uint8_t* r, conststruct YuvConstants* yuvconstants) { int b16; int g16; int r16;
YuvPixel12_16(y, u, v, &b16, &g16, &r16, yuvconstants);
*b = STATIC_CAST(uint8_t, Clamp(b16 >> 6));
*g = STATIC_CAST(uint8_t, Clamp(g16 >> 6));
*r = STATIC_CAST(uint8_t, Clamp(r16 >> 6));
}
// C reference code that mimics the YUV 16 bit assembly. // Reads 16 bit YUV and leaves result as 8 bit. static __inlinevoid YuvPixel16_8(uint16_t y,
uint16_t u,
uint16_t v,
uint8_t* b,
uint8_t* g,
uint8_t* r, conststruct YuvConstants* yuvconstants) {
LOAD_YUV_CONSTANTS;
uint32_t y32 = y;
u = STATIC_CAST(uint16_t, clamp255(u >> 8));
v = STATIC_CAST(uint16_t, clamp255(v >> 8));
CALC_RGB16;
*b = STATIC_CAST(uint8_t, Clamp((int32_t)(b16) >> 6));
*g = STATIC_CAST(uint8_t, Clamp((int32_t)(g16) >> 6));
*r = STATIC_CAST(uint8_t, Clamp((int32_t)(r16) >> 6));
}
// C reference code that mimics the YUV 16 bit assembly. // Reads 16 bit YUV and leaves result as 16 bit. static __inlinevoid YuvPixel16_16(uint16_t y,
uint16_t u,
uint16_t v, int* b, int* g, int* r, conststruct YuvConstants* yuvconstants) {
LOAD_YUV_CONSTANTS;
uint32_t y32 = y;
u = STATIC_CAST(uint16_t, clamp255(u >> 8));
v = STATIC_CAST(uint16_t, clamp255(v >> 8));
CALC_RGB16;
*b = b16;
*g = g16;
*r = r16;
}
// C reference code that mimics the YUV assembly. // Reads 8 bit YUV and leaves result as 8 bit. static __inlinevoid YPixel(uint8_t y,
uint8_t* b,
uint8_t* g,
uint8_t* r, conststruct YuvConstants* yuvconstants) { #ifdefined(__aarch64__) || defined(__arm__) || defined(__riscv) int yg = yuvconstants->kRGBCoeffBias[0]; int ygb = yuvconstants->kRGBCoeffBias[4]; #else int ygb = yuvconstants->kYBiasToRgb[0]; int yg = yuvconstants->kYToRgb[0]; #endif
uint32_t y1 = (uint32_t)(y * 0x0101 * yg) >> 16;
uint8_t b8 = STATIC_CAST(uint8_t, Clamp(((int32_t)(y1) + ygb) >> 6));
*b = b8;
*g = b8;
*r = b8;
}
staticvoid StoreAR30(uint8_t* rgb_buf, int b, int g, int r) {
uint32_t ar30;
b = b >> 4; // convert 8 bit 10.6 to 10 bit.
g = g >> 4;
r = r >> 4;
b = Clamp10(b);
g = Clamp10(g);
r = Clamp10(r);
ar30 = b | ((uint32_t)g << 10) | ((uint32_t)r << 20) | 0xc0000000;
(*(uint32_t*)rgb_buf) = ar30;
}
// 10 bit YUV to 10 bit AR30 void I210ToAR30Row_C(const uint16_t* src_y, const uint16_t* src_u, const uint16_t* src_v,
uint8_t* rgb_buf, conststruct YuvConstants* yuvconstants, int width) { int x; int b; int g; int r; for (x = 0; x < width - 1; x += 2) {
YuvPixel10_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf, b, g, r);
YuvPixel10_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf + 4, b, g, r);
src_y += 2;
src_u += 1;
src_v += 1;
rgb_buf += 8; // Advance 2 pixels.
} if (width & 1) {
YuvPixel10_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf, b, g, r);
}
}
// 12 bit YUV to 10 bit AR30 void I212ToAR30Row_C(const uint16_t* src_y, const uint16_t* src_u, const uint16_t* src_v,
uint8_t* rgb_buf, conststruct YuvConstants* yuvconstants, int width) { int x; int b; int g; int r; for (x = 0; x < width - 1; x += 2) {
YuvPixel12_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf, b, g, r);
YuvPixel12_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf + 4, b, g, r);
src_y += 2;
src_u += 1;
src_v += 1;
rgb_buf += 8; // Advance 2 pixels.
} if (width & 1) {
YuvPixel12_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf, b, g, r);
}
}
void I410ToAR30Row_C(const uint16_t* src_y, const uint16_t* src_u, const uint16_t* src_v,
uint8_t* rgb_buf, conststruct YuvConstants* yuvconstants, int width) { int x; int b; int g; int r; for (x = 0; x < width; ++x) {
YuvPixel10_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
StoreAR30(rgb_buf, b, g, r);
src_y += 1;
src_u += 1;
src_v += 1;
rgb_buf += 4; // Advance 1 pixel.
}
}
void DetileRow_C(const uint8_t* src,
ptrdiff_t src_tile_stride,
uint8_t* dst, int width) { int x; for (x = 0; x < width - 15; x += 16) {
memcpy(dst, src, 16);
dst += 16;
src += src_tile_stride;
} if (width & 15) {
memcpy(dst, src, width & 15);
}
}
void DetileRow_16_C(const uint16_t* src,
ptrdiff_t src_tile_stride,
uint16_t* dst, int width) { int x; for (x = 0; x < width - 15; x += 16) {
memcpy(dst, src, 16 * sizeof(uint16_t));
dst += 16;
src += src_tile_stride;
} if (width & 15) {
memcpy(dst, src, (width & 15) * sizeof(uint16_t));
}
}
void DetileSplitUVRow_C(const uint8_t* src_uv,
ptrdiff_t src_tile_stride,
uint8_t* dst_u,
uint8_t* dst_v, int width) { int x; for (x = 0; x < width - 15; x += 16) {
SplitUVRow_C(src_uv, dst_u, dst_v, 8);
dst_u += 8;
dst_v += 8;
src_uv += src_tile_stride;
} if (width & 15) {
SplitUVRow_C(src_uv, dst_u, dst_v, ((width & 15) + 1) / 2);
}
}
void DetileToYUY2_C(const uint8_t* src_y,
ptrdiff_t src_y_tile_stride, const uint8_t* src_uv,
ptrdiff_t src_uv_tile_stride,
uint8_t* dst_yuy2, int width) { for (int x = 0; x < width - 15; x += 16) { for (int i = 0; i < 8; i++) {
dst_yuy2[0] = src_y[0];
dst_yuy2[1] = src_uv[0];
dst_yuy2[2] = src_y[1];
dst_yuy2[3] = src_uv[1];
dst_yuy2 += 4;
src_y += 2;
src_uv += 2;
}
src_y += src_y_tile_stride - 16;
src_uv += src_uv_tile_stride - 16;
}
}
// Unpack MT2T into tiled P010 64 pixels at a time. MT2T's bitstream is encoded // in 80 byte blocks representing 64 pixels each. The first 16 bytes of the // block contain all of the lower 2 bits of each pixel packed together, and the // next 64 bytes represent all the upper 8 bits of the pixel. The lower bits are // packed into 1x4 blocks, whereas the upper bits are packed in normal raster // order. void UnpackMT2T_C(const uint8_t* src, uint16_t* dst, size_t size) { for (size_t i = 0; i < size; i += 80) { const uint8_t* src_lower_bits = src; const uint8_t* src_upper_bits = src + 16;
// Convert lsb formats to msb, depending on sample depth. void MergeUVRow_16_C(const uint16_t* src_u, const uint16_t* src_v,
uint16_t* dst_uv, int depth, int width) { int shift = 16 - depth;
assert(depth >= 8);
assert(depth <= 16); int x; for (x = 0; x < width; ++x) {
dst_uv[0] = STATIC_CAST(uint16_t, src_u[x] << shift);
dst_uv[1] = STATIC_CAST(uint16_t, src_v[x] << shift);
dst_uv += 2;
}
}
// Convert msb formats to lsb, depending on sample depth. void SplitUVRow_16_C(const uint16_t* src_uv,
uint16_t* dst_u,
uint16_t* dst_v, int depth, int width) { int shift = 16 - depth; int x;
assert(depth >= 8);
assert(depth <= 16); for (x = 0; x < width; ++x) {
dst_u[x] = src_uv[0] >> shift;
dst_v[x] = src_uv[1] >> shift;
src_uv += 2;
}
}
void MultiplyRow_16_C(const uint16_t* src_y,
uint16_t* dst_y, int scale, int width) { int x; for (x = 0; x < width; ++x) {
dst_y[x] = STATIC_CAST(uint16_t, src_y[x] * scale);
}
}
void DivideRow_16_C(const uint16_t* src_y,
uint16_t* dst_y, int scale, int width) { int x; for (x = 0; x < width; ++x) {
dst_y[x] = (src_y[x] * scale) >> 16;
}
}
// Use scale to convert lsb formats to msb, depending how many bits there are: // 32768 = 9 bits // 16384 = 10 bits // 4096 = 12 bits // 256 = 16 bits // TODO(fbarchard): change scale to bits #define C16TO8(v, scale) clamp255(((v) * (scale)) >> 16)
void Convert16To8Row_C(const uint16_t* src_y,
uint8_t* dst_y, int scale, int width) { int x;
assert(scale >= 256);
assert(scale <= 32768);
for (x = 0; x < width; ++x) {
dst_y[x] = STATIC_CAST(uint8_t, C16TO8(src_y[x], scale));
}
}
// Use scale to convert lsb formats to msb, depending how many bits there are: // 1024 = 10 bits void Convert8To16Row_C(const uint8_t* src_y,
uint16_t* dst_y, int scale, int width) { int x;
scale *= 0x0101; // replicates the byte. for (x = 0; x < width; ++x) {
dst_y[x] = (src_y[x] * scale) >> 16;
}
}
void ARGBSetRow_C(uint8_t* dst_argb, uint32_t v32, int width) { int x; for (x = 0; x < width; ++x) {
memcpy(dst_argb + x * sizeof v32, &v32, sizeof v32);
}
}
// Filter 2 rows of YUY2 UV's (422) into U and V (420). void YUY2ToUVRow_C(const uint8_t* src_yuy2, int src_stride_yuy2,
uint8_t* dst_u,
uint8_t* dst_v, int width) { // Output a row of UV values, filtering 2 rows of YUY2. int x; for (x = 0; x < width; x += 2) {
dst_u[0] = (src_yuy2[1] + src_yuy2[src_stride_yuy2 + 1] + 1) >> 1;
dst_v[0] = (src_yuy2[3] + src_yuy2[src_stride_yuy2 + 3] + 1) >> 1;
src_yuy2 += 4;
dst_u += 1;
dst_v += 1;
}
}
// Filter 2 rows of YUY2 UV's (422) into UV (NV12). void YUY2ToNVUVRow_C(const uint8_t* src_yuy2, int src_stride_yuy2,
uint8_t* dst_uv, int width) { // Output a row of UV values, filtering 2 rows of YUY2. int x; for (x = 0; x < width; x += 2) {
dst_uv[0] = (src_yuy2[1] + src_yuy2[src_stride_yuy2 + 1] + 1) >> 1;
dst_uv[1] = (src_yuy2[3] + src_yuy2[src_stride_yuy2 + 3] + 1) >> 1;
src_yuy2 += 4;
dst_uv += 2;
}
}
// Copy row of YUY2 UV's (422) into U and V (422). void YUY2ToUV422Row_C(const uint8_t* src_yuy2,
uint8_t* dst_u,
uint8_t* dst_v, int width) { // Output a row of UV values. int x; for (x = 0; x < width; x += 2) {
dst_u[0] = src_yuy2[1];
dst_v[0] = src_yuy2[3];
src_yuy2 += 4;
dst_u += 1;
dst_v += 1;
}
}
// Copy row of YUY2 Y's (422) into Y (420/422). void YUY2ToYRow_C(const uint8_t* src_yuy2, uint8_t* dst_y, int width) { // Output a row of Y values. int x; for (x = 0; x < width - 1; x += 2) {
dst_y[x] = src_yuy2[0];
dst_y[x + 1] = src_yuy2[2];
src_yuy2 += 4;
} if (width & 1) {
dst_y[width - 1] = src_yuy2[0];
}
}
// Filter 2 rows of UYVY UV's (422) into U and V (420). void UYVYToUVRow_C(const uint8_t* src_uyvy, int src_stride_uyvy,
uint8_t* dst_u,
uint8_t* dst_v, int width) { // Output a row of UV values. int x; for (x = 0; x < width; x += 2) {
dst_u[0] = (src_uyvy[0] + src_uyvy[src_stride_uyvy + 0] + 1) >> 1;
dst_v[0] = (src_uyvy[2] + src_uyvy[src_stride_uyvy + 2] + 1) >> 1;
src_uyvy += 4;
dst_u += 1;
dst_v += 1;
}
}
// Copy row of UYVY UV's (422) into U and V (422). void UYVYToUV422Row_C(const uint8_t* src_uyvy,
uint8_t* dst_u,
uint8_t* dst_v, int width) { // Output a row of UV values. int x; for (x = 0; x < width; x += 2) {
dst_u[0] = src_uyvy[0];
dst_v[0] = src_uyvy[2];
src_uyvy += 4;
dst_u += 1;
dst_v += 1;
}
}
// Copy row of UYVY Y's (422) into Y (420/422). void UYVYToYRow_C(const uint8_t* src_uyvy, uint8_t* dst_y, int width) { // Output a row of Y values. int x; for (x = 0; x < width - 1; x += 2) {
dst_y[x] = src_uyvy[1];
dst_y[x + 1] = src_uyvy[3];
src_uyvy += 4;
} if (width & 1) {
dst_y[width - 1] = src_uyvy[1];
}
}
#define BLEND(f, b, a) clamp255((((256 - a) * b) >> 8) + f)
// Blend src_argb over src_argb1 and store to dst_argb. // dst_argb may be src_argb or src_argb1. // This code mimics the SSSE3 version for better testability. void ARGBBlendRow_C(const uint8_t* src_argb, const uint8_t* src_argb1,
uint8_t* dst_argb, int width) { int x; for (x = 0; x < width - 1; x += 2) {
uint32_t fb = src_argb[0];
uint32_t fg = src_argb[1];
uint32_t fr = src_argb[2];
uint32_t a = src_argb[3];
uint32_t bb = src_argb1[0];
uint32_t bg = src_argb1[1];
uint32_t br = src_argb1[2];
dst_argb[0] = STATIC_CAST(uint8_t, BLEND(fb, bb, a));
dst_argb[1] = STATIC_CAST(uint8_t, BLEND(fg, bg, a));
dst_argb[2] = STATIC_CAST(uint8_t, BLEND(fr, br, a));
dst_argb[3] = 255u;
#ifdefined(LIBYUV_UNATTENUATE_DUP) // This code mimics the Intel SIMD version for better testability. #define UNATTENUATE(f, ia) clamp255(((f | (f << 8)) * ia) >> 16) #else #define UNATTENUATE(f, ia) clamp255((f * ia) >> 8) #endif
// mimics the Intel SIMD code for exactness. void ARGBUnattenuateRow_C(const uint8_t* src_argb,
uint8_t* dst_argb, int width) { int i; for (i = 0; i < width; ++i) {
uint32_t b = src_argb[0];
uint32_t g = src_argb[1];
uint32_t r = src_argb[2]; const uint32_t a = src_argb[3]; const uint32_t ia = fixed_invtbl8[a] & 0xffff; // 8.8 fixed point
// Clamping should not be necessary but is free in assembly.
dst_argb[0] = STATIC_CAST(uint8_t, UNATTENUATE(b, ia));
dst_argb[1] = STATIC_CAST(uint8_t, UNATTENUATE(g, ia));
dst_argb[2] = STATIC_CAST(uint8_t, UNATTENUATE(r, ia));
dst_argb[3] = STATIC_CAST(uint8_t, a);
src_argb += 4;
dst_argb += 4;
}
}
// Copy pixels from rotated source to destination row with a slope.
LIBYUV_API void ARGBAffineRow_C(const uint8_t* src_argb, int src_argb_stride,
uint8_t* dst_argb, constfloat* uv_dudv, int width) { int i; // Render a row of pixels from source into a buffer. float uv[2];
uv[0] = uv_dudv[0];
uv[1] = uv_dudv[1]; for (i = 0; i < width; ++i) { int x = (int)(uv[0]); int y = (int)(uv[1]);
*(uint32_t*)(dst_argb) =
*(const uint32_t*)(src_argb + y * src_argb_stride + x * 4);
dst_argb += 4;
uv[0] += uv_dudv[2];
uv[1] += uv_dudv[3];
}
}
// Blend 2 rows into 1. staticvoid HalfRow_C(const uint8_t* src_uv,
ptrdiff_t src_uv_stride,
uint8_t* dst_uv, int width) { int x; for (x = 0; x < width; ++x) {
dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
}
}
staticvoid HalfRow_16_C(const uint16_t* src_uv,
ptrdiff_t src_uv_stride,
uint16_t* dst_uv, int width) { int x; for (x = 0; x < width; ++x) {
dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
}
}
staticvoid HalfRow_16To8_C(const uint16_t* src_uv,
ptrdiff_t src_uv_stride,
uint8_t* dst_uv, int scale, int width) { int x; for (x = 0; x < width; ++x) {
dst_uv[x] = STATIC_CAST(
uint8_t,
C16TO8((src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1, scale));
}
}
// C version 2x2 -> 2x1. void InterpolateRow_C(uint8_t* dst_ptr, const uint8_t* src_ptr,
ptrdiff_t src_stride, int width, int source_y_fraction) { int y1_fraction = source_y_fraction; int y0_fraction = 256 - y1_fraction; const uint8_t* src_ptr1 = src_ptr + src_stride; int x;
assert(source_y_fraction >= 0);
assert(source_y_fraction < 256);
// Samples assumed to be unsigned in low 9, 10 or 12 bits. Scale factor // adjust the source integer range to the half float range desired.
// This magic constant is 2^-112. Multiplying by this // is the same as subtracting 112 from the exponent, which // is the difference in exponent bias between 32-bit and // 16-bit floats. Once we've done this subtraction, we can // simply extract the low bits of the exponent and the high // bits of the mantissa from our float and we're done.
// Work around GCC 7 punning warning -Wstrict-aliasing #ifdefined(__GNUC__) typedef uint32_t __attribute__((__may_alias__)) uint32_alias_t; #else typedef uint32_t uint32_alias_t; #endif
void HalfFloatRow_C(const uint16_t* src,
uint16_t* dst, float scale, int width) { int i; float mult = 1.9259299444e-34f * scale; for (i = 0; i < width; ++i) { float value = src[i] * mult;
dst[i] = (uint16_t)((*(const uint32_alias_t*)&value) >> 13);
}
}
void ByteToFloatRow_C(const uint8_t* src, float* dst, float scale, int width) { int i; for (i = 0; i < width; ++i) { float value = src[i] * scale;
dst[i] = value;
}
}
#ifdef HAS_INTERPOLATEROW_16TO8_AVX2 void InterpolateRow_16To8_AVX2(uint8_t* dst_ptr, const uint16_t* src_ptr,
ptrdiff_t src_stride, int scale, int width, int source_y_fraction) { // Row buffer for intermediate 16 bit pixels.
SIMD_ALIGNED(uint16_t row[MAXTWIDTH]); while (width > 0) { int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
InterpolateRow_16_C(row, src_ptr, src_stride, twidth, source_y_fraction);
Convert16To8Row_AVX2(row, dst_ptr, scale, twidth);
src_ptr += twidth;
dst_ptr += twidth;
width -= twidth;
}
} #endif// HAS_INTERPOLATEROW_16TO8_AVX2
float ScaleSumSamples_C(constfloat* src, float* dst, float scale, int width) { float fsum = 0.f; int i; for (i = 0; i < width; ++i) { float v = *src++;
fsum += v * v;
*dst++ = v * scale;
} return fsum;
}
float ScaleMaxSamples_C(constfloat* src, float* dst, float scale, int width) { float fmax = 0.f; int i; for (i = 0; i < width; ++i) { float v = *src++; float vs = v * scale;
fmax = (v > fmax) ? v : fmax;
*dst++ = vs;
} return fmax;
}
void ScaleSamples_C(constfloat* src, float* dst, float scale, int width) { int i; for (i = 0; i < width; ++i) {
*dst++ = *src++ * scale;
}
}
void GaussRow_C(const uint32_t* src, uint16_t* dst, int width) { int i; for (i = 0; i < width; ++i) {
*dst++ = STATIC_CAST(
uint16_t,
(src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4] + 128) >> 8);
++src;
}
}
// filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row. void GaussCol_C(const uint16_t* src0, const uint16_t* src1, const uint16_t* src2, const uint16_t* src3, const uint16_t* src4,
uint32_t* dst, int width) { int i; for (i = 0; i < width; ++i) {
*dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
}
}
void GaussRow_F32_C(constfloat* src, float* dst, int width) { int i; for (i = 0; i < width; ++i) {
*dst++ = (src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4]) *
(1.0f / 256.0f);
++src;
}
}
// filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row. void GaussCol_F32_C(constfloat* src0, constfloat* src1, constfloat* src2, constfloat* src3, constfloat* src4, float* dst, int width) { int i; for (i = 0; i < width; ++i) {
*dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
}
}
// Convert biplanar NV21 to packed YUV24 void NV21ToYUV24Row_C(const uint8_t* src_y, const uint8_t* src_vu,
uint8_t* dst_yuv24, int width) { int x; for (x = 0; x < width - 1; x += 2) {
dst_yuv24[0] = src_vu[0]; // V
dst_yuv24[1] = src_vu[1]; // U
dst_yuv24[2] = src_y[0]; // Y0
dst_yuv24[3] = src_vu[0]; // V
dst_yuv24[4] = src_vu[1]; // U
dst_yuv24[5] = src_y[1]; // Y1
src_y += 2;
src_vu += 2;
dst_yuv24 += 6; // Advance 2 pixels.
} if (width & 1) {
dst_yuv24[0] = src_vu[0]; // V
dst_yuv24[1] = src_vu[1]; // U
dst_yuv24[2] = src_y[0]; // Y0
}
}
// Filter 2 rows of AYUV UV's (444) into UV (420). // AYUV is VUYA in memory. UV for NV12 is UV order in memory. void AYUVToUVRow_C(const uint8_t* src_ayuv, int src_stride_ayuv,
uint8_t* dst_uv, int width) { // Output a row of UV values, filtering 2x2 rows of AYUV. int x; for (x = 0; x < width - 1; x += 2) {
dst_uv[0] = (src_ayuv[1] + src_ayuv[5] + src_ayuv[src_stride_ayuv + 1] +
src_ayuv[src_stride_ayuv + 5] + 2) >>
2;
dst_uv[1] = (src_ayuv[0] + src_ayuv[4] + src_ayuv[src_stride_ayuv + 0] +
src_ayuv[src_stride_ayuv + 4] + 2) >>
2;
src_ayuv += 8;
dst_uv += 2;
} if (width & 1) {
dst_uv[0] = (src_ayuv[1] + src_ayuv[src_stride_ayuv + 1] + 1) >> 1;
dst_uv[1] = (src_ayuv[0] + src_ayuv[src_stride_ayuv + 0] + 1) >> 1;
}
}
// Filter 2 rows of AYUV UV's (444) into VU (420). void AYUVToVURow_C(const uint8_t* src_ayuv, int src_stride_ayuv,
uint8_t* dst_vu, int width) { // Output a row of VU values, filtering 2x2 rows of AYUV. int x; for (x = 0; x < width - 1; x += 2) {
dst_vu[0] = (src_ayuv[0] + src_ayuv[4] + src_ayuv[src_stride_ayuv + 0] +
src_ayuv[src_stride_ayuv + 4] + 2) >>
2;
dst_vu[1] = (src_ayuv[1] + src_ayuv[5] + src_ayuv[src_stride_ayuv + 1] +
src_ayuv[src_stride_ayuv + 5] + 2) >>
2;
src_ayuv += 8;
dst_vu += 2;
} if (width & 1) {
dst_vu[0] = (src_ayuv[0] + src_ayuv[src_stride_ayuv + 0] + 1) >> 1;
dst_vu[1] = (src_ayuv[1] + src_ayuv[src_stride_ayuv + 1] + 1) >> 1;
}
}
// Copy row of AYUV Y's into Y void AYUVToYRow_C(const uint8_t* src_ayuv, uint8_t* dst_y, int width) { // Output a row of Y values. int x; for (x = 0; x < width; ++x) {
dst_y[x] = src_ayuv[2]; // v,u,y,a
src_ayuv += 4;
}
}
// Convert UV plane of NV12 to VU of NV21. void SwapUVRow_C(const uint8_t* src_uv, uint8_t* dst_vu, int width) { int x; for (x = 0; x < width; ++x) {
uint8_t u = src_uv[0];
uint8_t v = src_uv[1];
dst_vu[0] = v;
dst_vu[1] = u;
src_uv += 2;
dst_vu += 2;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.