forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB5c8eff8Tahinli2 months ago2949 commits

wedge_dump.c128 lines, 5.7 KiBHistoryRawDownload
1/* lane-wedge r3: standalone C reimplementation of libaom
2 * av1/common/reconinter.c's wedge master-mask + per-block codebook
3 * derivation (wedge_master_oblique_odd/even, wedge_master_vertical,
4 * shift_copy, init_wedge_master_masks, get_wedge_mask_inplace,
5 * init_wedge_masks), transcribed verbatim from the source read this round.
6 * Compiled and run independently of the Rust port to catch translation
7 * bugs (indexing, shift direction, sign) even though the table DATA is
8 * shared with the source -- see charter's shared-oracle-blindness note.
9 */
10#include <stdio.h>
11#include <stdint.h>
12#include <string.h>
13
14#define MASK_MASTER_SIZE 64
15#define MASK_MASTER_STRIDE 64
16#define WEDGE_WEIGHT_BITS 6
17#define MAX_WEDGE_TYPES 16
18
19enum { WEDGE_HORIZONTAL = 0, WEDGE_VERTICAL, WEDGE_OBLIQUE27, WEDGE_OBLIQUE63,
20 WEDGE_OBLIQUE117, WEDGE_OBLIQUE153, WEDGE_DIRECTIONS };
21
22static const uint8_t wedge_master_oblique_odd[MASK_MASTER_SIZE] = {
23 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 6, 18,
25 37, 53, 60, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
26 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
27};
28static const uint8_t wedge_master_oblique_even[MASK_MASTER_SIZE] = {
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 11, 27,
31 46, 58, 62, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
32 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
33};
34static const uint8_t wedge_master_vertical[MASK_MASTER_SIZE] = {
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 7, 21,
37 43, 57, 62, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
38 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
39};
40
41static void shift_copy(const uint8_t *src, uint8_t *dst, int shift, int width) {
42 if (shift >= 0) {
43 memcpy(dst + shift, src, width - shift);
44 memset(dst, src[0], shift);
45 } else {
46 shift = -shift;
47 memcpy(dst, src + shift, width - shift);
48 memset(dst + width - shift, src[width - 1], shift);
49 }
50}
51
52static uint8_t wedge_mask_obl[2][WEDGE_DIRECTIONS][MASK_MASTER_SIZE * MASK_MASTER_SIZE];
53
54static void init_wedge_master_masks(void) {
55 int i, j;
56 const int w = MASK_MASTER_SIZE, h = MASK_MASTER_SIZE, stride = MASK_MASTER_STRIDE;
57 int shift = h / 4;
58 for (i = 0; i < h; i += 2) {
59 shift_copy(wedge_master_oblique_even, &wedge_mask_obl[0][WEDGE_OBLIQUE63][i * stride], shift, MASK_MASTER_SIZE);
60 shift--;
61 shift_copy(wedge_master_oblique_odd, &wedge_mask_obl[0][WEDGE_OBLIQUE63][(i + 1) * stride], shift, MASK_MASTER_SIZE);
62 memcpy(&wedge_mask_obl[0][WEDGE_VERTICAL][i * stride], wedge_master_vertical, MASK_MASTER_SIZE);
63 memcpy(&wedge_mask_obl[0][WEDGE_VERTICAL][(i + 1) * stride], wedge_master_vertical, MASK_MASTER_SIZE);
64 }
65 for (i = 0; i < h; ++i) {
66 for (j = 0; j < w; ++j) {
67 const int msk = wedge_mask_obl[0][WEDGE_OBLIQUE63][i * stride + j];
68 wedge_mask_obl[0][WEDGE_OBLIQUE27][j * stride + i] = msk;
69 wedge_mask_obl[0][WEDGE_OBLIQUE117][i * stride + w - 1 - j] =
70 wedge_mask_obl[0][WEDGE_OBLIQUE153][(w - 1 - j) * stride + i] = (1 << WEDGE_WEIGHT_BITS) - msk;
71 wedge_mask_obl[1][WEDGE_OBLIQUE63][i * stride + j] =
72 wedge_mask_obl[1][WEDGE_OBLIQUE27][j * stride + i] = (1 << WEDGE_WEIGHT_BITS) - msk;
73 wedge_mask_obl[1][WEDGE_OBLIQUE117][i * stride + w - 1 - j] =
74 wedge_mask_obl[1][WEDGE_OBLIQUE153][(w - 1 - j) * stride + i] = msk;
75 const int mskx = wedge_mask_obl[0][WEDGE_VERTICAL][i * stride + j];
76 wedge_mask_obl[0][WEDGE_HORIZONTAL][j * stride + i] = mskx;
77 wedge_mask_obl[1][WEDGE_VERTICAL][i * stride + j] =
78 wedge_mask_obl[1][WEDGE_HORIZONTAL][j * stride + i] = (1 << WEDGE_WEIGHT_BITS) - mskx;
79 }
80 }
81}
82
83typedef struct { int direction, x_offset, y_offset; } wedge_code_type;
84
85/* wedge_codebook_16_heqw -- the ONLY codebook this decoder's reachable
86 * (square 8x8/16x16/32x32) leaves use. */
87static const wedge_code_type heqw[16] = {
88 { WEDGE_OBLIQUE27, 4, 4 }, { WEDGE_OBLIQUE63, 4, 4 },
89 { WEDGE_OBLIQUE117, 4, 4 }, { WEDGE_OBLIQUE153, 4, 4 },
90 { WEDGE_HORIZONTAL, 4, 2 }, { WEDGE_HORIZONTAL, 4, 6 },
91 { WEDGE_VERTICAL, 2, 4 }, { WEDGE_VERTICAL, 6, 4 },
92 { WEDGE_OBLIQUE27, 4, 2 }, { WEDGE_OBLIQUE27, 4, 6 },
93 { WEDGE_OBLIQUE153, 4, 2 }, { WEDGE_OBLIQUE153, 4, 6 },
94 { WEDGE_OBLIQUE63, 2, 4 }, { WEDGE_OBLIQUE63, 6, 4 },
95 { WEDGE_OBLIQUE117, 2, 4 }, { WEDGE_OBLIQUE117, 6, 4 },
96};
97/* wedge_signflip_lookup rows for BLOCK_8X8/16X16/32X32 -- identical row. */
98static const uint8_t signflip[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 };
99
100static const uint8_t *get_wedge_mask_inplace(int wedge_index, int neg, int bw, int bh) {
101 const wedge_code_type *a = &heqw[wedge_index];
102 const uint8_t wsignflip = signflip[wedge_index];
103 int woff = (a->x_offset * bw) >> 3;
104 int hoff = (a->y_offset * bh) >> 3;
105 return wedge_mask_obl[neg ^ wsignflip][a->direction] +
106 MASK_MASTER_STRIDE * (MASK_MASTER_SIZE / 2 - hoff) + MASK_MASTER_SIZE / 2 - woff;
107}
108
109static void dump_bsize(const char *name, int bw, int bh) {
110 for (int sign = 0; sign < 2; ++sign) {
111 for (int w = 0; w < MAX_WEDGE_TYPES; ++w) {
112 const uint8_t *mask = get_wedge_mask_inplace(w, sign, bw, bh);
113 uint64_t sum = 0;
114 for (int i = 0; i < bh; ++i)
115 for (int j = 0; j < bw; ++j)
116 sum += (uint64_t)mask[i * MASK_MASTER_STRIDE + j] * (uint64_t)(i * bw + j + 1);
117 printf("%s sign=%d idx=%d checksum=%llu\n", name, sign, w, (unsigned long long)sum);
118 }
119 }
120}
121
122int main(void) {
123 init_wedge_master_masks();
124 dump_bsize("8x8", 8, 8);
125 dump_bsize("16x16", 16, 16);
126 dump_bsize("32x32", 32, 32);
127 return 0;
128}