aboutsummaryrefslogtreecommitdiffstats
path: root/src/libanalog/ntsc.c
blob: 18fb4216a654938978ad899d2871ec9b429431cc (plain) (blame)
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
#include "libgha/include/libgha.h"

#include "ntsc.h"

#include <stdlib.h>
#include <stdint.h>

#include <stdio.h>
#include <string.h>

#include <math.h>
#include <assert.h>

struct ntsc_iq {
	float ei;
	float eq;
};

struct biquad_filter_ctx {
	float a0, a1, a2, b1, b2;
	float z1, z2;
};

struct verbose_ctx {
	FILE* fout;
};

struct ntsc_ctx {

	struct generator_ctx {
		float* sin_table;
		float* cos_table;
		int f;
		int shift;
	} generator;
	uint16_t width;
	struct ntsc_iq* iq;
	float* luma;
	float* chroma;

	struct biquad_filter_ctx i_filter;
	struct biquad_filter_ctx q_filter;

	struct verbose_ctx* verbose;
	uint16_t filter_width;
	gha_ctx_t gha;
};

static void ntsc_filter_init(struct biquad_filter_ctx* filter, float fc)
{
	float k = tan(M_PI * fc);
	float q = 0.7;
	float norm = 1 / (1 + k / q + k * k);
	filter->a0 = k * k * norm;
	filter->a1 = 2 * filter->a0;
	filter->a2 = filter->a0;
	filter->b1 = 2 * (k * k - 1) * norm;
	filter->b2 = (1 - k / q + k * k) * norm;
	filter->z1 = 0;
	filter->z2 = 0;
}

static float ntsc_process_filter(struct biquad_filter_ctx* filter, float in)
{
	float out = in * filter->a0 + filter->z1;
	filter->z1 = in * filter->a1 + filter->z2 - filter->b1 * out;
	filter->z2 = in * filter->a2 - filter->b2 * out;
	//fprintf(stderr, "filter in: %f, out: %f\n", in, out);
	return out;
}

static inline float ntsc_rgb_to_y(const float* rgb) {
	return rgb[0] * 0.30 + rgb[1] * 0.59 + rgb[2] * 0.11;
}

static inline void ntsc_rgb_to_iq(const float* rgb, struct ntsc_iq* iq)
{
	iq->ei = rgb[0] * 0.6 - rgb[1] * 0.28 - rgb[2] * 0.32;
	iq->eq = rgb[0] * 0.21 - rgb[1] * 0.52 + rgb[2] * 0.31;
}

static inline void ntsc_iqy_to_rgb(const struct ntsc_iq* iq, float y, float* rgb)
{
	rgb[0] = iq->ei * 0.96 + iq->eq * 0.62 + y;
	rgb[1] = iq->ei * (-0.27) - iq->eq * 0.65 + y;
	rgb[2] = iq->ei * (-1.11) + iq->eq * 1.7 + y;
}

static void ntsc_create_generator(struct ntsc_ctx* ctx)
{
	int i = 0;
	int n = 2 * ctx->width;
	float width = ctx->width;
	//TODO: tune the freq
	ctx->generator.f = (int)(width / 1.46) | 0x01;
	ctx->generator.sin_table = malloc(sizeof(float) * n);
	ctx->generator.cos_table = malloc(sizeof(float) * n);
	for (i = 0; i < n; i++) {
		ctx->generator.sin_table[i] = sin(ctx->generator.f * i * M_PI/width);
		ctx->generator.cos_table[i] = cos(ctx->generator.f * i * M_PI/width);
	}
	ctx->generator.shift = 0;
}

static void ntsc_free_generator(struct ntsc_ctx* ctx)
{
	free(ctx->generator.sin_table);
	free(ctx->generator.cos_table);
}

