• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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(s): Branimir Vasic (branimir.vasic@imgtec.com)
13 //            Djordje Pesut  (djordje.pesut@imgtec.com)
14 
15 #include "./dsp.h"
16 
17 // Code is disabled for now, in favor of the plain-C version
18 // TODO(djordje.pesut): adapt the code to reflect the C-version.
19 #if 0 // defined(WEBP_USE_MIPS_DSP_R2)
20 
21 #include <assert.h>
22 #include "./yuv.h"
23 
24 #if !defined(WEBP_YUV_USE_TABLE)
25 
26 #define YUV_TO_RGB(Y, U, V, R, G, B) do {                                      \
27     const int t1 = kYScale * Y;                                                \
28     const int t2 = kVToG * V;                                                  \
29     R = kVToR * V;                                                             \
30     G = kUToG * U;                                                             \
31     B = kUToB * U;                                                             \
32     R = t1 + R;                                                                \
33     G = t1 - G;                                                                \
34     B = t1 + B;                                                                \
35     R = R + kRCst;                                                             \
36     G = G - t2 + kGCst;                                                        \
37     B = B + kBCst;                                                             \
38     __asm__ volatile (                                                         \
39       "shll_s.w         %[" #R "],      %[" #R "],        9          \n\t"     \
40       "shll_s.w         %[" #G "],      %[" #G "],        9          \n\t"     \
41       "shll_s.w         %[" #B "],      %[" #B "],        9          \n\t"     \
42       "precrqu_s.qb.ph  %[" #R "],      %[" #R "],        $zero      \n\t"     \
43       "precrqu_s.qb.ph  %[" #G "],      %[" #G "],        $zero      \n\t"     \
44       "precrqu_s.qb.ph  %[" #B "],      %[" #B "],        $zero      \n\t"     \
45       "srl              %[" #R "],      %[" #R "],        24         \n\t"     \
46       "srl              %[" #G "],      %[" #G "],        24         \n\t"     \
47       "srl              %[" #B "],      %[" #B "],        24         \n\t"     \
48       : [R]"+r"(R), [G]"+r"(G), [B]"+r"(B)                                     \
49       :                                                                        \
50     );                                                                         \
51   } while (0)
52 
53 static WEBP_INLINE void YuvToRgb(int y, int u, int v, uint8_t* const rgb) {
54   int r, g, b;
55   YUV_TO_RGB(y, u, v, r, g, b);
56   rgb[0] = r;
57   rgb[1] = g;
58   rgb[2] = b;
59 }
60 static WEBP_INLINE void YuvToBgr(int y, int u, int v, uint8_t* const bgr) {
61   int r, g, b;
62   YUV_TO_RGB(y, u, v, r, g, b);
63   bgr[0] = b;
64   bgr[1] = g;
65   bgr[2] = r;
66 }
67 static WEBP_INLINE void YuvToRgb565(int y, int u, int v, uint8_t* const rgb) {
68   int r, g, b;
69   YUV_TO_RGB(y, u, v, r, g, b);
70   {
71     const int rg = (r & 0xf8) | (g >> 5);
72     const int gb = ((g << 3) & 0xe0) | (b >> 3);
73 #ifdef WEBP_SWAP_16BIT_CSP
74     rgb[0] = gb;
75     rgb[1] = rg;
76 #else
77     rgb[0] = rg;
78     rgb[1] = gb;
79 #endif
80   }
81 }
82 static WEBP_INLINE void YuvToRgba4444(int y, int u, int v,
83                                       uint8_t* const argb) {
84   int r, g, b;
85   YUV_TO_RGB(y, u, v, r, g, b);
86   {
87     const int rg = (r & 0xf0) | (g >> 4);
88     const int ba = (b & 0xf0) | 0x0f;     // overwrite the lower 4 bits
89 #ifdef WEBP_SWAP_16BIT_CSP
90     argb[0] = ba;
91     argb[1] = rg;
92 #else
93     argb[0] = rg;
94     argb[1] = ba;
95 #endif
96    }
97 }
98 #endif  // WEBP_YUV_USE_TABLE
99 
100 //-----------------------------------------------------------------------------
101 // Alpha handling variants
102 
103 static WEBP_INLINE void YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
104                                   uint8_t* const argb) {
105   int r, g, b;
106   YUV_TO_RGB(y, u, v, r, g, b);
107   argb[0] = 0xff;
108   argb[1] = r;
109   argb[2] = g;
110   argb[3] = b;
111 }
112 static WEBP_INLINE void YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
113                                   uint8_t* const bgra) {
114   int r, g, b;
115   YUV_TO_RGB(y, u, v, r, g, b);
116   bgra[0] = b;
117   bgra[1] = g;
118   bgra[2] = r;
119   bgra[3] = 0xff;
120 }
121 static WEBP_INLINE void YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
122                                   uint8_t* const rgba) {
123   int r, g, b;
124   YUV_TO_RGB(y, u, v, r, g, b);
125   rgba[0] = r;
126   rgba[1] = g;
127   rgba[2] = b;
128   rgba[3] = 0xff;
129 }
130 
131 //------------------------------------------------------------------------------
132 // Fancy upsampler
133 
134 #ifdef FANCY_UPSAMPLING
135 
136 // Given samples laid out in a square as:
137 //  [a b]
138 //  [c d]
139 // we interpolate u/v as:
140 //  ([9*a + 3*b + 3*c +   d    3*a + 9*b + 3*c +   d] + [8 8]) / 16
141 //  ([3*a +   b + 9*c + 3*d      a + 3*b + 3*c + 9*d]   [8 8]) / 16
142 
143 // We process u and v together stashed into 32bit (16bit each).
144 #define LOAD_UV(u, v) ((u) | ((v) << 16))
145 
146 #define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP)                                  \
147 static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y,           \
148                       const uint8_t* top_u, const uint8_t* top_v,              \
149                       const uint8_t* cur_u, const uint8_t* cur_v,              \
150                       uint8_t* top_dst, uint8_t* bottom_dst, int len) {        \
151   int x;                                                                       \
152   const int last_pixel_pair = (len - 1) >> 1;                                  \
153   uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]);   /* top-left sample */        \
154   uint32_t l_uv  = LOAD_UV(cur_u[0], cur_v[0]);   /* left-sample */            \
155   assert(top_y != NULL);                                                       \
156   {                                                                            \
157     const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2;                \
158     FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst);                          \
159   }                                                                            \
160   if (bottom_y != NULL) {                                                      \
161     const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2;                \
162     FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst);                    \
163   }                                                                            \
164   for (x = 1; x <= last_pixel_pair; ++x) {                                     \
165     const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]);  /* top sample */       \
166     const uint32_t uv   = LOAD_UV(cur_u[x], cur_v[x]);  /* sample */           \
167     /* precompute invariant values associated with first and second diagonals*/\
168     const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u;               \
169     const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3;                   \
170     const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3;                    \
171     {                                                                          \
172       const uint32_t uv0 = (diag_12 + tl_uv) >> 1;                             \
173       const uint32_t uv1 = (diag_03 + t_uv) >> 1;                              \
174       FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16),                          \
175            top_dst + (2 * x - 1) * XSTEP);                                     \
176       FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16),                          \
177            top_dst + (2 * x - 0) * XSTEP);                                     \
178     }                                                                          \
179     if (bottom_y != NULL) {                                                    \
180       const uint32_t uv0 = (diag_03 + l_uv) >> 1;                              \
181       const uint32_t uv1 = (diag_12 + uv) >> 1;                                \
182       FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16),                       \
183            bottom_dst + (2 * x - 1) * XSTEP);                                  \
184       FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16),                       \
185            bottom_dst + (2 * x + 0) * XSTEP);                                  \
186     }                                                                          \
187     tl_uv = t_uv;                                                              \
188     l_uv = uv;                                                                 \
189   }                                                                            \
190   if (!(len & 1)) {                                                            \
191     {                                                                          \
192       const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2;              \
193       FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16),                            \
194            top_dst + (len - 1) * XSTEP);                                       \
195     }                                                                          \
196     if (bottom_y != NULL) {                                                    \
197       const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2;              \
198       FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16),                         \
199            bottom_dst + (len - 1) * XSTEP);                                    \
200     }                                                                          \
201   }                                                                            \
202 }
203 
204 // All variants implemented.
205 UPSAMPLE_FUNC(UpsampleRgbLinePair,      YuvToRgb,      3)
206 UPSAMPLE_FUNC(UpsampleBgrLinePair,      YuvToBgr,      3)
207 UPSAMPLE_FUNC(UpsampleRgbaLinePair,     YuvToRgba,     4)
208 UPSAMPLE_FUNC(UpsampleBgraLinePair,     YuvToBgra,     4)
209 UPSAMPLE_FUNC(UpsampleArgbLinePair,     YuvToArgb,     4)
210 UPSAMPLE_FUNC(UpsampleRgba4444LinePair, YuvToRgba4444, 2)
211 UPSAMPLE_FUNC(UpsampleRgb565LinePair,   YuvToRgb565,   2)
212 
213 #undef LOAD_UV
214 #undef UPSAMPLE_FUNC
215 
216 //------------------------------------------------------------------------------
217 // Entry point
218 
219 extern void WebPInitUpsamplersMIPSdspR2(void);
220 
221 WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersMIPSdspR2(void) {
222   WebPUpsamplers[MODE_RGB]       = UpsampleRgbLinePair;
223   WebPUpsamplers[MODE_RGBA]      = UpsampleRgbaLinePair;
224   WebPUpsamplers[MODE_BGR]       = UpsampleBgrLinePair;
225   WebPUpsamplers[MODE_BGRA]      = UpsampleBgraLinePair;
226   WebPUpsamplers[MODE_ARGB]      = UpsampleArgbLinePair;
227   WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair;
228   WebPUpsamplers[MODE_RGB_565]   = UpsampleRgb565LinePair;
229   WebPUpsamplers[MODE_rgbA]      = UpsampleRgbaLinePair;
230   WebPUpsamplers[MODE_bgrA]      = UpsampleBgraLinePair;
231   WebPUpsamplers[MODE_Argb]      = UpsampleArgbLinePair;
232   WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair;
233 }
234 
235 #endif  // FANCY_UPSAMPLING
236 
237 //------------------------------------------------------------------------------
238 // YUV444 converter
239 
240 #define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP)                                    \
241 static void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v,    \
242                       uint8_t* dst, int len) {                                 \
243   int i;                                                                       \
244   for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * XSTEP]);           \
245 }
246 
247 YUV444_FUNC(Yuv444ToRgb,      YuvToRgb,      3)
248 YUV444_FUNC(Yuv444ToBgr,      YuvToBgr,      3)
249 YUV444_FUNC(Yuv444ToRgba,     YuvToRgba,     4)
250 YUV444_FUNC(Yuv444ToBgra,     YuvToBgra,     4)
251 YUV444_FUNC(Yuv444ToArgb,     YuvToArgb,     4)
252 YUV444_FUNC(Yuv444ToRgba4444, YuvToRgba4444, 2)
253 YUV444_FUNC(Yuv444ToRgb565,   YuvToRgb565,   2)
254 
255 #undef YUV444_FUNC
256 
257 //------------------------------------------------------------------------------
258 // Entry point
259 
260 extern void WebPInitYUV444ConvertersMIPSdspR2(void);
261 
262 WEBP_TSAN_IGNORE_FUNCTION void WebPInitYUV444ConvertersMIPSdspR2(void) {
263   WebPYUV444Converters[MODE_RGB]       = Yuv444ToRgb;
264   WebPYUV444Converters[MODE_RGBA]      = Yuv444ToRgba;
265   WebPYUV444Converters[MODE_BGR]       = Yuv444ToBgr;
266   WebPYUV444Converters[MODE_BGRA]      = Yuv444ToBgra;
267   WebPYUV444Converters[MODE_ARGB]      = Yuv444ToArgb;
268   WebPYUV444Converters[MODE_RGBA_4444] = Yuv444ToRgba4444;
269   WebPYUV444Converters[MODE_RGB_565]   = Yuv444ToRgb565;
270   WebPYUV444Converters[MODE_rgbA]      = Yuv444ToRgba;
271   WebPYUV444Converters[MODE_bgrA]      = Yuv444ToBgra;
272   WebPYUV444Converters[MODE_Argb]      = Yuv444ToArgb;
273   WebPYUV444Converters[MODE_rgbA_4444] = Yuv444ToRgba4444;
274 }
275 
276 #else  // !WEBP_USE_MIPS_DSP_R2
277 
278 WEBP_DSP_INIT_STUB(WebPInitYUV444ConvertersMIPSdspR2)
279 
280 #endif  // WEBP_USE_MIPS_DSP_R2
281 
282 #if 1  // !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_MIPS_DSP_R2))
283 WEBP_DSP_INIT_STUB(WebPInitUpsamplersMIPSdspR2)
284 #endif
285