15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2010 Google Inc. All Rights Reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license 4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// that can be found in the COPYING file in the root of the source 5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// tree. An additional intellectual property rights grant can be found 6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// in the file PATENTS. All contributing project authors may 7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// be found in the AUTHORS file in the root of the source tree. 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ----------------------------------------------------------------------------- 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// inline YUV<->RGB conversion function 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The exact naming is Y'CbCr, following the ITU-R BT.601 standard. 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// More information at: http://en.wikipedia.org/wiki/YCbCr 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// V = 0.4394 * R - 0.3679 * G - 0.0715 * B + 128 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// We use 16bit fixed point operations for RGB->YUV conversion (YUV_FIX). 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// For the Y'CbCr to RGB conversion, the BT.601 specification reads: 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// R = 1.164 * (Y-16) + 1.596 * (V-128) 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// G = 1.164 * (Y-16) - 0.813 * (V-128) - 0.391 * (U-128) 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// B = 1.164 * (Y-16) + 2.018 * (U-128) 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// where Y is in the [16,235] range, and U/V in the [16,240] range. 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// In the table-lookup version (WEBP_YUV_USE_TABLE), the common factor 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// "1.164 * (Y-16)" can be handled as an offset in the VP8kClip[] table. 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// So in this case the formulae should read: 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// R = 1.164 * [Y + 1.371 * (V-128) ] - 18.624 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// G = 1.164 * [Y - 0.698 * (V-128) - 0.336 * (U-128)] - 18.624 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// B = 1.164 * [Y + 1.733 * (U-128)] - 18.624 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// once factorized. 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// For YUV->RGB conversion, only 14bit fixed precision is used (YUV_FIX2). 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// That's the maximum possible for a convenient ARM implementation. 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: Skal (pascal.massimino@gmail.com) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WEBP_DSP_YUV_H_ 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define WEBP_DSP_YUV_H_ 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "./dsp.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "../dec/decode_vp8.h" 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Define the following to use the LUT-based code: 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// #define WEBP_YUV_USE_TABLE 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(WEBP_EXPERIMENTAL_FEATURES) 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Do NOT activate this feature for real compression. This is only experimental! 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This flag is for comparison purpose against JPEG's "YUVj" natural colorspace. 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This colorspace is close to Rec.601's Y'CbCr model with the notable 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// difference of allowing larger range for luma/chroma. 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// See http://en.wikipedia.org/wiki/YCbCr#JPEG_conversion paragraph, and its 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// difference with http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// #define USE_YUVj 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------ 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// YUV -> RGB conversion 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef __cplusplus 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)enum { 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_FIX = 16, // fixed-point precision for RGB->YUV 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_HALF = 1 << (YUV_FIX - 1), 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_MASK = (256 << YUV_FIX) - 1, 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_RANGE_MIN = -227, // min value of r/g/b output 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_RANGE_MAX = 256 + 226, // max value of r/g/b output 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_FIX2 = 14, // fixed-point precision for YUV->RGB 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_HALF2 = 1 << (YUV_FIX2 - 1), 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) YUV_MASK2 = (256 << YUV_FIX2) - 1 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// These constants are 14b fixed-point version of ITU-R BT.601 constants. 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kYScale 19077 // 1.164 = 255 / 219 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kVToR 26149 // 1.596 = 255 / 112 * 0.701 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kUToG 6419 // 0.391 = 255 / 112 * 0.886 * 0.114 / 0.587 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kVToG 13320 // 0.813 = 255 / 112 * 0.701 * 0.299 / 0.587 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kUToB 33050 // 2.018 = 255 / 112 * 0.886 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kRCst (-kYScale * 16 - kVToR * 128 + YUV_HALF2) 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kGCst (-kYScale * 16 + kUToG * 128 + kVToG * 128 + YUV_HALF2) 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define kBCst (-kYScale * 16 - kUToB * 128 + YUV_HALF2) 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//------------------------------------------------------------------------------ 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(WEBP_YUV_USE_TABLE) 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// slower on x86 by ~7-8%, but bit-exact with the SSE2 version 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8Clip8(int v) { 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ((v & ~YUV_MASK2) == 0) ? (v >> YUV_FIX2) : (v < 0) ? 0 : 255; 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8YUVToR(int y, int v) { 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8Clip8(kYScale * y + kVToR * v + kRCst); 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8YUVToG(int y, int u, int v) { 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8Clip8(kYScale * y - kUToG * u - kVToG * v + kGCst); 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8YUVToB(int y, int u) { 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8Clip8(kYScale * y + kUToB * u + kBCst); 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v, 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8_t* const rgb) { 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rgb[0] = VP8YUVToR(y, v); 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rgb[1] = VP8YUVToG(y, u, v); 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rgb[2] = VP8YUVToB(y, u); 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v, 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8_t* const bgr) { 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bgr[0] = VP8YUVToB(y, u); 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bgr[1] = VP8YUVToG(y, u, v); 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bgr[2] = VP8YUVToR(y, v); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v, 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8_t* const rgb) { 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int r = VP8YUVToR(y, v); // 5 usable bits 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int g = VP8YUVToG(y, u, v); // 6 usable bits 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int b = VP8YUVToB(y, u); // 5 usable bits 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int rg = (r & 0xf8) | (g >> 5); 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int gb = ((g << 3) & 0xe0) | (b >> 3); 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef WEBP_SWAP_16BIT_CSP 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[0] = gb; 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[1] = rg; 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[0] = rg; 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[1] = gb; 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v, 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8_t* const argb) { 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int r = VP8YUVToR(y, v); // 4 usable bits 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int g = VP8YUVToG(y, u, v); // 4 usable bits 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int b = VP8YUVToB(y, u); // 4 usable bits 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int rg = (r & 0xf0) | (g >> 4); 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef WEBP_SWAP_16BIT_CSP 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[0] = ba; 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[1] = rg; 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[0] = rg; 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[1] = ba; 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Table-based version, not totally equivalent to the SSE2 version. 1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Rounding diff is only +/-1 though. 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern int16_t VP8kVToR[256], VP8kUToB[256]; 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern int32_t VP8kVToG[256], VP8kUToG[256]; 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; 1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN]; 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v, 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8_t* const rgb) { 1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int r_off = VP8kVToR[v]; 1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; 1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int b_off = VP8kUToB[u]; 1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN]; 1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; 1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN]; 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v, 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8_t* const bgr) { 1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int r_off = VP8kVToR[v]; 1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int b_off = VP8kUToB[u]; 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN]; 1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; 1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN]; 1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v, 1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8_t* const rgb) { 1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int r_off = VP8kVToR[v]; 1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; 1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int b_off = VP8kUToB[u]; 1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int rg = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) | 1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5)); 1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int gb = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) | 1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3)); 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef WEBP_SWAP_16BIT_CSP 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[0] = gb; 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[1] = rg; 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[0] = rg; 1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rgb[1] = gb; 1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v, 2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8_t* const argb) { 2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int r_off = VP8kVToR[v]; 2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; 2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int b_off = VP8kUToB[u]; 2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int rg = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) | 2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]); 2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int ba = (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4) | 0x0f; 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef WEBP_SWAP_16BIT_CSP 2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[0] = ba; 2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[1] = rg; 2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[0] = rg; 2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[1] = ba; 2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // WEBP_YUV_USE_TABLE 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//----------------------------------------------------------------------------- 2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Alpha handling variants 2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v, 2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8_t* const argb) { 2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) argb[0] = 0xff; 2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) VP8YuvToRgb(y, u, v, argb + 1); 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v, 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8_t* const bgra) { 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) VP8YuvToBgr(y, u, v, bgra); 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bgra[3] = 0xff; 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v, 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8_t* const rgba) { 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) VP8YuvToRgb(y, u, v, rgba); 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rgba[3] = 0xff; 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Must be called before everything, to initialize the tables. 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void VP8YUVInit(void); 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//----------------------------------------------------------------------------- 2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// SSE2 extra functions (mostly for upsampling_sse2.c) 2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(WEBP_USE_SSE2) 2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// When the following is defined, tables are initialized statically, adding ~12k 2495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// to the binary size. Otherwise, they are initialized at run-time (small cost). 2505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define WEBP_YUV_USE_SSE2_TABLES 2515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(FANCY_UPSAMPLING) 2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Process 32 pixels and store the result (24b or 32b per pixel) in *dst. 2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void VP8YuvToRgba32(const uint8_t* y, const uint8_t* u, const uint8_t* v, 2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) uint8_t* dst); 2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void VP8YuvToRgb32(const uint8_t* y, const uint8_t* u, const uint8_t* v, 2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) uint8_t* dst); 2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void VP8YuvToBgra32(const uint8_t* y, const uint8_t* u, const uint8_t* v, 2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) uint8_t* dst); 2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void VP8YuvToBgr32(const uint8_t* y, const uint8_t* u, const uint8_t* v, 2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) uint8_t* dst); 2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // FANCY_UPSAMPLING 2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Must be called to initialize tables before using the functions. 2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void VP8YUVInitSSE2(void); 2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // WEBP_USE_SSE2 2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------ 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// RGB -> YUV conversion 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Stub functions that can be called with various rounding values: 2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8ClipUV(int uv, int rounding) { 2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) uv = (uv + rounding + (128 << (YUV_FIX + 2))) >> (YUV_FIX + 2); 2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ((uv & ~0xff) == 0) ? uv : (uv < 0) ? 0 : 255; 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef USE_YUVj 2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) { 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int luma = 16839 * r + 33059 * g + 6420 * b; 2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return (luma + rounding + (16 << YUV_FIX)) >> YUV_FIX; // no need to clip 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) { 2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const int u = -9719 * r - 19081 * g + 28800 * b; 2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8ClipUV(u, rounding); 2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) { 2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const int v = +28800 * r - 24116 * g - 4684 * b; 2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8ClipUV(v, rounding); 2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This JPEG-YUV colorspace, only for comparison! 2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// These are also 16bit precision coefficients from Rec.601, but with full 2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// [0..255] output range. 3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) { 3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const int luma = 19595 * r + 38470 * g + 7471 * b; 3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return (luma + rounding) >> YUV_FIX; // no need to clip 3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) { 3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const int u = -11058 * r - 21710 * g + 32768 * b; 3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8ClipUV(u, rounding); 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) { 3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const int v = 32768 * r - 27439 * g - 5329 * b; 3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return VP8ClipUV(v, rounding); 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // USE_YUVj 3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef __cplusplus 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // extern "C" 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* WEBP_DSP_YUV_H_ */ 322