/* Host test: decode a C1V2 stream with c1dec.c and write every frame's RGB555 words * (C1_ROWS x C1_FBW: the frame buffer's rows, borders included; little-endian) to OUT, * for r32x/check_host.py. * * gcc -O2 -std=c99 -o host_test host_test.c c1dec.c && ./host_test stream.c1v frames.raw */ #include #include #include #include "c1dec.h" static uint8_t planes[2][C1_ROWS * C1_W * 3 / 2]; int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "usage: host_test STREAM OUT\n"); return 2; } FILE *f = fopen(argv[1], "rb"); if (!f) { perror(argv[1]); return 1; } fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET); uint8_t *data = calloc(1, n + 8); /* the bit reader looks up to 3 bytes ahead */ if (fread(data, 1, n, f) != (size_t)n) return 1; fclose(f); if (memcmp(data, "C1V3", 4)) { fprintf(stderr, "not C1V3\n"); return 1; } uint32_t frames = (uint32_t)data[8] << 24 | data[9] << 16 | data[10] << 8 | data[11]; static c1_fast_t fast; c1_fast_init(&fast); c1_planes p[2]; for (int k = 0; k < 2; k++) { p[k].y = planes[k]; p[k].cb = planes[k] + C1_ROWS * C1_W; p[k].cr = p[k].cb + C1_ROWS * C1_W / 4; memset(p[k].y, 16, C1_ROWS * C1_W); memset(p[k].cb, 128, C1_ROWS * C1_W / 2); } /* Two frame buffers, as on the 32X: each frame converts only the MBs changed in it or * in the frame before, into the buffer that holds the picture from two frames back. */ static uint16_t fb[2][C1_ROWS * C1_FBW]; uint8_t changed[C1_NMB], prev[C1_NMB], mask[C1_NMB]; memset(prev, 1, sizeof prev); FILE *o = fopen(argv[2], "wb"); const uint8_t *s = data + 12; int cur = 0; for (uint32_t i = 0; i < frames; i++) { unsigned len = s[0] << 8 | s[1]; if (c1_decode_half(&fast, s + 2, 0, &p[cur], &p[cur ^ 1], prev, changed) || c1_decode_half(&fast, s + 2, 1, &p[cur], &p[cur ^ 1], prev, changed)) { fprintf(stderr, "frame %u: malformed\n", i); return 1; } cur ^= 1; s += 2 + len; for (int k = 0; k < C1_NMB; k++) mask[k] = changed[k] | prev[k]; memcpy(prev, changed, sizeof prev); uint16_t *back = fb[i & 1]; c1_to_rgb555(&fast, &p[cur], back, mask, 0, C1_NMB); fwrite(back, 2, C1_ROWS * C1_FBW, o); } fclose(o); printf("%u frames\n", frames); return 0; }