Skip to content

Commit 008e9c2

Browse files
authored
add combining mode
- add a new combining mode -cmbModeR -cmbModeG -cmbModeB with value (0,1) (default = 0) - some works in progress around pipe and audio
1 parent c324d3c commit 008e9c2

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

src/fl2k_file.c

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ double c_gain_r = 1;
9595
double c_gain_g = 1;
9696
double c_gain_b = 1;
9797

98+
//combine mode
99+
int cmb_mode_r = 0;
100+
int cmb_mode_g = 0;
101+
int cmb_mode_b = 0;
102+
98103
//
99104
int read_mode = 0;//0 = multitthreading / 1 = hybrid (R --> GB) / 2 = hybrid (RG --> B) / 3 = sequential (R -> G -> B)
100105

@@ -135,13 +140,16 @@ void usage(void)
135140
"\t[-G filename (use '-' to read from stdin)\n"
136141
"\t[-B filename (use '-' to read from stdin)\n"
137142
"\t[-A audio file (use '-' to read from stdin)\n"
138-
"\t[-syncA chanel used for sync the audio file \ default : G value : (R ,G ,B)\n"
143+
"\t[-syncA chanel used for sync the audio file \ default : G \ value = (R ,G ,B)\n"
139144
"\t[-R2 secondary file to be combined with R (use '-' to read from stdin)\n"
140145
"\t[-G2 secondary file to be combined with G (use '-' to read from stdin)\n"
141146
"\t[-B2 secondary file to be combined with B (use '-' to read from stdin)\n"
142147
"\t[-R16 (convert bits 16 to 8)\n"
143148
"\t[-G16 (convert bits 16 to 8)\n"
144149
"\t[-B16 (convert bits 16 to 8)\n"
150+
"\t[-cmbModeR combine mode \ default : 0 \ value = (0 ,1)\n"
151+
"\t[-cmbModeG combine mode \ default : 0 \ value = (0 ,1)\n"
152+
"\t[-cmbModeB combine mode \ default : 0 \ value = (0 ,1)\n"
145153
"\t[-tbcR interpret R as tbc file\n"
146154
"\t[-tbcG interpret G as tbc file\n"
147155
"\t[-tbcB interpret B as tbc file\n"
@@ -213,6 +221,7 @@ int *read_sample_file(void *inpt_color)
213221
void *buffer = NULL;
214222
FILE *stream = NULL;
215223
FILE *stream2 = NULL;
224+
FILE *streamA = NULL;
216225
int istbc = 0;
217226
char color = (char *) inpt_color;
218227
uint32_t sample_rate = samp_rate;
@@ -221,6 +230,8 @@ int *read_sample_file(void *inpt_color)
221230

222231
int is16 = 0;
223232
int is_stereo = 0;
233+
int combine_mode = 0;
234+
int is_sync_a = 0;
224235

225236
long i = 0;//counter for tmp_buf
226237
long y = 0;//counter for calc
@@ -232,6 +243,7 @@ int *read_sample_file(void *inpt_color)
232243
unsigned int v_end =0;
233244
unsigned long line_lengt = 0;
234245
unsigned long sample_skip = 0;
246+
unsigned int audio_frame = 0;
235247

236248
//COLOR BURST
237249
unsigned int cbust_sample = 0;
@@ -260,7 +272,13 @@ int *read_sample_file(void *inpt_color)
260272
chroma_gain = &c_gain_r;
261273
ire_level = &ire_r;
262274
is_stereo = red2;
275+
combine_mode = cmb_mode_r;
263276
is16 = r16;
277+
if(sync_a == 'R')
278+
{
279+
is_sync_a = 1;
280+
streamA = file_audio;
281+
}
264282
}
265283
else if(color == 'G')
266284
{
@@ -275,7 +293,13 @@ int *read_sample_file(void *inpt_color)
275293
chroma_gain = &c_gain_g;
276294
ire_level = &ire_g;
277295
is_stereo = green2;
296+
combine_mode = cmb_mode_g;
278297
is16 = g16;
298+
if(sync_a == 'G')
299+
{
300+
is_sync_a = 1;
301+
streamA = file_audio;
302+
}
279303
}
280304
else if(color == 'B')
281305
{
@@ -290,7 +314,13 @@ int *read_sample_file(void *inpt_color)
290314
chroma_gain = &c_gain_b;
291315
ire_level = &ire_b;
292316
is_stereo = blue2;
317+
combine_mode = cmb_mode_b;
293318
is16 = b16;
319+
if(sync_a == 'B')
320+
{
321+
is_sync_a = 1;
322+
streamA = file_audio;
323+
}
294324
}
295325

296326
//IRE
@@ -310,7 +340,8 @@ int *read_sample_file(void *inpt_color)
310340
v_end = 1107 * (1 + is16);
311341
cbust_start = 98 * (1 + is16);//not set
312342
cbust_end = 138 * (1 + is16);//not set
313-
sample_skip = 4 * (1 + is16);//remove 4 extra sample in pal
343+
audio_frame = ((88200/25) * (1 + b16));
344+
//sample_skip = 4 * (1 + is16);//remove 4 extra sample in pal
314345
}
315346
else if(sample_rate == 14318181 || sample_rate == 14318170)//NTSC multiplied by 2 if input is 16bit
316347
{
@@ -321,6 +352,7 @@ int *read_sample_file(void *inpt_color)
321352
v_end = 894 * (1 + is16);
322353
cbust_start = 78 * (1 + is16);
323354
cbust_end = 110 * (1 + is16);
355+
audio_frame = ((88200/30) * (1 + b16));
324356
}
325357

