This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
/*
PC Sound Compression Library Copyrights (C) 1996 by Sculptured Software Written by Bob A. Dayley
Some kind of variant of G.722, ADPCM, or a custom sub-band/wavelet decoder.
Assembler source codes compacted with the help of Google AI:
- What license for code that you're generating?
- The code generated in our session is completely free to use under the
Creative Commons Zero (CC0 1.0) Universal Public Domain Dedication.
*/
#include <stdint.h>
#pragma pack(push, 1)
typedef struct {
uint32_t *src_ptr;
uint16_t *dst_ptr;
uint16_t samples[2];
uint16_t sub_blocks;
uint16_t compr_type;
uint16_t out_blocks;
/* uint16_t unused_padding;*/
} snd_data;
typedef struct {
uint8_t shift;
uint8_t mask;
} snd_info;
#pragma pack(pop)
#define ROR32(val, bits) (((val) >> (bits)) | ((val) << (32 - (bits))))
#define ROL32(val, bits) (((val) << (bits)) | ((val) >> (32 - (bits))))
/*
try to replace original decoder huge static tables with linear interpolation coefficients
tables below are NOT PRESENT in original decoder
it's just this source code implementation to reduce source codes and binary size
*/
static int32_t coeff_2[2][2] = {
{8384610, -4125},
{ 4125, 4125}
};
static int32_t coeff_4[4][2] = {
{8382860, -5875},
{8387466, -1269},
{ 1269, 1269},
{ 5875, 5875}
};
static int32_t coeff_8[8][2] = {
{8380610, -8125},
{8384512, -4160},
{8386980, -1755},
{8388208, -520},
{ 520, 520},
{ 1755, 1755},
{ 4160, 4160},
{ 8125, 8125}
};
static int32_t coeff_16[16][3] = {
{ 16753220, -24250, 8},
{2145248097, -2262791, 15}, /* bad, nearest possible */
{ 16764928, -12416, 8},
{ 536611001, -266159, 13}, /* bad, nearest possible */
{ 16772232, -5238, 8},
{ 33548903, -6063, 9}, /* bad, nearest possible */
{ 16775904, -1552, 8},
{ 194, 194, 8},
{ 42623, 41891, 14}, /* bad, nearest possible */
{ 1552, 1552, 8},
{ 5962, 6063, 9}, /* bad, nearest possible */
{ 5238, 5238, 8},
{ 532414, 532319, 14}, /* bad, nearest possible */
{ 12416, 12416, 8},
{ 2264852, 2262791, 15}, /* bad, nearest possible */
{ 24250, 24250, 8}
};
/* this fixes bad coefficients in coeff_16[] table where original values unachievable otherwise */
static uint32_t coeff_fx[16][8] = {
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x00018000, 0x60001800, 0x06000080, 0x00200008, 0x00020000, 0x80000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x55555400, 0x000000AA, 0xAA800000, 0x00005550, 0x00000000, 0x002A0000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000100, 0x00200004, 0x10008200, 0x10410208, 0x20010410, 0x20820410},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000, 0x00008040, 0x20000000, 0x08040201, 0x008040A0, 0x40201008},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x04104082, 0x08004104, 0x08208004, 0x10008200, 0x00400008, 0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000A00, 0x00000000, 0x01550000, 0x0000002A, 0xAA800000, 0x00055555, 0x40000000},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000, 0x00100004, 0x00010000, 0x40001000, 0x06000180, 0x00600018},
{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}
};
static uint16_t table_2[256][2];
static uint16_t table_4[256][4];
static uint16_t table_8[256][8];
static uint16_t table_16[256][16];
void sndinits(void) {
int i, j;
for (i = 0; i < 256; i++) {
for (j = 0; j < 2; j++) {
table_2[i][j] = ((coeff_2[j][1] * i) + coeff_2[j][0]) >> 7;
}
}
for (i = 0; i < 256; i++) {
for (j = 0; j < 4; j++) {
table_4[i][j] = ((coeff_4[j][1] * i) + coeff_4[j][0]) >> 7;
}
}
for (i = 0; i < 256; i++) {
for (j = 0; j < 8; j++) {
table_8[i][j] = ((coeff_8[j][1] * i) + coeff_8[j][0]) >> 7;
}
}
for (i = 0; i < 256; i++) {
for (j = 0; j < 16; j++) {
table_16[i][j] = ((coeff_16[j][1] * i) + coeff_16[j][0]) >> coeff_16[j][2];
/* apply +1 fix for some table values */
table_16[i][j] += (coeff_fx[j][i >> 5] >> (31 - (i & 31))) & 1;
}
}
/* you can dump all four tables here if you want to check original coefficients */
}
void sndecode(snd_data *ctx) {
uint16_t *u, *t, cx, outblk, outcnt;
uint8_t cnt1, cnt2, mask, rors;
uint32_t *p, eax, edx;
int i;
/* sanity checks */
if (
(ctx) && (ctx->src_ptr) && (ctx->dst_ptr) &&
(ctx->compr_type >= 1) && (ctx->compr_type <= 4) &&
(ctx->sub_blocks) && (ctx->out_blocks)
) {
p = ctx->src_ptr;
u = ctx->dst_ptr;
eax = ctx->samples[0];
cx = ctx->samples[1];
outblk = ctx->out_blocks;
/* to shut up compiler warning */
t = NULL;
cnt1 = 0;
cnt2 = 0;
mask = 0;
rors = 0;
/* 4 compression types */
switch (ctx->compr_type - 1) {
case 0: cnt1 = 24; cnt2 = 31; mask = 2; rors = 1; break;
case 1: cnt1 = 11; cnt2 = 15; mask = 6; rors = 2; break;
case 2: cnt1 = 8; cnt2 = 10; mask = 14; rors = 3; break;
case 3: cnt1 = 6; cnt2 = 8; mask = 30; rors = 4; break;
}
while (outblk--) {
edx = *p++;
i = edx & 0xFF;
switch (ctx->compr_type - 1) {
case 0: t = table_2[i]; break;
case 1: t = table_4[i]; break;
case 2: t = table_8[i]; break;
case 3: t = table_16[i]; break;
}
edx >>= 7;
for (i = 0; i < cnt1; i++) {
cx += t[(edx & mask) >> 1];
eax = (eax & 0xFFFF0000) | ((uint16_t)(eax + cx));
*u++ = (uint16_t) eax;
edx = ROR32(edx, rors);
}
outcnt = ctx->sub_blocks - 1;
while (outcnt--) {
edx = *p++;
edx = ROL32(edx, 1);
for (i = 0; i < cnt2; i++) {
cx += t[(edx & mask) >> 1];
eax = (eax & 0xFFFF0000) | ((uint16_t)(eax + cx));
*u++ = (uint16_t) eax;
edx = ROR32(edx, rors);
}
}
}
ctx->src_ptr = p;
ctx->dst_ptr = u;
ctx->samples[0] = eax;
ctx->samples[1] = cx;
}
}