FFmpeg
mpegvideo_unquantize.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <assert.h>
20 #include <stddef.h>
21 
22 #include "config.h"
23 
24 #include "checkasm.h"
25 
26 #include "libavcodec/idctdsp.h"
27 #include "libavcodec/mathops.h"
28 #include "libavcodec/mpegvideo.h"
31 
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem_internal.h"
34 
35 #define randomize_struct(TYPE, s) do { \
36  static_assert(!(_Alignof(TYPE) % 4), \
37  "can't use aligned stores"); \
38  unsigned char *ptr = (unsigned char*)s; \
39  for (size_t i = 0; i < sizeof(*s) & ~3; i += 4) \
40  AV_WN32A(ptr + i, rnd()); \
41  for (size_t i = sizeof(*s) & ~3; i < sizeof(*s); ++i) \
42  ptr[i] = rnd(); \
43  } while (0)
44 
45 enum TestType {
49 };
50 
51 static void init_idct_scantable(MPVContext *const s, int intra_scantable)
52 {
53  static const enum idct_permutation_type permutation_types[] = {
56 #if ARCH_X86_32 && HAVE_X86ASM
58 #endif
59 #if ARCH_PPC || ARCH_X86
61 #endif
62 #if ARCH_ARM || ARCH_AARCH64
64 #endif
65 #if ARCH_X86 && HAVE_X86ASM
67 #endif
68  };
69  // Copied here to avoid #ifs.
70  static const uint8_t ff_wmv1_scantable[][64] = {
71  { 0x00, 0x08, 0x01, 0x02, 0x09, 0x10, 0x18, 0x11,
72  0x0A, 0x03, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x28,
73  0x30, 0x38, 0x29, 0x21, 0x1A, 0x13, 0x0C, 0x05,
74  0x06, 0x0D, 0x14, 0x1B, 0x22, 0x31, 0x39, 0x3A,
75  0x32, 0x2A, 0x23, 0x1C, 0x15, 0x0E, 0x07, 0x0F,
76  0x16, 0x1D, 0x24, 0x2B, 0x33, 0x3B, 0x3C, 0x34,
77  0x2C, 0x25, 0x1E, 0x17, 0x1F, 0x26, 0x2D, 0x35,
78  0x3D, 0x3E, 0x36, 0x2E, 0x27, 0x2F, 0x37, 0x3F, },
79  { 0x00, 0x08, 0x01, 0x02, 0x09, 0x10, 0x18, 0x11,
80  0x0A, 0x03, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x28,
81  0x21, 0x30, 0x1A, 0x13, 0x0C, 0x05, 0x06, 0x0D,
82  0x14, 0x1B, 0x22, 0x29, 0x38, 0x31, 0x39, 0x2A,
83  0x23, 0x1C, 0x15, 0x0E, 0x07, 0x0F, 0x16, 0x1D,
84  0x24, 0x2B, 0x32, 0x3A, 0x33, 0x3B, 0x2C, 0x25,
85  0x1E, 0x17, 0x1F, 0x26, 0x2D, 0x34, 0x3C, 0x35,
86  0x3D, 0x2E, 0x27, 0x2F, 0x36, 0x3E, 0x37, 0x3F, },
87  { 0x00, 0x01, 0x08, 0x02, 0x03, 0x09, 0x10, 0x18,
88  0x11, 0x0A, 0x04, 0x05, 0x0B, 0x12, 0x19, 0x20,
89  0x28, 0x30, 0x21, 0x1A, 0x13, 0x0C, 0x06, 0x07,
90  0x0D, 0x14, 0x1B, 0x22, 0x29, 0x38, 0x31, 0x39,
91  0x2A, 0x23, 0x1C, 0x15, 0x0E, 0x0F, 0x16, 0x1D,
92  0x24, 0x2B, 0x32, 0x3A, 0x33, 0x2C, 0x25, 0x1E,
93  0x17, 0x1F, 0x26, 0x2D, 0x34, 0x3B, 0x3C, 0x35,
94  0x2E, 0x27, 0x2F, 0x36, 0x3D, 0x3E, 0x37, 0x3F, },
95  { 0x00, 0x08, 0x10, 0x01, 0x18, 0x20, 0x28, 0x09,
96  0x02, 0x03, 0x0A, 0x11, 0x19, 0x30, 0x38, 0x29,
97  0x21, 0x1A, 0x12, 0x0B, 0x04, 0x05, 0x0C, 0x13,
98  0x1B, 0x22, 0x31, 0x39, 0x32, 0x2A, 0x23, 0x1C,
99  0x14, 0x0D, 0x06, 0x07, 0x0E, 0x15, 0x1D, 0x24,
100  0x2B, 0x33, 0x3A, 0x3B, 0x34, 0x2C, 0x25, 0x1E,
101  0x16, 0x0F, 0x17, 0x1F, 0x26, 0x2D, 0x3C, 0x35,
102  0x2E, 0x27, 0x2F, 0x36, 0x3D, 0x3E, 0x37, 0x3F, }
103  };
104 
105  static const uint8_t *const scantables[] = {
113  };
114  static const uint8_t *scantable = NULL;
115  static enum idct_permutation_type idct_permutation;
116 
117  if (!scantable) {
118  scantable = scantables[rnd() % FF_ARRAY_ELEMS(scantables)];
119  idct_permutation = permutation_types[rnd() % FF_ARRAY_ELEMS(permutation_types)];
120  }
121  ff_init_scantable_permutation(s->idsp.idct_permutation, idct_permutation);
122  ff_init_scantable(s->idsp.idct_permutation,
123  intra_scantable ? &s->intra_scantable : &s->inter_scantable,
124  scantable);
125 }
126 
127 static void init_h263_test(MPVContext *const s, int16_t block[64],
128  int last_nonzero_coeff, int qscale, int intra)
129 {
130  const uint8_t *permutation = s->inter_scantable.permutated;
131  if (intra) {
132  permutation = s->intra_scantable.permutated;
133  block[0] = rnd() & 511;
134  static int h263_aic = -1, ac_pred;
135  if (h263_aic < 0) {
136  h263_aic = rnd() & 1;
137  ac_pred = rnd() & 1;
138  }
139  s->h263_aic = h263_aic;
140  s->ac_pred = ac_pred;
141  if (s->ac_pred)
142  last_nonzero_coeff = 63;
143  }
144  for (int i = intra; i <= last_nonzero_coeff; ++i) {
145  int random = rnd();
146  if (random & 1)
147  continue;
148  random >>= 1;
149  // Select level so that the multiplication fits into 16 bits.
150  // FIXME: The FLV and MPEG-4 decoders can have escape values exceeding this.
151  block[permutation[i]] = sign_extend(random, 10);
152  }
153 }
154 
155 static void init_mpeg12_test(MPVContext *const s, int16_t block[64],
156  int last_nonzero_coeff, int qscale, int intra,
157  enum TestType type)
158 {
159  uint16_t *matrix = intra ? s->intra_matrix : s->inter_matrix;
160 
161  if (type == MPEG2)
162  qscale = s->q_scale_type ? ff_mpeg2_non_linear_qscale[qscale] : qscale << 1;
163 
164  for (int i = 0; i < 64; ++i)
165  matrix[i] = 1 + rnd() % 254;
166 
167  const uint8_t *permutation = s->intra_scantable.permutated;
168  if (intra) {
169  block[0] = (int8_t)rnd();
170  for (int i = 1; i <= last_nonzero_coeff; ++i) {
171  int j = permutation[i];
172  unsigned random = rnd();
173  if (random & 1)
174  continue;
175  random >>= 1;
176  // Select level so that the multiplication does not overflow
177  // an int16_t and so that it is within the possible range
178  // (-2048..2047). FIXME: It seems that this need not be fulfilled
179  // in practice for the MPEG-4 decoder at least.
180  int limit = FFMIN(INT16_MAX / (qscale * matrix[j]), 2047);
181  block[j] = random % (2 * limit + 1) - limit;
182  }
183  } else {
184  for (int i = 0; i <= last_nonzero_coeff; ++i) {
185  int j = permutation[i];
186  unsigned random = rnd();
187  if (random & 1)
188  continue;
189  random >>= 1;
190  int limit = FFMIN((INT16_MAX / (qscale * matrix[j]) - 1) / 2, 2047);
191  block[j] = random % (2 * limit + 1) - limit;
192  }
193  }
194 }
195 
197 {
198  static const struct {
199  const char *name;
200  size_t offset;
201  int intra, intra_scantable;
202  enum TestType type;
203  } tests[] = {
204 #define TEST(NAME, INTRA, INTRA_SCANTABLE, TYPE) \
205  { .name = #NAME, .offset = offsetof(MPVUnquantDSPContext, NAME), \
206  .intra = INTRA, .intra_scantable = INTRA_SCANTABLE, .type = TYPE }
207  TEST(dct_unquantize_mpeg1_intra, 1, 1, MPEG1),
208  TEST(dct_unquantize_mpeg1_inter, 0, 1, MPEG1),
209  TEST(dct_unquantize_mpeg2_intra, 1, 1, MPEG2),
210  TEST(dct_unquantize_mpeg2_inter, 0, 1, MPEG2),
211  TEST(dct_unquantize_h263_intra, 1, 1, H263),
212  TEST(dct_unquantize_h263_inter, 0, 0, H263),
213  };
214  MPVUnquantDSPContext unquant_dsp_ctx;
215  int q_scale_type = rnd() & 1;
216 
217  ff_mpv_unquantize_init(&unquant_dsp_ctx, 1 /* bitexact */, q_scale_type);
218  declare_func(void, const MPVContext *s, int16_t *block, int n, int qscale);
219 
220  for (size_t i = 0; i < FF_ARRAY_ELEMS(tests); ++i) {
221  void (*func)(const MPVContext *s, int16_t *block, int n, int qscale) =
222  *(void (**)(const MPVContext *, int16_t *, int, int))((char*)&unquant_dsp_ctx + tests[i].offset);
223  if (check_func(func, "%s", tests[i].name)) {
224  MPVContext new, ref;
225  DECLARE_ALIGNED(16, int16_t, block_new)[64];
226  DECLARE_ALIGNED(16, int16_t, block_ref)[64];
227  static int block_last_index = -1;
228 
229  randomize_struct(MPVContext, &ref);
230 
231  ref.q_scale_type = q_scale_type;
232 
233  init_idct_scantable(&ref, tests[i].intra_scantable);
234 
235  if (block_last_index < 0)
236  block_last_index = rnd() % 64;
237 
238  memset(block_ref, 0, sizeof(block_ref));
239 
240  if (tests[i].intra) {
241  // Less restricted than real dc_scale values
242  ref.y_dc_scale = 1 + rnd() % 64;
243  ref.c_dc_scale = 1 + rnd() % 64;
244  }
245 
246  static int qscale = 0;
247 
248  if (qscale == 0)
249  qscale = 1 + rnd() % 31;
250 
251  if (tests[i].type == H263)
252  init_h263_test(&ref, block_ref, block_last_index, qscale,
253  tests[i].intra);
254  else
255  init_mpeg12_test(&ref, block_ref, block_last_index, qscale,
256  tests[i].intra, tests[i].type);
257 
258  int n = rnd() % 6;
259  ref.block_last_index[n] = block_last_index;
260 
261  memcpy(&new, &ref, sizeof(new));
262  memcpy(block_new, block_ref, sizeof(block_new));
263 
264  call_ref(&ref, block_ref, n, qscale);
265  call_new(&new, block_new, n, qscale);
266 
267  if (memcmp(&ref, &new, sizeof(new)) || memcmp(block_new, block_ref, sizeof(block_new)))
268  fail();
269 
270  bench_new(&new, block_new, n, qscale);
271  }
272  }
273 }
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:66
mpegvideo_unquantize.h
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
mem_internal.h
init_h263_test
static void init_h263_test(MPVContext *const s, int16_t block[64], int last_nonzero_coeff, int qscale, int intra)
Definition: mpegvideo_unquantize.c:127
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
matrix
Definition: vc1dsp.c:43
check_func
#define check_func(func,...)
Definition: checkasm.h:205
ff_mpeg2_non_linear_qscale
const uint8_t ff_mpeg2_non_linear_qscale[32]
Definition: mpegvideodata.c:26
mpegvideo.h
call_ref
#define call_ref(...)
Definition: checkasm.h:220
fail
#define fail()
Definition: checkasm.h:214
checkasm.h
checkasm_check_mpegvideo_unquantize
void checkasm_check_mpegvideo_unquantize(void)
Definition: mpegvideo_unquantize.c:196
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
init_idct_scantable
static void init_idct_scantable(MPVContext *const s, int intra_scantable)
Definition: mpegvideo_unquantize.c:51
rnd
#define rnd()
Definition: checkasm.h:198
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
MPEG2
@ MPEG2
Definition: mpegvideo_unquantize.c:48
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
TestType
TestType
Definition: mpegvideo_unquantize.c:45
call_new
#define call_new(...)
Definition: checkasm.h:323
NULL
#define NULL
Definition: coverity.c:32
mathops.h
ff_alternate_horizontal_scan
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:52
MPEG1
@ MPEG1
Definition: mpegvideo_unquantize.c:47
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
FF_IDCT_PERM_NONE
@ FF_IDCT_PERM_NONE
Definition: idctdsp.h:28
ff_mpv_unquantize_init
av_cold void ff_mpv_unquantize_init(MPVUnquantDSPContext *s, int bitexact, int q_scale_type)
Definition: mpegvideo_unquantize.c:263
init_mpeg12_test
static void init_mpeg12_test(MPVContext *const s, int16_t block[64], int last_nonzero_coeff, int qscale, int intra, enum TestType type)
Definition: mpegvideo_unquantize.c:155
H263
@ H263
Definition: mpegvideo_unquantize.c:46
mpegvideodata.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
tests
const TestCase tests[]
Definition: fifo_muxer.c:363
randomize_struct
#define randomize_struct(TYPE, s)
Definition: mpegvideo_unquantize.c:35
bench_new
#define bench_new(...)
Definition: checkasm.h:408
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_alternate_vertical_scan
const uint8_t ff_alternate_vertical_scan[64]
Definition: mpegvideodata.c:63
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
idctdsp.h
limit
static double limit(double x)
Definition: vf_pseudocolor.c:142
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:137
FF_IDCT_PERM_TRANSPOSE
@ FF_IDCT_PERM_TRANSPOSE
Definition: idctdsp.h:31
FF_IDCT_PERM_SSE2
@ FF_IDCT_PERM_SSE2
Definition: idctdsp.h:33
FF_IDCT_PERM_PARTTRANS
@ FF_IDCT_PERM_PARTTRANS
Definition: idctdsp.h:32
TEST
#define TEST(NAME, INTRA, INTRA_SCANTABLE, TYPE)
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:132
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
idct_permutation_type
idct_permutation_type
Definition: idctdsp.h:27
ff_init_scantable_permutation
av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation, enum idct_permutation_type perm_type)
Definition: idctdsp.c:39
declare_func
#define declare_func(ret,...)
Definition: checkasm.h:209
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
FF_IDCT_PERM_SIMPLE
@ FF_IDCT_PERM_SIMPLE
Definition: idctdsp.h:30
ff_wmv1_scantable
const uint8_t ff_wmv1_scantable[WMV1_SCANTABLE_COUNT][64]
Definition: msmpeg4_vc1_data.c:220
FF_IDCT_PERM_LIBMPEG2
@ FF_IDCT_PERM_LIBMPEG2
Definition: idctdsp.h:29