ntsc_ctx* ntsc_create_context(int width, int encode)
{
	if (width >= 65535) {
		return NULL;
	}
	ntsc_ctx* p = malloc(sizeof(ntsc_ctx));
	if (!p)
		return NULL;

	p->width = width;
	ntsc_create_generator(p);
	p->iq = malloc(sizeof(struct ntsc_iq) * width);
	p->luma = calloc(width, sizeof(float));
	p->chroma = calloc(width, sizeof(float));

	ntsc_filter_init(&p->i_filter, 0.08);
	ntsc_filter_init(&p->q_filter, 0.05);
	p->verbose = NULL;
	p->filter_width = 16;
	p->gha = gha_create_ctx(p->filter_width) ;
	return p;
}

void ntsc_free_context(ntsc_ctx* ctx)
{
	if (ctx->verbose)
		free(ctx->verbose);
	ntsc_free_generator(ctx);
	free(ctx->luma);
	free(ctx->chroma);
	free(ctx->iq);
	gha_free_ctx(ctx->gha);
	free(ctx);
}

static void verbose_carrier(ntsc_ctx* ctx)
{
	float t = (ctx->width * 2) / (float)ctx->generator.f;
	fprintf(ctx->verbose->fout, "Width: %d pixels\n", ctx->width);
	fprintf(ctx->verbose->fout, "Carrier: period is %f pixels (%f of Fmax)\n", t, 2/t);
}

void ntsc_enable_verbose(FILE* fout, ntsc_ctx* ctx)
{
	ctx->verbose = malloc(sizeof(struct verbose_ctx));
	ctx->verbose->fout = fout;
	fprintf(ctx->verbose->fout, "Verbose output enabled\n");
	verbose_carrier(ctx);
}

static void ntsc_next_line(ntsc_ctx* ctx)
{
	if (ctx->generator.shift) {
		ctx->generator.shift = 0;
	} else {
		ctx->generator.shift = 1;
	}
}

static void ntsc_modulate_line(ntsc_ctx* ctx, const struct ntsc_iq* in, float* out)
{
	int i;
	float* sin = ctx->generator.sin_table;
	float* cos = ctx->generator.cos_table;

	const int chroma_shift = 3;
	if (ctx->generator.shift) {
		sin += ctx->width;
		cos += ctx->width;
	}

	for (i = 0; i < chroma_shift; i++) {
		const struct ntsc_iq* iq = in + i;
		(void)(ntsc_process_filter(&ctx->i_filter, iq->ei * sin[i]));
		(void)(ntsc_process_filter(&ctx->q_filter, iq->eq * cos[i]));
	}

	for (i = chroma_shift; i < ctx->width; i++) {
		const struct ntsc_iq* iq = in + i;
		int j = i - chroma_shift;
		out[j] = ntsc_process_filter(&ctx->i_filter, iq->ei) * sin[j] + ntsc_process_filter(&ctx->q_filter, iq->eq) * cos[j];
	}

	for (i = 0; i < chroma_shift; i++) {
		int j = i - chroma_shift + ctx->width;
		out[j] = ntsc_process_filter(&ctx->i_filter, 0.0) * sin[j] + ntsc_process_filter(&ctx->q_filter, 0.0) * cos[j];
	}
}

static void ntsc_demodulate_line(const float* input, ntsc_ctx* ctx)
{
	int i;
	float* sin = ctx->generator.sin_table;
	float* cos = ctx->generator.cos_table;

	// In theory we should have different delay for I and Q channel
	const int chroma_shift = 3;
	if (ctx->generator.shift) {
		sin += ctx->width;
		cos += ctx->width;
	}


	// Feed filter
	for (i = 0; i < chroma_shift; i++) {
		(void)(ntsc_process_filter(&ctx->i_filter, input[i] * sin[i]) * 2.0);
		(void)(ntsc_process_filter(&ctx->q_filter, input[i] * cos[i]) * 2.0);
	}

	// Shift color components for chroma_shift pixels left to compensate low pass filter delay
	for (i = chroma_shift; i < ctx->width; i++) {
		struct ntsc_iq* iq = ctx->iq + i - chroma_shift;
		iq->ei = ntsc_process_filter(&ctx->i_filter, input[i] * sin[i]) * 2.0;
		iq->eq = ntsc_process_filter(&ctx->q_filter, input[i] * cos[i]) * 2.0;
	}

	// Drain last delayed values
	for (i = 0; i < chroma_shift; i++) {
		struct ntsc_iq* iq = ctx->iq + i - chroma_shift + ctx->width;
		iq->ei = ntsc_process_filter(&ctx->i_filter, 0.0) * 2.0;
		iq->eq = ntsc_process_filter(&ctx->q_filter, 0.0) * 2.0;
	}
}

