-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcrt_core.c
482 lines (415 loc) · 13.2 KB
/
crt_core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*****************************************************************************/
/*
* NTSC/CRT - integer-only NTSC video signal encoding / decoding emulation
*
* by EMMIR 2018-2023
*
* YouTube: https://www.youtube.com/@EMMIR_KC/videos
* Discord: https://discord.com/invite/hdYctSmyQJ
*/
/*****************************************************************************/
#include "crt_core.h"
#include <stdlib.h>
#include <string.h>
/* ensure negative values for x get properly modulo'd */
#define POSMOD(x, n) (((x) % (n) + (n)) % (n))
static int sigpsin15[18] = { /* significant points on sine wave (15-bit) */
0x0000,
0x0c88,0x18f8,0x2528,0x30f8,0x3c50,0x4718,0x5130,0x5a80,
0x62f0,0x6a68,0x70e0,0x7640,0x7a78,0x7d88,0x7f60,0x8000,
0x7f60
};
static int
sintabil8(int n)
{
int f, i, a, b;
/* looks scary but if you don't change T14_2PI
* it won't cause out of bounds memory reads
*/
f = n >> 0 & 0xff;
i = n >> 8 & 0xff;
a = sigpsin15[i];
b = sigpsin15[i + 1];
return (a + ((b - a) * f >> 8));
}
/* 14-bit interpolated sine/cosine */
extern void
crt_sincos14(int *s, int *c, int n)
{
int h;
n &= T14_MASK;
h = n & ((T14_2PI >> 1) - 1);
if (h > ((T14_2PI >> 2) - 1)) {
*c = -sintabil8(h - (T14_2PI >> 2));
*s = sintabil8((T14_2PI >> 1) - h);
} else {
*c = sintabil8((T14_2PI >> 2) - h);
*s = sintabil8(h);
}
if (n > ((T14_2PI >> 1) - 1)) {
*c = -*c;
*s = -*s;
}
}
/*****************************************************************************/
/********************************* FILTERS ***********************************/
/*****************************************************************************/
/* convolution is much faster but the EQ looks softer, more authentic, and more analog */
#define USE_CONVOLUTION 0
#define USE_7_SAMPLE_KERNEL 0
#define USE_6_SAMPLE_KERNEL 0
#define USE_5_SAMPLE_KERNEL 1
#if USE_CONVOLUTION
/* NOT 3 band equalizer, faster convolution instead.
* eq function names preserved to keep code clean
*/
static struct EQF {
int h[5];
} eqY, eqI, eqQ;
/* params unused to keep the function the same */
static void
init_eq(struct EQF *f,
int f_lo, int f_hi, int rate,
int g_lo, int g_mid, int g_hi)
{
memset(f, 0, sizeof(struct EQF));
}
static void
reset_eq(struct EQF *f)
{
memset(f->h, 0, sizeof(f->h));
}
static int
eqf(struct EQF *f, int s)
{
int i;
int *h = f->h;
for (i = 4; i > 0; i--) {
h[i] = h[i - 1];
}
h[0] = s;
#if USE_7_SAMPLE
/* index : 0 1 2 3 4 5 6 */
/* weight: 1 4 7 8 7 4 1 */
return (s + h[6] + ((h[1] + h[5]) * 4) + ((h[2] + h[4]) * 7) + (h[3] * 8)) >> 5;
#elif USE_6_SAMPLE
/* index : 0 1 2 3 4 5 */
/* weight: 1 3 4 4 3 1 */
return (s + h[5] + 3 * (h[1] + h[4]) + 4 * (h[2] + h[3])) >> 4;
#elif USE_5_SAMPLE
/* index : 0 1 2 3 4 */
/* weight: 1 2 2 2 1 */
return (s + h[4] + ((h[1] + h[2] + h[3]) << 1)) >> 3;
#else
/* index : 0 1 2 3 */
/* weight: 1 1 1 1*/
return (s + h[3] + h[1] + h[2]) >> 2;
#endif
}
#else
#define HISTLEN 3
#define HISTOLD (HISTLEN - 1) /* oldest entry */
#define HISTNEW 0 /* newest entry */
#define EQ_P 16 /* if changed, the gains will need to be adjusted */
#define EQ_R (1 << (EQ_P - 1)) /* rounding */
/* three band equalizer */
static struct EQF {
int lf, hf; /* fractions */
int g[3]; /* gains */
int fL[4];
int fH[4];
int h[HISTLEN]; /* history */
} eqY, eqI, eqQ;
/* f_lo - low cutoff frequency
* f_hi - high cutoff frequency
* rate - sampling rate
* g_lo, g_mid, g_hi - gains
*/
static void
init_eq(struct EQF *f,
int f_lo, int f_hi, int rate,
int g_lo, int g_mid, int g_hi)
{
int sn, cs;
memset(f, 0, sizeof(struct EQF));
f->g[0] = g_lo;
f->g[1] = g_mid;
f->g[2] = g_hi;
crt_sincos14(&sn, &cs, T14_PI * f_lo / rate);
if (EQ_P >= 15) {
f->lf = 2 * (sn << (EQ_P - 15));
} else {
f->lf = 2 * (sn >> (15 - EQ_P));
}
crt_sincos14(&sn, &cs, T14_PI * f_hi / rate);
if (EQ_P >= 15) {
f->hf = 2 * (sn << (EQ_P - 15));
} else {
f->hf = 2 * (sn >> (15 - EQ_P));
}
}
static void
reset_eq(struct EQF *f)
{
memset(f->fL, 0, sizeof(f->fL));
memset(f->fH, 0, sizeof(f->fH));
memset(f->h, 0, sizeof(f->h));
}
static int
eqf(struct EQF *f, int s)
{
int i, r[3];
f->fL[0] += (f->lf * (s - f->fL[0]) + EQ_R) >> EQ_P;
f->fH[0] += (f->hf * (s - f->fH[0]) + EQ_R) >> EQ_P;
for (i = 1; i < 4; i++) {
f->fL[i] += (f->lf * (f->fL[i - 1] - f->fL[i]) + EQ_R) >> EQ_P;
f->fH[i] += (f->hf * (f->fH[i - 1] - f->fH[i]) + EQ_R) >> EQ_P;
}
r[0] = f->fL[3];
r[1] = f->fH[3] - f->fL[3];
r[2] = f->h[HISTOLD] - f->fH[3];
for (i = 0; i < 3; i++) {
r[i] = (r[i] * f->g[i]) >> EQ_P;
}
for (i = HISTOLD; i > 0; i--) {
f->h[i] = f->h[i - 1];
}
f->h[HISTNEW] = s;
return (r[0] + r[1] + r[2]);
}
#endif
/*****************************************************************************/
/***************************** PUBLIC FUNCTIONS ******************************/
/*****************************************************************************/
extern void
crt_resize(struct CRT *v, int w, int h, int *out)
{
v->outw = w;
v->outh = h;
v->out = out;
}
extern void
crt_reset(struct CRT *v)
{
v->hue = 0;
v->saturation = 10;
v->brightness = 0;
v->contrast = 180;
v->black_point = 0;
v->white_point = 100;
v->hsync = 0;
v->vsync = 0;
}
extern void
crt_init(struct CRT *v, int w, int h, int *out)
{
memset(v, 0, sizeof(struct CRT));
crt_resize(v, w, h, out);
crt_reset(v);
v->rn = 194;
/* kilohertz to line sample conversion */
#define kHz2L(kHz) (CRT_HRES * (kHz * 100) / L_FREQ)
/* band gains are pre-scaled as 16-bit fixed point
* if you change the EQ_P define, you'll need to update these gains too
*/
init_eq(&eqY, kHz2L(1500), kHz2L(3000), CRT_HRES, 65536, 8192, 9175);
init_eq(&eqI, kHz2L(80), kHz2L(1150), CRT_HRES, 65536, 65536, 1311);
init_eq(&eqQ, kHz2L(80), kHz2L(1000), CRT_HRES, 65536, 65536, 0);
}
/* search windows, in samples */
#define HSYNC_WINDOW 6
#define VSYNC_WINDOW 6
extern void
crt_demodulate(struct CRT *v, int noise)
{
struct {
int y, i, q;
} out[AV_LEN + 1], *yiqA, *yiqB;
int i, j, line, rn;
signed char *sig;
int s = 0;
int field, ratio;
int *ccr; /* color carrier signal */
int huesn, huecs;
int xnudge = -3, ynudge = 3;
int bright = v->brightness - (BLACK_LEVEL + v->black_point);
#if CRT_DO_BLOOM
int prev_e; /* filtered beam energy per scan line */
int max_e; /* approx maximum energy in a scan line */
#endif
crt_sincos14(&huesn, &huecs, ((v->hue % 360) + 33) * 8192 / 180);
huesn >>= 11; /* make 4-bit */
huecs >>= 11;
rn = v->rn;
for (i = 0; i < CRT_INPUT_SIZE; i++) {
rn = (214019 * rn + 140327895);
/* signal + noise */
s = v->analog[i] + (((((rn >> 16) & 0xff) - 0x7f) * noise) >> 8);
if (s > 127) { s = 127; }
if (s < -127) { s = -127; }
v->inp[i] = s;
}
v->rn = rn;
/* Look for vertical sync.
*
* This is done by integrating the signal and
* seeing if it exceeds a threshold. The threshold of
* the vertical sync pulse is much higher because the
* vsync pulse is a lot longer than the hsync pulse.
* The signal needs to be integrated to lessen
* the noise in the signal.
*/
for (i = -VSYNC_WINDOW; i < VSYNC_WINDOW; i++) {
line = POSMOD(v->vsync + i, CRT_VRES);
sig = v->inp + line * CRT_HRES;
s = 0;
for (j = 0; j < CRT_HRES; j++) {
s += sig[j];
/* increase the multiplier to make the vsync
* more stable when there is a lot of noise
*/
if (s <= (94 * SYNC_LEVEL)) {
goto vsync_found;
}
}
}
vsync_found:
#if CRT_DO_VSYNC
v->vsync = line; /* vsync found (or gave up) at this line */
#else
v->vsync = -3;
#endif
/* if vsync signal was in second half of line, odd field */
field = (j > (CRT_HRES / 2));
#if CRT_DO_BLOOM
max_e = (128 + (noise / 2)) * AV_LEN;
prev_e = (16384 / 8);
#endif
/* ratio of output height to active video lines in the signal */
ratio = (v->outh << 16) / CRT_LINES;
ratio = (ratio + 32768) >> 16;
field = (field * (ratio / 2));
for (line = CRT_TOP; line < CRT_BOT; line++) {
unsigned pos, ln;
int scanL, scanR, dx;
int L, R;
int *cL, *cR;
int wave[4];
int dci, dcq; /* decoded I, Q */
int xpos, ypos;
int beg, end;
int phasealign;
#if CRT_DO_BLOOM
int line_w;
#endif
beg = (line - CRT_TOP + 0) * v->outh / CRT_LINES + field;
end = (line - CRT_TOP + 1) * v->outh / CRT_LINES + field;
if (beg >= v->outh) { continue; }
if (end > v->outh) { end = v->outh; }
/* Look for horizontal sync.
* See comment above regarding vertical sync.
*/
ln = (POSMOD(line + v->vsync, CRT_VRES)) * CRT_HRES;
sig = v->inp + ln + v->hsync;
s = 0;
for (i = -HSYNC_WINDOW; i < HSYNC_WINDOW; i++) {
s += sig[SYNC_BEG + i];
if (s <= (4 * SYNC_LEVEL)) {
break;
}
}
#if CRT_DO_HSYNC
v->hsync = POSMOD(i + v->hsync, CRT_HRES);
#else
v->hsync = 0;
#endif
xpos = POSMOD(AV_BEG + v->hsync + xnudge, CRT_HRES);
ypos = POSMOD(line + v->vsync + ynudge, CRT_VRES);
pos = xpos + ypos * CRT_HRES;
ccr = v->ccf[ypos & 3];
sig = v->inp + ln + (v->hsync & ~3); /* burst @ 1/CB_FREQ sample rate */
for (i = CB_BEG; i < CB_BEG + (CB_CYCLES * CRT_CB_FREQ); i++) {
int p, n;
p = ccr[i & 3] * 127 / 128; /* fraction of the previous */
n = sig[i]; /* mixed with the new sample */
ccr[i & 3] = p + n;
}
phasealign = POSMOD(v->hsync, 4);
/* amplitude of carrier = saturation, phase difference = hue */
dci = ccr[(phasealign + 1) & 3] - ccr[(phasealign + 3) & 3];
dcq = ccr[(phasealign + 2) & 3] - ccr[(phasealign + 0) & 3];
/* rotate them by the hue adjustment angle */
wave[0] = ((dci * huecs - dcq * huesn) >> 4) * v->saturation;
wave[1] = ((dcq * huecs + dci * huesn) >> 4) * v->saturation;
wave[2] = -wave[0];
wave[3] = -wave[1];
sig = v->inp + pos;
#if CRT_DO_BLOOM
s = 0;
for (i = 0; i < AV_LEN; i++) {
s += sig[i]; /* sum up the scan line */
}
/* bloom emulation */
prev_e = (prev_e * 123 / 128) + ((((max_e >> 1) - s) << 10) / max_e);
line_w = (AV_LEN * 112 / 128) + (prev_e >> 9);
dx = (line_w << 12) / v->outw;
scanL = ((AV_LEN / 2) - (line_w >> 1) + 8) << 12;
scanR = (AV_LEN - 1) << 12;
L = (scanL >> 12);
R = (scanR >> 12);
#else
dx = ((AV_LEN - 1) << 12) / v->outw;
scanL = 0;
scanR = (AV_LEN - 1) << 12;
L = 0;
R = AV_LEN;
#endif
reset_eq(&eqY);
reset_eq(&eqI);
reset_eq(&eqQ);
for (i = L; i < R; i++) {
out[i].y = eqf(&eqY, sig[i] + bright) << 4;
out[i].i = eqf(&eqI, sig[i] * wave[(i + 0) & 3] >> 9) >> 3;
out[i].q = eqf(&eqQ, sig[i] * wave[(i + 3) & 3] >> 9) >> 3;
}
cL = v->out + beg * v->outw;
cR = cL + v->outw;
for (pos = scanL; pos < scanR && cL < cR; pos += dx) {
int y, i, q;
int r, g, b;
int aa, bb;
R = pos & 0xfff;
L = 0xfff - R;
s = pos >> 12;
yiqA = out + s;
yiqB = out + s + 1;
/* interpolate between samples if needed */
y = ((yiqA->y * L) >> 2) + ((yiqB->y * R) >> 2);
i = ((yiqA->i * L) >> 14) + ((yiqB->i * R) >> 14);
q = ((yiqA->q * L) >> 14) + ((yiqB->q * R) >> 14);
/* YIQ to RGB */
r = (((y + 3879 * i + 2556 * q) >> 12) * v->contrast) >> 8;
g = (((y - 1126 * i - 2605 * q) >> 12) * v->contrast) >> 8;
b = (((y - 4530 * i + 7021 * q) >> 12) * v->contrast) >> 8;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
if (v->blend) {
aa = (r << 16 | g << 8 | b);
bb = *cL;
/* blend with previous color there */
*cL++ = (((aa & 0xfefeff) >> 1) + ((bb & 0xfefeff) >> 1));
} else {
*cL++ = (r << 16 | g << 8 | b);
}
}
/* duplicate extra lines */
ln = v->outw * sizeof(int);
for (s = beg + 1; s < (end - v->scanlines); s++) {
memcpy(v->out + s * v->outw, v->out + (s - 1) * v->outw, ln);
}
}
}