Skip to content

Commit

Permalink
Mark pre-calculated parameters as constant
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Nov 20, 2024
1 parent dfec26f commit 6f6c8b4
Show file tree
Hide file tree
Showing 47 changed files with 967 additions and 1,346 deletions.
16 changes: 16 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
BasedOnStyle: LLVM
ColumnLimit: 120
IndentWidth: 4
TabWidth: 4
---
Language: Cpp
AccessModifierOffset: -4
AlignEscapedNewlines: DontAlign
AllowShortFunctionsOnASingleLine: Inline
Cpp11BracedListStyle: false
IndentPPDirectives: AfterHash
PointerAlignment: Middle
SortIncludes: CaseInsensitive
UseTab: ForIndentation
---
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# [open]aptx - CMakeLists.txt
# Copyright (c) 2017-2021 Arkadiusz Bokowy

cmake_minimum_required(VERSION 3.2)
cmake_minimum_required(VERSION 3.22)
project(openaptx
VERSION 1.3.2
DESCRIPTION "Reverse-engineered apt-X audio codec"
Expand Down
97 changes: 53 additions & 44 deletions include/aptx422.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#ifndef OPENAPTX_APTX422_H_
#define OPENAPTX_APTX422_H_

#include <assert.h>
#include <stddef.h>
#include <stdint.h>

Expand All @@ -19,29 +20,29 @@ extern "C" {
#endif

#define APTX_CHANNELS 2
#define APTX_SUBBANDS 4

enum aptX_subband {
APTX_SUBBAND_LL = 0,
APTX_SUBBAND_LH = 1,
APTX_SUBBAND_HL = 2,
APTX_SUBBAND_HH = 3,
__APTX_SUBBAND_MAX
};

typedef struct aptX_subband_params_422_t {
int32_t *p1;
int32_t *bit16_sl1;
int32_t *p3;
int32_t *dith16_sf1;
int32_t *mLamb16;
int32_t *incr16;
int32_t * p1;
const int32_t * bit16_sl1;
int32_t * p3;
const int32_t * dith16_sf1;
const int32_t * mLamb16;
const int32_t * incr16;
/* number of used bits */
int32_t bits;
int32_t unk1;
int32_t unk2;
/* width of prediction filter */
int32_t filter_width;
} __attribute__ ((packed)) aptX_subband_params_422;
} __attribute__((packed)) aptX_subband_params_422;

typedef struct aptX_prediction_filter_422_t {
int32_t width;
Expand All @@ -58,87 +59,95 @@ typedef struct aptX_prediction_filter_422_t {
int32_t unk6;
int32_t unk7;
int32_t unk8;
} __attribute__ ((packed)) aptX_prediction_filter_422;
} __attribute__((packed)) aptX_prediction_filter_422;

typedef struct aptX_inverter_422_t {
int32_t *subband_param_p1;
int32_t *subband_param_bit16_sl1;
int32_t *subband_param_dith16_sf1;
int32_t *subband_param_incr16;
int32_t * subband_param_p1;
const int32_t * subband_param_bit16_sl1;
const int32_t * subband_param_dith16_sf1;
const int32_t * subband_param_incr16;
int32_t subband_param_unk1;
int32_t subband_param_unk2;
int32_t unk9;
int32_t unk10;
int32_t unk11;
int32_t *log;
} __attribute__ ((packed)) aptX_inverter_422;
const int32_t * log;
} __attribute__((packed)) aptX_inverter_422;

typedef struct aptX_processor_422_t {
aptX_prediction_filter_422 filter;
aptX_inverter_422 inverter;
} __attribute__ ((packed)) aptX_processor_422;
} __attribute__((packed)) aptX_processor_422;

typedef struct aptX_quantizer_422_t {
int32_t subband_param_bits;
int32_t *subband_param_p1;
int32_t *subband_param_bit16_sl1;
int32_t *subband_param_p3;
int32_t *subband_param_mLamb16;
int32_t * subband_param_p1;
const int32_t * subband_param_bit16_sl1;
int32_t * subband_param_p3;
const int32_t * subband_param_mLamb16;
int32_t unk1;
int32_t unk2;
int32_t unk3;
} __attribute__ ((packed)) aptX_quantizer_422;
} __attribute__((packed)) aptX_quantizer_422;

