| Line | Branch | Exec | Source |
|---|---|---|---|
| 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" | ||
| 29 | #include "libavcodec/mpegvideodata.h" | ||
| 30 | #include "libavcodec/mpegvideo_unquantize.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 { | ||
| 46 | H263, | ||
| 47 | MPEG1, | ||
| 48 | MPEG2, | ||
| 49 | }; | ||
| 50 | |||
| 51 | 11 | static void init_idct_scantable(MPVContext *const s, int intra_scantable) | |
| 52 | { | ||
| 53 | static const enum idct_permutation_type permutation_types[] = { | ||
| 54 | FF_IDCT_PERM_NONE, | ||
| 55 | FF_IDCT_PERM_LIBMPEG2, | ||
| 56 | #if ARCH_X86_32 && HAVE_X86ASM | ||
| 57 | FF_IDCT_PERM_SIMPLE, | ||
| 58 | #endif | ||
| 59 | #if ARCH_PPC || ARCH_X86 | ||
| 60 | FF_IDCT_PERM_TRANSPOSE, | ||
| 61 | #endif | ||
| 62 | #if ARCH_ARM || ARCH_AARCH64 | ||
| 63 | FF_IDCT_PERM_PARTTRANS, | ||
| 64 | #endif | ||
| 65 | #if ARCH_X86 && HAVE_X86ASM | ||
| 66 | FF_IDCT_PERM_SSE2, | ||
| 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[] = { | ||
| 106 | ff_alternate_vertical_scan, | ||
| 107 | ff_alternate_horizontal_scan, | ||
| 108 | ff_zigzag_direct, | ||
| 109 | ff_wmv1_scantable[0], | ||
| 110 | ff_wmv1_scantable[1], | ||
| 111 | ff_wmv1_scantable[2], | ||
| 112 | ff_wmv1_scantable[3], | ||
| 113 | }; | ||
| 114 | static const uint8_t *scantable = NULL; | ||
| 115 | static enum idct_permutation_type idct_permutation; | ||
| 116 | |||
| 117 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 | if (!scantable) { |
| 118 | 1 | scantable = scantables[rnd() % FF_ARRAY_ELEMS(scantables)]; | |
| 119 | 1 | idct_permutation = permutation_types[rnd() % FF_ARRAY_ELEMS(permutation_types)]; | |
| 120 | } | ||
| 121 | 11 | ff_init_scantable_permutation(s->idsp.idct_permutation, idct_permutation); | |
| 122 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
|
11 | ff_init_scantable(s->idsp.idct_permutation, |
| 123 | intra_scantable ? &s->intra_scantable : &s->inter_scantable, | ||
| 124 | scantable); | ||
| 125 | 11 | } | |
| 126 | |||
| 127 | 4 | static void init_h263_test(MPVContext *const s, int16_t block[64], | |
| 128 | int last_nonzero_coeff, int qscale, int intra) | ||
| 129 | { | ||
| 130 | 4 | const uint8_t *permutation = s->inter_scantable.permutated; | |
| 131 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (intra) { |
| 132 | 2 | permutation = s->intra_scantable.permutated; | |
| 133 | 2 | block[0] = rnd() & 511; | |
| 134 | static int h263_aic = -1, ac_pred; | ||
| 135 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (h263_aic < 0) { |
| 136 | 1 | h263_aic = rnd() & 1; | |
| 137 | 1 | ac_pred = rnd() & 1; | |
| 138 | } | ||
| 139 | 2 | s->h263_aic = h263_aic; | |
| 140 | 2 | s->ac_pred = ac_pred; | |
| 141 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (s->ac_pred) |
| 142 | 2 | last_nonzero_coeff = 63; | |
| 143 | } | ||
| 144 |
2/2✓ Branch 0 taken 234 times.
✓ Branch 1 taken 4 times.
|
238 | for (int i = intra; i <= last_nonzero_coeff; ++i) { |
| 145 | 234 | int random = rnd(); | |
| 146 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 122 times.
|
234 | if (random & 1) |
| 147 | 112 | continue; | |
| 148 | 122 | 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 | 122 | block[permutation[i]] = sign_extend(random, 10); | |
| 152 | } | ||
| 153 | 4 | } | |
| 154 | |||
| 155 | 7 | 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 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | uint16_t *matrix = intra ? s->intra_matrix : s->inter_matrix; |
| 160 | |||
| 161 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | if (type == MPEG2) |
| 162 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | qscale = s->q_scale_type ? ff_mpeg2_non_linear_qscale[qscale] : qscale << 1; |
| 163 | |||
| 164 |
2/2✓ Branch 0 taken 448 times.
✓ Branch 1 taken 7 times.
|
455 | for (int i = 0; i < 64; ++i) |
| 165 | 448 | matrix[i] = 1 + rnd() % 254; | |
| 166 | |||
| 167 | 7 | const uint8_t *permutation = s->intra_scantable.permutated; | |
| 168 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
|
7 | if (intra) { |
| 169 | 3 | block[0] = (int8_t)rnd(); | |
| 170 |
2/2✓ Branch 0 taken 159 times.
✓ Branch 1 taken 3 times.
|
162 | for (int i = 1; i <= last_nonzero_coeff; ++i) { |
| 171 | 159 | int j = permutation[i]; | |
| 172 | 159 | unsigned random = rnd(); | |
| 173 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 79 times.
|
159 | if (random & 1) |
| 174 | 80 | continue; | |
| 175 | 79 | 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 | 79 | int limit = FFMIN(INT16_MAX / (qscale * matrix[j]), 2047); | |
| 181 | 79 | block[j] = random % (2 * limit + 1) - limit; | |
| 182 | } | ||
| 183 | } else { | ||
| 184 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 4 times.
|
220 | for (int i = 0; i <= last_nonzero_coeff; ++i) { |
| 185 | 216 | int j = permutation[i]; | |
| 186 | 216 | unsigned random = rnd(); | |
| 187 |
2/2✓ Branch 0 taken 107 times.
✓ Branch 1 taken 109 times.
|
216 | if (random & 1) |
| 188 | 107 | continue; | |
| 189 | 109 | random >>= 1; | |
| 190 |
1/2✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
|
109 | int limit = FFMIN((INT16_MAX / (qscale * matrix[j]) - 1) / 2, 2047); |
| 191 | 109 | block[j] = random % (2 * limit + 1) - limit; | |
| 192 | } | ||
| 193 | } | ||
| 194 | 7 | } | |
| 195 | |||
| 196 | 14 | void checkasm_check_mpegvideo_unquantize(void) | |
| 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 | 14 | int q_scale_type = rnd() & 1; | |
| 216 | |||
| 217 | 14 | ff_mpv_unquantize_init(&unquant_dsp_ctx, 1 /* bitexact */, q_scale_type); | |
| 218 | 14 | declare_func(void, const MPVContext *s, int16_t *block, int n, int qscale); | |
| 219 | |||
| 220 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 14 times.
|
98 | for (size_t i = 0; i < FF_ARRAY_ELEMS(tests); ++i) { |
| 221 | 84 | void (*func)(const MPVContext *s, int16_t *block, int n, int qscale) = | |
| 222 | 84 | *(void (**)(const MPVContext *, int16_t *, int, int))((char*)&unquant_dsp_ctx + tests[i].offset); | |
| 223 |
2/2✓ Branch 3 taken 11 times.
✓ Branch 4 taken 73 times.
|
84 | 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 |
3/4✓ Branch 1 taken 12056 times.
✓ Branch 2 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
|
12067 | randomize_struct(MPVContext, &ref); |
| 230 | |||
| 231 | 11 | ref.q_scale_type = q_scale_type; | |
| 232 | |||
| 233 | 11 | init_idct_scantable(&ref, tests[i].intra_scantable); | |
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 | if (block_last_index < 0) |
| 236 | 1 | block_last_index = rnd() % 64; | |
| 237 | |||
| 238 | 11 | memset(block_ref, 0, sizeof(block_ref)); | |
| 239 | |||
| 240 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
|
11 | if (tests[i].intra) { |
| 241 | // Less restricted than real dc_scale values | ||
| 242 | 5 | ref.y_dc_scale = 1 + rnd() % 64; | |
| 243 | 5 | ref.c_dc_scale = 1 + rnd() % 64; | |
| 244 | } | ||
| 245 | |||
| 246 | static int qscale = 0; | ||
| 247 | |||
| 248 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 | if (qscale == 0) |
| 249 | 1 | qscale = 1 + rnd() % 31; | |
| 250 | |||
| 251 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
|
11 | if (tests[i].type == H263) |
| 252 | 4 | init_h263_test(&ref, block_ref, block_last_index, qscale, | |
| 253 | 4 | tests[i].intra); | |
| 254 | else | ||
| 255 | 7 | init_mpeg12_test(&ref, block_ref, block_last_index, qscale, | |
| 256 | 7 | tests[i].intra, tests[i].type); | |
| 257 | |||
| 258 | 11 | int n = rnd() % 6; | |
| 259 | 11 | ref.block_last_index[n] = block_last_index; | |
| 260 | |||
| 261 | 11 | memcpy(&new, &ref, sizeof(new)); | |
| 262 | 11 | memcpy(block_new, block_ref, sizeof(block_new)); | |
| 263 | |||
| 264 | 11 | call_ref(&ref, block_ref, n, qscale); | |
| 265 | 11 | call_new(&new, block_new, n, qscale); | |
| 266 | |||
| 267 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
11 | if (memcmp(&ref, &new, sizeof(new)) || memcmp(block_new, block_ref, sizeof(block_new))) |
| 268 | ✗ | fail(); | |
| 269 | |||
| 270 |
1/8✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
|
11 | bench_new(&new, block_new, n, qscale); |
| 271 | } | ||
| 272 | 84 | report("%s", tests[i].name); | |
| 273 | } | ||
| 274 | 14 | } | |
| 275 |