2 * Alpha optimized DSP utils
3 * Copyright (c) 2002 Falk Hueffner <falk@debian.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/attributes.h"
23 #include "libavcodec/dsputil.h"
24 #include "dsputil_alpha.h"
27 void (*put_pixels_clamped_axp_p)(const int16_t *block, uint8_t *pixels,
29 void (*add_pixels_clamped_axp_p)(const int16_t *block, uint8_t *pixels,
33 /* These functions were the base for the optimized assembler routines,
34 and remain here for documentation purposes. */
35 static void put_pixels_clamped_mvi(const int16_t *block, uint8_t *pixels,
39 uint64_t clampmask = zap(-1, 0xaa); /* 0x00ff00ff00ff00ff */
42 uint64_t shorts0, shorts1;
45 shorts0 = maxsw4(shorts0, 0);
46 shorts0 = minsw4(shorts0, clampmask);
47 stl(pkwb(shorts0), pixels);
49 shorts1 = ldq(block + 4);
50 shorts1 = maxsw4(shorts1, 0);
51 shorts1 = minsw4(shorts1, clampmask);
52 stl(pkwb(shorts1), pixels + 4);
59 void add_pixels_clamped_mvi(const int16_t *block, uint8_t *pixels,
63 /* Keep this function a leaf function by generating the constants
64 manually (mainly for the hack value ;-). */
65 uint64_t clampmask = zap(-1, 0xaa); /* 0x00ff00ff00ff00ff */
66 uint64_t signmask = zap(-1, 0x33);
67 signmask ^= signmask >> 1; /* 0x8000800080008000 */
70 uint64_t shorts0, pix0, signs0;
71 uint64_t shorts1, pix1, signs1;
74 shorts1 = ldq(block + 4);
76 pix0 = unpkbw(ldl(pixels));
77 /* Signed subword add (MMX paddw). */
78 signs0 = shorts0 & signmask;
83 shorts0 = maxsw4(shorts0, 0);
84 shorts0 = minsw4(shorts0, clampmask);
87 pix1 = unpkbw(ldl(pixels + 4));
88 signs1 = shorts1 & signmask;
92 shorts1 = maxsw4(shorts1, 0);
93 shorts1 = minsw4(shorts1, clampmask);
95 stl(pkwb(shorts0), pixels);
96 stl(pkwb(shorts1), pixels + 4);
104 static void clear_blocks_axp(int16_t *blocks) {
105 uint64_t *p = (uint64_t *) blocks;
106 int n = sizeof(int16_t) * 6 * 64;
122 static inline uint64_t avg2_no_rnd(uint64_t a, uint64_t b)
124 return (a & b) + (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
127 static inline uint64_t avg2(uint64_t a, uint64_t b)
129 return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
133 /* The XY2 routines basically utilize this scheme, but reuse parts in
135 static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
137 uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
138 + ((l2 & ~BYTE_VEC(0x03)) >> 2)
139 + ((l3 & ~BYTE_VEC(0x03)) >> 2)
140 + ((l4 & ~BYTE_VEC(0x03)) >> 2);
141 uint64_t r2 = (( (l1 & BYTE_VEC(0x03))
142 + (l2 & BYTE_VEC(0x03))
143 + (l3 & BYTE_VEC(0x03))
144 + (l4 & BYTE_VEC(0x03))
145 + BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
150 #define OP(LOAD, STORE) \
152 STORE(LOAD(pixels), block); \
153 pixels += line_size; \
154 block += line_size; \
157 #define OP_X2(LOAD, STORE) \
159 uint64_t pix1, pix2; \
161 pix1 = LOAD(pixels); \
162 pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56); \
163 STORE(AVG2(pix1, pix2), block); \
164 pixels += line_size; \
165 block += line_size; \
168 #define OP_Y2(LOAD, STORE) \
170 uint64_t pix = LOAD(pixels); \
174 pixels += line_size; \
175 next_pix = LOAD(pixels); \
176 STORE(AVG2(pix, next_pix), block); \
177 block += line_size; \
182 #define OP_XY2(LOAD, STORE) \
184 uint64_t pix1 = LOAD(pixels); \
185 uint64_t pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56); \
186 uint64_t pix_l = (pix1 & BYTE_VEC(0x03)) \
187 + (pix2 & BYTE_VEC(0x03)); \
188 uint64_t pix_h = ((pix1 & ~BYTE_VEC(0x03)) >> 2) \
189 + ((pix2 & ~BYTE_VEC(0x03)) >> 2); \
192 uint64_t npix1, npix2; \
193 uint64_t npix_l, npix_h; \
196 pixels += line_size; \
197 npix1 = LOAD(pixels); \
198 npix2 = npix1 >> 8 | ((uint64_t) pixels[8] << 56); \
199 npix_l = (npix1 & BYTE_VEC(0x03)) \
200 + (npix2 & BYTE_VEC(0x03)); \
201 npix_h = ((npix1 & ~BYTE_VEC(0x03)) >> 2) \
202 + ((npix2 & ~BYTE_VEC(0x03)) >> 2); \
203 avg = (((pix_l + npix_l + AVG4_ROUNDER) >> 2) & BYTE_VEC(0x03)) \
207 block += line_size; \
213 #define MAKE_OP(OPNAME, SUFF, OPKIND, STORE) \
214 static void OPNAME ## _pixels ## SUFF ## _axp \
215 (uint8_t *restrict block, const uint8_t *restrict pixels, \
216 ptrdiff_t line_size, int h) \
218 if ((size_t) pixels & 0x7) { \
219 OPKIND(uldq, STORE); \
221 OPKIND(ldq, STORE); \
225 static void OPNAME ## _pixels16 ## SUFF ## _axp \
226 (uint8_t *restrict block, const uint8_t *restrict pixels, \
227 ptrdiff_t line_size, int h) \
229 OPNAME ## _pixels ## SUFF ## _axp(block, pixels, line_size, h); \
230 OPNAME ## _pixels ## SUFF ## _axp(block + 8, pixels + 8, line_size, h); \
233 #define PIXOP(OPNAME, STORE) \
234 MAKE_OP(OPNAME, , OP, STORE) \
235 MAKE_OP(OPNAME, _x2, OP_X2, STORE) \
236 MAKE_OP(OPNAME, _y2, OP_Y2, STORE) \
237 MAKE_OP(OPNAME, _xy2, OP_XY2, STORE)
239 /* Rounding primitives. */
242 #define AVG4_ROUNDER BYTE_VEC(0x02)
243 #define STORE(l, b) stq(l, b)
247 #define STORE(l, b) stq(AVG2(l, ldq(b)), b);
250 /* Not rounding primitives. */
255 #define AVG2 avg2_no_rnd
256 #define AVG4 avg4_no_rnd
257 #define AVG4_ROUNDER BYTE_VEC(0x01)
258 #define STORE(l, b) stq(l, b)
259 PIXOP(put_no_rnd, STORE);
262 #define STORE(l, b) stq(AVG2(l, ldq(b)), b);
263 PIXOP(avg_no_rnd, STORE);
265 static void put_pixels16_axp_asm(uint8_t *block, const uint8_t *pixels,
266 ptrdiff_t line_size, int h)
268 put_pixels_axp_asm(block, pixels, line_size, h);
269 put_pixels_axp_asm(block + 8, pixels + 8, line_size, h);
272 av_cold void ff_dsputil_init_alpha(DSPContext *c, AVCodecContext *avctx)
274 const int high_bit_depth = avctx->bits_per_raw_sample > 8;
276 if (!high_bit_depth) {
277 c->put_pixels_tab[0][0] = put_pixels16_axp_asm;
278 c->put_pixels_tab[0][1] = put_pixels16_x2_axp;
279 c->put_pixels_tab[0][2] = put_pixels16_y2_axp;
280 c->put_pixels_tab[0][3] = put_pixels16_xy2_axp;
282 c->put_no_rnd_pixels_tab[0][0] = put_pixels16_axp_asm;
283 c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_axp;
284 c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_axp;
285 c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_axp;
287 c->avg_pixels_tab[0][0] = avg_pixels16_axp;
288 c->avg_pixels_tab[0][1] = avg_pixels16_x2_axp;
289 c->avg_pixels_tab[0][2] = avg_pixels16_y2_axp;
290 c->avg_pixels_tab[0][3] = avg_pixels16_xy2_axp;
292 c->avg_no_rnd_pixels_tab[0] = avg_no_rnd_pixels16_axp;
293 c->avg_no_rnd_pixels_tab[1] = avg_no_rnd_pixels16_x2_axp;
294 c->avg_no_rnd_pixels_tab[2] = avg_no_rnd_pixels16_y2_axp;
295 c->avg_no_rnd_pixels_tab[3] = avg_no_rnd_pixels16_xy2_axp;
297 c->put_pixels_tab[1][0] = put_pixels_axp_asm;
298 c->put_pixels_tab[1][1] = put_pixels_x2_axp;
299 c->put_pixels_tab[1][2] = put_pixels_y2_axp;
300 c->put_pixels_tab[1][3] = put_pixels_xy2_axp;
302 c->put_no_rnd_pixels_tab[1][0] = put_pixels_axp_asm;
303 c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels_x2_axp;
304 c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels_y2_axp;
305 c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels_xy2_axp;
307 c->avg_pixels_tab[1][0] = avg_pixels_axp;
308 c->avg_pixels_tab[1][1] = avg_pixels_x2_axp;
309 c->avg_pixels_tab[1][2] = avg_pixels_y2_axp;
310 c->avg_pixels_tab[1][3] = avg_pixels_xy2_axp;
312 c->clear_blocks = clear_blocks_axp;
315 /* amask clears all bits that correspond to present features. */
316 if (amask(AMASK_MVI) == 0) {
317 c->put_pixels_clamped = put_pixels_clamped_mvi_asm;
318 c->add_pixels_clamped = add_pixels_clamped_mvi_asm;
321 c->get_pixels = get_pixels_mvi;
322 c->diff_pixels = diff_pixels_mvi;
323 c->sad[0] = pix_abs16x16_mvi_asm;
324 c->sad[1] = pix_abs8x8_mvi;
325 c->pix_abs[0][0] = pix_abs16x16_mvi_asm;
326 c->pix_abs[1][0] = pix_abs8x8_mvi;
327 c->pix_abs[0][1] = pix_abs16x16_x2_mvi;
328 c->pix_abs[0][2] = pix_abs16x16_y2_mvi;
329 c->pix_abs[0][3] = pix_abs16x16_xy2_mvi;
332 put_pixels_clamped_axp_p = c->put_pixels_clamped;
333 add_pixels_clamped_axp_p = c->add_pixels_clamped;
335 if (!avctx->lowres && avctx->bits_per_raw_sample <= 8 &&
336 (avctx->idct_algo == FF_IDCT_AUTO ||
337 avctx->idct_algo == FF_IDCT_SIMPLEALPHA)) {
338 c->idct_put = ff_simple_idct_put_axp;
339 c->idct_add = ff_simple_idct_add_axp;
340 c->idct = ff_simple_idct_axp;