diff --git a/README.md b/README.md index 4983cfc7..50f1a1e3 100644 --- a/README.md +++ b/README.md @@ -473,14 +473,19 @@ characters, and BOM is a helper "byte-order" mark). Windows' *notepad* creates f *"with BOM"* when selecting UTF-8 encoding in *save as* dialog, or you may use other programs like *notepad++.exe* to convert them. -More exactly, vgmstream matches filenames and reads tags assuming they are in UTF-8, -while foobar/winamp can only read UTF-8 Japanese/extended filenames in a `.m3u` if file -is saved *with BOM* (opening files manually or with a `playlist.m3u8` won't need BOM). +More exactly, vgmstream needs the file saved in *UTF-8* to match tags and filenames +(and ignores *BOM*), while foobar/Winamp won't understand UTF-8 *filenames* unless +`.m3u` is saved *with BOM* (ignoring tags). Whereas if saved in what Windows calls +"Unicode" (UTF-16) neither may work. + +Conversely, if your *filenames* only use English/ANSI characters you may ommit *BOM*, +and if your tags are English only you may save the `.m3u` as ANSI. Or if you only use +`!tags.m3u` for tags and not for opening files (for example opening them manually +or with a `playlist.m3u8`) you won't need BOM either. Other players may not need BOM (or CRLF), but for consistency use them when dealing with non-ASCII names and tags. - ### Tags with spaces Some players like foobar accept tags with spaces. To use them surround the tag with both characters. @@ -558,7 +563,8 @@ When tags change behavior varies depending on player: - *Audacious*: files need to be readded to the playlist Currently there is no tool to aid in the creation of these tags, but you can create -a base `.m3u` and edit as a text file. +a base `.m3u` and edit as a text file. You may try this python script to make the +base file: https://pastebin.com/Sdu82SAp vgmstream's "m3u tagging" is meant to be simple to make and share (just a text file), easier to support in multiple players (rather than needing a custom plugin), diff --git a/ext_libs/clHCA.c b/ext_libs/clHCA.c index 1877d812..79a25e58 100644 --- a/ext_libs/clHCA.c +++ b/ext_libs/clHCA.c @@ -1,23 +1,26 @@ /** * clHCA DECODER * + * Decodes CRI's HCA (High Compression Audio) codec. Also supports what CRI calls HCA-MX, which + * basically is the same thing with constrained encoder settings. + * * - Original decompilation and C++ decoder by nyaga * https://github.com/Nyagamon/HCADecoder * - Ported to C by kode54 * https://gist.github.com/kode54/ce2bf799b445002e125f06ed833903c0 - * - Cleaned up by bnnm using Thealexbarney's VGAudio decoder as reference + * - Cleaned up and re-reverse engineered for HCA v3 by bnnm, using Thealexbarney's VGAudio decoder as reference * https://github.com/Thealexbarney/VGAudio */ /* TODO: * - improve portability on types and float casts, sizeof(int) isn't necessarily sizeof(float) - * - check "packed_noise_level" vs VGAudio (CriHcaPacking.UnpackFrameHeader), weird behaviour * - check "delta scalefactors" vs VGAudio (CriHcaPacking.DeltaDecode), may be setting wrong values on bad data - * - check "read intensity" vs VGAudio (CriHcaPacking.ReadIntensity), skips intensities if first is 15 * - simplify DCT4 code - * - add extra validations: encoder_delay/padding < sample_count, bands/totals (max: 128?), track count==1, etc - * - calling clHCA_clear multiple times will not deallocate "comment" correctly + * - add extra validations: encoder_delay/padding < sample_count, coded/bands/totals (max: 128?), track count==1, etc + * - coded_scalefactor_count maybe rename coded_band_count or coded_count + * - intensity should calloc if intensity is 15 or set in reset */ + //-------------------------------------------------- // Includes //-------------------------------------------------- @@ -26,14 +29,41 @@ #include #include -#define HCA_MASK 0x7F7F7F7F /* chunk obfuscation when the HCA is encrypted with key */ +/* CRI libs only allow last version, though most decoding code takes older versions into account. + * lib is identified with "HCA Decoder (Float)" + version string. Some known versions: + * - ~V1.1 2011 [first public version] + * - ~V1.2 2011 [ciph/ath chunks, disabled ATH] + * - Ver.1.40 2011-04 [header mask] + * - Ver.1.42 2011-05 + * - Ver.1.45.02 2012-03 + * - Ver.2.00.02 2012-06 [decoding updates] + * - Ver.2.02.02 2013-12, 2014-11 + * - Ver.2.06.05 2018-12 [scramble subkey API] + * - Ver.2.06.07 2020-02, 2021-02 + * - Ver.3.01.00 2020-11 [decoding updates] + * Same version rebuilt gets a newer date, and new APIs change header strings, but header versions + * only change when decoder does. Despite the name, no "Integer" version seems to exist. + */ +#define HCA_VERSION_V101 0x0101 /* V1.1+ [El Shaddai (PS3/X360)] */ +#define HCA_VERSION_V102 0x0102 /* V1.2+ [Gekka Ryouran Romance (PSP)]] */ +#define HCA_VERSION_V103 0x0103 /* V1.4+ [Phantasy Star Online 2 (PC), Binary Domain (PS3)] */ +#define HCA_VERSION_V200 0x0200 /* V2.0+ [Yakuza 5 (PS3)] */ +#define HCA_VERSION_V300 0x0300 /* V3.0+ [Uma Musume (Android)] */ + +/* maxs depend on encoder quality settings (for example, stereo has: + * highest=0x400, high=0x2AA, medium=0x200, low=0x155, lowest=0x100) */ +#define HCA_MAX_FRAME_SIZE 0xFFFF /* max allowed by lib */ +#define HCA_MIN_FRAME_SIZE 0x8 /* assumed */ + +#define HCA_MASK 0x7F7F7F7F /* chunk obfuscation when the HCA is encrypted with key */ #define HCA_SUBFRAMES_PER_FRAME 8 #define HCA_SAMPLES_PER_SUBFRAME 128 #define HCA_SAMPLES_PER_FRAME (HCA_SUBFRAMES_PER_FRAME*HCA_SAMPLES_PER_SUBFRAME) -#define HCA_MDCT_BITS 7 /* (1<<7) = 128 */ +#define HCA_MDCT_BITS 7 /* (1<<7) = 128 */ -#define HCA_MAX_CHANNELS 16 /* internal max? in practice only 8 can be encoded */ +#define HCA_MAX_CHANNELS 16 /* internal max (in practice only 8 can be encoded) */ +#define HCA_ERROR_OK 0 #define HCA_ERROR_PARAMS -1 #define HCA_ERROR_HEADER -2 #define HCA_ERROR_CHECKSUM -3 @@ -45,16 +75,20 @@ // Decoder config/state //-------------------------------------------------- typedef enum { DISCRETE = 0, STEREO_PRIMARY = 1, STEREO_SECONDARY = 2 } channel_type_t; + typedef struct stChannel { /* HCA channel config */ - int type; /* discrete / stereo-primary / stereo-secondary */ - unsigned int coded_scalefactor_count; /* scalefactors used (depending on channel type) */ - unsigned char *hfr_scales; /* high frequency scales, pointing to higher scalefactors (simplification) */ + int type; /* channel_type_t */ + unsigned char* hfr_scales; /* high frequency scales, pointing to higher scalefactors (simplification) */ /* subframe state */ unsigned char intensity[HCA_SUBFRAMES_PER_FRAME]; /* intensity indexes (value max: 15 / 4b) */ unsigned char scalefactors[HCA_SAMPLES_PER_SUBFRAME]; /* scale indexes (value max: 64 / 6b)*/ unsigned char resolution[HCA_SAMPLES_PER_SUBFRAME]; /* resolution indexes (value max: 15 / 4b) */ + unsigned char unknowns[HCA_SAMPLES_PER_SUBFRAME]; /* ? indexes of resolutions 0? (value max: 128 / 8b) */ + unsigned int coded_scalefactor_count; /* scalefactors used (depending on channel type) */ + unsigned int unknown1_max; /* resolutions that use other values */ + unsigned int unknown2_max; /* resolutions that use 0 */ float gain[HCA_SAMPLES_PER_SUBFRAME]; /* gain to apply to quantized spectral data */ float spectra[HCA_SAMPLES_PER_SUBFRAME]; /* resulting dequantized data */ @@ -62,6 +96,7 @@ typedef struct stChannel { float dct[HCA_SAMPLES_PER_SUBFRAME]; /* result of DCT-IV */ float imdct_previous[HCA_SAMPLES_PER_SUBFRAME]; /* IMDCT */ + /* frame state */ float wave[HCA_SUBFRAMES_PER_FRAME][HCA_SAMPLES_PER_SUBFRAME]; /* resulting samples */ } stChannel; @@ -108,10 +143,11 @@ typedef struct clHCA { /* rva chunk */ float rva_volume; /* comm chunk */ - unsigned int comment_len; - char *comment; + unsigned int comment_len; /* max 0xFF */ + char comment[255+1]; /* initial state */ + int v3_flag; unsigned int hfr_group_count; unsigned char ath_curve[HCA_SAMPLES_PER_SUBFRAME]; unsigned char cipher_table[256]; @@ -120,15 +156,16 @@ typedef struct clHCA { } clHCA; typedef struct clData { - const unsigned char *data; + const unsigned char* data; int size; int bit; -} clData; +} clData; //-------------------------------------------------- // Checksum //-------------------------------------------------- +//hcacommon_crc_mask_table static const unsigned short crc16_lookup_table[256] = { 0x0000,0x8005,0x800F,0x000A,0x801B,0x001E,0x0014,0x8011,0x8033,0x0036,0x003C,0x8039,0x0028,0x802D,0x8027,0x0022, 0x8063,0x0066,0x006C,0x8069,0x0078,0x807D,0x8077,0x0072,0x0050,0x8055,0x805F,0x005A,0x804B,0x004E,0x0044,0x8041, @@ -148,7 +185,8 @@ static const unsigned short crc16_lookup_table[256] = { 0x0220,0x8225,0x822F,0x022A,0x823B,0x023E,0x0234,0x8231,0x8213,0x0216,0x021C,0x8219,0x0208,0x820D,0x8207,0x0202, }; -static unsigned short crc16_checksum(const unsigned char *data, unsigned int size) { +//HCACommon_CalculateCrc +static unsigned short crc16_checksum(const unsigned char* data, unsigned int size) { unsigned int i; unsigned short sum = 0; @@ -162,13 +200,13 @@ static unsigned short crc16_checksum(const unsigned char *data, unsigned int siz //-------------------------------------------------- // Bitstream reader //-------------------------------------------------- -static void bitreader_init(clData *br, const void *data, int size) { +static void bitreader_init(clData* br, const void *data, int size) { br->data = data; br->size = size * 8; br->bit = 0; } -static unsigned int bitreader_peek(clData *br, int bitsize) { +static unsigned int bitreader_peek(clData* br, int bitsize) { const unsigned int bit = br->bit; const unsigned int bit_rem = bit & 7; const unsigned int size = br->size; @@ -185,7 +223,7 @@ static unsigned int bitreader_peek(clData *br, int bitsize) { 0xFFFFFFFF,0x7FFFFFFF,0x3FFFFFFF,0x1FFFFFFF, 0x0FFFFFFF,0x07FFFFFF,0x03FFFFFF,0x01FFFFFF }; - const unsigned char *data = &br->data[bit >> 3]; + const unsigned char* data = &br->data[bit >> 3]; v = data[0]; v = (v << 8) | data[1]; v = (v << 8) | data[2]; @@ -198,7 +236,7 @@ static unsigned int bitreader_peek(clData *br, int bitsize) { 0xFFFFFF,0x7FFFFF,0x3FFFFF,0x1FFFFF, 0x0FFFFF,0x07FFFF,0x03FFFF,0x01FFFF }; - const unsigned char *data = &br->data[bit >> 3]; + const unsigned char* data = &br->data[bit >> 3]; v = data[0]; v = (v << 8) | data[1]; v = (v << 8) | data[2]; @@ -209,7 +247,7 @@ static unsigned int bitreader_peek(clData *br, int bitsize) { static const unsigned int mask[8] = { 0xFFFF,0x7FFF,0x3FFF,0x1FFF,0x0FFF,0x07FF,0x03FF,0x01FF }; - const unsigned char *data = &br->data[bit >> 3]; + const unsigned char* data = &br->data[bit >> 3]; v = data[0]; v = (v << 8) | data[1]; v &= mask[bit_rem]; @@ -219,7 +257,7 @@ static unsigned int bitreader_peek(clData *br, int bitsize) { static const unsigned int mask[8] = { 0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01 }; - const unsigned char *data = &br->data[bit >> 3]; + const unsigned char* data = &br->data[bit >> 3]; v = data[0]; v &= mask[bit_rem]; v >>= 8 - bit_rem - bitsize; @@ -227,13 +265,13 @@ static unsigned int bitreader_peek(clData *br, int bitsize) { return v; } -static unsigned int bitreader_read(clData *br, int bitsize) { +static unsigned int bitreader_read(clData* br, int bitsize) { unsigned int v = bitreader_peek(br, bitsize); br->bit += bitsize; return v; } -static void bitreader_skip(clData *br, int bitsize) { +static void bitreader_skip(clData* br, int bitsize) { br->bit += bitsize; } @@ -259,7 +297,7 @@ int clHCA_isOurFile(const void *data, unsigned int size) { return header_size; } -int clHCA_getInfo(clHCA *hca, clHCA_stInfo *info) { +int clHCA_getInfo(clHCA* hca, clHCA_stInfo *info) { if (!hca || !info || !hca->is_valid) return HCA_ERROR_PARAMS; @@ -282,7 +320,7 @@ int clHCA_getInfo(clHCA *hca, clHCA_stInfo *info) { return 0; } -void clHCA_ReadSamples16(clHCA *hca, signed short *samples) { +void clHCA_ReadSamples16(clHCA* hca, signed short *samples) { const float scale = 32768.0f; float f; signed int s; @@ -311,42 +349,38 @@ void clHCA_ReadSamples16(clHCA *hca, signed short *samples) { //-------------------------------------------------- // Allocation and creation //-------------------------------------------------- -static void clHCA_constructor(clHCA *hca) { +static void clHCA_constructor(clHCA* hca) { if (!hca) return; memset(hca, 0, sizeof(*hca)); hca->is_valid = 0; - hca->comment = 0; } -static void clHCA_destructor(clHCA *hca) { - if (!hca) - return; - free(hca->comment); - hca->comment = 0; +static void clHCA_destructor(clHCA* hca) { + hca->is_valid = 0; } int clHCA_sizeof() { return sizeof(clHCA); } -void clHCA_clear(clHCA *hca) { +void clHCA_clear(clHCA* hca) { clHCA_constructor(hca); } -void clHCA_done(clHCA *hca) { +void clHCA_done(clHCA* hca) { clHCA_destructor(hca); } -clHCA * clHCA_new() { - clHCA *hca = (clHCA *) malloc(clHCA_sizeof()); +clHCA* clHCA_new() { + clHCA* hca = malloc(clHCA_sizeof()); if (hca) { clHCA_constructor(hca); } return hca; } -void clHCA_delete(clHCA *hca) { +void clHCA_delete(clHCA* hca) { clHCA_destructor(hca); free(hca); } @@ -400,12 +434,12 @@ static const unsigned char ath_base_curve[656] = { 0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFF,0xFF, }; -static void ath_init0(unsigned char *ath_curve) { +static void ath_init0(unsigned char* ath_curve) { /* disable curve */ memset(ath_curve, 0, sizeof(ath_curve[0]) * HCA_SAMPLES_PER_SUBFRAME); } -static void ath_init1(unsigned char *ath_curve, unsigned int sample_rate) { +static void ath_init1(unsigned char* ath_curve, unsigned int sample_rate) { unsigned int i, index; unsigned int acc = 0; @@ -422,7 +456,7 @@ static void ath_init1(unsigned char *ath_curve, unsigned int sample_rate) { } } -static int ath_init(unsigned char *ath_curve, int type, unsigned int sample_rate) { +static int ath_init(unsigned char* ath_curve, int type, unsigned int sample_rate) { switch (type) { case 0: ath_init0(ath_curve); @@ -433,14 +467,14 @@ static int ath_init(unsigned char *ath_curve, int type, unsigned int sample_rate default: return HCA_ERROR_HEADER; } - return 0; + return HCA_ERROR_OK; } //-------------------------------------------------- // Encryption //-------------------------------------------------- -static void cipher_decrypt(unsigned char *cipher_table, unsigned char *data, int size) { +static void cipher_decrypt(unsigned char* cipher_table, unsigned char* data, int size) { unsigned int i; for (i = 0; i < size; i++) { @@ -448,7 +482,7 @@ static void cipher_decrypt(unsigned char *cipher_table, unsigned char *data, int } } -static void cipher_init0(unsigned char *cipher_table) { +static void cipher_init0(unsigned char* cipher_table) { unsigned int i; /* no encryption */ @@ -457,7 +491,7 @@ static void cipher_init0(unsigned char *cipher_table) { } } -static void cipher_init1(unsigned char *cipher_table) { +static void cipher_init1(unsigned char* cipher_table) { const int mul = 13; const int add = 11; unsigned int i, v = 0; @@ -473,7 +507,7 @@ static void cipher_init1(unsigned char *cipher_table) { cipher_table[0xFF] = 0xFF; } -static void cipher_init56_create_table(unsigned char *r, unsigned char key) { +static void cipher_init56_create_table(unsigned char* r, unsigned char key) { const int mul = ((key & 1) << 3) | 5; const int add = (key & 0xE) | 1; unsigned int i; @@ -485,7 +519,7 @@ static void cipher_init56_create_table(unsigned char *r, unsigned char key) { } } -static void cipher_init56(unsigned char *cipher_table, unsigned long long keycode) { +static void cipher_init56(unsigned char* cipher_table, unsigned long long keycode) { unsigned char kc[8]; unsigned char seed[16]; unsigned char base[256], base_r[16], base_c[16]; @@ -548,7 +582,7 @@ static void cipher_init56(unsigned char *cipher_table, unsigned long long keycod } } -static int cipher_init(unsigned char *cipher_table, int type, unsigned long long keycode) { +static int cipher_init(unsigned char* cipher_table, int type, unsigned long long keycode) { if (type == 56 && !(keycode)) type = 0; @@ -565,7 +599,7 @@ static int cipher_init(unsigned char *cipher_table, int type, unsigned long long default: return HCA_ERROR_HEADER; } - return 0; + return HCA_ERROR_OK; } //-------------------------------------------------- @@ -575,7 +609,7 @@ static unsigned int header_ceil2(unsigned int a, unsigned int b) { return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0; } -int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { +int clHCA_DecodeHeader(clHCA* hca, const void *data, unsigned int size) { clData br; int res; @@ -597,13 +631,13 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { hca->version = bitreader_read(&br, 16); hca->header_size = bitreader_read(&br, 16); -#if 0 // play unknown versions anyway (confirmed to exist: v1.1/v1.2/v1.3/v2.0) - if (hca->version != 0x0101 && - hca->version != 0x0102 && - hca->version != 0x0103 && - hca->version != 0x0200) + if (hca->version != HCA_VERSION_V101 && + hca->version != HCA_VERSION_V102 && + hca->version != HCA_VERSION_V103 && + hca->version != HCA_VERSION_V200 && + hca->version != HCA_VERSION_V300) return HCA_ERROR_HEADER; -#endif + if (size < hca->header_size) return HCA_ERROR_PARAMS; @@ -702,8 +736,8 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { hca->ath_type = bitreader_read(&br, 16); } else { - /* removed in v2.0, default in v1.x (maybe only used in v1.1, as v1.2/v1.3 set ath_type = 0) */ - hca->ath_type = (hca->version < 0x200) ? 1 : 0; + /* removed in v2.0, default in v1.x (only used in v1.1, as v1.2/v1.3 set ath_type = 0) */ + hca->ath_type = (hca->version < HCA_VERSION_V200) ? 1 : 0; } /* loop info */ @@ -757,23 +791,18 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { size -= 0x08; } else { - hca->rva_volume = 1; + hca->rva_volume = 1.0f; } /* comment */ if (size >= 0x05 && (bitreader_peek(&br, 32) & HCA_MASK) == 0x636F6D6D) {/* "comm" */ unsigned int i; - char *temp; bitreader_skip(&br, 32); hca->comment_len = bitreader_read(&br, 8); if (hca->comment_len > size) return HCA_ERROR_HEADER; - temp = realloc(hca->comment, hca->comment_len + 1); - if (!temp) - return HCA_ERROR_HEADER; - hca->comment = temp; for (i = 0; i < hca->comment_len; ++i) hca->comment[i] = bitreader_read(&br, 8); hca->comment[i] = '\0'; /* should be null terminated but make sure */ @@ -782,7 +811,6 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { } else { hca->comment_len = 0; - hca->comment = NULL; } /* padding info */ @@ -796,17 +824,25 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { /* extra validations */ - if (!(hca->frame_size >= 0x08 && hca->frame_size <= 0xFFFF)) /* actual max seems 0x155*channels */ + if (!(hca->frame_size >= HCA_MIN_FRAME_SIZE && hca->frame_size <= HCA_MAX_FRAME_SIZE)) /* actual max seems 0x155*channels */ return HCA_ERROR_HEADER; /* theoretically can be 0 if VBR (not seen) */ - if (!(hca->min_resolution == 1 && hca->max_resolution == 15)) - return HCA_ERROR_HEADER; + if (hca->version <= HCA_VERSION_V200) { + if (!(hca->min_resolution == 1 && hca->max_resolution == 15)) + return HCA_ERROR_HEADER; + } + else { + if (hca->min_resolution > hca->max_resolution || hca->max_resolution > 31) + return HCA_ERROR_HEADER; + } - /* inits state */ - if (hca->track_count == 0) + /* init state */ + if (hca->track_count == 0) //todo lib gives error? hca->track_count = 1; /* default to avoid division by zero */ + hca->v3_flag = 0; //hca->version >= HCA_VERSION_V300; //todo check actual flag + hca->hfr_group_count = header_ceil2( hca->total_band_count - hca->base_band_count - hca->stereo_band_count, hca->bands_per_hfr_group); @@ -825,7 +861,7 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { unsigned int i, channels_per_track; channels_per_track = hca->channels / hca->track_count; - if (hca->stereo_band_count && channels_per_track > 1) { + if (hca->stereo_band_count > 0 && channels_per_track > 1) { int *ct = channel_types; for (i = 0; i < hca->track_count; i++, ct += channels_per_track) { switch (channels_per_track) { @@ -840,7 +876,7 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { break; case 4: ct[0] = STEREO_PRIMARY; - ct[1] = 2; + ct[1] = STEREO_SECONDARY; if (hca->channel_config == 0) { ct[2] = STEREO_PRIMARY; ct[3] = STEREO_SECONDARY; @@ -898,6 +934,7 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { hca->channel[i].coded_scalefactor_count = (channel_types[i] != 2) ? hca->base_band_count + hca->stereo_band_count : hca->base_band_count; + /* simplifies usage a bit but isn't pre-saved as such in reversed original lib (calc'd during decode) */ hca->channel[i].hfr_scales = &hca->channel[i].scalefactors[hca->base_band_count + hca->stereo_band_count]; } } @@ -907,10 +944,10 @@ int clHCA_DecodeHeader(clHCA *hca, const void *data, unsigned int size) { * (keycode is not changed between calls) */ hca->is_valid = 1; - return 0; + return HCA_ERROR_OK; } -void clHCA_SetKey(clHCA *hca, unsigned long long keycode) { +void clHCA_SetKey(clHCA* hca, unsigned long long keycode) { if (!hca) return; hca->keycode = keycode; @@ -924,7 +961,7 @@ void clHCA_SetKey(clHCA *hca, unsigned long long keycode) { } } -int clHCA_TestBlock(clHCA *hca, void *data, unsigned int size) { +int clHCA_TestBlock(clHCA* hca, void *data, unsigned int size) { const int frame_samples = HCA_SUBFRAMES_PER_FRAME * HCA_SAMPLES_PER_SUBFRAME; const float scale = 32768.0f; unsigned int ch, sf, s; @@ -936,7 +973,7 @@ int clHCA_TestBlock(clHCA *hca, void *data, unsigned int size) { { int i; int is_empty = 1; - const unsigned char *buf = data; + const unsigned char* buf = data; for (i = 2; i < size - 0x02; i++) { if (buf[i] != 0) { @@ -1004,7 +1041,7 @@ void clHCA_DecodeReset(clHCA * hca) { return; for (i = 0; i < hca->channels; i++) { - stChannel *ch = &hca->channel[i]; + stChannel* ch = &hca->channel[i]; /* most values get overwritten during decode */ //memset(ch->intensity, 0, sizeof(ch->intensity[0]) * HCA_SUBFRAMES_PER_FRAME); @@ -1022,22 +1059,32 @@ void clHCA_DecodeReset(clHCA * hca) { //-------------------------------------------------- // Decode //-------------------------------------------------- -static int decode1_unpack_channel(stChannel *ch, clData *br, - unsigned int hfr_group_count, unsigned int packed_noise_level, const unsigned char *ath_curve); +static int unpack_scalefactors(stChannel* ch, clData* br, unsigned int hfr_group_count, unsigned int version); -static void decode2_dequantize_coefficients(stChannel *ch, clData *br); +static int unpack_intensity(stChannel* ch, clData* br, unsigned int hfr_group_count, unsigned int version); -static void decode3_reconstruct_high_frequency(stChannel *ch, +static void calculate_resolution(stChannel* ch, unsigned int packed_noise_level, const unsigned char* ath_curve, + unsigned int min_resolution, unsigned int max_resolution); + +static void calculate_gain(stChannel* ch); + +static void dequantize_coefficients(stChannel* ch, clData* br); + +static void reconstruct_high_frequency(stChannel* ch, unsigned int hfr_group_count, unsigned int bands_per_hfr_group, unsigned int stereo_band_count, unsigned int base_band_count, unsigned int total_band_count); -static void decode4_apply_intensity_stereo(stChannel *ch, int subframe, +static void reconstruct_resolution_0(stChannel* ch, unsigned int min_resolution, int v3_flag); + +static void apply_intensity_stereo(stChannel* ch_pair, int subframe, unsigned int usable_band_count, unsigned int base_band_count, unsigned int stereo_band_count); -static void decoder5_run_imdct(stChannel *ch, int subframe); +static void apply_unknown_stereo(stChannel* ch_pair, int v3_flag, unsigned int base_band_count); + +static void imdct_transform(stChannel* ch, int subframe); -int clHCA_DecodeBlock(clHCA *hca, void *data, unsigned int size) { +int clHCA_DecodeBlock(clHCA* hca, void *data, unsigned int size) { clData br; unsigned short sync; unsigned int subframe, ch; @@ -1062,15 +1109,20 @@ int clHCA_DecodeBlock(clHCA *hca, void *data, unsigned int size) { /* unpack frame values */ { - unsigned int frame_acceptable_noise_level = bitreader_read(&br, 9); + unsigned int frame_acceptable_noise_level = bitreader_read(&br, 9); //todo part of struct unsigned int frame_evaluation_boundary = bitreader_read(&br, 7); unsigned int packed_noise_level = (frame_acceptable_noise_level << 8) - frame_evaluation_boundary; for (ch = 0; ch < hca->channels; ch++) { - int unpack = decode1_unpack_channel(&hca->channel[ch], &br, - hca->hfr_group_count, packed_noise_level, hca->ath_curve); - if (unpack < 0) - return unpack; + int err = unpack_scalefactors(&hca->channel[ch], &br, hca->hfr_group_count, hca->version); + if (err < 0) + return err; + + unpack_intensity(&hca->channel[ch], &br, hca->hfr_group_count, hca->version); + + calculate_resolution(&hca->channel[ch], packed_noise_level, hca->ath_curve, hca->min_resolution, hca->max_resolution); + + calculate_gain(&hca->channel[ch]); } } @@ -1078,25 +1130,29 @@ int clHCA_DecodeBlock(clHCA *hca, void *data, unsigned int size) { /* unpack channel data and get dequantized spectra */ for (ch = 0; ch < hca->channels; ch++){ - decode2_dequantize_coefficients(&hca->channel[ch], &br); + dequantize_coefficients(&hca->channel[ch], &br); } /* restore missing bands from spectra 1 */ for (ch = 0; ch < hca->channels; ch++) { - decode3_reconstruct_high_frequency(&hca->channel[ch], + reconstruct_resolution_0(&hca->channel[ch], hca->min_resolution, hca->v3_flag); + + reconstruct_high_frequency(&hca->channel[ch], hca->hfr_group_count, hca->bands_per_hfr_group, hca->stereo_band_count, hca->base_band_count, hca->total_band_count); } /* restore missing bands from spectra 2 */ for (ch = 0; ch < hca->channels - 1; ch++) { - decode4_apply_intensity_stereo(&hca->channel[ch], subframe, + apply_intensity_stereo(&hca->channel[ch], subframe, hca->total_band_count, hca->base_band_count, hca->stereo_band_count); + + apply_unknown_stereo(&hca->channel[ch], hca->v3_flag, hca->base_band_count); } /* apply imdct */ for (ch = 0; ch < hca->channels; ch++) { - decoder5_run_imdct(&hca->channel[ch], subframe); + imdct_transform(&hca->channel[ch], subframe); } } @@ -1114,21 +1170,19 @@ int clHCA_DecodeBlock(clHCA *hca, void *data, unsigned int size) { //-------------------------------------------------- // Decode 1st step //-------------------------------------------------- -static const unsigned char decode1_scale_to_resolution_curve[64] = { - 0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0D,0x0D, - 0x0D,0x0D,0x0D,0x0D,0x0C,0x0C,0x0C,0x0C, - 0x0C,0x0C,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B, - 0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x09, - 0x09,0x09,0x09,0x09,0x09,0x08,0x08,0x08, - 0x08,0x08,0x08,0x07,0x06,0x06,0x05,0x04, - 0x04,0x04,0x03,0x03,0x03,0x02,0x02,0x02, - 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - /* for v1.x indexes after 56 are different, but can't be used anyway */ - //0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01, +/* curve/scale to quantized resolution */ +static const unsigned char hcadecoder_invert_table[66] = { + 14,14,14,14,14,14,13,13, 13,13,13,13,12,12,12,12, + 12,12,11,11,11,11,11,11, 10,10,10,10,10,10,10, 9, + 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 7, 6, 6, 5, 4, + 4, 4, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + /* indexes after 56 are not defined in v2.0<= (manually clamped to 1) */ }; +//hcadequantizer_scaling_table_float /* scalefactor-to-scaling table, generated from sqrt(128) * (2^(53/128))^(scale_factor - 63) */ -static const unsigned int decode1_dequantizer_scaling_table_int[64] = { +static const unsigned int decode1_dequantizer_scaling_table_hex[64] = { 0x342A8D26,0x34633F89,0x3497657D,0x34C9B9BE,0x35066491,0x353311C4,0x356E9910,0x359EF532, 0x35D3CCF1,0x360D1ADF,0x363C034A,0x367A83B3,0x36A6E595,0x36DE60F5,0x371426FF,0x3745672A, 0x37838359,0x37AF3B79,0x37E97C38,0x381B8D3A,0x384F4319,0x388A14D5,0x38B7FBF0,0x38F5257D, @@ -1138,127 +1192,252 @@ static const unsigned int decode1_dequantizer_scaling_table_int[64] = { 0x3E1C6573,0x3E506334,0x3E8AD4C6,0x3EB8FBAF,0x3EF67A41,0x3F243516,0x3F5ACB94,0x3F91C3D3, 0x3FC238D2,0x400164D2,0x402C6897,0x4065B907,0x40990B88,0x40CBEC15,0x4107DB35,0x413504F3, }; -static const float *decode1_dequantizer_scaling_table = (const float *)decode1_dequantizer_scaling_table_int; +static const float* decode1_dequantizer_scaling_table = (const float*)decode1_dequantizer_scaling_table_hex; -static const unsigned int decode1_quantizer_step_size_int[16] = { +//hcadequantizer_range_table_float +static const unsigned int decode1_quantizer_step_size_hex[16] = { 0x00000000,0x3F2AAAAB,0x3ECCCCCD,0x3E924925,0x3E638E39,0x3E3A2E8C,0x3E1D89D9,0x3E088889, 0x3D842108,0x3D020821,0x3C810204,0x3C008081,0x3B804020,0x3B002008,0x3A801002,0x3A000801, }; -static const float *decode1_quantizer_step_size = (const float *)decode1_quantizer_step_size_int; +static const float* decode1_quantizer_step_size = (const float*)decode1_quantizer_step_size_hex; -static int decode1_unpack_channel(stChannel *ch, clData *br, - unsigned int hfr_group_count, unsigned int packed_noise_level, const unsigned char *ath_curve) { - unsigned int i; - const unsigned int csf_count = ch->coded_scalefactor_count; +/* get scale indexes to normalize dequantized coefficients */ +static int unpack_scalefactors(stChannel* ch, clData* br, unsigned int hfr_group_count, unsigned int version) { + int i; + unsigned int csf_count = ch->coded_scalefactor_count; + unsigned char delta_bits = bitreader_read(br, 3); + unsigned int extra_count; - /* read scalefactors */ - { - /* scale indexes to normalize dequantized coefficients */ - unsigned char scalefactor_delta_bits = bitreader_read(br, 3); - if (scalefactor_delta_bits >= 6) { - /* normal scalefactors */ + /* added in v3.0 */ + if (ch->type == STEREO_SECONDARY || hfr_group_count <= 0 || version <= HCA_VERSION_V200) { + extra_count = 0; + } + else { + extra_count = hfr_group_count; + csf_count = csf_count + extra_count; + } + + /* just in case */ + if (csf_count > HCA_SAMPLES_PER_SUBFRAME) + return HCA_ERROR_UNPACK; + + /* lib does check that csf_count is 2+, but doesn't seem to affect anything */ + if (delta_bits >= 6) { + /* fixed scalefactors */ + if (csf_count > 1) { for (i = 0; i < csf_count; i++) { ch->scalefactors[i] = bitreader_read(br, 6); } } - else if (scalefactor_delta_bits > 0) { - /* delta scalefactors */ - const unsigned char expected_delta = (1 << scalefactor_delta_bits) - 1; + } + else if (delta_bits > 0) { + /* delta scalefactors */ + if (csf_count > 1) { + const unsigned char expected_delta = (1 << delta_bits) - 1; const unsigned char extra_delta = expected_delta >> 1; unsigned char scalefactor_prev = bitreader_read(br, 6); ch->scalefactors[0] = scalefactor_prev; for (i = 1; i < csf_count; i++) { - unsigned char delta = bitreader_read(br, scalefactor_delta_bits); + unsigned char delta = bitreader_read(br, delta_bits); + + if (delta == expected_delta) { + /* fixed scalefactor */ + scalefactor_prev = bitreader_read(br, 6); + } + else { + /* delta scalefactor */ - if (delta != expected_delta) { /* may happen with bad keycodes, scalefactors must be 6b indexes */ int scalefactor_test = (int)scalefactor_prev + ((int)delta - (int)extra_delta); if (scalefactor_test < 0 || scalefactor_test >= 64) { return HCA_ERROR_UNPACK; } - scalefactor_prev += delta - extra_delta; - } else { - scalefactor_prev = bitreader_read(br, 6); + scalefactor_prev = scalefactor_prev - extra_delta + delta; + + //todo as negative better? (may roll otherwise?) + //if (scalefactor_prev >= 64) { + // return HCA_ERROR_UNPACK; + //} } ch->scalefactors[i] = scalefactor_prev; } } - else { - /* no scalefactors */ - memset(ch->scalefactors, 0, sizeof(ch->scalefactors[0]) * HCA_SAMPLES_PER_SUBFRAME); - } - } - - if (ch->type == STEREO_SECONDARY) { - /* read intensity */ - unsigned char intensity_value = bitreader_peek(br, 4); - - ch->intensity[0] = intensity_value; - if (intensity_value < 15) { - for (i = 0; i < HCA_SUBFRAMES_PER_FRAME; i++) { - ch->intensity[i] = bitreader_read(br, 4); - } - } - /* 15 may be an invalid value? */ - //else { - // return HCA_ERROR_INSENSITY; - //} } else { - /* read hfr scalefactors */ - for (i = 0; i < hfr_group_count; i++) { - ch->hfr_scales[i] = bitreader_read(br, 6); + /* no scalefactors */ + for (i = 0; i < HCA_SAMPLES_PER_SUBFRAME; i++) { + ch->scalefactors[i] = 0; } } - /* calculate resolutions */ - { - /* resolution determines the range of values per encoded spectra, - * using codebooks for lower resolutions during dequantization */ - for (i = 0; i < csf_count; i++) { - unsigned char new_resolution = 0; - unsigned char scalefactor = ch->scalefactors[i]; - if (scalefactor > 0) { - int noise_level = ath_curve[i] + ((packed_noise_level + i) >> 8); - int curve_position = noise_level - ((5 * scalefactor) >> 1) + 1; +#if 0 + for (i = 0; i < extra_count; i++) { + ch->...[x] = ch->...[x]; + } +#endif - /* curve values can be simplified by clamping position to (0,58) and making - * scale_table[0] = 15, table[58] = 1 (like VGAudio does) */ - if (curve_position < 0) - new_resolution = 15; - else if (curve_position >= 57) - new_resolution = 1; - else - new_resolution = decode1_scale_to_resolution_curve[curve_position]; + return HCA_ERROR_OK; +} +static int unpack_intensity(stChannel* ch, clData* br, unsigned int hfr_group_count, unsigned int version) { + int i; + + if (ch->type == STEREO_SECONDARY) { + /* read subframe intensity for channel pair (peek first for valid values, not sure why not consumed) */ + if (version <= HCA_VERSION_V200) { + unsigned char value = bitreader_peek(br, 4); + + ch->intensity[0] = value; + if (value < 15) { + bitreader_skip(br, 4); + for (i = 1; i < HCA_SUBFRAMES_PER_FRAME; i++) { + ch->intensity[i] = bitreader_read(br, 4); + } } - ch->resolution[i] = new_resolution; + /* 15 may be an invalid value? (index 15 is 0, but may imply "reuse last subframe's intensity") */ + //else { + // return HCA_ERROR_UNPACK; + //} } - memset(&ch->resolution[csf_count], 0, sizeof(ch->resolution[0]) * (HCA_SAMPLES_PER_SUBFRAME - csf_count)); - } + else { + unsigned char pack = bitreader_peek(br, 6); + unsigned char value, delta_bits, bits, bmax, code; - /* calculate gain */ - { - /* get actual scales to dequantize */ - for (i = 0; i < csf_count; i++) { - float scalefactor_scale = decode1_dequantizer_scaling_table[ ch->scalefactors[i] ]; - float resolution_scale = decode1_quantizer_step_size[ ch->resolution[i] ]; - ch->gain[i] = scalefactor_scale * resolution_scale; + if (pack < 60) { + bitreader_skip(br, 6); + + value = pack >> 2; /* 4b */ + delta_bits = pack & 0x3; /* 2b */ + + ch->intensity[0] = value; + if (delta_bits == 3) { + /* fixed intensities */ + for (i = 1; i < HCA_SUBFRAMES_PER_FRAME; i++) { + ch->intensity[i] = bitreader_read(br, 4); + } + } + else { + /* delta intensities */ + bmax = (2 << delta_bits) - 1; + bits = delta_bits + 1; + + for (i = 1; i < HCA_SUBFRAMES_PER_FRAME; i++) { + code = bitreader_read(br, bits); + if (code == bmax) { + value = bitreader_read(br, 4); /* encoded */ + } + else { + value = value - (bmax >> 1) + code; /* differential */ + + if (value > 15) { /* not done originally */ + //todo check + return HCA_ERROR_UNPACK; + } + } + + ch->intensity[i] = value; + } + } + } + else { + bitreader_skip(br, 4); /* not 6! */ + for (i = 0; i < HCA_SUBFRAMES_PER_FRAME; i++) { + ch->intensity[i] = 7; + } + } + } + } + else { + /* read high frequency scalefactors */ + if (version <= HCA_VERSION_V200) { + for (i = 0; i < hfr_group_count; i++) { + ch->hfr_scales[i] = bitreader_read(br, 6); + } } } - return 0; + return HCA_ERROR_OK; +} + +/* resolution determines the range of values per encoded spectra, + * using codebooks for lower resolutions during dequantization */ +static void calculate_resolution(stChannel* ch, unsigned int packed_noise_level, const unsigned char* ath_curve, unsigned int min_resolution, unsigned int max_resolution) { + const unsigned int csf_count = ch->coded_scalefactor_count; + int i; + + int unknown1_pos = 0; + int unknown2_pos = 0; + + for (i = 0; i < csf_count; i++) { + unsigned char new_resolution = 0; + unsigned char scalefactor = ch->scalefactors[i]; + + if (scalefactor > 0) { + /* curve values are 0 in v1.2>= so ath_curve is actually removed in CRI's code */ + int noise_level = ath_curve[i] + ((packed_noise_level + i) >> 8); + int curve_position = noise_level + 1 - ((5 * scalefactor) >> 1); + + /* v2.0<= allows max 56 + sets rest to 1, while v3.0 table has 1 for 57..65 and + * clamps to min_resolution below, so v2.0 files are still supported */ + if (curve_position < 0) { + new_resolution = 15; + } + else if (curve_position <= 65) { + new_resolution = hcadecoder_invert_table[curve_position]; + } + else { + new_resolution = 0; + } + + /* resolution 0 handling added in v3.0 */ + if (new_resolution > max_resolution) + new_resolution = max_resolution; + else if (new_resolution < min_resolution) + new_resolution = min_resolution; + + /* indexes for resolution 0 (from 0..N) and other values (from N..0) */ + if (new_resolution < 1) { + ch->unknowns[unknown2_pos] = i; + unknown2_pos++; + } + else { + ch->unknowns[127 - unknown1_pos] = i; + unknown1_pos++; + } + } + ch->resolution[i] = new_resolution; + } + + ch->unknown1_max = unknown1_pos; + ch->unknown2_max = unknown2_pos; + + memset(&ch->resolution[csf_count], 0, sizeof(ch->resolution[0]) * (HCA_SAMPLES_PER_SUBFRAME - csf_count)); +} + +/* HCADequantizer_CalculateGain */ +static void calculate_gain(stChannel* ch) { + int i; + const unsigned int csf_count = ch->coded_scalefactor_count; + + /* get actual scales to dequantize */ + for (i = 0; i < csf_count; i++) { + float scalefactor_scale = decode1_dequantizer_scaling_table[ ch->scalefactors[i] ]; + float resolution_scale = decode1_quantizer_step_size[ ch->resolution[i] ]; + ch->gain[i] = scalefactor_scale * resolution_scale; + } } //-------------------------------------------------- // Decode 2nd step //-------------------------------------------------- -static const unsigned char decode2_quantized_spectrum_max_bits[16] = { +/* coded resolution to max bits */ +static const unsigned char hcatbdecoder_max_bit_table[16] = { 0,2,3,3,4,4,4,4,5,6,7,8,9,10,11,12 }; -static const unsigned char decode2_quantized_spectrum_bits[128] = { +/* bits used for quant codes */ +static const unsigned char hcatbdecoder_read_bit_table[128] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0, 2,2,2,2,2,2,3,3,0,0,0,0,0,0,0,0, @@ -1268,7 +1447,8 @@ static const unsigned char decode2_quantized_spectrum_bits[128] = { 3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4, 3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4, }; -static const float decode2_quantized_spectrum_value[128] = { +/* code to quantized spectrum value */ +static const float hcatbdecoder_read_val_table[128] = { +0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0, +0,+0,+1,-1,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0, +0,+0,+1,+1,-1,-1,+2,-2,+0,+0,+0,+0,+0,+0,+0,+0, @@ -1279,31 +1459,32 @@ static const float decode2_quantized_spectrum_value[128] = { +0,+0,+1,-1,+2,-2,+3,-3,+4,-4,+5,-5,+6,-6,+7,-7, }; -static void decode2_dequantize_coefficients(stChannel *ch, clData *br) { - unsigned int i; +static void dequantize_coefficients(stChannel* ch, clData* br) { + int i; const unsigned int csf_count = ch->coded_scalefactor_count; for (i = 0; i < csf_count; i++) { float qc; unsigned char resolution = ch->resolution[i]; - unsigned char bits = decode2_quantized_spectrum_max_bits[resolution]; + unsigned char bits = hcatbdecoder_max_bit_table[resolution]; unsigned int code = bitreader_read(br, bits); /* read spectral coefficients */ - if (resolution < 8) { - /* use prefix codebooks for lower resolutions */ - code += resolution << 4; - bitreader_skip(br, decode2_quantized_spectrum_bits[code] - bits); - qc = decode2_quantized_spectrum_value[code]; - } - else { + if (resolution > 7) { /* parse values in sign-magnitude form (lowest bit = sign) */ int signed_code = (1 - ((code & 1) << 1)) * (code >> 1); /* move sign */ if (signed_code == 0) bitreader_skip(br, -1); /* zero uses one less bit since it has no sign */ qc = (float)signed_code; } + else { + /* use prefix codebooks for lower resolutions */ + int index = (resolution << 4) + code; + int skip = hcatbdecoder_read_bit_table[index] - bits; + bitreader_skip(br, skip); + qc = hcatbdecoder_read_val_table[index]; + } /* dequantize coef with gain */ ch->spectra[i] = ch->gain[i] * qc; @@ -1316,61 +1497,9 @@ static void decode2_dequantize_coefficients(stChannel *ch, clData *br) { //-------------------------------------------------- // Decode 3rd step //-------------------------------------------------- -static const unsigned int decode3_scale_conversion_table_int[128] = { - 0x00000000,0x00000000,0x32A0B051,0x32D61B5E,0x330EA43A,0x333E0F68,0x337D3E0C,0x33A8B6D5, - 0x33E0CCDF,0x3415C3FF,0x34478D75,0x3484F1F6,0x34B123F6,0x34EC0719,0x351D3EDA,0x355184DF, - 0x358B95C2,0x35B9FCD2,0x35F7D0DF,0x36251958,0x365BFBB8,0x36928E72,0x36C346CD,0x370218AF, - 0x372D583F,0x3766F85B,0x3799E046,0x37CD078C,0x3808980F,0x38360094,0x38728177,0x38A18FAF, - 0x38D744FD,0x390F6A81,0x393F179A,0x397E9E11,0x39A9A15B,0x39E2055B,0x3A16942D,0x3A48A2D8, - 0x3A85AAC3,0x3AB21A32,0x3AED4F30,0x3B1E196E,0x3B52A81E,0x3B8C57CA,0x3BBAFF5B,0x3BF9295A, - 0x3C25FED7,0x3C5D2D82,0x3C935A2B,0x3CC4563F,0x3D02CD87,0x3D2E4934,0x3D68396A,0x3D9AB62B, - 0x3DCE248C,0x3E0955EE,0x3E36FD92,0x3E73D290,0x3EA27043,0x3ED87039,0x3F1031DC,0x3F40213B, - - 0x3F800000,0x3FAA8D26,0x3FE33F89,0x4017657D,0x4049B9BE,0x40866491,0x40B311C4,0x40EE9910, - 0x411EF532,0x4153CCF1,0x418D1ADF,0x41BC034A,0x41FA83B3,0x4226E595,0x425E60F5,0x429426FF, - 0x42C5672A,0x43038359,0x432F3B79,0x43697C38,0x439B8D3A,0x43CF4319,0x440A14D5,0x4437FBF0, - 0x4475257D,0x44A3520F,0x44D99D16,0x4510FA4D,0x45412C4D,0x4580B1ED,0x45AB7A3A,0x45E47B6D, - 0x461837F0,0x464AD226,0x46871F62,0x46B40AAF,0x46EFE4BA,0x471FD228,0x4754F35B,0x478DDF04, - 0x47BD08A4,0x47FBDFED,0x4827CD94,0x485F9613,0x4894F4F0,0x48C67991,0x49043A29,0x49302F0E, - 0x496AC0C7,0x499C6573,0x49D06334,0x4A0AD4C6,0x4A38FBAF,0x4A767A41,0x4AA43516,0x4ADACB94, - 0x4B11C3D3,0x4B4238D2,0x4B8164D2,0x4BAC6897,0x4BE5B907,0x4C190B88,0x4C4BEC15,0x00000000, -}; -static const float *decode3_scale_conversion_table = (const float *)decode3_scale_conversion_table_int; - -static void decode3_reconstruct_high_frequency(stChannel *ch, - unsigned int hfr_group_count, unsigned int bands_per_hfr_group, - unsigned int stereo_band_count, unsigned int base_band_count, unsigned int total_band_count) { - if (ch->type == STEREO_SECONDARY) - return; - if (bands_per_hfr_group == 0) /* not in v1.x */ - return; - - { - unsigned int group, i; - unsigned int start_band = stereo_band_count + base_band_count; - unsigned int highband = start_band; - unsigned int lowband = start_band - 1; - - for (group = 0; group < hfr_group_count; group++) { - for (i = 0; i < bands_per_hfr_group && highband < total_band_count; i++) { - unsigned int sc_index = ch->hfr_scales[group] - ch->scalefactors[lowband] + 64; - ch->spectra[highband] = decode3_scale_conversion_table[sc_index] * ch->spectra[lowband]; - highband++; - lowband--; - } - } - - ch->spectra[HCA_SAMPLES_PER_SUBFRAME - 1] = 0; /* last spectral coefficient should be 0 */ - } -} - -//-------------------------------------------------- -// Decode 4th step -//-------------------------------------------------- -static const unsigned int decode4_intensity_ratio_table_int[80] = { - 0x40000000,0x3FEDB6DB,0x3FDB6DB7,0x3FC92492,0x3FB6DB6E,0x3FA49249,0x3F924925,0x3F800000, - 0x3F5B6DB7,0x3F36DB6E,0x3F124925,0x3EDB6DB7,0x3E924925,0x3E124925,0x00000000,0x00000000, - /* v2.0 seems to define indexes over 15, but intensity is packed in 4b thus unused */ +/* in lib this table does start with a single 0.0 and adds + 63 below + * (other decoders start with two 0.0 and add + 64 below, that should be equivalent) */ +static const unsigned int hcadecoder_scale_conversion_table_hex[128] = { 0x00000000,0x32A0B051,0x32D61B5E,0x330EA43A,0x333E0F68,0x337D3E0C,0x33A8B6D5,0x33E0CCDF, 0x3415C3FF,0x34478D75,0x3484F1F6,0x34B123F6,0x34EC0719,0x351D3EDA,0x355184DF,0x358B95C2, 0x35B9FCD2,0x35F7D0DF,0x36251958,0x365BFBB8,0x36928E72,0x36C346CD,0x370218AF,0x372D583F, @@ -1378,11 +1507,79 @@ static const unsigned int decode4_intensity_ratio_table_int[80] = { 0x390F6A81,0x393F179A,0x397E9E11,0x39A9A15B,0x39E2055B,0x3A16942D,0x3A48A2D8,0x3A85AAC3, 0x3AB21A32,0x3AED4F30,0x3B1E196E,0x3B52A81E,0x3B8C57CA,0x3BBAFF5B,0x3BF9295A,0x3C25FED7, 0x3C5D2D82,0x3C935A2B,0x3CC4563F,0x3D02CD87,0x3D2E4934,0x3D68396A,0x3D9AB62B,0x3DCE248C, - 0x3E0955EE,0x3E36FD92,0x3E73D290,0x3EA27043,0x3ED87039,0x3F1031DC,0x3F40213B,0x00000000, -}; -static const float *decode4_intensity_ratio_table = (const float *)decode4_intensity_ratio_table_int; + 0x3E0955EE,0x3E36FD92,0x3E73D290,0x3EA27043,0x3ED87039,0x3F1031DC,0x3F40213B,0x3F800000, -static void decode4_apply_intensity_stereo(stChannel *ch_pair, int subframe, + 0x3FAA8D26,0x3FE33F89,0x4017657D,0x4049B9BE,0x40866491,0x40B311C4,0x40EE9910,0x411EF532, + 0x4153CCF1,0x418D1ADF,0x41BC034A,0x41FA83B3,0x4226E595,0x425E60F5,0x429426FF,0x42C5672A, + 0x43038359,0x432F3B79,0x43697C38,0x439B8D3A,0x43CF4319,0x440A14D5,0x4437FBF0,0x4475257D, + 0x44A3520F,0x44D99D16,0x4510FA4D,0x45412C4D,0x4580B1ED,0x45AB7A3A,0x45E47B6D,0x461837F0, + 0x464AD226,0x46871F62,0x46B40AAF,0x46EFE4BA,0x471FD228,0x4754F35B,0x478DDF04,0x47BD08A4, + 0x47FBDFED,0x4827CD94,0x485F9613,0x4894F4F0,0x48C67991,0x49043A29,0x49302F0E,0x496AC0C7, + 0x499C6573,0x49D06334,0x4A0AD4C6,0x4A38FBAF,0x4A767A41,0x4AA43516,0x4ADACB94,0x4B11C3D3, + 0x4B4238D2,0x4B8164D2,0x4BAC6897,0x4BE5B907,0x4C190B88,0x4C4BEC15,0x00000000,0x00000000, +}; +static const float* hcadecoder_scale_conversion_table = (const float*)hcadecoder_scale_conversion_table_hex; + + +static void reconstruct_resolution_0(stChannel* ch, unsigned int min_resolution, int v3_flag) { + if (min_resolution > 0) /* added in v3.0 */ + return; + if (ch->type != STEREO_PRIMARY || v3_flag) + return; + + //TODO +} + +static void reconstruct_high_frequency(stChannel* ch, + unsigned int hfr_group_count, unsigned int bands_per_hfr_group, + unsigned int stereo_band_count, unsigned int base_band_count, unsigned int total_band_count) { + if (ch->type == STEREO_SECONDARY) + return; + if (bands_per_hfr_group == 0) /* added in v2.0 */ + return; + //if (stereo_band_count == 0) //todo affects some mono files, recheck + // return; + + //todo handle v3 + { + int i; + unsigned int group; + unsigned int start_band = stereo_band_count + base_band_count; + unsigned int highband = start_band; + unsigned int lowband = start_band - 1; + unsigned int sc_index; + + for (group = 0; group < hfr_group_count; group++) { + for (i = 0; i < bands_per_hfr_group; i++) { + if (highband >= total_band_count || lowband < 0) + break; + + sc_index = ch->hfr_scales[group] - ch->scalefactors[lowband] + 63; + /* as clamped in lib in v3.0, though in theory 6b scalefactors don't reach this */ + sc_index = sc_index & ~(sc_index >> 31); + + ch->spectra[highband] = hcadecoder_scale_conversion_table[sc_index] * ch->spectra[lowband]; + highband++; + lowband--; + } + } + + /* last spectrum coefficient is 0 (highband = 128 here, but perhaps could 'break' before) */ + ch->spectra[highband - 1] = 0.0f; + } +} + +//-------------------------------------------------- +// Decode 4th step +//-------------------------------------------------- + +static const unsigned int hcadecoder_intensity_ratio_table_hex[16] = { /* max 4b */ + 0x40000000,0x3FEDB6DB,0x3FDB6DB7,0x3FC92492,0x3FB6DB6E,0x3FA49249,0x3F924925,0x3F800000, + 0x3F5B6DB7,0x3F36DB6E,0x3F124925,0x3EDB6DB7,0x3E924925,0x3E124925,0x00000000,0x00000000, +}; +static const float* hcadecoder_intensity_ratio_table = (const float*)hcadecoder_intensity_ratio_table_hex; + +static void apply_intensity_stereo(stChannel* ch_pair, int subframe, unsigned int total_band_count, unsigned int base_band_count, unsigned int stereo_band_count) { if (ch_pair[0].type != STEREO_PRIMARY) return; @@ -1390,23 +1587,34 @@ static void decode4_apply_intensity_stereo(stChannel *ch_pair, int subframe, return; { - float ratio_l = decode4_intensity_ratio_table[ ch_pair[1].intensity[subframe] ]; - float ratio_r = ratio_l - 2.0f; - float *sp_l = ch_pair[0].spectra; - float *sp_r = ch_pair[1].spectra; + float ratio_l = hcadecoder_intensity_ratio_table[ ch_pair[1].intensity[subframe] ]; + float ratio_r = ratio_l - 2.0f; //todo 2.0 - ratio_l + float* sp_l = ch_pair[0].spectra; + float* sp_r = ch_pair[1].spectra; unsigned int band; for (band = base_band_count; band < total_band_count; band++) { sp_r[band] = sp_l[band] * ratio_r; sp_l[band] = sp_l[band] * ratio_l; } + + //todo lib seems to do extra processing here } } +static void apply_unknown_stereo(stChannel* ch_pair, int v3_flag, unsigned int base_band_count) { + if (!v3_flag) /* added in v3.0 */ + return; + if (ch_pair[0].type != STEREO_PRIMARY || base_band_count == 0) + return; + + //TODO +} + //-------------------------------------------------- // Decode 5th step //-------------------------------------------------- -static const unsigned int decode5_sin_tables_int[7][64] = { +static const unsigned int sin_tables_hex[7][64] = { { 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75, 0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75,0x3DA73D75, @@ -1472,8 +1680,7 @@ static const unsigned int decode5_sin_tables_int[7][64] = { 0x3F44E3F5,0x3F42DE29,0x3F40D0DA,0x3F3EBC1B,0x3F3CA003,0x3F3A7CA4,0x3F385216,0x3F36206C, } }; - -static const unsigned int decode5_cos_tables_int[7][64]={ +static const unsigned int cos_tables_hex[7][64]={ { 0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4, 0x3D0A8BD4,0xBD0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0xBD0A8BD4,0x3D0A8BD4,0x3D0A8BD4,0xBD0A8BD4, @@ -1541,7 +1748,7 @@ static const unsigned int decode5_cos_tables_int[7][64]={ }; /* HCA window function, close to a KBD window with an alpha of around 3.82 (similar to AAC/Vorbis) */ -static const unsigned int decode5_imdct_window_int[128] = { +static const unsigned int hcaimdct_window_float_hex[128] = { 0x3A3504F0,0x3B0183B8,0x3B70C538,0x3BBB9268,0x3C04A809,0x3C308200,0x3C61284C,0x3C8B3F17, 0x3CA83992,0x3CC77FBD,0x3CE91110,0x3D0677CD,0x3D198FC4,0x3D2DD35C,0x3D434643,0x3D59ECC1, 0x3D71CBA8,0x3D85741E,0x3D92A413,0x3DA078B4,0x3DAEF522,0x3DBE1C9E,0x3DCDF27B,0x3DDE7A1D, @@ -1560,15 +1767,15 @@ static const unsigned int decode5_imdct_window_int[128] = { 0xBF7FA32E,0xBF7FB57B,0xBF7FC4F6,0xBF7FD1ED,0xBF7FDCAD,0xBF7FE579,0xBF7FEC90,0xBF7FF22E, 0xBF7FF688,0xBF7FF9D0,0xBF7FFC32,0xBF7FFDDA,0xBF7FFEED,0xBF7FFF8F,0xBF7FFFDF,0xBF7FFFFC, }; -static const float *decode5_imdct_window = (const float *)decode5_imdct_window_int; +static const float* hcaimdct_window_float = (const float*)hcaimdct_window_float_hex; -static void decoder5_run_imdct(stChannel *ch, int subframe) { +/* apply DCT-IV to dequantized spectra */ +//HCAIMDCT_Transform +static void imdct_transform(stChannel* ch, int subframe) { static const unsigned int size = HCA_SAMPLES_PER_SUBFRAME; static const unsigned int half = HCA_SAMPLES_PER_SUBFRAME / 2; static const unsigned int mdct_bits = HCA_MDCT_BITS; - - /* apply DCT-IV to dequantized spectra */ { unsigned int i, j, k; unsigned int count1a, count2a, count1b, count2b; @@ -1582,9 +1789,9 @@ static void decoder5_run_imdct(stChannel *ch, int subframe) { count1a = 1; count2a = half; for (i = 0; i < mdct_bits; i++) { - float *swap; - float *d1 = &temp2a[0]; - float *d2 = &temp2a[count2a]; + float* swap; + float* d1 = &temp2a[0]; + float* d2 = &temp2a[count2a]; for (j = 0; j < count1a; j++) { for (k = 0; k < count2a; k++) { @@ -1609,13 +1816,13 @@ static void decoder5_run_imdct(stChannel *ch, int subframe) { count1b = half; count2b = 1; for (i = 0; i < mdct_bits; i++) { - const float *sin_table = (const float *) decode5_sin_tables_int[i]; - const float *cos_table = (const float *) decode5_cos_tables_int[i]; - float *swap; - float *d1 = temp2b; - float *d2 = &temp2b[count2b * 2 - 1]; - const float *s1 = &temp1b[0]; - const float *s2 = &temp1b[count2b]; + const float* sin_table = (const float*) sin_tables_hex[i];//todo cleanup + const float* cos_table = (const float*) cos_tables_hex[i]; + float* swap; + float* d1 = temp2b; + float* d2 = &temp2b[count2b * 2 - 1]; + const float* s1 = &temp1b[0]; + const float* s2 = &temp1b[count2b]; for (j = 0; j < count1b; j++) { for (k = 0; k < count2b; k++) { @@ -1646,22 +1853,22 @@ static void decoder5_run_imdct(stChannel *ch, int subframe) { } } - /* update output/imdct */ + /* update output/imdct (lib fuses this with the above) */ { unsigned int i; for (i = 0; i < half; i++) { - ch->wave[subframe][i] = decode5_imdct_window[i] * ch->dct[i + half] + ch->imdct_previous[i]; - ch->wave[subframe][i + half] = decode5_imdct_window[i + half] * ch->dct[size - 1 - i] - ch->imdct_previous[i + half]; - ch->imdct_previous[i] = decode5_imdct_window[size - 1 - i] * ch->dct[half - i - 1]; - ch->imdct_previous[i + half] = decode5_imdct_window[half - i - 1] * ch->dct[i]; + ch->wave[subframe][i] = hcaimdct_window_float[i] * ch->dct[i + half] + ch->imdct_previous[i]; + ch->wave[subframe][i + half] = hcaimdct_window_float[i + half] * ch->dct[size - 1 - i] - ch->imdct_previous[i + half]; + ch->imdct_previous[i] = hcaimdct_window_float[size - 1 - i] * ch->dct[half - i - 1]; + ch->imdct_previous[i + half] = hcaimdct_window_float[half - i - 1] * ch->dct[i]; } #if 0 /* over-optimized IMDCT (for reference), barely noticeable even when decoding hundred of files */ - const float *imdct_window = decode5_imdct_window; - const float *dct; - float *imdct_previous; - float *wave = ch->wave[subframe]; + const float* imdct_window = hcaimdct_window_float; + const float* dct; + float* imdct_previous; + float* wave = ch->wave[subframe]; dct = &ch->dct[half]; imdct_previous = ch->imdct_previous; diff --git a/src/coding/hca_decoder.c b/src/coding/hca_decoder.c index 48205b40..98fad8b3 100644 --- a/src/coding/hca_decoder.c +++ b/src/coding/hca_decoder.c @@ -117,6 +117,8 @@ void decode_hca(hca_codec_data* data, sample_t* outbuf, int32_t samples_to_do) { break; } + data->current_block++; + /* decode frame */ status = clHCA_DecodeBlock(data->handle, (void*)(data->data_buffer), blockSize); if (status < 0) { @@ -127,7 +129,6 @@ void decode_hca(hca_codec_data* data, sample_t* outbuf, int32_t samples_to_do) { /* extract samples */ clHCA_ReadSamples16(data->handle, data->sample_buffer); - data->current_block++; data->samples_consumed = 0; data->samples_filled += data->info.samplesPerBlock; } diff --git a/src/meta/hca_keys.h b/src/meta/hca_keys.h index 0f8aa016..fccf0dce 100644 --- a/src/meta/hca_keys.h +++ b/src/meta/hca_keys.h @@ -391,6 +391,9 @@ static const hcakey_info hcakey_list[] = { /* Sakura Kakumei (iOS/Android) */ {382759}, // 000000000005D727 + /* Uma Musume (Android) */ + {75923756697503}, // 0000450D608C479F + /* Dragalia Lost (iOS/Android) */ {2967411924141, subkeys_dgl, sizeof(subkeys_dgl) / sizeof(subkeys_dgl[0]) }, // 000002B2E7889CAD