-
Notifications
You must be signed in to change notification settings - Fork 38
/
fourbsixb.c
71 lines (62 loc) · 2.32 KB
/
fourbsixb.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
#include "fourbsixb.h"
//fileprivate let codesRev:Dictionary<Int, UInt8> = [21: 0, 49: 1, 50: 2, 35: 3, 52: 4, 37: 5, 38: 6, 22: 7, 26: 8, 25: 9, 42: 10, 11: 11, 44: 12, 13: 13, 14: 14, 28: 15]
static uint8_t __xdata codes[] = {21,49,50,35,52,37,38,22,26,25,42,11,44,13,14,28};
void fourbsixb_init_encoder(EncoderState *state) {
state->fourbsixb.acc = 0;
state->fourbsixb.bits_avail = 0;
}
void fourbsixb_add_raw_byte(EncoderState *state, uint8_t raw) __reentrant {
uint16_t new_bits = (codes[raw >> 4] << 6) | codes[raw & 0xf];
state->fourbsixb.acc = (state->fourbsixb.acc << 12) | new_bits;
state->fourbsixb.bits_avail += 12;
}
uint8_t fourbsixb_next_encoded_byte(EncoderState *state, uint8_t *encoded, bool flush) __reentrant {
if (state->fourbsixb.bits_avail < 8 && !flush) {
return 0;
}
if (state->fourbsixb.bits_avail == 0) {
return 0;
}
if (flush && state->fourbsixb.bits_avail < 8) {
*encoded = state->fourbsixb.acc << (8-state->fourbsixb.bits_avail);
state->fourbsixb.bits_avail = 0;
} else {
*encoded = state->fourbsixb.acc >> (state->fourbsixb.bits_avail-8);
state->fourbsixb.bits_avail -= 8;
}
return 1;
}
void fourbsixb_init_decoder(DecoderState *state) {
state->fourbsixb.input_acc = 0;
state->fourbsixb.input_bits_avail = 0;
state->fourbsixb.output_acc = 0;
state->fourbsixb.output_bits_avail = 0;
}
uint8_t fourbsixb_add_encoded_byte(DecoderState *state, uint8_t encoded) __reentrant {
uint8_t code, i;
state->fourbsixb.input_acc = (state->fourbsixb.input_acc << 8) | encoded;
state->fourbsixb.input_bits_avail += 8;
while (state->fourbsixb.input_bits_avail >= 6) {
code = (state->fourbsixb.input_acc >> (state->fourbsixb.input_bits_avail - 6)) & 0b111111;
state->fourbsixb.input_bits_avail -= 6;
for (i=0; i<16; i++) {
if (codes[i] == code) {
break;
}
}
if (i == 16) {
return 1; // Encoding error
}
state->fourbsixb.output_acc = (state->fourbsixb.output_acc << 4) | i;
state->fourbsixb.output_bits_avail += 4;
}
return 0;
}
uint8_t fourbsixb_next_decoded_byte(DecoderState *state, uint8_t *decoded) __reentrant {
if (state->fourbsixb.output_bits_avail < 8) {
return 0;
}
*decoded = state->fourbsixb.output_acc >> (state->fourbsixb.output_bits_avail - 8);
state->fourbsixb.output_bits_avail -= 8;
return 1;
}