aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/hpeldsp.c
blob: 1ec76e7a45cabae149ffd2ba9c360764a5d178c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Half-pel DSP functions.
 * Copyright (c) 2000, 2001 Fabrice Bellard
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
 *
 * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Half-pel DSP functions.
 */

#include "libavutil/attributes.h"
#include "libavutil/intreadwrite.h"
#include "hpeldsp.h"

#define BIT_DEPTH 8
#include "hpel_template.c"
#include "pel_template.c"

#define PIXOP2(OPNAME, OP)                                              \
static inline void OPNAME ## _no_rnd_pixels8_l2_8(uint8_t *dst,         \
                                                  const uint8_t *src1,  \
                                                  const uint8_t *src2,  \
                                                  int dst_stride,       \
                                                  int src_stride1,      \
                                                  int src_stride2,      \
                                                  int h)                \
{                                                                       \
    int i;                                                              \
                                                                        \
    for (i = 0; i < h; i++) {                                           \
        uint32_t a, b;                                                  \
        a = AV_RN32(&src1[i * src_stride1]);                            \
        b = AV_RN32(&src2[i * src_stride2]);                            \
        OP(*((uint32_t *) &dst[i * dst_stride]),                        \
           no_rnd_avg32(a, b));                                         \
        a = AV_RN32(&src1[i * src_stride1 + 4]);                        \
        b = AV_RN32(&src2[i * src_stride2 + 4]);                        \
        OP(*((uint32_t *) &dst[i * dst_stride + 4]),                    \
           no_rnd_avg32(a, b));                                         \
    }                                                                   \
}                                                                       \
                                                                        \
static inline void OPNAME ## _no_rnd_pixels8_x2_8_c(uint8_t *block,     \
                                                    const uint8_t *pixels, \
                                                    ptrdiff_t line_size, \
                                                    int h)              \
{                                                                       \
    OPNAME ## _no_rnd_pixels8_l2_8(block, pixels, pixels + 1,           \
                                   line_size, line_size, line_size, h); \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels8_x2_8_c(uint8_t *block,            \
                                             const uint8_t *pixels,     \
                                             ptrdiff_t line_size,       \
                                             int h)                     \
{                                                                       \
    OPNAME ## _pixels8_l2_8(block, pixels, pixels + 1,                  \
                            line_size, line_size, line_size, h);        \
}                                                                       \
                                                                        \
