FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_unquantize.c
Date: 2025-12-07 22:02:57
Exec Total Coverage
Lines: 143 143 100.0%
Functions: 9 9 100.0%
Branches: 63 66 95.5%

Line Branch Exec Source
1 /*
2 * Unquantize functions for mpegvideo
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <stdint.h>
26
27 #include "config.h"
28
29 #include "libavutil/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "avcodec.h"
32 #include "mpegvideo.h"
33 #include "mpegvideodata.h"
34 #include "mpegvideo_unquantize.h"
35
36 5477 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
37 const uint8_t *src_scantable)
38 {
39 5477 st->scantable = src_scantable;
40
41
2/2
✓ Branch 0 taken 350528 times.
✓ Branch 1 taken 5477 times.
356005 for (int i = 0, end = -1; i < 64; i++) {
42 350528 int j = src_scantable[i];
43 350528 st->permutated[i] = permutation[j];
44
2/2
✓ Branch 0 taken 103201 times.
✓ Branch 1 taken 247327 times.
350528 if (permutation[j] > end)
45 103201 end = permutation[j];
46 350528 st->raster_end[i] = end;
47 }
48 5477 }
49
50 126027 static void dct_unquantize_mpeg1_intra_c(const MPVContext *s,
51 int16_t *block, int n, int qscale)
52 {
53 int i, level, nCoeffs;
54 const uint16_t *quant_matrix;
55
56 126027 nCoeffs= s->block_last_index[n];
57
58
2/2
✓ Branch 0 taken 84016 times.
✓ Branch 1 taken 42011 times.
126027 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
59 /* XXX: only MPEG-1 */
60 126027 quant_matrix = s->intra_matrix;
61
2/2
✓ Branch 0 taken 2913550 times.
✓ Branch 1 taken 126027 times.
3039577 for(i=1;i<=nCoeffs;i++) {
62 2913550 int j= s->intra_scantable.permutated[i];
63 2913550 level = block[j];
64
2/2
✓ Branch 0 taken 1214980 times.
✓ Branch 1 taken 1698570 times.
2913550 if (level) {
65
2/2
✓ Branch 0 taken 604319 times.
✓ Branch 1 taken 610661 times.
1214980 if (level < 0) {
66 604319 level = -level;
67 604319 level = (int)(level * qscale * quant_matrix[j]) >> 3;
68 604319 level = (level - 1) | 1;
69 604319 level = -level;
70 } else {
71 610661 level = (int)(level * qscale * quant_matrix[j]) >> 3;
72 610661 level = (level - 1) | 1;
73 }
74 1214980 block[j] = level;
75 }
76 }
77 126027 }
78
79 285602 static void dct_unquantize_mpeg1_inter_c(const MPVContext *s,
80 int16_t *block, int n, int qscale)
81 {
82 int i, level, nCoeffs;
83 const uint16_t *quant_matrix;
84
85 285602 nCoeffs= s->block_last_index[n];
86
87 285602 quant_matrix = s->inter_matrix;
88
2/2
✓ Branch 0 taken 9213137 times.
✓ Branch 1 taken 285602 times.
9498739 for(i=0; i<=nCoeffs; i++) {
89 9213137 int j= s->intra_scantable.permutated[i];
90 9213137 level = block[j];
91
2/2
✓ Branch 0 taken 2101031 times.
✓ Branch 1 taken 7112106 times.
9213137 if (level) {
92
2/2
✓ Branch 0 taken 1051959 times.
✓ Branch 1 taken 1049072 times.
2101031 if (level < 0) {
93 1051959 level = -level;
94 1051959 level = (((level << 1) + 1) * qscale *
95 1051959 ((int) (quant_matrix[j]))) >> 4;
96 1051959 level = (level - 1) | 1;
97 1051959 level = -level;
98 } else {
99 1049072 level = (((level << 1) + 1) * qscale *
100 1049072 ((int) (quant_matrix[j]))) >> 4;
101 1049072 level = (level - 1) | 1;
102 }
103 2101031 block[j] = level;
104 }
105 }
106 285602 }
107
108 131268 static void dct_unquantize_mpeg2_intra_c(const MPVContext *s,
109 int16_t *block, int n, int qscale)
110 {
111 int i, level, nCoeffs;
112 const uint16_t *quant_matrix;
113
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131268 times.
131268 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
115 131268 else qscale <<= 1;
116
117 131268 nCoeffs= s->block_last_index[n];
118
119
2/2
✓ Branch 0 taken 87512 times.
✓ Branch 1 taken 43756 times.
131268 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
120 131268 quant_matrix = s->intra_matrix;
121
2/2
✓ Branch 0 taken 462435 times.
✓ Branch 1 taken 131268 times.
593703 for(i=1;i<=nCoeffs;i++) {
122 462435 int j= s->intra_scantable.permutated[i];
123 462435 level = block[j];
124
2/2
✓ Branch 0 taken 152199 times.
✓ Branch 1 taken 310236 times.
462435 if (level) {
125
2/2
✓ Branch 0 taken 74700 times.
✓ Branch 1 taken 77499 times.
152199 if (level < 0) {
126 74700 level = -level;
127 74700 level = (int)(level * qscale * quant_matrix[j]) >> 4;
128 74700 level = -level;
129 } else {
130 77499 level = (int)(level * qscale * quant_matrix[j]) >> 4;
131 }
132 152199 block[j] = level;
133 }
134 }
135 131268 }
136
137 1267444 static void dct_unquantize_mpeg2_intra_bitexact(const MPVContext *s,
138 int16_t *block, int n, int qscale)
139 {
140 int i, level, nCoeffs;
141 const uint16_t *quant_matrix;
142 1267444 int sum=-1;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267444 times.
1267444 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
145 1267444 else qscale <<= 1;
146
147 1267444 nCoeffs= s->block_last_index[n];
148
149
2/2
✓ Branch 0 taken 793282 times.
✓ Branch 1 taken 474162 times.
1267444 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
150 1267444 sum += block[0];
151 1267444 quant_matrix = s->intra_matrix;
152
2/2
✓ Branch 0 taken 21672278 times.
✓ Branch 1 taken 1267444 times.
22939722 for(i=1;i<=nCoeffs;i++) {
153 21672278 int j= s->intra_scantable.permutated[i];
154 21672278 level = block[j];
155
2/2
✓ Branch 0 taken 10640441 times.
✓ Branch 1 taken 11031837 times.
21672278 if (level) {
156
2/2
✓ Branch 0 taken 5352348 times.
✓ Branch 1 taken 5288093 times.
10640441 if (level < 0) {
157 5352348 level = -level;
158 5352348 level = (int)(level * qscale * quant_matrix[j]) >> 4;
159 5352348 level = -level;
160 } else {
161 5288093 level = (int)(level * qscale * quant_matrix[j]) >> 4;
162 }
163 10640441 block[j] = level;
164 10640441 sum+=level;
165 }
166 }
167 1267444 block[63]^=sum&1;
168 1267444 }
169
170 3083300 static void dct_unquantize_mpeg2_inter_c(const MPVContext *s,
171 int16_t *block, int n, int qscale)
172 {
173 int i, level, nCoeffs;
174 const uint16_t *quant_matrix;
175 3083300 int sum=-1;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3083300 times.
3083300 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
178 3083300 else qscale <<= 1;
179
180 3083300 nCoeffs= s->block_last_index[n];
181
182 3083300 quant_matrix = s->inter_matrix;
183
2/2
✓ Branch 0 taken 72796464 times.
✓ Branch 1 taken 3083300 times.
75879764 for(i=0; i<=nCoeffs; i++) {
184 72796464 int j= s->intra_scantable.permutated[i];
185 72796464 level = block[j];
186
2/2
✓ Branch 0 taken 26322475 times.
✓ Branch 1 taken 46473989 times.
72796464 if (level) {
187
2/2
✓ Branch 0 taken 13200558 times.
✓ Branch 1 taken 13121917 times.
26322475 if (level < 0) {
188 13200558 level = -level;
189 13200558 level = (((level << 1) + 1) * qscale *
190 13200558 ((int) (quant_matrix[j]))) >> 5;
191 13200558 level = -level;
192 } else {
193 13121917 level = (((level << 1) + 1) * qscale *
194 13121917 ((int) (quant_matrix[j]))) >> 5;
195 }
196 26322475 block[j] = level;
197 26322475 sum+=level;
198 }
199 }
200 3083300 block[63]^=sum&1;
201 3083300 }
202
203 3308547 static void dct_unquantize_h263_intra_c(const MPVContext *s,
204 int16_t *block, int n, int qscale)
205 {
206 int i, level, qmul, qadd;
207 int nCoeffs;
208
209 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
210
211 3308547 qmul = qscale << 1;
212
213
2/2
✓ Branch 0 taken 3135351 times.
✓ Branch 1 taken 173196 times.
3308547 if (!s->h263_aic) {
214
2/2
✓ Branch 0 taken 2090235 times.
✓ Branch 1 taken 1045116 times.
3135351 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
215 3135351 qadd = (qscale - 1) | 1;
216 }else{
217 173196 qadd = 0;
218 }
219
2/2
✓ Branch 0 taken 68307 times.
✓ Branch 1 taken 3240240 times.
3308547 if(s->ac_pred)
220 68307 nCoeffs=63;
221 else
222 3240240 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
223
224
2/2
✓ Branch 0 taken 111855768 times.
✓ Branch 1 taken 3308547 times.
115164315 for(i=1; i<=nCoeffs; i++) {
225 111855768 level = block[i];
226
2/2
✓ Branch 0 taken 35463861 times.
✓ Branch 1 taken 76391907 times.
111855768 if (level) {
227
2/2
✓ Branch 0 taken 17755040 times.
✓ Branch 1 taken 17708821 times.
35463861 if (level < 0) {
228 17755040 level = level * qmul - qadd;
229 } else {
230 17708821 level = level * qmul + qadd;
231 }
232 35463861 block[i] = level;
233 }
234 }
235 3308547 }
236
237 5789704 static void dct_unquantize_h263_inter_c(const MPVContext *s,
238 int16_t *block, int n, int qscale)
239 {
240 int i, level, qmul, qadd;
241 int nCoeffs;
242
243 av_assert2(s->block_last_index[n]>=0);
244
245 5789704 qadd = (qscale - 1) | 1;
246 5789704 qmul = qscale << 1;
247
248 5789704 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
249
250
2/2
✓ Branch 0 taken 222003248 times.
✓ Branch 1 taken 5789704 times.
227792952 for(i=0; i<=nCoeffs; i++) {
251 222003248 level = block[i];
252
2/2
✓ Branch 0 taken 46310074 times.
✓ Branch 1 taken 175693174 times.
222003248 if (level) {
253
2/2
✓ Branch 0 taken 23077412 times.
✓ Branch 1 taken 23232662 times.
46310074 if (level < 0) {
254 23077412 level = level * qmul - qadd;
255 } else {
256 23232662 level = level * qmul + qadd;
257 }
258 46310074 block[i] = level;
259 }
260 }
261 5789704 }
262
263 695 av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s,
264 int bitexact, int q_scale_type)
265 {
266 695 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
267 695 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
268 695 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
269 695 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
270 695 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
271
2/2
✓ Branch 0 taken 583 times.
✓ Branch 1 taken 112 times.
695 if (bitexact)
272 583 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
273 695 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
274
275 #if HAVE_INTRINSICS_NEON
276 ff_mpv_unquantize_init_neon(s, bitexact);
277 #endif
278
279 #if ARCH_ARM
280 ff_mpv_unquantize_init_arm(s, bitexact);
281 #elif ARCH_PPC
282 ff_mpv_unquantize_init_ppc(s, bitexact);
283 #elif ARCH_RISCV
284 ff_mpv_unquantize_init_riscv(s, bitexact);
285 #elif ARCH_X86
286 695 ff_mpv_unquantize_init_x86(s, bitexact);
287 #elif ARCH_MIPS
288 ff_mpv_unquantize_init_mips(s, bitexact, q_scale_type);
289 #endif
290 695 }
291