Coverage Report

Created: 2025-12-31 07:57

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.03M
{
33
132M
    for (int i = 0; i < 64; i++) {
34
130M
        int j = src[i];
35
130M
        dst[i] = permutation[j];
36
130M
    }
37
2.03M
}
38
39
av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
40
                                           enum idct_permutation_type perm_type)
41
603k
{
42
603k
    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
603k
    switch (perm_type) {
51
550k
    case FF_IDCT_PERM_NONE:
52
35.7M
        for (i = 0; i < 64; i++)
53
35.2M
            idct_permutation[i] = i;
54
550k
        break;
55
52.8k
    case FF_IDCT_PERM_LIBMPEG2:
56
3.43M
        for (i = 0; i < 64; i++)
57
3.38M
            idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
58
52.8k
        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
603k
    }
71
603k
}
72
73
void ff_put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
74
                             ptrdiff_t line_size)
75
99.1M
{
76
99.1M
    int i;
77
78
    /* read the pixels */
79
892M
    for (i = 0; i < 8; i++) {
80
793M
        pixels[0] = av_clip_uint8(block[0]);
81
793M
        pixels[1] = av_clip_uint8(block[1]);
82
793M
        pixels[2] = av_clip_uint8(block[2]);
83
793M
        pixels[3] = av_clip_uint8(block[3]);
84
793M
        pixels[4] = av_clip_uint8(block[4]);
85
793M
        pixels[5] = av_clip_uint8(block[5]);
86
793M
        pixels[6] = av_clip_uint8(block[6]);
87
793M
        pixels[7] = av_clip_uint8(block[7]);
88
89
793M
        pixels += line_size;
90
793M
        block  += 8;
91
793M
    }
92
99.1M
}
93
94
static void put_pixels_clamped4_c(const int16_t *block, uint8_t *restrict pixels,
95
                                 int line_size)
96
11.9M
{
97
11.9M
    int i;
98
99
    /* read the pixels */
100
59.7M
    for(i=0;i<4;i++) {
101
47.8M
        pixels[0] = av_clip_uint8(block[0]);
102
47.8M
        pixels[1] = av_clip_uint8(block[1]);
103
47.8M
        pixels[2] = av_clip_uint8(block[2]);
104
47.8M
        pixels[3] = av_clip_uint8(block[3]);
105
106
47.8M
        pixels += line_size;
107
47.8M
        block += 8;
108
47.8M
    }
109
11.9M
}
110
111
static void put_pixels_clamped2_c(const int16_t *block, uint8_t *restrict pixels,
112
                                 int line_size)
113
4.04M
{
114
4.04M
    int i;
115
116
    /* read the pixels */
117
12.1M
    for(i=0;i<2;i++) {
118
8.09M
        pixels[0] = av_clip_uint8(block[0]);
119
8.09M
        pixels[1] = av_clip_uint8(block[1]);
120
121
8.09M
        pixels += line_size;
122
8.09M
        block += 8;
123
8.09M
    }
124
4.04M
}
125
126
static void put_signed_pixels_clamped_c(const int16_t *block,
127
                                        uint8_t *restrict pixels,
128
                                        ptrdiff_t line_size)
129
28.4M
{
130
28.4M
    int i, j;
131
132
255M
    for (i = 0; i < 8; i++) {
133
2.04G
        for (j = 0; j < 8; j++) {
134
1.81G
            if (*block < -128)
135
377M
                *pixels = 0;
136
1.44G
            else if (*block > 127)
137
544M
                *pixels = 255;
138
897M
            else
139
897M
                *pixels = (uint8_t) (*block + 128);
140
1.81G
            block++;
141
1.81G
            pixels++;
142
1.81G
        }
143
227M
        pixels += (line_size - 8);
144
227M
    }
145
28.4M
}
146
147
void ff_add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
148
                             ptrdiff_t line_size)
149
22.3M
{
150
22.3M
    int i;
151
152
    /* read the pixels */
153
201M
    for (i = 0; i < 8; i++) {
154
178M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
155
178M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
156
178M
        pixels[2] = av_clip_uint8(pixels[2] + block[2]);
157
178M
        pixels[3] = av_clip_uint8(pixels[3] + block[3]);
158
178M
        pixels[4] = av_clip_uint8(pixels[4] + block[4]);
159
178M
        pixels[5] = av_clip_uint8(pixels[5] + block[5]);
160
178M
        pixels[6] = av_clip_uint8(pixels[6] + block[6]);
161
178M
        pixels[7] = av_clip_uint8(pixels[7] + block[7]);
162
178M
        pixels   += line_size;
163
178M
        block    += 8;
164
178M
    }
165
22.3M
}
166
167
static void add_pixels_clamped4_c(const int16_t *block, uint8_t *restrict pixels,
168
                          int line_size)
169
3.04M
{
170
3.04M
    int i;
171
172
    /* read the pixels */
173
15.2M
    for(i=0;i<4;i++) {
174
12.1M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
175
12.1M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
176
12.1M
        pixels[2] = av_clip_uint8(pixels[2] + block[2]);
177
12.1M
        pixels[3] = av_clip_uint8(pixels[3] + block[3]);
178
12.1M
        pixels += line_size;
179
12.1M
        block += 8;
180
12.1M
    }
181
3.04M
}
182
183
static void add_pixels_clamped2_c(const int16_t *block, uint8_t *restrict pixels,
184
                          int line_size)