static inline void OPNAME ## _no_rnd_pixels8_y2_8_c(uint8_t *block,     \
                                                    const uint8_t *pixels, \
                                                    ptrdiff_t line_size, \
                                                    int h)              \
{                                                                       \
    OPNAME ## _no_rnd_pixels8_l2_8(block, pixels, pixels + line_size,   \
                                   line_size, line_size, line_size, h); \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels8_y2_8_c(uint8_t *block,            \
                                             const uint8_t *pixels,     \
                                             ptrdiff_t line_size,       \
                                             int h)                     \
{                                                                       \
    OPNAME ## _pixels8_l2_8(block, pixels, pixels + line_size,          \
                            line_size, line_size, line_size, h);        \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels4_x2_8_c(uint8_t *block,            \
                                             const uint8_t *pixels,     \
                                             ptrdiff_t line_size,       \
                                             int h)                     \
{                                                                       \
    OPNAME ## _pixels4_l2_8(block, pixels, pixels + 1,                  \
                            line_size, line_size, line_size, h);        \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels4_y2_8_c(uint8_t *block,            \
                                             const uint8_t *pixels,     \
                                             ptrdiff_t line_size,       \
                                             int h)                     \
{                                                                       \
    OPNAME ## _pixels4_l2_8(block, pixels, pixels + line_size,          \
                            line_size, line_size, line_size, h);        \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels2_x2_8_c(uint8_t *block,            \
                                             const uint8_t *pixels,     \
                                             ptrdiff_t line_size,       \
                                             int h)                     \
{                                                                       \
    OPNAME ## _pixels2_l2_8(block, pixels, pixels + 1,                  \
                            line_size, line_size, line_size, h);        \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels2_y2_8_c(uint8_t *block,            \
                                             const uint8_t *pixels,     \
                                             ptrdiff_t line_size,       \
                                             int h)                     \
{                                                                       \
    OPNAME ## _pixels2_l2_8(block, pixels, pixels + line_size,          \
                            line_size, line_size, line_size, h);        \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels2_xy2_8_c(uint8_t *block,           \
                                              const uint8_t *pixels,    \
                                              ptrdiff_t line_size,      \
                                              int h)                    \
{                                                                       \
    int i, a1, b1;                                                      \
    int a0 = pixels[0];                                                 \
    int b0 = pixels[1] + 2;                                             \
                                                                        \
    a0 += b0;                                                           \
    b0 += pixels[2];                                                    \
    pixels += line_size;                                                \
    for (i = 0; i < h; i += 2) {                                        \
        a1  = pixels[0];                                                \
        b1  = pixels[1];                                                \
        a1 += b1;                                                       \
        b1 += pixels[2];                                                \
                                                                        \
        block[0] = (a1 + a0) >> 2; /* FIXME non put */                  \
        block[1] = (b1 + b0) >> 2;                                      \
                                                                        \
        pixels += line_size;                                            \
        block  += line_size;                                            \
                                                                        \
        a0  = pixels[0];                                                \
        b0  = pixels[1] + 2;                                            \
        a0 += b0;                                                       \
        b0 += pixels[2];                                                \
                                                                        \
        block[0] = (a1 + a0) >> 2;                                      \
        block[1] = (b1 + b0) >> 2;                                      \
        pixels  += line_size;                                           \
        block   += line_size;                                           \
    }                                                                   \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels4_xy2_8_c(uint8_t *block,           \
                                              const uint8_t *pixels,    \
                                              ptrdiff_t line_size,      \
                                              int h)                    \
{                                                                       \
    /* FIXME HIGH BIT DEPTH */                                          \
    int i;                                                              \
    const uint32_t a = AV_RN32(pixels);                                 \
    const uint32_t b = AV_RN32(pixels + 1);                             \
    uint32_t l0 = (a & 0x03030303UL) +                                  \
                  (b & 0x03030303UL) +                                  \
                       0x02020202UL;                                    \
    uint32_t h0 = ((a & 0xFCFCFCFCUL) >> 2) +                           \
                  ((b & 0xFCFCFCFCUL) >> 2);                            \
    uint32_t l1, h1;                                                    \
                                                                        \
    pixels += line_size;                                                \
    for (i = 0; i < h; i += 2) {                                        \
        uint32_t a = AV_RN32(pixels);                                   \
        uint32_t b = AV_RN32(pixels + 1);                               \
        l1 = (a & 0x03030303UL) +                                       \
             (b & 0x03030303UL);                                        \
        h1 = ((a & 0xFCFCFCFCUL) >> 2) +                                \
             ((b & 0xFCFCFCFCUL) >> 2);                                 \
        OP(*((uint32_t *) block), h0 + h1 +                             \
           (((l0 + l1) >> 2) & 0x0F0F0F0FUL));                          \
        pixels += line_size;                                            \
        block  += line_size;                                            \
        a  = AV_RN32(pixels);                                           \
        b  = AV_RN32(pixels + 1);                                       \
        l0 = (a & 0x03030303UL) +                                       \
             (b & 0x03030303UL) +                                       \
                  0x02020202UL;                                         \
        h0 = ((a & 0xFCFCFCFCUL) >> 2) +                                \
             ((b & 0xFCFCFCFCUL) >> 2);                                 \
        OP(*((uint32_t *) block), h0 + h1 +                             \
           (((l0 + l1) >> 2) & 0x0F0F0F0FUL));                          \
        pixels += line_size;                                            \
        block  += line_size;                                            \
    }                                                                   \
}                                                                       \
                                                                        \
static inline void OPNAME ## _pixels8_xy2_8_c(uint8_t *block,           \
                                              const uint8_t *pixels,    \
                                              ptrdiff_t line_size,      \
                                              int h)                    \
{                                                                       \
    /* FIXME HIGH BIT DEPTH */                                          \
    int j;                                                              \
                                                                        \
    for (j = 0; j < 2; j++) {                                           \
        int i;                                                          \
        const uint32_t a = AV_RN32(pixels);                             \
        const uint32_t b = AV_RN32(pixels + 1);                         \
        uint32_t l0 = (a & 0x03030303UL) +                              \
                      (b & 0x03030303UL) +                              \
                           0x02020202UL;                                \
        uint32_t h0 = ((a & 0xFCFCFCFCUL) >> 2) +                       \
                      ((b & 0xFCFCFCFCUL) >> 2);                        \
        uint32_t l1, h1;                                                \
                                                                        \
        pixels += line_size;                                            \
        for (i = 0; i < h; i += 2) {                                    \
            uint32_t a = AV_RN32(pixels);                               \
            uint32_t b = AV_RN32(pixels + 1);                           \
            l1 = (a & 0x03030303UL) +                                   \
                 (b & 0x03030303UL);                                    \
            h1 = ((a & 0xFCFCFCFCUL) >> 2) +                            \
                 ((b & 0xFCFCFCFCUL) >> 2);                             \
            OP(*((uint32_t *) block), h0 + h1 +                         \
               (((l0 + l1) >> 2) & 0x0F0F0F0FUL));                      \
            pixels += line_size;                                        \
            block  += line_size;                                        \
            a  = AV_RN32(pixels);                                       \
            b  = AV_RN32(pixels + 1);                                   \
            l0 = (a & 0x03030303UL) +                                   \
                 (b & 0x03030303UL) +                                   \
                      0x02020202UL;                                     \
            h0 = ((a & 0xFCFCFCFCUL) >> 2) +                            \
                 ((b & 0xFCFCFCFCUL) >> 2);                             \
            OP(*((uint32_t *) block), h0 + h1 +                         \
               (((l0 + l1) >> 2) & 0x0F0F0F0FUL));                      \
            pixels += line_size;                                        \
            block  += line_size;                                        \
        }                                                               \
        pixels += 4 - line_size * (h + 1);                              \
        block  += 4 - line_size * h;                                    \
    }                                                                   \
}                                                                       \
                                                                        \
