-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse.c
830 lines (717 loc) · 26.8 KB
/
analyse.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
/*
@file analyse.c
Algorithms to analyse IR codes
Copyright 2011, Paul Chambers. All Rights Reserved.
*/
#include "common.h"
#include "analyse-ir-codes.h"
#include "analyse.h"
/* indexed by tDeviceType */
const char *gDeviceTypeName[] =
{
"unknown device",
#define defineDeviceType(id,string) string,
#include "devicetypemapping.h"
#undef defineDeviceType
NULL
};
/* indexed by tBrand */
const char *gBrandName[] =
{
"unknown brand",
#define defineBrand(id,string) string,
#include "brandmapping.h"
#undef defineBrand
NULL
};
/* have to do this the hard way - can't statically initialize flexible array members */
struct {
tCount count;
unsigned long period[8]; /* NOT tPeriods - these are not scaled */
} gRepeatStream[] = {
#define defineRepeatStream(id,...) __VA_ARGS__,
#include "repeatstreammapping.h"
#undef defineRepeatStream
{0}
};
tReferenceFingerprint gProtocol[] =
{
/* encoding, symbolCounts, carrierFreq, leading {mark, space}, durations, mark symbols, space symbols */
{ kFromSpec, "NEC", kSpaceVaries, {32}, 38000, {342,171}, 4104, {21}, {21,64}, kNECRepeatStream },
{ kFromSpec, "Sony SIRCS", kMarkVaries, {12,15,20}, 40000, {96}, 1800, {24,48}, {24} },
{ kFromSpec, "Philips RC-5", kBiphase, {13}, 36000, {0}, 4445, {32,64}, {32,64} },
{ kFromSpec, "Philips RC-5 (a)", kAmbiguous, {12}, 36000, {0}, 4445, {32,64}, {32,64} }, /* RC-5 '0' digit */
{ kFromSpec, "Philips RC-5e", kBiphaseExtended, {18,19}, 36000, {0}, 4445, {32,64}, {32,64} },
{ kFromSpec, "JVC (1)", kSpaceVaries, {16,32}, 38000, {320,160}, 2195, {20}, {20,60} },
{ kFromSpec, "JVC (2)", kSpaceVaries, {16,32}, 38000, {320,160}, 3373, {20}, {20,60} },
/* the following were measured */
{ kMeasured, "Panasonic", kSpaceVaries, {48}, 37000, {128,64}, 4673, {16}, {16,48} },
{ kMeasured, "RCA", kSpaceVaries, {24}, 57360, {229,229}, 3695, {29}, {57,114} },
{ kMeasured, "Denon", kSpaceVaries, {15}, 38000, {0}, 2564, {10}, {30,70} },
/* the following I created by hand, from analysis of the database */
{ kFromDB, "NEC-like (1)", kSpaceVaries, {32}, 38000, {171,171}, 4104, {21}, {21,64} },
{ kFromDB, "NEC-Like (2)", kSpaceVaries, {32}, 38000, {342,171}, 5893, {21}, {21,64} },
{ kFromDB, "NEC-Like (3)", kSpaceVaries, {28,40,42}, 38000, {342,171}, 4104, {21}, {21,64} },
{ kFromDB, "Mitsubishi?", kSpaceVaries, {16}, 33000, {0}, 1822, {11}, {28,67} },
{ kFromDB, "Mystery2?", kSpaceVaries, {10}, 36000, {0}, 872, {11}, {26,64} },
{ kFromDB, "JVC?", kSpaceVaries, {16}, 58800, {0,360}, 3600, {26}, {97,163} },
{ kFromDB, "Philips? (1)", kBiphaseExtended, {37}, 37000, {98}, 3886, {17,33,50}, {15,31} },
{ kFromDB, "Philips? (2)", kBiphase, {22,23}, 38000, {102}, 4000, {18,34}, {16,32} },
{ kFromDB, "Philips? (3)", kBiphaseExtended, {19,20}, 38000, {102}, 4000, {18,34}, {16,32} },
{ kFromDB, "TCL (Philips?)", kBiphase, {13}, 38400, {0}, 4062, {16,32}, {16,32} },
{ kFromDB, "Fujitsu?", kSpaceVaries, {48}, 38400, {132,59}, 3904, {20}, {11,44} }, /* set 100061,200033 */
{ kFromDB, "Panasonic? (3)", kSpaceVaries, {48}, 37000, {128,64}, 3200, {16}, {16,48} },
{ kFromDB, "Panasonic? (4)", kSpaceVaries, {22}, 56000, {196,196}, 5668, {50}, {45,140} },
{ kFromDB, "Funai?", kSpaceVaries, {24}, 38400, {136,136}, 4065, {35}, {31,100} },
{ kFromDB, "Mystery1?", kPPM, {16,21,22}, 40000, {0}, 7138, {21}, {18,163,203} },
{ kFromDB, "Pace?", kSpaceVaries, {17}, 40000, {361}, 4000, {23}, {86,178} }, /* set 200011, 200003 */
{ kFromDB, "Samsung? (2)", kPPM, {38}, 38400, {172}, 4608, {10}, {20,657} },
{ kFromDB, "Samsung? (1)", kSpaceVaries, {48}, 38400, {96,71}, 3724, {17}, {15,43} },
{ kFromDB, "Yamaha?", kPPM, {18}, 38400, {323}, 2550, {23}, {17,53,157} }, /* set 100090,100060,100014 - two part? */
{ kFromDB, "Mystery3?", kSpaceVaries, {11}, 48000, {0}, 6000, {11}, {239,365} },
{ kFromDB, "Mystery4?", kSpaceVaries, {17}, 38400, {94}, 4400, {47}, {44,106} }, /* set 200024 */
{ kFromDB, "Zenith?", kPPM, {15}, 40000, {0}, 7110, {21}, {18,161,201} }, /* set 200008 */
{ kFromDB, "Mystery5?", kBiphase, {11,12,13,14}, 38400, {230}, 1950, {23,46}, {22,45} }, /* set 200006 */
{ kFromDB, "Daewoo?", kPPM, {18}, 38400, {308}, 2291, {20}, {18,56,152} }, /* set 100093 - two part? */
{ kListEnd }
};
inline tPeriod toPeriod(unsigned long x) { return (tPeriod)(x*8+4); }
inline float periodToFloat( tPeriod x) { return (x/8.0); }
/**
@param reference the reference value to match against
@param value the value to check
@param threshold the limit within which the match is acceptable (in tenths of a percent)
*/
int fuzzyMatch(unsigned long reference, unsigned long value, unsigned int threshold)
{
return ( ((abs(reference - value) * 1000 * 2) / (reference + value)) < threshold );
}
int durationsMatch(int durationA, int durationB)
{
if ( (durationA * durationB) == 0 )
return ((durationA + durationB) == 0);
return fuzzyMatch( durationA, durationB, 100 );
}
int checkSymbolCount(tReferenceFingerprint *reference, int symbolCount)
{
int i;
for (i = 0; i < SYMBOL_ARRAY_SIZE; ++i)
{
if (reference->symbolCounts[i] == 0)
break;
if (reference->symbolCounts[i] == symbolCount)
return 1;
}
return 0;
}
inline int checkEncoding( tEncoding referenceEncoding, tEncoding fingerprintEncoding )
{
return (
(fingerprintEncoding == referenceEncoding)
|| (fingerprintEncoding == kAmbiguous && referenceEncoding < kAmbiguous)
);
}
tReferenceFingerprint *identifyProtocol(tFingerprint *fingerprint)
{
tReferenceFingerprint *result;
int refCarrier;
int fpCarrier, fpLeadMark, fpLeadSpace, fpDuration;
/* scale appropriately, to avoid both overflow and loss-of-precision */
fpCarrier = fingerprint->carrierFreq/100;
fpLeadMark = (fingerprint->leading.mark * 1000) / fpCarrier;
fpLeadSpace = (fingerprint->leading.space * 1000) / fpCarrier;
fpDuration = (fingerprint->duration * 1000) / fpCarrier;
result = &gProtocol[0];
while (result->confidence != kListEnd)
{
refCarrier = result->carrierFreq/100;
/* compare fingerprint with reference, see if they match */
if ( checkEncoding( result->encoding, fingerprint->encoding )
&& checkSymbolCount(result, fingerprint->symbolCount)
&& durationsMatch((result->leading.mark*1000)/refCarrier, fpLeadMark )
&& durationsMatch((result->leading.space*1000)/refCarrier, fpLeadSpace )
&& durationsMatch((result->duration*1000)/refCarrier, fpDuration)
)
{ /* we have a match */
++result->matched;
return result;
}
++result;
}
++result->matched;
return NULL;
}
void dumpFingerprintStats(void)
{
tReferenceFingerprint *protocol;
int total = 0;
logDebug(0, "--- table of fingerprints identified ---" );
protocol = &gProtocol[0];
while (protocol->confidence != kListEnd) {
logprintf(DEBUG_LINE_PREFIX " %4d codes identified as %s\n", protocol->matched, protocol->name );
total += protocol->matched;
++protocol;
}
total += protocol->matched;
logprintf(DEBUG_LINE_PREFIX " %4d codes not identified out of %d (%d%%)\n",
protocol->matched, total, (protocol->matched * 100 / total) );
}
void dumpHistogram(tHistogram *hist)
{
tCount i;
char sep;
if (hist == NULL) return;
logprintf("%lu: ", hist->count);
sep = ' ';
for (i = 0; i < hist->count; ++i)
{
logprintf("%c%.3f:%lu", sep, periodToFloat(hist->d[i].period), hist->d[i].count );
sep = ',';
}
logprintf("\n");
}
void dumpRepeatType(tIRCode *code)
{
const char * rptStr;
switch (code->fingerprint.repeatType)
{
case kFullRepeat: rptStr = "full repeat"; break;
case kPartialRepeat: rptStr = "partial repeat"; break;
case kRepeat: rptStr = "repeat"; break;
case kToggleRepeat: rptStr = "toggle"; break;
default: rptStr = "unknown"; break;
}
logDebug(0,"repeat type: %s", rptStr);
}
void dumpFingerprint(tFingerprint *fingerprint)
{
const char *encodingStr;
switch (fingerprint->encoding)
{
case kMarkVaries: encodingStr = "mark varies"; break;
case kSpaceVaries: encodingStr = "space varies"; break;
case kBiphase: encodingStr = "biphase"; break;
case kPPM: encodingStr = "PPM"; break;
case kAmbiguous: encodingStr = "ambiguous"; break;
case kMarkVariesExtended: encodingStr = "mark varies extended"; break;
case kSpaceVariesExtended: encodingStr = "space varies extended"; break;
case kBiphaseExtended: encodingStr = "biphase extended"; break;
default: encodingStr = "### unknown ###"; break;
}
logDebug(0, "encoding %s, symbol count %d", encodingStr, fingerprint->symbolCount);
logDebug(0, "carrier %lu, duration %lu, leading: {%lu,%lu}, trailing: {%lu,%lu}",
fingerprint->carrierFreq,
fingerprint->duration,
fingerprint->leading.mark,
fingerprint->leading.space,
fingerprint->trailing.mark,
fingerprint->trailing.space );
logprintf(DEBUG_LINE_PREFIX "mark "); dumpHistogram(fingerprint->mark);
logprintf(DEBUG_LINE_PREFIX "space "); dumpHistogram(fingerprint->space);
}
void dumpStream(tIRStream *stream)
{
tCount i;
char sep;
unsigned long duration;
if (stream == NULL)
{
logprintf("(empty)\n");
}
else
{
duration = 0;
sep = ' ';
for (i = 0; i < stream->count; ++i)
{
logprintf("%c%lu", sep, stream->period[i] );
duration += stream->period[i];
sep = (sep == '-')? ',' : '-';
}
logprintf(" (total %lu)\n", duration );
}
}
void dumpIRStreams(tIRCode *code)
{
logprintf(DEBUG_LINE_PREFIX "first/a:");
dumpStream( code->first.a );
if (code->first.b != NULL)
{
logprintf(DEBUG_LINE_PREFIX "first/b:");
dumpStream( code->first.b );
}
if (code->repeat.a != NULL)
{
logprintf(DEBUG_LINE_PREFIX "repeat/a:");
dumpStream( code->repeat.a );
}
if (code->repeat.b != NULL)
{
logprintf(DEBUG_LINE_PREFIX "repeat/b:");
dumpStream( code->repeat.b );
}
}
void dumpIRCode(tIRCode *code)
{
dumpFingerprint( &code->fingerprint );
dumpRepeatType( code );
dumpIRStreams( code );
}
tHistogram *dupHistogram(tRawHistogram *raw)
{
tHistogram *result;
tCount i;
if (raw == NULL || raw->count == 0)
return NULL;
result = calloc( 1, sizeof(tHistogram) + (raw->count * sizeof(tHistEntry)) );
if (result != NULL)
{
result->count = raw->count;
for (i = 0; i < raw->count; ++i)
{
result->d[i].period = raw->d[i].period;
result->d[i].count = raw->d[i].count;
}
}
return result;
}
tHistogram *normalizeRawHistogram(tRawHistogram *hist)
{
tCount i;
int refPeriod;
unsigned long periodSum;
tCount periodCount;
tRawHistogram symbols;
symbols.count = 0;
i = 0;
while (i < hist->count)
{
/* collapse runs of periods that differ by under 10% */
refPeriod = hist->d[i].period;
periodSum = 0;
periodCount = 0;
while ( (i < hist->count) && fuzzyMatch( refPeriod + 7, hist->d[i].period, 100) )
{
/* logprintf("[%d]=%d:%d,", i, hist->d[i].period, hist->d[i].count); */
periodSum += (hist->d[i].period * hist->d[i].count);
periodCount += hist->d[i].count;
refPeriod = hist->d[i].period;
++i;
}
if (periodCount > 0)
{
symbols.d[symbols.count].period = periodSum / periodCount;
symbols.d[symbols.count].count = periodCount;
if (symbols.count < MAX_HIST_SIZE)
++symbols.count;
}
/* logprintf("\n<%d> %d (%d/%d = %f)\n",
symbols.count,
(periodSum + periodCount/2) / periodCount,
periodSum, periodCount,
(float)periodSum/(float)periodCount ); */
}
return dupHistogram(&symbols);
}
void insertIntoSortedHistogram( tRawHistogram *hist, tPeriod period )
{
unsigned int insert,insertAt;
tCount j;
/* default is to append, if the loop completes (i.e. period is the largest seen so far) */
insert = 1;
insertAt = hist->count;
for (j = 0; j < hist->count; j++)
{
if (period == hist->d[j].period)
{
++hist->d[j].count;
insert = 0; /* don't insert, we found an existing entry */
break; /* terminate loop, we're done */
}
else if (period < hist->d[j].period)
{
/* tell the insertion code where to insert */
insert = 1;
insertAt = j;
/* move the latter half of the array up by one, to make a hole */
for (j = hist->count; j > insertAt; --j)
{
hist->d[j].period = hist->d[j-1].period;
hist->d[j].count = hist->d[j-1].count;
}
break; /* terminate loop, following 'insert' code does the rest */
}
}
if (insert) /* or append */
{
hist->d[insertAt].period = period;
hist->d[insertAt].count = 1;
if (hist->count < MAX_HIST_SIZE - 1)
++hist->count;
}
}
/* returns the count from the matching histgram entry, or 0 if there isn't one */
int countFromHistogram(tHistogram *hist, int period)
{
tCount j;
for (j = 0; j < hist->count; j++)
{
if ( fuzzyMatch(hist->d[j].period, period, 100) )
return hist->d[j].count;
}
return 0;
}
void incCountInHistogram(tHistogram *hist, int period)
{
tCount j;
for (j = 0; j < hist->count; j++)
{
if ( fuzzyMatch(hist->d[j].period, period, 100) )
{
++hist->d[j].count;
break;
}
}
}
void analyzeIRStream(tIRStream *stream, tFingerprint *fingerprint)
{
tRawHistogram mark, space, *rhist;
tHistogram *hist;
tCount i, last;
int period;
/* build histograms of the periods in this IR stream */
mark.count = 0;
space.count = 0;
rhist = &mark;
last = (stream->count - 2);
fingerprint->duration = stream->period[0];
fingerprint->duration += stream->period[1];
for (i = 2; i < last; ++i)
{
period = stream->period[i];
fingerprint->duration += period;
insertIntoSortedHistogram(rhist, toPeriod(period));
/* dumpHistogram((tHistogram *)hist); */
rhist = (rhist != &mark)? &mark : &space;
}
fingerprint->duration += stream->period[i++]; /* last mark */
fingerprint->duration += stream->period[i]; /* add in the inter-code gap */
fingerprint->mark = normalizeRawHistogram(&mark);
fingerprint->space = normalizeRawHistogram(&space);
/* we now have a histogram of the 'meat' of the code, i.e. ignoring leading pair
and trailing mark. Next determine if those are also valid symbols */
fingerprint->leading.mark = stream->period[0];
if ( countFromHistogram( fingerprint->mark, toPeriod(fingerprint->leading.mark)) > 0 )
{
incCountInHistogram( fingerprint->mark, toPeriod(fingerprint->leading.mark));
fingerprint->leading.mark = 0;
}
fingerprint->leading.space = stream->period[1];
if ( countFromHistogram( fingerprint->space, toPeriod(fingerprint->leading.space)) > 0 )
{
incCountInHistogram( fingerprint->space, toPeriod(fingerprint->leading.space));
fingerprint->leading.space = 0;
}
fingerprint->trailing.mark = stream->period[stream->count - 2];
if ( countFromHistogram( fingerprint->mark, toPeriod(fingerprint->trailing.mark)) > 0 )
{
incCountInHistogram( fingerprint->mark, toPeriod(fingerprint->trailing.mark));
fingerprint->trailing.mark = 0;
}
fingerprint->trailing.space = stream->period[stream->count - 1];
/* now try to determine the type of encoding */
switch (fingerprint->mark->count)
{
case 1: /* bursts are constart width, space varies. Predominant method. */
switch (fingerprint->space->count)
{
case 0:
fingerprint->encoding = kUnknown;
break;
case 1: /* Pathlogical case - both mark and space are a constant width. Encoding
could be kMarkVaries, kSpaceVaries, kBiphase or PPM, we can't tell. */
/* Typically this occurs when the code is either all zeros or all ones,
alternating ones and zeros for biphase, or identical repeated bit
groups for for PPM */
fingerprint->encoding = kAmbiguous;
break;
case 2: /* The most common encoding method by far. */
fingerprint->encoding = kSpaceVaries;
break;
case 3:
/* this is also ambiguous, a given PPM code could only use 3 symbols,
and the longest one may only occur once.
But PPM is unusal, so assume the common case */
if (fingerprint->space->d[2].count == 1)
fingerprint->encoding = kSpaceVariesExtended;
else
fingerprint->encoding = kPPM; /* probably... */
break;
default:
fingerprint->encoding = kPPM;
break;
}
break;
case 2: /* two widths of burst, either 'markvaries' or 'biphase' */
switch (fingerprint->space->count)
{
case 1:
/* could be ambiguous, if there's only one count of the larger mark
RC-5 '0' digit, for example */
if (fingerprint->mark->d[1].count == 1)
fingerprint->encoding = kAmbiguous;
else
fingerprint->encoding = kMarkVaries;
break;
case 2:
fingerprint->encoding = kBiphase;
break;
case 3:
if (fingerprint->space->d[2].count == 1)
fingerprint->encoding = kBiphaseExtended;
else
fingerprint->encoding = kUnknown;
break;
default:
/* don't know what the heck this is */
fingerprint->encoding = kUnknown;
break;
}
break;
case 3:
if (fingerprint->mark->d[2].count == 1)
fingerprint->encoding = kBiphaseExtended;
else
fingerprint->encoding = kUnknown;
break;
default: /* don't know what the heck this is, perhaps an analysis problem */
fingerprint->encoding = kUnknown;
break;
}
/* estimate the number of symbols, based on encoding */
/* inc if the leading pair is not AGC, therefore a valid symbol (probably) */
fingerprint->symbolCount = 0;
hist = NULL;
switch (fingerprint->encoding)
{
case kMarkVaries:
case kMarkVariesExtended:
hist = fingerprint->mark;
break;
case kAmbiguous: /* works for ambiguous cases of kSpaceVaries and kPPM. kBiphase only if all ones or zeros. */
case kSpaceVaries:
case kSpaceVariesExtended:
case kPPM:
hist = fingerprint->space;
break;
case kBiphase:
case kBiphaseExtended:
fingerprint->symbolCount = fingerprint->mark->d[0].count;
fingerprint->symbolCount += fingerprint->mark->d[1].count * 2;
fingerprint->symbolCount += fingerprint->space->d[0].count;
fingerprint->symbolCount += fingerprint->space->d[1].count * 2;
fingerprint->symbolCount /= 2;
break;
default:
break;
}
if (hist != NULL)
{
for (i = 0; i < hist->count; ++i)
{
fingerprint->symbolCount += hist->d[i].count;
}
}
}
unsigned long normalizeFromReference( unsigned long *period, tReferenceHistogram refhist)
{
int i;
for (i = 0; i < 4; ++i)
{
if (refhist[i] == 0)
break;
if ( fuzzyMatch(refhist[i], *period, 250) )
{
*period = refhist[i];
return *period;
}
}
return 0;
}
void adjustIRStream(tIRStream *stream, tFingerprint * UNUSED(fingerprint), tReferenceFingerprint *refprint, unsigned int *missed)
{
unsigned long intracodeGap;
unsigned long *period;
tCount count;
count = stream->count;
period = &stream->period[0];
if ( (count & 1) == 1 )
{
logError("Internal error: IRStream count must be even");
dumpStream(stream);
--count;
}
if (refprint != NULL)
{
intracodeGap = refprint->duration;
if (count >= 2)
{
if (refprint->leading.mark != 0)
{
*period = refprint->leading.mark;
}
else {
if (normalizeFromReference( period, refprint->mark) == 0)
{
logDebug(0, "leading mark period %lu didn't normalize", *period);
++(*missed);
}
}
intracodeGap -= *period++;
--count;
if (refprint->leading.space != 0)
{
*period = refprint->leading.space;
}
else {
if (normalizeFromReference( period, refprint->space) == 0)
{
logDebug(0, "leading space period %lu didn't normalize", *period);
++(*missed);
}
}
intracodeGap -= *period++;
--count;
}
while (count > 0)
{
if (normalizeFromReference( period, refprint->mark) == 0)
{
logDebug(0, "mark period %lu didn't normalize", *period);
++(*missed);
}
intracodeGap -= *period++;
--count;
if (count == 1)
{
*period++ = intracodeGap;
}
else {
if (normalizeFromReference( period, refprint->space) == 0)
{
logDebug(0, "space period %lu didn't normalize", *period);
++(*missed);
}
intracodeGap -= *period++;
}
--count;
}
}
}
void adjustIRCode(tIRCode *code)
{
tFingerprint *fingerprint = &code->fingerprint;
tReferenceFingerprint *refprint = fingerprint->protocol;
unsigned int missed;
missed = 0;
adjustIRStream( code->first.a, fingerprint, refprint, &missed );
if ( code->first.b != NULL )
adjustIRStream( code->first.b, fingerprint, refprint, &missed );
if (refprint != NULL)
{
switch (refprint->repeatStream)
{
case kUnknownRepeatStream:
if ( code->repeat.a != NULL )
adjustIRStream( code->repeat.a, fingerprint, refprint, &missed );
if ( code->repeat.b != NULL )
adjustIRStream( code->repeat.b, fingerprint, refprint, &missed );
break;
default:
if (code->repeat.a != NULL)
free( code->repeat.a );
code->repeat.a = (tIRStream *)&gRepeatStream[refprint->repeatStream];
if (code->repeat.b != NULL)
{
free( code->repeat.b );
code->repeat.b = NULL;
}
break;
}
fingerprint->carrierFreq = refprint->carrierFreq;
}
if (missed != 0)
logWarning("%u periods didn't normalize (on line %d)", missed, code->lineNumber );
}
void analyzeIRCode(tIRCode *code)
{
tFingerprint *fingerprint;
tIRStream *stream = NULL;
if (code == NULL) return;
fingerprint = &code->fingerprint;
if (code->first.a != NULL)
{
stream = code->first.a;
analyzeIRStream(code->first.a, fingerprint);
}
fingerprint->protocol = identifyProtocol( fingerprint );
if (fingerprint->protocol != NULL)
{
logDebug( 1, "Set %d (%s %s) - %s - protocol: %s",
code->parent->id,
gBrandName[code->parent->brand],
gDeviceTypeName[code->parent->deviceType],
code->button.label, fingerprint->protocol->name );
if (logDebugEnabled(2))
{
dumpIRCode(code);
}
switch (fingerprint->protocol->confidence)
{
case kFromSpec:
case kMeasured:
adjustIRCode(code);
if (logDebugEnabled(2))
{
logDebug(2, "######## After Adjustment ########");
logDebug(2, "carrier %lu", fingerprint->carrierFreq );
dumpIRStreams( code );
}
break;
default:
break;
}
}
else {
logError( "Set %d (%s %s) - %s - ### protocol not identified ###",
code->parent->id,
gBrandName[code->parent->brand],
gDeviceTypeName[code->parent->deviceType],
code->button.label );
if (logDebugEnabled(0))
{
dumpIRCode(code);
}
}
}
void analyzeIRCodeSets(void)
{
tIRCodeSet *codeSet;
tIRCode *code;
codeSet = gIRCodeSets;
while (codeSet != NULL)
{
logDebug(1, "Set %d (%s %s)",
codeSet->id,
gBrandName[codeSet->brand],
gDeviceTypeName[codeSet->deviceType] );
code = codeSet->irCodes;
while (code != NULL)
{
analyzeIRCode(code);
code = code->next;
}
codeSet = codeSet->next;
}
dumpFingerprintStats();
}