michael@0: // Copyright (c) 2010 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "yuv_row.h" michael@0: michael@0: #define DCHECK(a) michael@0: michael@0: extern "C" { michael@0: michael@0: // C reference code that mimic the YUV assembly. michael@0: #define packuswb(x) ((x) < 0 ? 0 : ((x) > 255 ? 255 : (x))) michael@0: #define paddsw(x, y) (((x) + (y)) < -32768 ? -32768 : \ michael@0: (((x) + (y)) > 32767 ? 32767 : ((x) + (y)))) michael@0: michael@0: static inline void YuvPixel(uint8 y, michael@0: uint8 u, michael@0: uint8 v, michael@0: uint8* rgb_buf) { michael@0: michael@0: int b = kCoefficientsRgbY[256+u][0]; michael@0: int g = kCoefficientsRgbY[256+u][1]; michael@0: int r = kCoefficientsRgbY[256+u][2]; michael@0: int a = kCoefficientsRgbY[256+u][3]; michael@0: michael@0: b = paddsw(b, kCoefficientsRgbY[512+v][0]); michael@0: g = paddsw(g, kCoefficientsRgbY[512+v][1]); michael@0: r = paddsw(r, kCoefficientsRgbY[512+v][2]); michael@0: a = paddsw(a, kCoefficientsRgbY[512+v][3]); michael@0: michael@0: b = paddsw(b, kCoefficientsRgbY[y][0]); michael@0: g = paddsw(g, kCoefficientsRgbY[y][1]); michael@0: r = paddsw(r, kCoefficientsRgbY[y][2]); michael@0: a = paddsw(a, kCoefficientsRgbY[y][3]); michael@0: michael@0: b >>= 6; michael@0: g >>= 6; michael@0: r >>= 6; michael@0: a >>= 6; michael@0: michael@0: *reinterpret_cast(rgb_buf) = (packuswb(b)) | michael@0: (packuswb(g) << 8) | michael@0: (packuswb(r) << 16) | michael@0: (packuswb(a) << 24); michael@0: } michael@0: michael@0: void FastConvertYUVToRGB32Row_C(const uint8* y_buf, michael@0: const uint8* u_buf, michael@0: const uint8* v_buf, michael@0: uint8* rgb_buf, michael@0: int width, michael@0: unsigned int x_shift) { michael@0: for (int x = 0; x < width; x += 2) { michael@0: uint8 u = u_buf[x >> x_shift]; michael@0: uint8 v = v_buf[x >> x_shift]; michael@0: uint8 y0 = y_buf[x]; michael@0: YuvPixel(y0, u, v, rgb_buf); michael@0: if ((x + 1) < width) { michael@0: uint8 y1 = y_buf[x + 1]; michael@0: if (x_shift == 0) { michael@0: u = u_buf[x + 1]; michael@0: v = v_buf[x + 1]; michael@0: } michael@0: YuvPixel(y1, u, v, rgb_buf + 4); michael@0: } michael@0: rgb_buf += 8; // Advance 2 pixels. michael@0: } michael@0: } michael@0: michael@0: // 16.16 fixed point is used. A shift by 16 isolates the integer. michael@0: // A shift by 17 is used to further subsample the chrominence channels. michael@0: // & 0xffff isolates the fixed point fraction. >> 2 to get the upper 2 bits, michael@0: // for 1/65536 pixel accurate interpolation. michael@0: void ScaleYUVToRGB32Row_C(const uint8* y_buf, michael@0: const uint8* u_buf, michael@0: const uint8* v_buf, michael@0: uint8* rgb_buf, michael@0: int width, michael@0: int source_dx) { michael@0: int x = 0; michael@0: for (int i = 0; i < width; i += 2) { michael@0: int y = y_buf[x >> 16]; michael@0: int u = u_buf[(x >> 17)]; michael@0: int v = v_buf[(x >> 17)]; michael@0: YuvPixel(y, u, v, rgb_buf); michael@0: x += source_dx; michael@0: if ((i + 1) < width) { michael@0: y = y_buf[x >> 16]; michael@0: YuvPixel(y, u, v, rgb_buf+4); michael@0: x += source_dx; michael@0: } michael@0: rgb_buf += 8; michael@0: } michael@0: } michael@0: michael@0: void LinearScaleYUVToRGB32Row_C(const uint8* y_buf, michael@0: const uint8* u_buf, michael@0: const uint8* v_buf, michael@0: uint8* rgb_buf, michael@0: int width, michael@0: int source_dx) { michael@0: int x = 0; michael@0: if (source_dx >= 0x20000) { michael@0: x = 32768; michael@0: } michael@0: for (int i = 0; i < width; i += 2) { michael@0: int y0 = y_buf[x >> 16]; michael@0: int y1 = y_buf[(x >> 16) + 1]; michael@0: int u0 = u_buf[(x >> 17)]; michael@0: int u1 = u_buf[(x >> 17) + 1]; michael@0: int v0 = v_buf[(x >> 17)]; michael@0: int v1 = v_buf[(x >> 17) + 1]; michael@0: int y_frac = (x & 65535); michael@0: int uv_frac = ((x >> 1) & 65535); michael@0: int y = (y_frac * y1 + (y_frac ^ 65535) * y0) >> 16; michael@0: int u = (uv_frac * u1 + (uv_frac ^ 65535) * u0) >> 16; michael@0: int v = (uv_frac * v1 + (uv_frac ^ 65535) * v0) >> 16; michael@0: YuvPixel(y, u, v, rgb_buf); michael@0: x += source_dx; michael@0: if ((i + 1) < width) { michael@0: y0 = y_buf[x >> 16]; michael@0: y1 = y_buf[(x >> 16) + 1]; michael@0: y_frac = (x & 65535); michael@0: y = (y_frac * y1 + (y_frac ^ 65535) * y0) >> 16; michael@0: YuvPixel(y, u, v, rgb_buf+4); michael@0: x += source_dx; michael@0: } michael@0: rgb_buf += 8; michael@0: } michael@0: } michael@0: michael@0: } // extern "C" michael@0: