FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_unquantize.c
Date: 2026-03-13 22:30:28
Exec Total Coverage
Lines: 143 143 100.0%
Functions: 9 9 100.0%
Branches: 64 66 97.0%

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 5451 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
37 const uint8_t *src_scantable)
38 {
39 5451 st->scantable = src_scantable;
40
41
2/2
✓ Branch 0 taken 348864 times.
✓ Branch 1 taken 5451 times.
354315 for (int i = 0, end = -1; i < 64; i++) {
42 348864 int j = src_scantable[i];
43 348864 st->permutated[i] = permutation[j];
44
2/2
✓ Branch 0 taken 102787 times.
✓ Branch 1 taken 246077 times.
348864 if (permutation[j] > end)
45 102787 end = permutation[j];
46 348864 st->raster_end[i] = end;
47 }
48 5451 }
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 84018 times.
✓ Branch 1 taken 42009 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 2913601 times.
✓ Branch 1 taken 126027 times.
3039628 for(i=1;i<=nCoeffs;i++) {
62 2913601 int j= s->intra_scantable.permutated[i];
63 2913601 level = block[j];
64
2/2
✓ Branch 0 taken 1215002 times.
✓ Branch 1 taken 1698599 times.
2913601 if (level) {
65
2/2
✓ Branch 0 taken 604342 times.
✓ Branch 1 taken 610660 times.
1215002 if (level < 0) {
66 604342 level = -level;
67 604342 level = (int)(level * qscale * quant_matrix[j]) >> 3;
68 604342 level = (level - 1) | 1;
69 604342 level = -level;
70 } else {
71 610660 level = (int)(level * qscale * quant_matrix[j]) >> 3;
72 610660 level = (level - 1) | 1;
73 }
74 1215002 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 9213188 times.
✓ Branch 1 taken 285602 times.
9498790 for(i=0; i<=nCoeffs; i++) {
89 9213188 int j= s->intra_scantable.permutated[i];
90 9213188 level = block[j];
91
2/2
✓ Branch 0 taken 2101046 times.
✓ Branch 1 taken 7112142 times.
9213188 if (level) {
92
2/2
✓ Branch 0 taken 1051958 times.
✓ Branch 1 taken 1049088 times.
2101046 if (level < 0) {
93 1051958 level = -level;
94 1051958 level = (((level << 1) + 1) * qscale *
95 1051958 ((int) (quant_matrix[j]))) >> 4;
96 1051958 level = (level - 1) | 1;
97 1051958 level = -level;
98 } else {
99 1049088 level = (((level << 1) + 1) * qscale *
100 1049088 ((int) (quant_matrix[j]))) >> 4;
101 1049088 level = (level - 1) | 1;
102 }
103 2101046 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 1267126 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 1267126 int sum=-1;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267126 times.
1267126 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
145 1267126 else qscale <<= 1;
146
147 1267126 nCoeffs= s->block_last_index[n];
148
149
2/2
✓ Branch 0 taken 793068 times.
✓ Branch 1 taken 474058 times.
1267126 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
150 1267126 sum += block[0];
151 1267126 quant_matrix = s->intra_matrix;
152
2/2
✓ Branch 0 taken 21662112 times.
✓ Branch 1 taken 1267126 times.
22929238 for(i=1;i<=nCoeffs;i++) {
153 21662112 int j= s->intra_scantable.permutated[i];
154 21662112 level = block[j];
155
2/2
✓ Branch 0 taken 10636377 times.
✓ Branch 1 taken 11025735 times.
21662112 if (level) {
156
2/2
✓ Branch 0 taken 5350368 times.
✓ Branch 1 taken 5286009 times.
10636377 if (level < 0) {
157 5350368 level = -level;
158 5350368 level = (int)(level * qscale * quant_matrix[j]) >> 4;
159 5350368 level = -level;
160 } else {
161 5286009 level = (int)(level * qscale * quant_matrix[j]) >> 4;
162 }
163 10636377 block[j] = level;
164 10636377 sum+=level;
165 }
166 }
167 1267126 block[63]^=sum&1;
168 1267126 }
169
170 3083597 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 3083597 int sum=-1;
176
177
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3083596 times.
3083597 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
178 3083596 else qscale <<= 1;
179
180 3083597 nCoeffs= s->block_last_index[n];
181
182 3083597 quant_matrix = s->inter_matrix;
183
2/2
✓ Branch 0 taken 72805082 times.
✓ Branch 1 taken 3083597 times.
75888679 for(i=0; i<=nCoeffs; i++) {
184 72805082 int j= s->intra_scantable.permutated[i];
185 72805082 level = block[j];
186
2/2
✓ Branch 0 taken 26321602 times.
✓ Branch 1 taken 46483480 times.
72805082 if (level) {
187
2/2
✓ Branch 0 taken 13200015 times.
✓ Branch 1 taken 13121587 times.
26321602 if (level < 0) {
188 13200015 level = -level;
189 13200015 level = (((level << 1) + 1) * qscale *
190 13200015 ((int) (quant_matrix[j]))) >> 5;
191 13200015 level = -level;
192 } else {
193 13121587 level = (((level << 1) + 1) * qscale *
194 13121587 ((int) (quant_matrix[j]))) >> 5;
195 }
196 26321602 block[j] = level;
197 26321602 sum+=level;
198 }
199 }
200 3083597 block[63]^=sum&1;
201 3083597 }
202
203 3297999 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 3297999 qmul = qscale << 1;
212
213
2/2
✓ Branch 0 taken 3124803 times.
✓ Branch 1 taken 173196 times.
3297999 if (!s->h263_aic) {
214
2/2
✓ Branch 0 taken 2083200 times.
✓ Branch 1 taken 1041603 times.
3124803 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
215 3124803 qadd = (qscale - 1) | 1;
216 }else{
217 173196 qadd = 0;
218 }
219
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3229695 times.
3297999 if(s->ac_pred)
220 68304 nCoeffs=63;
221 else
222 3229695 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
223
224
2/2
✓ Branch 0 taken 111440222 times.
✓ Branch 1 taken 3297999 times.
114738221 for(i=1; i<=nCoeffs; i++) {
225 111440222 level = block[i];
226
2/2
✓ Branch 0 taken 35320992 times.
✓ Branch 1 taken 76119230 times.
111440222 if (level) {
227
2/2
✓ Branch 0 taken 17683323 times.
✓ Branch 1 taken 17637669 times.
35320992 if (level < 0) {
228 17683323 level = level * qmul - qadd;
229 } else {
230 17637669 level = level * qmul + qadd;
231 }
232 35320992 block[i] = level;
233 }
234 }
235 3297999 }
236
237 5770572 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 5770572 qadd = (qscale - 1) | 1;
246 5770572 qmul = qscale << 1;
247
248 5770572 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
249
250
2/2
✓ Branch 0 taken 221258041 times.
✓ Branch 1 taken 5770572 times.
227028613 for(i=0; i<=nCoeffs; i++) {
251 221258041 level = block[i];
252
2/2
✓ Branch 0 taken 46108350 times.
✓ Branch 1 taken 175149691 times.
221258041 if (level) {
253
2/2
✓ Branch 0 taken 22977106 times.
✓ Branch 1 taken 23131244 times.
46108350 if (level < 0) {
254 22977106 level = level * qmul - qadd;
255 } else {
256 23131244 level = level * qmul + qadd;
257 }
258 46108350 block[i] = level;
259 }
260 }
261 5770572 }
262
263 700 av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s,
264 int bitexact, int q_scale_type)
265 {
266 700 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
267 700 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
268 700 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
269 700 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
270 700 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
271
2/2
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 112 times.
700 if (bitexact)
272 588 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
273 700 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 700 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 700 }
291