326358
unsigned long buf_size = (1310720 + (is16 * 1310720));
@@ -333,6 +365,8 @@ int *read_sample_file(void *inpt_color)
333365
buf_size += sample_skip;
334366

335367
unsigned char *tmp_buf = malloc(1310720);
368+
unsigned char *audio_buf = malloc(audio_frame);
369+
char *audio_buf_signed = (void *)audio_buf;
336370
unsigned char *calc = malloc(buf_size);
337371
unsigned char *calc2 = malloc(buf_size);
338372
unsigned short value16 = 0;
@@ -391,6 +425,16 @@ int *read_sample_file(void *inpt_color)
391425
//skip 1 line
392426
y += line_lengt;
393427
*sample_cnt = 0;
428+
429+
//write audio file to stdout only if its not a terminal
430+
if(isatty(STDOUT_FILENO) == 0 && is_sync_a)
431+
{
432+
//write(stdout, tmp_buf, 1310720);
433+
fread(audio_buf_signed,audio_frame,1,streamA);
434+
//write(stdout, audio_buf_signed, audio_frame);
435+
fwrite(audio_buf, audio_frame,1,stdout);
436+
fflush(stdout);
437+
}
394438
}
395439

396440
if(is16 == 1)
@@ -417,7 +461,15 @@ int *read_sample_file(void *inpt_color)
417461
{
418462
if(is_stereo)
419463
{
420-
tmp_buf[i] = round((*value16_signed + *value16_2_signed)/ 256.0) + 128;//convert to 8 bit
464+
if(combine_mode == 0)//default
465+
{
466+
tmp_buf[i] = round((*value16_signed + *value16_2_signed)/ 256.0) + 128;//convert to 8 bit
467+
}
468+
else//mode 1
469+
{
470+
tmp_buf[i] = round(((*value16_signed + *value16_2_signed)/2)/ 256.0);//convert to 8 bit
471+
}
472+
421473
}
422474
else
423475
{
@@ -426,7 +478,14 @@ int *read_sample_file(void *inpt_color)
426478
}
427479
else if(is_stereo)//combine 2 file
428480
{
429-
tmp_buf[i] = *value8_signed + *value8_2_signed + 128;
481+
if(combine_mode == 0)//default
482+
{
483+
tmp_buf[i] = *value8_signed + *value8_2_signed + 128;
484+
}
485+
else//mode 1
486+
{
487+
tmp_buf[i] = round((*value8_signed + *value8_2_signed)/2);
488+
}
430489
}
431490
else//no processing
432491
{
@@ -491,15 +550,8 @@ int *read_sample_file(void *inpt_color)
491550

492551
memcpy(buffer, tmp_buf, 1310720);
493552

494-
//write to stdout only if its not a terminal
495-
if(isatty(STDOUT_FILENO) == 0)
496-
{
497-
//write(stdout, tmp_buf, 1310720);
498-
fwrite(tmp_buf, 1310720,1,stdout);
499-
fflush(stdout);
500-
}
501-
502553
free(tmp_buf);
554+
free(audio_buf);
503555
free(calc);
504556
free(calc2);
505557

@@ -728,6 +780,9 @@ int main(int argc, char **argv)
728780
{"G2", 1, 0, 18},
729781
{"B2", 1, 0, 19},
730782
{"syncA", 1, 0, 20},
783+
{"cmbModeR", 1, 0, 21},
784+
{"cmbModeG", 1, 0, 22},
785+
{"cmbModeB", 1, 0, 23},
731786
{0, 0, 0, 0}
732787
};
733788

@@ -838,6 +893,15 @@ int main(int argc, char **argv)
838893
else if(optarg == 'b'){sync_a = 'B';}
839894
else{sync_a = optarg;}
840895
break;
896+
case 21:
897+
cmb_mode_r = atoi(optarg);
898+
break;
899+
case 22:
900+
cmb_mode_g = atoi(optarg);
901+
break;
902+
case 23:
903+
cmb_mode_b = atoi(optarg);
904+
break;
841905
default:
842906
usage();
843907
break;
@@ -878,6 +942,12 @@ int main(int argc, char **argv)
878942
else if(sync_a == 'B'){start_audio = start_b;}
879943
else {start_audio = start_g; sync_a = 'G';}//default value
880944

945+
if((cmb_mode_r < 0 || cmb_mode_r > 1) || (cmb_mode_g < 0 ||cmb_mode_g > 1) || (cmb_mode_b < 0 || cmb_mode_b > 1))
946+
{
947+
fprintf(stderr, "\nCombine mode invalid / value : (0 ,1)\n\n");
948+
usage();
949+
}
950+
881951
if((c_gain_r < 0 || c_gain_r > 6) || (c_gain_g < 0 || c_gain_g > 6) || (c_gain_b < 0 || c_gain_b > 6))
882952
{
883953
fprintf(stderr, "\nOne chroma gain is invalid / range : (0.0 ,4.0)\n\n");

0 commit comments

Comments
 (0)