typedef struct aptX_subband_encoder_422_t {
aptX_processor_422 processor[__APTX_SUBBAND_MAX];
aptX_processor_422 processor[APTX_SUBBANDS];
int32_t codeword;
int32_t dither_sign;
int32_t dither[__APTX_SUBBAND_MAX];
aptX_quantizer_422 quantizer[__APTX_SUBBAND_MAX];
} __attribute__ ((packed)) aptX_subband_encoder_422;
int32_t dither[APTX_SUBBANDS];
aptX_quantizer_422 quantizer[APTX_SUBBANDS];
} __attribute__((packed)) aptX_subband_encoder_422;

typedef struct aptX_QMF_analyzer_422_t {
int16_t outer[2][32];
int32_t inner[4][32];
int32_t i_inner;
int32_t i_outer;
} __attribute__ ((packed)) aptX_QMF_analyzer_422;
} aptX_QMF_analyzer_422;

static_assert(sizeof(aptX_QMF_analyzer_422) ==
2 * 32 * sizeof(int16_t) + 4 * 32 * sizeof(int32_t) + (1 + 1) * sizeof(int32_t),
"aptX_QMF_analyzer_422 size mismatch");

typedef struct aptX_encoder_422_t {
int32_t shift;
int32_t sync;
aptX_subband_encoder_422 encoder[APTX_CHANNELS];
aptX_QMF_analyzer_422 analyzer[APTX_CHANNELS];
} __attribute__ ((packed)) aptX_encoder_422;
} aptX_encoder_422;

static_assert(sizeof(aptX_encoder_422) == (1 + 1) * sizeof(int32_t) + APTX_CHANNELS * sizeof(aptX_subband_encoder_422) +
APTX_CHANNELS * sizeof(aptX_QMF_analyzer_422),
"aptX_encoder_422 size mismatch");

/* Functions (theoretically) available in the apt-X library. Some of them
* might not be present in the particular version of the apt-X library due
* to compiler optimizations. Also note, that not all of them are public. */
int32_t updateCodewordHistory(const int32_t a[4], int32_t codeword);
int32_t generateDither(int32_t codeword, int32_t dither[4]);
uint16_t packCodeword(const aptX_subband_encoder_422 *e);
uint16_t packCodeword(const aptX_subband_encoder_422 * e);
void AsmQmfConvO(const int16_t a1[16], const int16_t a2[16], const int32_t coeffs[16], int32_t out[3]);
void AsmQmfConvI(const int32_t a1[16], const int32_t a2[16], const int32_t coeffs[16], int32_t out[2]);
void QmfAnalysisFilter(const int32_t pcm[4], aptX_QMF_analyzer_422 *a, const int32_t refs[4], int32_t diff[4]);
void QmfAnalysisFilter(const int32_t pcm[4], aptX_QMF_analyzer_422 * a, const int32_t refs[4], int32_t diff[4]);
int32_t BsearchLL(uint32_t a, int32_t b, const int32_t data[65]);
int32_t BsearchLH(uint32_t a, int32_t b, const int32_t data[9]);
int32_t BsearchHL(uint32_t a, int32_t b, const int32_t data[3]);
int32_t BsearchHH(uint32_t a, int32_t b, const int32_t data[5]);
void quantiseDifferenceLL(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 *q);
void quantiseDifferenceLH(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 *q);
void quantiseDifferenceHL(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 *q);
void quantiseDifferenceHH(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 *q);
void aptxEncode(int32_t pcm[4], aptX_QMF_analyzer_422 *a, aptX_subband_encoder_422 *e);
void insertSync(aptX_subband_encoder_422 *e1, aptX_subband_encoder_422 *e2, int32_t *sync);
void invertQuantisation(int32_t a, int32_t dither, aptX_inverter_422 *i);
void invertQuantisationHL(int32_t a, int32_t dither, aptX_inverter_422 *i);
void performPredictionFiltering(int32_t a, aptX_prediction_filter_422 *f);
void performPredictionFilteringLL(int32_t a, aptX_prediction_filter_422 *f);
void performPredictionFilteringHL(int32_t a, aptX_prediction_filter_422 *f);
void processSubband(int32_t a, int32_t dither, aptX_prediction_filter_422 *f, aptX_inverter_422 *i);
void processSubbandLL(int32_t a, int32_t dither, aptX_prediction_filter_422 *f, aptX_inverter_422 *i);
void processSubbandHL(int32_t a, int32_t dither, aptX_prediction_filter_422 *f, aptX_inverter_422 *i);
void aptxPostEncode(aptX_subband_encoder_422 *e);
void quantiseDifferenceLL(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 * q);
void quantiseDifferenceLH(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 * q);
void quantiseDifferenceHL(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 * q);
void quantiseDifferenceHH(int32_t diff, int32_t dither, int32_t c, aptX_quantizer_422 * q);
void aptxEncode(int32_t pcm[4], aptX_QMF_analyzer_422 * a, aptX_subband_encoder_422 * e);
void insertSync(aptX_subband_encoder_422 * e1, aptX_subband_encoder_422 * e2, int32_t * sync);
void invertQuantisation(int32_t a, int32_t dither, aptX_inverter_422 * i);
void invertQuantisationHL(int32_t a, int32_t dither, aptX_inverter_422 * i);
void performPredictionFiltering(int32_t a, aptX_prediction_filter_422 * f);
void performPredictionFilteringLL(int32_t a, aptX_prediction_filter_422 * f);
void performPredictionFilteringHL(int32_t a, aptX_prediction_filter_422 * f);
void processSubband(int32_t a, int32_t dither, aptX_prediction_filter_422 * f, aptX_inverter_422 * i);
void processSubbandLL(int32_t a, int32_t dither, aptX_prediction_filter_422 * f, aptX_inverter_422 * i);
void processSubbandHL(int32_t a, int32_t dither, aptX_prediction_filter_422 * f, aptX_inverter_422 * i);
void aptxPostEncode(aptX_subband_encoder_422 * e);

