-
Notifications
You must be signed in to change notification settings - Fork 17
/
ffmpeg_to_composite.cpp
2377 lines (2083 loc) · 85.8 KB
/
ffmpeg_to_composite.cpp
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
// what to do next:
//
// If emulating PAL, fake subcarrier needs to alternate color phase every other field scanline.
//
// Fake macrovision "darkening" of the top of the picture, and
//
// Fake macrovision "bend" at the top of the picture.
//
// Analog video "banding", slight but noticeable bright/dark bands that vary according to video content.
//
// Minor VHS noise (occasional white specks)
//
// Tracking noise (the staticky band at the bottom of the screen).
//
// After processing audio, allow encode through non-PCM codec and write to output.
//
// After processing video, allow encode through non-uncompressed codec and write to output.
//
// At some point, this code should do PROPER processing in the NTSC YIQ colorspace instead of using the YUV colorspace to fake it.
#define __STDC_CONSTANT_MACROS
#define __STDC_LIMIT_MACROS
#include <sys/types.h>
#include <signal.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
extern "C" {
#include <libavutil/opt.h>
#include <libavutil/avutil.h>
#include <libavutil/pixfmt.h>
#include <libavutil/pixdesc.h>
#include <libavutil/samplefmt.h>
#include <libavutil/pixelutils.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/version.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavformat/version.h>
#include <libswscale/swscale.h>
#include <libswscale/version.h>
#include <libswresample/swresample.h>
#include <libswresample/version.h>
}
using namespace std;
#include <map>
#include <string>
#include <vector>
volatile int DIE = 0;
void sigma(int x) {
if (++DIE >= 20) abort();
}
int audio_stream_index = 0;
int video_stream_index = 0;
string input_file;
string output_file;
/* return a floating point value specifying what to scale the sample
* value by to reduce it from full volume to dB decibels */
double dBFS(double dB)
{
/* 10 ^ (dB / 20),
based on reversing the formula for converting samples to decibels:
dB = 20.0 * log10(sample);
where "sample" is -1.0 <= x <= 1.0 */
return pow(10.0,dB / 20.0);
}
/* attenuate a sample value by this many dBFS */
/* so if you want to reduce it by 20dBFS you pass -20 as dB */
double attenuate_dBFS(double sample,double dB)
{
return sample * dBFS(dB);
}
/* opposite: convert sample to decibels */
double dBFS_measure(double sample) {
return 20.0 * log10(sample);
}
// lowpass filter
// you can make it a highpass filter by applying a lowpass then subtracting from source.
class LowpassFilter {
public:
LowpassFilter() : timeInterval(0), cutoff(0), alpha(0), prev(0), tau(0) {
}
void setFilter(const double rate/*sample rate of audio*/,const double hz/*cutoff*/) {
#ifndef M_PI
#error your math.h does not include M_PI constant
#endif
timeInterval = 1.0 / rate;
tau = 1 / (hz * 2 * M_PI);
cutoff = hz;
alpha = timeInterval / (tau + timeInterval);
}
void resetFilter(const double val=0) {
prev = val;
}
double lowpass(const double sample) {
const double stage1 = sample * alpha;
const double stage2 = prev - (prev * alpha); /* NTS: Instead of prev * (1.0 - alpha) */
return (prev = (stage1 + stage2)); /* prev = stage1+stage2 then return prev */
}
double highpass(const double sample) {
const double stage1 = sample * alpha;
const double stage2 = prev - (prev * alpha); /* NTS: Instead of prev * (1.0 - alpha) */
return sample - (prev = (stage1 + stage2)); /* prev = stage1+stage2 then return (sample - prev) */
}
public:
double timeInterval;
double cutoff;
double alpha; /* timeInterval / (tau + timeInterval) */
double prev;
double tau;
};
class HiLoPair {
public:
LowpassFilter hi,lo; // highpass, lowpass
public:
void setFilter(const double rate/*sample rate of audio*/,const double low_hz,const double high_hz) {
lo.setFilter(rate,low_hz);
hi.setFilter(rate,high_hz);
}
double filter(const double sample) {
return hi.highpass(lo.lowpass(sample)); /* first lowpass, then highpass */
}
};
class HiLoPass : public vector<HiLoPair> { // all passes, one sample of one channel
public:
HiLoPass() : vector() { }
public:
void setFilter(const double rate/*sample rate of audio*/,const double low_hz,const double high_hz) {
for (size_t i=0;i < size();i++) (*this)[i].setFilter(rate,low_hz,high_hz);
}
double filter(double sample) {
for (size_t i=0;i < size();i++) sample = (*this)[i].lo.lowpass(sample);
for (size_t i=0;i < size();i++) sample = (*this)[i].hi.highpass(sample);
return sample;
}
void init(const unsigned int passes) {
clear();
resize(passes);
assert(size() >= passes);
}
};
class HiLoSample : public vector<HiLoPass> { // all passes, all channels of one sample period
public:
HiLoSample() : vector() { }
public:
void init(const unsigned int channels,const unsigned int passes) {
clear();
resize(channels);
assert(size() >= channels);
for (size_t i=0;i < size();i++) (*this)[i].init(passes);
}
void setFilter(const double rate/*sample rate of audio*/,const double low_hz,const double high_hz) {
for (size_t i=0;i < size();i++) (*this)[i].setFilter(rate,low_hz,high_hz);
}
};
class HiLoComboPass {
public:
HiLoComboPass() : passes(0), channels(0), rate(0), low_cutoff(0), high_cutoff(0) {
}
~HiLoComboPass() {
clear();
}
void setChannels(const size_t _channels) {
if (channels != _channels) {
clear();
channels = _channels;
}
}
void setCutoff(const double _low_cutoff,const double _high_cutoff) {
if (low_cutoff != _low_cutoff || high_cutoff != _high_cutoff) {
clear();
low_cutoff = _low_cutoff;
high_cutoff = _high_cutoff;
}
}
void setRate(const double _rate) {
if (rate != _rate) {
clear();
rate = _rate;
}
}
void setPasses(const size_t _passes) {
if (passes != _passes) {
clear();
passes = _passes;
}
}
void clear() {
audiostate.clear();
}
void init() {
clear();
if (channels == 0 || passes == 0 || rate == 0 || low_cutoff == 0 || high_cutoff == 0) return;
audiostate.init(channels,passes);
audiostate.setFilter(rate,low_cutoff,high_cutoff);
}
public:
double rate;
size_t passes;
size_t channels;
double low_cutoff;
double high_cutoff;
HiLoSample audiostate;
};
HiLoComboPass audio_hilopass;
// preemphsis emuluation
LowpassFilter audio_linear_preemphasis_pre[2];
LowpassFilter audio_linear_preemphasis_post[2];
AVFormatContext* input_avfmt = NULL;
AVStream* input_avstream_audio = NULL; // do not free
AVCodecContext* input_avstream_audio_codec_context = NULL; // do not free
AVStream* input_avstream_video = NULL; // do not free
AVCodecContext* input_avstream_video_codec_context = NULL; // do not free
AVFrame* input_avstream_audio_frame = NULL;
AVFrame* input_avstream_video_frame = NULL;
int input_avstream_audio_resampler_rate = -1;
int input_avstream_audio_resampler_channels = -1;
struct SwrContext* input_avstream_audio_resampler = NULL;
struct SwsContext* input_avstream_video_resampler = NULL;
AVFormatContext* output_avfmt = NULL;
AVStream* output_avstream_audio = NULL; // do not free
AVCodecContext* output_avstream_audio_codec_context = NULL; // do not free
AVStream* output_avstream_video = NULL; // do not free
AVCodecContext* output_avstream_video_codec_context = NULL; // do not free
AVFrame* output_avstream_video_filter_frame = NULL;
AVFrame* output_avstream_video_input_frame = NULL; // 4:2:2
AVFrame* output_avstream_video_frame = NULL; // 4:2:2
AVFrame* output_avstream_video_bob_frame = NULL; // 4:2:0 or 4:2:2
bool use_422_colorspace = false; // I would default this to true but Adobe Premiere Pro apparently can't handle 4:2:2 H.264 >:(
double transcode_start = -1;
double transcode_end = -1;
double transcode_dur = -1;
double composite_preemphasis = 0; // analog artifacts related to anything that affects the raw composite signal i.e. CATV modulation
double composite_preemphasis_cut = 1000000;
double vhs_out_sharpen = 1.5;
double vhs_out_sharpen_chroma = 0.85;
bool vhs_head_switching = false;
double vhs_head_switching_phase = 1.0 - ((4.5+0.01/*slight error, like most VHS tapes*/) / 262.5); // 4 scanlines NTSC up from vsync
double vhs_head_switching_phase_noise = (((1.0 / 300)/*slight error, like most VHS tapes*/) / 262.5); // 1/300th of a scanline
bool composite_in_chroma_lowpass = true; // apply chroma lowpass before composite encode
bool composite_out_chroma_lowpass = true;
bool composite_out_chroma_lowpass_lite = true;
int video_yc_recombine = 0; // additional Y/C combine/sep phases (testing)
int video_color_fields = 4; // NTSC color framing
int video_scanline_phase_shift = 180;
int video_scanline_phase_shift_offset = 0;
int video_chroma_noise = 0;
int video_chroma_phase_noise = 0;
int video_chroma_loss = 0;
int video_noise = 2;
int subcarrier_amplitude = 50;
int subcarrier_amplitude_back = 50;
bool output_video_as_interlaced = false; // render as 480i (half field rate). else, render at field rate with bob filter
AVRational output_field_rate = { 60000, 1001 }; // NTSC 60Hz default
int output_width = 720;
int output_height = 480;
bool output_ntsc = true; // NTSC color subcarrier emulation
bool output_pal = false; // PAL color subcarrier emulation
int output_audio_channels = 2; // VHS stereo (set to 1 for mono)
int output_audio_rate = 44100; // VHS Hi-Fi goes up to 20KHz
double output_audio_hiss_db = -72;
double output_audio_linear_buzz = -42; // how loud the "buzz" is audible in dBFS (S/N). Ever notice on old VHS tapes (prior to Hi-Fi) you can almost hear the video signal sync pulses in the audio?
double output_audio_highpass = 20; // highpass to filter out below 20Hz
double output_audio_lowpass = 20000; // lowpass to filter out above 20KHz
double vhs_linear_high_boost = 0.25;
// NTS:
// VHS Hi-Fi: 20Hz - 20KHz (70dBFS S/N)
// VHS SP: 100Hz - 10KHz (42dBFS S/N)
// VHS LP: 100Hz - 7KHz (right??) (42dBFS S/N)
// VHS EP: 100Hz - 4KHz (42dBFS S/N)
bool output_vhs_hifi = true;
bool output_vhs_linear_stereo = false; // not common
bool output_vhs_linear_audio = false; // if true (non Hi-Fi) then we emulate hiss and noise of linear VHS tracks including the video sync pulses audible in the audio.
bool emulating_vhs = false;
bool emulating_preemphasis = true; // emulate preemphasis
bool emulating_deemphasis = true; // emulate deemphasis
bool nocolor_subcarrier = false; // if set, emulate subcarrier but do not decode back to color (debug)
bool nocolor_subcarrier_after_yc_sep = false;// if set, separate luma-chroma but do not decode back to color (debug)
bool vhs_chroma_vert_blend = true; // if set, and VHS, blend vertically the chroma scanlines (as the VHS format does)
bool vhs_svideo_out = false; // if not set, and VHS, video is recombined as if composite out on VCR
bool enable_composite_emulation = true; // if not set, video goes straight back out to the encoder.
bool enable_audio_emulation = true;
/* composite keying emulation */
int black_key_level_feedback = -1; // >= 0 key against black on render
int output_audio_hiss_level = 0; // out of 10000
enum {
VHS_SP=0,
VHS_LP,
VHS_EP
};
int output_vhs_tape_speed = VHS_SP;
static inline int clampu8(const int x) {
if (x > 255)
return 255;
else if (x < 0)
return 0;
return x;
}
static inline int clips16(const int x) {
if (x < -32768)
return -32768;
else if (x > 32767)
return 32767;
return x;
}
void composite_video_chroma_lowpass(AVFrame *dst,unsigned int field,unsigned long long fieldno) {
unsigned int x,y;
{ /* lowpass the chroma more. composite video does not allocate as much bandwidth to color as luma. */
for (unsigned int p=1;p <= 2;p++) {
for (y=field;y < dst->height;y += 2) {
unsigned char *P = dst->data[p] + (y * dst->linesize[p]);
LowpassFilter lp[3];
LowpassFilter hp;
double cutoff;
int delay;
double s;
if (output_ntsc) {
// NTSC YIQ bandwidth: I=1.3MHz Q=0.6MHz
cutoff = (p == 1) ? 1300000 : 600000;
delay = (p == 1) ? 2 : 4;
}
else {
// PAL: R-Y and B-Y are 1.3MHz
cutoff = 1300000;
delay = 2;
}
hp.setFilter((315000000.00 * 4) / (88 * 2),cutoff/2); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 600KHz cutoff
hp.resetFilter(128);
for (unsigned int f=0;f < 3;f++) {
lp[f].setFilter((315000000.00 * 4) / (88 * 2),cutoff); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 600KHz cutoff
lp[f].resetFilter(128);
}
for (x=0;x < (dst->width/2)/*4:2:2*/;x++) {
s = P[x];
s += hp.highpass(s);
for (unsigned int f=0;f < 3;f++) s = lp[f].lowpass(s);
if (x >= delay) P[x-delay] = clampu8(s);
}
}
}
}
}
void composite_video_chroma_lowpass_lite(AVFrame *dst,unsigned int field,unsigned long long fieldno) {
unsigned int x,y;
{ /* lowpass the chroma more. composite video does not allocate as much bandwidth to color as luma. */
for (unsigned int p=1;p <= 2;p++) {
for (y=field;y < dst->height;y += 2) {
unsigned char *P = dst->data[p] + (y * dst->linesize[p]);
LowpassFilter lp[3];
double cutoff;
int delay;
double s;
if (output_ntsc) {
// NTSC YIQ bandwidth: I=1.3MHz Q=0.6MHz
cutoff = (315000000.00 * 4) / (88 * 2 * 4);
delay = 1;
}
else {
// PAL: R-Y and B-Y are 1.3MHz
cutoff = (315000000.00 * 4) / (88 * 2 * 4);
delay = 1;
}
for (unsigned int f=0;f < 3;f++) {
lp[f].setFilter((315000000.00 * 4) / (88 * 2),cutoff); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 600KHz cutoff
lp[f].resetFilter(128);
}
for (x=0;x < (dst->width/2)/*4:2:2*/;x++) {
s = P[x];
for (unsigned int f=0;f < 3;f++) s = lp[f].lowpass(s);
if (x >= delay) P[x-delay] = clampu8(s);
}
}
}
}
}
/* render the chroma into the luma as a fake NTSC color subcarrier */
void composite_video_yuv_to_ntsc(AVFrame *dst,unsigned int field,unsigned long long fieldno,const int subcarrier_amplitude) {
unsigned int x,y;
for (y=field;y < dst->height;y += 2) {
static const int8_t Umult[4] = { 1, 0,-1, 0 };
static const int8_t Vmult[4] = { 0, 1, 0,-1 };
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
unsigned int xc = dst->width;
unsigned int xi;
if (output_ntsc) { // NTSC 2 color frames long
if (video_scanline_phase_shift == 90)
xi = (fieldno + video_scanline_phase_shift_offset + (y >> 1)) & 3;
else if (video_scanline_phase_shift == 180)
xi = (((fieldno + y) & 2) + video_scanline_phase_shift_offset) & 3;
else if (video_scanline_phase_shift == 270)
xi = (fieldno + video_scanline_phase_shift_offset - (y >> 1)) & 3;
else
xi = 0;
}
else/*PAL*/ {
// FIXME: Is this right?
xi = (fieldno + y) & 3;
}
/* remember: this code assumes 4:2:2 */
/* NTS: the subcarrier is two sine waves superimposed on top of each other, 90 degrees apart */
for (x=0;x < xc;x += 2,Y += 2,U++,V++) {
for (unsigned int sx=0;sx < 2;sx++) {
unsigned int sxi = xi+x+sx;
int chroma;
chroma = ((int)U[0] - 128) * subcarrier_amplitude * Umult[sxi&3];
chroma += ((int)V[0] - 128) * subcarrier_amplitude * Vmult[sxi&3];
Y[sx] = clampu8(Y[sx] + (chroma / 50));
}
if (nocolor_subcarrier)
U[0] = V[0] = 128;
}
}
}
/* filter subcarrier back out, use result to emulate NTSC luma-chroma artifacts */
void composite_ntsc_to_yuv(AVFrame *dst,unsigned int field,unsigned long long fieldno,const int subcarrier_amplitude_back) {
unsigned char chroma[dst->width]; // WARNING: This is more GCC-specific C++ than normal
unsigned int x,y;
for (y=field;y < dst->height;y += 2) {
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
unsigned char delay[4] = {16,16,16,16};
unsigned int sum = 16 * (4 - 2);
unsigned char c;
// precharge by 2 pixels to center box blur
delay[2] = Y[0]; sum += delay[2];
delay[3] = Y[1]; sum += delay[3];
for (x=0;x < dst->width;x++) {
c = Y[x+2];
sum -= delay[0];
for (unsigned int j=0;j < (4-1);j++) delay[j] = delay[j+1];
delay[3] = c;
sum += delay[3];
Y[x] = sum / 4;
chroma[x] = clampu8(c + 128 - Y[x]);
if (nocolor_subcarrier_after_yc_sep) {
// debug option to SHOW what we got after filtering
Y[x] = chroma[x];
U[x/2] = V[x/2] = 128;
}
}
if (!nocolor_subcarrier_after_yc_sep) {
unsigned int xi = 0;
if (output_ntsc) { // NTSC 2 color frames long
if (video_scanline_phase_shift == 90)
xi = (fieldno + video_scanline_phase_shift_offset + (y >> 1)) & 3;
else if (video_scanline_phase_shift == 180)
xi = (((fieldno + y) & 2) + video_scanline_phase_shift_offset) & 3;
else if (video_scanline_phase_shift == 270)
xi = (fieldno + video_scanline_phase_shift_offset - (y >> 1)) & 3;
else
xi = 0;
}
else/*PAL*/ {
// FIXME: Is this right?
xi = (fieldno + y) & 3;
}
for (x=((4-xi)&3);x < dst->width;x += 4) { // flip the part of the sine wave that would correspond to negative U and V values
chroma[x+2] = 255 - chroma[x+2];
chroma[x+3] = 255 - chroma[x+3];
}
for (x=0;x < dst->width;x++) {
chroma[x] = clampu8(((((int)chroma[x] - 128) * 50) / subcarrier_amplitude_back) + 128);
}
/* decode the color right back out from the subcarrier we generated */
if (xi & 1) {
for (x=0;x < (dst->width/2);x++) {
U[x] = 255 - chroma[(x*2)+1];
V[x] = 255 - chroma[(x*2)+0];
}
}
else {
for (x=0;x < (dst->width/2);x++) {
U[x] = 255 - chroma[(x*2)+0];
V[x] = 255 - chroma[(x*2)+1];
}
}
}
}
}
static unsigned long long audio_proc_count = 0;
static LowpassFilter audio_post_vhs_boost[2];
void composite_audio_process(int16_t *audio,unsigned int samples) { // number of channels = output_audio_channels, sample rate = output_audio_rate. audio is interleaved.
assert(audio_hilopass.audiostate.size() >= output_audio_channels);
double linear_buzz = dBFS(output_audio_linear_buzz);
double hsync_hz = output_ntsc ? /*NTSC*/15734 : /*PAL*/15625;
int vsync_lines = output_ntsc ? /*NTSC*/525 : /*PAL*/625;
int vpulse_end = output_ntsc ? /*NTSC*/10 : /*PAL*/12;
double hpulse_end = output_ntsc ? /*NTSC*/(hsync_hz * (4.7/*us*/ / 1000000)) : /*PAL*/(hsync_hz * (4.0/*us*/ / 1000000));
for (unsigned int s=0;s < samples;s++,audio += output_audio_channels) {
for (unsigned int c=0;c < output_audio_channels;c++) {
double s;
s = (double)audio[c] / 32768;
/* lowpass filter */
s = audio_hilopass.audiostate[c].filter(s);
/* preemphasis */
if (emulating_preemphasis) {
for (unsigned int i=0;i < output_audio_channels;i++) {
s = s + audio_linear_preemphasis_pre[i].highpass(s);
}
}
/* that faint "buzzing" noise on linear tracks because of audio/video crosstalk */
if (!output_vhs_hifi && linear_buzz > 0.000000001) {
const unsigned int oversample = 16;
for (unsigned int oi=0;oi < oversample;oi++) {
double t = ((((double)audio_proc_count * oversample) + oi) * hsync_hz) / output_audio_rate / oversample;
double hpos = fmod(t,1.0);
int vline = (int)fmod(floor(t + 0.0001/*fudge*/ - hpos),(double)vsync_lines / 2);
bool pulse = false;
if (hpos < hpulse_end)
pulse = true; // HSYNC
if (vline < vpulse_end)
pulse = true; // VSYNC
if (pulse)
s -= linear_buzz / oversample / 2;
}
}
/* analog limiting (when the signal is too loud) */
if (s > 1.0)
s = 1.0;
else if (s < -1.0)
s = -1.0;
/* hiss */
if (output_audio_hiss_level != 0)
s += ((double)(((int)((unsigned int)rand() % ((output_audio_hiss_level * 2) + 1))) - output_audio_hiss_level)) / 20000;
/* some VCRs (at least mine) will boost higher frequencies if playing linear tracks */
if (!output_vhs_hifi && vhs_linear_high_boost > 0)
s += audio_post_vhs_boost[c].highpass(s) * vhs_linear_high_boost;
/* deemphasis */
if (emulating_deemphasis) {
for (unsigned int i=0;i < output_audio_channels;i++) {
s = audio_linear_preemphasis_post[i].lowpass(s);
}
}
audio[c] = clips16(s * 32768);
}
audio_proc_count++;
}
}
void composite_video_process(AVFrame *dst,unsigned int field,unsigned long long fieldno) {
unsigned int x,y;
if (composite_in_chroma_lowpass) composite_video_chroma_lowpass(dst,field,fieldno);
composite_video_yuv_to_ntsc(dst,field,fieldno,subcarrier_amplitude);
/* video composite preemphasis */
if (composite_preemphasis != 0 && composite_preemphasis_cut > 0) {
for (y=field;y < dst->height;y += 2) {
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
LowpassFilter pre;
double s;
pre.setFilter((315000000.00 * 4) / 88,composite_preemphasis_cut); // 315/88 Mhz rate * 4 vs 1.0MHz cutoff
pre.resetFilter(16);
for (x=0;x < dst->width;x++) {
s = Y[x];
s += pre.highpass(s) * composite_preemphasis;
Y[x] = clampu8(s);
}
}
}
/* add video noise */
if (video_noise != 0) {
int noise = 0,noise_mod = (video_noise * 255) / 100;
for (y=field;y < dst->height;y += 2) {
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
for (x=0;x < dst->width;x++) {
Y[x] = clampu8(Y[x] + noise);
noise += ((int)((unsigned int)rand() % ((video_noise*2)+1))) - video_noise;
noise /= 2;
}
}
}
// VHS head switching noise
if (vhs_head_switching) {
unsigned int twidth = dst->width + (dst->width / 10);
unsigned int tx,x,p,x2,shy=0;
double noise = 0;
int shif,ishif,y;
double t;
if (vhs_head_switching_phase_noise != 0) {
unsigned int x = (unsigned int)rand() * (unsigned int)rand() * (unsigned int)rand() * (unsigned int)rand();
x %= 2000000000U;
noise = ((double)x / 1000000000U) - 1.0;
noise *= vhs_head_switching_phase_noise;
}
if (output_ntsc)
t = twidth * 262.5;
else
t = twidth * 312.5;
p = (unsigned int)(fmod(vhs_head_switching_phase + noise,1.0) * t);
x = p % (unsigned int)twidth;
y = ((p / (unsigned int)twidth) * 2) + field;
if (output_ntsc)
y -= (262 - 240) * 2;
else
y -= (312 - 288) * 2;
tx = x;
if (x >= (twidth/2))
ishif = x - twidth;
else
ishif = x;
shif = 0;
while (y < dst->height) {
if (y >= 0) {
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
if (shif != 0) {
char tmp[twidth];
/* WARNING: This is not 100% accurate. On real VHS you'd see the line shifted over and the next line's contents after hsync. */
/* luma. the chroma subcarrier is there, so this is all we have to do. */
x2 = (tx + twidth + (unsigned int)shif) % (unsigned int)twidth;
memset(tmp,16,sizeof(tmp));
memcpy(tmp,Y,dst->width);
for (x=tx;x < dst->width;x++) {
Y[x] = tmp[x2];
if ((++x2) == twidth) x2 = 0;
}
}
}
if (shy == 0)
shif = ishif;
else
shif = (shif * 7) / 8;
tx = 0;
y += 2;
shy++;
}
}
if (!nocolor_subcarrier)
composite_ntsc_to_yuv(dst,field,fieldno,subcarrier_amplitude_back);
/* add video noise */
if (video_chroma_noise != 0) {
int noiseU = 0,noiseV = 0,noise_mod = (video_chroma_noise * 255) / 100;
for (y=field;y < dst->height;y += 2) {
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
for (x=0;x < (dst->width/2);x++) {
U[x] = clampu8(U[x] + noiseU);
V[x] = clampu8(V[x] + noiseV);
noiseU += ((int)((unsigned int)rand() % ((video_chroma_noise*2)+1))) - video_chroma_noise;
noiseU /= 2;
noiseV += ((int)((unsigned int)rand() % ((video_chroma_noise*2)+1))) - video_chroma_noise;
noiseV /= 2;
}
}
}
if (video_chroma_phase_noise != 0) {
int noise = 0,noise_mod = (video_chroma_noise * 255) / 100;
double pi,u,v,u_,v_;
for (y=field;y < dst->height;y += 2) {
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
noise += ((int)((unsigned int)rand() % ((video_chroma_phase_noise*2)+1))) - video_chroma_phase_noise;
noise /= 2;
pi = ((double)noise * M_PI) / 100;
for (x=0;x < (dst->width/2);x++) {
u = (int)U[x] - 128; // think of 'u' as x-coord
v = (int)V[x] - 128; // and 'v' as y-coord
// then this 2D rotation then makes more sense
u_ = (u * cos(pi)) - (u * sin(pi));
v_ = (v * cos(pi)) + (v * sin(pi));
// put it back
U[x] = clampu8(u_ + 128);
V[x] = clampu8(v_ + 128);
}
}
}
// NTS: At this point, the video best resembles what you'd get from a typical DVD player's composite video output.
// Slightly blurry, some color artifacts, and edges will have that "buzz" effect, but still a good picture.
if (emulating_vhs) {
double luma_cut,chroma_cut;
int chroma_delay;
switch (output_vhs_tape_speed) {
case VHS_SP:
luma_cut = 2400000; // 3.0MHz x 80%
chroma_cut = 320000; // 400KHz x 80%
chroma_delay = 4;
break;
case VHS_LP:
luma_cut = 1900000; // ..
chroma_cut = 300000; // 375KHz x 80%
chroma_delay = 5;
break;
case VHS_EP:
luma_cut = 1400000; // ..
chroma_cut = 280000; // 350KHz x 80%
chroma_delay = 6;
break;
default:
abort();
};
// luma lowpass
for (y=field;y < dst->height;y += 2) {
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
LowpassFilter lp[3];
LowpassFilter pre;
double s;
for (unsigned int f=0;f < 3;f++) {
lp[f].setFilter((315000000.00 * 4) / 88,luma_cut); // 315/88 Mhz rate * 4 vs 3.0MHz cutoff
lp[f].resetFilter(16);
}
pre.setFilter((315000000.00 * 4) / 88,luma_cut); // 315/88 Mhz rate * 4 vs 1.0MHz cutoff
pre.resetFilter(16);
for (x=0;x < dst->width;x++) {
s = Y[x];
for (unsigned int f=0;f < 3;f++) s = lp[f].lowpass(s);
s += pre.highpass(s) * 1.6;
Y[x] = clampu8(s);
}
}
// chroma lowpass
for (y=field;y < dst->height;y += 2) {
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
LowpassFilter lpU[3],lpV[3];
double s;
for (unsigned int f=0;f < 3;f++) {
lpU[f].setFilter((315000000.00 * 4) / (88 * 2/*4:2:2*/),chroma_cut); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 400KHz cutoff
lpU[f].resetFilter(128);
lpV[f].setFilter((315000000.00 * 4) / (88 * 2/*4:2:2*/),chroma_cut); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 400KHz cutoff
lpV[f].resetFilter(128);
}
for (x=0;x < (dst->width/2);x++) {
s = U[x];
for (unsigned int f=0;f < 3;f++) s = lpU[f].lowpass(s);
if (x >= chroma_delay) U[x-chroma_delay] = clampu8(s);
s = V[x];
for (unsigned int f=0;f < 3;f++) s = lpV[f].lowpass(s);
if (x >= chroma_delay) V[x-chroma_delay] = clampu8(s);
}
}
// VHS decks also vertically smear the chroma subcarrier using a delay line
// to add the previous line's color subcarrier to the current line's color subcarrier.
// note that phase changes in NTSC are compensated for by the VHS deck to make the
// phase line up per scanline (else summing the previous line's carrier would
// cancel it out).
if (vhs_chroma_vert_blend && output_ntsc) {
unsigned char delayU[dst->width/2];
unsigned char delayV[dst->width/2];
memset(delayU,128,dst->width/2);
memset(delayV,128,dst->width/2);
for (y=(field+2);y < dst->height;y += 2) {
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
unsigned char cU,cV;
for (x=0;x < (dst->width/2);x++) {
cU = U[x];
cV = V[x];
U[x] = (delayU[x]+cU+1)>>1;
V[x] = (delayV[x]+cV+1)>>1;
delayU[x] = cU;
delayV[x] = cV;
}
}
}
// VHS decks tend to sharpen the picture on playback
if (true/*TODO make option*/) {
// luma
for (y=field;y < dst->height;y += 2) {
unsigned char *Y = dst->data[0] + (y * dst->linesize[0]);
LowpassFilter lp[3];
double s,ts;
for (unsigned int f=0;f < 3;f++) {
lp[f].setFilter((315000000.00 * 4) / 88,luma_cut*2); // 315/88 Mhz rate * 4 vs 3.0MHz cutoff
lp[f].resetFilter(16);
}
for (x=0;x < dst->width;x++) {
s = ts = Y[x];
for (unsigned int f=0;f < 3;f++) ts = lp[f].lowpass(ts);
Y[x] = clampu8(s + ((s - ts) * vhs_out_sharpen));
}
}
// chroma
for (y=field;y < dst->height;y += 2) {
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
LowpassFilter lpU[3],lpV[3];
double s,ts;
for (unsigned int f=0;f < 3;f++) {
lpU[f].setFilter((315000000.00 * 4) / (88 * 2/*4:2:2*/),chroma_cut*2); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 400KHz cutoff
lpU[f].resetFilter(128);
lpV[f].setFilter((315000000.00 * 4) / (88 * 2/*4:2:2*/),chroma_cut*2); // 315/88 Mhz rate * 4 (divide by 2 for 4:2:2) vs 400KHz cutoff
lpV[f].resetFilter(128);
}
for (x=0;x < (dst->width/2);x++) {
s = ts = U[x];
for (unsigned int f=0;f < 3;f++) ts = lpU[f].lowpass(ts);
U[x] = clampu8(s + ((s - ts) * vhs_out_sharpen_chroma));
s = ts = V[x];
for (unsigned int f=0;f < 3;f++) ts = lpV[f].lowpass(ts);
V[x] = clampu8(s + ((s - ts) * vhs_out_sharpen_chroma));
}
}
}
if (!vhs_svideo_out) {
composite_video_yuv_to_ntsc(dst,field,fieldno,subcarrier_amplitude);
composite_ntsc_to_yuv(dst,field,fieldno,subcarrier_amplitude);
}
}
if (video_chroma_loss != 0) {
for (y=field;y < dst->height;y += 2) {
unsigned char *U = dst->data[1] + (y * dst->linesize[1]);
unsigned char *V = dst->data[2] + (y * dst->linesize[2]);
if ((((unsigned int)rand())%100000) < video_chroma_loss) {
memset(U,128,dst->width/2);
memset(V,128,dst->width/2);
}
}
}
for (int i=0;i < video_yc_recombine;i++) {
composite_video_yuv_to_ntsc(dst,field,fieldno,subcarrier_amplitude);
composite_ntsc_to_yuv(dst,field,fieldno,subcarrier_amplitude);
}
if (composite_out_chroma_lowpass)
composite_video_chroma_lowpass(dst,field,fieldno);
else if (composite_out_chroma_lowpass_lite)
composite_video_chroma_lowpass_lite(dst,field,fieldno);
}
void black_key(unsigned char *dY,unsigned char *dU,unsigned char *dV,unsigned char *fY,unsigned char *fU,unsigned char *fV,bool wchroma) {
int dLuma = *dY - (16 + black_key_level_feedback);
int dChroma = abs(((int)(*dU)) + ((int)(*dV)) - 256) - black_key_level_feedback;
int d = dLuma + dChroma;
if (d <= 0) {
*dY = *fY;
if (wchroma) {
*dU = *fU;
*dV = *fV;
}
}
*fY = *dY;
if (wchroma) {
*fU = *dU;
*fV = *dV;
}
}
void black_key_feedback(AVFrame *dst,AVFrame *flt,unsigned int field,unsigned long long field_number) {
unsigned char *dY,*dU,*dV;
unsigned char *fY,*fU,*fV;
unsigned int x,y;
// assume 4:2:2
for (y=field;y < dst->height;y += 2) {
dY = dst->data[0] + (y * dst->linesize[0]);
dU = dst->data[1] + (y * dst->linesize[1]);
dV = dst->data[2] + (y * dst->linesize[2]);
fY = flt->data[0] + (y * flt->linesize[0]);
fU = flt->data[1] + (y * flt->linesize[1]);
fV = flt->data[2] + (y * flt->linesize[2]);
for (x=0;x < dst->width;x += 2) {
black_key(dY+0,dU,dV,fY+0,fU,fV,true);
black_key(dY+1,dU,dV,fY+1,fU,fV,false);
dY += 2;
dU++;
dV++;
fY += 2;
fU++;
fV++;
}
}
}