Coverage Report

Created: 2026-04-01 07:42

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
2.41M
{
33
157M
    for (int i = 0; i < 64; i++) {
34
154M
        int j = src[i];
35
154M
        dst[i] = permutation[j];
36
154M
    }
37
2.41M
}
38
39
av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
40
                                           enum idct_permutation_type perm_type)
41
932k
{
42
932k
    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
932k
    switch (perm_type) {
51
852k
    case FF_IDCT_PERM_NONE:
52
55.4M
        for (i = 0; i < 64; i++)
53
54.5M
            idct_permutation[i] = i;
54
852k
        break;
55
80.2k
    case FF_IDCT_PERM_LIBMPEG2:
56
5.21M
        for (i = 0; i < 64; i++)
57
5.13M
            idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
58
80.2k
        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
932k
    }
71
932k
}
72
73
void ff_put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
74
                             ptrdiff_t line_size)
75
96.8M
{
76
96.8M
    int i;
77
78
    /* read the pixels */
79
871M
    for (i = 0; i < 8; i++) {
80
774M
        pixels[0] = av_clip_uint8(block[0]);
81
774M
        pixels[1] = av_clip_uint8(block[1]);
82
774M
        pixels[2] = av_clip_uint8(block[2]);
83
774M
        pixels[3] = av_clip_uint8(block[3]);
84
774M
        pixels[4] = av_clip_uint8(block[4]);
85
774M
        pixels[5] = av_clip_uint8(block[5]);
86
774M
        pixels[6] = av_clip_uint8(block[6]);
87
774M
        pixels[7] = av_clip_uint8(block[7]);
88
89
774M
        pixels += line_size;
90
774M
        block  += 8;
91
774M
    }
92
96.8M
}
93
94
static void put_pixels_clamped4_c(const int16_t *block, uint8_t *restrict pixels,
95
                                 int line_size)
96
13.0M
{
97
13.0M
    int i;
98
99
    /* read the pixels */
100
65.0M
    for(i=0;i<4;i++) {
101
52.0M
        pixels[0] = av_clip_uint8(block[0]);
102
52.0M
        pixels[1] = av_clip_uint8(block[1]);
103
52.0M
        pixels[2] = av_clip_uint8(block[2]);
104
52.0M
        pixels[3] = av_clip_uint8(block[3]);
105
106
52.0M
        pixels += line_size;
107
52.0M
        block += 8;
108
52.0M
    }
109
13.0M
}
110
111
static void put_pixels_clamped2_c(const int16_t *block, uint8_t *restrict pixels,
112
                                 int line_size)
113
7.03M
{
114
7.03M
    int i;
115
116
    /* read the pixels */
117
21.0M
    for(i=0;i<2;i++) {
118
14.0M
        pixels[0] = av_clip_uint8(block[0]);
119
14.0M
        pixels[1] = av_clip_uint8(block[1]);
120
121
14.0M
        pixels += line_size;
122
14.0M
        block += 8;
123
14.0M
    }
124
7.03M
}
125
126
static void put_signed_pixels_clamped_c(const int16_t *block,
127
                                        uint8_t *restrict pixels,
128
                                        ptrdiff_t line_size)
129
24.8M
{
130
24.8M
    int i, j;
131
132
224M
    for (i = 0; i < 8; i++) {
133
1.79G
        for (j = 0; j < 8; j++) {
134
1.59G
            if (*block < -128)
135
316M
                *pixels = 0;
136
1.27G
            else if (*block > 127)
137
492M
                *pixels = 255;
138
784M
            else
139
784M
                *pixels = (uint8_t) (*block + 128);
140
1.59G
            block++;
141
1.59G
            pixels++;
142
1.59G
        }
143
199M
        pixels += (line_size - 8);
144
199M
    }
145
24.8M
}
146
147
void ff_add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
148
                             ptrdiff_t line_size)
149
18.2M
{
150
18.2M
    int i;
151
152
    /* read the pixels */
153
164M
    for (i = 0; i < 8; i++) {
154
145M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
155
145M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
156
145M
        pixels[2] = av_clip_uint8(pixels[2] + block[2]);
157
145M
        pixels[3] = av_clip_uint8(pixels[3] + block[3]);
158
145M
        pixels[4] = av_clip_uint8(pixels[4] + block[4]);
159
145M
        pixels[5] = av_clip_uint8(pixels[5] + block[5]);
160
145M
        pixels[6] = av_clip_uint8(pixels[6] + block[6]);
161
145M
        pixels[7] = av_clip_uint8(pixels[7] + block[7]);
162
145M
        pixels   += line_size;
163
145M
        block    += 8;
164
145M
    }
165
18.2M
}
166
167
static void add_pixels_clamped4_c(const int16_t *block, uint8_t *restrict pixels,
168
                          int line_size)
169
3.26M
{
170
3.26M
    int i;
171
172
    /* read the pixels */
173
16.3M
    for(i=0;i<4;i++) {
174
13.0M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
175
13.0M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
176
13.0M
        pixels[2] = av_clip_uint8(pixels[2] + block[2]);
177
13.0M
        pixels[3] = av_clip_uint8(pixels[3] + block[3]);
178
13.0M
        pixels += line_size;
179
13.0M
        block += 8;
180
13.0M
    }
181
3.26M
}
182
183
static void add_pixels_clamped2_c(const int16_t *block, uint8_t *restrict pixels,
184
                          int line_size)
