-
Notifications
You must be signed in to change notification settings - Fork 9
/
video.c
491 lines (381 loc) · 15.8 KB
/
video.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
483
484
485
486
487
488
489
490
491
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <gtk/gtk.h>
#include <alsa/asoundlib.h>
#include <fftw3.h>
#include "common.h"
/* Demodulate the video signal & store all kinds of stuff for later stages
* Mode: M1, M2, S1, S2, R72, R36...
* Rate: exact sampling rate used
* Skip: number of PCM samples to skip at the beginning (for sync phase adjustment)
* Redraw: FALSE = Apply windowing and FFT to the signal, TRUE = Redraw from cached FFT data
* returns: TRUE when finished, FALSE when aborted
*/
gboolean GetVideo(guchar Mode, double Rate, int Skip, gboolean Redraw) {
guint MaxBin = 0;
guint VideoPlusNoiseBins=0, ReceiverBins=0, NoiseOnlyBins=0;
guint n=0;
guint SyncSampleNum;
guint i=0, j=0;
guint FFTLen=1024, WinLength=0;
guint SyncTargetBin;
int SampleNum, Length, NumChans;
int x = 0, y = 0, tx=0, k=0;
double Hann[7][1024] = {{0}};
double Freq = 0, PrevFreq = 0, InterpFreq = 0;
int NextSNRtime = 0, NextSyncTime = 0;
double Praw, Psync;
double Power[1024] = {0};
double Pvideo_plus_noise=0, Pnoise_only=0, Pnoise=0, Psignal=0;
double SNR = 0;
double ChanStart[4] = {0}, ChanLen[4] = {0};
guchar Image[800][616][3] = {{{0}}};
guchar Channel = 0, WinIdx = 0;
typedef struct {
int X;
int Y;
int Time;
guchar Channel;
gboolean Last;
} _PixelGrid;
_PixelGrid *PixelGrid;
PixelGrid = calloc( ModeSpec[Mode].ImgWidth * ModeSpec[Mode].NumLines * 3, sizeof(_PixelGrid) );
// Initialize Hann windows of different lengths
gushort HannLens[7] = { 48, 64, 96, 128, 256, 512, 1024 };
for (j = 0; j < 7; j++)
for (i = 0; i < HannLens[j]; i++)
Hann[j][i] = 0.5 * (1 - cos( (2 * M_PI * i) / (HannLens[j] - 1)) );
// Starting times of video channels on every line, counted from beginning of line
switch (Mode) {
case R36:
case R24:
ChanLen[0] = ModeSpec[Mode].PixelTime * ModeSpec[Mode].ImgWidth * 2;
ChanLen[1] = ChanLen[2] = ModeSpec[Mode].PixelTime * ModeSpec[Mode].ImgWidth;
ChanStart[0] = ModeSpec[Mode].SyncTime + ModeSpec[Mode].PorchTime;
ChanStart[1] = ChanStart[0] + ChanLen[0] + ModeSpec[Mode].SeptrTime;
ChanStart[2] = ChanStart[1];
break;
case S1:
case S2:
case SDX:
ChanLen[0] = ChanLen[1] = ChanLen[2] = ModeSpec[Mode].PixelTime * ModeSpec[Mode].ImgWidth;
ChanStart[0] = ModeSpec[Mode].SeptrTime;
ChanStart[1] = ChanStart[0] + ChanLen[0] + ModeSpec[Mode].SeptrTime;
ChanStart[2] = ChanStart[1] + ChanLen[1] + ModeSpec[Mode].SyncTime + ModeSpec[Mode].PorchTime;
break;
case PD50:
case PD90:
case PD120:
case PD160:
case PD180:
case PD240:
case PD290:
ChanLen[0] = ChanLen[1] = ChanLen[2] = ChanLen[3] = ModeSpec[Mode].PixelTime * ModeSpec[Mode].ImgWidth;
ChanStart[0] = ModeSpec[Mode].SyncTime + ModeSpec[Mode].PorchTime;
ChanStart[1] = ChanStart[0] + ChanLen[0] + ModeSpec[Mode].SeptrTime;
ChanStart[2] = ChanStart[1] + ChanLen[1] + ModeSpec[Mode].SeptrTime;
ChanStart[3] = ChanStart[2] + ChanLen[2] + ModeSpec[Mode].SeptrTime;
break;
default:
ChanLen[0] = ChanLen[1] = ChanLen[2] = ModeSpec[Mode].PixelTime * ModeSpec[Mode].ImgWidth;
ChanStart[0] = ModeSpec[Mode].SyncTime + ModeSpec[Mode].PorchTime;
ChanStart[1] = ChanStart[0] + ChanLen[0] + ModeSpec[Mode].SeptrTime;
ChanStart[2] = ChanStart[1] + ChanLen[1] + ModeSpec[Mode].SeptrTime;
break;
}
// Number of channels per line
switch(Mode) {
case R24BW:
case R12BW:
case R8BW:
NumChans = 1;
break;
case R24:
case R36:
NumChans = 2;
break;
//In PD* modes, each radio frame encodes
//4 channels, two luminance and two chroma
case PD50:
case PD90:
case PD120:
case PD160:
case PD180:
case PD240:
case PD290:
NumChans = 4;
break;
default:
NumChans = 3;
break;
}
// Plan ahead the time instants (in samples) at which to take pixels out
int PixelIdx = 0;
if (NumChans == 4){ //Woking on PD* mode
//Each radio frame encodes two image lines
for (y = 0; y < ModeSpec[Mode].NumLines; y += 2){
for (Channel = 0; Channel < NumChans; Channel++){
for (x = 0; x < ModeSpec[Mode].ImgWidth; x++){
PixelGrid[PixelIdx].Time = (int)round(Rate * ( y/2 * ModeSpec[Mode].LineTime + ChanStart[Channel] +
ModeSpec[Mode].PixelTime * 1.0 * (x + 0.5))) +
Skip;
if (Channel == 0) {
PixelGrid[PixelIdx].X = x;
PixelGrid[PixelIdx].Y = y;
PixelGrid[PixelIdx].Channel = Channel;
PixelGrid[PixelIdx].Last = FALSE;
PixelIdx++;
}
else if (Channel == 1 || Channel == 2) {
PixelGrid[PixelIdx].X = x;
PixelGrid[PixelIdx].Y = y;
PixelGrid[PixelIdx].Channel = Channel;
PixelGrid[PixelIdx].Last = FALSE;
PixelIdx++;
PixelGrid[PixelIdx].Time = PixelGrid[PixelIdx - 1].Time;
PixelGrid[PixelIdx].X = x;
PixelGrid[PixelIdx].Y = y + 1;
PixelGrid[PixelIdx].Channel = Channel;
PixelGrid[PixelIdx].Last = FALSE;
PixelIdx++;
}
else if (Channel == 3) {
PixelGrid[PixelIdx].X = x;
PixelGrid[PixelIdx].Y = y + 1;
PixelGrid[PixelIdx].Channel = 0;
PixelGrid[PixelIdx].Last = FALSE;
PixelIdx++;
}
}
}
}
PixelGrid[PixelIdx - 1].Last = TRUE;
}
else {
for (y = 0; y < ModeSpec[Mode].NumLines; y++) {
for (Channel = 0; Channel < NumChans; Channel++) {
for (x = 0; x < ModeSpec[Mode].ImgWidth; x++) {
if (Mode == R36 || Mode == R24) {
if (Channel == 1) {
if (y % 2 == 0)
PixelGrid[PixelIdx].Channel = 1;
else
PixelGrid[PixelIdx].Channel = 2;
}
else
PixelGrid[PixelIdx].Channel = 0;
}
else{
PixelGrid[PixelIdx].Channel = Channel;
}
PixelGrid[PixelIdx].Time = (int)round(Rate * (y * ModeSpec[Mode].LineTime + ChanStart[Channel] +
(1.0 * (x - .5) / ModeSpec[Mode].ImgWidth * ChanLen[PixelGrid[PixelIdx].Channel]))) +
Skip;
PixelGrid[PixelIdx].X = x;
PixelGrid[PixelIdx].Y = y;
PixelGrid[PixelIdx].Last = FALSE;
PixelIdx++;
}
}
}
PixelGrid[PixelIdx - 1].Last = TRUE;
}
for (k = 0; k < PixelIdx; k++) {
if (PixelGrid[k].Time >= 0) {
PixelIdx = k;
break;
}
}
/*case PD50:
case PD90:
case PD120:
case PD160:
case PD180:
case PD240:
case PD290:
if (CurLineTime >= ChanStart[2] + ChanLen[2]) Channel = 3; // ch 0 of even line
else if (CurLineTime >= ChanStart[2]) Channel = 2;
else if (CurLineTime >= ChanStart[1]) Channel = 1;
else Channel = 0;
break;*/
// Initialize pixbuffer
if (!Redraw) {
g_object_unref(pixbuf_rx);
pixbuf_rx = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, ModeSpec[Mode].ImgWidth, ModeSpec[Mode].NumLines);
gdk_pixbuf_fill(pixbuf_rx, 0);
}
int rowstride = gdk_pixbuf_get_rowstride (pixbuf_rx);
guchar *pixels, *p;
pixels = gdk_pixbuf_get_pixels(pixbuf_rx);
g_object_unref(pixbuf_disp);
pixbuf_disp = gdk_pixbuf_scale_simple(pixbuf_rx, 500,
500.0/ModeSpec[Mode].ImgWidth * ModeSpec[Mode].NumLines * ModeSpec[Mode].LineHeight, GDK_INTERP_BILINEAR);
gdk_threads_enter();
gtk_image_set_from_pixbuf(GTK_IMAGE(gui.image_rx), pixbuf_disp);
gdk_threads_leave();
if(NumChans == 4) //In PD* modes, each radio frame encodes two image lines
Length = ModeSpec[Mode].LineTime * ModeSpec[Mode].NumLines/2 * 44100;
else
Length = ModeSpec[Mode].LineTime * ModeSpec[Mode].NumLines * 44100;
SyncTargetBin = GetBin(1200 + CurrentPic.HedrShift, FFTLen);
Abort = FALSE;
SyncSampleNum = 0;
// Loop through signal
for (SampleNum = 0; SampleNum < Length; SampleNum++) {
if (!Redraw) {
/*** Read ahead from sound card ***/
if (pcm.WindowPtr == 0 || pcm.WindowPtr >= BUFLEN-1024) readPcm(2048);
/*** Store the sync band for later adjustments ***/
if (SampleNum == NextSyncTime) {
Praw = Psync = 0;
memset(fft.in, 0, sizeof(double)*FFTLen);
// Hann window
for (i = 0; i < 64; i++) fft.in[i] = pcm.Buffer[pcm.WindowPtr+i-32] / 32768.0 * Hann[1][i];
fftw_execute(fft.Plan1024);
for (i=GetBin(1500+CurrentPic.HedrShift,FFTLen); i<=GetBin(2300+CurrentPic.HedrShift, FFTLen); i++)
Praw += power(fft.out[i]);
for (i=SyncTargetBin-1; i<=SyncTargetBin+1; i++)
Psync += power(fft.out[i]) * (1- .5*abs(SyncTargetBin-i));
Praw /= (GetBin(2300+CurrentPic.HedrShift, FFTLen) - GetBin(1500+CurrentPic.HedrShift, FFTLen));
Psync /= 2.0;
// If there is more than twice the amount of power per Hz in the
// sync band than in the video band, we have a sync signal here
HasSync[SyncSampleNum] = (Psync > 2*Praw);
NextSyncTime += 13;
SyncSampleNum ++;
}
/*** Estimate SNR ***/
if (SampleNum == NextSNRtime) {
memset(fft.in, 0, sizeof(double)*FFTLen);
// Apply Hann window
for (i = 0; i < FFTLen; i++) fft.in[i] = pcm.Buffer[pcm.WindowPtr + i - FFTLen/2] / 32768.0 * Hann[6][i];
fftw_execute(fft.Plan1024);
// Calculate video-plus-noise power (1500-2300 Hz)
Pvideo_plus_noise = 0;
for (n = GetBin(1500+CurrentPic.HedrShift, FFTLen); n <= GetBin(2300+CurrentPic.HedrShift, FFTLen); n++)
Pvideo_plus_noise += power(fft.out[n]);
// Calculate noise-only power (400-800 Hz + 2700-3400 Hz)
Pnoise_only = 0;
for (n = GetBin(400+CurrentPic.HedrShift, FFTLen); n <= GetBin(800+CurrentPic.HedrShift, FFTLen); n++)
Pnoise_only += power(fft.out[n]);
for (n = GetBin(2700+CurrentPic.HedrShift, FFTLen); n <= GetBin(3400+CurrentPic.HedrShift, FFTLen); n++)
Pnoise_only += power(fft.out[n]);
// Bandwidths
VideoPlusNoiseBins = GetBin(2300, FFTLen) - GetBin(1500, FFTLen) + 1;
NoiseOnlyBins = GetBin(800, FFTLen) - GetBin(400, FFTLen) + 1 +
GetBin(3400, FFTLen) - GetBin(2700, FFTLen) + 1;
ReceiverBins = GetBin(3400, FFTLen) - GetBin(400, FFTLen);
// Eq 15
Pnoise = Pnoise_only * (1.0 * ReceiverBins / NoiseOnlyBins);
Psignal = Pvideo_plus_noise - Pnoise_only * (1.0 * VideoPlusNoiseBins / NoiseOnlyBins);
// Lower bound to -20 dB
SNR = ((Psignal / Pnoise < .01) ? -20 : 10 * log10(Psignal / Pnoise));
NextSNRtime += 256;
}
/*** FM demodulation ***/
if (SampleNum % 6 == 0) { // Take FFT every 6 samples
PrevFreq = Freq;
// Adapt window size to SNR
if (!Adaptive) WinIdx = 0;
else if (SNR >= 20) WinIdx = 0;
else if (SNR >= 10) WinIdx = 1;
else if (SNR >= 9) WinIdx = 2;
else if (SNR >= 3) WinIdx = 3;
else if (SNR >= -5) WinIdx = 4;
else if (SNR >= -10) WinIdx = 5;
else WinIdx = 6;
// Minimum winlength can be doubled for Scottie DX
if (Mode == SDX && WinIdx < 6) WinIdx++;
memset(fft.in, 0, sizeof(double)*FFTLen);
memset(Power, 0, sizeof(double)*1024);
// Apply window function
WinLength = HannLens[WinIdx];
for (i = 0; i < WinLength; i++) fft.in[i] = pcm.Buffer[pcm.WindowPtr + i - WinLength/2] / 32768.0 * Hann[WinIdx][i];
fftw_execute(fft.Plan1024);
MaxBin = 0;
// Find the bin with most power
for (n = GetBin(1500 + CurrentPic.HedrShift, FFTLen) - 1; n <= GetBin(2300 + CurrentPic.HedrShift, FFTLen) + 1; n++) {
Power[n] = power(fft.out[n]);
if (MaxBin == 0 || Power[n] > Power[MaxBin]) MaxBin = n;
}
// Find the peak frequency by Gaussian interpolation
if (MaxBin > GetBin(1500 + CurrentPic.HedrShift, FFTLen) - 1 && MaxBin < GetBin(2300 + CurrentPic.HedrShift, FFTLen) + 1) {
Freq = MaxBin + (log( Power[MaxBin + 1] / Power[MaxBin - 1] )) /
(2 * log( pow(Power[MaxBin], 2) / (Power[MaxBin + 1] * Power[MaxBin - 1])));
// In Hertz
Freq = Freq / FFTLen * 44100;
} else {
// Clip if out of bounds
Freq = ( (MaxBin > GetBin(1900 + CurrentPic.HedrShift, FFTLen)) ? 2300 : 1500 ) + CurrentPic.HedrShift;
}
} /* endif (SampleNum == PixelGrid[PixelIdx].Time) */
// Linear interpolation of (chronologically) intermediate frequencies, for redrawing
//InterpFreq = PrevFreq + (Freq-PrevFreq) * ... // TODO!
// Calculate luminency & store for later use
StoredLum[SampleNum] = clip((Freq - (1500 + CurrentPic.HedrShift)) / 3.1372549);
} /* endif (!Redraw) */
if (SampleNum == PixelGrid[PixelIdx].Time) {
//In PD* modes, two pixels need data from the same sample
//Can't move on from SampleNum, until all are processed
while (SampleNum == PixelGrid[PixelIdx].Time) {
x = PixelGrid[PixelIdx].X;
y = PixelGrid[PixelIdx].Y;
Channel = PixelGrid[PixelIdx].Channel;
// Store pixel
Image[x][y][Channel] = StoredLum[SampleNum];
// Some modes have R-Y & B-Y channels that are twice the height of the Y channel
if (Channel > 0 && (Mode == R36 || Mode == R24))
Image[x][y+1][Channel] = StoredLum[SampleNum];
// Calculate and draw pixels to pixbuf on line change
if (x == ModeSpec[Mode].ImgWidth - 1 || PixelGrid[PixelIdx].Last) {
for (tx = 0; tx < ModeSpec[Mode].ImgWidth; tx++) {
p = pixels + y * rowstride + tx * 3;
switch(ModeSpec[Mode].ColorEnc) {
case RGB:
p[0] = Image[tx][y][0];
p[1] = Image[tx][y][1];
p[2] = Image[tx][y][2];
break;
case GBR:
p[0] = Image[tx][y][2];
p[1] = Image[tx][y][0];
p[2] = Image[tx][y][1];
break;
case YUV:
p[0] = clip((100 * Image[tx][y][0] + 140 * Image[tx][y][1] - 17850) / 100.0);
p[1] = clip((100 * Image[tx][y][0] - 71 * Image[tx][y][1] - 33 *
Image[tx][y][2] + 13260) / 100.0);
p[2] = clip((100 * Image[tx][y][0] + 178 * Image[tx][y][2] - 22695) / 100.0);
break;
case BW:
p[0] = p[1] = p[2] = Image[tx][y][0];
break;
}
}
if (!Redraw || y % 5 == 0 || PixelGrid[PixelIdx].Last) {
// Scale and update image
g_object_unref(pixbuf_disp);
pixbuf_disp = gdk_pixbuf_scale_simple(pixbuf_rx, 500,
500.0 / ModeSpec[Mode].ImgWidth * ModeSpec[Mode].NumLines * ModeSpec[Mode].LineHeight, GDK_INTERP_BILINEAR);
gdk_threads_enter();
gtk_image_set_from_pixbuf(GTK_IMAGE(gui.image_rx), pixbuf_disp);
gdk_threads_leave();
}
}
PixelIdx++;
}
} /* endif (SampleNum == PixelGrid[PixelIdx].Time) */
if (!Redraw && SampleNum % 8820 == 0) {
setVU(Power, FFTLen, WinIdx, TRUE);
}
if (Abort) {
free(PixelGrid);
return FALSE;
}
pcm.WindowPtr ++;
}
free(PixelGrid);
return TRUE;
}