forked from osmocom/osmo-fl2k
/
fl2k_file.c
2112 lines (1899 loc) · 49.3 KB
/
fl2k_file.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
/*
* osmo-fl2k, turns FL2000-based USB 3.0 to VGA adapters into
* low cost DACs
*
* Copyright (C) 2016-2018 by Steve Markgraf <steve@steve-m.de>
*
* SPDX-License-Identifier: GPL-2.0+
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <getopt.h>
#include <unistd.h>
#ifndef _WIN32
#include <unistd.h>
#define sleep_ms(ms) usleep(ms*1000)
#else
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#define sleep_ms(ms) Sleep(ms)
#endif
#define _FILE_OFFSET_BITS 64
#ifdef _WIN64
#define FSEEK fseeko64
#else
#define FSEEK fseeko
#endif
#include "osmo-fl2k.h"
static fl2k_dev_t *dev = NULL;
soxr_t resampler_r = NULL;
soxr_t resampler_g = NULL;
soxr_t resampler_b = NULL;
static volatile int do_exit = 0;
static volatile int repeat = 1;
uint32_t input_sample_rate = 100000000;
uint32_t output_sample_rate = 100000000;
int resample = 0;
//buff size
uint32_t input_buf_size = FL2K_BUF_LEN;
//input file
FILE *file_r;
FILE *file_g;
FILE *file_b;
FILE *file2_r;
FILE *file2_g;
FILE *file2_b;
FILE *file_audio;
//input buffer
char *inbuf_r = NULL;
char *inbuf_g = NULL;
char *inbuf_b = NULL;
//resample buffer
short *resbuf_r = NULL;
short *resbuf_g = NULL;
short *resbuf_b = NULL;
//output buffer
char *outbuf_r = NULL;
char *outbuf_g = NULL;
char *outbuf_b = NULL;
//chanel activation
int red = 0;
int green = 0;
int blue = 0;
int red2 = 0;
int green2 = 0;
int blue2 = 0;
int audio = 0;
char sync_a = 'G';
//enable 16 bit to 8 bit conversion
int r16 = 0;
int g16 = 0;
int b16 = 0;
//signed = 1/ unsigned = 0
int r_sign = 0;
int g_sign = 0;
int b_sign = 0;
//if it's a tbc
int tbcR = 0;
int tbcG = 0;
int tbcB = 0;
//ire levels change
double ire_r = 0;
double ire_g = 0;
double ire_b = 0;
//chroma gain
double c_gain_r = 1;
double c_gain_g = 1;
double c_gain_b = 1;
//signal gain (dynamic range)
double signal_gain_r = 1;
double signal_gain_g = 1;
double signal_gain_b = 1;
//Voltage control
double v_max_r = -1;
double v_max_g = -1;
double v_max_b = -1;
int max_value_r = 255;
int max_value_g = 255;
int max_value_b = 255;
int min_value_r = 0;
int min_value_g = 0;
int min_value_b = 0;
//combine mode
int cmb_mode_r = 0;
int cmb_mode_g = 0;
int cmb_mode_b = 0;
//read mode
int read_mode = 0;//0 = multitthreading / 1 = hybrid (R --> GB) / 2 = hybrid (RG --> B) / 3 = sequential (R -> G -> B)
//pipe mode
char pipe_mode = 'A';
int sample_type_r = 1;// 1 == signed 0 == unsigned
int sample_type_g = 1;// 1 == signed 0 == unsigned
int sample_type_b = 1;// 1 == signed 0 == unsigned
char video_standard = 0;
uint32_t sample_cnt_r = 0;//used for tbc processing
uint32_t sample_cnt_g = 0;//used for tbc processing
uint32_t sample_cnt_b = 0;//used for tbc processing
uint32_t line_cnt_r = 0;//used for tbc processing
uint32_t line_cnt_g = 0;//used for tbc processing
uint32_t line_cnt_b = 0;//used for tbc processing
uint32_t line_sample_cnt_r = 0;//used for tbc processing
uint32_t line_sample_cnt_g = 0;//used for tbc processing
uint32_t line_sample_cnt_b = 0;//used for tbc processing
uint32_t field_cnt_r = 0;//used for tbc processing
uint32_t field_cnt_g = 0;//used for tbc processing
uint32_t field_cnt_b = 0;//used for tbc processing
//unsigned char *pipe_buf = NULL;
//thread for processing
pthread_t thread_r;
pthread_t thread_g;
pthread_t thread_b;
//thread for resampling
pthread_t thread_r_res;
pthread_t thread_g_res;
pthread_t thread_b_res;
typedef struct soxr_resample_data {//used with soxr and pthread
soxr_t soxr;
fl2k_data_info_t *data_info;
int *state_resample;
int *state_process;
char color;
} resample_data;
resample_data soxr_data_r;
resample_data soxr_data_g;
resample_data soxr_data_b;
//process synchronisation
//0 = initialisation | 1 = ready | 2 = processing | 3 = finished
int resample_r_state = 0;
int resample_g_state = 0;
int resample_b_state = 0;
int process_r_state = 0;
int process_g_state = 0;
int process_b_state = 0;
//pointer to variable
int *resample_r_state_ptr = &resample_r_state;
int *resample_g_state_ptr = &resample_g_state;
int *resample_b_state_ptr = &resample_b_state;
int *process_r_state_ptr = &process_r_state;
int *process_g_state_ptr = &process_g_state;
int *process_b_state_ptr = &process_b_state;
void usage(void)
{
fprintf(stderr,
"fl2k_file2, a sample player for FL2K VGA dongles\n\n"
"Usage:\n"
"\t[-d device_index (default: 0)]\n"
"\t[-s samplerate (default: 100 MS/s) you can write(ntsc) or (pal)]\n"
"\t[-u Set the output sample type of the fl2K to unsigned]\n"
"\t[-R filename (use '-' to read from stdin)\n"
"\t[-G filename (use '-' to read from stdin)\n"
"\t[-B filename (use '-' to read from stdin)\n"
"\t[-A audio file (use '-' to read from stdin)\n"
"\t[-syncA chanel used for sync the audio file \ default : G \ value = (R ,G ,B)\n"
"\t[-R2 secondary file to be combined with R (use '-' to read from stdin)\n"
"\t[-G2 secondary file to be combined with G (use '-' to read from stdin)\n"
"\t[-B2 secondary file to be combined with B (use '-' to read from stdin)\n"
"\t[-R16 (convert bits 16 to 8)\n"
"\t[-G16 (convert bits 16 to 8)\n"
"\t[-B16 (convert bits 16 to 8)\n"
"\t[-R8 interpret R input as 8 bit\n"
"\t[-G8 interpret G input as 8 bit\n"
"\t[-B8 interpret B input as 8 bit\n"
"\t[-resample active output resampling\n"
"\t[-signR interpret R input as (1 = signed / 0 = unsigned) or (s = signed / u = unsigned)\n"
"\t[-signG interpret G input as (1 = signed / 0 = unsigned) or (s = signed / u = unsigned)\n"
"\t[-signB interpret B input as (1 = signed / 0 = unsigned) or (s = signed / u = unsigned)\n"
"\t[-cmbModeR combine mode \ default : 0 \ value = (0 ,1 ,2)\n"
"\t[-cmbModeG combine mode \ default : 0 \ value = (0 ,1 ,2)\n"
"\t[-cmbModeB combine mode \ default : 0 \ value = (0 ,1 ,2)\n"
"\t[-tbcR interpret R as tbc file\n"
"\t[-tbcG interpret G as tbc file\n"
"\t[-tbcB interpret B as tbc file\n"
"\t[-not_tbcR disable tbc processing for input R file\n"
"\t[-not_tbcG disable tbc processing for input G file\n"
"\t[-not_tbcB disable tbc processing for input B file\n"
"\t[-CgainR chroma gain for input R (0.0 to 6.0) (using color burst)\n"
"\t[-CgainG chroma gain for input G (0.0 to 6.0) (using color burst)\n"
"\t[-CgainB chroma gain for input B (0.0 to 6.0) (using color burst)\n"
"\t[-SgainR signal gain for output R (0.5 to 2.0) (clipping white)\n"
"\t[-SgainG signal gain for output G (0.5 to 2.0) (clipping white)\n"
"\t[-SgainB signal gain for output B (0.5 to 2.0) (clipping white)\n"
"\t[-VmaxR maximum output voltage for channel R (0.003 to 0.7) (scale value) (disable Cgain and Sgain)\n"
"\t[-VmaxG maximum output voltage for channel G (0.003 to 0.7) (scale value) (disable Cgain and Sgain)\n"
"\t[-VmaxB maximum output voltage for channel B (0.003 to 0.7) (scale value) (disable Cgain and Sgain)\n"
"\t[-MaxValueR max value for channel R (1 to 255) (reference level) (used for Vmax)\n"
"\t[-MaxValueG max value for channel G (1 to 255) (reference level) (used for Vmax)\n"
"\t[-MaxValueB max value for channel B (1 to 255) (reference level) (used for Vmax)\n"
//"\t[-MinValueR min value for channel R (0 to 254) (reference level) (used for Vmax)\n"
//"\t[-MinValueG min value for channel G (0 to 254) (reference level) (used for Vmax)\n"
//"\t[-MinValueB min value for channel B (0 to 254) (reference level) (used for Vmax)\n"
"\t[-ireR IRE level for input R (-50.0 to +50.0)\n"
"\t[-ireG IRE level for input G (-50.0 to +50.0)\n"
"\t[-ireB IRE level for input B (-50.0 to +50.0)\n"
"\t[-FstartR seek to frame for input R\n"
"\t[-FstartG seek to frame for input G\n"
"\t[-FstartB seek to frame for input B\n"
"\t[-audioOffset offset audio from a duration of x frame\n"
"\t[-pipeMode (default = A) option : A = Audio file / R = output of R / G = output of G / B = output of B\n"
"\t[-readMode (default = 0) option : 0 = multit-threading (RGB) / 1 = hybrid (R --> GB) / 2 = hybrid (RG --> B) / 3 = sequential (R -> G -> B)\n"
"\n-info-version------------------------------------------------------\n\n"
"runtime=%s API="SOXR_THIS_VERSION_STR"\n",
soxr_version());
exit(1);
}
const char *get_filename_ext(const char *filename) {
const char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
#ifdef _WIN32
BOOL WINAPI
sighandler(int signum)
{
if (CTRL_C_EVENT == signum) {
fprintf(stderr, "Signal caught, exiting!\n");
fl2k_stop_tx(dev);
do_exit = 1;
return TRUE;
}
return FALSE;
}
#else
static void sighandler(int signum)
{
fprintf(stderr, "Signal caught, exiting!\n");
fl2k_stop_tx(dev);
do_exit = 1;
}
#endif
static size_t soxr_input_fn(void * ibuf, soxr_cbuf_t * buf, size_t len)
{
*buf = ibuf;
//memcpy(ibuf,copyinbuf);
return len+1;//+1 to avoid looping
}
void resampler_close(soxr_t soxr)
{
soxr_delete(soxr);
}
void resampler_open(fl2k_data_info_t *data_info, soxr_t *soxr, uint32_t orate0, char color)
{
double irate = 0;
void * ibuf = NULL;
size_t ilen = 0;
if(color == 'R')
{
irate = (float)data_info->r_rate;
ibuf = data_info->r_buf_res;
ilen = data_info->r_buf_len;
}
if(color == 'G')
{
irate = (float)data_info->g_rate;
ibuf = data_info->g_buf_res;
ilen = data_info->g_buf_len;
}
if(color == 'B')
{
irate = (float)data_info->b_rate;
ibuf = data_info->b_buf_res;
ilen = data_info->b_buf_len;
}
unsigned int i = 0;
char const * const arg0 = "", * engine = "";
double const orate = (float)orate0;
unsigned const chans = (unsigned)1;//nb channel
soxr_datatype_t const itype = (soxr_datatype_t)3;
unsigned const ospec = (soxr_datatype_t)11;
unsigned long const q_recipe= 0;//SOXR_16_BITQ;//between (0-3) SOXR_16_BITQ = 3
unsigned long const q_flags = 0;
double const passband_end = 0;
double const stopband_begin = 0;
double const phase_response = -1;
int const use_threads = 2;
soxr_datatype_t const otype = ospec & 3;
soxr_quality_spec_t q_spec = soxr_quality_spec(q_recipe, q_flags);
soxr_io_spec_t io_spec = soxr_io_spec(itype, otype);
soxr_runtime_spec_t const runtime_spec = soxr_runtime_spec(!use_threads);
// Allocate resampling input and output buffers in proportion to the input
// and output rates:
size_t const osize = soxr_datatype_size(otype) * chans;
size_t const isize = soxr_datatype_size(itype) * chans;
size_t const olen = FL2K_BUF_LEN;
size_t odone, clips = 0;
soxr_error_t error;
// Overrides (if given):
if (passband_end > 0) q_spec.passband_end = passband_end / 100;
if (stopband_begin > 0) q_spec.stopband_begin = stopband_begin / 100;
if (phase_response >=0) q_spec.phase_response = phase_response;
io_spec.flags = ospec & ~7u;
// Create a stream resampler:
*soxr = soxr_create(
irate, orate, chans, // Input rate, output rate, # of channels.
&error, // To report any error during creation.
&io_spec, &q_spec, &runtime_spec);
if (!error) // Register input_fn with the resampler:
{
//set input buffer
error = soxr_set_input_fn(*soxr, (soxr_input_fn_t)soxr_input_fn, ibuf, ilen);
}
fprintf(stderr,"engine inside = %s\n",soxr_engine(*soxr));
}
void fl2k_resample_to_freq(resample_data *soxr_data)
{
soxr_t soxr = soxr_data->soxr;
fl2k_data_info_t *data_info = soxr_data->data_info;
char color = soxr_data->color;
int resampled = 0;
char *buf_out;
short *buf_res;
int *process_state = soxr_data->state_process;
int *resample_state = soxr_data->state_resample;
if(color == 'R')
{
resampled = data_info->r_sample_resampled;
buf_out = data_info->r_buf;
buf_res = data_info->r_buf_res;
}
if(color == 'G')
{
resampled = data_info->g_sample_resampled;
buf_out = data_info->g_buf;
buf_res = data_info->g_buf_res;
}
if(color == 'B')
{
resampled = data_info->b_sample_resampled;
buf_out = data_info->b_buf;
buf_res = data_info->b_buf_res;
}
unsigned int i = 0;
size_t const olen = FL2K_BUF_LEN;
void * const obuf = malloc(2 * olen);
short *obuf16 = (void *)obuf;
if(*resample_state != 0)//if not first call
{
//set status to ready
*resample_state = 1;
//wait fl2K_callback to be ready
while(*process_state != 1){usleep(1);}
//set status to processing
//*resample_state = 2;
//process data
soxr_output(soxr, obuf, olen);
//resize to 8bit and clip value
//fprintf(stderr,"r\n");
i = 0;
while(i < FL2K_BUF_LEN)
{
if(obuf16[i] > 255)
{
buf_out[i] = 255;
}
else if(obuf16[i] < 0)
{
buf_out[i] = 0;
}
else
{
buf_out[i] = obuf16[i];
}
i++;
}
}
else//initialisation
{
//set status to ready
*resample_state = 1;
//wait fl2K_callback to be ready
while(*process_state != 1){usleep(1);}
//set status to processing
//*resample_state = 2;
//set empty buffer while first data are processed
i = 0;
while(i < FL2K_BUF_LEN)
{
buf_out[i] = 0;
i++;
}
}
//set status to finished
*resample_state = 3;
//wait fl2K_callback to finish
while(*process_state != 3){usleep(1);}
free(obuf);
}
//compute number of sample to skip
unsigned long calc_nb_skip(long sample_cnt,int linelength,long frame_lengt,long bufsize,char standard)
{
int nb_skip = 0;
//on enlève ce qui reste avant le skip
bufsize -= (frame_lengt - sample_cnt);
nb_skip = 1;
while(bufsize > 0)
{
bufsize -= frame_lengt;
//if we can do a complet skip
if((bufsize - linelength) > 0)
{
nb_skip ++;
}
//if we stop in the middle of a skip
else if(((bufsize - linelength) < 0) && bufsize > 0)
{
nb_skip ++;
}
}
if(standard == 'P')
{
linelength -= 8;//remove the 4 extra sample from the last line
}
return (nb_skip * linelength);//multiply for giving the number of byte to skip
}
int read_sample_file(void *inpt_color)
{
//parametter
char *buffer = NULL;
short *resbuffer = malloc(input_buf_size*2);//used for cast 8 bit to 16 bit
FILE *stream = NULL;
FILE *stream2 = NULL;
FILE *streamA = NULL;
int istbc = 0;
char color = (char *) inpt_color;
//uint32_t sample_rate = input_sample_rate;
double *chroma_gain = NULL;
double *ire_level = NULL;
double signal_gain = 1;
double v_max = -1;
int max_value = 255;
int is16 = 0;
int is_signed = 0;
int is_stereo = 0;
int combine_mode = 0;
int is_sync_a = 0;
int use_pipe = 0;
long i = 0;//counter for tmp_buf
long y = 0;//counter for calc
//(NTSC line = 910 frame = 477750) (PAL line = 1135 frame = 709375)
unsigned long frame_lengt = 0;
unsigned long frame_nb_line = 0;
unsigned int v_start =0;
unsigned int v_end =0;
unsigned long line_lengt = 0;
unsigned long sample_skip = 0;
unsigned int audio_frame = 0;
//COLOR BURST
unsigned int cbust_sample = 0;
unsigned int cbust_middle = 0;
double cbust_count = 0;
double cbust_offset = 0;
unsigned int cbust_start = 0;
unsigned int cbust_end = 0;
//count
uint32_t *sample_cnt = NULL;
uint32_t *line_cnt = NULL;
uint32_t *line_sample_cnt = NULL;
uint32_t *field_cnt = NULL;
int *process_state = NULL;
int *resample_state = NULL;
if(color == 'R')
{
process_state = process_r_state_ptr;
resample_state = resample_r_state_ptr;
buffer = inbuf_r;
stream = file_r;
stream2 = file2_r;
istbc = tbcR;
sample_cnt = &sample_cnt_r;
line_cnt = &line_cnt_r;
line_sample_cnt = &line_sample_cnt_r;
field_cnt = & field_cnt_r;
chroma_gain = &c_gain_r;
ire_level = &ire_r;
is_stereo = red2;
combine_mode = cmb_mode_r;
is16 = r16;
is_signed = r_sign;
signal_gain = signal_gain_r;
v_max = v_max_r;
max_value = max_value_r;
if(sync_a == 'R' && pipe_mode == 'A')
{
is_sync_a = 1;
streamA = file_audio;
}
else if(pipe_mode == 'R')
{
use_pipe = 1;
}
}
else if(color == 'G')
{
process_state = process_g_state_ptr;
resample_state = resample_g_state_ptr;
buffer = inbuf_g;
stream = file_g;
stream2 = file2_g;
istbc = tbcG;
sample_cnt = &sample_cnt_g;
line_cnt = &line_cnt_g;
line_sample_cnt = &line_sample_cnt_g;
field_cnt = & field_cnt_g;
chroma_gain = &c_gain_g;
ire_level = &ire_g;
is_stereo = green2;
combine_mode = cmb_mode_g;
is16 = g16;
is_signed = g_sign;
signal_gain = signal_gain_g;
v_max = v_max_g;
max_value = max_value_g;
if(sync_a == 'G' && pipe_mode == 'A')
{
is_sync_a = 1;
streamA = file_audio;
}
else if(pipe_mode == 'G')
{
use_pipe = 1;
}
}
else if(color == 'B')
{
process_state = process_b_state_ptr;
resample_state = resample_b_state_ptr;
buffer = inbuf_b;
stream = file_b;
stream2 = file2_b;
istbc = tbcB;
sample_cnt = &sample_cnt_b;
line_cnt = &line_cnt_b;
line_sample_cnt = &line_sample_cnt_b;
field_cnt = & field_cnt_b;
chroma_gain = &c_gain_b;
ire_level = &ire_b;
is_stereo = blue2;
combine_mode = cmb_mode_b;
is16 = b16;
is_signed = b_sign;
signal_gain = signal_gain_b;
v_max = v_max_b;
max_value = max_value_b;
if(sync_a == 'B' && pipe_mode == 'A')
{
is_sync_a = 1;
streamA = file_audio;
}
else if(pipe_mode == 'B')
{
use_pipe = 1;
}
}
//IRE
const float ire_conv = 1.59375;// (255/160)
const float ire_min = 63.75;//40 * (255/160)
const float ire_new_max = 159.375;// (140 * (255/160)) - (40 * (255/160))
const float ire_add = (*ire_level * ire_conv);
const float ire_gain = (ire_new_max / (ire_new_max + ire_add));
double ire_tmp = 0;
if(video_standard == 'P')//PAL value multiplied by 2 if input is 16bit
{
frame_lengt = 709379 * (1 + is16);//709375 + 4 extra sample
line_lengt = 1135 * (1 + is16);
frame_nb_line = 625;
v_start = 185 * (1 + is16);
v_end = 1107 * (1 + is16);
cbust_start = 98 * (1 + is16);//not set
cbust_end = 138 * (1 + is16);//not set
audio_frame = ((88200/25) *2);
//sample_skip = 4 * (1 + is16);//remove 4 extra sample in pal
}
else if(video_standard == 'N')//NTSC value multiplied by 2 if input is 16bit
{
frame_lengt = 477750 * (1 + is16);
line_lengt = 910 * (1 + is16);
frame_nb_line = 525;
v_start = 134 * (1 + is16);
v_end = 894 * (1 + is16);
cbust_start = 78 * (1 + is16);
cbust_end = 110 * (1 + is16);
audio_frame = ((88200/30) * 2);
}
unsigned long buf_size = (input_buf_size + (is16 * input_buf_size));
if(istbc == 1)//compute buf size
{
sample_skip += calc_nb_skip(*sample_cnt,line_lengt,frame_lengt,buf_size,video_standard);
}
buf_size += sample_skip;
unsigned char *tmp_buf = malloc(input_buf_size);//8bit data so we can use input_buf_size
unsigned char *audio_buf = malloc(audio_frame);
char *audio_buf_signed = (void *)audio_buf;
unsigned char *calc = malloc(buf_size);
unsigned char *calc2 = malloc(buf_size);
unsigned short value16 = 0;
unsigned short value16_2 = 0;
unsigned char value8 = 0;
unsigned char value8_2 = 0;
short *value16_signed = (void *)&value16;
short *value16_2_signed = (void *)&value16_2;
char *value8_signed = (void *)&value8;
char *value8_2_signed = (void *)&value8_2;
//set status to ready
*process_state = 1;
//wait resampler
if(resample)while(*resample_state != 1){usleep(1);}
//set status to processing
//*process_state = 2;
if (tmp_buf == NULL || calc == NULL)
{
free(tmp_buf); // Free both in case only one was allocated
free(calc);
fprintf(stderr, "(%c) malloc error (tmp_buf , calc)\n",color);
return -1;
}
if(istbc == 1)
{
sample_skip = calc_nb_skip(*sample_cnt,line_lengt,frame_lengt,buf_size,video_standard);
}
if(is_stereo)
{
if(fread(calc,buf_size,1,stream) != 1 || fread(calc2,buf_size,1,stream2) != 1)
{
free(tmp_buf); // Free both in case only one was allocated
free(calc);
free(calc2);
fprintf(stderr, "(%c) fread error %d : ",color,errno);
perror(NULL);
return -1;
}
}
else
{
if(fread(calc,buf_size,1,stream) != 1)
{
free(tmp_buf); // Free both in case only one was allocated
free(calc);
free(calc2);
fprintf(stderr, "(%c) fread error %d : ",color,errno);
perror(NULL);
return -1;
}
}
while((y < buf_size) && !do_exit)
{
//if we are at then end of the frame skip one line
if(*sample_cnt >= frame_lengt)
{
if(istbc == 1)
{
if(video_standard == 'P')
{
//skip 1 line - 4 sample
y += (line_lengt - 8);
}
else
{
//skip 1 line
y += line_lengt;
}
*sample_cnt = 0;
}
//write audio file to stdout only if its not a terminal
if(isatty(STDOUT_FILENO) == 0 && is_sync_a)
{
//write(stdout, tmp_buf, input_buf_size);
fread(audio_buf_signed,audio_frame,1,streamA);
//write(stdout, audio_buf_signed, audio_frame);
fwrite(audio_buf, audio_frame,1,stdout);
fflush(stdout);
}
}
if(is16 == 1)
{
value16 = ((calc[y+1] * 256) + calc[y]);
if(is_stereo)
{
value16_2 = ((calc2[y+1] * 256) + calc2[y]);
}
y += 2;
}
else
{
value8 = calc[y];
if(is_stereo)
{
value8_2 = calc2[y];
}
y += 1;
}
//convert 16bit to 8bit and combine
if(is16 == 1)
{
if(is_stereo)
{
if(combine_mode == 0)//default
{
if((round((*value16_signed + *value16_2_signed)/ 256.0) + 128) < -128)
{
tmp_buf[i] = -128;
}
/*else if((round((*value16_signed + *value16_2_signed)/ 256.0) + 128) > 0)
{
tmp_buf[i] = 0;
}*/
else
{
tmp_buf[i] = round((*value16_signed + *value16_2_signed)/ 256.0) + 128;//convert to 8 bit
}
}
else if(combine_mode == 2)
{
if(((*line_sample_cnt >= v_start) && (*line_sample_cnt <= v_end))&& *line_cnt > (22 + ((unsigned long)*field_cnt % 2)) )
{
tmp_buf[i] = round(round((value16) / 256.0) / 1.34) + 64;//convert to 8 bit
}
else
{
tmp_buf[i] = round(value16_2 / 256.0);//convert to 8 bit
}
}
else//mode 1
{
tmp_buf[i] = round(((value16 + value16_2)/2)/ 256.0);//convert to 8 bit
}
}
else
{
tmp_buf[i] = round(value16 / 256.0);//convert to 8 bit
}
}
else if(is_stereo)//combine 2 file
{
if(combine_mode == 0)//default
{
tmp_buf[i] = *value8_signed + *value8_2_signed + 128;
}
else//mode 1
{
tmp_buf[i] = round((value8 + value8_2)/2);
}
}
else//no processing
{
tmp_buf[i] = value8;
}
if(*chroma_gain != 1)
{
//color burst reading
if(((*line_sample_cnt >= cbust_start) && (*line_sample_cnt <= cbust_end)) && (*line_cnt == (21 + ((unsigned long)*field_cnt % 2))))
{
cbust_sample = tmp_buf[i];
cbust_count += 1;
cbust_middle = cbust_sample / cbust_count;
cbust_offset = (cbust_middle - (cbust_middle / *chroma_gain));
}
//chroma gain
if(((*line_sample_cnt >= cbust_start) && (*line_sample_cnt <= cbust_end * 1 + is16))&& (*line_cnt > (22 + ((unsigned long)*field_cnt % 2))))
{
tmp_buf[i] = round(tmp_buf[i] / *chroma_gain);// + cbust_offset;
}
}
//ire 7.5 to ire 0
if (*ire_level != 0)
{
if(((*line_sample_cnt >= v_start) && (*line_sample_cnt <= v_end))&& *line_cnt > (22 + ((unsigned long)*field_cnt % 2)) )
{
ire_tmp = (tmp_buf[i] - ire_min);
if(ire_tmp < 0)//clipping value
{
ire_tmp = 0;
}
ire_tmp = ire_tmp * ire_gain;
tmp_buf[i] = round(ire_tmp + ire_add + ire_min);
}
}
//signal gain
if(signal_gain != 1)
{
if(tmp_buf[i] > 5)
{
if((tmp_buf[i] * signal_gain) > 255)
{
tmp_buf[i] = 255;
}
else
{
tmp_buf[i] = round(tmp_buf[i] * signal_gain);
}
}
}
//scale to max voltage
if(v_max > 0.0)
{
if(round(tmp_buf[i]*(255/max_value)) > 255)
{
tmp_buf[i] = 255;
}
else
{
tmp_buf[i] = round((tmp_buf[i]*(255/max_value))/(0.7/v_max));
}
}
//fix sign and cast to 16 bit
if(!is_signed)
{
tmp_buf[i] = tmp_buf[i] - 128;
resbuffer[i] = tmp_buf[i];
}
else
{
resbuffer[i] = tmp_buf[i];
}
i += 1;//on avance tmp_buff de 1
if(*line_cnt == ((frame_nb_line / 2) + ((unsigned long)*field_cnt % 2)))//field 1 = (max - 0.5) field 2 = (max + 0.5)
{
*line_cnt = 0;
*field_cnt += 1;
cbust_sample = 0;
cbust_middle = 0;
cbust_count = 0;
}
if(*line_sample_cnt == line_lengt)
{
*line_sample_cnt = 0;
*line_cnt += 1;
}
*sample_cnt += (1 + is16);
*line_sample_cnt += (1 + is16);
}
//set status to finished
*process_state = 3;
//wait resampler to finish
if(resample)while(*resample_state != 3){usleep(1);}
if(resample)
{
if(color == 'R')
{
memcpy(resbuf_r, resbuffer, input_buf_size*2);
}
if(color == 'G')
{
memcpy(resbuf_g, resbuffer, input_buf_size*2);
}
if(color == 'B')
{
memcpy(resbuf_b, resbuffer, input_buf_size*2);