Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/simple_idct.c
Line
Count
Source
1
/*
2
 * Simple IDCT
3
 *
4
 * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * simpleidct in C.
26
 */
27
28
#include "libavutil/intreadwrite.h"
29
#include "mathops.h"
30
#include "simple_idct.h"
31
32
#define IN_IDCT_DEPTH 16
33
34
#define BIT_DEPTH 8
35
#include "simple_idct_template.c"
36
#undef BIT_DEPTH
37
38
393M
#define BIT_DEPTH 10
39
#include "simple_idct_template.c"
40
#undef BIT_DEPTH
41
42
155M
#define BIT_DEPTH 12
43
#include "simple_idct_template.c"
44
#undef BIT_DEPTH
45
#undef IN_IDCT_DEPTH
46
47
#define IN_IDCT_DEPTH 32
48
1.44M
#define BIT_DEPTH 10
49
#include "simple_idct_template.c"
50
#undef BIT_DEPTH
51
#undef IN_IDCT_DEPTH
52
53
/* 2x4x8 idct */
54
55
279M
#define CN_SHIFT 12
56
186M
#define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
57
93.2M
#define C1 C_FIX(0.6532814824)
58
93.2M
#define C2 C_FIX(0.2705980501)
59
60
/* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
61
   and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
62
279M
#define C_SHIFT (4+1+12)
63
64
static inline void idct4col_put(uint8_t *dest, ptrdiff_t line_size, const int16_t *col)
65
46.6M
{
66
46.6M
    int c0, c1, c2, c3, a0, a1, a2, a3;
67
68
46.6M
    a0 = col[8*0];
69
46.6M
    a1 = col[8*2];
70
46.6M
    a2 = col[8*4];
71
46.6M
    a3 = col[8*6];
72
46.6M
    c0 = ((a0 + a2) * (1 << CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
73
46.6M
    c2 = ((a0 - a2) * (1 << CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
74
46.6M
    c1 = a1 * C1 + a3 * C2;
75
46.6M
    c3 = a1 * C2 - a3 * C1;
76
46.6M
    dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
77
46.6M
    dest += line_size;
78
46.6M
    dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
79
46.6M
    dest += line_size;
80
46.6M
    dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
81
46.6M
    dest += line_size;
82
46.6M
    dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
83
46.6M
}
84
85
93.2M
#define BF(k) \
86
93.2M
{\
87
93.2M
    int a0, a1;\
88
93.2M
    a0 = ptr[k];\
89
93.2M
    a1 = ptr[8 + k];\
90
93.2M
    ptr[k] = a0 + a1;\
91
93.2M
    ptr[8 + k] = a0 - a1;\
92
93.2M
}
93
94
/* only used by DV codec. The input must be interlaced. 128 is added
95
   to the pixels before clamping to avoid systematic error
96
   (1024*sqrt(2)) offset would be needed otherwise. */
97
/* XXX: I think a 1.0/sqrt(2) normalization should be needed to
98
   compensate the extra butterfly stage - I don't have the full DV
99
   specification */
