Mercurial > hg > dmlib
comparison tests/encbr1test.c @ 2051:c67d863384a5
Changes to the BR1 encoder test.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 03 Dec 2018 19:02:29 +0200 |
parents | 210a0041081b |
children | d5ea82da40ab |
comparison
equal
deleted
inserted
replaced
2050:416af5a842ec | 2051:c67d863384a5 |
---|---|
1 #include "dmtool.h" | 1 #include "dmtool.h" |
2 #include "dmlib.h" | 2 #include "dmlib.h" |
3 #include "dmres.h" | 3 #include "dmres.h" |
4 | 4 #include "dmmutex.h" |
5 | 5 |
6 #define BR_DEBUG | 6 |
7 #define BR_DEBUG 1 | |
7 //#define BR_WRITE | 8 //#define BR_WRITE |
8 | 9 |
9 enum | 10 enum |
10 { | 11 { |
11 DMODE_LIT, | 12 DMODE_LIT, |
12 DMODE_RLE, | 13 DMODE_RLE, |
13 }; | 14 }; |
14 | 15 |
15 | 16 |
16 BOOL dmIFFEncodeByteRun1Flush( | 17 #if BR_DEBUG == 1 |
17 DMResource *fp, const int mode, const BOOL flush, | 18 static int mcount = 0; |
18 size_t *l_offs, const size_t offs, const Uint8 *buf, | 19 static int moffs = 0; |
19 const Uint8 data, unsigned int *r_count) | 20 |
20 { | 21 static void mprintbyte(const Uint8 data) |
21 #ifdef BR_DEBUG | 22 { |
22 if (flush) | 23 if (mcount == 0) printf("%04x | ", moffs); |
23 printf("FLUSH:\n"); | 24 printf("%02x ", data); |
24 #endif | 25 if (mcount++ >= 15) { printf("\n"); mcount = 0; } |
25 | 26 moffs++; |
26 if (mode == DMODE_LIT) | 27 } |
27 { | 28 #endif |
28 size_t l_count = offs - *l_offs; | 29 |
29 if (l_count > *r_count || flush) | 30 |
30 { | 31 BOOL dmIFFEncodeByteRun1LIT(DMResource *fp, |
31 size_t count = l_count - *r_count; | 32 const Uint8 *buf, const size_t offs, |
32 Uint8 tmp = count - 1; | 33 const size_t count) |
33 | 34 { |
34 #ifdef BR_DEBUG | 35 if (count <= 0) |
35 printf("L: "); | 36 return TRUE; |
36 for (size_t n = 0; n < count; n++) | 37 |
37 printf("%02x ", buf[*l_offs + n]); | 38 Uint8 tmp = count - 1; |
38 printf(" [%" DM_PRIu_SIZE_T " -> %d]\n", count, tmp); | 39 |
39 #endif | 40 #if BR_DEBUG == 2 |
40 | 41 printf("L: %02x ", tmp); |
41 #ifdef BR_WRITE | 42 for (size_t n = 0; n < count; n++) |
42 if (!dmf_write_byte(fp, tmp) || | 43 printf("%02x ", buf[offs + n]); |
43 !dmf_write_str(fp, buf + *l_offs, count)) | 44 printf("[%" DM_PRIu_SIZE_T "]\n", count); |
44 return FALSE; | 45 #elif BR_DEBUG == 1 |
45 #endif | 46 mprintbyte(tmp); |
46 } | 47 for (size_t n = 0; n < count; n++) |
47 (*r_count)++; | 48 mprintbyte(buf[offs + n]); |
48 } | 49 #endif |
49 else | 50 |
50 { | 51 #ifdef BR_WRITE |
51 if (*r_count > 0) | 52 if (!dmf_write_byte(fp, tmp) || |
52 { | 53 !dmf_write_str(fp, buf + offs, count)) |
53 unsigned int count = *r_count; | 54 return FALSE; |
54 Uint8 tmp = ((Uint8) count - 2) ^ 0xff; | 55 #endif |
55 | |
56 #ifdef BR_DEBUG | |
57 printf("R: %02x x %x [%02x]\n", data, count, tmp); | |
58 #endif | |
59 #ifdef BR_WRITE | |
60 if (!dmf_write_byte(fp, tmp) || | |
61 !dmf_write_byte(fp, data)) | |
62 return FALSE; | |
63 #endif | |
64 *r_count = 0; | |
65 } | |
66 *l_offs = offs; | |
67 } | |
68 | 56 |
69 return TRUE; | 57 return TRUE; |
70 } | 58 } |
71 | 59 |
72 | 60 |
61 BOOL dmIFFEncodeByteRun1RLE(DMResource *fp, | |
62 const Uint8 *buf, const size_t offs, | |
63 const size_t count) | |
64 { | |
65 if (count <= 0) | |
66 return TRUE; | |
67 | |
68 Uint8 tmp = ((Uint8) count - 2) ^ 0xff; | |
69 Uint8 data = buf[offs]; | |
70 | |
71 #if BR_DEBUG == 2 | |
72 printf("R: %02x %02x [%" DM_PRIu_SIZE_T "]\n", tmp, data, count); | |
73 #elif BR_DEBUG == 1 | |
74 mprintbyte(tmp); | |
75 mprintbyte(data); | |
76 #endif | |
77 #ifdef BR_WRITE | |
78 if (!dmf_write_byte(fp, tmp) || | |
79 !dmf_write_byte(fp, data)) | |
80 return FALSE; | |
81 #endif | |
82 | |
83 return TRUE; | |
84 } | |
85 | |
86 | |
73 BOOL dmIFFEncodeByteRun1Row(DMResource *fp, const Uint8 *buf, const size_t bufLen) | 87 BOOL dmIFFEncodeByteRun1Row(DMResource *fp, const Uint8 *buf, const size_t bufLen) |
74 { | 88 { |
75 unsigned int r_count = 0; | |
76 int prev = -1, mode = DMODE_LIT; | 89 int prev = -1, mode = DMODE_LIT; |
77 size_t offs, l_offs = 0; | 90 size_t offs, l_offs, r_offs; |
78 | 91 BOOL ret = TRUE; |
79 #ifdef BR_DEBUG | 92 |
80 printf("\n\nByteRUN1ROW: %" DM_PRIu_SIZE_T " = $%" DM_PRIx_SIZE_T "\n", bufLen, bufLen); | 93 #if BR_DEBUG == 1 |
81 #endif | 94 mcount = 0; |
82 for (offs = 0; offs < bufLen; offs++) | 95 #endif |
96 | |
97 for (offs = l_offs = r_offs = 0; offs < bufLen; offs++) | |
83 { | 98 { |
84 Uint8 data = buf[offs]; | 99 Uint8 data = buf[offs]; |
85 int next_mode; | 100 BOOL flush = FALSE; |
86 BOOL flush; | 101 int pmode = mode; |
87 | 102 |
88 if (data == prev) | 103 if (data == prev) |
89 { | 104 { |
90 r_count++; | 105 if (mode == DMODE_LIT && |
91 next_mode = DMODE_RLE; | 106 offs - r_offs >= 2) |
107 { | |
108 ret = dmIFFEncodeByteRun1LIT(fp, buf, l_offs, r_offs - l_offs); | |
109 mode = DMODE_RLE; | |
110 } | |
92 } | 111 } |
93 else | 112 else |
94 { | 113 { |
95 next_mode = DMODE_LIT; | 114 if (mode != DMODE_LIT) |
96 } | 115 { |
97 | 116 ret = dmIFFEncodeByteRun1RLE(fp, buf, r_offs, offs - r_offs); |
98 //#ifdef BR_DEBUG | 117 mode = DMODE_LIT; |
99 #if 0 | 118 l_offs = offs; |
100 printf("%08" DM_PRIx_SIZE_T ": %02x | r_count=%x, l_count=%" DM_PRIx_SIZE_T ", mode=%s [%08" DM_PRIx_SIZE_T "]\n", | 119 } |
101 offs, data, r_count, offs - l_offs + 1, mode == DMODE_RLE ? "RLE" : "LIT", l_offs); | 120 r_offs = offs; |
102 #endif | 121 } |
103 | 122 |
104 flush = offs - l_offs >= 128 || r_count >= 128; | 123 if (!ret) |
105 if ((next_mode != mode || flush) && | 124 goto out; |
106 !dmIFFEncodeByteRun1Flush(fp, mode, flush, &l_offs, offs, buf, prev, &r_count)) | 125 |
107 return FALSE; | 126 #if BR_DEBUG == 2 |
108 | 127 printf("%04" DM_PRIx_SIZE_T ": %02x | r_count=%" DM_PRIu_SIZE_T ", l_count=%" DM_PRIu_SIZE_T ", mode=%s | [%04" DM_PRIx_SIZE_T ", %04" DM_PRIx_SIZE_T "]", |
109 mode = next_mode; | 128 offs, data, offs - r_offs, offs - l_offs, |
129 mode == DMODE_RLE ? "RLE" : "LIT", | |
130 r_offs, l_offs); | |
131 #endif | |
132 | |
133 // NOTE! RLE max is 128, checked against DP2e | |
134 // Not sure about LIT max yet | |
135 if ((pmode == DMODE_RLE && offs - r_offs >= 128) || | |
136 (pmode == DMODE_LIT && offs - l_offs >= 128)) | |
137 { | |
138 flush = TRUE; | |
139 #if BR_DEBUG == 2 | |
140 printf(" <LEN FLUSH>"); | |
141 #endif | |
142 } | |
143 | |
144 // Check for last byte of input | |
145 if (offs == bufLen - 1) | |
146 { | |
147 offs++; | |
148 flush = TRUE; | |
149 pmode = mode; | |
150 #if BR_DEBUG == 2 | |
151 printf(" <FINAL FLUSH>"); | |
152 #endif | |
153 } | |
154 | |
155 #if BR_DEBUG == 2 | |
156 printf("\n"); | |
157 #endif | |
158 | |
159 if (flush) | |
160 { | |
161 if (pmode == DMODE_RLE) | |
162 ret = dmIFFEncodeByteRun1RLE(fp, buf, r_offs, offs - r_offs); | |
163 else | |
164 ret = dmIFFEncodeByteRun1LIT(fp, buf, l_offs, offs - l_offs); | |
165 | |
166 r_offs = l_offs = offs; | |
167 mode = DMODE_LIT; | |
168 | |
169 if (!ret) | |
170 goto out; | |
171 } | |
172 | |
110 prev = data; | 173 prev = data; |
111 } | 174 } |
112 | 175 |
113 #ifdef BR_DEBUG | 176 out: |
114 printf("END\n"); | 177 return ret; |
115 #endif | |
116 | |
117 if (!dmIFFEncodeByteRun1Flush(fp, mode, TRUE, &l_offs, offs, buf, prev, &r_count)) | |
118 return FALSE; | |
119 | |
120 return TRUE; | |
121 } | 178 } |
122 | 179 |
123 | 180 |
124 | 181 |
125 int main(int argc, char *argv[]) | 182 int main(int argc, char *argv[]) |
126 { | 183 { |
184 DMResource *fp = NULL; | |
127 (void) argc; | 185 (void) argc; |
128 (void) argv; | 186 (void) argv; |
129 | 187 |
130 Uint8 test[] = | 188 #ifdef BR_WRITE |
189 dmf_open_stdio_stream(stderr, &fp); | |
190 #endif | |
191 | |
192 static const Uint8 test[] = | |
131 { | 193 { |
132 1, | 194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
133 0, 0, 0, | 195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
134 2, | 196 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
135 0, 0, 0, 0, | 197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
136 3, 3, 3, | 198 |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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
152 | 213 |
153 // 15, | 214 1, 3, |
154 | 215 |
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 216 0, 0, 0, 1, |
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
159 | |
160 1 | |
161 //0, 0, 0, 0, | |
162 //1 | |
163 | |
164 }; | 217 }; |
165 | 218 |
166 dmIFFEncodeByteRun1Row(NULL, test, sizeof(test)); | 219 { |
220 int cnt = 0; | |
221 for (size_t xc = 0; xc < sizeof(test); xc++) | |
222 { | |
223 if (cnt == 0) printf("%04x | ", xc); | |
224 printf("%02x ", *(test + xc)); | |
225 if (cnt++ >= 15) { printf("\n"); cnt = 0; } | |
226 } | |
227 printf("\n\n"); | |
228 | |
229 dmIFFEncodeByteRun1Row(fp, test, sizeof(test)); | |
230 printf("\n--\n"); | |
231 } | |
232 | |
233 #ifdef BR_WRITE | |
234 dmf_close(fp); | |
235 #endif | |
236 | |
167 return 0; | 237 return 0; |
168 } | 238 } |