FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264idct_template.c
Date: 2026-08-14 15:11:37
Exec Total Coverage
Lines: 203 203 100.0%
Functions: 41 51 80.4%
Branches: 70 70 100.0%

Line Branch Exec Source
1 /*
2 * H.264 IDCT
3 * Copyright (c) 2004-2011 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 IDCT.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "bit_depth_template.c"
29 #include "libavutil/common.h"
30 #include "h264_parse.h"
31 #include "h264idct.h"
32
33 109162882 void FUNCC(ff_h264_idct_add)(uint8_t *_dst, int16_t *_block, ptrdiff_t stride)
34 {
35 int i;
36 109162882 pixel *dst = (pixel*)_dst;
37 109162882 dctcoef *block = (dctcoef*)_block;
38 109162882 stride >>= sizeof(pixel)-1;
39
40 109162882 block[0] += 1 << 5;
41
42
2/2
✓ Branch 0 taken 218325764 times.
✓ Branch 1 taken 54581441 times.
545814410 for(i=0; i<4; i++){
43 436651528 const SUINT z0= block[i + 4*0] + (unsigned)block[i + 4*2];
44 436651528 const SUINT z1= block[i + 4*0] - (unsigned)block[i + 4*2];
45 436651528 const SUINT z2= (block[i + 4*1]>>1) - (unsigned)block[i + 4*3];
46 436651528 const SUINT z3= block[i + 4*1] + (unsigned)(block[i + 4*3]>>1);
47
48 436651528 block[i + 4*0]= z0 + z3;
49 436651528 block[i + 4*1]= z1 + z2;
50 436651528 block[i + 4*2]= z1 - z2;
51 436651528 block[i + 4*3]= z0 - z3;
52 }
53
54
2/2
✓ Branch 0 taken 218325764 times.
✓ Branch 1 taken 54581441 times.
545814410 for(i=0; i<4; i++){
55 436651528 const SUINT z0= block[0 + 4*i] + (SUINT)block[2 + 4*i];
56 436651528 const SUINT z1= block[0 + 4*i] - (SUINT)block[2 + 4*i];
57 436651528 const SUINT z2= (block[1 + 4*i]>>1) - (SUINT)block[3 + 4*i];
58 436651528 const SUINT z3= block[1 + 4*i] + (SUINT)(block[3 + 4*i]>>1);
59
60 436651528 dst[i + 0*stride]= av_clip_pixel(dst[i + 0*stride] + ((int)(z0 + z3) >> 6));
61 436651528 dst[i + 1*stride]= av_clip_pixel(dst[i + 1*stride] + ((int)(z1 + z2) >> 6));
62 436651528 dst[i + 2*stride]= av_clip_pixel(dst[i + 2*stride] + ((int)(z1 - z2) >> 6));
63 436651528 dst[i + 3*stride]= av_clip_pixel(dst[i + 3*stride] + ((int)(z0 - z3) >> 6));
64 }
65
66 109162882 memset(block, 0, 16 * sizeof(dctcoef));
67 109162882 }
68
69 13600438 void FUNCC(ff_h264_idct8_add)(uint8_t *_dst, int16_t *_block, ptrdiff_t stride)
70 {
71 int i;
72 13600438 pixel *dst = (pixel*)_dst;
73 13600438 dctcoef *block = (dctcoef*)_block;
74 13600438 stride >>= sizeof(pixel)-1;
75
76 13600438 block[0] += 32;
77
78
2/2
✓ Branch 0 taken 54401752 times.
✓ Branch 1 taken 6800219 times.
122403942 for( i = 0; i < 8; i++ )
79 {
80 108803504 const unsigned int a0 = block[i+0*8] + (unsigned)block[i+4*8];
81 108803504 const unsigned int a2 = block[i+0*8] - (unsigned)block[i+4*8];
82 108803504 const unsigned int a4 = (block[i+2*8]>>1) - (unsigned)block[i+6*8];
83 108803504 const unsigned int a6 = (block[i+6*8]>>1) + (unsigned)block[i+2*8];
84
85 108803504 const unsigned int b0 = a0 + a6;
86 108803504 const unsigned int b2 = a2 + a4;
87 108803504 const unsigned int b4 = a2 - a4;
88 108803504 const unsigned int b6 = a0 - a6;
89
90 108803504 const int a1 = -block[i+3*8] + (unsigned)block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
91 108803504 const int a3 = block[i+1*8] + (unsigned)block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
92 108803504 const int a5 = -block[i+1*8] + (unsigned)block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
93 108803504 const int a7 = block[i+3*8] + (unsigned)block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
94
95 108803504 const int b1 = (a7>>2) + (unsigned)a1;
96 108803504 const int b3 = (unsigned)a3 + (a5>>2);
97 108803504 const int b5 = (a3>>2) - (unsigned)a5;
98 108803504 const int b7 = (unsigned)a7 - (a1>>2);
99
100 108803504 block[i+0*8] = b0 + b7;
101 108803504 block[i+7*8] = b0 - b7;
102 108803504 block[i+1*8] = b2 + b5;
103 108803504 block[i+6*8] = b2 - b5;
104 108803504 block[i+2*8] = b4 + b3;
105 108803504 block[i+5*8] = b4 - b3;
106 108803504 block[i+3*8] = b6 + b1;
107 108803504 block[i+4*8] = b6 - b1;
108 }
109
2/2
✓ Branch 0 taken 54401752 times.
✓ Branch 1 taken 6800219 times.
122403942 for( i = 0; i < 8; i++ )
110 {
111 108803504 const unsigned a0 = block[0+i*8] + (unsigned)block[4+i*8];
112 108803504 const unsigned a2 = block[0+i*8] - (unsigned)block[4+i*8];
113 108803504 const unsigned a4 = (block[2+i*8]>>1) - (unsigned)block[6+i*8];
114 108803504 const unsigned a6 = (block[6+i*8]>>1) + (unsigned)block[2+i*8];
115
116 108803504 const unsigned b0 = a0 + a6;
117 108803504 const unsigned b2 = a2 + a4;
118 108803504 const unsigned b4 = a2 - a4;
119 108803504 const unsigned b6 = a0 - a6;
120
121 108803504 const int a1 = -(unsigned)block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
122 108803504 const int a3 = (unsigned)block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
123 108803504 const int a5 = -(unsigned)block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
124 108803504 const int a7 = (unsigned)block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
125
126 108803504 const unsigned b1 = (a7>>2) + (unsigned)a1;
127 108803504 const unsigned b3 = (unsigned)a3 + (a5>>2);
128 108803504 const unsigned b5 = (a3>>2) - (unsigned)a5;
129 108803504 const unsigned b7 = (unsigned)a7 - (a1>>2);
130
131 108803504 dst[i + 0*stride] = av_clip_pixel( dst[i + 0*stride] + ((int)(b0 + b7) >> 6) );
132 108803504 dst[i + 1*stride] = av_clip_pixel( dst[i + 1*stride] + ((int)(b2 + b5) >> 6) );
133 108803504 dst[i + 2*stride] = av_clip_pixel( dst[i + 2*stride] + ((int)(b4 + b3) >> 6) );
134 108803504 dst[i + 3*stride] = av_clip_pixel( dst[i + 3*stride] + ((int)(b6 + b1) >> 6) );
135 108803504 dst[i + 4*stride] = av_clip_pixel( dst[i + 4*stride] + ((int)(b6 - b1) >> 6) );
136 108803504 dst[i + 5*stride] = av_clip_pixel( dst[i + 5*stride] + ((int)(b4 - b3) >> 6) );
137 108803504 dst[i + 6*stride] = av_clip_pixel( dst[i + 6*stride] + ((int)(b2 - b5) >> 6) );
138 108803504 dst[i + 7*stride] = av_clip_pixel( dst[i + 7*stride] + ((int)(b0 - b7) >> 6) );
139 }
140
141 13600438 memset(block, 0, 64 * sizeof(dctcoef));
142 13600438 }
143
144 // assumes all AC coefs are 0
145 58543434 void FUNCC(ff_h264_idct_dc_add)(uint8_t *_dst, int16_t *_block, ptrdiff_t stride)
146 {
147 int i, j;
148 58543434 pixel *dst = (pixel*)_dst;
149 58543434 dctcoef *block = (dctcoef*)_block;
150 58543434 int dc = (block[0] + 32) >> 6;
151 58543434 stride /= sizeof(pixel);
152 58543434 block[0] = 0;
153
2/2
✓ Branch 0 taken 117086868 times.
✓ Branch 1 taken 29271717 times.
292717170 for( j = 0; j < 4; j++ )
154 {
155
2/2
✓ Branch 0 taken 468347472 times.
✓ Branch 1 taken 117086868 times.
1170868680 for( i = 0; i < 4; i++ )
156 936694944 dst[i] = av_clip_pixel( dst[i] + dc );
157 234173736 dst += stride;
158 }
159 58543434 }
160
161 1254796 void FUNCC(ff_h264_idct8_dc_add)(uint8_t *_dst, int16_t *_block, ptrdiff_t stride)
162 {
163 int i, j;
164 1254796 pixel *dst = (pixel*)_dst;
165 1254796 dctcoef *block = (dctcoef*)_block;
166 1254796 int dc = (block[0] + 32) >> 6;
167 1254796 block[0] = 0;
168 1254796 stride /= sizeof(pixel);
169
2/2
✓ Branch 0 taken 5019184 times.
✓ Branch 1 taken 627398 times.
11293164 for( j = 0; j < 8; j++ )
170 {
171
2/2
✓ Branch 0 taken 40153472 times.
✓ Branch 1 taken 5019184 times.
90345312 for( i = 0; i < 8; i++ )
172 80306944 dst[i] = av_clip_pixel( dst[i] + dc );
173 10038368 dst += stride;
174 }
175 1254796 }
176
177 9790570 void FUNCC(ff_h264_idct_add16)(uint8_t *dst, const int *block_offset,
178 int16_t *block, ptrdiff_t stride,
179 const uint8_t nnzc[5 * 8])
180 {
181 int i;
182
2/2
✓ Branch 0 taken 78324560 times.
✓ Branch 1 taken 4895285 times.
166439690 for(i=0; i<16; i++){
183 156649120 int nnz = nnzc[ scan8[i] ];
184
2/2
✓ Branch 0 taken 29316176 times.
✓ Branch 1 taken 49008384 times.
156649120 if(nnz){
185
4/4
✓ Branch 0 taken 13237586 times.
✓ Branch 1 taken 16078590 times.
✓ Branch 2 taken 4676646 times.
✓ Branch 3 taken 8560940 times.
58632352 if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
186 49279060 else FUNCC(ff_h264_idct_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
187 }
188 }
189 9790570 }
190
191 1979660 void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset,
192 int16_t *block, ptrdiff_t stride,
193 const uint8_t nnzc[5 * 8])
194 {
195 int i;
196
2/2
✓ Branch 0 taken 15837280 times.
✓ Branch 1 taken 989830 times.
33654220 for(i=0; i<16; i++){
197
2/2
✓ Branch 0 taken 3765864 times.
✓ Branch 1 taken 12071416 times.
31674560 if(nnzc[ scan8[i] ]) FUNCC(ff_h264_idct_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
198
2/2
✓ Branch 0 taken 5056331 times.
✓ Branch 1 taken 7015085 times.
24142832 else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
199 }
200 1979660 }
201
202 1844124 void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset,
203 int16_t *block, ptrdiff_t stride,
204 const uint8_t nnzc[5 * 8])
205 {
206 int i;
207
2/2
✓ Branch 0 taken 3688248 times.
✓ Branch 1 taken 922062 times.
9220620 for(i=0; i<16; i+=4){
208 7376496 int nnz = nnzc[ scan8[i] ];
209
2/2
✓ Branch 0 taken 2334959 times.
✓ Branch 1 taken 1353289 times.
7376496 if(nnz){
210
4/4
✓ Branch 0 taken 700112 times.
✓ Branch 1 taken 1634847 times.
✓ Branch 2 taken 383423 times.
✓ Branch 3 taken 316689 times.
4669918 if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
211 3903072 else FUNCC(ff_h264_idct8_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
212 }
213 }
214 1844124 }
215
216 9054334 void FUNCC(ff_h264_idct_add8)(uint8_t **dest, const int *block_offset, int16_t *block,
217 ptrdiff_t stride, const uint8_t nnzc[15*8])
218 {
219 int i, j;
220
2/2
✓ Branch 0 taken 9054334 times.
✓ Branch 1 taken 4527167 times.
27163002 for(j=1; j<3; j++){
221
2/2
✓ Branch 0 taken 36217336 times.
✓ Branch 1 taken 9054334 times.
90543340 for(i=j*16; i<j*16+4; i++){
222
2/2
✓ Branch 0 taken 8312605 times.
✓ Branch 1 taken 27904731 times.
72434672 if(nnzc[ scan8[i] ])
223 16625210 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
224
2/2
✓ Branch 0 taken 12840953 times.
✓ Branch 1 taken 15063778 times.
55809462 else if(((dctcoef*)block)[i*16])
225 25681906 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
226 }
227 }
228 9054334 }
229
230 1350636 void FUNCC(ff_h264_idct_add8_422)(uint8_t **dest, const int *block_offset,
231 int16_t *block, ptrdiff_t stride, const uint8_t nnzc[15*8])
232 {
233 int i, j;
234
235
2/2
✓ Branch 0 taken 1350636 times.
✓ Branch 1 taken 675318 times.
4051908 for(j=1; j<3; j++){
236
2/2
✓ Branch 0 taken 5402544 times.
✓ Branch 1 taken 1350636 times.
13506360 for(i=j*16; i<j*16+4; i++){
237
2/2
✓ Branch 0 taken 1820857 times.
✓ Branch 1 taken 3581687 times.
10805088 if(nnzc[ scan8[i] ])
238 3641714 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
239
2/2
✓ Branch 0 taken 2290895 times.
✓ Branch 1 taken 1290792 times.
7163374 else if(((dctcoef*)block)[i*16])
240 4581790 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
241 }
242 }
243
244
2/2
✓ Branch 0 taken 1350636 times.
✓ Branch 1 taken 675318 times.
4051908 for(j=1; j<3; j++){
245
2/2
✓ Branch 0 taken 5402544 times.
✓ Branch 1 taken 1350636 times.
13506360 for(i=j*16+4; i<j*16+8; i++){
246
2/2
✓ Branch 0 taken 1845890 times.
✓ Branch 1 taken 3556654 times.
10805088 if(nnzc[ scan8[i+4] ])
247 3691780 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i+4], block + i*16*sizeof(pixel), stride);
248
2/2
✓ Branch 0 taken 2323433 times.
✓ Branch 1 taken 1233221 times.
7113308 else if(((dctcoef*)block)[i*16])
249 4646866 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i+4], block + i*16*sizeof(pixel), stride);
250 }
251 }
252 1350636 }
253
254 #if BIT_DEPTH == 8 || BIT_DEPTH == 9
255 /**
256 * IDCT transforms the 16 dc values and dequantizes them.
257 * @param qmul quantization parameter
258 */
259 1193860 void FUNCC2(ff_h264_luma_dc_dequant_idct)(int16_t *_output, int16_t *_input, int qmul)
260 {
261 #define stride 16
262 int i;
263 int temp[16];
264 static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
265 1193860 dctcoef *input = (dctcoef*)_input;
266 1193860 dctcoef *output = (dctcoef*)_output;
267
268
2/2
✓ Branch 0 taken 2387720 times.
✓ Branch 1 taken 596930 times.
5969300 for(i=0; i<4; i++){
269 4775440 const int z0= input[4*i+0] + input[4*i+1];
270 4775440 const int z1= input[4*i+0] - input[4*i+1];
271 4775440 const int z2= input[4*i+2] - input[4*i+3];
272 4775440 const int z3= input[4*i+2] + input[4*i+3];
273
274 4775440 temp[4*i+0]= z0+z3;
275 4775440 temp[4*i+1]= z0-z3;
276 4775440 temp[4*i+2]= z1-z2;
277 4775440 temp[4*i+3]= z1+z2;
278 }
279
280
2/2
✓ Branch 0 taken 2387720 times.
✓ Branch 1 taken 596930 times.
5969300 for(i=0; i<4; i++){
281 4775440 const int offset= x_offset[i];
282 4775440 const SUINT z0= temp[4*0+i] + temp[4*2+i];
283 4775440 const SUINT z1= temp[4*0+i] - temp[4*2+i];
284 4775440 const SUINT z2= temp[4*1+i] - temp[4*3+i];
285 4775440 const SUINT z3= temp[4*1+i] + temp[4*3+i];
286
287 4775440 output[stride* 0+offset]= (int)((z0 + z3)*qmul + 128 ) >> 8;
288 4775440 output[stride* 1+offset]= (int)((z1 + z2)*qmul + 128 ) >> 8;
289 4775440 output[stride* 4+offset]= (int)((z1 - z2)*qmul + 128 ) >> 8;
290 4775440 output[stride* 5+offset]= (int)((z0 - z3)*qmul + 128 ) >> 8;
291 }
292 #undef stride
293 1193860 }
294
295 2252062 void FUNCC2(ff_h264_chroma422_dc_dequant_idct)(int16_t *_block, int qmul)
296 {
297 2252062 const int stride= 16*2;
298 2252062 const int xStride= 16;
299 int i;
300 unsigned temp[8];
301 static const uint8_t x_offset[2]={0, 16};
302 2252062 dctcoef *block = (dctcoef*)_block;
303
304
2/2
✓ Branch 0 taken 4504124 times.
✓ Branch 1 taken 1126031 times.
11260310 for(i=0; i<4; i++){
305 9008248 temp[2*i+0] = block[stride*i + xStride*0] + (unsigned)block[stride*i + xStride*1];
306 9008248 temp[2*i+1] = block[stride*i + xStride*0] - (unsigned)block[stride*i + xStride*1];
307 }
308
309
2/2
✓ Branch 0 taken 2252062 times.
✓ Branch 1 taken 1126031 times.
6756186 for(i=0; i<2; i++){
310 4504124 const int offset= x_offset[i];
311 4504124 const SUINT z0= temp[2*0+i] + temp[2*2+i];
312 4504124 const SUINT z1= temp[2*0+i] - temp[2*2+i];
313 4504124 const SUINT z2= temp[2*1+i] - temp[2*3+i];
314 4504124 const SUINT z3= temp[2*1+i] + temp[2*3+i];
315
316 4504124 block[stride*0+offset]= (int)((z0 + z3)*qmul + 128) >> 8;
317 4504124 block[stride*1+offset]= (int)((z1 + z2)*qmul + 128) >> 8;
318 4504124 block[stride*2+offset]= (int)((z1 - z2)*qmul + 128) >> 8;
319 4504124 block[stride*3+offset]= (int)((z0 - z3)*qmul + 128) >> 8;
320 }
321 2252062 }
322
323 13179610 void FUNCC2(ff_h264_chroma_dc_dequant_idct)(int16_t *_block, int qmul)
324 {
325 13179610 const int stride= 16*2;
326 13179610 const int xStride= 16;
327 SUINT a,b,c,d,e;
328 13179610 dctcoef *block = (dctcoef*)_block;
329
330 13179610 a= block[stride*0 + xStride*0];
331 13179610 b= block[stride*0 + xStride*1];
332 13179610 c= block[stride*1 + xStride*0];
333 13179610 d= block[stride*1 + xStride*1];
334
335 13179610 e= a-b;
336 13179610 a= a+b;
337 13179610 b= c-d;
338 13179610 c= c+d;
339
340 13179610 block[stride*0 + xStride*0]= (int)((a+c)*qmul) >> 7;
341 13179610 block[stride*0 + xStride*1]= (int)((e+b)*qmul) >> 7;
342 13179610 block[stride*1 + xStride*0]= (int)((a-c)*qmul) >> 7;
343 13179610 block[stride*1 + xStride*1]= (int)((e-b)*qmul) >> 7;
344 13179610 }
345 #endif
346