185
1.13M
{
186
1.13M
    int i;
187
188
    /* read the pixels */
189
3.40M
    for(i=0;i<2;i++) {
190
2.26M
        pixels[0] = av_clip_uint8(pixels[0] + block[0]);
191
2.26M
        pixels[1] = av_clip_uint8(pixels[1] + block[1]);
192
2.26M
        pixels += line_size;
193
2.26M
        block += 8;
194
2.26M
    }
195
1.13M
}
196
197
static void ff_jref_idct4_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
198
11.9M
{
199
11.9M
    ff_j_rev_dct4 (block);
200
11.9M
    put_pixels_clamped4_c(block, dest, line_size);
201
11.9M
}
202
static void ff_jref_idct4_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
203
3.04M
{
204
3.04M
    ff_j_rev_dct4 (block);
205
3.04M
    add_pixels_clamped4_c(block, dest, line_size);
206
3.04M
}
207
208
static void ff_jref_idct2_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
209
4.04M
{
210
4.04M
    ff_j_rev_dct2 (block);
211
4.04M
    put_pixels_clamped2_c(block, dest, line_size);
212
4.04M
}
213
static void ff_jref_idct2_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
214
1.13M
{
215
1.13M
    ff_j_rev_dct2 (block);
216
1.13M
    add_pixels_clamped2_c(block, dest, line_size);
217
1.13M
}
218
219
static void ff_jref_idct1_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
220
6.33M
{
221
6.33M
    dest[0] = av_clip_uint8((block[0] + 4)>>3);
222
6.33M
}
223
static void ff_jref_idct1_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
224
1.66M
{
225
1.66M
    dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
226
1.66M
}
227
228
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
229
519k
{
230
519k
    av_unused const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
231
232
519k
    if (avctx->lowres==1) {
233
16.2k
        c->idct_put  = ff_jref_idct4_put;
234
16.2k
        c->idct_add  = ff_jref_idct4_add;
235
16.2k
        c->idct      = ff_j_rev_dct4;
236
16.2k
        c->perm_type = FF_IDCT_PERM_NONE;
237
503k
    } else if (avctx->lowres==2) {
238
8.82k
        c->idct_put  = ff_jref_idct2_put;
239
8.82k
        c->idct_add  = ff_jref_idct2_add;
240
8.82k
        c->idct      = ff_j_rev_dct2;
241
8.82k
        c->perm_type = FF_IDCT_PERM_NONE;
242
494k
    } else if (avctx->lowres==3) {
243
14.5k
        c->idct_put  = ff_jref_idct1_put;
244
14.5k
        c->idct_add  = ff_jref_idct1_add;
245
14.5k
        c->idct      = ff_j_rev_dct1;
246
14.5k
        c->perm_type = FF_IDCT_PERM_NONE;
247
480k
    } else {
248
480k
        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
57.4k
            if (c->mpeg4_studio_profile) {
252
937
                c->idct_put              = ff_simple_idct_put_int32_10bit;
253
937
                c->idct_add              = NULL;
254
937
                c->idct                  = NULL;
255
56.4k
            } else {
256
56.4k
                c->idct_put              = ff_simple_idct_put_int16_10bit;
257
56.4k
                c->idct_add              = ff_simple_idct_add_int16_10bit;
258
56.4k
                c->idct                  = ff_simple_idct_int16_10bit;
259
56.4k
            }
260
57.4k
            c->perm_type             = FF_IDCT_PERM_NONE;
261
422k
        } else if (avctx->bits_per_raw_sample == 12) {
262
19.5k
            c->idct_put              = ff_simple_idct_put_int16_12bit;
263
19.5k
            c->idct_add              = ff_simple_idct_add_int16_12bit;
264
19.5k
            c->idct                  = ff_simple_idct_int16_12bit;
265
19.5k
            c->perm_type             = FF_IDCT_PERM_NONE;
266
403k
        } else {
267
403k
            if (avctx->idct_algo == FF_IDCT_INT) {
268
52.8k
                c->idct_put  = ff_jref_idct_put;
269
52.8k
                c->idct_add  = ff_jref_idct_add;
270
52.8k
                c->idct      = ff_j_rev_dct;
271
52.8k
                c->perm_type = FF_IDCT_PERM_LIBMPEG2;
272
52.8k
#if CONFIG_FAANIDCT
273
350k
            } else if (avctx->idct_algo == FF_IDCT_FAAN) {
274
22.7k
                c->idct_put  = ff_faanidct_put;
275
22.7k
                c->idct_add  = ff_faanidct_add;
276
22.7k
                c->idct      = ff_faanidct;
277
22.7k
                c->perm_type = FF_IDCT_PERM_NONE;
278
22.7k
#endif /* CONFIG_FAANIDCT */
279
22.7k
#if CONFIG_MPEG4_DECODER
280
327k
            } else if (avctx->idct_algo == FF_IDCT_XVID) {
281
16.7k
                ff_xvid_idct_init(c);
282
16.7k
#endif
283
310k
            } else { // accurate/default
284
310k
                c->idct_put  = ff_simple_idct_put_int16_8bit;
285
310k
                c->idct_add  = ff_simple_idct_add_int16_8bit;
286
310k
                c->idct      = ff_simple_idct_int16_8bit;
287
310k
                c->perm_type = FF_IDCT_PERM_NONE;
288
310k
            }
289
403k
        }
290
480k
    }
291
292
519k
    c->put_pixels_clamped        = ff_put_pixels_clamped_c;
293
519k
    c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
294
519k
    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
519k
    ff_init_scantable_permutation(c->idct_permutation,
313
519k
                                  c->perm_type);
314
519k
}