upsampling.c revision e7492d303e42ac495966513a74d78918f9731bf7
1// Copyright 2011 Google Inc. All Rights Reserved. 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the COPYING file in the root of the source 5// tree. An additional intellectual property rights grant can be found 6// in the file PATENTS. All contributing project authors may 7// be found in the AUTHORS file in the root of the source tree. 8// ----------------------------------------------------------------------------- 9// 10// YUV to RGB upsampling functions. 11// 12// Author: somnath@google.com (Somnath Banerjee) 13 14#include "./dsp.h" 15#include "./yuv.h" 16 17#include <assert.h> 18 19//------------------------------------------------------------------------------ 20// Fancy upsampler 21 22#ifdef FANCY_UPSAMPLING 23 24// Fancy upsampling functions to convert YUV to RGB 25WebPUpsampleLinePairFunc WebPUpsamplers[MODE_LAST]; 26 27// Given samples laid out in a square as: 28// [a b] 29// [c d] 30// we interpolate u/v as: 31// ([9*a + 3*b + 3*c + d 3*a + 9*b + 3*c + d] + [8 8]) / 16 32// ([3*a + b + 9*c + 3*d a + 3*b + 3*c + 9*d] [8 8]) / 16 33 34// We process u and v together stashed into 32bit (16bit each). 35#define LOAD_UV(u, v) ((u) | ((v) << 16)) 36 37#define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \ 38static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \ 39 const uint8_t* top_u, const uint8_t* top_v, \ 40 const uint8_t* cur_u, const uint8_t* cur_v, \ 41 uint8_t* top_dst, uint8_t* bottom_dst, int len) { \ 42 int x; \ 43 const int last_pixel_pair = (len - 1) >> 1; \ 44 uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]); /* top-left sample */ \ 45 uint32_t l_uv = LOAD_UV(cur_u[0], cur_v[0]); /* left-sample */ \ 46 assert(top_y != NULL); \ 47 { \ 48 const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \ 49 FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \ 50 } \ 51 if (bottom_y != NULL) { \ 52 const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \ 53 FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst); \ 54 } \ 55 for (x = 1; x <= last_pixel_pair; ++x) { \ 56 const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]); /* top sample */ \ 57 const uint32_t uv = LOAD_UV(cur_u[x], cur_v[x]); /* sample */ \ 58 /* precompute invariant values associated with first and second diagonals*/\ 59 const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u; \ 60 const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3; \ 61 const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3; \ 62 { \ 63 const uint32_t uv0 = (diag_12 + tl_uv) >> 1; \ 64 const uint32_t uv1 = (diag_03 + t_uv) >> 1; \ 65 FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \ 66 top_dst + (2 * x - 1) * XSTEP); \ 67 FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16), \ 68 top_dst + (2 * x - 0) * XSTEP); \ 69 } \ 70 if (bottom_y != NULL) { \ 71 const uint32_t uv0 = (diag_03 + l_uv) >> 1; \ 72 const uint32_t uv1 = (diag_12 + uv) >> 1; \ 73 FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \ 74 bottom_dst + (2 * x - 1) * XSTEP); \ 75 FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16), \ 76 bottom_dst + (2 * x + 0) * XSTEP); \ 77 } \ 78 tl_uv = t_uv; \ 79 l_uv = uv; \ 80 } \ 81 if (!(len & 1)) { \ 82 { \ 83 const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \ 84 FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16), \ 85 top_dst + (len - 1) * XSTEP); \ 86 } \ 87 if (bottom_y != NULL) { \ 88 const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \ 89 FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16), \ 90 bottom_dst + (len - 1) * XSTEP); \ 91 } \ 92 } \ 93} 94 95// All variants implemented. 96UPSAMPLE_FUNC(UpsampleRgbLinePair, VP8YuvToRgb, 3) 97UPSAMPLE_FUNC(UpsampleBgrLinePair, VP8YuvToBgr, 3) 98UPSAMPLE_FUNC(UpsampleRgbaLinePair, VP8YuvToRgba, 4) 99UPSAMPLE_FUNC(UpsampleBgraLinePair, VP8YuvToBgra, 4) 100UPSAMPLE_FUNC(UpsampleArgbLinePair, VP8YuvToArgb, 4) 101UPSAMPLE_FUNC(UpsampleRgba4444LinePair, VP8YuvToRgba4444, 2) 102UPSAMPLE_FUNC(UpsampleRgb565LinePair, VP8YuvToRgb565, 2) 103 104#undef LOAD_UV 105#undef UPSAMPLE_FUNC 106 107#endif // FANCY_UPSAMPLING 108 109//------------------------------------------------------------------------------ 110// simple point-sampling 111 112#define SAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \ 113static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \ 114 const uint8_t* u, const uint8_t* v, \ 115 uint8_t* top_dst, uint8_t* bottom_dst, int len) { \ 116 int i; \ 117 for (i = 0; i < len - 1; i += 2) { \ 118 FUNC(top_y[0], u[0], v[0], top_dst); \ 119 FUNC(top_y[1], u[0], v[0], top_dst + XSTEP); \ 120 FUNC(bottom_y[0], u[0], v[0], bottom_dst); \ 121 FUNC(bottom_y[1], u[0], v[0], bottom_dst + XSTEP); \ 122 top_y += 2; \ 123 bottom_y += 2; \ 124 u++; \ 125 v++; \ 126 top_dst += 2 * XSTEP; \ 127 bottom_dst += 2 * XSTEP; \ 128 } \ 129 if (i == len - 1) { /* last one */ \ 130 FUNC(top_y[0], u[0], v[0], top_dst); \ 131 FUNC(bottom_y[0], u[0], v[0], bottom_dst); \ 132 } \ 133} 134 135// All variants implemented. 136SAMPLE_FUNC(SampleRgbLinePair, VP8YuvToRgb, 3) 137SAMPLE_FUNC(SampleBgrLinePair, VP8YuvToBgr, 3) 138SAMPLE_FUNC(SampleRgbaLinePair, VP8YuvToRgba, 4) 139SAMPLE_FUNC(SampleBgraLinePair, VP8YuvToBgra, 4) 140SAMPLE_FUNC(SampleArgbLinePair, VP8YuvToArgb, 4) 141SAMPLE_FUNC(SampleRgba4444LinePair, VP8YuvToRgba4444, 2) 142SAMPLE_FUNC(SampleRgb565LinePair, VP8YuvToRgb565, 2) 143 144#undef SAMPLE_FUNC 145 146const WebPSampleLinePairFunc WebPSamplers[MODE_LAST] = { 147 SampleRgbLinePair, // MODE_RGB 148 SampleRgbaLinePair, // MODE_RGBA 149 SampleBgrLinePair, // MODE_BGR 150 SampleBgraLinePair, // MODE_BGRA 151 SampleArgbLinePair, // MODE_ARGB 152 SampleRgba4444LinePair, // MODE_RGBA_4444 153 SampleRgb565LinePair, // MODE_RGB_565 154 SampleRgbaLinePair, // MODE_rgbA 155 SampleBgraLinePair, // MODE_bgrA 156 SampleArgbLinePair, // MODE_Argb 157 SampleRgba4444LinePair // MODE_rgbA_4444 158}; 159 160//------------------------------------------------------------------------------ 161 162#if !defined(FANCY_UPSAMPLING) 163#define DUAL_SAMPLE_FUNC(FUNC_NAME, FUNC) \ 164static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bot_y, \ 165 const uint8_t* top_u, const uint8_t* top_v, \ 166 const uint8_t* bot_u, const uint8_t* bot_v, \ 167 uint8_t* top_dst, uint8_t* bot_dst, int len) { \ 168 const int half_len = len >> 1; \ 169 int x; \ 170 assert(top_dst != NULL); \ 171 { \ 172 for (x = 0; x < half_len; ++x) { \ 173 FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x + 0); \ 174 FUNC(top_y[2 * x + 1], top_u[x], top_v[x], top_dst + 8 * x + 4); \ 175 } \ 176 if (len & 1) FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x); \ 177 } \ 178 if (bot_dst != NULL) { \ 179 for (x = 0; x < half_len; ++x) { \ 180 FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x + 0); \ 181 FUNC(bot_y[2 * x + 1], bot_u[x], bot_v[x], bot_dst + 8 * x + 4); \ 182 } \ 183 if (len & 1) FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x); \ 184 } \ 185} 186 187DUAL_SAMPLE_FUNC(DualLineSamplerBGRA, VP8YuvToBgra) 188DUAL_SAMPLE_FUNC(DualLineSamplerARGB, VP8YuvToArgb) 189#undef DUAL_SAMPLE_FUNC 190 191#endif // !FANCY_UPSAMPLING 192 193WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last) { 194 WebPInitUpsamplers(); 195 VP8YUVInit(); 196#ifdef FANCY_UPSAMPLING 197 return WebPUpsamplers[alpha_is_last ? MODE_BGRA : MODE_ARGB]; 198#else 199 return (alpha_is_last ? DualLineSamplerBGRA : DualLineSamplerARGB); 200#endif 201} 202 203//------------------------------------------------------------------------------ 204// YUV444 converter 205 206#define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP) \ 207static void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \ 208 uint8_t* dst, int len) { \ 209 int i; \ 210 for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * XSTEP]); \ 211} 212 213YUV444_FUNC(Yuv444ToRgb, VP8YuvToRgb, 3) 214YUV444_FUNC(Yuv444ToBgr, VP8YuvToBgr, 3) 215YUV444_FUNC(Yuv444ToRgba, VP8YuvToRgba, 4) 216YUV444_FUNC(Yuv444ToBgra, VP8YuvToBgra, 4) 217YUV444_FUNC(Yuv444ToArgb, VP8YuvToArgb, 4) 218YUV444_FUNC(Yuv444ToRgba4444, VP8YuvToRgba4444, 2) 219YUV444_FUNC(Yuv444ToRgb565, VP8YuvToRgb565, 2) 220 221#undef YUV444_FUNC 222 223const WebPYUV444Converter WebPYUV444Converters[MODE_LAST] = { 224 Yuv444ToRgb, // MODE_RGB 225 Yuv444ToRgba, // MODE_RGBA 226 Yuv444ToBgr, // MODE_BGR 227 Yuv444ToBgra, // MODE_BGRA 228 Yuv444ToArgb, // MODE_ARGB 229 Yuv444ToRgba4444, // MODE_RGBA_4444 230 Yuv444ToRgb565, // MODE_RGB_565 231 Yuv444ToRgba, // MODE_rgbA 232 Yuv444ToBgra, // MODE_bgrA 233 Yuv444ToArgb, // MODE_Argb 234 Yuv444ToRgba4444 // MODE_rgbA_4444 235}; 236 237//------------------------------------------------------------------------------ 238// Premultiplied modes 239 240// non dithered-modes 241 242// (x * a * 32897) >> 23 is bit-wise equivalent to (int)(x * a / 255.) 243// for all 8bit x or a. For bit-wise equivalence to (int)(x * a / 255. + .5), 244// one can use instead: (x * a * 65793 + (1 << 23)) >> 24 245#if 1 // (int)(x * a / 255.) 246#define MULTIPLIER(a) ((a) * 32897UL) 247#define PREMULTIPLY(x, m) (((x) * (m)) >> 23) 248#else // (int)(x * a / 255. + .5) 249#define MULTIPLIER(a) ((a) * 65793UL) 250#define PREMULTIPLY(x, m) (((x) * (m) + (1UL << 23)) >> 24) 251#endif 252 253static void ApplyAlphaMultiply(uint8_t* rgba, int alpha_first, 254 int w, int h, int stride) { 255 while (h-- > 0) { 256 uint8_t* const rgb = rgba + (alpha_first ? 1 : 0); 257 const uint8_t* const alpha = rgba + (alpha_first ? 0 : 3); 258 int i; 259 for (i = 0; i < w; ++i) { 260 const uint32_t a = alpha[4 * i]; 261 if (a != 0xff) { 262 const uint32_t mult = MULTIPLIER(a); 263 rgb[4 * i + 0] = PREMULTIPLY(rgb[4 * i + 0], mult); 264 rgb[4 * i + 1] = PREMULTIPLY(rgb[4 * i + 1], mult); 265 rgb[4 * i + 2] = PREMULTIPLY(rgb[4 * i + 2], mult); 266 } 267 } 268 rgba += stride; 269 } 270} 271#undef MULTIPLIER 272#undef PREMULTIPLY 273 274// rgbA4444 275 276#define MULTIPLIER(a) ((a) * 0x1111) // 0x1111 ~= (1 << 16) / 15 277 278static WEBP_INLINE uint8_t dither_hi(uint8_t x) { 279 return (x & 0xf0) | (x >> 4); 280} 281 282static WEBP_INLINE uint8_t dither_lo(uint8_t x) { 283 return (x & 0x0f) | (x << 4); 284} 285 286static WEBP_INLINE uint8_t multiply(uint8_t x, uint32_t m) { 287 return (x * m) >> 16; 288} 289 290static WEBP_INLINE void ApplyAlphaMultiply4444(uint8_t* rgba4444, 291 int w, int h, int stride, 292 int rg_byte_pos /* 0 or 1 */) { 293 while (h-- > 0) { 294 int i; 295 for (i = 0; i < w; ++i) { 296 const uint8_t rg = rgba4444[2 * i + rg_byte_pos]; 297 const uint8_t ba = rgba4444[2 * i + (rg_byte_pos ^ 1)]; 298 const uint8_t a = ba & 0x0f; 299 const uint32_t mult = MULTIPLIER(a); 300 const uint8_t r = multiply(dither_hi(rg), mult); 301 const uint8_t g = multiply(dither_lo(rg), mult); 302 const uint8_t b = multiply(dither_hi(ba), mult); 303 rgba4444[2 * i + rg_byte_pos] = (r & 0xf0) | ((g >> 4) & 0x0f); 304 rgba4444[2 * i + (rg_byte_pos ^ 1)] = (b & 0xf0) | a; 305 } 306 rgba4444 += stride; 307 } 308} 309#undef MULTIPLIER 310 311static void ApplyAlphaMultiply_16b(uint8_t* rgba4444, 312 int w, int h, int stride) { 313#ifdef WEBP_SWAP_16BIT_CSP 314 ApplyAlphaMultiply4444(rgba4444, w, h, stride, 1); 315#else 316 ApplyAlphaMultiply4444(rgba4444, w, h, stride, 0); 317#endif 318} 319 320void (*WebPApplyAlphaMultiply)(uint8_t*, int, int, int, int) 321 = ApplyAlphaMultiply; 322void (*WebPApplyAlphaMultiply4444)(uint8_t*, int, int, int) 323 = ApplyAlphaMultiply_16b; 324 325//------------------------------------------------------------------------------ 326// Main call 327 328void WebPInitUpsamplers(void) { 329#ifdef FANCY_UPSAMPLING 330 WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair; 331 WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair; 332 WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair; 333 WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair; 334 WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair; 335 WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair; 336 WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair; 337 338 // If defined, use CPUInfo() to overwrite some pointers with faster versions. 339 if (VP8GetCPUInfo != NULL) { 340#if defined(WEBP_USE_SSE2) 341 if (VP8GetCPUInfo(kSSE2)) { 342 WebPInitUpsamplersSSE2(); 343 } 344#endif 345#if defined(WEBP_USE_NEON) 346 if (VP8GetCPUInfo(kNEON)) { 347 WebPInitUpsamplersNEON(); 348 } 349#endif 350 } 351#endif // FANCY_UPSAMPLING 352} 353 354void WebPInitPremultiply(void) { 355 WebPApplyAlphaMultiply = ApplyAlphaMultiply; 356 WebPApplyAlphaMultiply4444 = ApplyAlphaMultiply_16b; 357 358#ifdef FANCY_UPSAMPLING 359 WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair; 360 WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair; 361 WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair; 362 WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair; 363 364 if (VP8GetCPUInfo != NULL) { 365#if defined(WEBP_USE_SSE2) 366 if (VP8GetCPUInfo(kSSE2)) { 367 WebPInitPremultiplySSE2(); 368 } 369#endif 370#if defined(WEBP_USE_NEON) 371 if (VP8GetCPUInfo(kNEON)) { 372 WebPInitPremultiplyNEON(); 373 } 374#endif 375 } 376#endif // FANCY_UPSAMPLING 377} 378 379