edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·4c07cbc·Tahinli·2 months ago·3583 commits
| 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | extern int av1_apply_selfguided_restoration_c( |
| 6 | const uint8_t *dat8, int width, int height, int stride, int eps, |
| 7 | const int *xqd, uint8_t *dst8, int dst_stride, int32_t *tmpbuf, |
| 8 | int bit_depth, int highbd); |
| 9 | extern int av1_selfguided_restoration_c(const uint8_t *dgd8, int width, |
| 10 | int height, int dgd_stride, |
| 11 | int32_t *flt0, int32_t *flt1, |
| 12 | int flt_stride, int sgr_params_idx, |
| 13 | int bit_depth, int highbd); |
| 14 | // Exported by scripts/instrument-aom-oracle.sh's rung 6 (normally `static` |
| 15 | // in restoration.c) so this harness can get real per-tap A[]/B[] instead of |
| 16 | // only the combined flt0/flt1 -- the missing half of r6's 9-tap cross-check. |
| 17 | extern void calculate_intermediate_result(int32_t *dgd, int width, int height, |
| 18 | int dgd_stride, int bit_depth, |
| 19 | int sgr_params_idx, int radius_idx, |
| 20 | int pass, int32_t *A, int32_t *B); |
| 21 | |
| 22 | #define SGRPROJ_BORDER_VERT 3 |
| 23 | #define SGRPROJ_BORDER_HORZ 3 |
| 24 | |
| 25 | int main(void) { |
| 26 | const int bw = 102, bh = 10; |
| 27 | // real libaom splits stripe_width into RESTORATION_PROC_UNIT_SIZE=64 |
| 28 | // chunks (sgrproj_filter_stripe's `for j += procunit_width` loop) -- |
| 29 | // av1_apply_selfguided_restoration_c's internal fixed-size arrays are |
| 30 | // sized for that max, so replicate the chunking (target col=6 is in |
| 31 | // the first 64-wide chunk). |
| 32 | const int w = 64, h = 4; |
| 33 | // The depth of `/tmp/lr_full.bin` is NOT in the file: the capture is a raw |
| 34 | // plane dump written by an env-gated `std::fs::write` inside |
| 35 | // `apply_sgrproj_stripe`, and whoever re-captures it from a 10/12-bit pin |
| 36 | // gets 2 bytes per sample, i.e. a file TWICE this size whose first half |
| 37 | // still looks like plausible plane data. Reading it at 1 byte per sample |
| 38 | // would then print self-guided-restoration A/B taps for the wrong pixels |
| 39 | // and nothing would fail -- so the size is the assertion: a 2x capture |
| 40 | // must not pass as an 8-bit one. |
| 41 | const size_t expect = (size_t)bw * bh; |
| 42 | uint8_t *buf = malloc(expect); |
| 43 | FILE *f = fopen("/tmp/lr_full.bin", "rb"); |
| 44 | if (!f) { perror("open"); return 1; } |
| 45 | fseek(f, 0, SEEK_END); |
| 46 | const long size = ftell(f); |
| 47 | fseek(f, 0, SEEK_SET); |
| 48 | if (size != (long)expect) { |
| 49 | fprintf(stderr, |
| 50 | "/tmp/lr_full.bin is %ld bytes, this harness reads it as %d-bit (%zu bytes: " |
| 51 | "%dx%d at 1 byte per sample). %s\n", |
| 52 | size, 8, expect, bw, bh, |
| 53 | size == 2 * (long)expect |
| 54 | ? "That is exactly a 2x-size high-bit-depth capture: re-capture it as 8-bit, " |
| 55 | "or port the harness to the uint16 arm (bit_depth=10, highbd=1) before " |
| 56 | "trusting a single printed tap." |
| 57 | : "Recapture it, or fix bw/bh to the plane the capture came from."); |
| 58 | return 1; |
| 59 | } |
| 60 | if (fread(buf, 1, expect, f) != expect) { fprintf(stderr, "short read\n"); return 1; } |
| 61 | fclose(f); |
| 62 | |
| 63 | const uint8_t *dgd8 = buf + 3 * bw + 3; // logical (0,0) |
| 64 | int xqd[2] = { -16, -32 }; |
| 65 | uint8_t dst[w * h]; |
| 66 | // flt1 = tmpbuf + RESTORATION_UNITPELS_MAX (~152100); undersizing this |
| 67 | // segfaults inside av1_apply_selfguided_restoration_c. |
| 68 | static int32_t tmpbuf[2 * 400 * 400]; |
| 69 | |
| 70 | int ret = av1_apply_selfguided_restoration_c(dgd8, w, h, bw, /*eps=*/6, xqd, |
| 71 | dst, w, tmpbuf, 8, 0); |
| 72 | printf("ret=%d\n", ret); |
| 73 | // target pixel: row i=1, col j=6 |
| 74 | printf("out[1][6] = %d\n", dst[1 * w + 6]); |
| 75 | printf("dgd[1][6] = %d\n", dgd8[1 * bw + 6]); |
| 76 | |
| 77 | static int32_t flt0[400 * 400], flt1[400 * 400]; |
| 78 | int r2 = av1_selfguided_restoration_c(dgd8, w, h, bw, flt0, flt1, w, 6, 8, 0); |
| 79 | printf("selfguided ret=%d flt0[1][6]=%d flt1[1][6]=%d\n", r2, flt0[1 * w + 6], |
| 80 | flt1[1 * w + 6]); |
| 81 | |
| 82 | // --- rung 6: real per-tap A[]/B[] ground truth for the dense (r1) arm --- |
| 83 | // Replicate av1_selfguided_restoration_c's own uint8->int32 border-extend |
| 84 | // copy (that function's `dgd32` setup, restoration.c) so |
| 85 | // calculate_intermediate_result sees exactly the same input it would from |
| 86 | // the real call path above. |
| 87 | const int dgd32_stride = w + 2 * SGRPROJ_BORDER_HORZ; |
| 88 | static int32_t dgd32_buf[(64 + 6) * (4 + 6)]; |
| 89 | int32_t *dgd32 = dgd32_buf + dgd32_stride * SGRPROJ_BORDER_VERT + SGRPROJ_BORDER_HORZ; |
| 90 | for (int i = -SGRPROJ_BORDER_VERT; i < h + SGRPROJ_BORDER_VERT; ++i) |
| 91 | for (int j = -SGRPROJ_BORDER_HORZ; j < w + SGRPROJ_BORDER_HORZ; ++j) |
| 92 | dgd32[i * dgd32_stride + j] = dgd8[i * bw + j]; |
| 93 | |
| 94 | const int width_ext = w + 2 * SGRPROJ_BORDER_HORZ; |
| 95 | const int buf_stride = ((width_ext + 3) & ~3) + 16; |
| 96 | static int32_t A_[2 * 400 * 400], B_[2 * 400 * 400]; |
| 97 | // radius_idx=1 (dense/r1 arm), pass=0 (every row, matches |
| 98 | // selfguided_restoration_internal's own call). |
| 99 | calculate_intermediate_result(dgd32, w, h, dgd32_stride, /*bit_depth=*/8, |
| 100 | /*sgr_params_idx=*/6, /*radius_idx=*/1, |
| 101 | /*pass=*/0, A_, B_); |
| 102 | int32_t *A = A_ + SGRPROJ_BORDER_VERT * buf_stride + SGRPROJ_BORDER_HORZ; |
| 103 | int32_t *B = B_ + SGRPROJ_BORDER_VERT * buf_stride + SGRPROJ_BORDER_HORZ; |
| 104 | const int ti = 1, tj = 6; |
| 105 | const char *names[9] = { "c", "u", "d", "l", "r", "ul", "ur", "dl", "dr" }; |
| 106 | const int di[9] = { 0, -1, 1, 0, 0, -1, -1, 1, 1 }; |
| 107 | const int dj[9] = { 0, 0, 0, -1, 1, -1, 1, -1, 1 }; |
| 108 | for (int t = 0; t < 9; ++t) { |
| 109 | int k = (ti + di[t]) * buf_stride + (tj + dj[t]); |
| 110 | printf("real_ab %s = (%d,%d)\n", names[t], A[k], B[k]); |
| 111 | } |
| 112 | return 0; |
| 113 | } |