Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/idctdsp.c
Line
Count
Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include "config.h"
20
#include "config_components.h"
21
#include "libavutil/attributes.h"
22
#include "libavutil/common.h"
23
#include "avcodec.h"
24
#include "dct.h"
25
#include "faanidct.h"
26
#include "idctdsp.h"
27
#include "simple_idct.h"
28
#include "xvididct.h"
29
30
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64],
31
                                  const uint8_t permutation[64])
32
1.70M
{
33
111M
    for (int i = 0; i < 64; i++) {
34
109M
        int j = src[i];
35
109M
        dst[i] = permutation[j];
36
109M
    }
37
1.70M
}
38
39
av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
40
                                           enum idct_permutation_type perm_type)
41
833k
{
42
833k
    int i;
43
44
#if ARCH_X86 && HAVE_X86ASM
45
    if (ff_init_scantable_permutation_x86(idct_permutation,
46
                                          perm_type))
47
        return;
48
#endif
49
50
833k
    switch (perm_type) {
51
752k
    case FF_IDCT_PERM_NONE:
52
48.9M
        for (i = 0; i < 64; i++)
53
48.1M
            idct_permutation[i] = i;
54
752k
        break;
55
80.4k
    case FF_IDCT_PERM_LIBMPEG2:
56
5.22M
        for (i = 0; i < 64; i++)
57
5.14M
            idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
58
80.4k
        break;
59
0
    case FF_IDCT_PERM_TRANSPOSE:
60
0
        for (i = 0; i < 64; i++)
61
0
            idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
62
0
        break;
63
0
    case FF_IDCT_PERM_PARTTRANS:
64
0
        for (i = 0; i < 64; i++)
65
0
            idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
66
0
        break;
67
0
    default:
68
0
        av_log(NULL, AV_LOG_ERROR,
69
0
               "Internal error, IDCT permutation not set\n");
70
833k
    }
71
833k
}
72
73
void ff_put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
74
                             ptrdiff_t line_size)
75
100M
{
76
100M
    int i;
77
78
    /* read the pixels */
79
907M
    for (i = 0; i < 8; i++) {
80
806M
        pixels[0] = av_clip_uint8(block[0]);
81
806M
        pixels[1] = av_clip_uint8(block[1]);
82
806M
        pixels[2] = av_clip_uint8(block[2]);
83
806M
        pixels[3] = av_clip_uint8(block[3]);
84
806M
        pixels[4] = av_clip_uint8(block[4]);
85
806M
        pixels[5] = av_clip_uint8(block[5]);
86
806M
        pixels[6] = av_clip_uint8(block[6]);
87
806M
        pixels[7] = av_clip_uint8(block[7]);
88
89
806M
        pixels += line_size;
90
806M
        block  += 8;
91
806M
    }
92
100M
}
93
94
static void put_pixels_clamped4_c(const int16_t *block, uint8_t *restrict pixels,
95
                                 int line_size)
96
12.6M
{
97
12.6M
    int i;
98
99
    /* read the pixels */
100
63.1M
    for(i=0;i<4;i++) {
101
50.4M
        pixels[0] = av_clip_uint8(block[0]);
102
50.4M
        pixels[1] = av_clip_uint8(block[1]);
103
50.4M
        pixels[2] = av_clip_uint8(block[2]);
104
50.4M
        pixels[3] = av_clip_uint8(block[3]);
105
106
50.4M
        pixels += line_size;
107
50.4M
        block += 8;
108
50.4M
    }
109
12.6M
}
110
111
static void put_pixels_clamped2_c(const int16_t *block, uint8_t *restrict pixels,
112
                                 int line_size)
113
5.78M
{
114
5.78M
    int i;
115
116
    /* read the pixels */
117
17.3M
    for(i=0;i<2;i++) {
118
11.5M
        pixels[0] = av_clip_uint8(block[0]);
119
11.5M
        pixels[1] = av_clip_uint8(block[1]);
120
121
11.5M
        pixels += line_size;
122
11.5M
        block += 8;
123
11.5M
    }
124
5.78M
}
125
126
static void put_signed_pixels_clamped_c(const int16_t *block,
127
                                        uint8_t *restrict pixels,
128
                                        ptrdiff_t line_size)
