├── .gitignore ├── .gitmodules ├── Makefile ├── README.md ├── bit_allocation.c ├── bit_allocation.h ├── bit_reader.c ├── bit_reader.h ├── huffCodes.c ├── huffCodes.h ├── imdct.c ├── imdct.h ├── kernel.patch ├── ldacdec.c ├── ldacdec.h ├── ldacenc.c ├── libldacBT_dec.h ├── libldacdec.c ├── log.h ├── patch-kernel.sh ├── spectrum.c ├── spectrum.h ├── utility.c └── utility.h /.gitignore: -------------------------------------------------------------------------------- 1 | ldacdec 2 | ldacenc 3 | *.ldac 4 | *.wav 5 | *.dat 6 | *.o 7 | *.d 8 | *.so 9 | *.so.1 10 | *.dump 11 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "libldac"] 2 | path = libldac 3 | url = https://android.googlesource.com/platform/external/libldac 4 | -------------------------------------------------------------------------------- /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 | #CFLAGS += -DDEBUG_ADAPTATION 15 | CFLAGS += -DDOUBLE64 16 | LDLIBS = -lm 17 | 18 | ifeq ($(ASAN),true) 19 | LCFLAGS += -fsanitize=address 20 | LDFLAGS += -fsanitize=address 21 | endif 22 | 23 | VPATH += libldac/src/ 24 | LDFLAGS += -L. 25 | 26 | all: libldacdec.so ldacdec ldacenc 27 | 28 | libldacdec.so: LDFLAGS += -shared -fpic -Wl,-soname,libldacdec.so.1 29 | libldacdec.so: CFLAGS += -fpic 30 | libldacdec.so: libldacdec.o bit_allocation.o huffCodes.o bit_reader.o utility.o imdct.o spectrum.o 31 | 32 | ldacenc: ldacenc.o ldaclib.o ldacBT.o 33 | 34 | ldacenc: LDLIBS += $(shell pkg-config sndfile --libs) $(shell pkg-config samplerate --libs) 35 | ldacenc: ldacenc.o ldaclib.o ldacBT.o 36 | 37 | ldacdec: libldacdec.so ldacdec.o 38 | ldacdec: LDFLAGS += -Wl,-rpath=. 39 | ldacdec: LDLIBS += -lldacdec -lsndfile 40 | 41 | mdct_imdct: LDLIBS += $(shell pkg-config sndfile --libs) 42 | #mdct_imdct: CFLAGS += -DSINGLE_PRECISION 43 | mdct_imdct: mdct_imdct.o ldaclib.o imdct.o 44 | 45 | install: libldacdec.so 46 | ln -sf libldacdec.so libldacdec.so.1 47 | cp -a libldacdec.so libldacdec.so.1 /usr/lib/ 48 | cp libldacBT_dec.h /usr/include/ldac/libldacBT_dec.h 49 | %.so: 50 | $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) 51 | 52 | .PHONY: clean 53 | clean: 54 | rm -f *.d *.o ldacenc ldacdec libldacdec.so libldacdec.so.1 55 | 56 | -include *.d 57 | 58 | 59 | -------------------------------------------------------------------------------- /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 | sudo apt install libsndfile1 libsndfile1-dev libsamplerate0 libsamplerate0-dev 11 | make libldacdec.so 12 | sudo make install 13 | 14 | ``` 15 | 16 | #### Usage 17 | see ldacdec.c for example usage 18 | 19 | #### ldacdec 20 | takes an LDAC stream and decodes it to WAV 21 | 22 | #### ldacenc 23 | uses Android LDAC encoder library to create LDAC streams from audio 24 | 25 | #### Build BlueALSA with support for this library: 26 | ```sh 27 | LDAC_INCLUDE_DIR=/usr/include/ldac 28 | LDAC_LIB_DIR=/usr/lib 29 | 30 | export LDAC_ABR_CFLAGS="-I$LDAC_INCLUDE_DIR" 31 | export LDAC_ABR_LIBS="-L$LDAC_LIB_DIR -lldacBT_abr" 32 | export LDAC_DEC_CFLAGS="-I$LDAC_INCLUDE_DIR" 33 | export LDAC_DEC_LIBS="-L$LDAC_LIB_DIR -lldacdec" 34 | export LDAC_ENC_CFLAGS="-I$LDAC_INCLUDE_DIR" 35 | export LDAC_ENC_LIBS="-L$LDAC_LIB_DIR -lldacBT_enc" 36 | 37 | 38 | autoreconf --install 39 | mkdir build && cd build 40 | ../configure --enable-aac --enable-ofono --enable-aptx --enable-aptx-hd --with-libopenaptx --enable-ldac --enable-debug 41 | make 42 | sudo make install 43 | ``` 44 | 45 | #### Building for Raspberry Pi 46 | 47 | Apply the following patch to your kernel 48 | ```diff 49 | diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c 50 | index eebe25610..64db1db3f 100644 51 | --- a/net/bluetooth/l2cap_sock.c 52 | +++ b/net/bluetooth/l2cap_sock.c 53 | @@ -1825,7 +1825,7 @@ static void l2cap_sock_init(struct sock *sk, struct sock *parent) 54 | break; 55 | } 56 | 57 | - chan->imtu = L2CAP_DEFAULT_MTU; 58 | + chan->imtu = 0; 59 | chan->omtu = 0; 60 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { 61 | chan->mode = L2CAP_MODE_ERTM; 62 | ``` 63 | 64 | If you're running a Debian-based system, you can also run [this script](https://github.com/anonymix007/libldacdec/blob/master/patch-kernel.sh) 65 | to patch and build the kernel, with this command: 66 | ```sh 67 | # If you have cloned the repository 68 | ./patch-kernel.sh --install 69 | 70 | # If you havent't cloned the repository 71 | curl https://raw.githubusercontent.com/anonymix007/libldacdec/master/patch-kernel.sh | bash -s -- --install 72 | ``` 73 | If you want to add additional configurations to the kernel, you can pass `--configure`. 74 | If you don't want to install the kernel, but you just want to compile it, you can remove `--install` from the command. 75 | 76 | Run the following script to automatically build and install bluez-alsa with LDAC decoding capabilities: 77 | ```sh 78 | #!/bin/sh 79 | 80 | sudo apt install -y git automake build-essential libtool pkg-config python3-docutils 81 | sudo apt install -y libasound2-dev libbluetooth-dev libdbus-1-dev libglib2.0-dev libsbc-dev 82 | sudo apt install -y check libfdk-aac-dev lcov libldacbt-enc-dev libldacbt-abr-dev libbsd-dev libopenaptx-dev libunwind-dev libncurses-dev libreadline-dev libspandsp-dev libsndfile1 libsndfile1-dev libsamplerate0 libsamplerate0-dev 83 | 84 | git clone https://github.com/anonymix007/libldacdec.git 85 | cd libldacdec 86 | make libldacdec.so 87 | sudo make install 88 | cd 89 | 90 | git clone https://github.com/Arkq/bluez-alsa.git 91 | cd bluez-alsa 92 | LDAC_INCLUDE_DIR=/usr/include/ldac 93 | LDAC_LIB_DIR=/usr/lib 94 | export LDAC_ABR_CFLAGS="-I$LDAC_INCLUDE_DIR" 95 | export LDAC_ABR_LIBS="-L$LDAC_LIB_DIR -lldacBT_abr" 96 | export LDAC_DEC_CFLAGS="-I$LDAC_INCLUDE_DIR" 97 | export LDAC_DEC_LIBS="-L$LDAC_LIB_DIR -lldacdec" 98 | export LDAC_ENC_CFLAGS="-I$LDAC_INCLUDE_DIR" 99 | export LDAC_ENC_LIBS="-L$LDAC_LIB_DIR -lldacBT_enc" 100 | autoreconf --install --force 101 | mkdir build && cd build 102 | ../configure --enable-aac --enable-aptx --enable-aptx-hd --with-libopenaptx --enable-ldac --enable-debug --enable-cli 103 | make -j4 104 | sudo make install 105 | ``` 106 | -------------------------------------------------------------------------------- /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 | scalar 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 | scalar 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 | scalar MdctWindow[3][256]; 8 | scalar ImdctWindow[3][256]; 9 | scalar SinTables[9][256]; 10 | scalar CosTables[9][256]; 11 | int ShuffleTables[9][256]; 12 | 13 | static void GenerateTrigTables(int sizeBits) 14 | { 15 | const int size = 1 << sizeBits; 16 | scalar* sinTab = SinTables[sizeBits]; 17 | scalar* cosTab = CosTables[sizeBits]; 18 | 19 | for (int i = 0; i < size; i++) 20 | { 21 | const scalar 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 | scalar* 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 | scalar* imdct = ImdctWindow[frameSizePower - 6]; 53 | scalar* 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 scalar* window = ImdctWindow[mdct->Bits - 6]; 84 | scalar* 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 scalar* sinTable = SinTables[MdctBits]; 103 | const scalar* cosTable = CosTables[MdctBits]; 104 | scalar 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 | scalar a = input[i2]; 114 | scalar b = input[lastIndex - i2]; 115 | scalar sin = sinTable[i]; 116 | scalar 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 | scalar a = dctTemp[frontPos] - dctTemp[backPos]; 139 | scalar b = dctTemp[frontPos + 1] - dctTemp[backPos + 1]; 140 | scalar sin = sinTable[i]; 141 | scalar 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 | #include "utility.h" 3 | 4 | typedef struct { 5 | int Bits; 6 | int Size; 7 | scalar Scale; 8 | scalar ImdctPrevious[MAX_FRAME_SAMPLES]; 9 | scalar* Window; 10 | scalar* SinTable; 11 | scalar* CosTable; 12 | } Mdct; 13 | 14 | void InitMdct(); 15 | void RunImdct(Mdct* mdct, float* input, float* output); 16 | 17 | -------------------------------------------------------------------------------- /kernel.patch: -------------------------------------------------------------------------------- 1 | diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c 2 | index eebe25610..64db1db3f 100644 3 | --- a/net/bluetooth/l2cap_sock.c 4 | +++ b/net/bluetooth/l2cap_sock.c 5 | @@ -1825,7 +1825,7 @@ static void l2cap_sock_init(struct sock *sk, struct sock *parent) 6 | break; 7 | } 8 | 9 | - chan->imtu = L2CAP_DEFAULT_MTU; 10 | + chan->imtu = 0; 11 | chan->omtu = 0; 12 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { 13 | chan->mode = L2CAP_MODE_ERTM; 14 | -------------------------------------------------------------------------------- /ldacdec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 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_24, 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*2) 35 | #define PCM_BUFFER_SIZE (256*2*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 | int32_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 | 78 | ptr = buf; 79 | if( bytesInBuffer == 0 ){ 80 | break; 81 | } 82 | #if 1 83 | while(ptr[0] != 0xAA) 84 | { 85 | ptr++; 86 | filePosition++; 87 | } 88 | #endif 89 | LOG("sync: %02x\n", ptr[0] ); 90 | int bytesUsed = 0; 91 | 92 | memset( pcm, 0, sizeof(pcm) ); 93 | LOG("count === %4d ===\n", blockId++ ); 94 | ret = ldacDecode_type( &dec, ptr, pcm, &bytesUsed, 4); 95 | if( ret < 0 ){ 96 | break; 97 | } 98 | LOG_ARRAY( pcm, "%4d, " ); 99 | if( out == NULL ) 100 | { 101 | printf("auto detect format!\n"); 102 | out = openAudioFile( audioFile, ldacdecGetSampleRate( &dec ), ldacdecGetChannelCount( &dec ) ); 103 | if( out == NULL ) 104 | return EXIT_FAILURE; 105 | } 106 | 107 | sf_writef_int( out, pcm, dec.frame.frameSamples ); 108 | filePosition += bytesUsed; 109 | fflush(stdout); 110 | } 111 | 112 | printf("Done!\n"); 113 | 114 | sf_close(out); 115 | fclose(in); 116 | 117 | return EXIT_SUCCESS; 118 | } 119 | -------------------------------------------------------------------------------- /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 | #include "libldacBT_dec.h" 10 | 11 | #define container_of( ptr, type, member ) ({ \ 12 | const typeof( ((type*)0)->member ) *__mptr = (ptr); \ 13 | (type *)((char*)__ptr - offsetof(type, memeber)); \ 14 | }) 15 | 16 | #define min( a, b ) \ 17 | ({ __typeof__ (a) _a = (a); \ 18 | __typeof__ (b) _b = (b); \ 19 | _a < _b ? _a : _b; }); 20 | 21 | #define max( a, b ) \ 22 | ({ __typeof__ (a) _a = (a); \ 23 | __typeof__ (b) _b = (b); \ 24 | _a > _b ? _a : _b; }); 25 | 26 | typedef struct Frame frame_t; 27 | typedef struct Channel channel_t; 28 | 29 | struct Channel { 30 | frame_t *frame; 31 | int scaleFactorMode; 32 | int scaleFactorBitlen; 33 | int scaleFactorOffset; 34 | int scaleFactorWeight; 35 | 36 | int scaleFactors[MAX_QUANT_UNITS]; 37 | 38 | int precisions[MAX_QUANT_UNITS]; 39 | int precisionsFine[MAX_QUANT_UNITS]; 40 | int precisionMask[MAX_QUANT_UNITS]; 41 | 42 | int quantizedSpectra[MAX_FRAME_SAMPLES]; 43 | int quantizedSpectraFine[MAX_FRAME_SAMPLES]; 44 | 45 | float spectra[MAX_FRAME_SAMPLES]; 46 | float pcm[MAX_FRAME_SAMPLES]; 47 | Mdct mdct; 48 | }; 49 | 50 | struct Frame { 51 | int sampleRateId; 52 | int channelConfigId; 53 | int frameLength; 54 | int frameStatus; 55 | int frameSamplesPower; 56 | int frameSamples; 57 | 58 | int nbrBands; 59 | 60 | // gradient data 61 | int gradient[MAX_QUANT_UNITS]; 62 | int gradientMode; 63 | int gradientStartUnit; 64 | int gradientEndUnit; 65 | int gradientStartValue; 66 | int gradientEndValue; 67 | int gradientBoundary; 68 | 69 | int quantizationUnitCount; 70 | 71 | int channelCount; 72 | channel_t channels[2]; 73 | }; 74 | 75 | typedef struct { 76 | frame_t frame; 77 | 78 | } ldacdec_t; 79 | 80 | int ldacdecInit( ldacdec_t *this ); 81 | int ldacDecode( ldacdec_t *this, uint8_t *stream, short *pcm, int *bytesUsed ); 82 | int ldacDecode_type( ldacdec_t *this, uint8_t *stream, void *pcm, int *bytesUsed, LDACBT_SMPL_FMT_T fmt ); 83 | int ldacNullPacket( ldacdec_t *this, uint8_t *output, int *bytesUsed ); 84 | int ldacdecGetSampleRate( ldacdec_t *this ); 85 | int ldacdecGetChannelCount( ldacdec_t *this ); 86 | 87 | #endif // __LDACDEC_H_ 88 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /libldacBT_dec.h: -------------------------------------------------------------------------------- 1 | #ifndef __LDACBTDEC_H_ 2 | #define __LDACBTDEC_H_ 3 | 4 | 5 | 6 | typedef unsigned char undefined; 7 | 8 | typedef unsigned char bool; 9 | typedef unsigned char byte; 10 | typedef unsigned int dword; 11 | typedef unsigned char uchar; 12 | typedef unsigned int uint; 13 | typedef unsigned long ulong; 14 | typedef unsigned long long ulonglong; 15 | typedef unsigned char undefined1; 16 | typedef unsigned short undefined2; 17 | typedef unsigned int undefined4; 18 | typedef unsigned long long undefined8; 19 | typedef unsigned short ushort; 20 | typedef unsigned short word; 21 | typedef struct _st_ldacbt_config _st_ldacbt_config, *P_st_ldacbt_config; 22 | 23 | typedef struct _st_ldacbt_config * P_LDACBT_CONFIG; 24 | 25 | struct _st_ldacbt_config { 26 | int id; 27 | int nfrm_in_pkt; 28 | int frmlen; 29 | int frmlen_1ch; 30 | }; 31 | 32 | typedef struct _st_ldacbt_eqmid_property _st_ldacbt_eqmid_property, *P_st_ldacbt_eqmid_property; 33 | 34 | struct _st_ldacbt_eqmid_property { 35 | int eqmid; 36 | char strModeName[4]; 37 | int id_for_2DH5; 38 | }; 39 | 40 | typedef struct _audio_block_ldac _audio_block_ldac, *P_audio_block_ldac; 41 | 42 | typedef struct _audio_channel_ldac _audio_channel_ldac, *P_audio_channel_ldac; 43 | 44 | typedef struct _audio_channel_ldac AC; 45 | 46 | typedef struct _audio_block_ldac AB; 47 | 48 | typedef struct _audio_channel_sub_ldac _audio_channel_sub_ldac, *P_audio_channel_sub_ldac; 49 | 50 | typedef struct _audio_channel_sub_ldac ACSUB; 51 | 52 | struct _audio_block_ldac { 53 | int blk_type; 54 | int blk_nchs; 55 | int nbands; 56 | int nqus; 57 | int unk1; 58 | int unk2; 59 | int grad_mode; 60 | int grad_qu_l; 61 | int grad_qu_h; 62 | int grad_os_l; 63 | int grad_os_h; 64 | int a_grad[50]; 65 | int nadjqus; 66 | int abc_status; 67 | int nbits_ab; 68 | int nbits_band; 69 | int nbits_grad; 70 | int nbits_scfc; 71 | int nbits_spec; 72 | int nbits_avail; 73 | int nbits_used; 74 | int * p_smplrate_id; 75 | int * p_error_code; 76 | AC * ap_ac[2]; 77 | }; 78 | 79 | struct _audio_channel_ldac { 80 | int ich; 81 | int frmana_cnt; 82 | int unk3; 83 | int sfc_mode; 84 | int sfc_bitlen; 85 | int sfc_offset; 86 | int sfc_weight; 87 | int a_idsf[34]; 88 | int a_idwl1[34]; 89 | int a_idwl2[34]; 90 | int a_addwl[34]; 91 | int a_tmp[34]; 92 | int a_qspec[256]; 93 | int a_rspec[256]; 94 | AB * p_ab; 95 | ACSUB * p_ascub; 96 | }; 97 | 98 | struct _audio_channel_sub_ldac { 99 | float a_time[512]; 100 | float a_spec[256]; 101 | }; 102 | 103 | typedef union IEEE754_FI IEEE754_FI, *PIEEE754_FI; 104 | 105 | union IEEE754_FI { 106 | float f; 107 | int i; 108 | }; 109 | 110 | typedef struct _config_info_ldac _config_info_ldac, *P_config_info_ldac; 111 | 112 | typedef struct _config_info_ldac CFG; 113 | 114 | struct _config_info_ldac { 115 | int syncword; 116 | int smplrate_id; 117 | int chconfig_id; 118 | int ch; 119 | int frame_length; 120 | int frame_status; 121 | }; 122 | 123 | typedef struct _sfinfo_ldac _sfinfo_ldac, *P_sfinfo_ldac; 124 | 125 | typedef struct _sfinfo_ldac SFINFO; 126 | 127 | struct _sfinfo_ldac { 128 | CFG CFG; 129 | AB * p_ab; 130 | AC * ap_ac[2]; 131 | char * p_mempos; 132 | int error_code; 133 | }; 134 | 135 | enum{ 136 | _2_DH1=3, 137 | _2_DH2=4, 138 | _2_DH3=5, 139 | _3_DH1=6, 140 | _3_DH2=7, 141 | _3_DH3=8, 142 | ___DH1=0, 143 | ___DH2=1, 144 | ___DH3=2 145 | }; 146 | 147 | typedef enum LDAC_SMPL_FMT_T { 148 | LDAC_SMPL_FMT_F32=5, 149 | LDAC_SMPL_FMT_MAX=2147483647, 150 | LDAC_SMPL_FMT_NONE=0, 151 | LDAC_SMPL_FMT_NUM=6, 152 | LDAC_SMPL_FMT_S08=1, 153 | LDAC_SMPL_FMT_S16=2, 154 | LDAC_SMPL_FMT_S24=3, 155 | LDAC_SMPL_FMT_S32=4 156 | } LDAC_SMPL_FMT_T; 157 | 158 | typedef struct _handle_ldac_struct _handle_ldac_struct, *P_handle_ldac_struct; 159 | 160 | struct _handle_ldac_struct { 161 | int nlnn; 162 | int nbands; 163 | int grad_mode; 164 | int grad_qu_l; 165 | int grad_qu_h; 166 | int grad_os_l; 167 | int grad_os_h; 168 | int abc_status; 169 | int error_code; 170 | SFINFO sfinfo; 171 | }; 172 | 173 | typedef struct _ldacbt_pcm_ring_buf _ldacbt_pcm_ring_buf, *P_ldacbt_pcm_ring_buf; 174 | 175 | typedef struct _ldacbt_pcm_ring_buf LDACBT_PCM_RING_BUF; 176 | 177 | struct _ldacbt_pcm_ring_buf { 178 | char buf[6144]; 179 | int wp; 180 | int rp; 181 | int nsmpl; 182 | }; 183 | 184 | typedef struct _ldacbt_transport_frame_buf _ldacbt_transport_frame_buf, *P_ldacbt_transport_frame_buf; 185 | 186 | struct _ldacbt_transport_frame_buf { 187 | char buf[1024]; 188 | int used; 189 | int nfrm_in; 190 | }; 191 | 192 | typedef struct _st_ldacbt_handle _st_ldacbt_handle, *P_st_ldacbt_handle; 193 | 194 | typedef struct _handle_ldac_struct * HANDLE_LDAC; 195 | 196 | typedef enum LDACBT_PROCMODE { 197 | LDACBT_PROCMODE_DECODE=2, 198 | LDACBT_PROCMODE_ENCODE=1, 199 | LDACBT_PROCMODE_UNSET=4294967295 200 | } LDACBT_PROCMODE; 201 | 202 | typedef struct _ldacbt_pcm_info _ldacbt_pcm_info, *P_ldacbt_pcm_info; 203 | 204 | typedef struct _ldacbt_pcm_info LDACBT_PCM_INFO; 205 | 206 | typedef struct _ldacbt_tx_info _ldacbt_tx_info, *P_ldacbt_tx_info; 207 | 208 | typedef struct _ldacbt_tx_info LDACBT_TX_INFO; 209 | 210 | typedef struct _ldacbt_transport_frame_buf LDACBT_TRANSPORT_FRM_BUF; 211 | 212 | typedef enum LDACBT_SMPL_FMT_T { 213 | LDACBT_SMPL_FMT_F32=5, 214 | LDACBT_SMPL_FMT_S16=2, 215 | LDACBT_SMPL_FMT_S24=3, 216 | LDACBT_SMPL_FMT_S32=4 217 | } LDACBT_SMPL_FMT_T; 218 | 219 | struct _ldacbt_tx_info { 220 | int mtu; 221 | int tx_size; 222 | int pkt_type; 223 | int pkt_hdr_sz; 224 | int nrfm_in_pkt; 225 | }; 226 | 227 | struct _ldacbt_pcm_info { 228 | int sf; 229 | int ch; 230 | int wl; 231 | enum LDACBT_SMPL_FMT_T fmt; 232 | }; 233 | 234 | struct _st_ldacbt_handle { 235 | HANDLE_LDAC hLDAC; 236 | enum LDACBT_PROCMODE proc_mode; 237 | int error_code; 238 | int error_code_api; 239 | LDACBT_PCM_INFO pcm; 240 | LDACBT_TX_INFO tx; 241 | int frm_samples; 242 | int sfid; 243 | int nshift; 244 | int flg_encode_flushed; 245 | int frm_status; 246 | int frmlen; 247 | int frmlen_tx; 248 | int bitrate; 249 | int eqmid; 250 | int tgt_eqmid; 251 | int tgt_nfrm_in_pkt; 252 | int tgt_frmlen; 253 | int stat_alter_op; 254 | int cm; 255 | int cci; 256 | int transport; 257 | int some_code; 258 | LDACBT_TRANSPORT_FRM_BUF ldac_trns_frm_buf; 259 | LDACBT_PCM_RING_BUF pcmring; 260 | char * * pp_pcm; 261 | char * ap_pcm[2]; 262 | char a_pcm[4096]; 263 | }; 264 | 265 | typedef struct _st_ldacbt_handle STRUCT_LDACBT_HANDLE; 266 | 267 | typedef uchar STREAM; 268 | 269 | typedef struct _st_ldacbt_eqmid_property * P_LDACBT_EQMID_PROPERTY; 270 | 271 | typedef struct _st_ldacbt_handle * HANDLE_LDAC_BT; 272 | 273 | typedef struct _st_ldacbt_eqmid_property LDACBT_EQMID_PROPERTY; 274 | 275 | typedef struct _st_ldacbt_config LDACBT_CONFIG; 276 | 277 | int ldacBT_decode(HANDLE_LDAC_BT hLdacBT, uchar *p_stream, void *p_pcm, LDACBT_SMPL_FMT_T fmt, int stream_sz, int *used_bytes, int *pcm_sz); 278 | int ldacBT_init_handle_decode(HANDLE_LDAC_BT hLdacBT, int cm, int sf, int nshift, int var1, int var2); 279 | HANDLE_LDAC_BT ldacBT_get_handle(void); 280 | void ldacBT_free_handle(HANDLE_LDAC_BT hLdacBT); 281 | 282 | #endif // __LDACBTDEC_H_ 283 | -------------------------------------------------------------------------------- /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 | #include "libldacBT_dec.h" 13 | 14 | #define clear_data_ldac(p, n) memset((p), 0, (n)) 15 | 16 | #define LDAC_SYNCWORDBITS (8) 17 | #define LDAC_SYNCWORD (0xAA) 18 | /** Sampling Rate **/ 19 | #define LDAC_SMPLRATEBITS (3) 20 | /** Channel **/ 21 | #define LDAC_CHCONFIG2BITS (2) 22 | enum CHANNEL { 23 | MONO = 0, 24 | STEREO = 1 25 | }; 26 | /** Frame Length **/ 27 | #define LDAC_FRAMELEN2BITS (9) 28 | /** Frame Status **/ 29 | #define LDAC_FRAMESTATBITS (2) 30 | 31 | /** Band Info **/ 32 | #define LDAC_NBANDBITS (4) 33 | #define LDAC_BAND_OFFSET (2) 34 | 35 | /** Band **/ 36 | #define LDAC_MAXNBANDS 16 37 | 38 | /* Flag */ 39 | #define LDAC_FLAGBITS (1) 40 | #define LDAC_TRUE (1) 41 | #define LDAC_FALSE (0) 42 | 43 | /* Mode */ 44 | #define LDAC_MODE_0 (0) 45 | #define LDAC_MODE_1 (1) 46 | #define LDAC_MODE_2 (2) 47 | #define LDAC_MODE_3 (3) 48 | 49 | /** Gradient Data **/ 50 | #define LDAC_GRADMODEBITS (2) 51 | #define LDAC_GRADOSBITS (5) 52 | #define LDAC_GRADQU0BITS (6) 53 | #define LDAC_GRADQU1BITS (5) 54 | #define LDAC_NADJQUBITS (5) 55 | 56 | /** Scale Factor Data **/ 57 | #define LDAC_SFCMODEBITS 1 58 | #define LDAC_IDSFBITS 5 59 | #define LDAC_NSFCWTBL 8 60 | #define LDAC_SFCBLENBITS 2 61 | #define LDAC_MINSFCBLEN_0 3 62 | #define LDAC_MINSFCBLEN_1 2 63 | #define LDAC_MINSFCBLEN_2 2 64 | #define LDAC_SFCWTBLBITS 3 65 | 66 | #define LDAC_MINIDWL1 1 67 | #define LDAC_MAXIDWL1 15 68 | #define LDAC_MAXIDWL2 15 69 | 70 | // Quantization Units 71 | #define LDAC_MAXNQUS 34 72 | #define LDAC_MAXGRADQU 50 73 | 74 | /*************************************************************************************************** 75 | Weighting Tables for Scale Factor Data 76 | ***************************************************************************************************/ 77 | static const uint8_t gaa_sfcwgt_ldac[LDAC_NSFCWTBL][LDAC_MAXNQUS] = { 78 | { 79 | 1, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 80 | 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 81 | }, 82 | { 83 | 0, 1, 1, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 84 | 7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 85 | }, 86 | { 87 | 0, 1, 1, 2, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 88 | 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, 89 | }, 90 | { 91 | 0, 1, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 92 | 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 93 | }, 94 | { 95 | 0, 1, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 96 | 10, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 97 | }, 98 | { 99 | 1, 0, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8, 8, 8, 100 | 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 101 | }, 102 | { 103 | 0, 0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 104 | 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 7, 7, 7, 8, 9, 9, 9, 9, 105 | }, 106 | { 107 | 0, 0, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8, 108 | 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 109 | }, 110 | }; 111 | 112 | static const uint8_t ga_nqus_ldac[LDAC_MAXNBANDS+1] = { 113 | 0, 4, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25, 26, 28, 30, 32, 34, 114 | }; 115 | 116 | 117 | int ldacdecInit( ldacdec_t *this ) 118 | { 119 | InitHuffmanCodebooks(); 120 | InitMdct(); 121 | 122 | memset( &this->frame.channels[0].mdct, 0, sizeof( Mdct ) ); 123 | memset( &this->frame.channels[1].mdct, 0, sizeof( Mdct ) ); 124 | 125 | this->frame.channels[0].frame = &this->frame; 126 | this->frame.channels[1].frame = &this->frame; 127 | 128 | return 0; 129 | } 130 | 131 | static int decodeBand( frame_t *this, BitReaderCxt *br ) 132 | { 133 | this->nbrBands = ReadInt( br, LDAC_NBANDBITS ) + LDAC_BAND_OFFSET; 134 | LOG("nbrBands: %d\n", this->nbrBands ); 135 | ReadInt( br, LDAC_FLAGBITS ); // unused 136 | 137 | this->quantizationUnitCount = ga_nqus_ldac[this->nbrBands]; 138 | return 0; 139 | } 140 | 141 | static int decodeGradient( frame_t *this, BitReaderCxt *br ) 142 | { 143 | this->gradientMode = ReadInt( br, LDAC_GRADMODEBITS ); 144 | if( this->gradientMode == LDAC_MODE_0 ) 145 | { 146 | this->gradientStartUnit = ReadInt( br, LDAC_GRADQU0BITS ); 147 | this->gradientEndUnit = ReadInt( br, LDAC_GRADQU0BITS ) + 1; 148 | this->gradientStartValue = ReadInt( br, LDAC_GRADOSBITS ); 149 | this->gradientEndValue = ReadInt( br, LDAC_GRADOSBITS ); 150 | LOG("gradient\n\tqu [%3d,%3d]\n\tvalue [%3d,%3d]\n", 151 | this->gradientStartUnit, this->gradientEndUnit, 152 | this->gradientStartValue, this->gradientEndValue ); 153 | } else 154 | { 155 | this->gradientStartUnit = ReadInt( br, LDAC_GRADQU1BITS ); 156 | this->gradientEndUnit = 26; 157 | this->gradientStartValue = ReadInt( br, LDAC_GRADOSBITS ); 158 | this->gradientEndValue = 31; 159 | LOG("gradient\n\tqu [%3d,%3d\n\tvalue [%3d,%3d]\n", 160 | this->gradientStartUnit, this->gradientEndUnit, 161 | this->gradientStartValue, this->gradientEndValue ); 162 | } 163 | 164 | this->gradientBoundary = ReadInt( br, LDAC_NADJQUBITS ); 165 | return 0; 166 | } 167 | 168 | static void calculateGradient( frame_t *this ) 169 | { 170 | int valueCount = this->gradientEndValue - this->gradientStartValue; 171 | int unitCount = this->gradientEndUnit - this->gradientStartUnit; 172 | 173 | memset( this->gradient, 0, sizeof( this->gradient ) ); 174 | 175 | for( int i=0; igradientEndUnit; ++i ) 176 | this->gradient[i] = -this->gradientStartValue; 177 | for( int i=this->gradientEndUnit; iquantizationUnitCount; ++i ) 178 | this->gradient[i] = -this->gradientEndValue; 179 | 180 | if( unitCount > 0 ) 181 | { 182 | const uint8_t *curve = gradientCurves[unitCount-1]; 183 | if( valueCount > 0 ) 184 | { 185 | for( int i=this->gradientStartUnit; igradientEndUnit; ++i ) 186 | { 187 | this->gradient[i] -= ((curve[i-this->gradientStartUnit] * (valueCount-1)) >> 8) + 1; 188 | } 189 | } else if( valueCount < 0 ) 190 | { 191 | for( int i=this->gradientStartUnit; igradientEndUnit; ++i ) 192 | { 193 | this->gradient[i] -= ((curve[i-this->gradientStartUnit] * (valueCount-1)) >> 8) + 1; 194 | } 195 | } 196 | } 197 | 198 | LOG_ARRAY_LEN( this->gradient, "%3d, ", this->quantizationUnitCount ); 199 | } 200 | 201 | static void calculatePrecisionMask(channel_t* this) 202 | { 203 | memset(this->precisionMask, 0, sizeof(this->precisionMask)); 204 | for (int i = 1; i < this->frame->quantizationUnitCount; i++) 205 | { 206 | const int delta = this->scaleFactors[i] - this->scaleFactors[i - 1]; 207 | if (delta > 1) 208 | { 209 | this->precisionMask[i] += min(delta - 1, 5); 210 | } 211 | else if (delta < -1) 212 | { 213 | this->precisionMask[i - 1] += min(delta * -1 - 1, 5); 214 | } 215 | } 216 | } 217 | 218 | 219 | static void calculatePrecisions( channel_t *this ) 220 | { 221 | frame_t *frame = this->frame; 222 | 223 | for( int i=0; iquantizationUnitCount; ++i ) 224 | { 225 | switch( frame->gradientMode ) 226 | { 227 | case LDAC_MODE_0: 228 | { 229 | int precision = this->scaleFactors[i] + frame->gradient[i]; 230 | precision = max( precision, LDAC_MINIDWL1 ); 231 | this->precisions[i] = precision; 232 | break; 233 | } 234 | case LDAC_MODE_1: 235 | { 236 | int precision = this->scaleFactors[i] + frame->gradient[i] + this->precisionMask[i]; 237 | if( precision > 0 ) 238 | precision /= 2; 239 | precision = max( precision, LDAC_MINIDWL1 ); 240 | this->precisions[i] = precision; 241 | break; 242 | } 243 | case LDAC_MODE_2: 244 | { 245 | int precision = this->scaleFactors[i] + frame->gradient[i] + this->precisionMask[i]; 246 | if( precision > 0 ) 247 | precision = ( precision * 3 ) / 8; 248 | precision = max( precision, LDAC_MINIDWL1 ); 249 | this->precisions[i] = precision; 250 | break; 251 | } 252 | case LDAC_MODE_3: 253 | { 254 | int precision = this->scaleFactors[i] + frame->gradient[i] + this->precisionMask[i]; 255 | if( precision > 0 ) 256 | precision /= 4; 257 | precision = max( precision, LDAC_MINIDWL1 ); 258 | this->precisions[i] = precision; 259 | break; 260 | } 261 | default: 262 | LOG("Gradient mode: %d\n",frame->gradientMode); 263 | //assert(0); 264 | break; 265 | } 266 | } 267 | 268 | for( int i=0; igradientBoundary; ++i ) 269 | { 270 | this->precisions[i]++; 271 | } 272 | 273 | for( int i=0; iquantizationUnitCount; ++i ) 274 | { 275 | this->precisionsFine[i] = 0; 276 | if( this->precisions[i] > LDAC_MAXIDWL1 ) 277 | { 278 | this->precisionsFine[i] = this->precisions[i] - LDAC_MAXIDWL1; 279 | this->precisions[i] = LDAC_MAXIDWL1; 280 | } 281 | } 282 | 283 | LOG_ARRAY_LEN( this->precisions, "%3d, ", frame->quantizationUnitCount ); 284 | LOG_ARRAY_LEN( this->precisionsFine, "%3d, ", frame->quantizationUnitCount ); 285 | } 286 | 287 | static int decodeScaleFactor0( channel_t *this, BitReaderCxt *br ) 288 | { 289 | LOG_FUNCTION(); 290 | frame_t *frame = this->frame; 291 | this->scaleFactorBitlen = ReadInt( br, LDAC_SFCBLENBITS ) + LDAC_MINSFCBLEN_0; 292 | this->scaleFactorOffset = ReadInt( br, LDAC_IDSFBITS ); 293 | this->scaleFactorWeight = ReadInt( br, LDAC_SFCWTBLBITS ); 294 | 295 | LOG("scale factor bitlen = %d\n", this->scaleFactorBitlen ); 296 | LOG("scale factor offset = %d\n", this->scaleFactorOffset ); 297 | LOG("scale factor weight = %d\n", this->scaleFactorWeight ); 298 | 299 | const int mask = (1<scaleFactorBitlen)-1; 300 | const uint8_t *weightTable = gaa_sfcwgt_ldac[this->scaleFactorWeight]; 301 | const HuffmanCodebook* codebook = &HuffmanScaleFactorsUnsigned[this->scaleFactorBitlen]; 302 | this->scaleFactors[0] = ReadInt( br, this->scaleFactorBitlen ); 303 | // printf("diff =\n "); 304 | for( int i=1; iquantizationUnitCount; ++i ) 305 | { 306 | int diff = ReadHuffmanValue( codebook, br, 1 ); 307 | 308 | // printf("%2d, ", diff ); 309 | this->scaleFactors[i] = (this->scaleFactors[i-1] + diff) & mask; 310 | this->scaleFactors[i-1] += this->scaleFactorOffset - weightTable[i-1]; // cancel weights 311 | } 312 | this->scaleFactors[frame->quantizationUnitCount-1] += this->scaleFactorOffset - weightTable[frame->quantizationUnitCount-1]; 313 | 314 | LOG_ARRAY_LEN( this->scaleFactors, "%2d, ", frame->quantizationUnitCount ); 315 | return 0; 316 | } 317 | 318 | static int decodeScaleFactor1( channel_t *this, BitReaderCxt *br ) 319 | { 320 | LOG_FUNCTION(); 321 | frame_t *frame = this->frame; 322 | this->scaleFactorBitlen = ReadInt( br, LDAC_SFCBLENBITS ) + LDAC_MINSFCBLEN_1; 323 | 324 | if( this->scaleFactorBitlen > 4 ) 325 | { 326 | for( int i=0; iquantizationUnitCount; ++i ) 327 | { 328 | this->scaleFactors[i] = ReadInt( br, LDAC_IDSFBITS ); 329 | } 330 | } else 331 | { 332 | this->scaleFactorOffset = ReadInt( br, LDAC_IDSFBITS ); 333 | this->scaleFactorWeight = ReadInt( br, LDAC_SFCWTBLBITS ); 334 | const uint8_t *weightTable = gaa_sfcwgt_ldac[this->scaleFactorWeight]; 335 | for( int i=0; iquantizationUnitCount; ++i ) 336 | { 337 | this->scaleFactors[i] = ReadInt( br, this->scaleFactorBitlen ) - weightTable[i] + this->scaleFactorOffset; 338 | } 339 | } 340 | return 0; 341 | } 342 | 343 | int decodeScaleFactor2( channel_t *this, BitReaderCxt *br ) 344 | { 345 | LOG_FUNCTION(); 346 | frame_t *frame = this->frame; 347 | channel_t *other = &frame->channels[0]; 348 | 349 | this->scaleFactorBitlen = ReadInt( br, LDAC_SFCBLENBITS ) + LDAC_MINSFCBLEN_2; 350 | LOG("scale factor bitlen: %d\n", this->scaleFactorBitlen ); 351 | const HuffmanCodebook* codebook = &HuffmanScaleFactorsSigned[this->scaleFactorBitlen]; 352 | // printf("diff =\n"); 353 | for( int i=0; iquantizationUnitCount; ++i ) 354 | { 355 | int diff = ReadHuffmanValue( codebook, br, 1 ); 356 | // printf("%2d, ", diff ); 357 | this->scaleFactors[i] = other->scaleFactors[i] + diff; 358 | } 359 | 360 | LOG_ARRAY_LEN( this->scaleFactors, "%2d, ", frame->quantizationUnitCount ); 361 | return 0; 362 | } 363 | 364 | int decodeScaleFactors( frame_t *this, BitReaderCxt *br, int channelNbr ) 365 | { 366 | LOG_FUNCTION(); 367 | channel_t *channel = &this->channels[channelNbr]; 368 | channel->scaleFactorMode = ReadInt( br, LDAC_SFCMODEBITS ); 369 | LOG("scale factor mode = %d\n", channel->scaleFactorMode ); 370 | if( channelNbr == 0 ) 371 | { 372 | if( channel->scaleFactorMode == LDAC_MODE_0 ) 373 | decodeScaleFactor0( channel, br ); 374 | else 375 | decodeScaleFactor1( channel, br ); 376 | } else 377 | { 378 | if( channel->scaleFactorMode == LDAC_MODE_0 ) 379 | decodeScaleFactor0( channel, br ); 380 | else 381 | decodeScaleFactor2( channel, br ); 382 | } 383 | return 0; 384 | } 385 | 386 | enum { 387 | CHANNEL_1CH = 1, 388 | CHANNEL_2CH = 2, 389 | }; 390 | 391 | static const char gaa_block_setting_ldac[4][4]= 392 | { 393 | {CHANNEL_1CH, 1, MONO}, 394 | {CHANNEL_2CH, 2, MONO, MONO}, 395 | {CHANNEL_2CH, 1, STEREO}, 396 | {0, 0, 0}, 397 | }; 398 | 399 | static void pcmFloatToShort( frame_t *this, int16_t *pcmOut ) 400 | { 401 | int i=0; 402 | for(int smpl=0; smplframeSamples; ++smpl ) 403 | { 404 | for( int ch=0; chchannelCount; ++ch, ++i ) 405 | { 406 | pcmOut[i] = Clamp16(Round(this->channels[ch].pcm[smpl])); 407 | } 408 | } 409 | } 410 | 411 | static void pcmFloatToFloat( frame_t *this, float *pcmOut ) 412 | { 413 | int i=0; 414 | for(int smpl=0; smplframeSamples; ++smpl ) 415 | { 416 | for( int ch=0; chchannelCount; ++ch, ++i ) 417 | { 418 | pcmOut[i] = this->channels[ch].pcm[smpl]/(1<<15); 419 | } 420 | } 421 | } 422 | 423 | static void pcmFloatToInt( frame_t *this, int32_t *pcmOut ) 424 | { 425 | int i=0; 426 | for(int smpl=0; smplframeSamples; ++smpl ) 427 | { 428 | for( int ch=0; chchannelCount; ++ch, ++i ) 429 | { 430 | pcmOut[i] = Round(this->channels[ch].pcm[smpl]*(1<<15)); 431 | } 432 | } 433 | } 434 | static const int channelConfigIdToChannelCount[] = { 1, 2, 2 }; 435 | 436 | int ldacdecGetChannelCount( ldacdec_t *this ) 437 | { 438 | return channelConfigIdToChannelCount[this->frame.channelConfigId]; 439 | } 440 | 441 | static const unsigned short sampleRateIdToSamplesPower[] = { 442 | 7, 7, 8, 8 443 | }; 444 | 445 | static const int sampleRateIdToFrequency[] = { 44100, 48000, 88200, 96000 }; 446 | 447 | int ldacdecGetSampleRate( ldacdec_t *this ) 448 | { 449 | return sampleRateIdToFrequency[this->frame.sampleRateId]; 450 | } 451 | 452 | static int decodeFrame( frame_t *this, BitReaderCxt *br ) 453 | { 454 | int syncWord = ReadInt( br, LDAC_SYNCWORDBITS ); 455 | 456 | LOG_ADAPT("syncWord: %x, ", syncWord); 457 | 458 | if( syncWord != LDAC_SYNCWORD ) 459 | return -1; 460 | 461 | this->sampleRateId = ReadInt( br, LDAC_SMPLRATEBITS ); 462 | this->channelConfigId = ReadInt( br, LDAC_CHCONFIG2BITS ); 463 | this->frameLength = ReadInt( br, LDAC_FRAMELEN2BITS ) + 1; 464 | this->frameStatus = ReadInt( br, LDAC_FRAMESTATBITS ); 465 | 466 | this->channelCount = channelConfigIdToChannelCount[this->channelConfigId]; 467 | this->frameSamplesPower = sampleRateIdToSamplesPower[this->sampleRateId]; 468 | this->frameSamples = 1<frameSamplesPower; 469 | 470 | this->channels[0].mdct.Bits = this->frameSamplesPower; 471 | this->channels[1].mdct.Bits = this->frameSamplesPower; 472 | 473 | LOG("sampleRateId: %d\n", this->sampleRateId ); 474 | LOG(" sample rate: %d\n", sampleRateIdToFrequency[this->sampleRateId] ); 475 | LOG(" samplePower: %d\n", this->frameSamplesPower ); 476 | LOG("channelConfigId: %d\n", this->channelConfigId ); 477 | LOG("frameLength: %d\n", this->frameLength ); 478 | LOG("frameStatus: %d\n", this->frameStatus ); 479 | 480 | return 0; 481 | } 482 | 483 | int ldacDecode( ldacdec_t *this, uint8_t *stream, short *pcm, int *bytesUsed ) 484 | { 485 | BitReaderCxt brObject; 486 | BitReaderCxt *br = &brObject; 487 | InitBitReaderCxt( br, stream ); 488 | 489 | frame_t *frame = &this->frame; 490 | 491 | int ret = decodeFrame( frame, br ); 492 | if( ret < 0 ) 493 | return -1; 494 | 495 | for( int block = 0; blockchannelConfigId][1]; ++block ) 496 | { 497 | decodeBand( frame, br ); 498 | decodeGradient( frame, br ); 499 | calculateGradient( frame ); 500 | 501 | for( int i=0; ichannelCount; ++i ) 502 | { 503 | channel_t *channel = &frame->channels[i]; 504 | decodeScaleFactors( frame, br, i ); 505 | calculatePrecisionMask( channel ); 506 | calculatePrecisions( channel ); 507 | 508 | decodeSpectrum( channel, br ); 509 | decodeSpectrumFine( channel, br ); 510 | dequantizeSpectra( channel ); 511 | scaleSpectrum( channel ); 512 | 513 | RunImdct( &channel->mdct, channel->spectra, channel->pcm ); 514 | } 515 | AlignPosition( br, 8 ); 516 | 517 | pcmFloatToShort( frame, pcm ); 518 | } 519 | AlignPosition( br, (frame->frameLength)*8 + 24 ); 520 | 521 | if( bytesUsed != NULL ) 522 | *bytesUsed = br->Position / 8; 523 | return 0; 524 | } 525 | 526 | int ldacDecode_type( ldacdec_t *this, uint8_t *stream, void *pcm, int *bytesUsed, LDACBT_SMPL_FMT_T fmt) 527 | { 528 | BitReaderCxt brObject; 529 | BitReaderCxt *br = &brObject; 530 | InitBitReaderCxt( br, stream ); 531 | 532 | frame_t *frame = &this->frame; 533 | 534 | int ret = decodeFrame( frame, br ); 535 | if( ret < 0 ) 536 | return -1; 537 | 538 | for( int block = 0; blockchannelConfigId][1]; ++block ) 539 | { 540 | decodeBand( frame, br ); 541 | decodeGradient( frame, br ); 542 | calculateGradient( frame ); 543 | 544 | for( int i=0; ichannelCount; ++i ) 545 | { 546 | channel_t *channel = &frame->channels[i]; 547 | decodeScaleFactors( frame, br, i ); 548 | calculatePrecisionMask( channel ); 549 | calculatePrecisions( channel ); 550 | 551 | decodeSpectrum( channel, br ); 552 | decodeSpectrumFine( channel, br ); 553 | dequantizeSpectra( channel ); 554 | scaleSpectrum( channel ); 555 | 556 | RunImdct( &channel->mdct, channel->spectra, channel->pcm ); 557 | } 558 | AlignPosition( br, 8 ); 559 | 560 | switch(fmt){ 561 | case LDACBT_SMPL_FMT_F32: 562 | pcmFloatToFloat( frame, pcm ); 563 | break; 564 | case LDACBT_SMPL_FMT_S32: 565 | pcmFloatToInt( frame, pcm ); 566 | break; 567 | case LDACBT_SMPL_FMT_S16: 568 | pcmFloatToShort( frame, pcm); 569 | break; 570 | default: return 666; 571 | } 572 | 573 | } 574 | AlignPosition( br, (frame->frameLength)*8 + 24 ); 575 | 576 | if( bytesUsed != NULL ) 577 | *bytesUsed = br->Position / 8; 578 | return 0; 579 | } 580 | 581 | // for packet loss concealment 582 | static const int sa_null_data_size_ldac[2] = { 583 | 11, 15, 584 | }; 585 | 586 | static const uint8_t saa_null_data_ldac[2][15] = { 587 | {0x07, 0xa0, 0x16, 0x00, 0x20, 0xad, 0x51, 0x45, 0x14, 0x50, 0x49}, 588 | {0x07, 0xa0, 0x0a, 0x00, 0x20, 0xad, 0x51, 0x41, 0x24, 0x93, 0x00, 0x28, 0xa0, 0x92, 0x49}, 589 | }; 590 | 591 | int ldacNullPacket( ldacdec_t *this, uint8_t *output, int *bytesUsed ) 592 | { 593 | frame_t *frame = &this->frame; 594 | uint8_t *ptr = output; 595 | 596 | for( int block = 0; blockchannelConfigId][1]; ++block ) 597 | { 598 | const int channelType = gaa_block_setting_ldac[frame->channelConfigId][2]; 599 | const int size = sa_null_data_size_ldac[channelType]; 600 | memcpy( ptr, saa_null_data_ldac[channelType], size ); 601 | ptr += size; 602 | } 603 | 604 | *bytesUsed = frame->frameLength + 3; 605 | 606 | return 0; 607 | } 608 | 609 | void ldacBT_free_handle(HANDLE_LDAC_BT hLdacBT) 610 | { 611 | if(hLdacBT == NULL) return; 612 | 613 | if(hLdacBT->hLDAC != NULL) free(hLdacBT->hLDAC); 614 | 615 | free(hLdacBT); 616 | } 617 | 618 | HANDLE_LDAC_BT ldacBT_get_handle(void) 619 | { 620 | HANDLE_LDAC_BT hLdacBT; 621 | 622 | hLdacBT = (HANDLE_LDAC_BT)calloc(1, sizeof(_st_ldacbt_handle)); 623 | if( hLdacBT == NULL )return NULL; 624 | 625 | ldacdec_t *dec = calloc(1, sizeof(ldacdec_t)); 626 | 627 | /* Get ldaclib Handle */ 628 | if((hLdacBT->hLDAC = (HANDLE_LDAC)dec) == NULL){ 629 | free(hLdacBT); 630 | return NULL; 631 | } 632 | 633 | hLdacBT->proc_mode = LDACBT_PROCMODE_DECODE; 634 | 635 | return hLdacBT; 636 | } 637 | 638 | int ldacBT_init_handle_decode(HANDLE_LDAC_BT hLdacBT, int cm, int sf, int nshift, int var1, int var2){ 639 | (void)cm; 640 | (void)sf; 641 | (void)nshift; 642 | (void)var1; 643 | (void)var2; 644 | 645 | if(hLdacBT->proc_mode != LDACBT_PROCMODE_DECODE) { 646 | free(hLdacBT->hLDAC); 647 | hLdacBT->proc_mode = LDACBT_PROCMODE_DECODE; 648 | ldacdec_t *dec = calloc(1, sizeof(ldacdec_t)); 649 | assert(dec != NULL); 650 | hLdacBT->hLDAC = (HANDLE_LDAC)dec; 651 | } 652 | 653 | return ldacdecInit((ldacdec_t *)hLdacBT->hLDAC); 654 | } 655 | 656 | int ldacBT_decode(HANDLE_LDAC_BT hLdacBT, uchar *p_stream, void *p_pcm, LDACBT_SMPL_FMT_T fmt, int stream_sz, int *used_bytes, int *pcm_sz){ 657 | (void)stream_sz; 658 | ldacdec_t *dec = (ldacdec_t *)hLdacBT->hLDAC; 659 | 660 | LOG_ADAPT("libldacdec: format: %d\n", fmt); 661 | 662 | if(fmt != LDACBT_SMPL_FMT_S16 && fmt != LDACBT_SMPL_FMT_S32 && fmt != LDACBT_SMPL_FMT_F32) return hLdacBT->error_code_api=517; 663 | 664 | if(hLdacBT->proc_mode != LDACBT_PROCMODE_DECODE) return hLdacBT->error_code_api=1000; 665 | 666 | int result = ldacDecode_type(dec, p_stream, p_pcm, used_bytes, fmt); 667 | 668 | *pcm_sz = dec->frame.frameSamples * (fmt < 4 ? fmt*2 : 4*2); 669 | LOG_ADAPT("libldacdec: result: %d, pcm_sz: %d, stream_sz: %d, used_bytes: %d\n", result, *pcm_sz, stream_sz, *used_bytes); 670 | if(result == -1) return hLdacBT->error_code_api=516; 671 | 672 | 673 | return hLdacBT->error_code_api=result; 674 | } 675 | -------------------------------------------------------------------------------- /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 /dev/null; then 24 | echo " - Error: This script works only with Debian-based distros, at least for now." 1>&2 25 | exit 1 26 | fi 27 | 28 | if ! grep -q "^deb-src" /etc/apt/sources.list /etc/apt/sources.list.d/*; then 29 | echo " - Error: Source repositories are disabled or not configured in your /etc/apt/sources.list. You need to enable them for this script to work." 1>&2 30 | echo " - Note: If you're compiling for a Raspberry Pi, you also need to enable source repositories in /etc/apt/sources.list.d/raspi.conf." 1>&2 31 | exit 1 32 | fi 33 | 34 | if [[ "$MORE" -gt 1 ]]; then 35 | echo " - Warning: You set \$MORE to $MORE, which is greater than 1, and this may slow down the build. I recommend you to set it to 0 or 1." 1>&2 36 | fi 37 | 38 | 39 | JOBS=$(($(nproc --all) + $MORE)) 40 | 41 | abort() { 42 | echo " - Aborting." 1>&2 43 | exit 2 44 | } 45 | 46 | run() { 47 | echo " - Running $*" 1>&2 48 | $* 49 | } 50 | 51 | backup() { 52 | dest="$1" 53 | if [[ $2 == 1 ]]; then 54 | maybe_sudo="sudo" 55 | else 56 | maybe_sudo="" 57 | fi 58 | 59 | if [[ ! -e "$dest" ]]; then return 1; fi 60 | 61 | if [ -e "$dest.old" ]; then 62 | counter=1 63 | while [ -e "$dest.old.$counter" ]; do 64 | counter=$((counter + 1)) 65 | done 66 | $maybe_sudo mv "$dest" "$dest.old.$counter" 67 | else 68 | $maybe_sudo mv "$dest" "$dest.old" 69 | fi 70 | } 71 | 72 | copy_with_backup() { 73 | source="$1" 74 | dest="$2" 75 | if [[ $3 == 1 ]]; then 76 | maybe_sudo="sudo" 77 | else 78 | maybe_sudo="" 79 | fi 80 | 81 | 82 | if [[ ! -e "$source" ]]; then 83 | return 1 84 | fi 85 | 86 | if [ -e "$dest" ]; then 87 | backup "$dest" $3 88 | fi 89 | 90 | $maybe_sudo cp "$source" "$dest" 91 | } 92 | 93 | if [ -f /proc/device-tree/model ]; then 94 | read -r -d '' model < /proc/device-tree/model 95 | 96 | case $model in 97 | "Raspberry Pi 4"*) 98 | defconfig="bcm2711" 99 | is_pi=1 100 | ;; 101 | "Raspberry Pi 3"*) 102 | defconfig="bcmrpi3" 103 | is_pi=1 104 | ;; 105 | "Raspberry Pi 2"*) 106 | defconfig="bcm2709" 107 | is_pi=1 108 | ;; 109 | "Raspberry Pi Zero"* | "Raspberry Pi 1"*) 110 | defconfig="bcmrpi" 111 | is_pi=1 112 | ;; 113 | *) 114 | if [ -f "/proc/cpuinfo" ]; then 115 | tempconfig=$(cat /proc/cpuinfo | grep "Hardware" | awk '{print tolower($3)}') 116 | if [ -n "$tempconfig" ]; then 117 | defconfig=$tempconfig 118 | else 119 | defconfig=$(uname -m) 120 | generic_defconfig=1 121 | fi 122 | else 123 | defconfig=$(uname -m) 124 | generic_defconfig=1 125 | fi 126 | ;; 127 | esac 128 | else 129 | defconfig=$(uname -m) 130 | generic_defconfig=1 131 | fi 132 | 133 | if [ -e "$WORKDIR" ]; then 134 | rm -r "$WORKDIR" 135 | fi 136 | 137 | mkdir -p "$WORKDIR" || abort 138 | cd "$WORKDIR" || abort 139 | 140 | echo " - Preparing the system to compile the kernel..." 141 | sudo apt update 142 | sudo apt install build-essential libncurses-dev bison flex libssl-dev xz-utils libelf-dev fakeroot patch devscripts || abort 143 | 144 | MAKE="make -j$JOBS" 145 | 146 | echo " - Downloading the kernel source package..." 147 | apt source linux || abort 148 | 149 | if [[ "$DEB_BUILD" != 1 ]]; then 150 | if [[ "$generic_defconfig" == 1 ]]; then 151 | echo " - Warning: Couldn't get information for defconfig. Falling back to the generic defconfig." 1>&2 152 | fi 153 | 154 | echo " - Defconfig: $defconfig" 155 | fi 156 | 157 | cd linux-* 158 | 159 | if [[ "$DEB_BUILD" == 1 ]]; then 160 | out=$(dpkg-checkbuilddeps 2>&1 >/dev/null || true) 161 | 162 | if [[ ! -z "$out" ]]; then 163 | depln=$(echo "$out" | sed -n 's/.*Unmet build dependencies: //p') 164 | 165 | if [[ ! -z "$depln" ]]; then 166 | deps=$(echo "$depln" \ 167 | | sed -E 's/\([^)]*\)//g' \ 168 | | sed -E 's/([[:alnum:]._:+-]+)\s*\|\s*[[:alnum:]._:+-]+/\1/g') 169 | 170 | echo " - Installing missing build dependencies." 171 | sudo apt install -y $deps 172 | fi 173 | fi 174 | fi 175 | 176 | if patch -sfp0 --ignore-whitespace --dry-run net/bluetooth/l2cap_sock.c < "$PATCH" &>/dev/null; then 177 | if patch -s --ignore-whitespace net/bluetooth/l2cap_sock.c < "$PATCH" &>/dev/null; then 178 | echo " - Applied patch to net/bluetooth/l2cap_sock.c." 179 | else 180 | echo " - Could not apply patch to net/bluetooth/l2cap_sock.c. Aborting." 1>&2 181 | exit 1 182 | fi 183 | else 184 | echo " - Patch already applied." 185 | fi 186 | 187 | if [[ "$DEB_BUILD" == 1 ]]; then 188 | export DEB_BUILD_OPTIONS="parallel=$JOBS" 189 | export DEB_BUILD_PROFILES="nodoc" 190 | run fakeroot debian/rules binary -j$JOBS || abort 191 | else 192 | run $MAKE clean 193 | run $MAKE mrproper 194 | run $MAKE ${defconfig}_defconfig || run $MAKE defconfig 195 | if [[ "$RUN_MENUCONFIG" == 1 ]]; then 196 | run $MAKE menuconfig 197 | fi 198 | if [[ "$is_pi" == 1 ]]; then 199 | run $MAKE Image.gz modules dtbs || abort 200 | else 201 | run $MAKE || abort 202 | fi 203 | fi 204 | 205 | if [[ -e "/boot/firmware/kernel8.img" ]]; then 206 | kn=8 207 | elif [[ -e "/boot/firmware/kernel7.img" ]]; then 208 | kn=7 209 | fi 210 | 211 | case $(uname -m) in 212 | aarch64) arch="arm64" ;; 213 | armv7l) arch="arm" ;; 214 | i386|x86_64|amd64) arch="x86" ;; 215 | *) arch="" ;; 216 | esac 217 | 218 | if [[ "$INSTALL" == 1 ]]; then 219 | if [[ "$DEB_BUILD" == 1 ]]; then 220 | run sudo dpkg -i ../*.deb || abort 221 | else 222 | run sudo $MAKE modules_install || abort 223 | if [[ "$is_pi" == 1 ]]; then 224 | if [[ "$arch" == "" ]]; then 225 | echo " - Error: Could not detect the architecture ($(uname -m)) - you have to copy the image to /boot yourself." 1>&2 226 | exit 1 227 | fi 228 | 229 | echo -n " - Copying the Linux image to /boot..." 230 | copy_with_backup arch/$arch/boot/Image /boot/firmware/kernel$kn.img 1 || abort 231 | backup /boot/firmware/overlays 1 232 | run sudo mkdir /boot/firmware/overlays 233 | run sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/ 234 | run sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/firmware/overlays/ 235 | run sudo cp arch/arm64/boot/dts/overlays/README /boot/firmware/overlays/ 236 | echo "done.\n - To restore the original kernel, copy /boot/firmware/kernel$kn.img.old back to /boot/firmware/kernel$kn.img" 237 | else 238 | run sudo $MAKE install || abort 239 | fi 240 | fi 241 | echo " - Done, the patched kernel has been successfully installed!" 242 | echo " - Reboot to run the new kernel." 243 | else 244 | echo -n " - Successfully built the patched kernel. To install it, just " 245 | if [[ "$DEB_BUILD" == 1 ]]; then 246 | echo "install the .deb packages inside the $WORKDIR directory:" 247 | echo " - sudo apt install $WORKDIR/*.deb" 248 | elif [[ "$is_pi" == 1 ]]; then 249 | echo "copy $(readlink -f arch/$arch/boot/Image) to /boot/firmware/kernel$kn.img." 250 | else 251 | echo "run \"sudo $MAKE install\" inside the directory $WORKDIR." 252 | fi 253 | fi 254 | -------------------------------------------------------------------------------- /spectrum.c: -------------------------------------------------------------------------------- 1 | #include 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 scalar 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 scalar 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(scalar 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 | #if defined(DOUBLE64) 14 | typedef double scalar; 15 | #elif defined(FIXEDPOINT) 16 | #include 17 | typedef fract scalar; 18 | #else 19 | typedef float scalar; 20 | #endif 21 | 22 | int Max(int a, int b); 23 | int Min(int a, int b); 24 | uint32_t BitReverse32(uint32_t value, int bitCount); 25 | int32_t SignExtend32(int32_t value, int bits); 26 | int16_t Clamp16(int value); 27 | int Round(scalar x); 28 | 29 | --------------------------------------------------------------------------------