/src/ffmpeg/libavcodec/mpegvideo_unquantize.c
Line | Count | 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 | | av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st, |
37 | | const uint8_t *src_scantable) |
38 | 736k | { |
39 | 736k | st->scantable = src_scantable; |
40 | | |
41 | 47.8M | for (int i = 0, end = -1; i < 64; i++) { |
42 | 47.1M | int j = src_scantable[i]; |
43 | 47.1M | st->permutated[i] = permutation[j]; |
44 | 47.1M | if (permutation[j] > end) |
45 | 11.1M | end = permutation[j]; |
46 | 47.1M | st->raster_end[i] = end; |
47 | 47.1M | } |
48 | 736k | } |
49 | | |
50 | | static void dct_unquantize_mpeg1_intra_c(const MPVContext *s, |
51 | | int16_t *block, int n, int qscale) |
52 | 763k | { |
53 | 763k | int i, level, nCoeffs; |
54 | 763k | const uint16_t *quant_matrix; |
55 | | |
56 | 763k | nCoeffs= s->block_last_index[n]; |
57 | | |
58 | 763k | block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale; |
59 | | /* XXX: only MPEG-1 */ |
60 | 763k | quant_matrix = s->intra_matrix; |
61 | 7.06M | for(i=1;i<=nCoeffs;i++) { |
62 | 6.30M | int j= s->intra_scantable.permutated[i]; |
63 | 6.30M | level = block[j]; |
64 | 6.30M | if (level) { |
65 | 4.35M | if (level < 0) { |
66 | 2.16M | level = -level; |
67 | 2.16M | level = (int)(level * qscale * quant_matrix[j]) >> 3; |
68 | 2.16M | level = (level - 1) | 1; |
69 | 2.16M | level = -level; |
70 | 2.18M | } else { |
71 | 2.18M | level = (int)(level * qscale * quant_matrix[j]) >> 3; |
72 | 2.18M | level = (level - 1) | 1; |
73 | 2.18M | } |
74 | 4.35M | block[j] = level; |
75 | 4.35M | } |
76 | 6.30M | } |
77 | 763k | } |
78 | | |
79 | | static void dct_unquantize_mpeg1_inter_c(const MPVContext *s, |
80 | | int16_t *block, int n, int qscale) |
81 | 0 | { |
82 | 0 | int i, level, nCoeffs; |
83 | 0 | const uint16_t *quant_matrix; |
84 | |
|
85 | 0 | nCoeffs= s->block_last_index[n]; |
86 | |
|
87 | 0 | quant_matrix = s->inter_matrix; |
88 | 0 | for(i=0; i<=nCoeffs; i++) { |
89 | 0 | int j= s->intra_scantable.permutated[i]; |
90 | 0 | level = block[j]; |
91 | 0 | if (level) { |
92 | 0 | if (level < 0) { |
93 | 0 | level = -level; |
94 | 0 | level = (((level << 1) + 1) * qscale * |
95 | 0 | ((int) (quant_matrix[j]))) >> 4; |
96 | 0 | level = (level - 1) | 1; |
97 | 0 | level = -level; |
98 | 0 | } else { |
99 | 0 | level = (((level << 1) + 1) * qscale * |
100 | 0 | ((int) (quant_matrix[j]))) >> 4; |
101 | 0 | level = (level - 1) | 1; |
102 | 0 | } |
103 | 0 | block[j] = level; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | static void dct_unquantize_mpeg2_intra_c(const MPVContext *s, |
109 | | int16_t *block, int n, int qscale) |
110 | 1.47M | { |
111 | 1.47M | int i, level, nCoeffs; |
112 | 1.47M | const uint16_t *quant_matrix; |
113 | | |
114 | 1.47M | if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale]; |
115 | 1.47M | else qscale <<= 1; |
116 | | |
117 | 1.47M | nCoeffs= s->block_last_index[n]; |
118 | | |
119 | 1.47M | block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale; |
120 | 1.47M | quant_matrix = s->intra_matrix; |
121 | 15.7M | for(i=1;i<=nCoeffs;i++) { |
122 | 14.2M | int j= s->intra_scantable.permutated[i]; |
123 | 14.2M | level = block[j]; |
124 | 14.2M | if (level) { |
125 | 5.81M | if (level < 0) { |
126 | 2.94M | level = -level; |
127 | 2.94M | level = (int)(level * qscale * quant_matrix[j]) >> 4; |
128 | 2.94M | level = -level; |
129 | 2.94M | } else { |
130 | 2.86M | level = (int)(level * qscale * quant_matrix[j]) >> 4; |
131 | 2.86M | } |
132 | 5.81M | block[j] = level; |
133 | 5.81M | } |
134 | 14.2M | } |
135 | 1.47M | } |
136 | | |
137 | | static void dct_unquantize_mpeg2_intra_bitexact(const MPVContext *s, |
138 | | int16_t *block, int n, int qscale) |
139 | 0 | { |
140 | 0 | int i, level, nCoeffs; |
141 | 0 | const uint16_t *quant_matrix; |
142 | 0 | int sum=-1; |
143 | |
|
144 | 0 | if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale]; |
145 | 0 | else qscale <<= 1; |
146 | |
|
147 | 0 | nCoeffs= s->block_last_index[n]; |
148 | |
|
149 | 0 | block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale; |
150 | 0 | sum += block[0]; |
151 | 0 | quant_matrix = s->intra_matrix; |
152 | 0 | for(i=1;i<=nCoeffs;i++) { |
153 | 0 | int j= s->intra_scantable.permutated[i]; |
154 | 0 | level = block[j]; |
155 | 0 | if (level) { |
156 | 0 | if (level < 0) { |
157 | 0 | level = -level; |
158 | 0 | level = (int)(level * qscale * quant_matrix[j]) >> 4; |
159 | 0 | level = -level; |
160 | 0 | } else { |
161 | 0 | level = (int)(level * qscale * quant_matrix[j]) >> 4; |
162 | 0 | } |
163 | 0 | block[j] = level; |
164 | 0 | sum+=level; |
165 | 0 | } |
166 | 0 | } |
167 | 0 | block[63]^=sum&1; |
168 | 0 | } |
169 | | |
170 | | static void dct_unquantize_mpeg2_inter_c(const MPVContext *s, |
171 | | int16_t *block, int n, int qscale) |
172 | 56.2k | { |
173 | 56.2k | int i, level, nCoeffs; |
174 | 56.2k | const uint16_t *quant_matrix; |
175 | 56.2k | int sum=-1; |
176 | | |
177 | 56.2k | if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale]; |
178 | 56.2k | else qscale <<= 1; |
179 | | |
180 | 56.2k | nCoeffs= s->block_last_index[n]; |
181 | | |
182 | 56.2k | quant_matrix = s->inter_matrix; |
183 | 889k | for(i=0; i<=nCoeffs; i++) { |
184 | 833k | int j= s->intra_scantable.permutated[i]; |
185 | 833k | level = block[j]; |
186 | 833k | if (level) { |
187 | 233k | if (level < 0) { |
188 | 141k | level = -level; |
189 | 141k | level = (((level << 1) + 1) * qscale * |
190 | 141k | ((int) (quant_matrix[j]))) >> 5; |
191 | 141k | level = -level; |
192 | 141k | } else { |
193 | 91.7k | level = (((level << 1) + 1) * qscale * |
194 | 91.7k | ((int) (quant_matrix[j]))) >> 5; |
195 | 91.7k | } |
196 | 233k | block[j] = level; |
197 | 233k | sum+=level; |
198 | 233k | } |
199 | 833k | } |
200 | 56.2k | block[63]^=sum&1; |
201 | 56.2k | } |
202 | | |
203 | | static void dct_unquantize_h263_intra_c(const MPVContext *s, |
204 | | int16_t *block, int n, int qscale) |
205 | 48.4M | { |
206 | 48.4M | int i, level, qmul, qadd; |
207 | 48.4M | int nCoeffs; |
208 | | |
209 | 48.4M | av_assert2(s->block_last_index[n]>=0 || s->h263_aic); |
210 | | |
211 | 48.4M | qmul = qscale << 1; |
212 | | |
213 | 48.4M | if (!s->h263_aic) { |
214 | 45.8M | block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale; |
215 | 45.8M | qadd = (qscale - 1) | 1; |
216 | 45.8M | }else{ |
217 | 2.57M | qadd = 0; |
218 | 2.57M | } |
219 | 48.4M | if(s->ac_pred) |
220 | 11.9M | nCoeffs=63; |
221 | 36.4M | else |
222 | 36.4M | nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]; |
223 | | |
224 | 1.21G | for(i=1; i<=nCoeffs; i++) { |
225 | 1.16G | level = block[i]; |
226 | 1.16G | if (level) { |
227 | 132M | if (level < 0) { |
228 | 71.0M | level = level * qmul - qadd; |
229 | 71.0M | } else { |
230 | 61.6M | level = level * qmul + qadd; |
231 | 61.6M | } |
232 | 132M | block[i] = level; |
233 | 132M | } |
234 | 1.16G | } |
235 | 48.4M | } |
236 | | |
237 | | static void dct_unquantize_h263_inter_c(const MPVContext *s, |
238 | | int16_t *block, int n, int qscale) |
239 | 3.26M | { |
240 | 3.26M | int i, level, qmul, qadd; |
241 | 3.26M | int nCoeffs; |
242 | | |
243 | 3.26M | av_assert2(s->block_last_index[n]>=0); |
244 | | |
245 | 3.26M | qadd = (qscale - 1) | 1; |
246 | 3.26M | qmul = qscale << 1; |
247 | | |
248 | 3.26M | nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; |
249 | | |
250 | 98.4M | for(i=0; i<=nCoeffs; i++) { |
251 | 95.2M | level = block[i]; |
252 | 95.2M | if (level) { |
253 | 9.06M | if (level < 0) { |
254 | 3.78M | level = level * qmul - qadd; |
255 | 5.27M | } else { |
256 | 5.27M | level = level * qmul + qadd; |
257 | 5.27M | } |
258 | 9.06M | block[i] = level; |
259 | 9.06M | } |
260 | 95.2M | } |
261 | 3.26M | } |
262 | | |
263 | | av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s, |
264 | | int bitexact, int q_scale_type) |
265 | 69.5k | { |
266 | 69.5k | s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c; |
267 | 69.5k | s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c; |
268 | 69.5k | s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c; |
269 | 69.5k | s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c; |
270 | 69.5k | s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c; |
271 | 69.5k | if (bitexact) |
272 | 0 | s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact; |
273 | 69.5k | 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 | | 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 | 69.5k | } |