-
Notifications
You must be signed in to change notification settings - Fork 0
/
analogtv.c
1602 lines (1320 loc) · 46.7 KB
/
analogtv.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* analogtv, Copyright (c) 2003, 2004 Trevor Blackwell <[email protected]>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
/*
This is the code for implementing something that looks like a conventional
analog TV set. It simulates the following characteristics of standard
televisions:
- Realistic rendering of a composite video signal
- Compression & brightening on the right, as the scan gets truncated
because of saturation in the flyback transformer
- Blooming of the picture dependent on brightness
- Overscan, cutting off a few pixels on the left side.
- Colored text in mixed graphics/text modes
It's amazing how much it makes your high-end monitor look like at large
late-70s TV. All you need is to put a big "Solid State" logo in curly script
on it and you'd be set.
In DirectColor or TrueColor modes, it generates pixel values
directly from RGB values it calculates across each scan line. In
PseudoColor mode, it consider each possible pattern of 5 preceding
bit values in each possible position modulo 4 and allocates a color
for each. A few things, like the brightening on the right side as
the horizontal trace slows down, aren't done in PseudoColor.
I originally wrote it for the Apple ][ emulator, and generalized it
here for use with a rewrite of xteevee and possibly others.
A maxim of technology is that failures reveal underlying mechanism.
A good way to learn how something works is to push it to failure.
The way it fails will usually tell you a lot about how it works. The
corollary for this piece of software is that in order to emulate
realistic failures of a TV set, it has to work just like a TV set.
So there is lots of DSP-style emulation of analog circuitry for
things like color decoding, H and V sync following, and more. In
2003, computers are just fast enough to do this at television signal
rates. We use a 14 MHz sample rate here, so we can do on the order
of a couple hundred instructions per sample and keep a good frame
rate.
Trevor Blackwell <[email protected]>
*/
/*
2014-04-20, Dave Odell <[email protected]>:
API change: Folded analogtv_init_signal and *_add_signal into *_draw().
Added SMP support.
Replaced doubles with floats, including constants and transcendental functions.
Fixed a bug or two.
*/
/*
2014-06-11, Anders Kaare Straadt <[email protected]>:
forked analogtv.*, stripped it of xlib/xscreensaver dependencies
*/
struct analogtv_yiq_s {
float y,i,q;
} /*yiq[ANALOGTV_PIC_LEN+10] */;
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "analogtv.h"
#define FASTRND_A 1103515245
#define FASTRND_C 12345
#define FASTRND (fastrnd = fastrnd*FASTRND_A+FASTRND_C)
static float frand(float scale)
{
return ((float)rand() / (float)RAND_MAX) * scale;
}
static float puramp(const analogtv *it, float tc, float start, float over)
{
float pt=it->powerup-start;
float ret;
if (pt<0.0f) return 0.0f;
if (pt>900.0f || pt/tc>8.0f) return 1.0f;
ret=(1.0f-expf(-pt/tc))*over;
if (ret>1.0f) return 1.0f;
return ret*ret;
}
/*
There are actual standards for TV signals: NTSC and RS-170A describe the
system used in the US and Japan. Europe has slightly different systems, but
not different enough to make substantially different screensaver displays.
Sadly, the standards bodies don't do anything so useful as publish the spec on
the web. Best bets are:
http://www.ee.washington.edu/conselec/CE/kuhn/ntsc/95x4.htm
http://www.ntsc-tv.com/ntsc-index-02.htm
In DirectColor or TrueColor modes, it generates pixel values directly from RGB
values it calculates across each scan line. In PseudoColor mode, it consider
each possible pattern of 5 preceding bit values in each possible position
modulo 4 and allocates a color for each. A few things, like the brightening on
the right side as the horizontal trace slows down, aren't done in PseudoColor.
I'd like to add a bit of visible retrace, but it conflicts with being able to
bitcopy the image when fast scrolling. After another couple of CPU
generations, we could probably regenerate the whole image from scratch every
time. On a P4 2 GHz it can manage this fine for blinking text, but scrolling
looks too slow.
*/
void
analogtv_set_defaults(analogtv *it)
{
it->tint_control = 5.0f;
it->color_control = 70.0f/100.0f;
it->brightness_control = ANALOGTV_DEF_BRIGHTNESS / 100.0;
it->contrast_control = ANALOGTV_DEF_CONTRAST / 100.0;
it->height_control = 1.0;
it->width_control = 1.0;
it->squish_control = 0.0;
it->powerup=1000.0;
#if 0
it->hashnoise_rpm=0;
it->hashnoise_on=0;
it->hashnoise_enable=1;
#endif
it->horiz_desync=frand(10.0)-5.0;
it->squeezebottom=frand(5.0)-1.0;
}
/* Can be any power-of-two <= 32. 16 a slightly better choice for 2-3 threads. */
#define ANALOGTV_SUBTOTAL_LEN 32
analogtv *
analogtv_allocate(int width, int height)
{
analogtv *it=NULL;
int i;
const size_t rx_signal_len = ANALOGTV_SIGNAL_LEN + 2*ANALOGTV_H;
it=(analogtv *)calloc(1,sizeof(analogtv));
if (!it) return 0;
it->width = width;
it->height = height;
it->image = malloc(width * height * sizeof(uint32_t));
it->bytes_per_line = it->width * sizeof(uint32_t);
it->bytes_per_pixel = sizeof(uint32_t);
it->rx_signal=NULL;
it->signal_subtotals=NULL;
it->rx_signal = malloc(sizeof(it->rx_signal[0]) * rx_signal_len);
if (it->rx_signal == NULL) goto fail;
assert(!(ANALOGTV_SIGNAL_LEN % ANALOGTV_SUBTOTAL_LEN));
it->signal_subtotals = malloc(sizeof(it->signal_subtotals[0]) * (rx_signal_len / ANALOGTV_SUBTOTAL_LEN));
if (it->signal_subtotals == NULL) goto fail;
#if 0
it->shrinkpulse=-1;
#endif
for (i=0; i<ANALOGTV_CV_MAX; i++) {
int intensity = pow(i/256.0, 0.8)*255.0; /* gamma correction */
if (intensity > 255) intensity = 255;
it->intensity[i] = intensity;
}
it->xrepl=1+it->width/640;
if (it->xrepl>2) it->xrepl=2;
it->subwidth=it->width/it->xrepl;
analogtv_set_defaults(it);
return it;
fail:
if (it) {
free(it);
}
return NULL;
}
void
analogtv_release(analogtv *it)
{
free(it->rx_signal);
free(it->signal_subtotals);
free(it);
}
/*
First generate the I and Q reference signals, which we'll multiply
the input signal by to accomplish the demodulation. Normally they
are shifted 33 degrees from the colorburst. I think this was convenient
for inductor-capacitor-vacuum tube implementation.
The tint control, FWIW, just adds a phase shift to the chroma signal,
and the color control controls the amplitude.
In text modes (colormode==0) the system disabled the color burst, and no
color was detected by the monitor.
freq_error gives a mismatch between the built-in oscillator and the
TV's colorbust. Some II Plus machines seemed to occasionally get
instability problems -- the crystal oscillator was a single
transistor if I remember correctly -- and the frequency would vary
enough that the tint would change across the width of the screen.
The left side would be in correct tint because it had just gotten
resynchronized with the color burst.
If we're using a colormap, set it up.
*/
#if 0
unsigned int
analogtv_line_signature(analogtv_input *input, int lineno)
{
int i;
char *origsignal=&input->signal[(lineno+input->vsync)
%ANALOGTV_V][input->line_hsync[lineno]];
unsigned int hash=0;
/* probably lame */
for (i=0; i<ANALOGTV_PIC_LEN; i++) {
int c=origsignal[i];
hash = hash + (hash<<17) + c;
}
hash += input->line_hsync[lineno];
hash ^= hash >> 2;
/*
hash += input->hashnoise_times[lineno];
hash ^= hash >> 2;
*/
return hash;
}
#endif
/* Here we model the analog circuitry of an NTSC television.
Basically, it splits the signal into 3 signals: Y, I and Q. Y
corresponds to luminance, and you get it by low-pass filtering the
input signal to below 3.57 MHz.
I and Q are the in-phase and quadrature components of the 3.57 MHz
subcarrier. We get them by multiplying by cos(3.57 MHz*t) and
sin(3.57 MHz*t), and low-pass filtering. Because the eye has less
resolution in some colors than others, the I component gets
low-pass filtered at 1.5 MHz and the Q at 0.5 MHz. The I component
is approximately orange-blue, and Q is roughly purple-green. See
http://www.ntsc-tv.com for details.
We actually do an awful lot to the signal here. I suspect it would
make sense to wrap them all up together by calculating impulse
response and doing FFT convolutions.
*/
static void
analogtv_ntsc_to_yiq(const analogtv *it, int lineno, const float *signal,
int start, int end, struct analogtv_yiq_s *it_yiq)
{
enum {MAXDELAY=32};
int i;
const float *sp;
int phasecorr=(signal-it->rx_signal)&3;
struct analogtv_yiq_s *yiq;
int colormode;
float agclevel=it->agclevel;
float brightadd=it->brightness_control*100.0 - ANALOGTV_BLACK_LEVEL;
float delay[MAXDELAY+ANALOGTV_PIC_LEN], *dp;
float multiq2[4];
{
double cb_i=(it->line_cb_phase[lineno][(2+phasecorr)&3]-
it->line_cb_phase[lineno][(0+phasecorr)&3])/16.0;
double cb_q=(it->line_cb_phase[lineno][(3+phasecorr)&3]-
it->line_cb_phase[lineno][(1+phasecorr)&3])/16.0;
colormode = (cb_i * cb_i + cb_q * cb_q) > 2.8;
if (colormode) {
double tint_i = -cos((103 + it->color_control)*3.1415926/180);
double tint_q = sin((103 + it->color_control)*3.1415926/180);
multiq2[0] = (cb_i*tint_i - cb_q*tint_q) * it->color_control;
multiq2[1] = (cb_q*tint_i + cb_i*tint_q) * it->color_control;
multiq2[2]=-multiq2[0];
multiq2[3]=-multiq2[1];
}
}
#if 0
if (lineno==100) {
printf("multiq = [%0.3f %0.3f %0.3f %0.3f] ",
it->multiq[60],it->multiq[61],it->multiq[62],it->multiq[63]);
printf("it->line_cb_phase = [%0.3f %0.3f %0.3f %0.3f]\n",
it->line_cb_phase[lineno][0],it->line_cb_phase[lineno][1],
it->line_cb_phase[lineno][2],it->line_cb_phase[lineno][3]);
printf("multiq2 = [%0.3f %0.3f %0.3f %0.3f]\n",
multiq2[0],multiq2[1],multiq2[2],multiq2[3]);
}
#endif
dp=delay+ANALOGTV_PIC_LEN-MAXDELAY;
for (i=0; i<5; i++) dp[i]=0.0f;
assert(start>=0);
assert(end < ANALOGTV_PIC_LEN+10);
dp=delay+ANALOGTV_PIC_LEN-MAXDELAY;
for (i=0; i<24; i++) dp[i]=0.0;
for (i=start, yiq=it_yiq+start, sp=signal+start;
i<end;
i++, dp--, yiq++, sp++) {
/* Now filter them. These are infinite impulse response filters
calculated by the script at
http://www-users.cs.york.ac.uk/~fisher/mkfilter. This is
fixed-point integer DSP, son. No place for wimps. We do it in
integer because you can count on integer being faster on most
CPUs. We care about speed because we need to recalculate every
time we blink text, and when we spew random bytes into screen
memory. This is roughly 16.16 fixed point arithmetic, but we
scale some filter values up by a few bits to avoid some nasty
precision errors. */
/* Filter Y with a 4-pole low-pass Butterworth filter at 3.5 MHz
with an extra zero at 3.5 MHz, from
mkfilter -Bu -Lp -o 4 -a 2.1428571429e-01 0 -Z 2.5e-01 -l
Delay about 2 */
dp[0] = sp[0] * 0.0469904257251935f * agclevel;
dp[8] = (+1.0f*(dp[6]+dp[0])
+4.0f*(dp[5]+dp[1])
+7.0f*(dp[4]+dp[2])
+8.0f*(dp[3])
-0.0176648f*dp[12]
-0.4860288f*dp[10]);
yiq->y = dp[8] + brightadd;
}
if (colormode) {
dp=delay+ANALOGTV_PIC_LEN-MAXDELAY;
for (i=0; i<27; i++) dp[i]=0.0;
for (i=start, yiq=it_yiq+start, sp=signal+start;
i<end;
i++, dp--, yiq++, sp++) {
float sig=*sp;
/* Filter I and Q with a 3-pole low-pass Butterworth filter at
1.5 MHz with an extra zero at 3.5 MHz, from
mkfilter -Bu -Lp -o 3 -a 1.0714285714e-01 0 -Z 2.5000000000e-01 -l
Delay about 3.
*/
dp[0] = sig*multiq2[i&3] * 0.0833333333333f;
yiq->i=dp[8] = (dp[5] + dp[0]
+3.0f*(dp[4] + dp[1])
+4.0f*(dp[3] + dp[2])
-0.3333333333f * dp[10]);
dp[16] = sig*multiq2[(i+3)&3] * 0.0833333333333f;
yiq->q=dp[24] = (dp[16+5] + dp[16+0]
+3.0f*(dp[16+4] + dp[16+1])
+4.0f*(dp[16+3] + dp[16+2])
-0.3333333333f * dp[24+2]);
}
} else {
for (i=start, yiq=it_yiq+start; i<end; i++, yiq++) {
yiq->i = yiq->q = 0.0f;
}
}
}
void
analogtv_setup_frame(analogtv *it)
{
//int i,x,y;
if (it->flutter_horiz_desync) {
/* Horizontal sync during vertical sync instability. */
it->horiz_desync += -0.10*(it->horiz_desync-3.0) +
((int)(rand()&0xff)-0x80) *
((int)(rand()&0xff)-0x80) *
((int)(rand()&0xff)-0x80) * 0.000001;
}
#if 0
for (i=0; i<ANALOGTV_V; i++) {
it->hashnoise_times[i]=0;
}
if (it->hashnoise_enable && !it->hashnoise_on) {
if (rand()%10000 == 0) {
it->hashnoise_on=1;
it->shrinkpulse=rand()%ANALOGTV_V;
}
}
if (rand()%1000==0) {
it->hashnoise_on=0;
}
if (it->hashnoise_on) {
it->hashnoise_rpm += (15000.0 - it->hashnoise_rpm)*0.05 +
((int)(rand()%2000)-1000)*0.1;
} else {
it->hashnoise_rpm -= 100 + 0.01*it->hashnoise_rpm;
if (it->hashnoise_rpm<0.0) it->hashnoise_rpm=0.0;
}
if (it->hashnoise_rpm > 0.0) {
int hni;
int hnc=it->hashnoise_counter; /* in 24.8 format */
/* Convert rpm of a 16-pole motor into dots in 24.8 format */
hni = (int)(ANALOGTV_V * ANALOGTV_H * 256.0 /
(it->hashnoise_rpm * 16.0 / 60.0 / 60.0));
while (hnc < (ANALOGTV_V * ANALOGTV_H)<<8) {
y=(hnc>>8)/ANALOGTV_H;
x=(hnc>>8)%ANALOGTV_H;
if (x>0 && x<ANALOGTV_H - ANALOGTV_HASHNOISE_LEN) {
it->hashnoise_times[y]=x;
}
hnc += hni + (int)(rand()%65536)-32768;
}
/* hnc -= (ANALOGTV_V * ANALOGTV_H)<<8;*/
}
#endif
if (it->rx_signal_level != 0.0)
it->agclevel = 1.0/it->rx_signal_level;
#ifdef DEBUG2
printf("filter: ");
for (i=0; i<ANALOGTV_GHOSTFIR_LEN; i++) {
printf(" %0.3f",it->ghostfir[i]);
}
printf(" siglevel=%g agc=%g\n", siglevel, it->agclevel);
#endif
}
void
analogtv_setup_synclevel(analogtv_input *input, int do_cb, int synclevel)
{
int i,lineno,vsync;
signed char *sig;
for (lineno=0; lineno<ANALOGTV_V; lineno++) {
vsync=lineno>=3 && lineno<7;
sig=input->signal[lineno];
i=ANALOGTV_SYNC_START;
if (vsync) {
while (i<ANALOGTV_BP_START) sig[i++]=ANALOGTV_BLANK_LEVEL;
while (i<ANALOGTV_H) sig[i++]=synclevel;
} else {
while (i<ANALOGTV_BP_START) sig[i++]=synclevel;
while (i<ANALOGTV_PIC_START) sig[i++]=ANALOGTV_BLANK_LEVEL;
while (i<ANALOGTV_FP_START) sig[i++]=ANALOGTV_BLACK_LEVEL;
}
while (i<ANALOGTV_H) sig[i++]=ANALOGTV_BLANK_LEVEL;
if (do_cb) {
/* 9 cycles of colorburst */
for (i=ANALOGTV_CB_START; i<ANALOGTV_CB_START+36; i+=4) {
sig[i+1] += ANALOGTV_CB_LEVEL;
sig[i+3] -= ANALOGTV_CB_LEVEL;
}
}
}
}
void
analogtv_setup_sync(analogtv_input *input, int do_cb, int do_ssavi)
{
analogtv_setup_synclevel(input, do_cb, do_ssavi ? ANALOGTV_WHITE_LEVEL : ANALOGTV_SYNC_LEVEL);
}
static void
analogtv_sync(analogtv *it)
{
int cur_hsync=it->cur_hsync;
int cur_vsync=it->cur_vsync;
int lineno = 0;
int i,j;
float osc,filt;
float *sp;
float cbfc=1.0f/128.0f;
/* sp = it->rx_signal + lineno*ANALOGTV_H + cur_hsync;*/
for (i=-32; i<32; i++) {
lineno = (cur_vsync + i + ANALOGTV_V) % ANALOGTV_V;
sp = it->rx_signal + lineno*ANALOGTV_H;
filt=0.0f;
for (j=0; j<ANALOGTV_H; j+=ANALOGTV_H/16) {
filt += sp[j];
}
filt *= it->agclevel;
osc = (float)(ANALOGTV_V+i)/(float)ANALOGTV_V;
if (osc >= 1.05f+0.0002f * filt) break;
}
cur_vsync = (cur_vsync + i + ANALOGTV_V) % ANALOGTV_V;
for (lineno=0; lineno<ANALOGTV_V; lineno++) {
if (lineno>5 && lineno<ANALOGTV_V-3) { /* ignore vsync interval */
unsigned lineno2 = (lineno + cur_vsync + ANALOGTV_V)%ANALOGTV_V;
if (!lineno2) lineno2 = ANALOGTV_V;
sp = it->rx_signal + lineno2*ANALOGTV_H + cur_hsync;
for (i=-8; i<8; i++) {
osc = (float)(ANALOGTV_H+i)/(float)ANALOGTV_H;
filt=(sp[i-3]+sp[i-2]+sp[i-1]+sp[i]) * it->agclevel;
if (osc >= 1.005f + 0.0001f*filt) break;
}
cur_hsync = (cur_hsync + i + ANALOGTV_H) % ANALOGTV_H;
}
it->line_hsync[lineno]=(cur_hsync + ANALOGTV_PIC_START +
ANALOGTV_H) % ANALOGTV_H;
/* Now look for the colorburst, which is a few cycles after the H
sync pulse, and store its phase.
The colorburst is 9 cycles long, and we look at the middle 5
cycles.
*/
if (lineno>15) {
sp = it->rx_signal + lineno*ANALOGTV_H + (cur_hsync&~3);
for (i=ANALOGTV_CB_START+8; i<ANALOGTV_CB_START+36-8; i++) {
it->cb_phase[i&3] = it->cb_phase[i&3]*(1.0f-cbfc) +
sp[i]*it->agclevel*cbfc;
}
}
{
float tot=0.1f;
float cbgain;
for (i=0; i<4; i++) {
tot += it->cb_phase[i]*it->cb_phase[i];
}
cbgain = 32.0f/sqrtf(tot);
for (i=0; i<4; i++) {
it->line_cb_phase[lineno][i]=it->cb_phase[i]*cbgain;
}
}
#ifdef DEBUG
if (0) printf("hs=%d cb=[%0.3f %0.3f %0.3f %0.3f]\n",
cur_hsync,
it->cb_phase[0], it->cb_phase[1],
it->cb_phase[2], it->cb_phase[3]);
#endif
/* if (rand()%2000==0) cur_hsync=rand()%ANALOGTV_H; */
}
it->cur_hsync = cur_hsync;
it->cur_vsync = cur_vsync;
}
#if 0
static double
analogtv_levelmult(const analogtv *it, int level)
{
static const double levelfac[3]={-7.5, 5.5, 24.5};
return (40.0 + levelfac[level]*puramp(it, 3.0, 6.0, 1.0))/256.0;
}
#endif
#if 0
static int
analogtv_level(const analogtv *it, int y, int ytop, int ybot)
{
int level;
if (ybot-ytop>=7) {
if (y==ytop || y==ybot-1) level=0;
else if (y==ytop+1 || y==ybot-2) level=1;
else level=2;
}
else if (ybot-ytop>=5) {
if (y==ytop || y==ybot-1) level=0;
else level=2;
}
else if (ybot-ytop>=3) {
if (y==ytop) level=0;
else level=2;
}
else {
level=2;
}
return level;
}
#endif
/*
The point of this stuff is to ensure that when height is not a
multiple of VISLINES so that TV scan lines map to different numbers
of vertical screen pixels, the total brightness of each scan line
remains the same.
ANALOGTV_MAX_LINEHEIGHT corresponds to 2400 vertical pixels, beyond which
it interpolates extra black lines.
*/
static void
analogtv_setup_levels(analogtv *it, double avgheight)
{
int i,height;
static const double levelfac[3]={-7.5, 5.5, 24.5};
for (height=0; height<avgheight+2.0 && height<=ANALOGTV_MAX_LINEHEIGHT; height++) {
for (i=0; i<height; i++) {
it->leveltable[height][i].index = 2;
}
if (avgheight>=3) {
it->leveltable[height][0].index=0;
}
if (avgheight>=5) {
if (height >= 1) it->leveltable[height][height-1].index=0;
}
if (avgheight>=7) {
it->leveltable[height][1].index=1;
if (height >= 2) it->leveltable[height][height-2].index=1;
}
for (i=0; i<height; i++) {
it->leveltable[height][i].value =
(40.0 + levelfac[it->leveltable[height][i].index]*puramp(it, 3.0, 6.0, 1.0)) / 256.0;
}
}
}
static void rnd_combine(unsigned *a0, unsigned *c0, unsigned a1, unsigned c1)
{
*a0 = (*a0 * a1) & 0xffffffffu;
*c0 = (c1 + a1 * *c0) & 0xffffffffu;
}
static void rnd_seek_ac(unsigned *a, unsigned *c, unsigned dist)
{
unsigned int a1 = *a, c1 = *c;
*a = 1, *c = 0;
while(dist)
{
if(dist & 1)
rnd_combine(a, c, a1, c1);
dist >>= 1;
rnd_combine(&a1, &c1, a1, c1);
}
}
static unsigned int rnd_seek(unsigned a, unsigned c, unsigned rnd, unsigned dist)
{
rnd_seek_ac(&a, &c, dist);
return a * rnd + c;
}
static void analogtv_init_signal(const analogtv *it, double noiselevel, unsigned start, unsigned end)
{
float *ps=it->rx_signal + start;
float *pe=it->rx_signal + end;
float *p=ps;
unsigned int fastrnd=rnd_seek(FASTRND_A, FASTRND_C, it->random0, start);
float nm1,nm2;
float noisemul = sqrt(noiselevel*150)/(float)0x7fffffff;
nm1 = ((int)fastrnd-(int)0x7fffffff) * noisemul;
while (p != pe) {
nm2=nm1;
fastrnd = (fastrnd*FASTRND_A+FASTRND_C) & 0xffffffffu;
nm1 = ((int)fastrnd-(int)0x7fffffff) * noisemul;
*p++ = nm1*nm2;
}
}
static void analogtv_add_signal(const analogtv *it, const analogtv_reception *rec, unsigned start, unsigned end, int ec)
{
analogtv_input *inp=rec->input;
float *ps=it->rx_signal + start;
float *pe=it->rx_signal + end;
float *p=ps;
signed char *ss=&inp->signal[0][0];
signed char *se=&inp->signal[0][0] + ANALOGTV_SIGNAL_LEN;
signed char *s=ss + ((start + (unsigned)rec->ofs) % ANALOGTV_SIGNAL_LEN);
signed char *s2;
int i;
float level=rec->level;
float hfloss=rec->hfloss;
unsigned int fastrnd=rnd_seek(FASTRND_A, FASTRND_C, it->random1, start);
float dp[5];
const float noise_decay = 0.99995f;
float noise_ampl = 1.3f * powf(noise_decay, start);
if (ec > end)
ec = end;
/* assert((se-ss)%4==0 && (se-s)%4==0); */
for (i = start; i < ec; i++) { /* Sometimes start > ec. */
/* Do a big noisy transition. We can make the transition noise of
high constant strength regardless of signal strength.
There are two separate state machines. here, One is the noise
process and the other is the
We don't bother with the FIR filter here
*/
float sig0=(float)s[0];
float noise = ((int)fastrnd-(int)0x7fffffff) * (50.0f/(float)0x7fffffff);
fastrnd = (fastrnd*FASTRND_A+FASTRND_C) & 0xffffffffu;
p[0] += sig0 * level * (1.0f - noise_ampl) + noise * noise_ampl;
noise_ampl *= noise_decay;
p++;
s++;
if (s>=se) s=ss;
}
dp[0]=0.0;
s2 = s;
for (i=1; i<5; i++) {
s2 -= 4;
if (s2 < ss)
s2 += ANALOGTV_SIGNAL_LEN;
dp[i] = (float)((int)s2[0]+(int)s2[1]+(int)s2[2]+(int)s2[3]);
}
assert(p <= pe);
assert(!((pe - p) % 4));
while (p != pe) {
float sig0,sig1,sig2,sig3,sigr;
sig0=(float)s[0];
sig1=(float)s[1];
sig2=(float)s[2];
sig3=(float)s[3];
dp[0]=sig0+sig1+sig2+sig3;
/* Get the video out signal, and add some ghosting, typical of RF
monitor cables. This corresponds to a pretty long cable, but
looks right to me.
*/
sigr=(dp[1]*rec->ghostfir[0] + dp[2]*rec->ghostfir[1] +
dp[3]*rec->ghostfir[2] + dp[4]*rec->ghostfir[3]);
dp[4]=dp[3]; dp[3]=dp[2]; dp[2]=dp[1]; dp[1]=dp[0];
p[0] += (sig0+sigr + sig2*hfloss) * level;
p[1] += (sig1+sigr + sig3*hfloss) * level;
p[2] += (sig2+sigr + sig0*hfloss) * level;
p[3] += (sig3+sigr + sig1*hfloss) * level;
p += 4;
s += 4;
if (s>=se) s = ss + (s-se);
}
assert(p == pe);
}
static void analogtv_thread_add_signals(analogtv* it)
{
unsigned i, j;
unsigned subtotal_end;
unsigned start = 0;
unsigned int signal_end = ANALOGTV_SIGNAL_LEN;
while(start != signal_end)
{
float *p;
/* Work on 8 KB blocks; these should fit in L1. */
/* (Though it doesn't seem to help much on my system.) */
unsigned end = start + 2048;
if(end > signal_end)
end = signal_end;
analogtv_init_signal (it, it->noiselevel, start, end);
for (i = 0; i != it->rec_count; ++i) {
analogtv_add_signal (it, it->recs[i], start, end,
!i ? it->channel_change_cycles : 0);
}
assert (!(start % ANALOGTV_SUBTOTAL_LEN));
assert (!(end % ANALOGTV_SUBTOTAL_LEN));
p = it->rx_signal + start;
subtotal_end = end / ANALOGTV_SUBTOTAL_LEN;
for (i = start / ANALOGTV_SUBTOTAL_LEN; i != subtotal_end; ++i) {
float sum = p[0];
for (j = 1; j != ANALOGTV_SUBTOTAL_LEN; ++j)
sum += p[j];
it->signal_subtotals[i] = sum;
p += ANALOGTV_SUBTOTAL_LEN;
}
start = end;
}
}
static int analogtv_get_line(const analogtv *it, int lineno, int *slineno,
int *ytop, int *ybot, unsigned *signal_offset)
{
*slineno=lineno-ANALOGTV_TOP;
*ytop=(int)((*slineno*it->height/ANALOGTV_VISLINES -
it->height/2)*it->puheight) + it->height/2;
*ybot=(int)(((*slineno+1)*it->height/ANALOGTV_VISLINES -
it->height/2)*it->puheight) + it->height/2;
#if 0
int linesig=analogtv_line_signature(input,lineno)
+ it->hashnoise_times[lineno];
#endif
*signal_offset = ((lineno+it->cur_vsync+ANALOGTV_V) % ANALOGTV_V) * ANALOGTV_H +
it->line_hsync[lineno];
if (*ytop==*ybot) return 0;
if (*ybot<0 || *ytop>it->height) return 0;
if (*ytop<0) *ytop=0;
if (*ybot>it->height) *ybot=it->height;
if (*ybot > *ytop+ANALOGTV_MAX_LINEHEIGHT) *ybot=*ytop+ANALOGTV_MAX_LINEHEIGHT;
return 1;
}
static void
analogtv_blast_imagerow(const analogtv *it,
float *rgbf, float *rgbf_end,
int ytop, int ybot)
{
int i,j,x,y;
float *rpf;
uint8_t *level_copyfrom[3];
int xrepl=it->xrepl;
unsigned lineheight = ybot - ytop;
if (lineheight > ANALOGTV_MAX_LINEHEIGHT) lineheight = ANALOGTV_MAX_LINEHEIGHT;
for (i=0; i<3; i++) level_copyfrom[i]=NULL;
for (y=ytop; y<ybot; y++) {
uint8_t *rowdata = it->image + y*it->bytes_per_line;
unsigned line = y-ytop;
int level=it->leveltable[lineheight][line].index;
float levelmult=it->leveltable[lineheight][line].value;
if (level_copyfrom[level]) {
memcpy(rowdata, level_copyfrom[level], it->bytes_per_line);
}
else {
level_copyfrom[level] = rowdata;
for (x=0, rpf=rgbf; rpf!=rgbf_end ; x++, rpf+=3) {
int ntscri=rpf[0]*levelmult;
int ntscgi=rpf[1]*levelmult;
int ntscbi=rpf[2]*levelmult;
if (ntscri>=ANALOGTV_CV_MAX) ntscri=ANALOGTV_CV_MAX-1;
if (ntscgi>=ANALOGTV_CV_MAX) ntscgi=ANALOGTV_CV_MAX-1;
if (ntscbi>=ANALOGTV_CV_MAX) ntscbi=ANALOGTV_CV_MAX-1;
int r = it->intensity[ntscri];
int g = it->intensity[ntscgi];
int b = it->intensity[ntscbi];
for (j=0; j<xrepl; j++) {
uint8_t* pixel = (uint8_t*) (rowdata + (x*xrepl + j) * it->bytes_per_pixel);
pixel[0] = b;
pixel[1] = g;
pixel[2] = r;
pixel[3] = 255;
}
}
}
}
}
static void analogtv_thread_draw_lines(analogtv* it)
{
int lineno;
float *raw_rgb_start;
float *raw_rgb_end;
raw_rgb_start=(float *)calloc(it->subwidth*3, sizeof(float)); // XXX don't malloc here!
if (! raw_rgb_start) return;
raw_rgb_end=raw_rgb_start+3*it->subwidth;
for (lineno=ANALOGTV_TOP; lineno<ANALOGTV_BOT; lineno++) {
int i;
int slineno, ytop, ybot;
unsigned signal_offset;
const float *signal;
int scanstart_i,scanend_i,squishright_i,squishdiv,pixrate;
float *rgb_start, *rgb_end;
float pixbright;
int pixmultinc;
float *rrp;
struct analogtv_yiq_s yiq[ANALOGTV_PIC_LEN+10];
if (! analogtv_get_line(it, lineno, &slineno, &ytop, &ybot,
&signal_offset))
continue;
signal = it->rx_signal + signal_offset;
{
float bloomthisrow,shiftthisrow;
float viswidth,middle;
float scanwidth;
int scw,scl,scr;
bloomthisrow = -10.0f * it->crtload[lineno];
if (bloomthisrow<-10.0f) bloomthisrow=-10.0f;
if (bloomthisrow>2.0f) bloomthisrow=2.0f;
if (slineno<16) {
shiftthisrow=it->horiz_desync * (expf(-0.17f*slineno) *
(0.7f+cosf(slineno*0.6f)));
} else {
shiftthisrow=0.0f;
}
viswidth=ANALOGTV_PIC_LEN * 0.79f - 5.0f*bloomthisrow;
middle=ANALOGTV_PIC_LEN/2 - shiftthisrow;
scanwidth=it->width_control * puramp(it, 0.5f, 0.3f, 1.0f);
scw=it->subwidth*scanwidth;
if (scw>it->subwidth) scw=it->width;
scl=it->subwidth/2 - scw/2;
scr=it->subwidth/2 + scw/2;
pixrate=(int)((viswidth*65536.0f*1.0f)/it->subwidth)/scanwidth;
scanstart_i=(int)((middle-viswidth*0.5f)*65536.0f);
scanend_i=(ANALOGTV_PIC_LEN-1)*65536;
squishright_i=(int)((middle+viswidth*(0.25f + 0.25f*puramp(it, 2.0f, 0.0f, 1.1f)
- it->squish_control)) *65536.0f);
squishdiv=it->subwidth/15;
rgb_start=raw_rgb_start+scl*3;
rgb_end=raw_rgb_start+scr*3;
assert(scanstart_i>=0);
}
// draw?
analogtv_ntsc_to_yiq(it, lineno, signal,
(scanstart_i>>16)-10, (scanend_i>>16)+10, yiq);
pixbright=it->contrast_control * puramp(it, 1.0f, 0.0f, 1.0f)
/ (0.5f+0.5f*it->puheight) * 1024.0f/100.0f;