185
759k
{
186
759k
    int i;
187
188
    /* read the pixels */
189
2.27M
    for(i=0;i<2;i++) {
190
1.51M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
191
1.51M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
192
1.51M
        pixels += line_size;
193
1.51M
        block += 8;
194
1.51M
    }
195
759k
}
196
197
static void ff_jref_idct4_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
198
13.0M
{
199
13.0M
    ff_j_rev_dct4 (block);
200
13.0M
    put_pixels_clamped4_c(block, dest, line_size);
201
13.0M
}
202
static void ff_jref_idct4_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
203
3.26M
{
204
3.26M
    ff_j_rev_dct4 (block);
205
3.26M
    add_pixels_clamped4_c(block, dest, line_size);
206
3.26M
}
207
208
static void ff_jref_idct2_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
209
7.03M
{
210
7.03M
    ff_j_rev_dct2 (block);
211
7.03M
    put_pixels_clamped2_c(block, dest, line_size);
212
7.03M
}
213
static void ff_jref_idct2_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
214
759k
{
215
759k
    ff_j_rev_dct2 (block);
216
759k
    add_pixels_clamped2_c(block, dest, line_size);
217
759k
}
218
219
static void ff_jref_idct1_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
220
6.69M
{
221
6.69M
    dest[0] = av_clip_uint8((block[0] + 4)>>3);
222
6.69M
}
223
static void ff_jref_idct1_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
224
2.16M
{
225
2.16M
    dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
226
2.16M
}
227
228
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
229
855k
{
230
855k
    av_unused const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
231
232
855k
    if (avctx->lowres==1) {
233
26.2k
        c->idct_put  = ff_jref_idct4_put;
234
26.2k
        c->idct_add  = ff_jref_idct4_add;
235
26.2k
        c->idct      = ff_j_rev_dct4;
236
26.2k
        c->perm_type = FF_IDCT_PERM_NONE;
237
828k
    } else if (avctx->lowres==2) {
238
27.5k
        c->idct_put  = ff_jref_idct2_put;
239
27.5k
        c->idct_add  = ff_jref_idct2_add;
240
27.5k
        c->idct      = ff_j_rev_dct2;
241
27.5k
        c->perm_type = FF_IDCT_PERM_NONE;
242
801k
    } else if (avctx->lowres==3) {
243
17.9k
        c->idct_put  = ff_jref_idct1_put;
244
17.9k
        c->idct_add  = ff_jref_idct1_add;
245
17.9k
        c->idct      = ff_j_rev_dct1;
246
17.9k
        c->perm_type = FF_IDCT_PERM_NONE;
247
783k
    } else {
248
783k
        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
112k
            if (c->mpeg4_studio_profile) {
252
965
                c->idct_put              = ff_simple_idct_put_int32_10bit;
253
965
                c->idct_add              = NULL;
254
965
                c->idct                  = NULL;
255
111k
            } else {
256
111k
                c->idct_put              = ff_simple_idct_put_int16_10bit;
257
111k
                c->idct_add              = ff_simple_idct_add_int16_10bit;
258
111k
                c->idct                  = ff_simple_idct_int16_10bit;
259
111k
            }
260
112k
            c->perm_type             = FF_IDCT_PERM_NONE;
261
671k
        } else if (avctx->bits_per_raw_sample == 12) {
262
30.5k
            c->idct_put              = ff_simple_idct_put_int16_12bit;
263
30.5k
            c->idct_add              = ff_simple_idct_add_int16_12bit;
264
30.5k
            c->idct                  = ff_simple_idct_int16_12bit;
265
30.5k
            c->perm_type             = FF_IDCT_PERM_NONE;
266
640k
        } else {
267
640k
            if (avctx->idct_algo == FF_IDCT_INT) {
268
80.2k
                c->idct_put  = ff_jref_idct_put;
269
80.2k
                c->idct_add  = ff_jref_idct_add;
270
80.2k
                c->idct      = ff_j_rev_dct;
271
80.2k
                c->perm_type = FF_IDCT_PERM_LIBMPEG2;
272
80.2k
#if CONFIG_FAANIDCT
273
560k
            } else if (avctx->idct_algo == FF_IDCT_FAAN) {
274
25.9k
                c->idct_put  = ff_faanidct_put;
275
25.9k
                c->idct_add  = ff_faanidct_add;
276
25.9k
                c->idct      = ff_faanidct;
277
25.9k
                c->perm_type = FF_IDCT_PERM_NONE;
278
25.9k
#endif /* CONFIG_FAANIDCT */
279
25.9k
#if CONFIG_MPEG4_DECODER
280
534k
            } else if (avctx->idct_algo == FF_IDCT_XVID) {
281
89.3k
                ff_xvid_idct_init(c);
282
89.3k
#endif
283
445k
            } else { // accurate/default
284
445k
                c->idct_put  = ff_simple_idct_put_int16_8bit;
285
445k
                c->idct_add  = ff_simple_idct_add_int16_8bit;
286
445k
                c->idct      = ff_simple_idct_int16_8bit;
287
445k
                c->perm_type = FF_IDCT_PERM_NONE;
288
445k
            }
289
640k
        }
290
783k
    }
291
292
855k
    c->put_pixels_clamped        = ff_put_pixels_clamped_c;
293
855k
    c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
294
855k
    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
855k
    ff_init_scantable_permutation(c->idct_permutation,
313
855k
                                  c->perm_type);
314
855k
}