FFmpeg
mpegvideo_unquantize.c
Go to the documentation of this file.
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 {
39  st->scantable = src_scantable;
40 
41  for (int i = 0, end = -1; i < 64; i++) {
42  int j = src_scantable[i];
43  st->permutated[i] = permutation[j];
44  if (permutation[j] > end)
45  end = permutation[j];
46  st->raster_end[i] = end;
47  }
48 }
49 
50 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  nCoeffs= s->block_last_index[n];
57 
58  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
59  /* XXX: only MPEG-1 */
60  quant_matrix = s->intra_matrix;
61  for(i=1;i<=nCoeffs;i++) {
62  int j= s->intra_scantable.permutated[i];
63  level = block[j];
64  if (level) {
65  if (level < 0) {
66  level = -level;
67  level = (int)(level * qscale * quant_matrix[j]) >> 3;
68  level = (level - 1) | 1;
69  level = -level;
70  } else {
71  level = (int)(level * qscale * quant_matrix[j]) >> 3;
72  level = (level - 1) | 1;
73  }
74  block[j] = level;
75  }
76  }
77 }
78 
79 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  nCoeffs= s->block_last_index[n];
86 
87  quant_matrix = s->inter_matrix;
88  for(i=0; i<=nCoeffs; i++) {
89  int j= s->intra_scantable.permutated[i];
90  level = block[j];
91  if (level) {
92  if (level < 0) {
93  level = -level;
94  level = (((level << 1) + 1) * qscale *
95  ((int) (quant_matrix[j]))) >> 4;
96  level = (level - 1) | 1;
97  level = -level;
98  } else {
99  level = (((level << 1) + 1) * qscale *
100  ((int) (quant_matrix[j]))) >> 4;
101  level = (level - 1) | 1;
102  }
103  block[j] = level;
104  }
105  }
106 }
107 
108 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  if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
115  else qscale <<= 1;
116 
117  nCoeffs= s->block_last_index[n];
118 
119  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
120  quant_matrix = s->intra_matrix;
121  for(i=1;i<=nCoeffs;i++) {
122  int j= s->intra_scantable.permutated[i];
123  level = block[j];
124  if (level) {
125  if (level < 0) {
126  level = -level;
127  level = (int)(level * qscale * quant_matrix[j]) >> 4;
128  level = -level;
129  } else {
130  level = (int)(level * qscale * quant_matrix[j]) >> 4;
131  }
132  block[j] = level;
133  }
134  }
135 }
136 
137 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  int sum=-1;
143 
144  if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
145  else qscale <<= 1;
146 
147  nCoeffs= s->block_last_index[n];
148 
149  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
150  sum += block[0];
151  quant_matrix = s->intra_matrix;
152  for(i=1;i<=nCoeffs;i++) {
153  int j= s->intra_scantable.permutated[i];
154  level = block[j];
155  if (level) {
156  if (level < 0) {
157  level = -level;
158  level = (int)(level * qscale * quant_matrix[j]) >> 4;
159  level = -level;
160  } else {
161  level = (int)(level * qscale * quant_matrix[j]) >> 4;
162  }
163  block[j] = level;
164  sum+=level;
165  }
166  }
167  block[63]^=sum&1;
168 }
169 
170 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  int sum=-1;
176 
177  if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
178  else qscale <<= 1;
179 
180  nCoeffs= s->block_last_index[n];
181 
182  quant_matrix = s->inter_matrix;
183  for(i=0; i<=nCoeffs; i++) {
184  int j= s->intra_scantable.permutated[i];
185  level = block[j];
186  if (level) {
187  if (level < 0) {
188  level = -level;
189  level = (((level << 1) + 1) * qscale *
190  ((int) (quant_matrix[j]))) >> 5;
191  level = -level;
192  } else {
193  level = (((level << 1) + 1) * qscale *
194  ((int) (quant_matrix[j]))) >> 5;
195  }
196  block[j] = level;
197  sum+=level;
198  }
199  }
200  block[63]^=sum&1;
201 }
202 
203 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  qmul = qscale << 1;
212 
213  if (!s->h263_aic) {
214  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
215  qadd = (qscale - 1) | 1;
216  }else{
217  qadd = 0;
218  }
219  if(s->ac_pred)
220  nCoeffs=63;
221  else
222  nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
223 
224  for(i=1; i<=nCoeffs; i++) {
225  level = block[i];
226  if (level) {
227  if (level < 0) {
228  level = level * qmul - qadd;
229  } else {
230  level = level * qmul + qadd;
231  }
232  block[i] = level;
233  }
234  }
235 }
236 
237 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  qadd = (qscale - 1) | 1;
246  qmul = qscale << 1;
247 
248  nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
249 
250  for(i=0; i<=nCoeffs; i++) {
251  level = block[i];
252  if (level) {
253  if (level < 0) {
254  level = level * qmul - qadd;
255  } else {
256  level = level * qmul + qadd;
257  }
258  block[i] = level;
259  }
260  }
261 }
262 
264  int bitexact, int q_scale_type)
265 {
266  s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
267  s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
268  s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
269  s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
270  s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
271  if (bitexact)
272  s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
273  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_X86
284  ff_mpv_unquantize_init_x86(s, bitexact);
285 #elif ARCH_MIPS
286  ff_mpv_unquantize_init_mips(s, bitexact, q_scale_type);
287 #endif
288 }
mpegvideo_unquantize.h
dct_unquantize_mpeg2_intra_bitexact
static void dct_unquantize_mpeg2_intra_bitexact(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:137
level
uint8_t level
Definition: svq3.c:208
dct_unquantize_mpeg2_intra_c
static void dct_unquantize_mpeg2_intra_c(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:108
ff_init_scantable
av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: mpegvideo_unquantize.c:36
ff_mpv_unquantize_init_arm
av_cold void ff_mpv_unquantize_init_arm(MPVUnquantDSPContext *s, int bitexact)
Definition: mpegvideo_arm.c:51
ff_mpv_unquantize_init_x86
void ff_mpv_unquantize_init_x86(MPVUnquantDSPContext *s, int bitexact)
Definition: mpegvideo.c:376
ff_mpv_unquantize_init_neon
void ff_mpv_unquantize_init_neon(MPVUnquantDSPContext *s, int bitexact)
Definition: mpegvideo.c:124
ff_mpv_unquantize_init_ppc
void ff_mpv_unquantize_init_ppc(MPVUnquantDSPContext *s, int bitexact)
Definition: mpegvideo_altivec.c:102
ff_mpeg2_non_linear_qscale
const uint8_t ff_mpeg2_non_linear_qscale[32]
Definition: mpegvideodata.c:26
mpegvideo.h
dct_unquantize_h263_inter_c
static void dct_unquantize_h263_inter_c(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:237
dct_unquantize_mpeg1_inter_c
static void dct_unquantize_mpeg1_inter_c(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:79
dct_unquantize_mpeg2_inter_c
static void dct_unquantize_mpeg2_inter_c(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:170
avassert.h
av_cold
#define av_cold
Definition: attributes.h:106
dct_unquantize_h263_intra_c
static void dct_unquantize_h263_intra_c(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:203
s
#define s(width, name)
Definition: cbs_vp9.c:198
ScanTable::scantable
const uint8_t * scantable
Definition: mpegvideo.h:49
ff_mpv_unquantize_init
av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s, int bitexact, int q_scale_type)
Definition: mpegvideo_unquantize.c:263
mpegvideodata.h
attributes.h
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_mpv_unquantize_init_mips
av_cold void ff_mpv_unquantize_init_mips(MPVUnquantDSPContext *s, int bitexact, int q_scale_type)
Definition: mpegvideo_init_mips.c:27
avcodec.h
ScanTable
Scantable.
Definition: mpegvideo.h:48
dct_unquantize_mpeg1_intra_c
static void dct_unquantize_mpeg1_intra_c(const MPVContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo_unquantize.c:50
ScanTable::permutated
uint8_t permutated[64]
Definition: mpegvideo.h:50
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
MPVUnquantDSPContext
Definition: mpegvideo_unquantize.h:34
ScanTable::raster_end
uint8_t raster_end[64]
Definition: mpegvideo.h:51