Coverage Report

Created: 2026-01-16 07:48

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