-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcm1line.cpp
690 lines (646 loc) · 17.5 KB
/
pcm1line.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
#include "pcm1line.h"
PCM1Line::PCM1Line()
{
this->clear();
}
PCM1Line::PCM1Line(const PCM1Line &in_object) : PCMLine(in_object)
{
// Copy base class fields.
PCMLine::operator =(in_object);
// Copy own fields.
picked_bits_left = in_object.picked_bits_left;
picked_bits_right = in_object.picked_bits_right;
// Copy pixel coordinates.
for(uint8_t stage=0;stage<PCM_LINE_MAX_PS_STAGES;stage++)
{
for(uint8_t bit=0;bit<BITS_PCM_DATA;bit++)
{
pixel_coordinates[stage][bit] = in_object.pixel_coordinates[stage][bit];
}
}
// Copy data words.
for(uint8_t index=0;index<WORD_CNT;index++)
{
words[index] = in_object.words[index];
}
}
PCM1Line& PCM1Line::operator= (const PCM1Line &in_object)
{
if(this==&in_object) return *this;
// Copy base class fields.
PCMLine::operator =(in_object);
// Copy own fields.
picked_bits_left = in_object.picked_bits_left;
picked_bits_right = in_object.picked_bits_right;
// Copy pixel coordinates.
for(uint8_t stage=0;stage<PCM_LINE_MAX_PS_STAGES;stage++)
{
for(uint8_t bit=0;bit<BITS_PCM_DATA;bit++)
{
pixel_coordinates[stage][bit] = in_object.pixel_coordinates[stage][bit];
}
}
// Copy data words.
for(uint8_t index=0;index<WORD_CNT;index++)
{
words[index] = in_object.words[index];
}
return *this;
}
//------------------------ Reset all fields to default.
void PCM1Line::clear()
{
// Clear base class fields.
PCMLine::clear();
// Clear own fields.
picked_bits_left = picked_bits_right = 0;
// Reset pixel coordinates.
for(uint8_t bit=0;bit<BITS_PCM_DATA;bit++)
{
for(uint8_t stage=0;stage<PCM_LINE_MAX_PS_STAGES;stage++)
{
pixel_coordinates[stage][bit] = 0;
}
}
// Reset data words.
setSilent();
// Force CRC to be bad until good binarization.
//calcCRC();
calc_crc = CRC_SILENT; // Pre-calculated CRC for silenced line.
setInvalidCRC(); // Set "read" CRC to inverted one.
}
//------------------------ Set service flag "header/footer line".
void PCM1Line::setServHeader()
{
// Convert this line into service line.
PCMLine::setServiceLine();
// Set service tag.
service_type = SRVLINE_HEADER_LINE;
}
//------------------------ Set word with source CRC.
void PCM1Line::setSourceCRC(uint16_t in_crc)
{
words[WORD_CRCC] = in_crc;
}
//------------------------ Zero out all data words.
void PCM1Line::setSilent()
{
for(uint8_t i=WORD_L2;i<=WORD_R6;i++)
{
words[i] = BIT_RANGE_POS;
}
calcCRC();
}
//------------------------ Copy 13-bit word into the object.
void PCM1Line::setWord(uint8_t index, uint16_t in_word)
{
if(index<WORD_CNT)
{
if(index==WORD_CRCC)
{
words[index] = in_word&CRC_WORD_MASK;
}
else
{
words[index] = in_word&DATA_WORD_MASK;
}
}
}
//------------------------ Get number of data bits in the line object.
uint8_t PCM1Line::getBitsPerObject()
{
return BITS_PCM_DATA;
}
//------------------------ Get number of data bits in the source line by the standard.
uint8_t PCM1Line::getBitsPerSourceLine()
{
return BITS_IN_LINE;
}
//------------------------ Get number of data bits between data coordinates (for PPB calculation).
uint8_t PCM1Line::getBitsBetweenDataCoordinates()
{
return BITS_PCM_DATA;
}
//------------------------ Get highest bit number for the left part of the line for pixel-shifting.
uint8_t PCM1Line::getLeftShiftZoneBit()
{
return BITS_LEFT_SHIFT;
}
//------------------------ Get lowest bit number for the right part of the line for pixel-shifting.
uint8_t PCM1Line::getRightShiftZoneBit()
{
return BITS_RIGHT_SHIFT;
}
//------------------------ Get pre-calculated coordinate of pixel in video line for requested PCM bit number and pixel-shifting stage.
//------------------------ [calcPPB()] MUST be called before any [findVideoPixel()] calls!
uint16_t PCM1Line::getVideoPixelByTable(uint8_t pcm_bit, uint8_t shift_stage)
{
return pixel_coordinates[shift_stage][pcm_bit];
}
//------------------------ Re-calculate CRCC for all words in the line.
void PCM1Line::calcCRC()
{
CRC16_init(&calc_crc);
for(uint8_t i=WORD_L2;i<=WORD_R6;i++)
{
calc_crc = getCalcCRC16(calc_crc, ~words[i], BITS_PER_WORD);
}
calc_crc = ~calc_crc;
#ifdef PCM1_LINE_EN_DBG_OUT
QString log_line;
log_line.sprintf("[PCM1L] Calculating CRC... %04x", calc_crc);
qInfo()<<log_line;
#endif
}
//------------------------ Get CRC that was read from the source.
uint16_t PCM1Line::getSourceCRC()
{
return words[WORD_CRCC];
}
//------------------------ Get the type of PCM to determine class derived from [PCMLine].
uint8_t PCM1Line::getPCMType()
{
return TYPE_PCM1;
}
//------------------------ Get one word.
uint16_t PCM1Line::getWord(uint8_t index)
{
if(index<WORD_CNT)
{
return words[index];
}
return BIT_RANGE_POS;
}
//------------------------ Convert one 13-bit word to a 16-bit sample.
int16_t PCM1Line::getSample(uint8_t index)
{
bool is_positive;
uint16_t data_word;
if(index<WORD_CRCC)
{
data_word = words[index];
// Check 13th bit for range (R bit).
if((data_word&BIT_RANGE_POS)==0)
{
// Higher range.
// Move 4 bits left to fill up 16-bit MSBs.
data_word = (data_word<<4);
}
else
{
// Lower range.
// Pick sign of the value.
is_positive = ((data_word&BIT_SIGN_POS)==0);
// Remove range bit.
data_word = data_word&(~BIT_RANGE_POS);
// Move 2 bits left, leaving 2 MSBs empty.
data_word = (data_word<<2);
if(is_positive==false)
{
// Fill all MSBs following negative sign.
data_word|=(1<<15)|(1<<14);
}
}
}
else
{
// Index out-of-bounds.
data_word = 0;
}
return (int16_t)data_word;
}
//------------------------ Get number of different bits from provided line.
uint8_t PCM1Line::getWordsDiffBitCount(PCM1Line *in_line)
{
if(in_line==NULL)
{
return 0;
}
uint8_t bit_cnt, diff_mask;
bit_cnt = 0;
// Cycle through all words.
for(uint8_t index=WORD_L2;index<=WORD_R6;index++)
{
// XOR to detect differences in words.
diff_mask = words[index]^in_line->words[index];
if(diff_mask!=0)
{
// Words are not the same.
// Cycle through all bits of those words.
for(uint8_t bit=0;bit<=16;bit++)
{
// Count number of different bits.
if((diff_mask&(1<<bit))!=0)
{
bit_cnt++;
}
}
}
}
return bit_cnt;
}
//------------------------ Does provided line have the same words?
bool PCM1Line::hasSameWords(PCM1Line *in_line)
{
if(in_line==NULL)
{
return false;
}
for(uint8_t index=WORD_L2;index<=WORD_R6;index++)
{
if(words[index]!=in_line->words[index])
{
return false;
}
}
return true;
}
//------------------------ Does this line contain words with picked bits?
bool PCM1Line::hasPickedWords()
{
if((picked_bits_left!=0)||(picked_bits_right!=0))
{
return true;
}
return false;
}
//------------------------ Were the leftmost word's bits picked during binarization?
bool PCM1Line::hasPickedLeft()
{
if(picked_bits_left!=0)
{
return true;
}
return false;
}
//------------------------ Were the rightmost word's bits picked during binarization?
bool PCM1Line::hasPickedRight()
{
if(picked_bits_right!=0)
{
return true;
}
return false;
}
//------------------------ Does this line contain header line (and no audio data)?
bool PCM1Line::hasHeader()
{
if((words[WORD_L2]==0x0666)&&(words[WORD_R2]==0x0CCC)&&(words[WORD_L4]==0x1999)
&&(words[WORD_R4]==0x1333)&&(words[WORD_L6]==0x0666)&&(words[WORD_R6]==0x0CCC)
&&(getSourceCRC()==0xCCCC))
{
return true;
}
return false;
}
//------------------------ Is CRC valid (re-calculated CRC is the same as read one) or does the line contain the header (PCM-1 special case)?
bool PCM1Line::isCRCValidIgnoreForced()
{
if((getCalculatedCRC()==getSourceCRC())||(hasHeader()!=false))
{
return true;
}
return false;
}
//------------------------ Is audio sample near zero value?
bool PCM1Line::isNearSilence(uint8_t index)
{
int16_t audio_sample;
audio_sample = getSample(index);
// Allow 1 LSBs (1 LSBs in 14-bit word become 3 LSBs in 16-bit state) wiggle room.
if(audio_sample>=(int16_t)(1<<3))
{
return false;
}
if(audio_sample<(0-(int16_t)(1<<3)))
{
return false;
}
return true;
}
//------------------------ Are audio samples in both channels near zero?
bool PCM1Line::isAlmostSilent()
{
uint8_t silent_words;
silent_words = 0;
// Check if any channel is close to silence.
for(uint8_t index=WORD_L2;index<=WORD_R6;index++)
{
if(isNearSilence(index)!=false)
{
silent_words++;
}
}
if(silent_words>=2) return true;
return false;
}
//------------------------ Are all audio words zeroed?
bool PCM1Line::isSilent()
{
for(uint8_t index=WORD_L2;index<=WORD_R6;index++)
{
if(getSample(index)!=0)
{
return false;
}
}
return true;
}
//------------------------ Check if line has service tag "header/footer line".
bool PCM1Line::isServHeader()
{
if(service_type==SRVLINE_HEADER_LINE)
{
return true;
}
return false;
}
//------------------------ Convert PCM data to string for debug.
std::string PCM1Line::dumpWordsString()
{
std::string text_out;
uint8_t ind, bit_pos;
// Printing 13 bit words data.
for(ind=WORD_L2;ind<=WORD_R6;ind++)
{
if(isCRCValid()==false)
{
text_out += DUMP_WBRL_BAD;
}
else
{
text_out += DUMP_WBRL_OK;
}
bit_pos = BITS_PER_WORD;
do
{
bit_pos--;
if(isCRCValid()==false)
{
if((words[ind]&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO_BAD;
}
else
{
text_out += DUMP_BIT_ONE_BAD;
}
}
else if((ind==WORD_L2)&&(bit_pos>=(BITS_PER_WORD-picked_bits_left)))
{
if((words[ind]&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO_BAD;
}
else
{
text_out += DUMP_BIT_ONE_BAD;
}
}
else
{
if((words[ind]&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO;
}
else
{
text_out += DUMP_BIT_ONE;
}
}
}
while(bit_pos>0);
if(isCRCValid()==false)
{
text_out += DUMP_WBRR_BAD;
}
else
{
text_out += DUMP_WBRR_OK;
}
}
// Printing 16 bit CRCC data.
if(isCRCValid()==false)
{
text_out += DUMP_WBRL_BAD;
}
else
{
text_out += DUMP_WBRL_OK;
}
bit_pos = BITS_PER_CRC;
do
{
bit_pos--;
if(isCRCValid()==false)
{
if((getSourceCRC()&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO_BAD;
}
else
{
text_out += DUMP_BIT_ONE_BAD;
}
}
else if(bit_pos<picked_bits_right)
{
if((getSourceCRC()&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO_BAD;
}
else
{
text_out += DUMP_BIT_ONE_BAD;
}
}
else
{
if((getSourceCRC()&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO;
}
else
{
text_out += DUMP_BIT_ONE;
}
}
}
while(bit_pos>0);
if(isCRCValid()==false)
{
text_out += DUMP_WBRR_BAD;
}
else
{
text_out += DUMP_WBRR_OK;
}
return text_out;
}
//------------------------ Output full information about the line.
std::string PCM1Line::dumpContentString()
{
std::string text_out;
char c_buf[256];
if(isServiceLine()!=false)
{
if(isServNewFile()!=false)
{
sprintf(c_buf, "F[%03u] L[%03u] SERVICE LINE: next lines are from new file: %s", (unsigned int)frame_number, line_number, file_path.substr(0, 192).c_str());
text_out += c_buf;
}
else if(isServEndFile()!=false)
{
sprintf(c_buf, "F[%03u] L[%03u] SERVICE LINE: previous lines were last in the file", (unsigned int)frame_number, line_number);
text_out += c_buf;
}
else if(isServFiller()!=false)
{
sprintf(c_buf, "F[%03u] L[%03u] SERVICE LINE: line from a filler frame", (unsigned int)frame_number, line_number);
text_out += c_buf;
}
else if(isServEndField()!=false)
{
sprintf(c_buf, "F[%03u] L[%03u] SERVICE LINE: field of a frame ended", (unsigned int)frame_number, line_number);
text_out += c_buf;
}
else if(isServEndFrame()!=false)
{
sprintf(c_buf, "F[%03u] L[%03u] SERVICE LINE: frame ended", (unsigned int)frame_number, line_number);
text_out += c_buf;
}
else if(isServHeader()!=false)
{
sprintf(c_buf, "F[%03u] L[%03u] SERVICE LINE: PCM-1 header/footer line", (unsigned int)frame_number, line_number);
text_out += c_buf;
}
}
else
{
sprintf(c_buf, "F[%03u] L[%03u] ", (unsigned int)frame_number, line_number);
text_out += c_buf;
if(hasBWSet()==false)
{
sprintf(c_buf, "Y[%03u?%03u] ", black_level, white_level);
text_out += c_buf;
}
else
{
sprintf(c_buf, "Y[%03u|%03u] ", black_level, white_level);
text_out += c_buf;
}
if(isDataByRefSweep()!=false)
{
sprintf(c_buf, "R>[%03u|%03u|%03u] ", ref_low, ref_level, ref_high);
}
else if(isDataBySkip()!=false)
{
sprintf(c_buf, "R=[%03u|%03u|%03u] ", ref_low, ref_level, ref_high);
}
else
{
sprintf(c_buf, "R?[%03u|%03u|%03u] ", ref_low, ref_level, ref_high);
}
text_out += c_buf;
if(isDataByCoordSweep()!=false)
{
sprintf(c_buf, "D[%03d:%04d] ", coords.data_start, coords.data_stop);
text_out += c_buf;
}
else if(coords.areValid()!=false)
{
sprintf(c_buf, "D[%03d|%04d] ", coords.data_start, coords.data_stop);
text_out += c_buf;
}
else
{
text_out += "D[N/A| N/A] ";
}
text_out += dumpWordsString();
text_out += " BP[";
if(picked_bits_left>0)
{
sprintf(c_buf, "%01u/", picked_bits_left);
text_out += c_buf;
}
else
{
text_out += "-/";
}
if(picked_bits_right>0)
{
sprintf(c_buf, "%01u] ", picked_bits_right);
text_out += c_buf;
}
else
{
text_out += "-] ";
}
if(isSilent()!=false)
{
text_out += "FS ";
}
else if(isAlmostSilent()!=false)
{
text_out += "AS ";
}
else
{
text_out += "NS ";
}
sprintf(c_buf, "PPB[%02u.%03u] ", getPPB(), getPPBfrac());
text_out += c_buf;
sprintf(c_buf, "HD[%02u] ", hysteresis_depth);
text_out += c_buf;
sprintf(c_buf, "SS[%01u] ", shift_stage);
text_out += c_buf;
sprintf(c_buf, "T[%05u] ", process_time);
text_out += c_buf;
if(isCRCValidIgnoreForced()!=false)
{
if(isForcedBad()==false)
{
sprintf(c_buf, "CRC OK [0x%04X]", getCalculatedCRC());
text_out += c_buf;
}
else
{
sprintf(c_buf, "BD CRC! [0x%04X] (forced)", getCalculatedCRC());
text_out += c_buf;
}
}
else if(hasBWSet()!=false)
{
sprintf(c_buf, "BD CRC! [0x%04X!=0x%04X]", getCalculatedCRC(), getSourceCRC());
text_out += c_buf;
}
else
{
text_out += "No PCM!";
}
}
return text_out;
}
//------------------------ Next line in help output.
std::string PCM1Line::helpDumpNext()
{
std::string text_out;
if(help_stage<PCM1_LINE_HELP_SIZE)
{
text_out = PCM1_HELP_LIST[help_stage];
help_stage++;
}
else
{
text_out.clear();
}
return text_out;
}
//------------------------ Pre-calculate pixel coordinates list for given pixel shift stage.
void PCM1Line::calcCoordinates(uint8_t in_shift)
{
for(uint8_t bit=0;bit<BITS_PCM_DATA;bit++)
{
pixel_coordinates[in_shift][bit] = PCMLine::getVideoPixeBylCalc(bit, in_shift);
}
}