Debian Codegraph @

AudioEncode/aencode.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <cassert>
20 #include <cmath>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24
25 #include "aencode.h"
26
27 struct BitsEncoder {
28 FILE *m_outFile; // var50 | offset 0x10
29 uint32_t m_bitData; // var4C | offset 0x14
30 uint32_t m_bitCount; // var48 | offset 0x18
31
32 void WriteBits(int32_t val, uint32_t numBits) {
33 assert((numBits + m_bitCount) <= 32);
34 m_bitData |= static_cast<uint32_t>(val << m_bitCount);
35 m_bitCount += numBits;
36
37 while (m_bitCount >= 8) {
38 uint8_t v = m_bitData & 0xFF;
39 putc(v, m_outFile);
40
41 m_bitData >>= 8;
42 m_bitCount -= 8;
43 }
44 }
45
46 void Flush() {
47 while (m_bitCount >= 8) {
48 uint8_t v = m_bitData & 0xFF;
49 putc(v, m_outFile);
50
51 m_bitData >>= 8;
52 m_bitCount -= 8;
53 }
54
55 if (m_bitCount > 0) {
56 uint8_t v = m_bitData & 0xFF;
57 putc(v, m_outFile);
58 m_bitCount = 0;
59 m_bitData = 0;
60 }
61 }
62 };
63
64 struct Encoder {
65 ReadSampleFunction *m_reader; // var60 | offset 0x00
66 void *m_pReaderData; // var5C | offset 0x04
67 uint32_t m_sampleCount; // var58 | offset 0x08
68 float m_volume; // var54 | offset 0x0C
69 BitsEncoder m_bits; // var50 - var48 | offset 0x10 - 0x18
70 int8_t m_levels; // var44* | offset 0x1C
71 int8_t m_pad[3]; // 43, 42, 41
72 int32_t m_numColumns; // var40 | offset 0x20
73 int32_t m_samples_per_subband; // var3C | offset 0x24
74 int32_t m_samplesPerBlock; // var38 | offset 0x28
75 int32_t m_adjustedSamplesTimeNumColumns; // var34 | offset 0x2C
76 float **m_levelSlots; // var30 | offset 0x30
77 float *m_pCurrBlockData; // var2C | offset 0x34
78 int32_t m_blockSamplesRemaining; // var28 | offset 0x38
79 int32_t m_bandWriteEnabled; // var24 | offset 0x3C
80 int32_t m_finishedReading; // var20 | offset 0x40
81 int32_t m_someVal; // var1C | offset 0x44
82 float *m_lo_filter; // var18 | offset 0x48
83 float *m_hi_filter; // var14 | offset 0x4C
84 uint32_t *m_pFormatIdPerColumn; // var10 | offset 0x50
85 int32_t m_currBlockBitPower; // var0C | offset 0x54
86 int32_t m_currBlockBitValue; // var08 | offset 0x58
87 int32_t m_threshold; // var04 | offset 0x5C
88 };
89
90 typedef void (*WriteBandFunc)(Encoder &enc, int32_t colIndex, uint32_t packerId);
91 void WriteBand_Fmt0(Encoder &enc, int32_t colIndex, uint32_t packerId);
92 void WriteBand_Fmt3_16(Encoder &enc, int32_t colIndex, uint32_t packerId);
93 void WriteBand_Fmt17(Encoder &enc, int32_t colIndex, uint32_t packerId);
94 void WriteBand_Fmt18(Encoder &enc, int32_t colIndex, uint32_t packerId);
95 void WriteBand_Fmt19(Encoder &enc, int32_t colIndex, uint32_t packerId);
96 void WriteBand_Fmt20(Encoder &enc, int32_t colIndex, uint32_t packerId);
97 void WriteBand_Fmt21(Encoder &enc, int32_t colIndex, uint32_t packerId);
98 void WriteBand_Fmt22(Encoder &enc, int32_t colIndex, uint32_t packerId);
99 void WriteBand_Fmt23(Encoder &enc, int32_t colIndex, uint32_t packerId);
100 void WriteBand_Fmt24(Encoder &enc, int32_t colIndex, uint32_t packerId);
101 void WriteBand_Fmt26(Encoder &enc, int32_t colIndex, uint32_t packerId);
102 void WriteBand_Fmt27(Encoder &enc, int32_t colIndex, uint32_t packerId);
103 void WriteBand_Fmt29(Encoder &enc, int32_t colIndex, uint32_t packerId);
104
105 WriteBandFunc WriteBand_tbl[] = {WriteBand_Fmt0,
106 NULL,
107 NULL,
108 WriteBand_Fmt3_16,
109 WriteBand_Fmt3_16,
110 WriteBand_Fmt3_16,
111 WriteBand_Fmt3_16,
112 WriteBand_Fmt3_16,
113 WriteBand_Fmt3_16,
114 WriteBand_Fmt3_16,
115 WriteBand_Fmt3_16,
116 WriteBand_Fmt3_16,
117 WriteBand_Fmt3_16,
118 WriteBand_Fmt3_16,
119 WriteBand_Fmt3_16,
120 WriteBand_Fmt3_16,
121 WriteBand_Fmt3_16,
122 WriteBand_Fmt17,
123 WriteBand_Fmt18,
124 WriteBand_Fmt19,
125 WriteBand_Fmt20,
126 WriteBand_Fmt21,
127 WriteBand_Fmt22,
128 WriteBand_Fmt23,
129 WriteBand_Fmt24,
130 NULL,
131 WriteBand_Fmt26,
132 WriteBand_Fmt27,
133 NULL,
134 WriteBand_Fmt29,
135 NULL,
136 NULL};
137
138 float std_lo_filter[] = {
139 -0.0012475221f, -0.0024950907f, 0.0087309526f, 0.019957958f, -0.050528999f, -0.12055097f, 0.29304558f, 0.70617616f,
140 };
141
142 float std_hi_filter[] = {
143 0.0012475221f, -0.0024950907f, -0.0087309526f, 0.019957958f, 0.050528999f, -0.12055097f, -0.29304558f, 0.70617616f,
144 };
145
146 const float T911 = -32767.0f;
147 const float T913 = 32767.0f;
148 const float T1266 = 0.0f;
149
150 void WriteBand_Fmt0(Encoder &enc, int32_t colIndex, uint32_t formatId) {}
151
152 void WriteBand_Fmt3_16(Encoder &enc, int32_t colIndex, uint32_t formatId) {
153 const float currBlockValue = float(enc.m_currBlockBitValue);
154 const float halfCurrBlockValue = currBlockValue * 0.5f;
155 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockValue);
156 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockValue);
157
158 float *pColumnData = &enc.m_levelSlots[enc.m_levels][colIndex];
159 for (int32_t i = 0; i < enc.m_samples_per_subband; ++i) {
160 int32_t val = (int32_t)floorf((*pColumnData + halfCurrBlockValue) / currBlockValue);
161 if (minValue > val) {
162 val = minValue;
163 } else if (val > maxValue) {
164 val = maxValue;
165 }
166
167 pColumnData += enc.m_numColumns;
168 enc.m_bits.WriteBits(val + (1 << (formatId - 1)), formatId);
169 }
170 }
171
172 void WriteBand_Fmt17(Encoder &enc, int32_t colIndex, uint32_t formatId) {
173 const float currBitValue = static_cast<float>(enc.m_currBlockBitValue);
174 const float halfCurrBitValue = currBitValue * 0.5f;
175 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBitValue);
176 const int32_t maxValue = (int32_t)floorf(32767.0f / currBitValue);
177
178 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
179 int32_t currSampleIndex = enc.m_samples_per_subband;
180 while (currSampleIndex) {
181 --currSampleIndex;
182 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBitValue) / currBitValue);
183 if (minValue > val) {
184 val = minValue;
185 } else if (maxValue < val) {
186 val = maxValue;
187 }
188
189 pCurrSample += enc.m_numColumns;
190 if (val == 0) {
191 if (currSampleIndex != 0 && !(int)floorf((*pCurrSample + halfCurrBitValue) / currBitValue)) {
192 enc.m_bits.WriteBits(0, 1);
193
194 if (currSampleIndex == 0)
195 return;
196 --currSampleIndex;
197 pCurrSample += enc.m_numColumns;
198 continue;
199 }
200
201 enc.m_bits.WriteBits(1, 2);
202 continue;
203 }
204
205 enc.m_bits.WriteBits(3, 2);
206 enc.m_bits.WriteBits((val == 1) ? 1 : 0, 1);
207 }
208 }
209
210 void WriteBand_Fmt18(Encoder &enc, int32_t colIndex, uint32_t formatId) {
211 const float currBlockBitValue = static_cast<float>(enc.m_currBlockBitValue);
212 const float halfCurrBlockBitValue = enc.m_currBlockBitValue * 0.5f;
213 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockBitValue);
214 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockBitValue);
215
216 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
217 int32_t currSampleIndex = enc.m_samples_per_subband;
218 while (currSampleIndex) {
219 --currSampleIndex;
220 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBlockBitValue) / currBlockBitValue);
221 if (minValue > val) {
222 val = minValue;
223 } else if (val > maxValue) {
224 val = maxValue;
225 }
226
227 pCurrSample += enc.m_numColumns;
228
229 if (!val) {
230 enc.m_bits.WriteBits(0, 1);
231 continue;
232 }
233
234 enc.m_bits.WriteBits(1, 1);
235 enc.m_bits.WriteBits((val == 1) ? 1 : 0, 1);
236 }
237 }
238
239 void WriteBand_Fmt19(Encoder &enc, int32_t colIndex, uint32_t formatId) {
240 const float currBlockBitValue = static_cast<float>(enc.m_currBlockBitValue);
241 const float halfCurrBlockBitValue = currBlockBitValue * 0.5f;
242 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockBitValue);
243 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockBitValue);
244
245 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
246 int32_t currSampleIndex = enc.m_samples_per_subband;
247 while (currSampleIndex) {
248 --currSampleIndex;
249 int32_t workingVal = (int32_t)floorf((*pCurrSample + halfCurrBlockBitValue) / currBlockBitValue);
250 if (minValue > workingVal) {
251 workingVal = minValue;
252 } else if (maxValue < workingVal) {
253 workingVal = maxValue;
254 }
255 pCurrSample += enc.m_numColumns;
256 int32_t baseValue = workingVal + 1;
257 if (currSampleIndex) {
258 --currSampleIndex;
259 workingVal = (int32_t)floorf((*pCurrSample + halfCurrBlockBitValue) / currBlockBitValue);
260 if (minValue > workingVal) {
261 workingVal = minValue;
262 } else if (maxValue < workingVal) {
263 workingVal = maxValue;
264 }
265 pCurrSample += enc.m_numColumns;
266 } else {
267 workingVal = 0;
268 }
269
270 baseValue += workingVal * 3 + 3;
271 if (currSampleIndex) {
272 --currSampleIndex;
273 workingVal = (int32_t)floorf((*pCurrSample + halfCurrBlockBitValue) / currBlockBitValue);
274 if (minValue > workingVal) {
275 workingVal = minValue;
276 } else if (maxValue < workingVal) {
277 workingVal = maxValue;
278 }
279 pCurrSample += enc.m_numColumns;
280 } else {
281 workingVal = 0;
282 }
283
284 enc.m_bits.WriteBits(workingVal * 9 + 9 + baseValue, 5);
285 }
286 }
287
288 void WriteBand_Fmt20(Encoder &enc, int32_t colIndex, uint32_t formatId) {
289 const float currBlockValue = static_cast<float>(enc.m_currBlockBitValue);
290 const float halfCurrBlockValue = currBlockValue * 0.5f;
291 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockValue);
292 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockValue);
293
294 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
295 int32_t currSampleIndex = enc.m_samples_per_subband;
296 while (currSampleIndex) {
297 --currSampleIndex;
298 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
299 if (minValue > val) {
300 val = minValue;
301 } else if (maxValue < val) {
302 val = maxValue;
303 }
304
305 pCurrSample += enc.m_numColumns;
306 if (val == 0) {
307 if (currSampleIndex != 0 && !(int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue)) {
308 enc.m_bits.WriteBits(0, 1);
309
310 if (currSampleIndex == 0)
311 return;
312 --currSampleIndex;
313 pCurrSample += enc.m_numColumns;
314 continue;
315 }
316 enc.m_bits.WriteBits(1, 2);
317 continue;
318 }
319 enc.m_bits.WriteBits(3, 2);
320 if (val < 0) {
321 val += 2;
322 } else {
323 ++val;
324 }
325
326 enc.m_bits.WriteBits(val, 2);
327 }
328 }
329
330 void WriteBand_Fmt21(Encoder &enc, int32_t colIndex, uint32_t formatId) {
331 const float currBlockValue = static_cast<float>(enc.m_currBlockBitValue);
332 const float halfCurrBlockValue = currBlockValue * 0.5f;
333 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockValue);
334 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockValue);
335
336 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
337 int32_t currSampleIndex = enc.m_samples_per_subband;
338 while (currSampleIndex) {
339 --currSampleIndex;
340 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
341 if (minValue > val) {
342 val = minValue;
343 } else if (maxValue < val) {
344 val = maxValue;
345 }
346
347 pCurrSample += enc.m_numColumns;
348 if (!val) {
349 enc.m_bits.WriteBits(0, 1);
350 continue;
351 }
352 enc.m_bits.WriteBits(1, 1);
353
354 if (val < 0) {
355 val += 2;
356 } else {
357 ++val;
358 }
359
360 enc.m_bits.WriteBits(val, 2);
361 }
362 }
363
364 void WriteBand_Fmt22(Encoder &enc, int32_t colIndex, uint32_t formatId) {
365 const float currBlockValue = static_cast<float>(enc.m_currBlockBitValue);
366 const float halfCurrBlockValue = currBlockValue * 0.5f;
367 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockValue);
368 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockValue);
369
370 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
371
372 int32_t currSampleIndex = enc.m_samples_per_subband;
373 while (currSampleIndex) {
374 --currSampleIndex;
375 int32_t workingVal = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
376 if (minValue > workingVal) {
377 workingVal = minValue;
378 } else if (maxValue < workingVal) {
379 workingVal = maxValue;
380 }
381
382 int32_t baseValue = workingVal + 2;
383 pCurrSample += enc.m_numColumns;
384 if (currSampleIndex) {
385 --currSampleIndex;
386 workingVal = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
387 if (minValue > workingVal) {
388 workingVal = minValue;
389 } else if (maxValue < workingVal) {
390 workingVal = maxValue;
391 }
392 pCurrSample += enc.m_numColumns;
393 } else {
394 workingVal = 0;
395 }
396
397 baseValue += workingVal * 5 + 10;
398 if (currSampleIndex) {
399 --currSampleIndex;
400 workingVal = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
401 if (minValue > workingVal) {
402 workingVal = minValue;
403 } else if (maxValue < workingVal) {
404 workingVal = maxValue;
405 }
406 pCurrSample += enc.m_numColumns;
407 } else {
408 workingVal = 0;
409 }
410
411 enc.m_bits.WriteBits(workingVal * 25 + 50 + baseValue, 7);
412 }
413 }
414
415 void WriteBand_Fmt23(Encoder &enc, int32_t colIndex, uint32_t formatId) {
416 const float currBlockValue = static_cast<float>(enc.m_currBlockBitValue);
417 const float halfCurrBlockValue = currBlockValue * 0.5f;
418 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockValue);
419 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockValue);
420
421 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
422 int32_t currSampleIndex = enc.m_samples_per_subband;
423 while (currSampleIndex) {
424 --currSampleIndex;
425
426 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
427 if (minValue > val) {
428 val = minValue;
429 } else if (maxValue < val) {
430 val = maxValue;
431 }
432
433 pCurrSample += enc.m_numColumns;
434 if (!val) {
435 if (currSampleIndex != 0) {
436 if (!(int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue)) {
437 enc.m_bits.WriteBits(0, 1);
438
439 if (currSampleIndex == 0)
440 return;
441 --currSampleIndex;
442
443 pCurrSample += enc.m_numColumns;
444 continue;
445 }
446 }
447
448 enc.m_bits.WriteBits(1, 2);
449 continue;
450 }
451
452 enc.m_bits.WriteBits(3, 2);
453
454 if (val != -1 && val != 1) {
455 enc.m_bits.WriteBits(1, 1);
456
457 if (val < 0) {
458 val += 3;
459 }
460
461 enc.m_bits.WriteBits(val, 2);
462 continue;
463 }
464
465 enc.m_bits.WriteBits(0, 1);
466 enc.m_bits.WriteBits((val == 1) ? 1 : 0, 1);
467 }
468 }
469
470 void WriteBand_Fmt24(Encoder &enc, int32_t colIndex, uint32_t formatId) {
471 const float currBlockBitValue = static_cast<float>(enc.m_currBlockBitValue);
472 const float halfCurrBlockBitValue = currBlockBitValue * 0.5f;
473 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockBitValue);
474 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockBitValue);
475
476 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
477 int32_t currSampleIndex = enc.m_samples_per_subband;
478 while (currSampleIndex) {
479 --currSampleIndex;
480 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBlockBitValue) / currBlockBitValue);
481 if (minValue > val) {
482 val = minValue;
483 } else if (maxValue < val) {
484 val = maxValue;
485 }
486
487 pCurrSample += enc.m_numColumns;
488 if (!val) {
489 enc.m_bits.WriteBits(0, 1);
490 continue;
491 }
492
493 enc.m_bits.WriteBits(1, 1);
494
495 if (val != -1 && val != 1) {
496 enc.m_bits.WriteBits(1, 1);
497
498 if (val < 0) {
499 val += 3;
500 }
501 enc.m_bits.WriteBits(val, 2);
502 continue;
503 }
504
505 enc.m_bits.WriteBits(0, 1);
506 enc.m_bits.WriteBits((val == 1) ? 1 : 0, 1);
507 }
508 }
509
510 void WriteBand_Fmt26(Encoder &enc, int32_t colIndex, uint32_t formatId) {
511 const float currBlockValue = static_cast<float>(enc.m_currBlockBitValue);
512 const float halfCurrBlockValue = currBlockValue * 0.5f;
513 const int32_t minValue = static_cast<int32_t>(ceilf(-32767.0f / currBlockValue));
514 const int32_t maxValue = static_cast<int32_t>(floorf(32767.0f / currBlockValue));
515 const float *pColumnData = &enc.m_levelSlots[enc.m_levels][colIndex];
516
517 int32_t currSampleIdx = enc.m_samples_per_subband;
518 while (currSampleIdx) {
519 --currSampleIdx;
520
521 int32_t val = (int32_t)floorf((*pColumnData + halfCurrBlockValue) / currBlockValue);
522 if (minValue > val) {
523 val = minValue;
524 } else if (maxValue < val) {
525 val = maxValue;
526 }
527
528 pColumnData += enc.m_numColumns;
529 if (!val) {
530 if (currSampleIdx) {
531 int32_t testVal = (int32_t)floorf((*pColumnData + halfCurrBlockValue) / currBlockValue);
532 if (!testVal) {
533 enc.m_bits.WriteBits(0, 1);
534
535 if (currSampleIdx == 0)
536 return;
537
538 --currSampleIdx;
539 pColumnData += enc.m_numColumns;
540 continue;
541 }
542 }
543
544 enc.m_bits.WriteBits(1, 2);
545 continue;
546 }
547
548 enc.m_bits.WriteBits(3, 2);
549
550 if (val >= 0) {
551 val += 3;
552 } else {
553 val += 4;
554 }
555
556 enc.m_bits.WriteBits(val, 3);
557 }
558 }
559
560 void WriteBand_Fmt27(Encoder &enc, int32_t colIndex, uint32_t formatId) {
561 const float currBlockValue = static_cast<float>(enc.m_currBlockBitValue);
562 const float halfCurrBlockValue = currBlockValue * 0.5f;
563 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBlockValue);
564 const int32_t maxValue = (int32_t)floorf(32767.0f / currBlockValue);
565
566 const float *pCurrSample = &enc.m_levelSlots[enc.m_levels][colIndex];
567 int32_t currSampleIndex = enc.m_samples_per_subband;
568 while (currSampleIndex) {
569 --currSampleIndex;
570 int32_t val = (int32_t)floorf((*pCurrSample + halfCurrBlockValue) / currBlockValue);
571 if (val < minValue) {
572 val = minValue;
573 } else if (maxValue < val) {
574 val = maxValue;
575 }
576
577 pCurrSample += enc.m_numColumns;
578 if (!val) {
579 enc.m_bits.WriteBits(0, 1);
580 continue;
581 }
582
583 enc.m_bits.WriteBits(1, 1);
584
585 if (val < 0) {
586 val += 4;
587 } else {
588 val += 3;
589 }
590
591 enc.m_bits.WriteBits(val, 3);
592 }
593 }
594
595 void WriteBand_Fmt29(Encoder &enc, int32_t colIndex, uint32_t formatId) {
596 const float currBitValue = (float)enc.m_currBlockBitValue;
597 const float halfCurrBitValue = currBitValue * 0.5f;
598 const int32_t minValue = (int32_t)ceilf(-32767.0f / currBitValue);
599 const int32_t maxValue = (int32_t)floorf(32767.0f / currBitValue);
600
601 const float *pColumnData = &enc.m_levelSlots[enc.m_levels][colIndex];
602 int32_t currSampleIndex = enc.m_samples_per_subband;
603 while (currSampleIndex) {
604 --currSampleIndex;
605
606 int32_t val = (int32_t)floorf((*pColumnData + halfCurrBitValue) / currBitValue);
607 if (minValue > val) {
608 val = minValue;
609 } else if (maxValue < val) {
610 val = maxValue;
611 }
612
613 int32_t baseValue = val + 5;
614 pColumnData += enc.m_numColumns;
615 if (currSampleIndex != 0) {
616 --currSampleIndex;
617
618 val = (int32_t)floorf((*pColumnData + halfCurrBitValue) / currBitValue);
619 if (minValue > val) {
620 val = minValue;
621 } else if (maxValue < val) {
622 val = maxValue;
623 }
624
625 pColumnData += enc.m_numColumns;
626 } else {
627 val = 0;
628 }
629
630 enc.m_bits.WriteBits(11 * val + 55 + baseValue, 7);
631 }
632 }
633
634 int ReadSample_init(Encoder &enc, ReadSampleFunction *read, void *data) {
635 enc.m_reader = read;
636 enc.m_pReaderData = data;
637 return 1;
638 }
639
640 int bits_init(BitsEncoder &bits, FILE *out) {
641 bits.m_outFile = out;
642 bits.m_bitData = 0;
643 bits.m_bitCount = 0;
644 return 1;
645 }
646
647 int SetupEncoder(Encoder &enc, int someVal, float std_lo_filter[], float std_hi_filter[], int8_t levels,
648 int samples_per_subband) {
649 enc.m_someVal = someVal;
650 enc.m_lo_filter = std_lo_filter;
651 enc.m_hi_filter = std_hi_filter;
652 enc.m_levels = levels;
653 enc.m_numColumns = 1 << levels;
654 enc.m_samples_per_subband = samples_per_subband;
655 enc.m_samplesPerBlock = samples_per_subband * enc.m_numColumns;
656
657 int adjustedSomeVal = (((someVal < -1) ? (someVal + 1) : (someVal)) + 1) >> 1;
658 enc.m_adjustedSamplesTimeNumColumns = adjustedSomeVal * (enc.m_numColumns - 1);
659 enc.m_levelSlots = reinterpret_cast<float **>(malloc(sizeof(float *) * (levels + 1)));
660 if (enc.m_levelSlots == NULL)
661 return 0;
662
663 if (levels >= 0) {
664 for (int8_t i = 0; i <= levels; ++i) {
665 int extraSamples = 0;
666 if (i != levels) {
667 extraSamples = (someVal - 1) << i;
668 }
669
670 float *blockData = reinterpret_cast<float *>(malloc((enc.m_samplesPerBlock + extraSamples) * sizeof(float)));
671 enc.m_levelSlots[i] = blockData;
672 if (blockData == NULL)
673 return 0;
674
675 memset(blockData, 0, (enc.m_samplesPerBlock + extraSamples) * sizeof(float));
676 enc.m_levelSlots[i] += extraSamples;
677 }
678 }
679
680 enc.m_pFormatIdPerColumn = reinterpret_cast<uint32_t *>(malloc(enc.m_numColumns * sizeof(uint32_t)));
681 if (enc.m_pFormatIdPerColumn == NULL)
682 return 0;
683
684 enc.m_sampleCount = 0;
685
686 int32_t edxOffset =
687 ((enc.m_samplesPerBlock * sizeof(float) * 25) - enc.m_adjustedSamplesTimeNumColumns) % enc.m_samplesPerBlock;
688 enc.m_pCurrBlockData = enc.m_levelSlots[0] + edxOffset;
689 enc.m_blockSamplesRemaining = enc.m_samplesPerBlock - edxOffset;
690 enc.m_bandWriteEnabled = 0;
691 enc.m_finishedReading = 0;
692 return 1;
693 }
694
695 void DestroyEncoder(Encoder &enc) {
696 if (enc.m_levelSlots != NULL) {
697 for (int i = 0; i <= enc.m_levels; ++i) {
698 if (enc.m_levelSlots[i] != 0) {
699 int extraSamples = 0;
700 if (enc.m_levels != i) {
701 extraSamples = (enc.m_someVal - 1) << i;
702 }
703
704 free(enc.m_levelSlots[i] - extraSamples);
705 }
706 }
707
708 free(enc.m_levelSlots);
709 }
710
711 if (enc.m_pFormatIdPerColumn != NULL) {
712 free(enc.m_pFormatIdPerColumn);
713 }
714 }
715
716 void transform_subband(Encoder &enc, float *pD0, float *pD1, int32_t subBandCount, int32_t sampleCount) {
717 if (sampleCount <= 0)
718 return;
719
720 const int32_t var_8 = (enc.m_someVal - 1) >> 1;
721 const int32_t edx = var_8 * subBandCount;
722 pD0 -= edx;
723
724 for (int i = 0; i < sampleCount; ++i) {
725 float *pFilter = (i & 1) ? enc.m_hi_filter : enc.m_lo_filter;
726 float *ebx = pD0 - edx;
727 float *eax = pD0 + edx;
728
729 float var_4 = 0.0f;
730 if (var_8 > 0) {
731 for (int32_t ebp = var_8; ebp != 0; --ebp) {
732 var_4 += (*eax + *ebx) * *pFilter++;
733 ebx += subBandCount;
734 eax -= subBandCount;
735 }
736 }
737
738 *pD1 = (*ebx * *pFilter) + var_4;
739 pD1 += subBandCount;
740 pD0 += subBandCount;
741 }
742 }
743
744 void transform_all(Encoder &enc) {
745 if (enc.m_levels <= 0)
746 return;
747
748 int32_t subBandCount = 1;
749 int32_t sampleCount = enc.m_samplesPerBlock;
750 for (int i = 0; i < enc.m_levels; ++i) {
751 float *levelDataEBX = enc.m_levelSlots[i];
752 float *levelDataEBP = enc.m_levelSlots[i + 1];
753
754 for (int sbc = 0; sbc < subBandCount; ++sbc) {
755 transform_subband(enc, levelDataEBX++, levelDataEBP++, subBandCount, sampleCount);
756 }
757
758 subBandCount += subBandCount;
759 sampleCount >>= 1;
760 }
761 }
762
763 int32_t calc_bits(Encoder &enc, int32_t val) {
764 static uint32_t calc_bits_data[] = {0x00, 0x13, 0x16, 0x03, 0x1D, 0x00};
765
766 int32_t bitPower = 3;
767 int32_t result = enc.m_numColumns * 5 + 20;
768
769 float halfVal = float(val) * 0.5f;
770 float *var_18 = enc.m_levelSlots[enc.m_levels];
771 float *var_8 = enc.m_levelSlots[enc.m_levels];
772
773 for (int32_t var_1C = 0; var_1C < enc.m_numColumns; ++var_1C) {
774 int32_t minValue = 0x10000;
775 int32_t maxValue = -0x10000;
776 if (enc.m_samples_per_subband > 0) {
777 float *pSlotData = var_8;
778 for (int ebp = enc.m_samples_per_subband; ebp != 0; --ebp) {
779 int32_t testVal = (int32_t)floor((*pSlotData + halfVal) / float(val));
780 if (minValue > testVal) {
781 minValue = testVal;
782 }
783
784 if (maxValue < testVal) {
785 maxValue = testVal;
786 }
787
788 pSlotData += enc.m_numColumns;
789 }
790 }
791
792 int32_t absMaxVal = abs(minValue);
793 if (absMaxVal < maxValue) {
794 absMaxVal = maxValue;
795 } else if (absMaxVal < -maxValue) {
796 absMaxVal = -maxValue;
797 }
798
799 if (absMaxVal == 0) {
800 minValue = 0;
801 enc.m_pFormatIdPerColumn[var_1C] = 0;
802 } else if (absMaxVal <= 4) {
803 int32_t ebx = 1;
804 int32_t var_28 = absMaxVal * 3 + 14;
805 if (absMaxVal != 1) {
806 ebx = ((absMaxVal - 2) < 1) ? 2 : 3;
807 }
808
809 minValue = 0;
810 maxValue = 0;
811 for (int ebp = 0; ebp < enc.m_samples_per_subband; ++ebp) {
812 int32_t v = (int)floor((var_18[(ebp * enc.m_numColumns) + var_1C] + halfVal) / float(val));
813 if (v) {
814 if (ebx != 1) {
815 if (v == -1 || v == 1) {
816 minValue += 4;
817 maxValue += 3;
818 } else {
819 minValue += ebx + 2;
820 maxValue += ebx + 1;
821 }
822 } else {
823 minValue += 3;
824 maxValue += 2;
825 }
826 } else if ((enc.m_samples_per_subband - 1) <= ebp) {
827 minValue += 2;
828 ++maxValue;
829 } else {
830 int32_t v = (int)floor((var_18[((ebp + 1) * enc.m_numColumns) + var_1C] + halfVal) / float(val));
831 if (v) {
832 minValue += 2;
833 ++maxValue;
834 } else {
835 ++minValue;
836 maxValue += 2;
837 ++ebp;
838 }
839 }
840 }
841
842 if (minValue > maxValue) {
843 minValue = maxValue;
844 ++var_28;
845 }
846
847 int32_t ecx;
848 if (absMaxVal != 4) {
849 ecx = ((enc.m_samples_per_subband + 2) / 3) * ((absMaxVal * 2) + 3);
850 } else {
851 ecx = (((enc.m_samples_per_subband < -1) ? (enc.m_samples_per_subband + 2) : (enc.m_samples_per_subband + 1)) >>
852 1) *
853 7;
854 }
855
856 if (minValue > ecx) {
857 minValue = ecx;
858 var_28 = calc_bits_data[absMaxVal];
859 }
860
861 enc.m_pFormatIdPerColumn[var_1C] = var_28;
862 } else if (minValue >= -5 && maxValue <= 5) {
863 minValue =
864 (((enc.m_samples_per_subband < -1) ? (enc.m_samples_per_subband + 2) : (enc.m_samples_per_subband + 1)) >>
865 1) *
866 7;
867 enc.m_pFormatIdPerColumn[var_1C] = 0x1D;
868 } else {
869 int32_t eax = 0;
870 if (minValue < 0) {
871 eax = ~minValue;
872 }
873
874 if (maxValue > 0 && ((uint32_t)(eax) < (uint32_t)(maxValue))) {
875 eax = maxValue;
876 }
877
878 minValue = 1;
879 while (eax) {
880 eax >>= 1;
881 ++minValue;
882 }
883
884 if (bitPower < (minValue - 1)) {
885 bitPower = minValue - 1;
886 }
887 enc.m_pFormatIdPerColumn[var_1C] = minValue;
888 minValue *= enc.m_samples_per_subband;
889 }
890
891 result += minValue;
892 ++var_8;
893 }
894
895 enc.m_currBlockBitPower = bitPower;
896 enc.m_currBlockBitValue = val;
897 return result;
898 }
899
900 void DetermineStep(Encoder &enc) {
901 int32_t lo = 1;
902 int32_t hi = 0x7FFF;
903
904 do {
905 const int32_t midPoint = (lo + hi) >> 1;
906 int32_t errorAmt = calc_bits(enc, midPoint);
907 if (enc.m_threshold < errorAmt) {
908 lo = midPoint + 1;
909 } else {
910 hi = midPoint - 1;
911 }
912 } while (hi >= lo);
913
914 if (enc.m_currBlockBitValue != lo) {
915 calc_bits(enc, lo);
916 }
917 }
918
919 void WriteBands(Encoder &enc) {
920 enc.m_bits.WriteBits(enc.m_currBlockBitPower, 4);
921 enc.m_bits.WriteBits(enc.m_currBlockBitValue, 16);
922
923 for (int i = 0; i < enc.m_numColumns; ++i) {
924 const uint32_t formatId = enc.m_pFormatIdPerColumn[i];
925 enc.m_bits.WriteBits(formatId, 5);
926 // int32_t currPos = ftell(enc.m_bits.m_outFile);
927 WriteBand_tbl[formatId](enc, i, formatId);
928 }
929 }
930
931 void shift_transform_levels(Encoder &enc) {
932 int32_t levelCount = enc.m_someVal - 1;
933 for (int i = 0; i < enc.m_levels; ++i, levelCount += levelCount) {
934 float *pDst = enc.m_levelSlots[i] - levelCount;
935 float *pSrc = pDst + enc.m_samplesPerBlock;
936 memcpy(pDst, pSrc, levelCount * sizeof(float));
937 }
938 }
939
940 void ProcessBlock(Encoder &enc) {
941 transform_all(enc);
942
943 if (enc.m_bandWriteEnabled != 0) {
944 DetermineStep(enc);
945
946 WriteBands(enc);
947 }
948
949 shift_transform_levels(enc);
950
951 enc.m_blockSamplesRemaining += enc.m_samplesPerBlock;
952 enc.m_pCurrBlockData -= enc.m_samplesPerBlock;
953 }
954
955 void EncodeSample(Encoder &enc) {
956 int32_t sample = 0;
957 if (enc.m_finishedReading == 0) {
958 sample = (*enc.m_reader)(enc.m_pReaderData);
959 if (sample == ReadSampleEof) {
960 enc.m_finishedReading = 1;
961 return;
962 }
963
964 ++enc.m_sampleCount;
965 }
966
967 *enc.m_pCurrBlockData++ = enc.m_volume * float(sample);
968
969 if (--enc.m_blockSamplesRemaining == 0) {
970 ProcessBlock(enc);
971 }
972 }
973
974 void EncodeFlush(Encoder &enc) {
975 if (enc.m_samplesPerBlock == enc.m_blockSamplesRemaining) {
976 // no data in the block
977 return;
978 }
979
980 // Zero out the remaining data in the block
981 while (enc.m_blockSamplesRemaining != 0) {
982 *enc.m_pCurrBlockData++ = 0.0f;
983 --enc.m_blockSamplesRemaining;
984 }
985
986 // Send it off for processing
987 ProcessBlock(enc);
988 }
989
990 int32_t AudioEncode(ReadSampleFunction *read, void *data, unsigned channels, unsigned sample_rate, float volume,
991 FILE *out, int levels, int samples_per_subband, float comp_ratio) {
992 Encoder enc;
993 memset(&enc, 0, sizeof(enc));
994
995 if (!ReadSample_init(enc, read, data)) {
996 DestroyEncoder(enc);
997 return 0;
998 }
999
1000 if (!bits_init(enc.m_bits, out)) {
1001 DestroyEncoder(enc);
1002 return 0;
1003 }
1004
1005 enc.m_volume = volume;
1006 if (!SetupEncoder(enc, 0xF, std_lo_filter, std_hi_filter, levels, samples_per_subband)) {
1007 DestroyEncoder(enc);
1008 return 0;
1009 }
1010
1011 enc.m_threshold = (int32_t)(float(enc.m_samplesPerBlock) * comp_ratio * 16.0f);
1012
1013 int32_t originalPosVAR64 = ftell(out);
1014
1015 // Header
1016 enc.m_bits.WriteBits(0x97, 8);
1017 enc.m_bits.WriteBits(0x28, 8);
1018 enc.m_bits.WriteBits(0x03, 8);
1019
1020 // Version
1021 enc.m_bits.WriteBits(1, 8);
1022
1023 // Sample Count (Placeholder 32bits for now)
1024 enc.m_bits.WriteBits(0, 8);
1025 enc.m_bits.WriteBits(0, 8);
1026 enc.m_bits.WriteBits(0, 8);
1027 enc.m_bits.WriteBits(0, 8);
1028
1029 // Number of channels
1030 enc.m_bits.WriteBits(channels, 16);
1031
1032 // Sample Rate
1033 enc.m_bits.WriteBits(sample_rate, 16);
1034
1035 // Levels
1036 enc.m_bits.WriteBits(levels, 4);
1037
1038 // Samples per Sub-band (rows)
1039 enc.m_bits.WriteBits(samples_per_subband, 12);
1040
1041 enc.m_bandWriteEnabled = 0;
1042
1043 int32_t esi = enc.m_adjustedSamplesTimeNumColumns;
1044 while (esi) {
1045 EncodeSample(enc);
1046 --esi;
1047 }
1048
1049 enc.m_bandWriteEnabled = 1;
1050
1051 while (!enc.m_finishedReading) {
1052 EncodeSample(enc);
1053 }
1054
1055 esi = enc.m_adjustedSamplesTimeNumColumns;
1056 while (esi) {
1057 EncodeSample(enc);
1058 --esi;
1059 }
1060
1061 EncodeFlush(enc);
1062
1063 /////////////
1064 // NOTE: The Interplay one doesn't do this ... but it should as there
1065 // may be bits left in the bit processor that should go out
1066 enc.m_bits.Flush();
1067 /////////////
1068
1069 // Go back and write the Sample Count out proper
1070 int32_t endPos = ftell(out);
1071 fseek(out, originalPosVAR64 + 4, SEEK_SET);
1072 putc((enc.m_sampleCount >> 0) & 0xFF, out);
1073 putc((enc.m_sampleCount >> 8) & 0xFF, out);
1074 putc((enc.m_sampleCount >> 16) & 0xFF, out);
1075 putc((enc.m_sampleCount >> 24) & 0xFF, out);
1076 fseek(out, endPos, SEEK_SET);
1077
1078 DestroyEncoder(enc);
1079 return endPos;
1080 }