#ifdef __cplusplus
}
Expand Down
80 changes: 45 additions & 35 deletions include/aptxHD100.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#ifndef OPENAPTX_APTXHD100_H_
#define OPENAPTX_APTXHD100_H_

#include <assert.h>
#include <stddef.h>
#include <stdint.h>

Expand All @@ -19,29 +20,29 @@ extern "C" {
#endif

#define APTXHD_CHANNELS 2
#define APTXHD_SUBBANDS 4

enum aptXHD_subband {
APTXHD_SUBBAND_LL = 0,
APTXHD_SUBBAND_LH = 1,
APTXHD_SUBBAND_HL = 2,
APTXHD_SUBBAND_HH = 3,
__APTXHD_SUBBAND_MAX
};

typedef struct aptXHD_subband_params_100_t {
int32_t *p1;
int32_t *bit16_sl1;
int32_t *p3;
int32_t *dith16_sf1;
int32_t *mLamb16;
int32_t *incr16;
int32_t * p1;
const int32_t * bit16_sl1;
int32_t * p3;
const int32_t * dith16_sf1;
const int32_t * mLamb16;
const int32_t * incr16;
/* number of used bits */
int32_t bits;
int32_t unk1;
int32_t unk2;
/* width of prediction filter */
int32_t filter_width;
} __attribute__ ((packed)) aptXHD_subband_params_100;
} __attribute__((packed)) aptXHD_subband_params_100;

typedef struct aptXHD_prediction_filter_100_t {
int32_t width;
Expand All @@ -58,70 +59,79 @@ typedef struct aptXHD_prediction_filter_100_t {
int32_t unk6;
int32_t unk7;
int32_t unk8;
} __attribute__ ((packed)) aptXHD_prediction_filter_100;
} __attribute__((packed)) aptXHD_prediction_filter_100;

typedef struct aptXHD_inverter_100_t {
int32_t *subband_param_p1;
int32_t *subband_param_bit16_sl1;
int32_t *subband_param_dith16_sf1;
int32_t *subband_param_incr16;
int32_t * subband_param_p1;
const int32_t * subband_param_bit16_sl1;
const int32_t * subband_param_dith16_sf1;
const int32_t * subband_param_incr16;
int32_t subband_param_unk1;
int32_t subband_param_unk2;
int32_t unk9;
int32_t unk10;
int32_t unk11;
int32_t *log;
} __attribute__ ((packed)) aptXHD_inverter_100;
const int32_t * log;
} __attribute__((packed)) aptXHD_inverter_100;

typedef struct aptXHD_processor_100_t {
aptXHD_prediction_filter_100 filter;
aptXHD_inverter_100 inverter;
} __attribute__ ((packed)) aptXHD_processor_100;
} __attribute__((packed)) aptXHD_processor_100;