100
void ff_simple_idct248_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
101
2.91M
{
102
2.91M
    int i;
103
2.91M
    int16_t *ptr;
104
105
    /* butterfly */
106
2.91M
    ptr = block;
107
14.5M
    for(i=0;i<4;i++) {
108
11.6M
        BF(0);
109
11.6M
        BF(1);
110
11.6M
        BF(2);
111
11.6M
        BF(3);
112
11.6M
        BF(4);
113
11.6M
        BF(5);
114
11.6M
        BF(6);
115
11.6M
        BF(7);
116
11.6M
        ptr += 2 * 8;
117
11.6M
    }
118
119
    /* IDCT8 on each line */
120
26.2M
    for(i=0; i<8; i++) {
121
23.3M
        idctRowCondDC_int16_8bit(block + i*8, 0);
122
23.3M
    }
123
124
    /* IDCT4 and store */
125
26.2M
    for(i=0;i<8;i++) {
126
23.3M
        idct4col_put(dest + i, 2 * line_size, block + i);
127
23.3M
        idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
128
23.3M
    }
129
2.91M
}
130
131
/* 8x4 & 4x8 WMV2 IDCT */
132
#undef CN_SHIFT
133
#undef C_SHIFT
134
#undef C_FIX
135
#undef C1
136
#undef C2
137
362M
#define CN_SHIFT 12
138
362M
#define C_FIX(x) ((int)((x) * M_SQRT2 * (1 << CN_SHIFT) + 0.5))
139
120M
#define C1 C_FIX(0.6532814824)
140
120M
#define C2 C_FIX(0.2705980501)
141
120M
#define C3 C_FIX(0.5)
142
362M
#define C_SHIFT (4+1+12)
143
static inline void idct4col_add(uint8_t *dest, ptrdiff_t line_size, const int16_t *col)
144
60.4M
{
145
60.4M
    int c0, c1, c2, c3, a0, a1, a2, a3;
146
147
60.4M
    a0 = col[8*0];
148
60.4M
    a1 = col[8*1];
149
60.4M
    a2 = col[8*2];
150
60.4M
    a3 = col[8*3];
151
60.4M
    c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
152
60.4M
    c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
153
60.4M
    c1 = a1 * C1 + a3 * C2;
154
60.4M
    c3 = a1 * C2 - a3 * C1;
155
60.4M
    dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
156
60.4M
    dest += line_size;
157
60.4M
    dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
158
60.4M
    dest += line_size;
159
60.4M
    dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
160
60.4M
    dest += line_size;
161
60.4M
    dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
162
60.4M
}
163
164
653M
#define RN_SHIFT 15
165
653M
#define R_FIX(x) ((int)((x) * M_SQRT2 * (1 << RN_SHIFT) + 0.5))
166
217M
#define R1 R_FIX(0.6532814824)
167
217M
#define R2 R_FIX(0.2705980501)
168
217M
#define R3 R_FIX(0.5)
169
653M
#define R_SHIFT 11
170
static inline void idct4row(int16_t *row)
171
108M
{
172
108M
    unsigned c0, c1, c2, c3;
173
108M
    int a0, a1, a2, a3;
174
175
108M
    a0 = row[0];
176
108M
    a1 = row[1];
177
108M
    a2 = row[2];
178
108M
    a3 = row[3];
179
108M
    c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
180
108M
    c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
181
108M
    c1 = a1 * R1 + a3 * R2;
182
108M
    c3 = a1 * R2 - a3 * R1;
183
108M
    row[0]= (c0 + c1) >> R_SHIFT;
184
108M
    row[1]= (c2 + c3) >> R_SHIFT;
185
108M
    row[2]= (c2 - c3) >> R_SHIFT;
186
108M
    row[3]= (c0 - c1) >> R_SHIFT;
187
108M
}
188
189
void ff_simple_idct84_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
190
6.10M
{
191
6.10M
    int i;
192
193
    /* IDCT8 on each line */
194
30.5M
    for(i=0; i<4; i++) {
195
24.4M
        idctRowCondDC_int16_8bit(block + i*8, 0);
196
24.4M
    }
197
198
    /* IDCT4 and store */
199
54.9M
    for(i=0;i<8;i++) {
200
48.8M
        idct4col_add(dest + i, line_size, block + i);
201
48.8M
    }
202
6.10M
}
203
204
void ff_simple_idct48_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
205
12.1M
{
206
12.1M
    int i;
207
208
    /* IDCT4 on each line */
209
109M
    for(i=0; i<8; i++) {
210
97.2M
        idct4row(block + i*8);
211
97.2M
    }
212
213
    /* IDCT8 and store */
214
60.7M
    for(i=0; i<4; i++){
215
48.6M
        idctSparseColAdd_int16_8bit(dest + i, line_size, block + i);
216
48.6M
    }
217
12.1M
}
218
219
void ff_simple_idct44_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
220
2.90M
{
221
2.90M
    int i;
222
223
    /* IDCT4 on each line */
224
14.5M
    for(i=0; i<4; i++) {
225
11.6M
        idct4row(block + i*8);
226
11.6M
    }
227
228
    /* IDCT4 and store */
229
14.5M
    for(i=0; i<4; i++){
230
11.6M
        idct4col_add(dest + i, line_size, block + i);
231
11.6M
    }
232
2.90M
}