vc1dsp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (c) 2022 Ben Avison
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <string.h>
  21. #include "checkasm.h"
  22. #include "libavcodec/vc1dsp.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/internal.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/mem_internal.h"
  27. #define VC1DSP_TEST(func) { #func, offsetof(VC1DSPContext, func) },
  28. #define VC1DSP_SIZED_TEST(func, width, height) { #func, offsetof(VC1DSPContext, func), width, height },
  29. typedef struct {
  30. const char *name;
  31. size_t offset;
  32. int width;
  33. int height;
  34. } test;
  35. typedef struct matrix {
  36. size_t width;
  37. size_t height;
  38. float d[];
  39. } matrix;
  40. static const matrix T8 = { 8, 8, {
  41. 12, 12, 12, 12, 12, 12, 12, 12,
  42. 16, 15, 9, 4, -4, -9, -15, -16,
  43. 16, 6, -6, -16, -16, -6, 6, 16,
  44. 15, -4, -16, -9, 9, 16, 4, -15,
  45. 12, -12, -12, 12, 12, -12, -12, 12,
  46. 9, -16, 4, 15, -15, -4, 16, -9,
  47. 6, -16, 16, -6, -6, 16, -16, 6,
  48. 4, -9, 15, -16, 16, -15, 9, -4
  49. } };
  50. static const matrix T4 = { 4, 4, {
  51. 17, 17, 17, 17,
  52. 22, 10, -10, -22,
  53. 17, -17, -17, 17,
  54. 10, -22, 22, -10
  55. } };
  56. static const matrix T8t = { 8, 8, {
  57. 12, 16, 16, 15, 12, 9, 6, 4,
  58. 12, 15, 6, -4, -12, -16, -16, -9,
  59. 12, 9, -6, -16, -12, 4, 16, 15,
  60. 12, 4, -16, -9, 12, 15, -6, -16,
  61. 12, -4, -16, 9, 12, -15, -6, 16,
  62. 12, -9, -6, 16, -12, -4, 16, -15,
  63. 12, -15, 6, 4, -12, 16, -16, 9,
  64. 12, -16, 16, -15, 12, -9, 6, -4
  65. } };
  66. static const matrix T4t = { 4, 4, {
  67. 17, 22, 17, 10,
  68. 17, 10, -17, -22,
  69. 17, -10, -17, 22,
  70. 17, -22, 17, -10
  71. } };
  72. static matrix *new_matrix(size_t width, size_t height)
  73. {
  74. matrix *out = av_mallocz(sizeof (matrix) + height * width * sizeof (float));
  75. if (out == NULL) {
  76. fprintf(stderr, "Memory allocation failure\n");
  77. exit(EXIT_FAILURE);
  78. }
  79. out->width = width;
  80. out->height = height;
  81. return out;
  82. }
  83. static matrix *multiply(const matrix *a, const matrix *b)
  84. {
  85. matrix *out;
  86. if (a->width != b->height) {
  87. fprintf(stderr, "Incompatible multiplication\n");
  88. exit(EXIT_FAILURE);
  89. }
  90. out = new_matrix(b->width, a->height);
  91. for (int j = 0; j < out->height; ++j)
  92. for (int i = 0; i < out->width; ++i) {
  93. float sum = 0;
  94. for (int k = 0; k < a->width; ++k)
  95. sum += a->d[j * a->width + k] * b->d[k * b->width + i];
  96. out->d[j * out->width + i] = sum;
  97. }
  98. return out;
  99. }
  100. static void normalise(matrix *a)
  101. {
  102. for (int j = 0; j < a->height; ++j)
  103. for (int i = 0; i < a->width; ++i) {
  104. float *p = a->d + j * a->width + i;
  105. *p *= 64;
  106. if (a->height == 4)
  107. *p /= (const unsigned[]) { 289, 292, 289, 292 } [j];
  108. else
  109. *p /= (const unsigned[]) { 288, 289, 292, 289, 288, 289, 292, 289 } [j];
  110. if (a->width == 4)
  111. *p /= (const unsigned[]) { 289, 292, 289, 292 } [i];
  112. else
  113. *p /= (const unsigned[]) { 288, 289, 292, 289, 288, 289, 292, 289 } [i];
  114. }
  115. }
  116. static void divide_and_round_nearest(matrix *a, float by)
  117. {
  118. for (int j = 0; j < a->height; ++j)
  119. for (int i = 0; i < a->width; ++i) {
  120. float *p = a->d + j * a->width + i;
  121. *p = rintf(*p / by);
  122. }
  123. }
  124. static void tweak(matrix *a)
  125. {
  126. for (int j = 4; j < a->height; ++j)
  127. for (int i = 0; i < a->width; ++i) {
  128. float *p = a->d + j * a->width + i;
  129. *p += 1;
  130. }
  131. }
  132. /* The VC-1 spec places restrictions on the values permitted at three
  133. * different stages:
  134. * - D: the input coefficients in frequency domain
  135. * - E: the intermediate coefficients, inverse-transformed only horizontally
  136. * - R: the fully inverse-transformed coefficients
  137. *
  138. * To fully cater for the ranges specified requires various intermediate
  139. * values to be held to 17-bit precision; yet these conditions do not appear
  140. * to be utilised in real-world streams. At least some assembly
  141. * implementations have chosen to restrict these values to 16-bit precision,
  142. * to accelerate the decoding of real-world streams at the cost of strict
  143. * adherence to the spec. To avoid our test marking these as failures,
  144. * reduce our random inputs.
  145. */
  146. #define ATTENUATION 4
  147. static matrix *generate_inverse_quantized_transform_coefficients(size_t width, size_t height)
  148. {
  149. matrix *raw, *tmp, *D, *E, *R;
  150. raw = new_matrix(width, height);
  151. for (int i = 0; i < width * height; ++i)
  152. raw->d[i] = (int) (rnd() % (1024/ATTENUATION)) - 512/ATTENUATION;
  153. tmp = multiply(height == 8 ? &T8 : &T4, raw);
  154. D = multiply(tmp, width == 8 ? &T8t : &T4t);
  155. normalise(D);
  156. divide_and_round_nearest(D, 1);
  157. for (int i = 0; i < width * height; ++i) {
  158. if (D->d[i] < -2048/ATTENUATION || D->d[i] > 2048/ATTENUATION-1) {
  159. /* Rare, so simply try again */
  160. av_free(raw);
  161. av_free(tmp);
  162. av_free(D);
  163. return generate_inverse_quantized_transform_coefficients(width, height);
  164. }
  165. }
  166. E = multiply(D, width == 8 ? &T8 : &T4);
  167. divide_and_round_nearest(E, 8);
  168. for (int i = 0; i < width * height; ++i)
  169. if (E->d[i] < -4096/ATTENUATION || E->d[i] > 4096/ATTENUATION-1) {
  170. /* Rare, so simply try again */
  171. av_free(raw);
  172. av_free(tmp);
  173. av_free(D);
  174. av_free(E);
  175. return generate_inverse_quantized_transform_coefficients(width, height);
  176. }
  177. R = multiply(height == 8 ? &T8t : &T4t, E);
  178. tweak(R);
  179. divide_and_round_nearest(R, 128);
  180. for (int i = 0; i < width * height; ++i)
  181. if (R->d[i] < -512/ATTENUATION || R->d[i] > 512/ATTENUATION-1) {
  182. /* Rare, so simply try again */
  183. av_free(raw);
  184. av_free(tmp);
  185. av_free(D);
  186. av_free(E);
  187. av_free(R);
  188. return generate_inverse_quantized_transform_coefficients(width, height);
  189. }
  190. av_free(raw);
  191. av_free(tmp);
  192. av_free(E);
  193. av_free(R);
  194. return D;
  195. }
  196. #define RANDOMIZE_BUFFER16(name, size) \
  197. do { \
  198. int i; \
  199. for (i = 0; i < size; ++i) { \
  200. uint16_t r = rnd(); \
  201. AV_WN16A(name##0 + i, r); \
  202. AV_WN16A(name##1 + i, r); \
  203. } \
  204. } while (0)
  205. #define RANDOMIZE_BUFFER8(name, size) \
  206. do { \
  207. int i; \
  208. for (i = 0; i < size; ++i) { \
  209. uint8_t r = rnd(); \
  210. name##0[i] = r; \
  211. name##1[i] = r; \
  212. } \
  213. } while (0)
  214. #define RANDOMIZE_BUFFER8_MID_WEIGHTED(name, size) \
  215. do { \
  216. uint8_t *p##0 = name##0, *p##1 = name##1; \
  217. int i = (size); \
  218. while (i-- > 0) { \
  219. int x = 0x80 | (rnd() & 0x7F); \
  220. x >>= rnd() % 9; \
  221. if (rnd() & 1) \
  222. x = -x; \
  223. *p##1++ = *p##0++ = 0x80 + x; \
  224. } \
  225. } while (0)
  226. static void check_inv_trans_inplace(void)
  227. {
  228. /* Inverse transform input coefficients are stored in a 16-bit buffer
  229. * with row stride of 8 coefficients irrespective of transform size.
  230. * vc1_inv_trans_8x8 differs from the others in two ways: coefficients
  231. * are stored in column-major order, and the outputs are written back
  232. * to the input buffer, so we oversize it slightly to catch overruns. */
  233. LOCAL_ALIGNED_16(int16_t, inv_trans_in0, [10 * 8]);
  234. LOCAL_ALIGNED_16(int16_t, inv_trans_in1, [10 * 8]);
  235. VC1DSPContext h;
  236. ff_vc1dsp_init(&h);
  237. if (check_func(h.vc1_inv_trans_8x8, "vc1dsp.vc1_inv_trans_8x8")) {
  238. matrix *coeffs;
  239. declare_func(void, int16_t *);
  240. RANDOMIZE_BUFFER16(inv_trans_in, 10 * 8);
  241. coeffs = generate_inverse_quantized_transform_coefficients(8, 8);
  242. for (int j = 0; j < 8; ++j)
  243. for (int i = 0; i < 8; ++i) {
  244. int idx = 8 + i * 8 + j;
  245. inv_trans_in1[idx] = inv_trans_in0[idx] = coeffs->d[j * 8 + i];
  246. }
  247. call_ref(inv_trans_in0 + 8);
  248. call_new(inv_trans_in1 + 8);
  249. if (memcmp(inv_trans_in0, inv_trans_in1, 10 * 8 * sizeof (int16_t)))
  250. fail();
  251. bench_new(inv_trans_in1 + 8);
  252. av_free(coeffs);
  253. }
  254. }
  255. static void check_inv_trans_adding(void)
  256. {
  257. /* Inverse transform input coefficients are stored in a 16-bit buffer
  258. * with row stride of 8 coefficients irrespective of transform size. */
  259. LOCAL_ALIGNED_16(int16_t, inv_trans_in0, [8 * 8]);
  260. LOCAL_ALIGNED_16(int16_t, inv_trans_in1, [8 * 8]);
  261. /* For all but vc1_inv_trans_8x8, the inverse transform is narrowed and
  262. * added with saturation to an array of unsigned 8-bit values. Oversize
  263. * this by 8 samples left and right and one row above and below. */
  264. LOCAL_ALIGNED_8(uint8_t, inv_trans_out0, [10 * 24]);
  265. LOCAL_ALIGNED_8(uint8_t, inv_trans_out1, [10 * 24]);
  266. VC1DSPContext h;
  267. const test tests[] = {
  268. VC1DSP_SIZED_TEST(vc1_inv_trans_8x4, 8, 4)
  269. VC1DSP_SIZED_TEST(vc1_inv_trans_4x8, 4, 8)
  270. VC1DSP_SIZED_TEST(vc1_inv_trans_4x4, 4, 4)
  271. VC1DSP_SIZED_TEST(vc1_inv_trans_8x8_dc, 8, 8)
  272. VC1DSP_SIZED_TEST(vc1_inv_trans_8x4_dc, 8, 4)
  273. VC1DSP_SIZED_TEST(vc1_inv_trans_4x8_dc, 4, 8)
  274. VC1DSP_SIZED_TEST(vc1_inv_trans_4x4_dc, 4, 4)
  275. };
  276. ff_vc1dsp_init(&h);
  277. for (size_t t = 0; t < FF_ARRAY_ELEMS(tests); ++t) {
  278. void (*func)(uint8_t *, ptrdiff_t, int16_t *) = *(void **)((intptr_t) &h + tests[t].offset);
  279. if (check_func(func, "vc1dsp.%s", tests[t].name)) {
  280. matrix *coeffs;
  281. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *, ptrdiff_t, int16_t *);
  282. RANDOMIZE_BUFFER16(inv_trans_in, 8 * 8);
  283. RANDOMIZE_BUFFER8(inv_trans_out, 10 * 24);
  284. coeffs = generate_inverse_quantized_transform_coefficients(tests[t].width, tests[t].height);
  285. for (int j = 0; j < tests[t].height; ++j)
  286. for (int i = 0; i < tests[t].width; ++i) {
  287. int idx = j * 8 + i;
  288. inv_trans_in1[idx] = inv_trans_in0[idx] = coeffs->d[j * tests[t].width + i];
  289. }
  290. call_ref(inv_trans_out0 + 24 + 8, 24, inv_trans_in0);
  291. call_new(inv_trans_out1 + 24 + 8, 24, inv_trans_in1);
  292. if (memcmp(inv_trans_out0, inv_trans_out1, 10 * 24))
  293. fail();
  294. bench_new(inv_trans_out1 + 24 + 8, 24, inv_trans_in1 + 8);
  295. av_free(coeffs);
  296. }
  297. }
  298. }
  299. static void check_loop_filter(void)
  300. {
  301. /* Deblocking filter buffers are big enough to hold a 16x16 block,
  302. * plus 16 columns left and 4 rows above to hold filter inputs
  303. * (depending on whether v or h neighbouring block edge, oversized
  304. * horizontally to maintain 16-byte alignment) plus 16 columns and
  305. * 4 rows below to catch write overflows */
  306. LOCAL_ALIGNED_16(uint8_t, filter_buf0, [24 * 48]);
  307. LOCAL_ALIGNED_16(uint8_t, filter_buf1, [24 * 48]);
  308. VC1DSPContext h;
  309. const test tests[] = {
  310. VC1DSP_TEST(vc1_v_loop_filter4)
  311. VC1DSP_TEST(vc1_h_loop_filter4)
  312. VC1DSP_TEST(vc1_v_loop_filter8)
  313. VC1DSP_TEST(vc1_h_loop_filter8)
  314. VC1DSP_TEST(vc1_v_loop_filter16)
  315. VC1DSP_TEST(vc1_h_loop_filter16)
  316. };
  317. ff_vc1dsp_init(&h);
  318. for (size_t t = 0; t < FF_ARRAY_ELEMS(tests); ++t) {
  319. void (*func)(uint8_t *, ptrdiff_t, int) = *(void **)((intptr_t) &h + tests[t].offset);
  320. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *, ptrdiff_t, int);
  321. if (check_func(func, "vc1dsp.%s", tests[t].name)) {
  322. for (int count = 1000; count > 0; --count) {
  323. int pq = rnd() % 31 + 1;
  324. RANDOMIZE_BUFFER8_MID_WEIGHTED(filter_buf, 24 * 48);
  325. call_ref(filter_buf0 + 4 * 48 + 16, 48, pq);
  326. call_new(filter_buf1 + 4 * 48 + 16, 48, pq);
  327. if (memcmp(filter_buf0, filter_buf1, 24 * 48))
  328. fail();
  329. }
  330. }
  331. for (int j = 0; j < 24; ++j)
  332. for (int i = 0; i < 48; ++i)
  333. filter_buf1[j * 48 + i] = 0x60 + 0x40 * (i >= 16 && j >= 4);
  334. if (check_func(func, "vc1dsp.%s_bestcase", tests[t].name))
  335. bench_new(filter_buf1 + 4 * 48 + 16, 48, 1);
  336. if (check_func(func, "vc1dsp.%s_worstcase", tests[t].name))
  337. bench_new(filter_buf1 + 4 * 48 + 16, 48, 31);
  338. }
  339. }
  340. #define TEST_UNESCAPE \
  341. do { \
  342. for (int count = 100; count > 0; --count) { \
  343. escaped_offset = rnd() & 7; \
  344. unescaped_offset = rnd() & 7; \
  345. escaped_len = (1u << (rnd() % 8) + 3) - (rnd() & 7); \
  346. RANDOMIZE_BUFFER8(unescaped, UNESCAPE_BUF_SIZE); \
  347. len0 = call_ref(escaped0 + escaped_offset, escaped_len, unescaped0 + unescaped_offset); \
  348. len1 = call_new(escaped1 + escaped_offset, escaped_len, unescaped1 + unescaped_offset); \
  349. if (len0 != len1 || memcmp(unescaped0, unescaped1, UNESCAPE_BUF_SIZE)) \
  350. fail(); \
  351. } \
  352. } while (0)
  353. static void check_unescape(void)
  354. {
  355. /* This appears to be a typical length of buffer in use */
  356. #define LOG2_UNESCAPE_BUF_SIZE 17
  357. #define UNESCAPE_BUF_SIZE (1u<<LOG2_UNESCAPE_BUF_SIZE)
  358. LOCAL_ALIGNED_8(uint8_t, escaped0, [UNESCAPE_BUF_SIZE]);
  359. LOCAL_ALIGNED_8(uint8_t, escaped1, [UNESCAPE_BUF_SIZE]);
  360. LOCAL_ALIGNED_8(uint8_t, unescaped0, [UNESCAPE_BUF_SIZE]);
  361. LOCAL_ALIGNED_8(uint8_t, unescaped1, [UNESCAPE_BUF_SIZE]);
  362. VC1DSPContext h;
  363. ff_vc1dsp_init(&h);
  364. if (check_func(h.vc1_unescape_buffer, "vc1dsp.vc1_unescape_buffer")) {
  365. int len0, len1, escaped_offset, unescaped_offset, escaped_len;
  366. declare_func(int, const uint8_t *, int, uint8_t *);
  367. /* Test data which consists of escapes sequences packed as tightly as possible */
  368. for (int x = 0; x < UNESCAPE_BUF_SIZE; ++x)
  369. escaped1[x] = escaped0[x] = 3 * (x % 3 == 0);
  370. TEST_UNESCAPE;
  371. /* Test random data */
  372. RANDOMIZE_BUFFER8(escaped, UNESCAPE_BUF_SIZE);
  373. TEST_UNESCAPE;
  374. /* Test data with escape sequences at random intervals */
  375. for (int x = 0; x <= UNESCAPE_BUF_SIZE - 4;) {
  376. int gap, gap_msb;
  377. escaped1[x+0] = escaped0[x+0] = 0;
  378. escaped1[x+1] = escaped0[x+1] = 0;
  379. escaped1[x+2] = escaped0[x+2] = 3;
  380. escaped1[x+3] = escaped0[x+3] = rnd() & 3;
  381. gap_msb = 2u << (rnd() % 8);
  382. gap = (rnd() &~ -gap_msb) | gap_msb;
  383. x += gap;
  384. }
  385. TEST_UNESCAPE;
  386. /* Test data which is known to contain no escape sequences */
  387. memset(escaped0, 0xFF, UNESCAPE_BUF_SIZE);
  388. memset(escaped1, 0xFF, UNESCAPE_BUF_SIZE);
  389. TEST_UNESCAPE;
  390. /* Benchmark the no-escape-sequences case */
  391. bench_new(escaped1, UNESCAPE_BUF_SIZE, unescaped1);
  392. }
  393. }
  394. void checkasm_check_vc1dsp(void)
  395. {
  396. check_inv_trans_inplace();
  397. check_inv_trans_adding();
  398. report("inv_trans");
  399. check_loop_filter();
  400. report("loop_filter");
  401. check_unescape();
  402. report("unescape_buffer");
  403. }