-
Notifications
You must be signed in to change notification settings - Fork 2
/
binarizer.cpp
8055 lines (7766 loc) · 309 KB
/
binarizer.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
#include "binarizer.h"
bin_preset_t::bin_preset_t()
{
reset();
}
bin_preset_t::bin_preset_t(const bin_preset_t &in_object)
{
max_black_lvl = in_object.max_black_lvl;
min_white_lvl = in_object.min_white_lvl;
min_contrast = in_object.min_contrast;
min_ref_lvl = in_object.min_ref_lvl;
max_ref_lvl = in_object.max_ref_lvl;
min_valid_crcs = in_object.min_valid_crcs;
mark_max_dist = in_object.mark_max_dist;
left_bit_pick = in_object.left_bit_pick;
right_bit_pick = in_object.right_bit_pick;
horiz_coords = in_object.horiz_coords;
en_force_coords = in_object.en_force_coords;
en_coord_search = in_object.en_coord_search;
en_first_line_dup = in_object.en_first_line_dup;
en_good_no_marker = in_object.en_good_no_marker;
}
bin_preset_t& bin_preset_t::operator= (const bin_preset_t &in_object)
{
if(this==&in_object) return *this;
max_black_lvl = in_object.max_black_lvl;
min_white_lvl = in_object.min_white_lvl;
min_contrast = in_object.min_contrast;
min_ref_lvl = in_object.min_ref_lvl;
max_ref_lvl = in_object.max_ref_lvl;
min_valid_crcs = in_object.min_valid_crcs;
mark_max_dist = in_object.mark_max_dist;
left_bit_pick = in_object.left_bit_pick;
right_bit_pick = in_object.right_bit_pick;
horiz_coords = in_object.horiz_coords;
en_force_coords = in_object.en_force_coords;
en_coord_search = in_object.en_coord_search;
en_first_line_dup = in_object.en_first_line_dup;
en_good_no_marker = in_object.en_good_no_marker;
return *this;
}
void bin_preset_t::reset()
{
max_black_lvl = 160;
min_white_lvl = 28;
min_contrast = 10;
min_ref_lvl = 7;
max_ref_lvl = 240;
min_valid_crcs = 5; // (picked after extensive testing)
mark_max_dist = 6;
left_bit_pick = 4;
right_bit_pick = 2;
horiz_coords.clear();
horiz_coords.setToZero();
en_force_coords = false;
en_coord_search = true;
en_first_line_dup = true;
en_good_no_marker = true;
}
Binarizer::Binarizer()
{
// Set default preset.
setFineSettings(getDefaultFineSettings());
video_line = NULL;
out_pcm_line = NULL;
log_level = 0;
in_def_black = in_def_reference = in_def_white = 0;
in_def_coord.clear();
in_max_hysteresis_depth = HYST_DEPTH_SAFE;
in_max_shift_stages = SHIFT_STAGES_MIN;
do_coord_search = true;
do_start_mark_sweep = true;
do_ref_lvl_sweep = false;
force_bit_picker = true;
proc_state = STG_REF_FIND;
setMode(MODE_FAST);
setLinePartMode(FULL_LINE);
hysteresis_depth_lim = HYST_DEPTH_MIN;
shift_stages_lim = SHIFT_STAGES_MIN;
line_length = 0;
scan_start = scan_end = 0;
mark_start_max = 0;
mark_end_min = 0xFFFF;
estimated_ppb = 0;
was_BW_scanned = false;
resetCRCStats(shift_crcs, SHIFT_STAGES_MAX+1);
resetCRCStats(hyst_crcs, HYST_DEPTH_MAX+1);
resetCRCStats(crc_stats, MAX_COLL_CRCS+1);
}
//------------------------ Set debug logging level (LOG_PROCESS, etc...).
void Binarizer::setLogLevel(uint8_t in_level)
{
log_level = in_level;
}
//------------------------ Set input video line pointer.
void Binarizer::setSource(VideoLine *in_ptr)
{
video_line = in_ptr;
}
//------------------------ Set output PCM line pointer.
void Binarizer::setOutput(PCMLine *out_ptr)
{
out_pcm_line = out_ptr;
}
//------------------------ Set mode (speed/quality) of binarization.
void Binarizer::setMode(uint8_t in_mode)
{
#ifdef LB_EN_DBG_OUT
bool print_out = false;
if(bin_mode!=in_mode)
{
print_out = true;
}
#endif
if(in_mode==MODE_DRAFT)
{
bin_mode = in_mode;
in_max_hysteresis_depth = HYST_DEPTH_SAFE;
in_max_shift_stages = SHIFT_STAGES_MIN;
}
else if(in_mode==MODE_FAST)
{
bin_mode = in_mode;
in_max_hysteresis_depth = 7;
in_max_shift_stages = SHIFT_STAGES_SAFE;
}
else if(in_mode==MODE_INSANE)
{
bin_mode = in_mode;
in_max_hysteresis_depth = HYST_DEPTH_MAX;
in_max_shift_stages = SHIFT_STAGES_MAX;
}
else
{
bin_mode = MODE_NORMAL;
in_max_hysteresis_depth = HYST_DEPTH_SAFE;
in_max_shift_stages = SHIFT_STAGES_SAFE; // More shifts are prone to false positive CRCs and clicks.
}
#ifdef LB_EN_DBG_OUT
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
if(print_out!=false)
{
if(bin_mode==MODE_DRAFT)
{
qInfo()<<"[LB] Binarization mode set to 'draft' (level sweep: off, hysteresis sweep depth:"<<in_max_hysteresis_depth<<", pixel-shift:"<<in_max_shift_stages<<"stages).";
}
else if(bin_mode==MODE_FAST)
{
qInfo()<<"[LB] Binarization mode set to 'fast' (level sweep: off, hysteresis sweep depth:"<<in_max_hysteresis_depth<<", pixel-shift:"<<in_max_shift_stages<<"stages).";
}
else if(bin_mode==MODE_INSANE)
{
qInfo()<<"[LB] Binarization mode set to 'insane' (level sweep: on, hysteresis sweep depth:"<<in_max_hysteresis_depth<<", pixel-shift:"<<in_max_shift_stages<<"stages).";
}
else
{
qInfo()<<"[LB] Binarization mode set to 'normal' (level sweep: on, hysteresis sweep depth:"<<in_max_hysteresis_depth<<", pixel-shift:"<<in_max_shift_stages<<"stages).";
}
}
}
#endif
}
//------------------------ Set source line part mode.
void Binarizer::setLinePartMode(uint8_t in_mode)
{
#ifdef LB_EN_DBG_OUT
if(line_part_mode!=in_mode)
{
if(in_mode<PART_MAX)
{
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
if(in_mode==PART_PCM16X0_LEFT)
{
qInfo()<<"[LB] Line part mode set to 'PCM-16x0 left third'";
}
else if(in_mode==PART_PCM16X0_MIDDLE)
{
qInfo()<<"[LB] Line part mode set to 'PCM-16x0 middle third'";
}
else if(in_mode==PART_PCM16X0_RIGHT)
{
qInfo()<<"[LB] Line part mode set to 'PCM-16x0 right third'";
}
else
{
qInfo()<<"[LB] Line part mode set to 'full video line'";
}
}
}
}
#endif
if(in_mode<PART_MAX)
{
// Set value from the input.
line_part_mode = in_mode;
}
}
//------------------------ Enable/disable data coordinates search.
void Binarizer::setCoordinatesSearch(bool in_flag)
{
#ifdef LB_EN_DBG_OUT
if(do_coord_search!=in_flag)
{
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
if(in_flag==false)
{
qInfo()<<"[LB] Coordinates search override is disabled.";
}
else
{
qInfo()<<"[LB] Coordinates search override is enabled.";
}
}
}
#endif
// Set value from the input.
do_coord_search = in_flag;
}
//------------------------Set default BLACK and WHITE levels (0 = disable).
void Binarizer::setBWLevels(uint8_t in_black, uint8_t in_white)
{
// Check BW validity.
if((in_black<in_white)&&(in_black<digi_set.max_black_lvl)&&(in_white>digi_set.min_white_lvl)&&(in_white!=0))
{
#ifdef LB_EN_DBG_OUT
if((in_def_black!=in_black)||(in_def_white!=in_white))
{
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
QString log_line;
log_line.sprintf("[LB] Default BLACK and WHITE levels set to: [%03d|%03d]", in_black, in_white);
qInfo()<<log_line;
}
}
#endif
in_def_black = in_black;
in_def_white = in_white;
}
else
{
// Set defaults (disable preset BW levels).
in_def_black = in_def_white = 0;
#ifdef LB_EN_DBG_OUT
if((in_def_black!=in_black)||(in_def_white!=in_white))
{
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
qInfo()<<"[LB] Default BLACK and WHITE levels are disabled.";
}
}
#endif
}
}
//------------------------ Set default reference level (0 = disable).
void Binarizer::setReferenceLevel(uint8_t in_ref)
{
#ifdef LB_EN_DBG_OUT
if(in_def_reference!=in_ref)
{
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
if(in_ref==0)
{
qInfo()<<"[LB] Default reference level is disabled.";
}
else
{
QString log_line;
log_line.sprintf("[LB] Default reference level set to: [%03d]", in_ref);
qInfo()<<log_line;
}
}
}
#endif
// Set value from the input.
in_def_reference = in_ref;
}
//------------------------ Set default pixel coordinates for data (0 = disable).
void Binarizer::setDataCoordinates(int16_t in_data_start, int16_t in_data_stop)
{
CoordinatePair tmp_coord;
// Check coordinates' validity.
if((in_data_start<in_data_stop)&&(in_data_stop!=0)&&(in_data_start!=CoordinatePair::NO_COORD_LEFT)&&(in_data_stop!=CoordinatePair::NO_COORD_RIGHT))
{
// Set values from the input.
tmp_coord.setCoordinates(in_data_start, in_data_stop);
}
#ifdef LB_EN_DBG_OUT
else
{
if(((log_level&LOG_PROCESS)!=0)&&((log_level&LOG_SETTINGS)!=0))
{
qInfo()<<"[LB] Default data pixel coordinates are disabled.";
}
}
#endif
setDataCoordinates(tmp_coord);
}
//------------------------ Set default pixel coordinates for data (0 = disable).
void Binarizer::setDataCoordinates(CoordinatePair in_data_coord)
{
// Check coordinates' validity.
if(in_data_coord.areValid()!=false)
{
// Set values from the input.
in_def_coord = in_data_coord;
#ifdef LB_EN_DBG_OUT
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
QString log_line;
log_line.sprintf("[LB] Default data pixel coordinates set to: [%03d:%04d]", in_data_coord.data_start, in_data_coord.data_stop);
qInfo()<<log_line;
}
#endif
}
else
{
// Set defaults (disable preset coordinates).
in_def_coord.clear();
#ifdef LB_EN_DBG_OUT
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
qInfo()<<"[LB] Default data pixel coordinates are disabled.";
}
#endif
}
}
//------------------------ Set default B&W, reference level and pixel coordinates for data (NULL = disable).
void Binarizer::setGoodParameters(PCMLine *in_pcm_line)
{
if(in_pcm_line==NULL)
{
// Disable default reference level and data coordinates.
setReferenceLevel();
setDataCoordinates();
setBWLevels();
}
else if(in_pcm_line->isCRCValidIgnoreForced()!=false)
{
// Set both default reference level and data coordinates from the input.
setReferenceLevel(in_pcm_line->ref_level);
setDataCoordinates(in_pcm_line->coords);
setBWLevels(in_pcm_line->black_level, in_pcm_line->white_level);
#ifdef LB_EN_DBG_OUT
if(((log_level&LOG_PROCESS)!=0)||((log_level&LOG_SETTINGS)!=0))
{
QString log_line;
log_line.sprintf("[LB] Default parameters are updated: [%03d:%04d]@[%03u]", in_pcm_line->coords.data_start, in_pcm_line->coords.data_stop, in_pcm_line->ref_level);
qInfo()<<log_line;
}
#endif
}
}
//------------------------ Get defaults for internal fine binarization settings.
bin_preset_t Binarizer::getDefaultFineSettings()
{
// All field are preset to defaults in [bin_preset_t] class constructor.
bin_preset_t def_set;
return def_set;
}
//------------------------ Get internal fine binarization settings.
bin_preset_t Binarizer::getCurrentFineSettings()
{
return digi_set;
}
//------------------------ Set internal fine binarization settings.
void Binarizer::setFineSettings(bin_preset_t in_set)
{
digi_set = in_set;
#ifdef LB_EN_DBG_OUT
if((log_level&LOG_SETTINGS)!=0)
{
qInfo()<<"[LB] Fine binarization settings updated";
}
#endif
}
//------------------------ Are BLACK and WHITE levels preset from outside?
bool Binarizer::areBWLevelsPreset()
{
if((in_def_white>digi_set.min_white_lvl)&&(in_def_black<digi_set.max_black_lvl))
{
// Check if reference level also provided.
if(isRefLevelPreset()!=false)
{
// Check if provided ref. level is inside BW levels.
if((in_def_reference<=in_def_black)||(in_def_reference>=in_def_white))
{
return false;
}
}
return true;
}
else
{
return false;
}
}
//------------------------ Is reference level preset from outside?
bool Binarizer::isRefLevelPreset()
{
if(in_def_reference>=digi_set.min_ref_lvl)
{
return true;
}
else
{
return false;
}
}
//------------------------ Main processing.
//------------------------ Take video line [VideoLine], perform AGC, TBC, binarization.
//------------------------ Output PCM data line [PCMLine].
uint8_t Binarizer::processLine()
{
bool suppress_log;
uint8_t stage_count;
uint32_t tmp_calc;
CoordinatePair forced_coords;
// Measure processing time for debuging.
#ifdef QT_VERSION
uint64_t line_time;
uint64_t time_prep, time_in_all, time_in_level, time_ref_find, time_ref_sweep, time_read;
Q_UNUSED(time_prep);
line_time = 0;
time_prep = time_in_all = time_in_level = time_ref_find = time_ref_sweep = time_read = 0;
QElapsedTimer line_timer;
line_timer.start();
#endif
#ifdef LB_EN_DBG_OUT
QString log_line;
#endif
if(video_line==NULL)
{
#ifdef LB_EN_DBG_OUT
qWarning()<<DBG_ANCHOR<<"[LB] Null pointer for video line provided, exiting...";
#endif
return (uint8_t)LB_RET_NULL_VIDEO;
}
if(out_pcm_line==NULL)
{
#ifdef LB_EN_DBG_OUT
qWarning()<<DBG_ANCHOR<<"[LB] Null pointer for PCM line provided, exiting...";
#endif
return (uint8_t)LB_RET_NULL_PCM;
}
// Prepare PCM line for binarization.
// Clear internal line structure according to PCM type.
if(out_pcm_line->getPCMType()==PCMLine::TYPE_PCM1)
{
// PCM-1.
PCM1Line *temp_pcm_ptr;
temp_pcm_ptr = static_cast<PCM1Line *>(out_pcm_line);
temp_pcm_ptr->clear();
}
else if(out_pcm_line->getPCMType()==PCMLine::TYPE_PCM16X0)
{
// PCM-1600/1610/1630.
PCM16X0SubLine *temp_pcm_ptr;
temp_pcm_ptr = static_cast<PCM16X0SubLine *>(out_pcm_line);
temp_pcm_ptr->clear();
// Copy line part assignment.
if(line_part_mode==PART_PCM16X0_LEFT)
{
// Left part.
temp_pcm_ptr->line_part = PCM16X0SubLine::PART_LEFT;
}
else if(line_part_mode==PART_PCM16X0_MIDDLE)
{
// Middle part.
temp_pcm_ptr->line_part = PCM16X0SubLine::PART_MIDDLE;
}
else if(line_part_mode==PART_PCM16X0_RIGHT)
{
// Right part.
temp_pcm_ptr->line_part = PCM16X0SubLine::PART_RIGHT;
}
}
else if(out_pcm_line->getPCMType()==PCMLine::TYPE_STC007)
{
// STC-007/PCM-F1.
STC007Line *temp_pcm_ptr;
temp_pcm_ptr = static_cast<STC007Line *>(out_pcm_line);
temp_pcm_ptr->clear();
}
else if(out_pcm_line->getPCMType()==PCMLine::TYPE_ARVA)
{
// ArVid Audio.
ArVidLine *temp_pcm_ptr;
temp_pcm_ptr = static_cast<ArVidLine *>(out_pcm_line);
temp_pcm_ptr->clear();
}
else
{
// Unsupported PCM type.
out_pcm_line->clear();
}
// Store video line origins in the new PCM line object.
out_pcm_line->frame_number = video_line->frame_number;
out_pcm_line->line_number = video_line->line_number;
suppress_log = !((log_level&LOG_PROCESS)!=0);
// Check if input line has service tags.
if(video_line->isServiceLine()!=false)
{
// Video line contains service tag.
if(video_line->isServNewFile()!=false)
{
// Set output line as service line for new input file.
out_pcm_line->setServNewFile(video_line->file_path);
}
else if(video_line->isServEndFile()!=false)
{
// Set output line as service line for ended video file.
out_pcm_line->setServEndFile();
}
else if(video_line->isServFiller()!=false)
{
// Set output line as filler line.
out_pcm_line->setServFiller();
}
else if(video_line->isServEndField()!=false)
{
// Set output line as service line for ended field.
out_pcm_line->setServEndField();
}
else if(video_line->isServEndFrame()!=false)
{
// Set output line as service line for ended frame.
out_pcm_line->setServEndFrame();
}
// Can not determine if [SRVLINE_HEADER_LINE] or [SRVLINE_CTRL_BLOCK] are applicable before binarization.
}
// Check if source line is 100% empty.
else if(video_line->isEmpty()==false)
{
// Calculate line length, limits for PCM START and PCM STOP markers search, estimate PPB (Pixels Per Bit) value.
// Save current video line length in pixels.
line_length = (uint16_t)video_line->pixel_data.size();
out_pcm_line->setFromDoubledState(video_line->isDoubleWidth());
// Set line scan limits.
scan_start = 0;
scan_end = (line_length-1);
out_pcm_line->setSourcePixels(scan_start, scan_end);
// Check if there no less pixels than bits in PCM line.
if(line_length<out_pcm_line->getBitsPerSourceLine())
{
#ifdef LB_EN_DBG_OUT
qWarning()<<DBG_ANCHOR<<"[LB] Line is too short ("<<line_length<<") for PCM data, exiting...";
qWarning()<<DBG_ANCHOR<<"[LB] Minimal size:"<<out_pcm_line->getBitsPerSourceLine();
#endif
return (uint8_t)LB_RET_SHORT_LINE;
}
// Calculate limits for PCM START and PCM STOP markers search.
if(out_pcm_line->canUseMarkers()==false)
{
// Current PCM line does not support markers.
mark_start_max = 0;
mark_end_min = 0xFFFF;
#ifdef LB_LOG_MARKER_VERBOSE
if(suppress_log==false)
{
log_line.sprintf("[LB] Scan region: [%03u:%04u]", scan_start, scan_end);
qInfo()<<log_line;
}
#endif
}
else
{
// Determine limits within the line for marker search.
mark_start_max = line_length*digi_set.mark_max_dist;
mark_start_max = mark_start_max/100;
mark_end_min = scan_end-mark_start_max;
mark_start_max = scan_start+mark_start_max;
#ifdef LB_LOG_MARKER_VERBOSE
if(suppress_log==false)
{
log_line.sprintf("[LB] Scan regions: [%03u:%03u], [%04u:%04u]",
scan_start, mark_start_max, mark_end_min, scan_end);
qInfo()<<log_line;
}
#endif
}
// Integer calculation magic.
tmp_calc = line_length*PCMLine::INT_CALC_MULT;
// Take all valueable bits in video line.
tmp_calc = tmp_calc/out_pcm_line->getBitsPerSourceLine();
// Estimate minimum bit length (PPB = Pixels Per Bit).
estimated_ppb = (uint16_t)((tmp_calc+(PCMLine::INT_CALC_MULT/2))/PCMLine::INT_CALC_MULT);
// Set preliminary data coordinates at the boundaries of scan range.
out_pcm_line->coords.setCoordinates(scan_start, scan_end);
forced_coords.clear();
if(digi_set.en_force_coords!=false)
{
// Calculate forced data coordinates.
forced_coords.data_start = (int16_t)scan_start+digi_set.horiz_coords.data_start;
forced_coords.data_stop = (int16_t)scan_end-digi_set.horiz_coords.data_stop;
if(video_line->isDoubleWidth()!=false)
{
forced_coords.data_start += digi_set.horiz_coords.data_start;
forced_coords.data_stop -= digi_set.horiz_coords.data_stop;
}
//qDebug()<<scan_start<<forced_coords.data_start<<forced_coords.data_stop<<scan_end;
if(forced_coords.areValid()!=false)
{
// Apply forced coordinates.
out_pcm_line->coords = forced_coords;
out_pcm_line->setDataCoordinatesState(true);
}
}
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
QString sprint_line;
log_line = "[LB] -------------------- Processing line ";
sprint_line.sprintf("[%03u:%03u], size: [%03u] pixels",
out_pcm_line->frame_number, out_pcm_line->line_number, video_line->pixel_data.size());
log_line += sprint_line;
if(out_pcm_line->isSourceDoubleWidth()!=false)
{
log_line += " (line is resized to double the length)";
}
if(forced_coords.areValid()==false)
{
sprint_line.sprintf(", scan area: [%03d:%04d], est. PPB: [%u]",
out_pcm_line->coords.data_start, out_pcm_line->coords.data_stop, estimated_ppb);
}
else
{
sprint_line.sprintf(", forced coordinated: [%03d:%04d], est. PPB: [%u]",
out_pcm_line->coords.data_start, out_pcm_line->coords.data_stop, estimated_ppb);
}
log_line += sprint_line;
qInfo()<<log_line;
if(out_pcm_line->getPCMType()==PCMLine::TYPE_PCM1)
{
qInfo()<<"[LB] PCM type set to PCM-1 (Sony Standard B)";
}
else if(out_pcm_line->getPCMType()==PCMLine::TYPE_PCM16X0)
{
qInfo()<<"[LB] PCM type set to PCM-16x0 (Sony Standard A)";
}
else if(out_pcm_line->getPCMType()==PCMLine::TYPE_STC007)
{
qInfo()<<"[LB] PCM type set to STC-007/Sony PCM-F1";
}
else if(out_pcm_line->getPCMType()==PCMLine::TYPE_ARVA)
{
qInfo()<<"[LB] PCM type set to ArVid Audio";
}
else
{
qWarning()<<DBG_ANCHOR<<"[LB] Unknown PCM type is set!";
}
}
#endif
// Preset the first stage for state machine (reference level calculation by default).
proc_state = STG_REF_FIND;
// BLACK and WHITE level detection was not yet run for the line.
was_BW_scanned = false;
// Check for provided B&W levels.
if(areBWLevelsPreset()!=false)
{
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
log_line.sprintf("[LB] Black and white levels are provided: [%02u|%03u]", in_def_black, in_def_white);
qInfo()<<log_line;
}
#endif
// Set provided B&W levels for output PCM line.
out_pcm_line->black_level = in_def_black;
out_pcm_line->white_level = in_def_white;
out_pcm_line->setBWLevelsState(true);
}
// Check for provided coordinates and reference level (data for speeding up the conversion).
if(isRefLevelPreset()!=false)
{
// Reference level is preset from outside.
// Check for preset data coordinates.
if(in_def_coord.areValid()!=false)
{
// Data coordinates also preset from outside.
// Set the state for all possible presets.
proc_state = STG_INPUT_ALL;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
log_line.sprintf("[LB] Reference level is provided: [%03u]", in_def_reference);
qInfo()<<log_line;
log_line.sprintf("[LB] Data coordinates are provided: [%03d:%04d]", in_def_coord.data_start, in_def_coord.data_stop);
qInfo()<<log_line;
}
#endif
}
else
{
// Set the state to indicate that only reference level is provided.
proc_state = STG_INPUT_LEVEL;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
log_line.sprintf("[LB] Reference level is provided: [%03u]", in_def_reference);
qInfo()<<log_line;
}
#endif
}
}
// Set hysteresis and pixel-shifting limits from set mode.
hysteresis_depth_lim = in_max_hysteresis_depth;
shift_stages_lim = in_max_shift_stages;
#ifdef QT_VERSION
time_prep = line_timer.nsecsElapsed();
#endif
// Cycle through stages.
stage_count = 0;
do
{
// Count loops.
stage_count++;
// DEBUG hook.
/*if((out_pcm_line->frame_number==176)&&(out_pcm_line->line_number==419))
{
qInfo()<<"";
}*/
// Select processing mode.
if(proc_state==STG_INPUT_ALL)
{
// Data coordinates and reference level are preset.
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
qInfo()<<"[LB] ---------- Parameters are set from the input, bypassing coordinates and reference level calculation...";
}
#endif
// Check if B&W levels were preset for the line.
if(out_pcm_line->hasBWSet()==false)
{
// Find WHITE and BLACK levels for hysteresis calculations or [readPCMdata()] will fail.
findBlackWhite();
}
// Check if forced coordinates are preset.
if(forced_coords.areValid()==false)
{
// No forced coordinates.
// Preset data coordinates from input.
out_pcm_line->coords = in_def_coord;
}
// Preset reference level from input.
out_pcm_line->ref_level = in_def_reference;
// Reference level sweep is disabled for this pass.
out_pcm_line->setSweepedReference(false);
// Check if valid B&W levels were found at all.
if(out_pcm_line->hasBWSet()==false)
{
// No valid B&W levels.
proc_state = STG_NO_GOOD;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
qInfo()<<"[LB] No valid BLACK and WHITE levels were found.";
}
#endif
}
else
{
// Check if detected B&W levels and provided ref level fit.
if((in_def_reference>=out_pcm_line->white_level)||(in_def_reference<=out_pcm_line->black_level))
{
// Forcing full processing.
proc_state = STG_REF_FIND;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
log_line.sprintf("[LB] Reference level [%03u] is outside of B&W levels [%03u:%03u]!",
in_def_reference,
out_pcm_line->black_level,
out_pcm_line->white_level);
qInfo()<<log_line;
}
#endif
}
else
{
// Provided ref level fits between B&W levels.
bool force_level_find;
force_level_find = false;
// Check if coordinates search is allowed.
if((do_coord_search!=false)&&(forced_coords.areValid()==false))
{
// Coordinates search is allowed.
if(out_pcm_line->getPCMType()==PCMLine::TYPE_STC007)
{
// STC-007 processing.
STC007Line *temp_stc;
temp_stc = static_cast<STC007Line *>(out_pcm_line);
// Check if valid data without markers is allowed.
if(digi_set.en_good_no_marker==false)
{
// Binarization without markers for STC-007 is not allowed.
// Force find markers for the line.
findSTC007Coordinates(temp_stc);
// Revert to provided coordinates after those were overwritten by search.
out_pcm_line->coords = in_def_coord;
if(temp_stc->hasMarkers()==false)
{
// At least one marker was NOT found.
force_level_find = true;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
qInfo()<<"[LB] Both markers should be present for valid CRC, but at least one is missing";
}
#endif
}
}
}
}
// Try to read PCM data with provided parameters.
readPCMdata(out_pcm_line);
// Check the result.
if(out_pcm_line->isCRCValid()!=false)
{
// Data is valid.
if(force_level_find==false)
{
// Set the flag that data was decoded using external parameters.
out_pcm_line->data_by_ext_tune = true;
// Data is OK, go to exit.
proc_state = STG_DATA_OK;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
if(out_pcm_line->getPCMType()==PCMLine::TYPE_STC007)
{
STC007Line *temp_stc;
temp_stc = static_cast<STC007Line *>(out_pcm_line);
if(temp_stc->hasMarkers()==false)
{
qInfo()<<"[LB] Managed to get VALID data with preset coordinates while no markers detected.";
}
}
}
#endif
}
else
{
// Both markers are set to be required for STC-007.
// Fast binarization gave valid CRC, but at least one marker is absent.
// Forcing full processing.
proc_state = STG_REF_FIND;
}
}
else
{
// Check PCM format to determine next step.
if((out_pcm_line->getPCMType()==PCMLine::TYPE_STC007)&&(forced_coords.areValid()==false))
{
// No valid data, let's try to find PCM data with only preset reference level.
// Skip that step is coordinates are forced.
proc_state = STG_INPUT_LEVEL;
}
else
{
// No valid data, start full paramater detection.
// For PCM-1 and PCM-1600 no need to try to detect only new coordinates (without new reference level),
// because coordinates detection is much more heavy load than for STC-007 (no markers).
// It will take twice as long still if data coordinates search will fail
// and than will be repeated with new reference level.
proc_state = STG_REF_FIND;
}
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
qInfo()<<"[LB] No valid data found with provided data coordinates and reference level.";
}
#endif
}
}
}
#ifdef QT_VERSION
time_in_all = line_timer.nsecsElapsed();
#endif
}
else if(proc_state==STG_INPUT_LEVEL)
{
// Only reference level is preset, need to find data coordinates.
// Check if B&W levels were already found.
if(was_BW_scanned==false)
{
// Find WHITE and BLACK levels for hysteresis calculations or [readPCMdata()] will fail.
findBlackWhite();
}
if(forced_coords.areValid()==false)
{
// Reset data coordinates.
out_pcm_line->coords.setCoordinates(scan_start, scan_end);
}
// Preset reference level from input.
out_pcm_line->ref_level = in_def_reference;
// Reference level sweep is disabled for this pass.
out_pcm_line->setSweepedReference(false);
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
qInfo()<<"[LB] ---------- Reference level is set from the input, bypassing reference level calculation...";
}
#endif
// Check if valid B&W levels were found at all.
if(out_pcm_line->hasBWSet()==false)
{
// No valid B&W levels.
proc_state = STG_NO_GOOD;
#ifdef LB_EN_DBG_OUT
if(suppress_log==false)
{
qInfo()<<"[LB] No valid BLACK and WHITE levels were found.";
}
#endif
}
else
{
// Valid B&W levels are set.
// Preset "go full processing" mode by default.
proc_state = STG_REF_FIND;
// Check PCM format.
// PCM-1 and PCM-16x0 go straight to full detection stage.
if(out_pcm_line->getPCMType()==PCMLine::TYPE_STC007)
{
// STC-007 processing.
// Check if detected B&W levels and provided ref level fit.
if((in_def_reference<out_pcm_line->white_level)&&(in_def_reference>out_pcm_line->black_level))
{
// Provided ref level fits between B&W levels.
STC007Line *temp_stc;
temp_stc = static_cast<STC007Line *>(out_pcm_line);
// Check if data coordinates search is enabled.
if(do_coord_search==false)
{
if(in_def_coord.areValid()==false)
{
// Estimate coordinates.
out_pcm_line->coords.setCoordinates((scan_start+estimated_ppb), (scan_end-(4*estimated_ppb)));
}
else
{
// Set external coordinates.
out_pcm_line->coords = in_def_coord;
}
}
else
{
// Find coordinates for PCM START and STOP markers ([findPCMmarkers()] resets all data coordinates).