yuv.h revision b6dbce6bfeaabde2a7b581c4c6888d532d32f3ac
1// Copyright 2010 Google Inc. All Rights Reserved.
2//
3// This code is licensed under the same terms as WebM:
4//  Software License Agreement:  http://www.webmproject.org/license/software/
5//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// inline YUV<->RGB conversion function
9//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#ifndef WEBP_DSP_YUV_H_
13#define WEBP_DSP_YUV_H_
14
15#include "../dec/decode_vp8.h"
16
17/*
18 * Define ANDROID_WEBP_RGB to enable specific optimizations for Android
19 * RGBA_4444 & RGB_565 color support.
20 *
21 */
22
23#define ANDROID_WEBP_RGB
24
25//------------------------------------------------------------------------------
26// YUV -> RGB conversion
27
28#if defined(__cplusplus) || defined(c_plusplus)
29extern "C" {
30#endif
31
32enum { YUV_FIX = 16,                // fixed-point precision
33       YUV_RANGE_MIN = -227,        // min value of r/g/b output
34       YUV_RANGE_MAX = 256 + 226    // max value of r/g/b output
35};
36extern int16_t VP8kVToR[256], VP8kUToB[256];
37extern int32_t VP8kVToG[256], VP8kUToG[256];
38extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
39extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
40
41static WEBP_INLINE void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v,
42                                    uint8_t* const rgb) {
43  const int r_off = VP8kVToR[v];
44  const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
45  const int b_off = VP8kUToB[u];
46  rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN];
47  rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
48  rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN];
49}
50
51static WEBP_INLINE void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v,
52                                       uint8_t* const rgb) {
53  const int r_off = VP8kVToR[v];
54  const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
55  const int b_off = VP8kUToB[u];
56#ifdef ANDROID_WEBP_RGB
57  rgb[1] = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) |
58            (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5));
59  rgb[0] = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) |
60            (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3));
61#else
62  rgb[0] = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) |
63            (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5));
64  rgb[1] = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) |
65            (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3));
66#endif
67}
68
69static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
70                                     uint8_t* const argb) {
71  argb[0] = 0xff;
72  VP8YuvToRgb(y, u, v, argb + 1);
73}
74
75static WEBP_INLINE void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v,
76                                         uint8_t* const argb) {
77  const int r_off = VP8kVToR[v];
78  const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
79  const int b_off = VP8kUToB[u];
80#ifdef ANDROID_WEBP_RGB
81  argb[1] = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) |
82             VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]);
83  argb[0] = 0x0f | (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4);
84#else
85  argb[0] = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) |
86             VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]);
87  argb[1] = 0x0f | (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4);
88#endif
89}
90
91static WEBP_INLINE void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v,
92                                    uint8_t* const bgr) {
93  const int r_off = VP8kVToR[v];
94  const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
95  const int b_off = VP8kUToB[u];
96  bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN];
97  bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
98  bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN];
99}
100
101static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
102                                     uint8_t* const bgra) {
103  VP8YuvToBgr(y, u, v, bgra);
104  bgra[3] = 0xff;
105}
106
107static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
108                                     uint8_t* const rgba) {
109  VP8YuvToRgb(y, u, v, rgba);
110  rgba[3] = 0xff;
111}
112
113// Must be called before everything, to initialize the tables.
114void VP8YUVInit(void);
115
116//------------------------------------------------------------------------------
117// RGB -> YUV conversion
118// The exact naming is Y'CbCr, following the ITU-R BT.601 standard.
119// More information at: http://en.wikipedia.org/wiki/YCbCr
120// Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16
121// U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128
122// V = 0.4394 * R - 0.3679 * G - 0.0715 * B + 128
123// We use 16bit fixed point operations.
124
125static WEBP_INLINE int VP8ClipUV(int v) {
126   v = (v + (257 << (YUV_FIX + 2 - 1))) >> (YUV_FIX + 2);
127   return ((v & ~0xff) == 0) ? v : (v < 0) ? 0 : 255;
128}
129
130static WEBP_INLINE int VP8RGBToY(int r, int g, int b) {
131  const int kRound = (1 << (YUV_FIX - 1)) + (16 << YUV_FIX);
132  const int luma = 16839 * r + 33059 * g + 6420 * b;
133  return (luma + kRound) >> YUV_FIX;  // no need to clip
134}
135
136static WEBP_INLINE int VP8RGBToU(int r, int g, int b) {
137  return VP8ClipUV(-9719 * r - 19081 * g + 28800 * b);
138}
139
140static WEBP_INLINE int VP8RGBToV(int r, int g, int b) {
141  return VP8ClipUV(+28800 * r - 24116 * g - 4684 * b);
142}
143
144#if defined(__cplusplus) || defined(c_plusplus)
145}    // extern "C"
146#endif
147
148#endif  /* WEBP_DSP_YUV_H_ */
149