FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_unquantize.c
Date: 2025-04-02 21:09:40
Exec Total Coverage
Lines: 134 134 100.0%
Functions: 8 8 100.0%
Branches: 59 62 95.2%

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 126024 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
37 int16_t *block, int n, int qscale)
38 {
39 int i, level, nCoeffs;
40 const uint16_t *quant_matrix;
41
42 126024 nCoeffs= s->block_last_index[n];
43
44
2/2
✓ Branch 0 taken 84016 times.
✓ Branch 1 taken 42008 times.
126024 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
45 /* XXX: only MPEG-1 */
46 126024 quant_matrix = s->intra_matrix;
47
2/2
✓ Branch 0 taken 2913490 times.
✓ Branch 1 taken 126024 times.
3039514 for(i=1;i<=nCoeffs;i++) {
48 2913490 int j= s->intra_scantable.permutated[i];
49 2913490 level = block[j];
50
2/2
✓ Branch 0 taken 1214954 times.
✓ Branch 1 taken 1698536 times.
2913490 if (level) {
51
2/2
✓ Branch 0 taken 604308 times.
✓ Branch 1 taken 610646 times.
1214954 if (level < 0) {
52 604308 level = -level;
53 604308 level = (int)(level * qscale * quant_matrix[j]) >> 3;
54 604308 level = (level - 1) | 1;
55 604308 level = -level;
56 } else {
57 610646 level = (int)(level * qscale * quant_matrix[j]) >> 3;
58 610646 level = (level - 1) | 1;
59 }
60 1214954 block[j] = level;
61 }
62 }
63 126024 }
64
65 285599 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
66 int16_t *block, int n, int qscale)
67 {
68 int i, level, nCoeffs;
69 const uint16_t *quant_matrix;
70
71 285599 nCoeffs= s->block_last_index[n];
72
73 285599 quant_matrix = s->inter_matrix;
74
2/2
✓ Branch 0 taken 9213074 times.
✓ Branch 1 taken 285599 times.
9498673 for(i=0; i<=nCoeffs; i++) {
75 9213074 int j= s->intra_scantable.permutated[i];
76 9213074 level = block[j];
77
2/2
✓ Branch 0 taken 2100996 times.
✓ Branch 1 taken 7112078 times.
9213074 if (level) {
78
2/2
✓ Branch 0 taken 1051940 times.
✓ Branch 1 taken 1049056 times.
2100996 if (level < 0) {
79 1051940 level = -level;
80 1051940 level = (((level << 1) + 1) * qscale *
81 1051940 ((int) (quant_matrix[j]))) >> 4;
82 1051940 level = (level - 1) | 1;
83 1051940 level = -level;
84 } else {
85 1049056 level = (((level << 1) + 1) * qscale *
86 1049056 ((int) (quant_matrix[j]))) >> 4;
87 1049056 level = (level - 1) | 1;
88 }
89 2100996 block[j] = level;
90 }
91 }
92 285599 }
93
94 131250 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
95 int16_t *block, int n, int qscale)
96 {
97 int i, level, nCoeffs;
98 const uint16_t *quant_matrix;
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131250 times.
131250 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
101 131250 else qscale <<= 1;
102
103 131250 nCoeffs= s->block_last_index[n];
104
105
2/2
✓ Branch 0 taken 87500 times.
✓ Branch 1 taken 43750 times.
131250 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
106 131250 quant_matrix = s->intra_matrix;
107
2/2
✓ Branch 0 taken 459002 times.
✓ Branch 1 taken 131250 times.
590252 for(i=1;i<=nCoeffs;i++) {
108 459002 int j= s->intra_scantable.permutated[i];
109 459002 level = block[j];
110
2/2
✓ Branch 0 taken 152203 times.
✓ Branch 1 taken 306799 times.
459002 if (level) {
111
2/2
✓ Branch 0 taken 74569 times.
✓ Branch 1 taken 77634 times.
152203 if (level < 0) {
112 74569 level = -level;
113 74569 level = (int)(level * qscale * quant_matrix[j]) >> 4;
114 74569 level = -level;
115 } else {
116 77634 level = (int)(level * qscale * quant_matrix[j]) >> 4;
117 }
118 152203 block[j] = level;
119 }
120 }
121 131250 }
122
123 1229966 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
124 int16_t *block, int n, int qscale)
125 {
126 int i, level, nCoeffs;
127 const uint16_t *quant_matrix;
128 1229966 int sum=-1;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1229966 times.
1229966 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
131 1229966 else qscale <<= 1;
132
133 1229966 nCoeffs= s->block_last_index[n];
134
135
2/2
✓ Branch 0 taken 768296 times.
✓ Branch 1 taken 461670 times.
1229966 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
136 1229966 sum += block[0];
137 1229966 quant_matrix = s->intra_matrix;
138
2/2
✓ Branch 0 taken 21366164 times.
✓ Branch 1 taken 1229966 times.
22596130 for(i=1;i<=nCoeffs;i++) {
139 21366164 int j= s->intra_scantable.permutated[i];
140 21366164 level = block[j];
141
2/2
✓ Branch 0 taken 10557857 times.
✓ Branch 1 taken 10808307 times.
21366164 if (level) {
142
2/2
✓ Branch 0 taken 5311062 times.
✓ Branch 1 taken 5246795 times.
10557857 if (level < 0) {
143 5311062 level = -level;
144 5311062 level = (int)(level * qscale * quant_matrix[j]) >> 4;
145 5311062 level = -level;
146 } else {
147 5246795 level = (int)(level * qscale * quant_matrix[j]) >> 4;
148 }
149 10557857 block[j] = level;
150 10557857 sum+=level;
151 }
152 }
153 1229966 block[63]^=sum&1;
154 1229966 }
155
156 3081484 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
157 int16_t *block, int n, int qscale)
158 {
159 int i, level, nCoeffs;
160 const uint16_t *quant_matrix;
161 3081484 int sum=-1;
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3081484 times.
3081484 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
164 3081484 else qscale <<= 1;
165
166 3081484 nCoeffs= s->block_last_index[n];
167
168 3081484 quant_matrix = s->inter_matrix;
169
2/2
✓ Branch 0 taken 72658009 times.
✓ Branch 1 taken 3081484 times.
75739493 for(i=0; i<=nCoeffs; i++) {
170 72658009 int j= s->intra_scantable.permutated[i];
171 72658009 level = block[j];
172
2/2
✓ Branch 0 taken 26316487 times.
✓ Branch 1 taken 46341522 times.
72658009 if (level) {
173
2/2
✓ Branch 0 taken 13197409 times.
✓ Branch 1 taken 13119078 times.
26316487 if (level < 0) {
174 13197409 level = -level;
175 13197409 level = (((level << 1) + 1) * qscale *
176 13197409 ((int) (quant_matrix[j]))) >> 5;
177 13197409 level = -level;
178 } else {
179 13119078 level = (((level << 1) + 1) * qscale *
180 13119078 ((int) (quant_matrix[j]))) >> 5;
181 }
182 26316487 block[j] = level;
183 26316487 sum+=level;
184 }
185 }
186 3081484 block[63]^=sum&1;
187 3081484 }
188
189 3318498 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
190 int16_t *block, int n, int qscale)
191 {
192 int i, level, qmul, qadd;
193 int nCoeffs;
194
195 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
196
197 3318498 qmul = qscale << 1;
198
199
2/2
✓ Branch 0 taken 3145302 times.
✓ Branch 1 taken 173196 times.
3318498 if (!s->h263_aic) {
200
2/2
✓ Branch 0 taken 2096868 times.
✓ Branch 1 taken 1048434 times.
3145302 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
201 3145302 qadd = (qscale - 1) | 1;
202 }else{
203 173196 qadd = 0;
204 }
205
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3250194 times.
3318498 if(s->ac_pred)
206 68304 nCoeffs=63;
207 else
208 3250194 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
209
210
2/2
✓ Branch 0 taken 114165824 times.
✓ Branch 1 taken 3318498 times.
117484322 for(i=1; i<=nCoeffs; i++) {
211 114165824 level = block[i];
212
2/2
✓ Branch 0 taken 35779119 times.
✓ Branch 1 taken 78386705 times.
114165824 if (level) {
213
2/2
✓ Branch 0 taken 17913625 times.
✓ Branch 1 taken 17865494 times.
35779119 if (level < 0) {
214 17913625 level = level * qmul - qadd;
215 } else {
216 17865494 level = level * qmul + qadd;
217 }
218 35779119 block[i] = level;
219 }
220 }
221 3318498 }
222
223 5789701 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
224 int16_t *block, int n, int qscale)
225 {
226 int i, level, qmul, qadd;
227 int nCoeffs;
228
229 av_assert2(s->block_last_index[n]>=0);
230
231 5789701 qadd = (qscale - 1) | 1;
232 5789701 qmul = qscale << 1;
233
234 5789701 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
235
236
2/2
✓ Branch 0 taken 222003125 times.
✓ Branch 1 taken 5789701 times.
227792826 for(i=0; i<=nCoeffs; i++) {
237 222003125 level = block[i];
238
2/2
✓ Branch 0 taken 46310047 times.
✓ Branch 1 taken 175693078 times.
222003125 if (level) {
239
2/2
✓ Branch 0 taken 23077395 times.
✓ Branch 1 taken 23232652 times.
46310047 if (level < 0) {
240 23077395 level = level * qmul - qadd;
241 } else {
242 23232652 level = level * qmul + qadd;
243 }
244 46310047 block[i] = level;
245 }
246 }
247 5789701 }
248
249 683 av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s,
250 int bitexact, int q_scale_type)
251 {
252 683 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
253 683 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
254 683 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
255 683 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
256 683 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
257
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 112 times.
683 if (bitexact)
258 571 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
259 683 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
260
261 #if HAVE_INTRINSICS_NEON
262 ff_mpv_unquantize_init_neon(s, bitexact);
263 #endif
264
265 #if ARCH_ARM
266 ff_mpv_unquantize_init_arm(s, bitexact);
267 #elif ARCH_PPC
268 ff_mpv_unquantize_init_ppc(s, bitexact);
269 #elif ARCH_X86
270 683 ff_mpv_unquantize_init_x86(s, bitexact);
271 #elif ARCH_MIPS
272 ff_mpv_unquantize_init_mips(s, bitexact, q_scale_type);
273 #endif
274 683 }
275