static inline void OPNAME ## _no_rnd_pixels8_xy2_8_c(uint8_t *block,    \
                                                     const uint8_t *pixels, \
                                                     ptrdiff_t line_size, \
                                                     int h)             \
{                                                                       \
    /* FIXME HIGH BIT DEPTH */                                          \
    int j;                                                              \
                                                                        \
    for (j = 0; j < 2; j++) {                                           \
        int i;                                                          \
        const uint32_t a = AV_RN32(pixels);                             \
        const uint32_t b = AV_RN32(pixels + 1);                         \
        uint32_t l0 = (a & 0x03030303UL) +                              \
                      (b & 0x03030303UL) +                              \
                           0x01010101UL;                                \
        uint32_t h0 = ((a & 0xFCFCFCFCUL) >> 2) +                       \
                      ((b & 0xFCFCFCFCUL) >> 2);                        \
        uint32_t l1, h1;                                                \
                                                                        \
        pixels += line_size;                                            \
        for (i = 0; i < h; i += 2) {                                    \
            uint32_t a = AV_RN32(pixels);                               \
            uint32_t b = AV_RN32(pixels + 1);                           \
            l1 = (a & 0x03030303UL) +                                   \
                 (b & 0x03030303UL);                                    \
            h1 = ((a & 0xFCFCFCFCUL) >> 2) +                            \
                 ((b & 0xFCFCFCFCUL) >> 2);                             \
            OP(*((uint32_t *) block), h0 + h1 +                         \
               (((l0 + l1) >> 2) & 0x0F0F0F0FUL));                      \
            pixels += line_size;                                        \
            block  += line_size;                                        \
            a  = AV_RN32(pixels);                                       \
            b  = AV_RN32(pixels + 1);                                   \
            l0 = (a & 0x03030303UL) +                                   \
                 (b & 0x03030303UL) +                                   \
                      0x01010101UL;                                     \
            h0 = ((a & 0xFCFCFCFCUL) >> 2) +                            \
                 ((b & 0xFCFCFCFCUL) >> 2);                             \
            OP(*((uint32_t *) block), h0 + h1 +                         \
               (((l0 + l1) >> 2) & 0x0F0F0F0FUL));                      \
            pixels += line_size;                                        \
            block  += line_size;                                        \
        }                                                               \
        pixels += 4 - line_size * (h + 1);                              \
        block  += 4 - line_size * h;                                    \
    }                                                                   \
}                                                                       \
                                                                        \
