/* C1V2 video decoder: r32x/c1.py's decode_frame, made fast enough for the SH-2 while * staying exact (every shortcut skips only terms that are zero). */ #include "c1dec.h" #include "c1_tables.h" #include #if defined(__sh__) || defined(__SH2__) #define C1_SH2 1 #define HOT __attribute__((section(".sdram_text"), noinline)) /* SDRAM code (build_c1.sh puts it in .data, padded with NOPs); noinline, or LTO inlines it into ROM callers */ /* c1asm.s */ void c1_idct_mac(c1_fast_t *f, int rows, int cols); void c1_conv_mb(const uint8_t *y, const uint8_t *cb, const uint8_t *cr, volatile uint32_t *dst, const uint8_t *tabs); #else #define C1_SH2 0 #define HOT #endif /* Each CPU passes its own c1_fast_t (scratch + table copies), in SDRAM. Measured * 2026-11-04 in sega32x-python: the cache's RAM half (two-way mode) was slightly slower. */ _Static_assert(offsetof(c1_fast_t, res) == 256 && offsetof(c1_fast_t, coef) == 512 && offsetof(c1_fast_t, kt) == 768 && offsetof(c1_fast_t, cl5) == 1024 && offsetof(c1_fast_t, cbt) == 1792 && offsetof(c1_fast_t, crt) == 2816, "c1asm.s depends on these offsets"); static inline int32_t fix16(int32_t k, int c) { return (k * (c - 128) + 32768) >> 16; } /* r32x/c1.py */ void c1_fast_init(c1_fast_t *f) { for (int i = 0; i < 64; i++) { f->kt[(i & 7) * 8 + (i >> 3)] = C1_K[i]; /* C1_K[v*8+c] -> kt[c*8+v] */ f->zigzag[i] = C1_ZIGZAG[i]; f->mintra[i] = C1_MINTRA[i]; } for (int i = 0; i < 768; i++) f->cl5[i] = C1_CL5[i]; for (int c = 0; c < 256; c++) { f->cbt[c] = (int32_t)((uint32_t)fix16(C1_K_BCB, c) << 16 | ((uint32_t)fix16(C1_K_GCB, c) & 0xFFFF)); f->crt[c] = (int32_t)((uint32_t)fix16(C1_K_RCR, c) << 16 | ((uint32_t)fix16(C1_K_GCR, c) & 0xFFFF)); } } enum { SKIP, INTER0, INTER, INTRA }; typedef struct { const uint8_t *p; uint32_t buf; /* next bits, MSB first */ int n; /* valid bits in buf */ } bits_t; static inline void refill(bits_t *b) { while (b->n <= 24) { b->buf |= (uint32_t)(*b->p++) << (24 - b->n); b->n += 8; } } static inline unsigned getu(bits_t *b, int k) { if (k == 0) return 0; refill(b); unsigned v = b->buf >> (32 - k); b->buf <<= k; b->n -= k; return v; } static inline unsigned get1(bits_t *b) { refill(b); unsigned v = b->buf >> 31; b->buf <<= 1; b->n--; return v; } static inline unsigned getue(bits_t *b) { refill(b); uint32_t x = b->buf; int z = 0; while (!(x & 0x80000000u) && z < 24) { x <<= 1; z++; } b->buf = x << 1; b->n -= z + 1; return ((1u << z) - 1) + getu(b, z); } static inline int getse(bits_t *b) { unsigned k = getue(b); return (k & 1) ? (int)((k + 1) >> 1) : -(int)(k >> 1); } static inline int clip8(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; } /* Run-level pairs from zigzag position start into blk (zeroed by the caller), each level * times step[raster index] (or q when step is 0). rows, cols: one past the last non-zero * row and column so far. */ static inline int pairs(bits_t *b, const uint8_t *zz, int16_t *blk, int start, const int16_t *step, int q, int *rows, int *cols) { int pos = start; for (;;) { pos += (int)getue(b); unsigned k = getue(b); if (pos > 63) return -1; int mag = (int)(k >> 1) + 1; int lv = (k & 1) ? -mag : mag; int r = zz[pos]; blk[r] = (int16_t)(lv * (step ? step[r] : q)); if ((r >> 3) >= *rows) *rows = (r >> 3) + 1; if ((r & 7) >= *cols) *cols = (r & 7) + 1; pos++; if (get1(b)) return 0; } } /* Arithmetic x >> 12 (floor) without libgcc: the SH-2 has no barrel shifter and gcc calls * ___ashiftrt_r4_12 (twelve SHARs). A logical shift of the offset value is three. */ static inline int32_t asr12(int32_t x) { return (int32_t)(((uint32_t)x ^ 0x80000000u) >> 12) - 0x80000; } /* out[y][x] = (sum_u K[u][y] T[u][x] + 2048) >> 12, T[u][x] = (sum_v X[u][v] K[v][x] + 2048) >> 12, * over the non-zero rows u < rows and columns v < cols only (the rest contribute 0). */ HOT static void idct(c1_fast_t *f, const int16_t *x, int rows, int cols, int32_t *out) { int32_t *t = f->t; const int16_t *KT = f->kt; for (int u = 0; u < rows; u++) { const int16_t *row = x + u * 8; for (int c = 0; c < 8; c++) { int32_t s = 2048; for (int v = 0; v < cols; v++) s += (int32_t)row[v] * KT[c * 8 + v]; t[u * 8 + c] = asr12(s); } } for (int y = 0; y < 8; y++) { int32_t k0 = KT[y * 8]; for (int c = 0; c < 8; c++) { int32_t s = 2048 + k0 * t[c]; for (int u = 1; u < rows; u++) s += (int32_t)KT[y * 8 + u] * t[u * 8 + c]; out[y * 8 + c] = asr12(s); } } } /* f->res = the IDCT of f->coef (rows/cols: one past the last non-zero row/column). * A DC-only block's outputs are all equal under the spec's arithmetic (K's row 0 is * constant), so it is a fill. Otherwise the assembly on the SH-2, the C on the host. */ static inline void idct_run(c1_fast_t *f, int rows, int cols) { if (rows == 1 && cols == 1) { int32_t k0 = f->kt[0]; int32_t v = asr12(2048 + k0 * asr12(2048 + f->coef[0] * k0)); for (int i = 0; i < 64; i++) f->res[i] = v; return; } #if C1_SH2 c1_idct_mac(f, rows, cols); #else idct(f, f->coef, rows, cols, f->res); #endif } /* n x n block of plane (h x w) at (y0, x0), coordinates clamped, copied to dst (stride w). */ static inline void mc_copy(const uint8_t *plane, int w, int h, int y0, int x0, int n, uint8_t *dst) { if (y0 >= 0 && x0 >= 0 && y0 + n <= h && x0 + n <= w) { const uint8_t *s = plane + y0 * w + x0; if (((uintptr_t)s & 3) == 0) { for (int r = 0; r < n; r++, s += w, dst += w) { const uint32_t *s4 = (const uint32_t *)s; uint32_t *d4 = (uint32_t *)dst; for (int c = 0; c < n / 4; c++) d4[c] = s4[c]; } } else { for (int r = 0; r < n; r++, s += w, dst += w) for (int c = 0; c < n; c++) dst[c] = s[c]; } return; } for (int r = 0; r < n; r++, dst += w) { int yy = y0 + r; yy = yy < 0 ? 0 : yy >= h ? h - 1 : yy; const uint8_t *s = plane + yy * w; for (int c = 0; c < n; c++) { int xx = x0 + c; xx = xx < 0 ? 0 : xx >= w ? w - 1 : xx; dst[c] = s[xx]; } } } /* dst (8 x 8, stride w) = clip(dst + res), or clip(128 + res) when intra. */ static inline void add_clip(uint8_t *dst, int w, const int32_t *res, int intra) { for (int r = 0; r < 8; r++, dst += w) for (int c = 0; c < 8; c++) dst[c] = (uint8_t)clip8((intra ? 128 : dst[c]) + res[r * 8 + c]); } static inline void copy_mb(const c1_planes *ref, c1_planes *cur, int my, int mx) { for (int r = 0; r < 16; r++) { const uint32_t *s = (const uint32_t *)(ref->y + (my * 16 + r) * C1_W + mx * 16); uint32_t *d = (uint32_t *)(cur->y + (my * 16 + r) * C1_W + mx * 16); d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; d[3] = s[3]; } for (int r = 0; r < 8; r++) { int o = (my * 8 + r) * (C1_W / 2) + mx * 8; const uint32_t *s = (const uint32_t *)(ref->cb + o); uint32_t *d = (uint32_t *)(cur->cb + o); d[0] = s[0]; d[1] = s[1]; s = (const uint32_t *)(ref->cr + o); d = (uint32_t *)(cur->cr + o); d[0] = s[0]; d[1] = s[1]; } } HOT int c1_decode_half(c1_fast_t *f, const uint8_t *payload, int h, const c1_planes *ref, c1_planes *cur, uint8_t *changed) { int q = payload[0] << 8 | payload[1]; int off = h ? (payload[2] << 8 | payload[3]) : 4; bits_t b = { payload + off, 0, 0 }; int16_t *si = f->si, *coef = f->coef; int32_t *res = f->res; for (int k = 0; k < 64; k++) { int s = (q * f->mintra[k] + 8) >> 4; si[k] = (int16_t)(s < 1 ? 1 : s); } si[0] = 8; int pos = h ? C1_HALF : 0, end = pos + C1_HALF; int my = pos / C1_MBX, mx = 0, mvy = 0, mvx = 0; for (;;) { int run = (int)getue(&b); for (int k = 0; k < run && pos < end; k++, pos++) { copy_mb(ref, cur, my, mx); changed[pos] = 0; if (++mx == C1_MBX) { mx = 0; my++; mvy = mvx = 0; } } if (pos >= end) break; int mode = get1(&b) ? INTER : get1(&b) ? INTER0 : INTRA; changed[pos] = 1; int ys = my * 16, xs = mx * 16, cy = my * 8, cx = mx * 8; uint8_t *dy = cur->y + ys * C1_W + xs; uint8_t *dcb = cur->cb + cy * (C1_W / 2) + cx; uint8_t *dcr = cur->cr + cy * (C1_W / 2) + cx; if (mode != INTRA) { mvy += getse(&b); mvx += getse(&b); mc_copy(ref->y, C1_W, C1_ROWS, ys + mvy, xs + mvx, 16, dy); mc_copy(ref->cb, C1_W / 2, C1_ROWS / 2, cy + (mvy >> 1), cx + (mvx >> 1), 8, dcb); mc_copy(ref->cr, C1_W / 2, C1_ROWS / 2, cy + (mvy >> 1), cx + (mvx >> 1), 8, dcr); if (mode == INTER) { unsigned cbp = getu(&b, 6); for (int k = 0; k < 6; k++) { if (!(cbp & (32u >> k))) continue; for (int i = 0; i < 64; i++) coef[i] = 0; int rows = 0, cols = 0; if (pairs(&b, f->zigzag, coef, 0, 0, q, &rows, &cols)) return -1; idct_run(f, rows, cols); if (k < 4) add_clip(dy + (k >> 1) * 8 * C1_W + (k & 1) * 8, C1_W, res, 0); else add_clip(k == 4 ? dcb : dcr, C1_W / 2, res, 0); } } } else { int prev_dc = 0; for (int k = 0; k < 6; k++) { int d = getse(&b); int dc = (k >= 1 && k <= 3) ? prev_dc + d : d; if (k < 4) prev_dc = dc; for (int i = 0; i < 64; i++) coef[i] = 0; int rows = 1, cols = 1; if (get1(&b) && pairs(&b, f->zigzag, coef, 1, si, q, &rows, &cols)) return -1; coef[0] = (int16_t)(dc * 8); idct_run(f, rows, cols); if (k < 4) add_clip(dy + (k >> 1) * 8 * C1_W + (k & 1) * 8, C1_W, res, 1); else add_clip(k == 4 ? dcb : dcr, C1_W / 2, res, 1); } } pos++; if (++mx == C1_MBX) { mx = 0; my++; mvy = mvx = 0; } if (pos >= end) break; } return 0; } HOT void c1_to_rgb555(const c1_fast_t *f, const c1_planes *p, volatile uint16_t *dst, const uint8_t *mask, int mby0, int mby1) { const uint8_t *cl = f->cl5 + 256; for (int my = mby0; my < mby1; my++) { for (int mx = 0; mx < C1_MBX; mx++) { if (!mask[my * C1_MBX + mx]) continue; #if C1_SH2 c1_conv_mb(p->y + my * 16 * C1_W + mx * 16, p->cb + my * 8 * (C1_W / 2) + mx * 8, p->cr + my * 8 * (C1_W / 2) + mx * 8, (volatile uint32_t *)(dst + my * 16 * C1_W + mx * 16), f->cl5); continue; #endif for (int r = my * 16; r < my * 16 + 16; r += 2) { const uint8_t *y0 = p->y + r * C1_W + mx * 16; const uint8_t *y1 = y0 + C1_W; const uint8_t *cb = p->cb + (r >> 1) * (C1_W / 2) + mx * 8; const uint8_t *cr = p->cr + (r >> 1) * (C1_W / 2) + mx * 8; #if defined(__sh__) || defined(__SH2__) volatile uint32_t *d0 = (volatile uint32_t *)(dst + r * C1_W + mx * 16); volatile uint32_t *d1 = (volatile uint32_t *)(dst + (r + 1) * C1_W + mx * 16); #else volatile uint16_t *d0 = dst + r * C1_W + mx * 16; volatile uint16_t *d1 = d0 + C1_W; #endif for (int x = 0; x < 8; x++) { int32_t v = cr[x] - 128, u = cb[x] - 128; /* One table pointer per channel: each lookup is then @(R0 = y, Rn). */ const uint8_t *tr = cl + ((C1_K_RCR * v + 32768) >> 16); const uint8_t *tg = cl + ((C1_K_GCB * u + 32768) >> 16) + ((C1_K_GCR * v + 32768) >> 16); const uint8_t *tb = cl + ((C1_K_BCB * u + 32768) >> 16); #define PX(yv) ((uint16_t)(tr[yv] | (tg[yv] << 5) | (tb[yv] << 10))) uint16_t pa = PX(y0[2 * x]), pb = PX(y0[2 * x + 1]); uint16_t pc = PX(y1[2 * x]), pe = PX(y1[2 * x + 1]); #undef PX #if defined(__sh__) || defined(__SH2__) d0[x] = (uint32_t)pa << 16 | pb; /* big-endian: pa at the lower address */ d1[x] = (uint32_t)pc << 16 | pe; #else d0[2 * x] = pa; d0[2 * x + 1] = pb; d1[2 * x] = pc; d1[2 * x + 1] = pe; #endif } } } } } void c1_line_table(volatile uint16_t *table, uint16_t base) { for (int i = 0; i < C1_LINES; i++) table[i] = (uint16_t)(base + C1_LINE_ROW[i] * C1_W); for (int i = C1_LINES; i < 256; i++) table[i] = base; } #ifdef C1_BENCH /* Kernel timings for the player's profile mode (clock ticks; keep each under the 16-bit * FRT's wrap): 0 copy_mb x240, 1 MC copy x240, 2 full IDCT x240, 3 2x2 IDCT x240, * 4 DC-only IDCT x240, 5 convert 60 MBs into SDRAM, 6 convert the same 60 MBs into the * frame buffer. */ static uint16_t bench_buf[48 * C1_W] __attribute__((aligned(4))); /* MBs 0-59 are rows 0-47 */ HOT void c1_bench(c1_fast_t *f, const c1_planes *ref, c1_planes *cur, volatile uint16_t *fb, uint16_t (*clock)(void), uint16_t *out) { static int16_t coef[64]; static int32_t res[64]; static uint8_t mask[C1_NMB]; volatile int32_t sink = 0; uint16_t t; t = clock(); for (int k = 0; k < C1_NMB; k++) copy_mb(ref, cur, k / C1_MBX, k % C1_MBX); out[0] = (uint16_t)(clock() - t); t = clock(); for (int k = 0; k < C1_NMB; k++) { int my = k / C1_MBX, mx = k % C1_MBX; mc_copy(ref->y, C1_W, C1_ROWS, my * 16 + 1, mx * 16 + 1, 16, cur->y + my * 16 * C1_W + mx * 16); mc_copy(ref->cb, C1_W / 2, C1_ROWS / 2, my * 8, mx * 8, 8, cur->cb + my * 8 * (C1_W / 2) + mx * 8); mc_copy(ref->cr, C1_W / 2, C1_ROWS / 2, my * 8, mx * 8, 8, cur->cr + my * 8 * (C1_W / 2) + mx * 8); } out[1] = (uint16_t)(clock() - t); static const int shapes[3][2] = { { 8, 8 }, { 2, 2 }, { 1, 1 } }; for (int sh = 0; sh < 3; sh++) { t = clock(); for (int k = 0; k < C1_NMB; k++) { f->coef[0] = (int16_t)k; f->coef[9] = (int16_t)(k & 7); idct_run(f, shapes[sh][0], shapes[sh][1]); sink += f->res[k & 63]; } out[2 + sh] = (uint16_t)(clock() - t); } for (int k = 0; k < C1_NMB; k++) mask[k] = k < 60; t = clock(); c1_to_rgb555(f, cur, bench_buf, mask, 0, C1_MBY); out[5] = (uint16_t)(clock() - t); t = clock(); c1_to_rgb555(f, cur, fb, mask, 0, C1_MBY); out[6] = (uint16_t)(clock() - t); (void)sink; } #endif