├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── bit_allocation.c ├── bit_allocation.h ├── bit_reader.c ├── bit_reader.h ├── huffCodes.c ├── huffCodes.h ├── imdct.c ├── imdct.h ├── ldacdec.c ├── ldacdec.h ├── ldacenc.c ├── libldacdec.c ├── log.h ├── spectrum.c ├── spectrum.h ├── utility.c └── utility.h /.gitignore: -------------------------------------------------------------------------------- 1 | ldacdec 2 | ldacenc 3 | *.o 4 | *.d 5 | *.so 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "libldac"] 2 | path = libldac 3 | url = https://android.googlesource.com/platform/external/libldac 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Dirk Helbig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CROSS_COMPILE?= 2 | ASAN ?= false 3 | 4 | CC = $(CROSS_COMPILE)gcc 5 | 6 | GIT_VERSION ?= $(shell git describe --tags --abbrev=4 --dirty --always) 7 | 8 | CFLAGS = -MMD -MP -O3 -g -march=native 9 | CFLAGS += -DVERSION="\"$(GIT_VERSION)\"" 10 | CFLAGS += -std=gnu11 11 | CFLAGS += -Wall -Wextra 12 | CFLAGS += -Ilibldac/inc -Ilibldac/src 13 | #CFLAGS += -DDEBUG 14 | LDLIBS = -lm 15 | 16 | ifeq ($(ASAN),true) 17 | LCFLAGS += -fsanitize=address 18 | LDFLAGS += -fsanitize=address 19 | endif 20 | 21 | VPATH += libldac/src/ 22 | LDFLAGS += -L. 23 | 24 | all: ldacdec ldacenc 25 | 26 | libldacdec.so: LDFLAGS += -shared -fpic -Wl,-soname,libldacdec.so.1 27 | libldacdec.so: CFLAGS += -fpic 28 | libldacdec.so: libldacdec.o bit_allocation.o huffCodes.o bit_reader.o utility.o imdct.o spectrum.o 29 | 30 | ldacenc: ldacenc.o ldaclib.o ldacBT.o 31 | 32 | ldacenc: LDLIBS += $(shell pkg-config sndfile --libs) $(shell pkg-config samplerate --libs) 33 | ldacenc: ldacenc.o ldaclib.o ldacBT.o 34 | 35 | ldacdec: ldacdec.o libldacdec.so 36 | ldacdec: LDFLAGS += -Wl,-rpath=. 37 | ldacdec: LDLIBS += -lldacdec -lsndfile 38 | 39 | mdct_imdct: LDLIBS += $(shell pkg-config sndfile --libs) 40 | #mdct_imdct: CFLAGS += -DSINGLE_PRECISION 41 | mdct_imdct: mdct_imdct.o ldaclib.o imdct.o 42 | 43 | %.so: 44 | $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) 45 | 46 | .PHONY: clean 47 | clean: 48 | rm -f *.d *.o ldacenc ldacdec libldacdec.so 49 | 50 | -include *.d 51 | 52 | 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### LDAC decoder 2 | 3 | this is an early stage, jet functional LDAC audio stream decoder. 4 | 5 | Shout-out to [@Thealexbarney](https://github.com/Thealexbarney) for the heavy lifting. 6 | LDAC is basically a stripped down, streaming only ATRAC9. 7 | 8 | #### Build 9 | ```sh 10 | $ make 11 | ``` 12 | 13 | #### Usage 14 | see ldacdec.c for example usage 15 | 16 | #### ldacdec 17 | takes an LDAC stream and decodes it to WAV 18 | 19 | #### ldacenc 20 | uses Android LDAC encoder library to create LDAC streams from audio 21 | -------------------------------------------------------------------------------- /bit_allocation.c: -------------------------------------------------------------------------------- 1 | #include "bit_allocation.h" 2 | //#include "tables.h" 3 | #include "utility.h" 4 | #include 5 | #include 6 | 7 | /*************************************************************************************************** 8 | Resampled Gradient Table 9 | ***************************************************************************************************/ 10 | const uint8_t gradientCurves[50][50] = { 11 | { 12 | 128,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 13 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 14 | }, 15 | { 16 | 31,225,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 17 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 18 | }, 19 | { 20 | 17,128,239,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 21 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 22 | }, 23 | { 24 | 12, 69,187,244,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 25 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 26 | }, 27 | { 28 | 10, 43,128,213,246,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 29 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 30 | }, 31 | { 32 | 9, 31, 87,169,225,247,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 33 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 34 | }, 35 | { 36 | 8, 24, 62,128,194,232,248,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 37 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 38 | }, 39 | { 40 | 8, 19, 47, 97,159,209,237,248,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 41 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 42 | }, 43 | { 44 | 7, 17, 37, 75,128,181,219,239,249,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 45 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 46 | }, 47 | { 48 | 7, 15, 31, 59,103,153,197,225,241,249,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 49 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 50 | }, 51 | { 52 | 7, 13, 26, 48, 83,128,173,208,230,243,249,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 53 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 54 | }, 55 | { 56 | 6, 12, 23, 41, 69,107,149,187,215,233,244,250,255,255,255,255,255,255,255,255,255,255,255,255,255, 57 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 58 | }, 59 | { 60 | 6, 11, 20, 35, 58, 90,128,166,198,221,236,245,250,255,255,255,255,255,255,255,255,255,255,255,255, 61 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 62 | }, 63 | { 64 | 6, 11, 18, 31, 49, 76,110,146,180,207,225,238,245,250,255,255,255,255,255,255,255,255,255,255,255, 65 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 66 | }, 67 | { 68 | 6, 10, 17, 27, 43, 66, 95,128,161,190,213,229,239,246,250,255,255,255,255,255,255,255,255,255,255, 69 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 70 | }, 71 | { 72 | 6, 10, 15, 24, 38, 57, 82,112,144,174,199,218,232,241,246,250,255,255,255,255,255,255,255,255,255, 73 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 74 | }, 75 | { 76 | 6, 9, 14, 22, 34, 50, 72, 98,128,158,184,206,222,234,242,247,250,255,255,255,255,255,255,255,255, 77 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 78 | }, 79 | { 80 | 6, 9, 13, 20, 31, 45, 63, 87,114,142,169,193,211,225,236,243,247,250,255,255,255,255,255,255,255, 81 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 82 | }, 83 | { 84 | 6, 9, 13, 19, 28, 40, 56, 77,101,128,155,179,200,216,228,237,243,247,250,255,255,255,255,255,255, 85 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 86 | }, 87 | { 88 | 6, 8, 12, 18, 26, 36, 51, 69, 91,115,141,165,187,205,220,230,238,244,248,250,255,255,255,255,255, 89 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 90 | }, 91 | { 92 | 6, 8, 12, 17, 24, 33, 46, 62, 81,104,128,152,175,194,210,223,232,239,244,248,250,255,255,255,255, 93 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 94 | }, 95 | { 96 | 6, 8, 11, 16, 22, 31, 42, 56, 74, 94,116,140,162,182,200,214,225,234,240,245,248,250,255,255,255, 97 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 98 | }, 99 | { 100 | 5, 8, 11, 15, 21, 28, 38, 51, 67, 85,106,128,150,171,189,205,218,228,235,241,245,248,251,255,255, 101 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 102 | }, 103 | { 104 | 5, 8, 10, 14, 19, 26, 35, 47, 61, 78, 97,117,139,159,178,195,209,221,230,237,242,246,248,251,255, 105 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 106 | }, 107 | { 108 | 5, 7, 10, 14, 18, 25, 33, 43, 56, 71, 88,108,128,148,168,185,200,213,223,231,238,242,246,249,251, 109 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 110 | }, 111 | { 112 | 5, 7, 10, 13, 17, 23, 31, 40, 51, 65, 81, 99,118,138,157,175,191,205,216,225,233,239,243,246,249, 113 | 251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 114 | }, 115 | { 116 | 5, 7, 9, 13, 17, 22, 29, 37, 47, 60, 75, 91,109,128,147,165,181,196,209,219,227,234,239,243,247, 117 | 249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 118 | }, 119 | { 120 | 5, 7, 9, 12, 16, 21, 27, 35, 44, 55, 69, 84,101,119,137,155,172,187,201,212,221,229,235,240,244, 121 | 247,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 122 | }, 123 | { 124 | 5, 7, 9, 12, 15, 20, 25, 32, 41, 51, 64, 78, 94,110,128,146,162,178,192,205,215,224,231,236,241, 125 | 244,247,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 126 | }, 127 | { 128 | 5, 7, 9, 11, 15, 19, 24, 31, 38, 48, 59, 72, 87,103,119,137,153,169,184,197,208,218,225,232,237, 129 | 241,245,247,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 130 | }, 131 | { 132 | 5, 7, 9, 11, 14, 18, 23, 29, 36, 45, 55, 67, 81, 96,112,128,144,160,175,189,201,211,220,227,233, 133 | 238,242,245,247,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 134 | }, 135 | { 136 | 5, 7, 8, 11, 14, 17, 22, 27, 34, 42, 52, 63, 75, 89,104,120,136,152,167,181,193,204,214,222,229, 137 | 234,239,242,245,248,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 138 | }, 139 | { 140 | 5, 7, 8, 11, 13, 17, 21, 26, 32, 40, 48, 59, 70, 83, 98,113,128,143,158,173,186,197,208,216,224, 141 | 230,235,239,243,245,248,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 142 | }, 143 | { 144 | 5, 7, 8, 10, 13, 16, 20, 25, 31, 37, 46, 55, 66, 78, 91,106,120,136,150,165,178,190,201,210,219, 145 | 225,231,236,240,243,246,248,249,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 146 | }, 147 | { 148 | 5, 6, 8, 10, 12, 15, 19, 24, 29, 35, 43, 52, 62, 73, 86, 99,113,128,143,157,170,183,194,204,213, 149 | 221,227,232,237,241,244,246,248,250,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 150 | }, 151 | { 152 | 5, 6, 8, 10, 12, 15, 18, 23, 28, 34, 41, 49, 58, 69, 81, 93,107,121,135,149,163,175,187,198,207, 153 | 215,222,228,233,238,241,244,246,248,250,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 154 | }, 155 | { 156 | 5, 6, 8, 10, 12, 15, 18, 22, 26, 32, 39, 46, 55, 65, 76, 88,101,114,128,142,155,168,180,191,201, 157 | 210,217,224,230,234,238,241,244,246,248,250,251,255,255,255,255,255,255,255,255,255,255,255,255,255, 158 | }, 159 | { 160 | 5, 6, 8, 9, 12, 14, 17, 21, 25, 31, 37, 44, 52, 61, 72, 83, 95,108,121,135,148,161,173,184,195, 161 | 204,212,219,225,231,235,239,242,244,247,248,250,251,255,255,255,255,255,255,255,255,255,255,255,255, 162 | }, 163 | { 164 | 5, 6, 8, 9, 11, 14, 17, 20, 24, 29, 35, 42, 49, 58, 68, 78, 90,102,115,128,141,154,166,178,188, 165 | 198,207,214,221,227,232,236,239,242,245,247,248,250,251,255,255,255,255,255,255,255,255,255,255,255, 166 | }, 167 | { 168 | 5, 6, 8, 9, 11, 13, 16, 19, 23, 28, 33, 40, 47, 55, 64, 74, 85, 97,109,122,134,147,159,171,182, 169 | 192,201,209,216,223,228,233,237,240,243,245,247,248,250,251,255,255,255,255,255,255,255,255,255,255, 170 | }, 171 | { 172 | 5, 6, 7, 9, 11, 13, 16, 19, 22, 27, 32, 38, 44, 52, 61, 70, 80, 92,103,116,128,140,153,164,176, 173 | 186,195,204,212,218,224,229,234,237,240,243,245,247,249,250,251,255,255,255,255,255,255,255,255,255, 174 | }, 175 | { 176 | 5, 6, 7, 9, 11, 13, 15, 18, 22, 26, 31, 36, 42, 49, 58, 66, 76, 87, 98,110,122,134,146,158,169, 177 | 180,190,198,207,214,220,225,230,234,238,241,243,245,247,249,250,251,255,255,255,255,255,255,255,255, 178 | }, 179 | { 180 | 5, 6, 7, 9, 10, 12, 15, 18, 21, 25, 29, 34, 40, 47, 55, 63, 72, 82, 93,104,116,128,140,152,163, 181 | 174,184,193,201,209,216,222,227,231,235,238,241,244,246,247,249,250,251,255,255,255,255,255,255,255, 182 | }, 183 | { 184 | 5, 6, 7, 9, 10, 12, 14, 17, 20, 24, 28, 33, 39, 45, 52, 60, 69, 78, 89, 99,111,122,134,145,157, 185 | 167,178,187,196,204,211,217,223,228,232,236,239,242,244,246,247,249,250,251,255,255,255,255,255,255, 186 | }, 187 | { 188 | 5, 6, 7, 8, 10, 12, 14, 17, 20, 23, 27, 32, 37, 43, 50, 57, 66, 75, 84, 95,105,117,128,139,151, 189 | 161,172,181,190,199,206,213,219,224,229,233,236,239,242,244,246,248,249,250,251,255,255,255,255,255, 190 | }, 191 | { 192 | 5, 6, 7, 8, 10, 12, 14, 16, 19, 22, 26, 31, 36, 41, 48, 55, 62, 71, 80, 90,101,111,122,134,145, 193 | 155,166,176,185,194,201,208,215,220,225,230,234,237,240,242,244,246,248,249,250,251,255,255,255,255, 194 | }, 195 | { 196 | 5, 6, 7, 8, 10, 11, 13, 16, 18, 22, 25, 29, 34, 39, 45, 52, 60, 68, 77, 86, 96,106,117,128,139, 197 | 150,160,170,179,188,196,204,211,217,222,227,231,234,238,240,243,245,246,248,249,250,251,255,255,255, 198 | }, 199 | { 200 | 5, 6, 7, 8, 10, 11, 13, 15, 18, 21, 24, 28, 33, 38, 44, 50, 57, 65, 73, 82, 92,102,112,123,133, 201 | 144,154,164,174,183,191,199,206,212,218,223,228,232,235,238,241,243,245,246,248,249,250,251,255,255, 202 | }, 203 | { 204 | 5, 6, 7, 8, 9, 11, 13, 15, 17, 20, 24, 27, 32, 36, 42, 48, 55, 62, 70, 78, 88, 97,107,118,128, 205 | 138,149,159,168,178,186,194,201,208,214,220,224,229,232,236,239,241,243,245,247,248,249,250,251,255, 206 | }, 207 | { 208 | 5, 6, 7, 8, 9, 11, 13, 15, 17, 20, 23, 26, 31, 35, 40, 46, 52, 59, 67, 75, 84, 93,103,113,123, 209 | 133,143,153,163,172,181,189,197,204,210,216,221,225,230,233,236,239,241,243,245,247,248,249,250,251, 210 | }, 211 | }; 212 | 213 | static unsigned char GradientCurves[50][50]; 214 | #if 0 215 | At9Status CreateGradient(Block* block) 216 | { 217 | int valueCount = block->GradientEndValue - block->GradientStartValue; 218 | int unitCount = block->GradientEndUnit - block->GradientStartUnit; 219 | 220 | for (int i = 0; i < block->GradientEndUnit; i++) 221 | { 222 | block->Gradient[i] = block->GradientStartValue; 223 | } 224 | 225 | for (int i = block->GradientEndUnit; i <= block->QuantizationUnitCount; i++) 226 | { 227 | block->Gradient[i] = block->GradientEndValue; 228 | } 229 | if (unitCount <= 0) return ERR_SUCCESS; 230 | if (valueCount == 0) return ERR_SUCCESS; 231 | 232 | const unsigned char* curve = GradientCurves[unitCount - 1]; 233 | if (valueCount <= 0) 234 | { 235 | double scale = (-valueCount - 1) / 31.0; 236 | int baseVal = block->GradientStartValue - 1; 237 | for (int i = block->GradientStartUnit; i < block->GradientEndUnit; i++) 238 | { 239 | block->Gradient[i] = baseVal - (int)(curve[i - block->GradientStartUnit] * scale); 240 | } 241 | } 242 | else 243 | { 244 | double scale = (valueCount - 1) / 31.0; 245 | int baseVal = block->GradientStartValue + 1; 246 | for (int i = block->GradientStartUnit; i < block->GradientEndUnit; i++) 247 | { 248 | block->Gradient[i] = baseVal + (int)(curve[i - block->GradientStartUnit] * scale); 249 | } 250 | } 251 | 252 | return ERR_SUCCESS; 253 | } 254 | 255 | void CalculateMask(Channel* channel) 256 | { 257 | memset(channel->PrecisionMask, 0, sizeof(channel->PrecisionMask)); 258 | for (int i = 1; i < channel->Block->QuantizationUnitCount; i++) 259 | { 260 | const int delta = channel->ScaleFactors[i] - channel->ScaleFactors[i - 1]; 261 | if (delta > 1) 262 | { 263 | channel->PrecisionMask[i] += Min(delta - 1, 5); 264 | } 265 | else if (delta < -1) 266 | { 267 | channel->PrecisionMask[i - 1] += Min(delta * -1 - 1, 5); 268 | } 269 | } 270 | } 271 | 272 | void CalculatePrecisions(Channel* channel) 273 | { 274 | Block* block = channel->Block; 275 | 276 | if (block->GradientMode != 0) 277 | { 278 | for (int i = 0; i < block->QuantizationUnitCount; i++) 279 | { 280 | channel->Precisions[i] = channel->ScaleFactors[i] + channel->PrecisionMask[i] - block->Gradient[i]; 281 | if (channel->Precisions[i] > 0) 282 | { 283 | switch (block->GradientMode) 284 | { 285 | case 1: 286 | channel->Precisions[i] /= 2; 287 | break; 288 | case 2: 289 | channel->Precisions[i] = 3 * channel->Precisions[i] / 8; 290 | break; 291 | case 3: 292 | channel->Precisions[i] /= 4; 293 | break; 294 | } 295 | } 296 | } 297 | } 298 | else 299 | { 300 | for (int i = 0; i < block->QuantizationUnitCount; i++) 301 | { 302 | channel->Precisions[i] = channel->ScaleFactors[i] - block->Gradient[i]; 303 | } 304 | } 305 | 306 | for (int i = 0; i < block->QuantizationUnitCount; i++) 307 | { 308 | if (channel->Precisions[i] < 1) 309 | { 310 | channel->Precisions[i] = 1; 311 | } 312 | } 313 | 314 | for (int i = 0; i < block->GradientBoundary; i++) 315 | { 316 | channel->Precisions[i]++; 317 | } 318 | 319 | for (int i = 0; i < block->QuantizationUnitCount; i++) 320 | { 321 | channel->PrecisionsFine[i] = 0; 322 | if (channel->Precisions[i] > 15) 323 | { 324 | channel->PrecisionsFine[i] = channel->Precisions[i] - 15; 325 | channel->Precisions[i] = 15; 326 | } 327 | } 328 | } 329 | #endif 330 | #if 0 331 | static const unsigned char BaseCurve[48] = 332 | { 333 | 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 334 | 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 26, 27, 27, 28, 28, 28, 29, 29, 335 | 29, 29, 30, 30, 30, 30 336 | }; 337 | #endif 338 | static const unsigned char BaseCurve[50] = 339 | { 340 | 5, 6, 7, 8, 9, 11, 13, 15, 17, 20, 23 341 | }; 342 | 343 | 344 | 345 | void GenerateGradientCurves() 346 | { 347 | const int baseLength = sizeof(BaseCurve) / sizeof(BaseCurve[0]); 348 | 349 | for (int length = 1; length <= baseLength; length++) 350 | { 351 | for (int i = 0; i < length; i++) 352 | { 353 | GradientCurves[length - 1][i] = BaseCurve[i * baseLength / length]; 354 | } 355 | } 356 | 357 | for( int i=0; i<50; ++i ) 358 | { 359 | for(int j=0; j<48; ++j ) 360 | printf("%2d ", GradientCurves[i][j] ); 361 | printf("\n"); 362 | } 363 | 364 | } 365 | -------------------------------------------------------------------------------- /bit_allocation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | //#include "unpack.h" 5 | 6 | #if 0 7 | At9Status CreateGradient(Block* block); 8 | void CalculateMask(Channel* channel); 9 | void CalculatePrecisions(Channel* channel); 10 | #endif 11 | extern const uint8_t gradientCurves[50][50]; 12 | void GenerateGradientCurves(); 13 | -------------------------------------------------------------------------------- /bit_reader.c: -------------------------------------------------------------------------------- 1 | #include "bit_reader.h" 2 | #include "utility.h" 3 | 4 | static int PeekIntFallback(BitReaderCxt* br, int bitCount); 5 | 6 | void InitBitReaderCxt(BitReaderCxt* br, const void * buffer) 7 | { 8 | br->Buffer = buffer; 9 | br->Position = 0; 10 | } 11 | 12 | uint32_t ReadInt(BitReaderCxt* br, const int bits) 13 | { 14 | const uint32_t value = PeekInt(br, bits); 15 | br->Position += bits; 16 | return value; 17 | } 18 | 19 | int32_t ReadSignedInt(BitReaderCxt* br, const int bits) 20 | { 21 | const int value = PeekInt(br, bits); 22 | br->Position += bits; 23 | return SignExtend32(value, bits); 24 | } 25 | 26 | int ReadOffsetBinary(BitReaderCxt* br, const int bits) 27 | { 28 | const int offset = 1 << (bits - 1); 29 | const int value = PeekInt(br, bits) - offset; 30 | br->Position += bits; 31 | return value; 32 | } 33 | 34 | uint32_t PeekInt(BitReaderCxt* br, const int bits) 35 | { 36 | const unsigned int byteIndex = br->Position / 8; 37 | const unsigned int bitIndex = br->Position % 8; 38 | const uint8_t* buffer = br->Buffer; 39 | 40 | if (bits <= 9) 41 | { 42 | uint32_t value = buffer[byteIndex] << 8 | buffer[byteIndex + 1]; 43 | value &= 0xFFFF >> bitIndex; 44 | value >>= 16 - bits - bitIndex; 45 | return value; 46 | } 47 | 48 | if (bits <= 17) 49 | { 50 | uint32_t value = buffer[byteIndex] << 16 | buffer[byteIndex + 1] << 8 | buffer[byteIndex + 2]; 51 | value &= 0xFFFFFF >> bitIndex; 52 | value >>= 24 - bits - bitIndex; 53 | return value; 54 | } 55 | 56 | if (bits <= 25) 57 | { 58 | uint32_t value = buffer[byteIndex] << 24 59 | | buffer[byteIndex + 1] << 16 60 | | buffer[byteIndex + 2] << 8 61 | | buffer[byteIndex + 3]; 62 | 63 | value &= (int)(0xFFFFFFFF >> bitIndex); 64 | value >>= 32 - bits - bitIndex; 65 | return value; 66 | } 67 | return PeekIntFallback(br, bits); 68 | } 69 | 70 | void AlignPosition(BitReaderCxt* br, const unsigned int multiple) 71 | { 72 | const int position = br->Position; 73 | if (position % multiple == 0) 74 | { 75 | return; 76 | } 77 | 78 | br->Position = position + multiple - position % multiple; 79 | } 80 | 81 | static int PeekIntFallback(BitReaderCxt* br, int bitCount) 82 | { 83 | int value = 0; 84 | int byteIndex = br->Position / 8; 85 | int bitIndex = br->Position % 8; 86 | const unsigned char* buffer = br->Buffer; 87 | 88 | while (bitCount > 0) 89 | { 90 | if (bitIndex >= 8) 91 | { 92 | bitIndex = 0; 93 | byteIndex++; 94 | } 95 | 96 | int bitsToRead = bitCount; 97 | if (bitsToRead > 8 - bitIndex) 98 | { 99 | bitsToRead = 8 - bitIndex; 100 | } 101 | 102 | const int mask = 0xFF >> bitIndex; 103 | const int currentByte = (mask & buffer[byteIndex]) >> (8 - bitIndex - bitsToRead); 104 | 105 | value = (value << bitsToRead) | currentByte; 106 | bitIndex += bitsToRead; 107 | bitCount -= bitsToRead; 108 | } 109 | return value; 110 | } 111 | -------------------------------------------------------------------------------- /bit_reader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | typedef struct { 6 | const uint8_t * Buffer; 7 | int Position; 8 | } BitReaderCxt; 9 | 10 | // Make MSVC compiler happy. Leave const in for value parameters 11 | 12 | void InitBitReaderCxt(BitReaderCxt* br, const void * buffer); 13 | uint32_t PeekInt(BitReaderCxt* br, const int bits); 14 | uint32_t ReadInt(BitReaderCxt* br, const int bits); 15 | int32_t ReadSignedInt(BitReaderCxt* br, const int bits); 16 | int32_t ReadOffsetBinary(BitReaderCxt* br, const int bits); 17 | void AlignPosition(BitReaderCxt* br, const unsigned int multiple); 18 | -------------------------------------------------------------------------------- /huffCodes.c: -------------------------------------------------------------------------------- 1 | #include "huffCodes.h" 2 | #include "utility.h" 3 | #include 4 | 5 | int ReadHuffmanValue(const HuffmanCodebook* huff, BitReaderCxt* br, int isSigned) 6 | { 7 | const int code = PeekInt(br, huff->MaxBitSize); 8 | const unsigned char value = huff->Lookup[code]; 9 | const int bits = huff->Bits[value]; 10 | br->Position += bits; 11 | return isSigned ? SignExtend32(value, huff->ValueBits) : value; 12 | } 13 | 14 | void DecodeHuffmanValues(int* spectrum, int index, int bandCount, const HuffmanCodebook* huff, const int* values) 15 | { 16 | const int valueCount = bandCount >> huff->ValueCountPower; 17 | const int mask = (1 << huff->ValueBits) - 1; 18 | 19 | for (int i = 0; i < valueCount; i++) 20 | { 21 | int value = values[i]; 22 | for (int j = 0; j < huff->ValueCount; j++) 23 | { 24 | spectrum[index++] = SignExtend32(value & mask, huff->ValueBits); 25 | value >>= huff->ValueBits; 26 | } 27 | } 28 | } 29 | 30 | void InitHuffmanCodebook(const HuffmanCodebook* codebook) 31 | { 32 | const int huffLength = codebook->Length; 33 | if (huffLength == 0) return; 34 | 35 | unsigned char* dest = codebook->Lookup; 36 | 37 | for (int i = 0; i < huffLength; i++) 38 | { 39 | if (codebook->Bits[i] == 0) continue; 40 | const int unusedBits = codebook->MaxBitSize - codebook->Bits[i]; 41 | 42 | const int start = codebook->Codes[i] << unusedBits; 43 | const int length = 1 << unusedBits; 44 | const int end = start + length; 45 | 46 | for (int j = start; j < end; j++) 47 | { 48 | dest[j] = i; 49 | } 50 | } 51 | } 52 | 53 | static const uint8_t ScaleFactorsA3Bits[8] = 54 | { 55 | 2, 2, 4, 6, 6, 5, 3, 2 56 | }; 57 | 58 | static const uint16_t ScaleFactorsA3Codes[8] = 59 | { 60 | 0x00, 0x01, 0x0E, 0x3E, 0x3F, 0x1E, 0x06, 0x02 61 | }; 62 | 63 | static const uint8_t ScaleFactorsA4Bits[16] = 64 | { 65 | 2, 2, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 6, 5, 4, 2 66 | }; 67 | 68 | static const uint16_t ScaleFactorsA4Codes[16] = 69 | { 70 | 0x01, 0x02, 0x00, 0x06, 0x0F, 0x13, 0x23, 0x24, 0x25, 0x22, 0x21, 0x20, 0x0E, 0x05, 0x01, 0x03 71 | }; 72 | 73 | static const uint8_t ScaleFactorsA5Bits[32] = 74 | { 75 | 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 76 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 6, 5, 5, 4, 3 77 | }; 78 | 79 | static const uint16_t ScaleFactorsA5Codes[32] = 80 | { 81 | 0x02, 0x01, 0x07, 0x0D, 0x0C, 0x18, 0x1B, 0x21, 0x3F, 0x6A, 0x6B, 0x68, 0x73, 0x79, 0x7C, 0x7D, 82 | 0x7A, 0x7B, 0x78, 0x72, 0x44, 0x45, 0x47, 0x46, 0x69, 0x38, 0x20, 0x1D, 0x19, 0x09, 0x05, 0x00 83 | }; 84 | 85 | static const uint8_t ScaleFactorsA6Bits[64] = 86 | { 87 | 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 8, 8, 88 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 89 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 90 | 8, 8, 8, 8, 8, 7, 7, 7, 6, 6, 5, 5, 5, 4, 4, 4 91 | }; 92 | 93 | static const uint16_t ScaleFactorsA6Codes[64] = 94 | { 95 | 0x00, 0x01, 0x04, 0x05, 0x12, 0x13, 0x2E, 0x2F, 0x30, 0x66, 0x67, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 96 | 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 97 | 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 98 | 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x68, 0x69, 0x6A, 0x31, 0x32, 0x14, 0x15, 0x16, 0x06, 0x07, 0x08 99 | }; 100 | 101 | static const uint8_t ScaleFactorsB2Bits[4] = 102 | { 103 | 1, 2, 0, 2 104 | }; 105 | 106 | static const uint16_t ScaleFactorsB2Codes[4] = 107 | { 108 | 0x00, 0x03, 0x00, 0x02 109 | }; 110 | 111 | static const uint8_t ScaleFactorsB3Bits[8] = 112 | { 113 | 1, 3, 5, 6, 0, 6, 4, 2 114 | }; 115 | 116 | static const uint16_t ScaleFactorsB3Codes[8] = 117 | { 118 | 0x01, 0x00, 0x04, 0x0B, 0x00, 0x0A, 0x03, 0x01 119 | }; 120 | 121 | static const uint8_t ScaleFactorsB4Bits[16] = 122 | { 123 | 1, 3, 4, 5, 5, 7, 8, 8, 0, 8, 8, 7, 6, 6, 4, 3 124 | }; 125 | 126 | static const uint16_t ScaleFactorsB4Codes[16] = 127 | { 128 | 0x01, 0x01, 0x04, 0x0E, 0x0F, 0x2C, 0x5A, 0x5D, 0x00, 0x5C, 0x5B, 0x2F, 0x15, 0x14, 0x06, 0x00 129 | }; 130 | 131 | static const uint8_t ScaleFactorsB5Bits[32] = 132 | { 133 | 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 6, 7, 7, 7, 8, 8, 134 | 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 3 135 | }; 136 | 137 | static const uint16_t ScaleFactorsB5Codes[32] = 138 | { 139 | 0x00, 0x05, 0x07, 0x0C, 0x04, 0x02, 0x03, 0x05, 0x09, 0x10, 0x23, 0x33, 0x36, 0x6E, 0x60, 0x65, 140 | 0x62, 0x61, 0x63, 0x64, 0x6F, 0x6D, 0x6C, 0x6B, 0x6A, 0x68, 0x69, 0x45, 0x44, 0x37, 0x1A, 0x07 141 | }; 142 | 143 | static uint8_t ScaleFactorsA3Lookup[64]; 144 | static uint8_t ScaleFactorsA4Lookup[256]; 145 | static uint8_t ScaleFactorsA5Lookup[256]; 146 | static uint8_t ScaleFactorsA6Lookup[256]; 147 | 148 | static uint8_t ScaleFactorsB2Lookup[4]; 149 | static uint8_t ScaleFactorsB3Lookup[64]; 150 | static uint8_t ScaleFactorsB4Lookup[256]; 151 | static uint8_t ScaleFactorsB5Lookup[256]; 152 | 153 | HuffmanCodebook HuffmanScaleFactorsUnsigned[7] = { 154 | {0}, 155 | {0}, 156 | {0}, 157 | {ScaleFactorsA3Bits, ScaleFactorsA3Codes, ScaleFactorsA3Lookup, 8, 1, 0, 3, 8, 6}, 158 | {ScaleFactorsA4Bits, ScaleFactorsA4Codes, ScaleFactorsA4Lookup, 16, 1, 0, 4, 16, 8}, 159 | {ScaleFactorsA5Bits, ScaleFactorsA5Codes, ScaleFactorsA5Lookup, 32, 1, 0, 5, 32, 8}, 160 | {ScaleFactorsA6Bits, ScaleFactorsA6Codes, ScaleFactorsA6Lookup, 64, 1, 0, 6, 64, 8}, 161 | }; 162 | 163 | HuffmanCodebook HuffmanScaleFactorsSigned[6] = { 164 | {0}, 165 | {0}, 166 | {ScaleFactorsB2Bits, ScaleFactorsB2Codes, ScaleFactorsB2Lookup, 4, 1, 0, 2, 4, 2}, 167 | {ScaleFactorsB3Bits, ScaleFactorsB3Codes, ScaleFactorsB3Lookup, 8, 1, 0, 3, 8, 6}, 168 | {ScaleFactorsB4Bits, ScaleFactorsB4Codes, ScaleFactorsB4Lookup, 16, 1, 0, 4, 16, 8}, 169 | {ScaleFactorsB5Bits, ScaleFactorsB5Codes, ScaleFactorsB5Lookup, 32, 1, 0, 5, 32, 8}, 170 | }; 171 | 172 | static void InitHuffmanSet(const HuffmanCodebook* codebooks, int count) 173 | { 174 | for (int i = 0; i < count; i++) 175 | { 176 | InitHuffmanCodebook(&codebooks[i]); 177 | } 178 | } 179 | 180 | void InitHuffmanCodebooks() 181 | { 182 | InitHuffmanSet(HuffmanScaleFactorsUnsigned, sizeof(HuffmanScaleFactorsUnsigned) / sizeof(HuffmanCodebook)); 183 | InitHuffmanSet(HuffmanScaleFactorsSigned, sizeof(HuffmanScaleFactorsSigned) / sizeof(HuffmanCodebook)); 184 | } 185 | 186 | 187 | -------------------------------------------------------------------------------- /huffCodes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "bit_reader.h" 4 | 5 | typedef struct 6 | { 7 | const unsigned char* Bits; 8 | const unsigned short* Codes; 9 | unsigned char* Lookup; 10 | const int Length; 11 | const int ValueCount; 12 | const int ValueCountPower; 13 | const int ValueBits; 14 | const int ValueMax; 15 | const int MaxBitSize; 16 | } HuffmanCodebook; 17 | 18 | int ReadHuffmanValue(const HuffmanCodebook* huff, BitReaderCxt* br, int isSigned); 19 | void DecodeHuffmanValues(int* spectrum, int index, int bandCount, const HuffmanCodebook* huff, const int* values); 20 | void InitHuffmanCodebook(const HuffmanCodebook* codebook); 21 | 22 | extern HuffmanCodebook HuffmanScaleFactorsUnsigned[7]; 23 | extern HuffmanCodebook HuffmanScaleFactorsSigned[6]; 24 | extern HuffmanCodebook HuffmanSpectrum[2][8][4]; 25 | 26 | void InitHuffmanCodebooks(); 27 | 28 | -------------------------------------------------------------------------------- /imdct.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "ldacdec.h" 5 | #include "utility.h" 6 | 7 | double MdctWindow[3][256]; 8 | double ImdctWindow[3][256]; 9 | double SinTables[9][256]; 10 | double CosTables[9][256]; 11 | int ShuffleTables[9][256]; 12 | 13 | static void GenerateTrigTables(int sizeBits) 14 | { 15 | const int size = 1 << sizeBits; 16 | double* sinTab = SinTables[sizeBits]; 17 | double* cosTab = CosTables[sizeBits]; 18 | 19 | for (int i = 0; i < size; i++) 20 | { 21 | const double value = M_PI * (4 * i + 1) / (4 * size); 22 | sinTab[i] = sin(value); 23 | cosTab[i] = cos(value); 24 | } 25 | } 26 | 27 | static void GenerateShuffleTable(int sizeBits) 28 | { 29 | const int size = 1 << sizeBits; 30 | int* table = ShuffleTables[sizeBits]; 31 | 32 | for (int i = 0; i < size; i++) 33 | { 34 | table[i] = BitReverse32(i ^ (i / 2), sizeBits); 35 | } 36 | } 37 | 38 | static void GenerateMdctWindow(int frameSizePower) 39 | { 40 | const int frameSize = 1 << frameSizePower; 41 | double* mdct = MdctWindow[frameSizePower - 6]; 42 | 43 | for (int i = 0; i < frameSize; i++) 44 | { 45 | mdct[i] = (sin(((i + 0.5) / frameSize - 0.5) * M_PI) + 1.0) * 0.5; 46 | } 47 | } 48 | 49 | static void GenerateImdctWindow(int frameSizePower) 50 | { 51 | const int frameSize = 1 << frameSizePower; 52 | double* imdct = ImdctWindow[frameSizePower - 6]; 53 | double* mdct = MdctWindow[frameSizePower - 6]; 54 | 55 | for (int i = 0; i < frameSize; i++) 56 | { 57 | imdct[i] = mdct[i] / (mdct[frameSize - 1 - i] * mdct[frameSize - 1 - i] + mdct[i] * mdct[i]); 58 | } 59 | } 60 | 61 | void InitMdct() 62 | { 63 | for (int i = 0; i < 9; i++) 64 | { 65 | GenerateTrigTables(i); 66 | GenerateShuffleTable(i); 67 | } 68 | GenerateMdctWindow(7); 69 | GenerateImdctWindow(7); 70 | 71 | GenerateMdctWindow(8); 72 | GenerateImdctWindow(8); 73 | } 74 | 75 | 76 | static void Dct4(Mdct* mdct, float* input, float* output); 77 | 78 | void RunImdct(Mdct* mdct, float* input, float* output) 79 | { 80 | const int size = 1 << mdct->Bits; 81 | const int half = size / 2; 82 | float dctOut[MAX_FRAME_SAMPLES] = { 0.f }; 83 | const double* window = ImdctWindow[mdct->Bits - 6]; 84 | double* previous = mdct->ImdctPrevious; 85 | 86 | Dct4(mdct, input, dctOut); 87 | 88 | for (int i = 0; i < half; i++) 89 | { 90 | output[i] = window[i] * dctOut[i + half] + previous[i]; 91 | output[i + half] = window[i + half] * -dctOut[size - 1 - i] - previous[i + half]; 92 | previous[i] = window[size - 1 - i] * -dctOut[half - i - 1]; 93 | previous[i + half] = window[half - i - 1] * dctOut[i]; 94 | } 95 | } 96 | 97 | static void Dct4(Mdct* mdct, float* input, float* output) 98 | { 99 | int MdctBits = mdct->Bits; 100 | int MdctSize = 1 << MdctBits; 101 | const int* shuffleTable = ShuffleTables[MdctBits]; 102 | const double* sinTable = SinTables[MdctBits]; 103 | const double* cosTable = CosTables[MdctBits]; 104 | double dctTemp[MAX_FRAME_SAMPLES]; 105 | 106 | int size = MdctSize; 107 | int lastIndex = size - 1; 108 | int halfSize = size / 2; 109 | 110 | for (int i = 0; i < halfSize; i++) 111 | { 112 | int i2 = i * 2; 113 | double a = input[i2]; 114 | double b = input[lastIndex - i2]; 115 | double sin = sinTable[i]; 116 | double cos = cosTable[i]; 117 | dctTemp[i2] = a * cos + b * sin; 118 | dctTemp[i2 + 1] = a * sin - b * cos; 119 | } 120 | int stageCount = MdctBits - 1; 121 | 122 | for (int stage = 0; stage < stageCount; stage++) 123 | { 124 | int blockCount = 1 << stage; 125 | int blockSizeBits = stageCount - stage; 126 | int blockHalfSizeBits = blockSizeBits - 1; 127 | int blockSize = 1 << blockSizeBits; 128 | int blockHalfSize = 1 << blockHalfSizeBits; 129 | sinTable = SinTables[blockHalfSizeBits]; 130 | cosTable = CosTables[blockHalfSizeBits]; 131 | 132 | for (int block = 0; block < blockCount; block++) 133 | { 134 | for (int i = 0; i < blockHalfSize; i++) 135 | { 136 | int frontPos = (block * blockSize + i) * 2; 137 | int backPos = frontPos + blockSize; 138 | double a = dctTemp[frontPos] - dctTemp[backPos]; 139 | double b = dctTemp[frontPos + 1] - dctTemp[backPos + 1]; 140 | double sin = sinTable[i]; 141 | double cos = cosTable[i]; 142 | dctTemp[frontPos] += dctTemp[backPos]; 143 | dctTemp[frontPos + 1] += dctTemp[backPos + 1]; 144 | dctTemp[backPos] = a * cos + b * sin; 145 | dctTemp[backPos + 1] = a * sin - b * cos; 146 | } 147 | } 148 | } 149 | 150 | for (int i = 0; i < MdctSize; i++) 151 | { 152 | output[i] = dctTemp[shuffleTable[i]]; 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /imdct.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef struct { 4 | int Bits; 5 | int Size; 6 | double Scale; 7 | double ImdctPrevious[MAX_FRAME_SAMPLES]; 8 | double* Window; 9 | double* SinTable; 10 | double* CosTable; 11 | } Mdct; 12 | 13 | void InitMdct(); 14 | void RunImdct(Mdct* mdct, float* input, float* output); 15 | 16 | -------------------------------------------------------------------------------- /ldacdec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | #include "ldacdec.h" 11 | 12 | #include "sndfile.h" 13 | 14 | SNDFILE *openAudioFile( const char *fileName, int freq, int channels ) 15 | { 16 | SF_INFO sfinfo = { 17 | .samplerate = freq, 18 | .channels = channels, 19 | .format = SF_FORMAT_WAV | SF_FORMAT_PCM_16, 20 | }; 21 | printf("opening \"%s\" for writing ...\n", fileName ); 22 | printf("sampling frequency: %d\n", freq ); 23 | printf("channel count: %d\n", channels ); 24 | SNDFILE *out = sf_open( fileName, SFM_WRITE, &sfinfo ); 25 | if( out == NULL ) 26 | { 27 | printf("can't open output: %s\n", sf_strerror( NULL ) ); 28 | return NULL; 29 | } 30 | return out; 31 | } 32 | 33 | 34 | #define BUFFER_SIZE (680*2) 35 | #define PCM_BUFFER_SIZE (256*2) 36 | 37 | int main(int argc, char *args[] ) 38 | { 39 | if( argc < 2 ) 40 | { 41 | printf("usage:\n\t%s \n", args[0] ); 42 | return EXIT_SUCCESS; 43 | } 44 | const char *inputFile = args[1]; 45 | const char *audioFile = "output.wav"; 46 | 47 | if( argc > 2 ) 48 | audioFile = args[2]; 49 | 50 | printf("opening \"%s\" ...\n", inputFile ); 51 | 52 | ldacdec_t dec; 53 | ldacdecInit( &dec ); 54 | 55 | FILE *in = fopen( inputFile, "rb" ); 56 | if( in == NULL ) 57 | { 58 | perror("can't open stream file"); 59 | return EXIT_FAILURE; 60 | } 61 | 62 | SNDFILE *out = NULL; 63 | 64 | uint8_t buf[BUFFER_SIZE]; 65 | uint8_t *ptr = NULL; 66 | int16_t pcm[PCM_BUFFER_SIZE] = { 0 }; 67 | size_t filePosition = 0; 68 | int blockId = 0; 69 | int bytesInBuffer = 0; 70 | while(1) 71 | { 72 | LOG("%ld =>\n", filePosition ); 73 | int ret = fseek( in, filePosition, SEEK_SET ); 74 | if( ret < 0 ) 75 | break; 76 | bytesInBuffer = fread( buf, 1, BUFFER_SIZE, in ); 77 | ptr = buf; 78 | if( bytesInBuffer == 0 ) 79 | break; 80 | #if 1 81 | if(ptr[1] == 0xAA) 82 | { 83 | ptr++; 84 | filePosition++; 85 | } 86 | #endif 87 | LOG("sync: %02x\n", ptr[0] ); 88 | int bytesUsed = 0; 89 | 90 | memset( pcm, 0, sizeof(pcm) ); 91 | LOG("count === %4d ===\n", blockId++ ); 92 | ret = ldacDecode( &dec, ptr, pcm, &bytesUsed ); 93 | if( ret < 0 ) 94 | break; 95 | LOG_ARRAY( pcm, "%4d, " ); 96 | if( out == NULL ) 97 | { 98 | printf("auto detect format!\n"); 99 | out = openAudioFile( audioFile, ldacdecGetSampleRate( &dec ), ldacdecGetChannelCount( &dec ) ); 100 | if( out == NULL ) 101 | return EXIT_FAILURE; 102 | } 103 | 104 | sf_writef_short( out, pcm, dec.frame.frameSamples ); 105 | filePosition += bytesUsed; 106 | } 107 | 108 | printf("done.\n"); 109 | 110 | sf_close( out ); 111 | fclose(in); 112 | 113 | return EXIT_SUCCESS; 114 | } 115 | -------------------------------------------------------------------------------- /ldacdec.h: -------------------------------------------------------------------------------- 1 | #ifndef __LDACDEC_H_ 2 | #define __LDACDEC_H_ 3 | 4 | #define MAX_QUANT_UNITS (34) 5 | #define MAX_FRAME_SAMPLES (256) 6 | 7 | #include "log.h" 8 | #include "imdct.h" 9 | 10 | #define container_of( ptr, type, member ) ({ \ 11 | const typeof( ((type*)0)->member ) *__mptr = (ptr); \ 12 | (type *)((char*)__ptr - offsetof(type, memeber)); \ 13 | }) 14 | 15 | #define min( a, b ) \ 16 | ({ __typeof__ (a) _a = (a); \ 17 | __typeof__ (b) _b = (b); \ 18 | _a < _b ? _a : _b; }); 19 | 20 | #define max( a, b ) \ 21 | ({ __typeof__ (a) _a = (a); \ 22 | __typeof__ (b) _b = (b); \ 23 | _a > _b ? _a : _b; }); 24 | 25 | typedef struct Frame frame_t; 26 | typedef struct Channel channel_t; 27 | 28 | struct Channel { 29 | frame_t *frame; 30 | int scaleFactorMode; 31 | int scaleFactorBitlen; 32 | int scaleFactorOffset; 33 | int scaleFactorWeight; 34 | 35 | int scaleFactors[MAX_QUANT_UNITS]; 36 | 37 | int precisions[MAX_QUANT_UNITS]; 38 | int precisionsFine[MAX_QUANT_UNITS]; 39 | int precisionMask[MAX_QUANT_UNITS]; 40 | 41 | int quantizedSpectra[MAX_FRAME_SAMPLES]; 42 | int quantizedSpectraFine[MAX_FRAME_SAMPLES]; 43 | 44 | float spectra[MAX_FRAME_SAMPLES]; 45 | float pcm[MAX_FRAME_SAMPLES]; 46 | Mdct mdct; 47 | }; 48 | 49 | struct Frame { 50 | int sampleRateId; 51 | int channelConfigId; 52 | int frameLength; 53 | int frameStatus; 54 | int frameSamplesPower; 55 | int frameSamples; 56 | 57 | int nbrBands; 58 | 59 | // gradient data 60 | int gradient[MAX_QUANT_UNITS]; 61 | int gradientMode; 62 | int gradientStartUnit; 63 | int gradientEndUnit; 64 | int gradientStartValue; 65 | int gradientEndValue; 66 | int gradientBoundary; 67 | 68 | int quantizationUnitCount; 69 | 70 | int channelCount; 71 | channel_t channels[2]; 72 | }; 73 | 74 | typedef struct { 75 | frame_t frame; 76 | 77 | } ldacdec_t; 78 | 79 | int ldacdecInit( ldacdec_t *this ); 80 | int ldacDecode( ldacdec_t *this, uint8_t *stream, int16_t *pcm, int *bytesUsed ); 81 | int ldacNullPacket( ldacdec_t *this, uint8_t *output, int *bytesUsed ); 82 | int ldacdecGetSampleRate( ldacdec_t *this ); 83 | int ldacdecGetChannelCount( ldacdec_t *this ); 84 | 85 | #endif // __LDACDEC_H_ 86 | -------------------------------------------------------------------------------- /ldacenc.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | #include "ldacBT.h" 16 | 17 | void do_ldac(SNDFILE *in, SF_INFO *info, FILE *out, int eqmid ) 18 | { 19 | const int channels = info->channels; 20 | float pcmSamples[128*channels]; 21 | int pcm_used = 0; 22 | uint8_t ldacFrame[1024] = { 0 }; 23 | int ldac_stream_size = 0; 24 | int ldac_frame_num = 0; 25 | HANDLE_LDAC_BT h = ldacBT_get_handle(); 26 | float *buf = pcmSamples; 27 | 28 | const int bytesPeerFrame = sizeof(float) * channels; 29 | fflush(stdout); 30 | assert( channels == 2 ); 31 | int ret = ldacBT_init_handle_encode(h, 679, eqmid, LDACBT_CHANNEL_MODE_STEREO, LDACBT_SMPL_FMT_F32, info->samplerate); 32 | if( ret < 0 ) 33 | { 34 | printf("ldacBT_init_handler_encode failed! error code %d\n", ldacBT_get_error_code( h ) ); 35 | return; 36 | } 37 | size_t frameCount = 0; 38 | while( 1 ) 39 | { 40 | if( buf == NULL ) 41 | break; 42 | 43 | int count = sf_readf_float( in, pcmSamples, 128 ); 44 | frameCount += count; 45 | 46 | // pad samples in case we can't fill all at the end of file 47 | if( count < 128 ) 48 | { 49 | memset( (uint8_t*)pcmSamples+bytesPeerFrame*count, 0, (128-count) * bytesPeerFrame ); 50 | } 51 | 52 | // flush codec if no samples left 53 | if( count == 0 ) 54 | buf = NULL; 55 | 56 | int error = ldacBT_encode(h, buf, &pcm_used, ldacFrame, &ldac_stream_size, &ldac_frame_num); 57 | if( error < 0 ) 58 | { 59 | printf("ldacBT_encode failed : %d\n", ldacBT_get_error_code( h ) ); 60 | break; 61 | } 62 | if( ldac_stream_size > 0 ) 63 | fwrite( ldacFrame, ldac_stream_size, 1, out ); 64 | 65 | } 66 | printf("done.\n"); 67 | ldacBT_close_handle(h); 68 | } 69 | 70 | void do_ldac_resample(SNDFILE *in, SF_INFO *info, int new_sample_rate, FILE *out, int eqmid ) 71 | { 72 | // resampling 73 | int converter = SRC_SINC_BEST_QUALITY; 74 | int error = 0; 75 | SRC_STATE *src_state = NULL; 76 | SRC_DATA src_data = { 0 }; 77 | float src_ratio = 1.f; 78 | int channels = info->channels; 79 | 80 | /* Initialize the sample rate converter. */ 81 | if ((src_state = src_new (converter, channels, &error)) == NULL) 82 | { 83 | printf ("Error : src_new() failed : %s.\n", src_strerror (error)) ; 84 | return; 85 | } 86 | 87 | if (new_sample_rate > 0) 88 | { 89 | src_ratio = (1.0 * new_sample_rate) / info->samplerate ; 90 | } 91 | 92 | if (fabs (src_ratio - 1.0) < 1e-20) 93 | { 94 | printf ("Target samplerate and input samplerate are the same. Exiting.\n"); 95 | return; 96 | } 97 | 98 | // printf ("SRC Ratio : %f\n", src_ratio) ; 99 | // printf ("Converter : %s\n\n", src_get_name (converter)) ; 100 | 101 | if (src_is_valid_ratio (src_ratio) == 0) 102 | { 103 | printf ("Error : Sample rate change out of valid range.\n"); 104 | return; 105 | } 106 | 107 | HANDLE_LDAC_BT h = ldacBT_get_handle(); 108 | assert( channels == 2 ); 109 | int ret = ldacBT_init_handle_encode(h, 679, eqmid, LDACBT_CHANNEL_MODE_STEREO, LDACBT_SMPL_FMT_F32, new_sample_rate); 110 | if( ret < 0 ) 111 | { 112 | printf("ldacBT_init_handler_encode failed! error code %d\n", ldacBT_get_error_code( h ) ); 113 | return; 114 | } 115 | 116 | size_t frameCount = 0; 117 | const int bytesPeerFrame = sizeof(float) * channels; 118 | const ssize_t bufFrames = 1000000; 119 | float *pcmSamples = malloc( bytesPeerFrame * bufFrames ); 120 | float output[128*2]; // max input for encoder 121 | 122 | src_data.end_of_input = SF_FALSE; 123 | src_data.input_frames = 0; 124 | src_data.src_ratio = src_ratio; 125 | src_data.data_in = pcmSamples; 126 | src_data.data_out = output; 127 | src_data.output_frames = 128; 128 | int pcm_used = 0; 129 | int ldac_stream_size = 0; 130 | int ldac_frame_num = 0; 131 | uint8_t ldacFrame[1024] = { 0 }; 132 | float *ptrOut = output; 133 | while( 1 ) 134 | { 135 | /* Terminate if done. */ 136 | if( ptrOut == NULL ) 137 | { 138 | printf("done.\n"); 139 | break; 140 | } 141 | 142 | if( src_data.end_of_input == SF_TRUE ) 143 | { 144 | ptrOut = NULL; 145 | } 146 | 147 | /* If the input buffer is empty, refill it. */ 148 | if (src_data.input_frames == 0) 149 | { 150 | src_data.input_frames = sf_readf_float (in, pcmSamples, bufFrames) ; 151 | src_data.data_in = pcmSamples; 152 | frameCount += src_data.input_frames; 153 | /* The last read will not be a full buffer, so snd_of_input. */ 154 | if (src_data.input_frames < bufFrames) 155 | src_data.end_of_input = SF_TRUE; 156 | printf("%3.0f%%", (float)frameCount/info->frames*100.f ); 157 | fflush( stdout ); 158 | printf("\b\b\b\b"); 159 | } 160 | if ((error = src_process (src_state, &src_data))) 161 | { 162 | printf ("src_process faild : %s\n", src_strerror (error)) ; 163 | break; 164 | } 165 | 166 | if( (ptrOut != NULL) && (src_data.output_frames_gen == 0) ) 167 | continue; 168 | 169 | // pad last frame if needed 170 | if( (src_data.end_of_input == SF_TRUE) && (src_data.output_frames_gen < 128) ) 171 | { 172 | memset( (uint8_t*)output + src_data.output_frames_gen * bytesPeerFrame, 0, (128-src_data.output_frames_gen) * bytesPeerFrame ); 173 | } 174 | 175 | if( src_data.end_of_input == SF_FALSE ) 176 | assert( src_data.output_frames_gen == 128 ); 177 | 178 | error = ldacBT_encode(h, ptrOut, &pcm_used, ldacFrame, &ldac_stream_size, &ldac_frame_num); 179 | if( error < 0 ) 180 | { 181 | printf("ldacBT_encode failed : %d\n", ldacBT_get_error_code( h ) ); 182 | break; 183 | } 184 | if( ldac_stream_size > 0 ) 185 | fwrite( ldacFrame, ldac_stream_size, 1, out ); 186 | 187 | src_data.data_in += src_data.input_frames_used * channels; 188 | src_data.input_frames -= src_data.input_frames_used; 189 | } 190 | 191 | free( pcmSamples ); 192 | 193 | ldacBT_close_handle(h); 194 | 195 | src_delete(src_state); 196 | } 197 | 198 | 199 | static char short_options[] = "hr:q:v"; 200 | 201 | static struct option long_options[] = { 202 | {"help", no_argument, NULL, 'h'}, 203 | {"rate", required_argument, NULL, 'r'}, 204 | {"eqmi", required_argument, NULL, 'q'}, 205 | {"version", no_argument, NULL, 'v'}, 206 | 207 | {0, 0, 0, 0} 208 | }; 209 | 210 | static char *help_options[] = { 211 | "print (this) help.", 212 | "sample rate of encoded stream", 213 | "encode quality mode index", 214 | "print version", 215 | }; 216 | 217 | static const int sampleRates[] = 218 | { 219 | 44100, 220 | 48000, 221 | 88200, 222 | 96000, 223 | }; 224 | 225 | static int sampleRateSupported( const int rate ) 226 | { 227 | for( size_t i=0; i fname && 273 | *end != '.' && 274 | *end != '\\' && 275 | *end != '/' ) 276 | { 277 | --end; 278 | } 279 | 280 | if( (end > fname && *end == '.') && 281 | (*(end -1) != '\\' && *(end-1) != '/') ) 282 | { 283 | *end = '\0'; 284 | } 285 | } 286 | 287 | static void printVersion() 288 | { 289 | printf("ldacenc %s\n", VERSION ); 290 | } 291 | 292 | static void usage( char *progName ) 293 | { 294 | int i; 295 | printVersion(); 296 | printf( "\nusage:\n" ); 297 | printf( "%s [options] \n\n", progName ); 298 | for( i=0; long_options[i].name != 0; i++) 299 | { 300 | printf("--%s|-%c\t\t%s\n", long_options[i].name, long_options[i].val, help_options[i] ); 301 | } 302 | 303 | printf("\nsupported sample rates: "); 304 | for( size_t i=0; i 0 ) 318 | { 319 | switch (c) 320 | { 321 | case 'r': 322 | sampleRate = atoi(optarg); 323 | if( !sampleRateSupported( sampleRate ) ) 324 | { 325 | printf("invalid sample rate!\n"); 326 | usage( args[0] ); 327 | return EXIT_FAILURE; 328 | } 329 | break; 330 | 331 | case 'q': 332 | eqmi = atoi(optarg); 333 | if( !eqmiSupported( eqmi ) ) 334 | { 335 | fprintf(stderr, "invalid encode quality mode index!\n"); 336 | usage( args[0] ); 337 | return EXIT_FAILURE; 338 | } 339 | break; 340 | 341 | case 'v': 342 | printVersion(); 343 | break; 344 | 345 | case '?': 346 | case 'h': 347 | default: 348 | usage( args[0] ); 349 | return EXIT_FAILURE; 350 | } 351 | } 352 | 353 | if( optind < argc ) 354 | { 355 | printf("current settings:\n"); 356 | printf("sample rate: %d\n", sampleRate ); 357 | printf("encode quality mode index: %s\n", eqmiToString(eqmi) ); 358 | 359 | do 360 | { 361 | const char *fileName = args[optind]; 362 | char outFileName[255] = { 0 }; 363 | strncpy( outFileName, basename( fileName ), 254 ); 364 | strip_ext( outFileName ); 365 | strcat( outFileName, ".ldac" ); 366 | 367 | printf("convert: \"%s\" -> \"%s\" ", fileName, outFileName ); 368 | SNDFILE *in = NULL; 369 | SF_INFO sfinfo = { 0 }; 370 | in = sf_open( fileName, SFM_READ, &sfinfo ); 371 | if( in == NULL ) 372 | { 373 | printf("sndfile: can't open file: %s\n", sf_strerror( NULL ) ); 374 | return EXIT_FAILURE; 375 | } 376 | 377 | FILE *out = fopen( outFileName, "wb" ); 378 | if( out == NULL ) 379 | { 380 | perror("can't open output file!"); 381 | return EXIT_FAILURE; 382 | } 383 | 384 | if( (sampleRate < 0) && sampleRateSupported( sfinfo.samplerate ) ) 385 | { 386 | do_ldac( in, &sfinfo, out, eqmi ); 387 | } else if( (sampleRate > 0) && (sampleRate != sfinfo.samplerate) ) 388 | { 389 | do_ldac_resample( in, &sfinfo, sampleRate, out, eqmi ); 390 | } else 391 | { 392 | printf( "not supported sample frequency (%dHz)\n", sfinfo.samplerate ); 393 | } 394 | 395 | fclose( out ); 396 | sf_close( in ); 397 | } while( ++optind < argc ); 398 | } else 399 | { 400 | usage( args[0] ); 401 | return EXIT_FAILURE; 402 | } 403 | 404 | return EXIT_SUCCESS; 405 | } 406 | 407 | -------------------------------------------------------------------------------- /libldacdec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "ldacdec.h" 5 | #include "log.h" 6 | #include "utility.h" 7 | #include "bit_reader.h" 8 | #include "imdct.h" 9 | #include "huffCodes.h" 10 | #include "spectrum.h" 11 | #include "bit_allocation.h" 12 | 13 | #define LDAC_SYNCWORDBITS (8) 14 | #define LDAC_SYNCWORD (0xAA) 15 | /** Sampling Rate **/ 16 | #define LDAC_SMPLRATEBITS (3) 17 | /** Channel **/ 18 | #define LDAC_CHCONFIG2BITS (2) 19 | enum CHANNEL { 20 | MONO = 0, 21 | STEREO = 1 22 | }; 23 | /** Frame Length **/ 24 | #define LDAC_FRAMELEN2BITS (9) 25 | /** Frame Status **/ 26 | #define LDAC_FRAMESTATBITS (2) 27 | 28 | /** Band Info **/ 29 | #define LDAC_NBANDBITS (4) 30 | #define LDAC_BAND_OFFSET (2) 31 | 32 | /** Band **/ 33 | #define LDAC_MAXNBANDS 16 34 | 35 | /* Flag */ 36 | #define LDAC_FLAGBITS (1) 37 | #define LDAC_TRUE (1) 38 | #define LDAC_FALSE (0) 39 | 40 | /* Mode */ 41 | #define LDAC_MODE_0 (0) 42 | #define LDAC_MODE_1 (1) 43 | #define LDAC_MODE_2 (2) 44 | #define LDAC_MODE_3 (3) 45 | 46 | /** Gradient Data **/ 47 | #define LDAC_GRADMODEBITS (2) 48 | #define LDAC_GRADOSBITS (5) 49 | #define LDAC_GRADQU0BITS (6) 50 | #define LDAC_GRADQU1BITS (5) 51 | #define LDAC_NADJQUBITS (5) 52 | 53 | /** Scale Factor Data **/ 54 | #define LDAC_SFCMODEBITS 1 55 | #define LDAC_IDSFBITS 5 56 | #define LDAC_NSFCWTBL 8 57 | #define LDAC_SFCBLENBITS 2 58 | #define LDAC_MINSFCBLEN_0 3 59 | #define LDAC_MINSFCBLEN_1 2 60 | #define LDAC_MINSFCBLEN_2 2 61 | #define LDAC_SFCWTBLBITS 3 62 | 63 | #define LDAC_MINIDWL1 1 64 | #define LDAC_MAXIDWL1 15 65 | #define LDAC_MAXIDWL2 15 66 | 67 | // Quantization Units 68 | #define LDAC_MAXNQUS 34 69 | #define LDAC_MAXGRADQU 50 70 | 71 | /*************************************************************************************************** 72 | Weighting Tables for Scale Factor Data 73 | ***************************************************************************************************/ 74 | static const uint8_t gaa_sfcwgt_ldac[LDAC_NSFCWTBL][LDAC_MAXNQUS] = { 75 | { 76 | 1, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 77 | 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 78 | }, 79 | { 80 | 0, 1, 1, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 81 | 7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 82 | }, 83 | { 84 | 0, 1, 1, 2, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 85 | 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, 86 | }, 87 | { 88 | 0, 1, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 89 | 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 90 | }, 91 | { 92 | 0, 1, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 93 | 10, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 94 | }, 95 | { 96 | 1, 0, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8, 8, 8, 97 | 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 98 | }, 99 | { 100 | 0, 0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 101 | 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 7, 7, 7, 8, 9, 9, 9, 9, 102 | }, 103 | { 104 | 0, 0, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8, 105 | 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 106 | }, 107 | }; 108 | 109 | static const uint8_t ga_nqus_ldac[LDAC_MAXNBANDS+1] = { 110 | 0, 4, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25, 26, 28, 30, 32, 34, 111 | }; 112 | 113 | 114 | int ldacdecInit( ldacdec_t *this ) 115 | { 116 | InitHuffmanCodebooks(); 117 | InitMdct(); 118 | 119 | memset( &this->frame.channels[0].mdct, 0, sizeof( Mdct ) ); 120 | memset( &this->frame.channels[1].mdct, 0, sizeof( Mdct ) ); 121 | 122 | this->frame.channels[0].frame = &this->frame; 123 | this->frame.channels[1].frame = &this->frame; 124 | 125 | return 0; 126 | } 127 | 128 | static int decodeBand( frame_t *this, BitReaderCxt *br ) 129 | { 130 | this->nbrBands = ReadInt( br, LDAC_NBANDBITS ) + LDAC_BAND_OFFSET; 131 | LOG("nbrBands: %d\n", this->nbrBands ); 132 | ReadInt( br, LDAC_FLAGBITS ); // unused 133 | 134 | this->quantizationUnitCount = ga_nqus_ldac[this->nbrBands]; 135 | return 0; 136 | } 137 | 138 | static int decodeGradient( frame_t *this, BitReaderCxt *br ) 139 | { 140 | this->gradientMode = ReadInt( br, LDAC_GRADMODEBITS ); 141 | if( this->gradientMode == LDAC_MODE_0 ) 142 | { 143 | this->gradientStartUnit = ReadInt( br, LDAC_GRADQU0BITS ); 144 | this->gradientEndUnit = ReadInt( br, LDAC_GRADQU0BITS ) + 1; 145 | this->gradientStartValue = ReadInt( br, LDAC_GRADOSBITS ); 146 | this->gradientEndValue = ReadInt( br, LDAC_GRADOSBITS ); 147 | LOG("gradient\n\tqu [%3d,%3d]\n\tvalue [%3d,%3d]\n", 148 | this->gradientStartUnit, this->gradientEndUnit, 149 | this->gradientStartValue, this->gradientEndValue ); 150 | } else 151 | { 152 | this->gradientStartUnit = ReadInt( br, LDAC_GRADQU1BITS ); 153 | this->gradientEndUnit = 26; 154 | this->gradientStartValue = ReadInt( br, LDAC_GRADOSBITS ); 155 | this->gradientEndValue = 31; 156 | LOG("gradient\n\tqu [%3d,%3d\n\tvalue [%3d,%3d]\n", 157 | this->gradientStartUnit, this->gradientEndUnit, 158 | this->gradientStartValue, this->gradientEndValue ); 159 | } 160 | 161 | this->gradientBoundary = ReadInt( br, LDAC_NADJQUBITS ); 162 | return 0; 163 | } 164 | 165 | static void calculateGradient( frame_t *this ) 166 | { 167 | int valueCount = this->gradientEndValue - this->gradientStartValue; 168 | int unitCount = this->gradientEndUnit - this->gradientStartUnit; 169 | 170 | memset( this->gradient, 0, sizeof( this->gradient ) ); 171 | 172 | for( int i=0; igradientEndUnit; ++i ) 173 | this->gradient[i] = -this->gradientStartValue; 174 | for( int i=this->gradientEndUnit; iquantizationUnitCount; ++i ) 175 | this->gradient[i] = -this->gradientEndValue; 176 | 177 | if( unitCount > 0 ) 178 | { 179 | const uint8_t *curve = gradientCurves[unitCount-1]; 180 | if( valueCount > 0 ) 181 | { 182 | for( int i=this->gradientStartUnit; igradientEndUnit; ++i ) 183 | { 184 | this->gradient[i] -= ((curve[i-this->gradientStartUnit] * (valueCount-1)) >> 8) + 1; 185 | } 186 | } else if( valueCount < 0 ) 187 | { 188 | for( int i=this->gradientStartUnit; igradientEndUnit; ++i ) 189 | { 190 | this->gradient[i] -= ((curve[i-this->gradientStartUnit] * (valueCount-1)) >> 8) + 1; 191 | } 192 | } 193 | } 194 | 195 | LOG_ARRAY_LEN( this->gradient, "%3d, ", this->quantizationUnitCount ); 196 | } 197 | 198 | static void calculatePrecisionMask(channel_t* this) 199 | { 200 | memset(this->precisionMask, 0, sizeof(this->precisionMask)); 201 | for (int i = 1; i < this->frame->quantizationUnitCount; i++) 202 | { 203 | const int delta = this->scaleFactors[i] - this->scaleFactors[i - 1]; 204 | if (delta > 1) 205 | { 206 | this->precisionMask[i] += min(delta - 1, 5); 207 | } 208 | else if (delta < -1) 209 | { 210 | this->precisionMask[i - 1] += min(delta * -1 - 1, 5); 211 | } 212 | } 213 | } 214 | 215 | 216 | static void calculatePrecisions( channel_t *this ) 217 | { 218 | frame_t *frame = this->frame; 219 | 220 | for( int i=0; iquantizationUnitCount; ++i ) 221 | { 222 | switch( frame->gradientMode ) 223 | { 224 | case LDAC_MODE_0: 225 | { 226 | int precision = this->scaleFactors[i] + frame->gradient[i]; 227 | precision = max( precision, LDAC_MINIDWL1 ); 228 | this->precisions[i] = precision; 229 | break; 230 | } 231 | case LDAC_MODE_1: 232 | { 233 | int precision = this->scaleFactors[i] + frame->gradient[i] + this->precisionMask[i]; 234 | if( precision > 0 ) 235 | precision /= 2; 236 | precision = max( precision, LDAC_MINIDWL1 ); 237 | this->precisions[i] = precision; 238 | break; 239 | } 240 | case LDAC_MODE_2: 241 | { 242 | int precision = this->scaleFactors[i] + frame->gradient[i] + this->precisionMask[i]; 243 | if( precision > 0 ) 244 | precision = ( precision * 3 ) / 8; 245 | precision = max( precision, LDAC_MINIDWL1 ); 246 | this->precisions[i] = precision; 247 | break; 248 | } 249 | case LDAC_MODE_3: 250 | { 251 | int precision = this->scaleFactors[i] + frame->gradient[i] + this->precisionMask[i]; 252 | if( precision > 0 ) 253 | precision /= 4; 254 | precision = max( precision, LDAC_MINIDWL1 ); 255 | this->precisions[i] = precision; 256 | break; 257 | } 258 | default: 259 | assert(0); 260 | break; 261 | } 262 | } 263 | 264 | for( int i=0; igradientBoundary; ++i ) 265 | { 266 | this->precisions[i]++; 267 | } 268 | 269 | for( int i=0; iquantizationUnitCount; ++i ) 270 | { 271 | this->precisionsFine[i] = 0; 272 | if( this->precisions[i] > LDAC_MAXIDWL1 ) 273 | { 274 | this->precisionsFine[i] = this->precisions[i] - LDAC_MAXIDWL1; 275 | this->precisions[i] = LDAC_MAXIDWL1; 276 | } 277 | } 278 | 279 | LOG_ARRAY_LEN( this->precisions, "%3d, ", frame->quantizationUnitCount ); 280 | LOG_ARRAY_LEN( this->precisionsFine, "%3d, ", frame->quantizationUnitCount ); 281 | } 282 | 283 | static int decodeScaleFactor0( channel_t *this, BitReaderCxt *br ) 284 | { 285 | LOG_FUNCTION(); 286 | frame_t *frame = this->frame; 287 | this->scaleFactorBitlen = ReadInt( br, LDAC_SFCBLENBITS ) + LDAC_MINSFCBLEN_0; 288 | this->scaleFactorOffset = ReadInt( br, LDAC_IDSFBITS ); 289 | this->scaleFactorWeight = ReadInt( br, LDAC_SFCWTBLBITS ); 290 | 291 | LOG("scale factor bitlen = %d\n", this->scaleFactorBitlen ); 292 | LOG("scale factor offset = %d\n", this->scaleFactorOffset ); 293 | LOG("scale factor weight = %d\n", this->scaleFactorWeight ); 294 | 295 | const int mask = (1<scaleFactorBitlen)-1; 296 | const uint8_t *weightTable = gaa_sfcwgt_ldac[this->scaleFactorWeight]; 297 | const HuffmanCodebook* codebook = &HuffmanScaleFactorsUnsigned[this->scaleFactorBitlen]; 298 | this->scaleFactors[0] = ReadInt( br, this->scaleFactorBitlen ); 299 | // printf("diff =\n "); 300 | for( int i=1; iquantizationUnitCount; ++i ) 301 | { 302 | int diff = ReadHuffmanValue( codebook, br, 1 ); 303 | 304 | // printf("%2d, ", diff ); 305 | this->scaleFactors[i] = (this->scaleFactors[i-1] + diff) & mask; 306 | this->scaleFactors[i-1] += this->scaleFactorOffset - weightTable[i-1]; // cancel weights 307 | } 308 | this->scaleFactors[frame->quantizationUnitCount-1] += this->scaleFactorOffset - weightTable[frame->quantizationUnitCount-1]; 309 | 310 | LOG_ARRAY_LEN( this->scaleFactors, "%2d, ", frame->quantizationUnitCount ); 311 | return 0; 312 | } 313 | 314 | static int decodeScaleFactor1( channel_t *this, BitReaderCxt *br ) 315 | { 316 | LOG_FUNCTION(); 317 | frame_t *frame = this->frame; 318 | this->scaleFactorBitlen = ReadInt( br, LDAC_SFCBLENBITS ) + LDAC_MINSFCBLEN_1; 319 | 320 | if( this->scaleFactorBitlen > 4 ) 321 | { 322 | for( int i=0; iquantizationUnitCount; ++i ) 323 | { 324 | this->scaleFactors[i] = ReadInt( br, LDAC_IDSFBITS ); 325 | } 326 | } else 327 | { 328 | this->scaleFactorOffset = ReadInt( br, LDAC_IDSFBITS ); 329 | this->scaleFactorWeight = ReadInt( br, LDAC_SFCWTBLBITS ); 330 | const uint8_t *weightTable = gaa_sfcwgt_ldac[this->scaleFactorWeight]; 331 | for( int i=0; iquantizationUnitCount; ++i ) 332 | { 333 | this->scaleFactors[i] = ReadInt( br, this->scaleFactorBitlen ) - weightTable[i] + this->scaleFactorOffset; 334 | } 335 | } 336 | return 0; 337 | } 338 | 339 | int decodeScaleFactor2( channel_t *this, BitReaderCxt *br ) 340 | { 341 | LOG_FUNCTION(); 342 | frame_t *frame = this->frame; 343 | channel_t *other = &frame->channels[0]; 344 | 345 | this->scaleFactorBitlen = ReadInt( br, LDAC_SFCBLENBITS ) + LDAC_MINSFCBLEN_2; 346 | LOG("scale factor bitlen: %d\n", this->scaleFactorBitlen ); 347 | const HuffmanCodebook* codebook = &HuffmanScaleFactorsSigned[this->scaleFactorBitlen]; 348 | // printf("diff =\n"); 349 | for( int i=0; iquantizationUnitCount; ++i ) 350 | { 351 | int diff = ReadHuffmanValue( codebook, br, 1 ); 352 | // printf("%2d, ", diff ); 353 | this->scaleFactors[i] = other->scaleFactors[i] + diff; 354 | } 355 | 356 | LOG_ARRAY_LEN( this->scaleFactors, "%2d, ", frame->quantizationUnitCount ); 357 | return 0; 358 | } 359 | 360 | int decodeScaleFactors( frame_t *this, BitReaderCxt *br, int channelNbr ) 361 | { 362 | LOG_FUNCTION(); 363 | channel_t *channel = &this->channels[channelNbr]; 364 | channel->scaleFactorMode = ReadInt( br, LDAC_SFCMODEBITS ); 365 | LOG("scale factor mode = %d\n", channel->scaleFactorMode ); 366 | if( channelNbr == 0 ) 367 | { 368 | if( channel->scaleFactorMode == LDAC_MODE_0 ) 369 | decodeScaleFactor0( channel, br ); 370 | else 371 | decodeScaleFactor1( channel, br ); 372 | } else 373 | { 374 | if( channel->scaleFactorMode == LDAC_MODE_0 ) 375 | decodeScaleFactor0( channel, br ); 376 | else 377 | decodeScaleFactor2( channel, br ); 378 | } 379 | return 0; 380 | } 381 | 382 | enum { 383 | CHANNEL_1CH = 1, 384 | CHANNEL_2CH = 2, 385 | }; 386 | 387 | static const char gaa_block_setting_ldac[4][4]= 388 | { 389 | {CHANNEL_1CH, 1, MONO}, 390 | {CHANNEL_2CH, 2, MONO, MONO}, 391 | {CHANNEL_2CH, 1, STEREO}, 392 | {0, 0, 0}, 393 | }; 394 | 395 | static void pcmFloatToShort( frame_t *this, int16_t *pcmOut ) 396 | { 397 | int i=0; 398 | for(int smpl=0; smplframeSamples; ++smpl ) 399 | { 400 | for( int ch=0; chchannelCount; ++ch, ++i ) 401 | { 402 | pcmOut[i] = Clamp16(Round(this->channels[ch].pcm[smpl])); 403 | } 404 | } 405 | } 406 | 407 | static const int channelConfigIdToChannelCount[] = { 1, 2, 2 }; 408 | 409 | int ldacdecGetChannelCount( ldacdec_t *this ) 410 | { 411 | return channelConfigIdToChannelCount[this->frame.channelConfigId]; 412 | } 413 | 414 | static const unsigned short sampleRateIdToSamplesPower[] = { 415 | 7, 7, 8, 8 416 | }; 417 | 418 | static const int sampleRateIdToFrequency[] = { 44100, 48000, 88200, 96000 }; 419 | 420 | int ldacdecGetSampleRate( ldacdec_t *this ) 421 | { 422 | return sampleRateIdToFrequency[this->frame.sampleRateId]; 423 | } 424 | 425 | static int decodeFrame( frame_t *this, BitReaderCxt *br ) 426 | { 427 | int syncWord = ReadInt( br, LDAC_SYNCWORDBITS ); 428 | if( syncWord != LDAC_SYNCWORD ) 429 | return -1; 430 | 431 | this->sampleRateId = ReadInt( br, LDAC_SMPLRATEBITS ); 432 | this->channelConfigId = ReadInt( br, LDAC_CHCONFIG2BITS ); 433 | this->frameLength = ReadInt( br, LDAC_FRAMELEN2BITS ) + 1; 434 | this->frameStatus = ReadInt( br, LDAC_FRAMESTATBITS ); 435 | 436 | this->channelCount = channelConfigIdToChannelCount[this->channelConfigId]; 437 | this->frameSamplesPower = sampleRateIdToSamplesPower[this->sampleRateId]; 438 | this->frameSamples = 1<frameSamplesPower; 439 | 440 | this->channels[0].mdct.Bits = this->frameSamplesPower; 441 | this->channels[1].mdct.Bits = this->frameSamplesPower; 442 | 443 | LOG("sampleRateId: %d\n", this->sampleRateId ); 444 | LOG(" sample rate: %d\n", sampleRateIdToFrequency[this->sampleRateId] ); 445 | LOG(" samplePower: %d\n", this->frameSamplesPower ); 446 | LOG("channelConfigId: %d\n", this->channelConfigId ); 447 | LOG("frameLength: %d\n", this->frameLength ); 448 | LOG("frameStatus: %d\n", this->frameStatus ); 449 | 450 | return 0; 451 | } 452 | 453 | int ldacDecode( ldacdec_t *this, uint8_t *stream, int16_t *pcm, int *bytesUsed ) 454 | { 455 | BitReaderCxt brObject; 456 | BitReaderCxt *br = &brObject; 457 | InitBitReaderCxt( br, stream ); 458 | 459 | frame_t *frame = &this->frame; 460 | 461 | int ret = decodeFrame( frame, br ); 462 | if( ret < 0 ) 463 | return -1; 464 | 465 | for( int block = 0; blockchannelConfigId][1]; ++block ) 466 | { 467 | decodeBand( frame, br ); 468 | decodeGradient( frame, br ); 469 | calculateGradient( frame ); 470 | 471 | for( int i=0; ichannelCount; ++i ) 472 | { 473 | channel_t *channel = &frame->channels[i]; 474 | decodeScaleFactors( frame, br, i ); 475 | calculatePrecisionMask( channel ); 476 | calculatePrecisions( channel ); 477 | 478 | decodeSpectrum( channel, br ); 479 | decodeSpectrumFine( channel, br ); 480 | dequantizeSpectra( channel ); 481 | scaleSpectrum( channel ); 482 | 483 | RunImdct( &channel->mdct, channel->spectra, channel->pcm ); 484 | } 485 | AlignPosition( br, 8 ); 486 | 487 | pcmFloatToShort( frame, pcm ); 488 | } 489 | AlignPosition( br, (frame->frameLength)*8 + 24 ); 490 | 491 | if( bytesUsed != NULL ) 492 | *bytesUsed = br->Position / 8; 493 | return 0; 494 | } 495 | 496 | // for packet loss concealment 497 | static const int sa_null_data_size_ldac[2] = { 498 | 11, 15, 499 | }; 500 | 501 | static const uint8_t saa_null_data_ldac[2][15] = { 502 | {0x07, 0xa0, 0x16, 0x00, 0x20, 0xad, 0x51, 0x45, 0x14, 0x50, 0x49}, 503 | {0x07, 0xa0, 0x0a, 0x00, 0x20, 0xad, 0x51, 0x41, 0x24, 0x93, 0x00, 0x28, 0xa0, 0x92, 0x49}, 504 | }; 505 | 506 | int ldacNullPacket( ldacdec_t *this, uint8_t *output, int *bytesUsed ) 507 | { 508 | frame_t *frame = &this->frame; 509 | uint8_t *ptr = output; 510 | 511 | for( int block = 0; blockchannelConfigId][1]; ++block ) 512 | { 513 | const int channelType = gaa_block_setting_ldac[frame->channelConfigId][2]; 514 | const int size = sa_null_data_size_ldac[channelType]; 515 | memcpy( ptr, saa_null_data_ldac[channelType], size ); 516 | ptr += size; 517 | } 518 | 519 | *bytesUsed = frame->frameLength + 3; 520 | } 521 | -------------------------------------------------------------------------------- /log.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOG_H_ 2 | #define __LOG_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifdef DEBUG 9 | #define LOG( format, ... ) \ 10 | { \ 11 | printf( format, __VA_ARGS__ ); \ 12 | } 13 | 14 | #define LOG_ARRAY( var, format ) \ 15 | { \ 16 | __auto_type len = sizeof(var)/sizeof(var[0]); \ 17 | printf("%s[%ld] =\n", #var, len ); \ 18 | for( typeof(len) i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | #include "ldacdec.h" 7 | #include "spectrum.h" 8 | #include "log.h" 9 | 10 | #define LDAC_MAXNQUS 34 11 | #define LDAC_MAXNSPS 16 12 | 13 | /** Spectrum/Residual Data **/ 14 | #define LDAC_NIDWL 16 15 | #define LDAC_NIDSF 32 16 | #define LDAC_2DIMSPECBITS 3 17 | #define LDAC_N2DIMSPECENCTBL 16 18 | #define LDAC_N2DIMSPECDECTBL 8 19 | #define LDAC_4DIMSPECBITS 7 20 | #define LDAC_N4DIMSPECENCTBL 256 21 | #define LDAC_N4DIMSPECDECTBL 81 22 | 23 | /*************************************************************************************************** 24 | Encoding/Decoding Tables for Spectrum Data 25 | ***************************************************************************************************/ 26 | static const uint8_t ga_wl_ldac[LDAC_NIDWL] = { 27 | 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 28 | }; 29 | 30 | static const uint8_t ga_nsps_ldac[LDAC_MAXNQUS] = { 31 | 2, 2, 2, 2, 2, 2, 2, 2, 32 | 4, 4, 4, 4, 33 | 4, 4, 4, 4, 34 | 4, 4, 4, 4, 35 | 8, 8, 36 | 8, 8, 37 | 16, 16, 38 | 16, 16, 39 | 16, 16, 40 | 16, 16, 41 | 16, 16, 42 | }; 43 | 44 | static const uint16_t ga_isp_ldac[LDAC_MAXNQUS+1] = { 45 | 0, 2, 4, 6, 8, 10, 12, 14, 46 | 16, 20, 24, 28, 47 | 32, 36, 40, 44, 48 | 48, 52, 56, 60, 49 | 64, 72, 50 | 80, 88, 51 | 96,112, 52 | 128,144, 53 | 160,176, 54 | 192,208, 55 | 224,240, 56 | 256, 57 | }; 58 | 59 | static const int decode2DSpectrum[8] = { 60 | 0, 1, 2, 4, 6, 8, 9, 10 61 | }; 62 | 63 | static const int decode4DSpectrum[128] = { 64 | 0, 1, 2, 4, 5, 6, 8, 9, 10, 16, 17, 18, 20, 21, 22, 24, 65 | 25, 26, 32, 33, 34, 36, 37, 38, 40, 41, 42, 64, 65, 66, 68, 69, 66 | 70, 72, 73, 74, 80, 81, 82, 84, 85, 86, 88, 89, 90, 96, 97, 98, 67 | 100, 101, 102, 104, 105, 106, 128, 129, 130, 132, 133, 134, 136, 137, 138, 144, 68 | 145, 146, 148, 149, 150, 152, 153, 154, 160, 161, 162, 164, 165, 166, 168, 169, 69 | 170, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 70 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 71 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 72 | }; 73 | 74 | /*************************************************************************************************** 75 | Quantization Tables 76 | ***************************************************************************************************/ 77 | /* Inverse of Quantize Factor for Spectrum/Residual Quantization */ 78 | static const float QuantizerStepSize[16] = { 79 | 2.0000000000000000e+00, 6.6666666666666663e-01, 2.8571428571428570e-01, 1.3333333333333333e-01, 80 | 6.4516129032258063e-02, 3.1746031746031744e-02, 1.5748031496062992e-02, 7.8431372549019607e-03, 81 | 3.9138943248532287e-03, 1.9550342130987292e-03, 9.7703957010258913e-04, 4.8840048840048840e-04, 82 | 2.4417043096081065e-04, 1.2207776353537203e-04, 6.1037018951994385e-05, 3.0518043793392844e-05, 83 | }; 84 | 85 | static const float QuantizerFineStepSize[16] = 86 | { 87 | 3.0518043793392844e-05, 1.0172681264464281e-05, 4.3597205419132631e-06, 2.0345362528928561e-06, 88 | 9.8445302559331759e-07, 4.8441339354591809e-07, 2.4029955742829012e-07, 1.1967860311134448e-07, 89 | 5.9722199204291275e-08, 2.9831909866464167e-08, 1.4908668194134265e-08, 7.4525137468602791e-09, 90 | 3.7258019525568114e-09, 1.8627872668859698e-09, 9.3136520869755679e-10, 4.6567549848772173e-10 91 | }; 92 | 93 | 94 | int decodeSpectrum( channel_t *this, BitReaderCxt *br ) 95 | { 96 | frame_t *frame = this->frame; 97 | for( int i=0; iquantizationUnitCount; ++i ) 98 | { 99 | int startSubband = ga_isp_ldac[i]; 100 | int endSubband = ga_isp_ldac[i+1]; 101 | int nsps = ga_nsps_ldac[i]; 102 | int wl = ga_wl_ldac[this->precisions[i]]; 103 | 104 | if( this->precisions[i] == 1 ) 105 | { 106 | int idxSpectrum = startSubband; 107 | if( nsps == 2 ) 108 | { 109 | int value = decode2DSpectrum[ReadInt( br, LDAC_2DIMSPECBITS )]; 110 | this->quantizedSpectra[idxSpectrum] = ((value>>2)&3) - 1; 111 | this->quantizedSpectra[idxSpectrum+1] = (value&3) - 1; 112 | } else 113 | { 114 | for (int j = 0; j < nsps/4; j++, idxSpectrum+=4) 115 | { 116 | int value = decode4DSpectrum[ReadInt( br, LDAC_4DIMSPECBITS )]; 117 | this->quantizedSpectra[idxSpectrum] = ((value>>6)&3) - 1; 118 | this->quantizedSpectra[idxSpectrum+1] = ((value>>4)&3) - 1; 119 | this->quantizedSpectra[idxSpectrum+2] = ((value>>2)&3) - 1; 120 | this->quantizedSpectra[idxSpectrum+3] = (value&3) - 1; 121 | } 122 | } 123 | } else 124 | { 125 | for( int j = startSubband; jquantizedSpectra[j] = ReadSignedInt( br, wl ); 127 | } 128 | } 129 | 130 | LOG_ARRAY_LEN( this->quantizedSpectra, "%3d, ", frame->quantizationUnitCount ); 131 | return 0; 132 | } 133 | 134 | int decodeSpectrumFine( channel_t *this, BitReaderCxt *br ) 135 | { 136 | frame_t *frame = this->frame; 137 | memset( this->quantizedSpectraFine, 0, sizeof( this->quantizedSpectraFine ) ); 138 | for( int i=0; iquantizationUnitCount; ++i ) 139 | { 140 | if( this->precisionsFine[i] > 0 ) 141 | { 142 | int startSubband = ga_isp_ldac[i]; 143 | int endSubband = ga_isp_ldac[i+1]; 144 | int wl = ga_wl_ldac[this->precisionsFine[i]]; 145 | for( int j=startSubband; jquantizedSpectraFine[j] = ReadSignedInt( br, wl ); 148 | } 149 | } 150 | } 151 | 152 | LOG_ARRAY_LEN( this->quantizedSpectraFine, "%3d, ", frame->quantizationUnitCount ); 153 | return 0; 154 | } 155 | 156 | static void dequantizeQuantUnit( channel_t* this, int band ) 157 | { 158 | const int subBandIndex = ga_isp_ldac[band]; 159 | const int subBandCount = ga_nsps_ldac[band]; 160 | const float stepSize = QuantizerStepSize[this->precisions[band]]; 161 | const float stepSizeFine = QuantizerFineStepSize[this->precisions[band]]; 162 | 163 | for( int sb=0; sbquantizedSpectra[subBandIndex+sb] * stepSize; 166 | const double fine = this->quantizedSpectraFine[subBandIndex+sb] * stepSizeFine; 167 | this->spectra[subBandIndex + sb] = coarse + fine; 168 | } 169 | } 170 | 171 | void dequantizeSpectra( channel_t *this ) 172 | { 173 | frame_t *frame = this->frame; 174 | 175 | memset( this->spectra, 0, sizeof(this->spectra) ); 176 | 177 | for( int i=0; iquantizationUnitCount; ++i ) 178 | { 179 | dequantizeQuantUnit( this, i ); 180 | } 181 | 182 | LOG_ARRAY_LEN( this->spectra, "%e, ", ga_isp_ldac[frame->quantizationUnitCount-1] + ga_nsps_ldac[frame->quantizationUnitCount-1] ); 183 | } 184 | 185 | static const double spectrumScale[32] = 186 | { 187 | 3.0517578125e-5, 6.1035156250e-5, 1.2207031250e-4, 2.4414062500e-4, 188 | 4.8828125000e-4, 9.7656250000e-4, 1.9531250000e-3, 3.9062500000e-3, 189 | 7.8125000000e-3, 1.5625000000e-2, 3.1250000000e-2, 6.2500000000e-2, 190 | 1.2500000000e-1, 2.5000000000e-1, 5.0000000000e-1, 1.0000000000e+0, 191 | 2.0000000000e+0, 4.0000000000e+0, 8.0000000000e+0, 1.6000000000e+1, 192 | 3.2000000000e+1, 6.4000000000e+1, 1.2800000000e+2, 2.5600000000e+2, 193 | 5.1200000000e+2, 1.0240000000e+3, 2.0480000000e+3, 4.0960000000e+3, 194 | 8.1920000000e+3, 1.6384000000e+4, 3.2768000000e+4, 6.5536000000e+4 195 | }; 196 | 197 | void scaleSpectrum(channel_t* this) 198 | { 199 | const frame_t *frame = this->frame; 200 | const int quantUnitCount = frame->quantizationUnitCount; 201 | float * const spectra = this->spectra; 202 | 203 | for (int i = 0; i < quantUnitCount; i++) 204 | { 205 | const int startSubBand = ga_isp_ldac[i]; 206 | const int endSubBand = ga_isp_ldac[i+1]; 207 | if( this->scaleFactors[i] > 0 ) 208 | { 209 | for (int sb = startSubBand; sb < endSubBand; sb++) 210 | { 211 | spectra[sb] *= spectrumScale[this->scaleFactors[i]]; 212 | } 213 | } 214 | } 215 | 216 | LOG_ARRAY_LEN( this->spectra, "%e, ", ga_isp_ldac[frame->quantizationUnitCount-1] + ga_nsps_ldac[frame->quantizationUnitCount-1] ); 217 | } 218 | 219 | 220 | -------------------------------------------------------------------------------- /spectrum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SPECTRUM_H_ 2 | #define _SPECTRUM_H_ 3 | 4 | #include "ldacdec.h" 5 | #include "bit_reader.h" 6 | 7 | int decodeSpectrum( channel_t *this, BitReaderCxt *br ); 8 | int decodeSpectrumFine( channel_t *this, BitReaderCxt *br ); 9 | 10 | void dequantizeSpectra( channel_t *this ); 11 | void scaleSpectrum(channel_t* this); 12 | 13 | #endif // _SPECTRUM_H_ 14 | -------------------------------------------------------------------------------- /utility.c: -------------------------------------------------------------------------------- 1 | #include "utility.h" 2 | #include 3 | 4 | int Max(int a, int b) { return a > b ? a : b; } 5 | int Min(int a, int b) { return a > b ? b : a; } 6 | 7 | uint32_t BitReverse32(uint32_t value, int bitCount) 8 | { 9 | value = ((value & 0xaaaaaaaa) >> 1) | ((value & 0x55555555) << 1); 10 | value = ((value & 0xcccccccc) >> 2) | ((value & 0x33333333) << 2); 11 | value = ((value & 0xf0f0f0f0) >> 4) | ((value & 0x0f0f0f0f) << 4); 12 | value = ((value & 0xff00ff00) >> 8) | ((value & 0x00ff00ff) << 8); 13 | value = (value >> 16) | (value << 16); 14 | return value >> (32 - bitCount); 15 | } 16 | 17 | int32_t SignExtend32(int32_t value, int bits) 18 | { 19 | const int shift = 8 * sizeof(int32_t) - bits; 20 | return (value << shift) >> shift; 21 | } 22 | 23 | int16_t Clamp16(int value) 24 | { 25 | if (value > SHRT_MAX) 26 | return SHRT_MAX; 27 | if (value < SHRT_MIN) 28 | return SHRT_MIN; 29 | return (int16_t)value; 30 | } 31 | 32 | int Round(double x) 33 | { 34 | x += 0.5; 35 | return (int)x - (x < (int)x); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /utility.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define FALSE 0 7 | #define TRUE 1 8 | 9 | #ifndef M_PI 10 | #define M_PI 3.14159265358979323846 11 | #endif 12 | 13 | int Max(int a, int b); 14 | int Min(int a, int b); 15 | uint32_t BitReverse32(uint32_t value, int bitCount); 16 | int32_t SignExtend32(int32_t value, int bits); 17 | int16_t Clamp16(int value); 18 | int Round(double x); 19 | 20 | --------------------------------------------------------------------------------