void ntsc_process_encode(const float* input, float* output, ntsc_ctx* ctx)
{
	int i;
	for (i = 0; i < ctx->width; i++) {
		ntsc_rgb_to_iq(input + i * 3, (ctx->iq + i));
	}

	ntsc_modulate_line(ctx, ctx->iq, output);

	for (i = 0; i < ctx->width; i++) {
		output[i] += ntsc_rgb_to_y(input + i * 3);
	}

	ntsc_next_line(ctx);
}

static void ntsc_apply_harmonic_filter(float* input, float* chroma, ntsc_ctx* ctx)
{
	int i, j;
	const int blocks = 2 * ctx->width / ctx->filter_width;
	struct gha_info *info = malloc(sizeof(struct gha_info) * blocks);
	float *buf = malloc(sizeof(float) * ctx->filter_width);
	for (i = 0; i < blocks; i++) {
		size_t to_copy = ctx->filter_width;
		size_t start = i * ctx->filter_width / 2;
		if (start + to_copy > ctx->width) {
			to_copy = ctx->width - start;
			bzero(buf + to_copy, sizeof(float) * (ctx->filter_width - to_copy));
		}
		memcpy(buf, chroma + start, sizeof(float) * to_copy);
		struct gha_info t[4];
		gha_extract_many_simple(buf, &t[0], 4, ctx->gha);
		j = 0;
		for (; j < 4; j++) {
			float freq = t[j].frequency;
			if (freq > 2.0 && freq < 2.3) {
				break;
			}
		}
		if (j == 4) {
			bzero(&info[i], sizeof(struct gha_info));
		} else {
			memcpy(&info[i], &t[j], sizeof(struct gha_info));
		}
	}

	//for (i = 0; i < blocks; i++) {
		//fprintf(stderr, "%f ", info[i].frequency);
	//}
	//fprintf(stderr, "\n");
	int cont = 1;
	i = 0;
	while (cont) {
		assert(i < blocks);
		struct gha_info* t = &info[i];
		if (i == 0) {
			for (j = 0; j < ctx->filter_width / 4; j++) {
				input[j] -= t->magnitude * sin(t->frequency * j + t->phase);
			}
		}
		for (j = ctx->filter_width / 4; j < (ctx->filter_width / 2 + ctx->filter_width / 4); j++) {
			size_t line_pos = i * ctx->filter_width / 2 + j;
			if (line_pos >= ctx->width) {
				cont = 0;
				break;
			}
			input[line_pos] -= t->magnitude * sin(t->frequency * j + t->phase);
		}
		i++;
	}
	free(buf);
	free(info);
}

void ntsc_process_decode(const float* input, float* output, ntsc_ctx* ctx)
{
	int i;

	if (ctx->gha == NULL) {
		for (i = 0; i < ctx->width; i++) {
			float* l = ctx->luma + i;
			float* c = ctx->chroma + i;
			*l = (input[i] + *l) / 2.0;
			*c = (input[i] - *c) / 2.0;
		}
	} else {
		for (i = 0; i < ctx->width; i++) {
			float* l = ctx->luma + i;
			float* c = ctx->chroma + i;
			*l = input[i];
			*c = (input[i] - *c) / 2.0;
		}
		ntsc_apply_harmonic_filter(ctx->luma, ctx->chroma, ctx);
	}

	ntsc_demodulate_line(ctx->chroma, ctx);

	for (i = 0; i < ctx->width; i++) {
		struct ntsc_iq* iq = ctx->iq + i;
		ntsc_iqy_to_rgb(iq, ctx->luma[i], output + i * 3);
	}

	for (i = 0; i < ctx->width; i++) {
		float* l = ctx->luma + i;
		float* c = ctx->chroma + i;
		*l = input[i];
		*c = input[i];
	}

	ntsc_next_line(ctx);
}