129
25.8M
{
130
25.8M
    int i, j;
131
132
232M
    for (i = 0; i < 8; i++) {
133
1.86G
        for (j = 0; j < 8; j++) {
134
1.65G
            if (*block < -128)
135
364M
                *pixels = 0;
136
1.29G
            else if (*block > 127)
137
515M
                *pixels = 255;
138
775M
            else
139
775M
                *pixels = (uint8_t) (*block + 128);
140
1.65G
            block++;
141
1.65G
            pixels++;
142
1.65G
        }
143
207M
        pixels += (line_size - 8);
144
207M
    }
145
25.8M
}
146
147
void ff_add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
148
                             ptrdiff_t line_size)
149
17.5M
{
150
17.5M
    int i;
151
152
    /* read the pixels */
153
157M
    for (i = 0; i < 8; i++) {
154
140M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
155
140M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
156
140M
        pixels[2] = av_clip_uint8(pixels[2] + block[2]);
157
140M
        pixels[3] = av_clip_uint8(pixels[3] + block[3]);
158
140M
        pixels[4] = av_clip_uint8(pixels[4] + block[4]);
159
140M
        pixels[5] = av_clip_uint8(pixels[5] + block[5]);
160
140M
        pixels[6] = av_clip_uint8(pixels[6] + block[6]);
161
140M
        pixels[7] = av_clip_uint8(pixels[7] + block[7]);
162
140M
        pixels   += line_size;
163
140M
        block    += 8;
164
140M
    }
165
17.5M
}
166
167
static void add_pixels_clamped4_c(const int16_t *block, uint8_t *restrict pixels,
168
                          int line_size)
169
3.15M
{
170
3.15M
    int i;
171
172
    /* read the pixels */
173
15.7M
    for(i=0;i<4;i++) {
174
12.6M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
175
12.6M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
176
12.6M
        pixels[2] = av_clip_uint8(pixels[2] + block[2]);
177
12.6M
        pixels[3] = av_clip_uint8(pixels[3] + block[3]);
178
12.6M
        pixels += line_size;
179
12.6M
        block += 8;
180
12.6M
    }
181
3.15M
}
182
183
static void add_pixels_clamped2_c(const int16_t *block, uint8_t *restrict pixels,
184
                          int line_size)
