Debian Codegraph @

src/filter/ntsc/crt_ntsc.c

raw symbols SCIP
1 /*****************************************************************************/
2 /*
3 * NTSC/CRT - integer-only NTSC video signal encoding / decoding emulation
4 *
5 * by EMMIR 2018-2023
6 *
7 * YouTube: https://www.youtube.com/@EMMIR_KC/videos
8 * Discord: https://discord.com/invite/hdYctSmyQJ
9 */
10 /*****************************************************************************/
11
12 #include "crt_core.h"
13
14 #if (CRT_SYSTEM == CRT_SYSTEM_NTSC)
15 #include <stdlib.h>
16 #include <string.h>
17
18 #if (CRT_CHROMA_PATTERN == 1)
19 /* 227.5 subcarrier cycles per line means every other line has reversed phase */
20 #define CC_PHASE(ln) (((ln) & 1) ? -1 : 1)
21 #else
22 #define CC_PHASE(ln) (1)
23 #endif
24
25 #define EXP_P 11
26 #define EXP_ONE (1 << EXP_P)
27 #define EXP_MASK (EXP_ONE - 1)
28 #define EXP_PI 6434
29 #define EXP_MUL(x, y) (((x) * (y)) >> EXP_P)
30 #define EXP_DIV(x, y) (((x) << EXP_P) / (y))
31
32 static int e11[] = {
33 EXP_ONE,
34 5567, /* e */
35 15133, /* e^2 */
36 41135, /* e^3 */
37 111817 /* e^4 */
38 };
39
40 /* fixed point e^x */
41 static int
42 expx(int n)
43 {
44 int neg, idx, res;
45 int nxt, acc, del;
46 int i;
47
48 if (n == 0) {
49 return EXP_ONE;
50 }
51 neg = n < 0;
52 if (neg) {
53 n = -n;
54 }
55 idx = n >> EXP_P;
56 res = EXP_ONE;
57 for (i = 0; i < idx / 4; i++) {
58 res = EXP_MUL(res, e11[4]);
59 }
60 idx &= 3;
61 if (idx > 0) {
62 res = EXP_MUL(res, e11[idx]);
63 }
64
65 n &= EXP_MASK;
66 nxt = EXP_ONE;
67 acc = 0;
68 del = 1;
69 for (i = 1; i < 17; i++) {
70 acc += nxt / del;
71 nxt = EXP_MUL(nxt, n);
72 del *= i;
73 if (del > nxt || nxt <= 0 || del <= 0) {
74 break;
75 }
76 }
77 res = EXP_MUL(res, acc);
78
79 if (neg) {
80 res = EXP_DIV(EXP_ONE, res);
81 }
82 return res;
83 }
84
85 /*****************************************************************************/
86 /********************************* FILTERS ***********************************/
87 /*****************************************************************************/
88
89 /* infinite impulse response low pass filter for bandlimiting YIQ */
90 static struct IIRLP {
91 int c;
92 int h; /* history */
93 } iirY, iirI, iirQ;
94
95 /* freq - total bandwidth
96 * limit - max frequency
97 */
98 static void
99 init_iir(struct IIRLP *f, int freq, int limit)
100 {
101 int rate; /* cycles/pixel rate */
102
103 memset(f, 0, sizeof(struct IIRLP));
104 rate = (freq << 9) / limit;
105 f->c = EXP_ONE - expx(-((EXP_PI << 9) / rate));
106 }
107
108 static void
109 reset_iir(struct IIRLP *f)
110 {
111 f->h = 0;
112 }
113
114 /* hi-pass for debugging */
115 #define HIPASS 0
116
117 static int
118 iirf(struct IIRLP *f, int s)
119 {
120 f->h += EXP_MUL(s - f->h, f->c);
121 #if HIPASS
122 return s - f->h;
123 #else
124 return f->h;
125 #endif
126 }
127
128 extern void
129 crt_modulate(struct CRT *v, struct NTSC_SETTINGS *s)
130 {
131 int x, y, xo, yo;
132 int destw = AV_LEN;
133 int desth = ((CRT_LINES * 64500) >> 16);
134 int iccf[CRT_CC_SAMPLES];
135 int ccmodI[CRT_CC_SAMPLES]; /* color phase for mod */
136 int ccmodQ[CRT_CC_SAMPLES]; /* color phase for mod */
137 int ccburst[CRT_CC_SAMPLES]; /* color phase for burst */
138 int sn, cs, n, ph;
139 int inv_phase = 0;
140
141 if (!s->iirs_initialized) {
142 init_iir(&iirY, L_FREQ, Y_FREQ);
143 init_iir(&iirI, L_FREQ, I_FREQ);
144 init_iir(&iirQ, L_FREQ, Q_FREQ);
145 s->iirs_initialized = 1;
146 }
147 #if CRT_DO_BLOOM
148 #if NTSC_RAW
149 destw = s->w;
150 desth = s->h;
151 if (destw > ((AV_LEN * 55500) >> 16)) {
152 destw = ((AV_LEN * 55500) >> 16);
153 }
154 if (desth > ((CRT_LINES * 63500) >> 16)) {
155 desth = ((CRT_LINES * 63500) >> 16);
156 }
157 #else
158 destw = (AV_LEN * 55500) >> 16;
159 desth = (CRT_LINES * 63500) >> 16;
160 #endif
161 #else
162 #if NTSC_RAW
163 destw = s->w;
164 desth = s->h;
165 if (destw > AV_LEN) {
166 destw = AV_LEN;
167 }
168 if (desth > ((CRT_LINES * 64500) >> 16)) {
169 desth = ((CRT_LINES * 64500) >> 16);
170 }
171 #endif
172 #endif
173 #if NTSC_AS_COLOR
174 for (x = 0; x < CRT_CC_SAMPLES; x++) {
175 n = NTSC_HUE + x * (360 / CRT_CC_SAMPLES);
176 crt_sincos14(&sn, &cs, (n + 33) * 8192 / 180);
177 ccburst[x] = sn >> 10;
178 crt_sincos14(&sn, &cs, n * 8192 / 180);
179 ccmodI[x] = sn >> 10;
180 crt_sincos14(&sn, &cs, (n - 90) * 8192 / 180);
181 ccmodQ[x] = sn >> 10;
182 }
183 #else
184 memset(ccburst, 0, sizeof(ccburst));
185 memset(ccmodI, 0, sizeof(ccmodI));
186 memset(ccmodQ, 0, sizeof(ccmodQ));
187 #endif
188
189 xo = AV_BEG + NTSC_X_OFFSET + (AV_LEN - destw) / 2;
190 yo = CRT_TOP + NTSC_Y_OFFSET + (CRT_LINES - desth) / 2;
191
192 s->field &= 1;
193 s->frame &= 1;
194 inv_phase = (s->field == s->frame);
195 ph = CC_PHASE(inv_phase);
196
197 /* align signal */
198 xo = (xo & ~3);
199
200 for (n = 0; n < CRT_VRES; n++) {
201 int t; /* time */
202 signed char *line = &v->analog[n * CRT_HRES];
203
204 t = LINE_BEG;
205
206 if (n <= 3 || (n >= 7 && n <= 9)) {
207 /* equalizing pulses - small blips of sync, mostly blank */
208 while (t < (4 * CRT_HRES / 100)) line[t++] = SYNC_LEVEL;
209 while (t < (50 * CRT_HRES / 100)) line[t++] = BLANK_LEVEL;
210 while (t < (54 * CRT_HRES / 100)) line[t++] = SYNC_LEVEL;
211 while (t < (100 * CRT_HRES / 100)) line[t++] = BLANK_LEVEL;
212 } else if (n >= 4 && n <= 6) {
213 int even[4] = { 46, 50, 96, 100 };
214 int odd[4] = { 4, 50, 96, 100 };
215 int *offs = even;
216 if (s->field == 1) {
217 offs = odd;
218 }
219 /* vertical sync pulse - small blips of blank, mostly sync */
220 while (t < (offs[0] * CRT_HRES / 100)) line[t++] = SYNC_LEVEL;
221 while (t < (offs[1] * CRT_HRES / 100)) line[t++] = BLANK_LEVEL;
222 while (t < (offs[2] * CRT_HRES / 100)) line[t++] = SYNC_LEVEL;
223 while (t < (offs[3] * CRT_HRES / 100)) line[t++] = BLANK_LEVEL;
224 } else {
225 int cb;
226
227 /* video line */
228 while (t < SYNC_BEG) line[t++] = BLANK_LEVEL; /* FP */
229 while (t < BW_BEG) line[t++] = SYNC_LEVEL; /* SYNC */
230 while (t < AV_BEG) line[t++] = BLANK_LEVEL; /* BW + CB + BP */
231 if (n < CRT_TOP) {
232 while (t < CRT_HRES) line[t++] = BLANK_LEVEL;
233 }
234
235 /* CB_CYCLES of color burst at 3.579545 Mhz */
236 for (t = CB_BEG; t < CB_BEG + (CB_CYCLES * CRT_CB_FREQ); t++) {
237 #if (CRT_CHROMA_PATTERN == 1)
238 int off180 = CRT_CC_SAMPLES / 2;
239 cb = ccburst[(t + inv_phase * off180) % CRT_CC_SAMPLES];
240 #else
241 cb = ccburst[t % CRT_CC_SAMPLES];
242 #endif
243 line[t] = (BLANK_LEVEL + (cb * BURST_LEVEL)) >> 5;
244 iccf[t % CRT_CC_SAMPLES] = line[t];
245 }
246 }
247 }
248
249 for (y = 0; y < desth; y++) {
250 int field_offset;
251 int sy;
252
253 field_offset = (s->field * s->h + desth) / desth / 2;
254 sy = (y * s->h) / desth;
255
256 sy += field_offset;
257
258 if (sy >= s->h) sy = s->h;
259
260 sy *= s->w;
261
262 reset_iir(&iirY);
263 reset_iir(&iirI);
264 reset_iir(&iirQ);
265
266 for (x = 0; x < destw; x++) {
267 int fy, fi, fq;
268 int rA, gA, bA;
269 const unsigned char *pix;
270 int ire; /* composite signal */
271 int xoff;
272
273 pix = s->data + ((((x * s->w) / destw) + sy) * CRT_BPP);
274
275 rA = pix[0];
276 gA = pix[1];
277 bA = pix[2];
278
279 /* RGB to YIQ */
280 fy = (19595 * rA + 38470 * gA + 7471 * bA) >> 14;
281 fi = (39059 * rA - 18022 * gA - 21103 * bA) >> 14;
282 fq = (13894 * rA - 34275 * gA + 20382 * bA) >> 14;
283 ire = BLACK_LEVEL + CRT_BLACK_PT;
284
285 xoff = (x + xo) % CRT_CC_SAMPLES;
286 /* bandlimit Y,I,Q */
287 fy = iirf(&iirY, fy);
288 fi = iirf(&iirI, fi) * ph * ccmodI[xoff] >> 4;
289 fq = iirf(&iirQ, fq) * ph * ccmodQ[xoff] >> 4;
290 ire += (fy + fi + fq) * (WHITE_LEVEL * CRT_WHITE_PT / 100) >> 10;
291 if (ire < 0) ire = 0;
292 if (ire > 110) ire = 110;
293
294 v->analog[(x + xo) + (y + yo) * CRT_HRES] = ire;
295 }
296 }
297 for (n = 0; n < CRT_CC_VPER; n++) {
298 for (x = 0; x < CRT_CC_SAMPLES; x++) {
299 v->ccf[n][x] = iccf[x] << 7;
300 }
301 }
302 }
303 #endif