FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_unquantize.c
Date: 2026-06-06 18:10:07
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 5469 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
37 const uint8_t *src_scantable)
38 {
39 5469 st->scantable = src_scantable;
40
41
2/2
✓ Branch 0 taken 350016 times.
✓ Branch 1 taken 5469 times.
355485 for (int i = 0, end = -1; i < 64; i++) {
42 350016 int j = src_scantable[i];
43 350016 st->permutated[i] = permutation[j];
44
2/2
✓ Branch 0 taken 103078 times.
✓ Branch 1 taken 246938 times.
350016 if (permutation[j] > end)
45 103078 end = permutation[j];
46 350016 st->raster_end[i] = end;
47 }
48 5469 }
49
50 126028 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 126028 nCoeffs= s->block_last_index[n];
57
58
2/2
✓ Branch 0 taken 84020 times.
✓ Branch 1 taken 42008 times.
126028 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
59 /* XXX: only MPEG-1 */
60 126028 quant_matrix = s->intra_matrix;
61
2/2
✓ Branch 0 taken 2913558 times.
✓ Branch 1 taken 126028 times.
3039586 for(i=1;i<=nCoeffs;i++) {
62 2913558 int j= s->intra_scantable.permutated[i];
63 2913558 level = block[j];
64
2/2
✓ Branch 0 taken 1214984 times.
✓ Branch 1 taken 1698574 times.
2913558 if (level) {
65
2/2
✓ Branch 0 taken 604326 times.
✓ Branch 1 taken 610658 times.
1214984 if (level < 0) {
66 604326 level = -level;
67 604326 level = (int)(level * qscale * quant_matrix[j]) >> 3;
68 604326 level = (level - 1) | 1;
69 604326 level = -level;
70 } else {
71 610658 level = (int)(level * qscale * quant_matrix[j]) >> 3;
72 610658 level = (level - 1) | 1;
73 }
74 1214984 block[j] = level;
75 }
76 }
77 126028 }
78
79 285603 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 285603 nCoeffs= s->block_last_index[n];
86
87 285603 quant_matrix = s->inter_matrix;
88
2/2
✓ Branch 0 taken 9213146 times.
✓ Branch 1 taken 285603 times.
9498749 for(i=0; i<=nCoeffs; i++) {
89 9213146 int j= s->intra_scantable.permutated[i];
90 9213146 level = block[j];
91
2/2
✓ Branch 0 taken 2101024 times.
✓ Branch 1 taken 7112122 times.
9213146 if (level) {
92
2/2
✓ Branch 0 taken 1051956 times.
✓ Branch 1 taken 1049068 times.
2101024 if (level < 0) {
93 1051956 level = -level;
94 1051956 level = (((level << 1) + 1) * qscale *
95 1051956 ((int) (quant_matrix[j]))) >> 4;
96 1051956 level = (level - 1) | 1;
97 1051956 level = -level;
98 } else {
99 1049068 level = (((level << 1) + 1) * qscale *
100 1049068 ((int) (quant_matrix[j]))) >> 4;
101 1049068 level = (level - 1) | 1;
102 }
103 2101024 block[j] = level;
104 }
105 }
106 285603 }
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 1267127 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 1267127 int sum=-1;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1267127 times.
1267127 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
145 1267127 else qscale <<= 1;
146
147 1267127 nCoeffs= s->block_last_index[n];
148
149
2/2
✓ Branch 0 taken 793071 times.
✓ Branch 1 taken 474056 times.
1267127 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
150 1267127 sum += block[0];
151 1267127 quant_matrix = s->intra_matrix;
152
2/2
✓ Branch 0 taken 21662089 times.
✓ Branch 1 taken 1267127 times.
22929216 for(i=1;i<=nCoeffs;i++) {
153 21662089 int j= s->intra_scantable.permutated[i];
154 21662089 level = block[j];
155
2/2
✓ Branch 0 taken 10636357 times.
✓ Branch 1 taken 11025732 times.
21662089 if (level) {
156
2/2
✓ Branch 0 taken 5350363 times.
✓ Branch 1 taken 5285994 times.
10636357 if (level < 0) {
157 5350363 level = -level;
158 5350363 level = (int)(level * qscale * quant_matrix[j]) >> 4;
159 5350363 level = -level;
160 } else {
161 5285994 level = (int)(level * qscale * quant_matrix[j]) >> 4;
162 }
163 10636357 block[j] = level;
164 10636357 sum+=level;
165 }
166 }
167 1267127 block[63]^=sum&1;
168 1267127 }
169
170 3083598 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 3083598 int sum=-1;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3083598 times.
3083598 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
178 3083598 else qscale <<= 1;
179
180 3083598 nCoeffs= s->block_last_index[n];
181
182 3083598 quant_matrix = s->inter_matrix;
183
2/2
✓ Branch 0 taken 72805040 times.
✓ Branch 1 taken 3083598 times.
75888638 for(i=0; i<=nCoeffs; i++) {
184 72805040 int j= s->intra_scantable.permutated[i];
185 72805040 level = block[j];
186
2/2
✓ Branch 0 taken 26321593 times.
✓ Branch 1 taken 46483447 times.
72805040 if (level) {
187
2/2
✓ Branch 0 taken 13200021 times.
✓ Branch 1 taken 13121572 times.
26321593 if (level < 0) {
188 13200021 level = -level;
189 13200021 level = (((level << 1) + 1) * qscale *
190 13200021 ((int) (quant_matrix[j]))) >> 5;
191 13200021 level = -level;
192 } else {
193 13121572 level = (((level << 1) + 1) * qscale *
194 13121572 ((int) (quant_matrix[j]))) >> 5;
195 }
196 26321593 block[j] = level;
197 26321593 sum+=level;
198 }
199 }
200 3083598 block[63]^=sum&1;
201 3083598 }
202
203 3298000 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 3298000 qmul = qscale << 1;
212
213
2/2
✓ Branch 0 taken 3124804 times.
✓ Branch 1 taken 173196 times.
3298000 if (!s->h263_aic) {
214
2/2
✓ Branch 0 taken 2083203 times.
✓ Branch 1 taken 1041601 times.
3124804 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
215 3124804 qadd = (qscale - 1) | 1;
216 }else{
217 173196 qadd = 0;
218 }
219
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3229696 times.
3298000 if(s->ac_pred)
220 68304 nCoeffs=63;
221 else
222 3229696 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
223
224
2/2
✓ Branch 0 taken 111440243 times.
✓ Branch 1 taken 3298000 times.
114738243 for(i=1; i<=nCoeffs; i++) {
225 111440243 level = block[i];
226
2/2
✓ Branch 0 taken 35320968 times.
✓ Branch 1 taken 76119275 times.
111440243 if (level) {
227
2/2
✓ Branch 0 taken 17683307 times.
✓ Branch 1 taken 17637661 times.
35320968 if (level < 0) {
228 17683307 level = level * qmul - qadd;
229 } else {
230 17637661 level = level * qmul + qadd;
231 }
232 35320968 block[i] = level;
233 }
234 }
235 3298000 }
236
237 5770573 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 5770573 qadd = (qscale - 1) | 1;
246 5770573 qmul = qscale << 1;
247
248 5770573 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
249
250
2/2
✓ Branch 0 taken 221258063 times.
✓ Branch 1 taken 5770573 times.
227028636 for(i=0; i<=nCoeffs; i++) {
251 221258063 level = block[i];
252
2/2
✓ Branch 0 taken 46108328 times.
✓ Branch 1 taken 175149735 times.
221258063 if (level) {
253
2/2
✓ Branch 0 taken 22977094 times.
✓ Branch 1 taken 23131234 times.
46108328 if (level < 0) {
254 22977094 level = level * qmul - qadd;
255 } else {
256 23131234 level = level * qmul + qadd;
257 }
258 46108328 block[i] = level;
259 }
260 }
261 5770573 }
262
263 709 av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s,
264 int bitexact, int q_scale_type)
265 {
266 709 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
267 709 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
268 709 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
269 709 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
270 709 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
271
2/2
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 121 times.
709 if (bitexact)
272 588 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
273 709 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 709 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 709 }
291