185
954k
{
186
954k
    int i;
187
188
    /* read the pixels */
189
2.86M
    for(i=0;i<2;i++) {
190
1.90M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
191
1.90M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
192
1.90M
        pixels += line_size;
193
1.90M
        block += 8;
194
1.90M
    }
195
954k
}
196
197
static void ff_jref_idct4_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
198
12.6M
{
199
12.6M
    ff_j_rev_dct4 (block);
200
12.6M
    put_pixels_clamped4_c(block, dest, line_size);
201
12.6M
}
202
static void ff_jref_idct4_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
203
3.15M
{
204
3.15M
    ff_j_rev_dct4 (block);
205
3.15M
    add_pixels_clamped4_c(block, dest, line_size);
206
3.15M
}
207
208
static void ff_jref_idct2_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
209
5.78M
{
210
5.78M
    ff_j_rev_dct2 (block);
211
5.78M
    put_pixels_clamped2_c(block, dest, line_size);
212
5.78M
}
213
static void ff_jref_idct2_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
214
954k
{
215
954k
    ff_j_rev_dct2 (block);
216
954k
    add_pixels_clamped2_c(block, dest, line_size);
217
954k
}
218
219
static void ff_jref_idct1_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
220
6.90M
{
221
6.90M
    dest[0] = av_clip_uint8((block[0] + 4)>>3);
222
6.90M
}
223
static void ff_jref_idct1_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
224
2.49M
{
225
2.49M
    dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
226
2.49M
}
227
228
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
229
756k
{
230
756k
    av_unused const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
231
232
756k
    if (avctx->lowres==1) {
233
38.5k
        c->idct_put  = ff_jref_idct4_put;
234
38.5k
        c->idct_add  = ff_jref_idct4_add;
235
38.5k
        c->idct      = ff_j_rev_dct4;
236
38.5k
        c->perm_type = FF_IDCT_PERM_NONE;
237
718k
    } else if (avctx->lowres==2) {
238
14.6k
        c->idct_put  = ff_jref_idct2_put;
239
14.6k
        c->idct_add  = ff_jref_idct2_add;
240
14.6k
        c->idct      = ff_j_rev_dct2;
241
14.6k
        c->perm_type = FF_IDCT_PERM_NONE;
242
703k
    } else if (avctx->lowres==3) {
243
17.3k
        c->idct_put  = ff_jref_idct1_put;
244
17.3k
        c->idct_add  = ff_jref_idct1_add;
245
17.3k
        c->idct      = ff_j_rev_dct1;
246
17.3k
        c->perm_type = FF_IDCT_PERM_NONE;
247
686k
    } else {
248
686k
        if (avctx->bits_per_raw_sample == 10 || avctx->bits_per_raw_sample == 9) {
249
            /* 10-bit MPEG-4 Simple Studio Profile requires a higher precision IDCT
250
               However, it only uses idct_put */
251
86.5k
            if (c->mpeg4_studio_profile) {
252
1.00k
                c->idct_put              = ff_simple_idct_put_int32_10bit;
253
1.00k
                c->idct_add              = NULL;
254
1.00k
                c->idct                  = NULL;
255
85.5k
            } else {
256
85.5k
                c->idct_put              = ff_simple_idct_put_int16_10bit;
257
85.5k
                c->idct_add              = ff_simple_idct_add_int16_10bit;
258
85.5k
                c->idct                  = ff_simple_idct_int16_10bit;
259
85.5k
            }
260
86.5k
            c->perm_type             = FF_IDCT_PERM_NONE;
261
599k
        } else if (avctx->bits_per_raw_sample == 12) {
262
29.1k
            c->idct_put              = ff_simple_idct_put_int16_12bit;
263
29.1k
            c->idct_add              = ff_simple_idct_add_int16_12bit;
264
29.1k
            c->idct                  = ff_simple_idct_int16_12bit;
265
29.1k
            c->perm_type             = FF_IDCT_PERM_NONE;
266
570k
        } else {
267
570k
            if (avctx->idct_algo == FF_IDCT_INT) {
268
80.4k
                c->idct_put  = ff_jref_idct_put;
269
80.4k
                c->idct_add  = ff_jref_idct_add;
270
80.4k
                c->idct      = ff_j_rev_dct;
271
80.4k
                c->perm_type = FF_IDCT_PERM_LIBMPEG2;
272
80.4k
#if CONFIG_FAANIDCT
273
489k
            } else if (avctx->idct_algo == FF_IDCT_FAAN) {
274
22.0k
                c->idct_put  = ff_faanidct_put;
275
22.0k
                c->idct_add  = ff_faanidct_add;
276
22.0k
                c->idct      = ff_faanidct;
277
22.0k
                c->perm_type = FF_IDCT_PERM_NONE;
278
22.0k
#endif /* CONFIG_FAANIDCT */
279
22.0k
#if CONFIG_MPEG4_DECODER
280
467k
            } else if (avctx->idct_algo == FF_IDCT_XVID) {
281
67.1k
                ff_xvid_idct_init(c);
282
67.1k
#endif
283
400k
            } else { // accurate/default
284
400k
                c->idct_put  = ff_simple_idct_put_int16_8bit;
285
400k
                c->idct_add  = ff_simple_idct_add_int16_8bit;
286
400k
                c->idct      = ff_simple_idct_int16_8bit;
287
400k
                c->perm_type = FF_IDCT_PERM_NONE;
288
400k
            }
289
570k
        }
290
686k
    }
291
292
756k
    c->put_pixels_clamped        = ff_put_pixels_clamped_c;
293
756k
    c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
294
756k
    c->add_pixels_clamped        = ff_add_pixels_clamped_c;
295
296
#if ARCH_AARCH64
297
    ff_idctdsp_init_aarch64(c, avctx, high_bit_depth);
298
#elif ARCH_ARM
299
    ff_idctdsp_init_arm(c, avctx, high_bit_depth);
300
#elif ARCH_PPC
301
    ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
302
#elif ARCH_RISCV
303
    ff_idctdsp_init_riscv(c, avctx, high_bit_depth);
304
#elif ARCH_X86 && HAVE_X86ASM
305
    ff_idctdsp_init_x86(c, avctx, high_bit_depth);
306
#elif ARCH_MIPS
307
    ff_idctdsp_init_mips(c, avctx, high_bit_depth);
308
#elif ARCH_LOONGARCH
309
    ff_idctdsp_init_loongarch(c, avctx, high_bit_depth);
310
#endif
311
312
756k
    ff_init_scantable_permutation(c->idct_permutation,
313
756k
                                  c->perm_type);
314
756k
}