typedef struct aptXHD_quantizer_100_t {
int32_t subband_param_bits;
int32_t *subband_param_p1;
int32_t *subband_param_bit16_sl1;
int32_t *subband_param_p3;
int32_t *subband_param_mLamb16;
int32_t * subband_param_p1;
const int32_t * subband_param_bit16_sl1;
int32_t * subband_param_p3;
const int32_t * subband_param_mLamb16;
int32_t unk1;
int32_t unk2;
int32_t unk3;
} __attribute__ ((packed)) aptXHD_quantizer_100;
} __attribute__((packed)) aptXHD_quantizer_100;

typedef struct aptXHD_subband_encoder_100_t {
aptXHD_processor_100 processor[__APTXHD_SUBBAND_MAX];
aptXHD_processor_100 processor[APTXHD_SUBBANDS];
int32_t codeword;
int32_t dither_sign;
int32_t dither[__APTXHD_SUBBAND_MAX];
aptXHD_quantizer_100 quantizer[__APTXHD_SUBBAND_MAX];
} __attribute__ ((packed)) aptXHD_subband_encoder_100;
int32_t dither[APTXHD_SUBBANDS];
aptXHD_quantizer_100 quantizer[APTXHD_SUBBANDS];
} __attribute__((packed)) aptXHD_subband_encoder_100;

typedef struct aptXHD_QMF_analyzer_100_t {
int32_t outer[2][32];
int32_t inner[4][32];
int32_t i_inner;
int32_t i_outer;
} __attribute__ ((packed)) aptXHD_QMF_analyzer_100;
} aptXHD_QMF_analyzer_100;

static_assert(sizeof(aptXHD_QMF_analyzer_100) ==
2 * 32 * sizeof(int32_t) + 4 * 32 * sizeof(int32_t) + (1 + 1) * sizeof(int32_t),
"aptXHD_QMF_analyzer_100 size mismatch");

typedef struct aptXHD_encoder_t {
int32_t shift;
int32_t sync;
aptXHD_subband_encoder_100 encoder[APTXHD_CHANNELS];
aptXHD_QMF_analyzer_100 analyzer[APTXHD_CHANNELS];
} __attribute__ ((packed)) aptXHD_encoder_100;
} aptXHD_encoder_100;

static_assert(sizeof(aptXHD_encoder_100) == (1 + 1) * sizeof(int32_t) +
APTXHD_CHANNELS * sizeof(aptXHD_subband_encoder_100) +
APTXHD_CHANNELS * sizeof(aptXHD_QMF_analyzer_100),
"aptXHD_encoder_100 size mismatch");

void AsmQmfConvO(const int32_t a1[16], const int32_t a2[16], const int32_t coeffs[16], int32_t out[3]);
void AsmQmfConvI(const int32_t a1[16], const int32_t a2[16], const int32_t coeffs[16], int32_t out[2]);
int32_t BsearchLH(uint32_t a, int32_t b, const int32_t data[9]);
void quantiseDifferenceLL(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 *q);
void quantiseDifferenceLH(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 *q);
void quantiseDifferenceHL(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 *q);
void quantiseDifferenceHH(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 *q);
void aptxEncode(int32_t pcm[4], aptXHD_QMF_analyzer_100 *a, aptXHD_subband_encoder_100 *e);
void processSubband(int32_t a, int32_t dither, aptXHD_prediction_filter_100 *f, aptXHD_inverter_100 *i);
void processSubbandLL(int32_t a, int32_t dither, aptXHD_prediction_filter_100 *f, aptXHD_inverter_100 *i);
void processSubbandHL(int32_t a, int32_t dither, aptXHD_prediction_filter_100 *f, aptXHD_inverter_100 *i);
void quantiseDifferenceLL(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 * q);
void quantiseDifferenceLH(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 * q);
void quantiseDifferenceHL(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 * q);
void quantiseDifferenceHH(int32_t diff, int32_t dither, int32_t c, aptXHD_quantizer_100 * q);
void aptxEncode(int32_t pcm[4], aptXHD_QMF_analyzer_100 * a, aptXHD_subband_encoder_100 * e);
void processSubband(int32_t a, int32_t dither, aptXHD_prediction_filter_100 * f, aptXHD_inverter_100 * i);
void processSubbandLL(int32_t a, int32_t dither, aptXHD_prediction_filter_100 * f, aptXHD_inverter_100 * i);
void processSubbandHL(int32_t a, int32_t dither, aptXHD_prediction_filter_100 * f, aptXHD_inverter_100 * i);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 6f6c8b4

Please sign in to comment.