/* 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_idct_add(c1_fast_t *f, int rows, int cols, uint8_t *dst, int stride, int intra); /* c1bfly.s */ void c1_bf_row(const int16_t *x, int16_t *t, int dense); void c1_bf_col(const int16_t *tc, uint8_t *d, int w, int mode, int32_t bias); 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_assert(offsetof(c1_fast_t, rblk) % 4 == 0, "rblk is filled with longword stores (the SH-2 faults on unaligned ones)"); 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 i = 0; i < 64; i++) f->coef[i] = 0; /* ue() codes of up to 7 bits (z <= 3 leading zeros: values 0-14) by table. */ for (int v = 0; v < 256; v++) { int z = 0; while (z < 8 && !(v & (0x80 >> z))) z++; int len = 2 * z + 1; f->ue8[v] = z <= 3 ? (uint16_t)(len << 8 | (((1 << z) - 1) + ((v >> (8 - len)) & ((1 << z) - 1)))) : 0; } /* The butterfly's constants, x16 (see idct_bfly_add). */ f->k0 = 16 * C1_K[0]; f->ke[0] = 16 * C1_K[2 * 8 + 0]; f->ke[1] = 16 * C1_K[6 * 8 + 0]; f->ke[2] = 16 * C1_K[2 * 8 + 1]; f->ke[3] = 16 * C1_K[6 * 8 + 1]; for (int k = 0; k < 4; k++) for (int c = 0; c < 4; c++) f->ko[k * 4 + c] = 16 * C1_K[(2 * k + 1) * 8 + c]; 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 }; #ifdef C1_PROF #define PROF_T(v) uint16_t v = f->clock ? f->clock() : 0 #define PROF_ADD(k, t0) (f->clock ? (void)(f->prof[k] += (uint16_t)(f->clock() - (t0))) : (void)0) #else #define PROF_T(v) (void)0 #define PROF_ADD(k, t0) (void)0 #endif typedef struct { const uint8_t *p; uint32_t buf; /* next bits, MSB first */ int n; /* valid bits in buf */ const uint16_t *ue8; /* c1_fast_t.ue8 */ } 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); unsigned e = b->ue8[b->buf >> 24]; if (e) { /* the common short codes: one lookup */ b->buf <<= e >> 8; b->n -= (int)(e >> 8); return e & 255; } 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 C1_SH2 /* Big-endian: an unaligned row from aligned longwords, funnel-shifted, stored as * longwords (dst is aligned). The last longword read may run up to 3 bytes past * the row; only bytes inside it are used. */ #define FUNNEL(A, L, R) \ for (int r = 0; r < n; r++, s += w, dst += w) { \ const uint32_t *p = (const uint32_t *)(s - (A)); \ uint32_t *d4 = (uint32_t *)dst, w0 = p[0]; \ for (int c = 0; c < n / 4; c++) { \ uint32_t w1 = p[c + 1]; \ d4[c] = (w0 << (L)) | (w1 >> (R)); \ w0 = w1; \ } \ } \ return switch ((uintptr_t)s & 3) { case 1: FUNNEL(1, 8, 24); case 2: FUNNEL(2, 16, 16); case 3: FUNNEL(3, 24, 8); default: break; } #undef FUNNEL #endif 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]); } /* dst (8 x 8, stride w) = clip(dst + IDCT(f->coef)), or clip(128 + IDCT) when intra. */ /* The spec's IDCT, regrouped exactly (2026-11-04 review; checked on 20,000 random blocks * and on every block of both test clips). For one 8-point pass with basis inputs x0..x7, * K's integer symmetries (K[v][7-c] = (-1)^v K[v][c], K[0][c] = K[0][0], K[4][c] = * +-K[0][0], K[2], K[6] two values each) give S[c] = E[c] + O[c], S[7-c] = E[c] - O[c]: * 22 multiplies, not 64, with the same integer sum. Constants are x16, so the spec's * (S + 2048) >> 12 is (16S + 32768) >> 16: the top halfword, sign-extended (|16S| < * 3.8e8 for |coef| < 2400, the bound the encoder asserts). */ static inline int32_t hi16(int32_t x) { return (int32_t)(int16_t)((uint32_t)x >> 16); } /* A 16 x 16 -> 32 product (MULS.W): both operands fit 16 bits (x0 + x4 included: < 4800). */ #define M16(a, b) ((int32_t)(int16_t)(a) * (int32_t)(int16_t)(b)) #define BFLY_EVEN(x0, x2, x4, x6, e) \ do { \ int32_t ee0 = M16(f->k0, (x0) + (x4)), ee1 = M16(f->k0, (x0) - (x4)); \ int32_t eo0 = M16(f->ke[0], x2) + M16(f->ke[1], x6), eo1 = M16(f->ke[2], x2) + M16(f->ke[3], x6); \ e[0] = ee0 + eo0; e[1] = ee1 + eo1; e[2] = ee1 - eo1; e[3] = ee0 - eo0; \ } while (0) #define BFLY_ODD(x1, x3, x5, x7, o) \ do { \ for (int c_ = 0; c_ < 4; c_++) \ o[c_] = M16(f->ko[c_], x1) + M16(f->ko[4 + c_], x3) + M16(f->ko[8 + c_], x5) + M16(f->ko[12 + c_], x7); \ } while (0) /* dst (8 x 8, stride w) = clip(dst + IDCT(f->coef)) (intra: clip(128 + IDCT)). */ HOT static void idct_bfly_add(c1_fast_t *f, int rows, int cols, uint8_t *dst, int w, int intra) { int16_t *t = f->t16; /* t[u*8 + c]: |t| < 6400 */ int32_t e[4], o[4]; for (int u = 0; u < rows; u++) { const int16_t *x = f->coef + u * 8; if (cols <= 2) { int32_t e0 = M16(f->k0, x[0]) + 32768; for (int c = 0; c < 4; c++) { int32_t oc = M16(f->ko[c], x[1]); t[u * 8 + c] = (int16_t)hi16(e0 + oc); t[u * 8 + 7 - c] = (int16_t)hi16(e0 - oc); } } else { BFLY_EVEN(x[0], x[2], x[4], x[6], e); BFLY_ODD(x[1], x[3], x[5], x[7], o); for (int c = 0; c < 4; c++) { t[u * 8 + c] = (int16_t)hi16(e[c] + o[c] + 32768); t[u * 8 + 7 - c] = (int16_t)hi16(e[c] - o[c] + 32768); } } } int32_t bias = 32768 + (intra ? (128 << 16) : 0); for (int c = 0; c < 8; c++) { int32_t s[8]; if (rows <= 2) { int32_t t0 = t[c], t1 = rows > 1 ? t[8 + c] : 0; int32_t e0 = M16(f->k0, t0) + bias; for (int y = 0; y < 4; y++) { int32_t oy = M16(f->ko[y], t1); s[y] = e0 + oy; s[7 - y] = e0 - oy; } } else { int32_t x0 = t[c], x1 = t[8 + c], x2 = t[16 + c], x3 = rows > 3 ? t[24 + c] : 0; int32_t x4 = rows > 4 ? t[32 + c] : 0, x5 = rows > 5 ? t[40 + c] : 0; int32_t x6 = rows > 6 ? t[48 + c] : 0, x7 = rows > 7 ? t[56 + c] : 0; BFLY_EVEN(x0, x2, x4, x6, e); BFLY_ODD(x1, x3, x5, x7, o); for (int y = 0; y < 4; y++) { s[y] = e[y] + o[y] + bias; s[7 - y] = e[y] - o[y] + bias; } } uint8_t *d = dst + c; for (int y = 0; y < 8; y++, d += w) *d = (uint8_t)clip8((intra ? 0 : *d) + hi16(s[y])); } } /* res[y*8 + c] = the spec's IDCT of f->coef (inter: no +128), for the merged * prediction + residual pass. Same regrouping as idct_bfly_add. */ HOT static void idct_res(c1_fast_t *f, int rows, int cols, int16_t *res) { if (rows == 1 && cols == 1) { int32_t k0 = f->kt[0]; int16_t v = (int16_t)asr12(2048 + k0 * asr12(2048 + f->coef[0] * k0)); /* Longword stores through a may_alias type: res is read as int16 later, and a * plain uint32_t* lets LTO reorder those reads ahead of this fill. */ typedef uint32_t __attribute__((__may_alias__)) u32a; uint32_t p = (uint16_t)v * 0x00010001u; u32a *r4 = (u32a *)res; for (int i = 0; i < 32; i++) r4[i] = p; return; } int16_t *t = f->t16; int32_t e[4], o[4]; for (int u = 0; u < rows; u++) { const int16_t *x = f->coef + u * 8; if (cols <= 2) { int32_t e0 = M16(f->k0, x[0]) + 32768; for (int c = 0; c < 4; c++) { int32_t oc = M16(f->ko[c], x[1]); t[u * 8 + c] = (int16_t)hi16(e0 + oc); t[u * 8 + 7 - c] = (int16_t)hi16(e0 - oc); } } else { BFLY_EVEN(x[0], x[2], x[4], x[6], e); BFLY_ODD(x[1], x[3], x[5], x[7], o); for (int c = 0; c < 4; c++) { t[u * 8 + c] = (int16_t)hi16(e[c] + o[c] + 32768); t[u * 8 + 7 - c] = (int16_t)hi16(e[c] - o[c] + 32768); } } } for (int c = 0; c < 8; c++) { int32_t s[8]; if (rows <= 2) { int32_t t0 = t[c], t1 = rows > 1 ? t[8 + c] : 0; int32_t e0 = M16(f->k0, t0) + 32768; for (int y = 0; y < 4; y++) { int32_t oy = M16(f->ko[y], t1); s[y] = e0 + oy; s[7 - y] = e0 - oy; } } else { int32_t x0 = t[c], x1 = t[8 + c], x2 = t[16 + c], x3 = rows > 3 ? t[24 + c] : 0; int32_t x4 = rows > 4 ? t[32 + c] : 0, x5 = rows > 5 ? t[40 + c] : 0; int32_t x6 = rows > 6 ? t[48 + c] : 0, x7 = rows > 7 ? t[56 + c] : 0; BFLY_EVEN(x0, x2, x4, x6, e); BFLY_ODD(x1, x3, x5, x7, o); for (int y = 0; y < 4; y++) { s[y] = e[y] + o[y] + 32768; s[7 - y] = e[y] - o[y] + 32768; } } for (int y = 0; y < 8; y++) res[y * 8 + c] = (int16_t)hi16(s[y]); } } /* dst (8 x 8, stride w) = clip(prediction + res): the prediction read straight from the * reference plane (h x w) at (y0, x0), clamped at its edges. dst is 8-byte aligned. */ static inline void mc_add8(const uint8_t *plane, int w, int h, int y0, int x0, uint8_t *dst, const int16_t *res) { int inside = y0 >= 0 && x0 >= 0 && y0 + 8 <= h && x0 + 8 <= w; for (int r = 0; r < 8; r++, dst += w, res += 8) { int yy = y0 + r; if (!inside) yy = yy < 0 ? 0 : yy >= h ? h - 1 : yy; const uint8_t *s = plane + yy * w; uint8_t px[8]; for (int c = 0; c < 8; c++) { int xx = x0 + c; if (!inside) xx = xx < 0 ? 0 : xx >= w ? w - 1 : xx; px[c] = (uint8_t)clip8(s[xx] + res[c]); } #if C1_SH2 uint32_t *d4 = (uint32_t *)dst; /* two longword stores: write-through bus writes */ d4[0] = (uint32_t)px[0] << 24 | (uint32_t)px[1] << 16 | (uint32_t)px[2] << 8 | px[3]; d4[1] = (uint32_t)px[4] << 24 | (uint32_t)px[5] << 16 | (uint32_t)px[6] << 8 | px[7]; #else for (int c = 0; c < 8; c++) dst[c] = px[c]; #endif } } /* f->coef is all zero between blocks: after a block, clear only its rows x cols corner * (everything pairs() can have set), not all 64 (each store is a write-through bus write). */ static inline void coef_clear(c1_fast_t *f, int rows, int cols) { for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) f->coef[r * 8 + c] = 0; } static inline void idct_add_run(c1_fast_t *f, int rows, int cols, uint8_t *dst, int w, int intra) { if (rows == 1 && cols == 1) { int32_t k0 = f->kt[0]; int32_t v = asr12(2048 + k0 * asr12(2048 + f->coef[0] * k0)) + (intra ? 128 : 0); #if C1_SH2 /* Longword stores: the cache is write-through, so each store is a bus write. */ if (intra) { uint32_t p = (uint32_t)clip8(v) * 0x01010101u; for (int r = 0; r < 8; r++, dst += w) { uint32_t *d = (uint32_t *)dst; d[0] = p; d[1] = p; } } else { for (int r = 0; r < 8; r++, dst += w) { uint32_t *d = (uint32_t *)dst; for (int k = 0; k < 2; k++) { uint32_t s = d[k]; d[k] = (uint32_t)clip8((int)(s >> 24) + v) << 24 | (uint32_t)clip8((int)(s >> 16 & 255) + v) << 16 | (uint32_t)clip8((int)(s >> 8 & 255) + v) << 8 | (uint32_t)clip8((int)(s & 255) + v); } } } #else for (int r = 0; r < 8; r++, dst += w) for (int c = 0; c < 8; c++) dst[c] = (uint8_t)clip8((intra ? 0 : dst[c]) + v); #endif return; } #if C1_SH2 && defined(C1_IDCT_MAC) /* The MAC.W kernel (c1asm.s), kept for comparison: build with -DC1_IDCT_MAC. */ if (intra) for (int r = 0; r < 8; r++) { uint32_t *d = (uint32_t *)(dst + r * w); d[0] = 0; d[1] = 0; } c1_idct_add(f, rows, cols, dst, w, intra); #elif C1_SH2 && defined(C1_IDCT_BFLY_ASM) /* The butterfly in assembly (c1bfly.s), opt-in: exact, but measured no faster than * the C butterfly (decode 1.83 vs 1.75 fields, 2026-11-04). Rows of pass 1, then the 8 columns of pass * 2 added into dst. Pass 2's dense kernel reads all 8 rows of t, so rows the block * does not use are zeroed; intra blocks add onto a zeroed dst with +128 in the bias. */ int16_t *t = f->t16; int dense = cols > 2; for (int u = 0; u < rows; u++) c1_bf_row(f->coef + u * 8, t + u * 8, dense); int mode = rows <= 2 ? rows - 1 : 2; if (mode == 2) for (int u = rows; u < 8; u++) { uint32_t *z = (uint32_t *)(t + u * 8); z[0] = z[1] = z[2] = z[3] = 0; } if (intra) for (int r = 0; r < 8; r++) { uint32_t *d = (uint32_t *)(dst + r * w); d[0] = 0; d[1] = 0; } int32_t bias = 32768 + (intra ? (128 << 16) : 0); for (int c = 0; c < 8; c++) c1_bf_col(t + c, dst + c, w, mode, bias); #else idct_bfly_add(f, rows, cols, dst, w, intra); #endif } 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, const uint8_t *prev, uint8_t *changed) { int q = payload[0] << 8 | payload[1]; int off = h ? (payload[2] << 8 | payload[3]) : 5; int split = c1_split(payload); bits_t b = { payload + off, 0, 0, f->ue8 }; int16_t *si = f->si, *coef = f->coef; 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 ? split : 0, end = h ? C1_NMB : split; int my = pos / C1_MBX, mx = pos % C1_MBX, mvy = 0, mvx = 0; for (;;) { int run = (int)getue(&b); PROF_T(tp0); for (int k = 0; k < run && pos < end; k++, pos++) { if (prev[pos]) copy_mb(ref, cur, my, mx); changed[pos] = 0; if (++mx == C1_MBX) { mx = 0; my++; mvy = mvx = 0; } } PROF_ADD(0, tp0); 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); unsigned cbp = mode == INTER ? getu(&b, 6) : 0; /* Residuals first, then each 8x8 written once: clip(prediction + residual) * read straight from the reference, or a plain copy where nothing is coded. * (Copying the prediction and adding to it re-read lines the write-through * cache had not kept.) */ for (int k = 0; k < 6; k++) { if (!(cbp & (32u >> k))) continue; int rows = 0, cols = 0; if (pairs(&b, f->zigzag, coef, 0, 0, q, &rows, &cols)) return -1; PROF_T(tp2); idct_res(f, rows, cols, f->rblk[k]); coef_clear(f, rows, cols); PROF_ADD(2, tp2); } PROF_T(tp1); int py = ys + mvy, px = xs + mvx, pcy = cy + (mvy >> 1), pcx = cx + (mvx >> 1); if (!(cbp & 0x3C)) { mc_copy(ref->y, C1_W, C1_ROWS, py, px, 16, dy); } else { for (int k = 0; k < 4; k++) { int oy = (k >> 1) * 8, ox = (k & 1) * 8; if (cbp & (32u >> k)) mc_add8(ref->y, C1_W, C1_ROWS, py + oy, px + ox, dy + oy * C1_W + ox, f->rblk[k]); else mc_copy(ref->y, C1_W, C1_ROWS, py + oy, px + ox, 8, dy + oy * C1_W + ox); } } if (cbp & 2) mc_add8(ref->cb, C1_W / 2, C1_ROWS / 2, pcy, pcx, dcb, f->rblk[4]); else mc_copy(ref->cb, C1_W / 2, C1_ROWS / 2, pcy, pcx, 8, dcb); if (cbp & 1) mc_add8(ref->cr, C1_W / 2, C1_ROWS / 2, pcy, pcx, dcr, f->rblk[5]); else mc_copy(ref->cr, C1_W / 2, C1_ROWS / 2, pcy, pcx, 8, dcr); PROF_ADD(1, tp1); } 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; 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); PROF_T(tp3); if (k < 4) idct_add_run(f, rows, cols, dy + (k >> 1) * 8 * C1_W + (k & 1) * 8, C1_W, 1); else idct_add_run(f, rows, cols, k == 4 ? dcb : dcr, C1_W / 2, 1); coef_clear(f, rows, cols); PROF_ADD(2, tp3); } } 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 mb0, int mb1) { const uint8_t *cl = f->cl5 + 256; int my = mb0 / C1_MBX, mx = mb0 % C1_MBX; /* once; the SH-2 has no divide */ for (int mb = mb0; mb < mb1; mb++, mx = (mx == C1_MBX - 1) ? (my++, 0) : mx + 1) { { if (!mask[mb]) 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_FBW + C1_FBX + 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_FBW + C1_FBX + mx * 16); volatile uint32_t *d1 = (volatile uint32_t *)(dst + (r + 1) * C1_FBW + C1_FBX + mx * 16); #else volatile uint16_t *d0 = dst + r * C1_FBW + C1_FBX + mx * 16; volatile uint16_t *d1 = d0 + C1_FBW; #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_FBW); 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_FBW] __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_NMB); out[5] = (uint16_t)(clock() - t); t = clock(); c1_to_rgb555(f, cur, fb, mask, 0, C1_NMB); out[6] = (uint16_t)(clock() - t); (void)sink; } #endif