CALL_2X_PIXELS(OPNAME ## _pixels16_x2_8_c,                              \
               OPNAME ## _pixels8_x2_8_c,                               \
               8)                                                       \
CALL_2X_PIXELS(OPNAME ## _pixels16_y2_8_c,                              \
               OPNAME ## _pixels8_y2_8_c,                               \
               8)                                                       \
CALL_2X_PIXELS(OPNAME ## _pixels16_xy2_8_c,                             \
               OPNAME ## _pixels8_xy2_8_c,                              \
               8)                                                       \
CALL_2X_PIXELS(OPNAME ## _no_rnd_pixels16_8_c,                          \
               OPNAME ## _pixels8_8_c,                                  \
               8)                                                       \
CALL_2X_PIXELS(OPNAME ## _no_rnd_pixels16_x2_8_c,                       \
               OPNAME ## _no_rnd_pixels8_x2_8_c,                        \
               8)                                                       \
CALL_2X_PIXELS(OPNAME ## _no_rnd_pixels16_y2_8_c,                       \
               OPNAME ## _no_rnd_pixels8_y2_8_c,                        \
               8)                                                       \
CALL_2X_PIXELS(OPNAME ## _no_rnd_pixels16_xy2_8_c,                      \
               OPNAME ## _no_rnd_pixels8_xy2_8_c,                       \
               8)                                                       \

#define op_avg(a, b) a = rnd_avg32(a, b)
#define op_put(a, b) a = b
#define put_no_rnd_pixels8_8_c put_pixels8_8_c
PIXOP2(avg, op_avg)
PIXOP2(put, op_put)
#undef op_avg
#undef op_put

av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
{
#define hpel_funcs(prefix, idx, num) \
    c->prefix ## _pixels_tab idx [0] = prefix ## _pixels ## num ## _8_c; \
    c->prefix ## _pixels_tab idx [1] = prefix ## _pixels ## num ## _x2_8_c; \
    c->prefix ## _pixels_tab idx [2] = prefix ## _pixels ## num ## _y2_8_c; \
    c->prefix ## _pixels_tab idx [3] = prefix ## _pixels ## num ## _xy2_8_c

    hpel_funcs(put, [0], 16);
    hpel_funcs(put, [1],  8);
    hpel_funcs(put, [2],  4);
    hpel_funcs(put, [3],  2);
    hpel_funcs(put_no_rnd, [0], 16);
    hpel_funcs(put_no_rnd, [1],  8);
    hpel_funcs(avg, [0], 16);
    hpel_funcs(avg, [1],  8);
    hpel_funcs(avg, [2],  4);
    hpel_funcs(avg, [3],  2);
    hpel_funcs(avg_no_rnd,, 16);

#if ARCH_AARCH64
    ff_hpeldsp_init_aarch64(c, flags);
#elif ARCH_ALPHA
    ff_hpeldsp_init_alpha(c, flags);
#elif ARCH_ARM
    ff_hpeldsp_init_arm(c, flags);
#elif ARCH_PPC
    ff_hpeldsp_init_ppc(c, flags);
#elif ARCH_X86
    ff_hpeldsp_init_x86(c, flags);
#elif ARCH_MIPS
    ff_hpeldsp_init_mips(c, flags);
#elif ARCH_LOONGARCH64
    ff_hpeldsp_init_loongarch(c, flags);
#endif
}