-#include "twinvq_data.h"
-
-enum FrameType {
- FT_SHORT = 0, ///< Short frame (divided in n sub-blocks)
- FT_MEDIUM, ///< Medium frame (divided in m<n sub-blocks)
- FT_LONG, ///< Long frame (single sub-block + PPC)
- FT_PPC, ///< Periodic Peak Component (part of the long frame)
-};
-
-/**
- * Parameters and tables that are different for each frame type
- */
-struct FrameMode {
- uint8_t sub; ///< Number subblocks in each frame
- const uint16_t *bark_tab;
-
- /** number of distinct bark scale envelope values */
- uint8_t bark_env_size;
-
- const int16_t *bark_cb; ///< codebook for the bark scale envelope (BSE)
- uint8_t bark_n_coef;///< number of BSE CB coefficients to read
- uint8_t bark_n_bit; ///< number of bits of the BSE coefs
-
- //@{
- /** main codebooks for spectrum data */
- const int16_t *cb0;
- const int16_t *cb1;
- //@}
-
- uint8_t cb_len_read; ///< number of spectrum coefficients to read
-};
-
-/**
- * Parameters and tables that are different for every combination of
- * bitrate/sample rate
- */
-typedef struct {
- struct FrameMode fmode[3]; ///< frame type-dependant parameters
-
- uint16_t size; ///< frame size in samples
- uint8_t n_lsp; ///< number of lsp coefficients
- const float *lspcodebook;
-
- /* number of bits of the different LSP CB coefficients */
- uint8_t lsp_bit0;
- uint8_t lsp_bit1;
- uint8_t lsp_bit2;
-
- uint8_t lsp_split; ///< number of CB entries for the LSP decoding
- const int16_t *ppc_shape_cb; ///< PPC shape CB
-
- /** number of the bits for the PPC period value */
- uint8_t ppc_period_bit;
-
- uint8_t ppc_shape_bit; ///< number of bits of the PPC shape CB coeffs
- uint8_t ppc_shape_len; ///< size of PPC shape CB
- uint8_t pgain_bit; ///< bits for PPC gain
-
- /** constant for peak period to peak width conversion */
- uint16_t peak_per2wid;
-} ModeTab;
-
-static const ModeTab mode_08_08 = {
- {
- { 8, bark_tab_s08_64, 10, tab.fcb08s , 1, 5, tab.cb0808s0, tab.cb0808s1, 18},
- { 2, bark_tab_m08_256, 20, tab.fcb08m , 2, 5, tab.cb0808m0, tab.cb0808m1, 16},
- { 1, bark_tab_l08_512, 30, tab.fcb08l , 3, 6, tab.cb0808l0, tab.cb0808l1, 17}
- },
- 512 , 12, tab.lsp08, 1, 5, 3, 3, tab.shape08 , 8, 28, 20, 6, 40
-};
-
-static const ModeTab mode_11_08 = {
- {
- { 8, bark_tab_s11_64, 10, tab.fcb11s , 1, 5, tab.cb1108s0, tab.cb1108s1, 29},
- { 2, bark_tab_m11_256, 20, tab.fcb11m , 2, 5, tab.cb1108m0, tab.cb1108m1, 24},
- { 1, bark_tab_l11_512, 30, tab.fcb11l , 3, 6, tab.cb1108l0, tab.cb1108l1, 27}
- },
- 512 , 16, tab.lsp11, 1, 6, 4, 3, tab.shape11 , 9, 36, 30, 7, 90
-};
-
-static const ModeTab mode_11_10 = {
- {
- { 8, bark_tab_s11_64, 10, tab.fcb11s , 1, 5, tab.cb1110s0, tab.cb1110s1, 21},
- { 2, bark_tab_m11_256, 20, tab.fcb11m , 2, 5, tab.cb1110m0, tab.cb1110m1, 18},
- { 1, bark_tab_l11_512, 30, tab.fcb11l , 3, 6, tab.cb1110l0, tab.cb1110l1, 20}
- },
- 512 , 16, tab.lsp11, 1, 6, 4, 3, tab.shape11 , 9, 36, 30, 7, 90
-};
-
-static const ModeTab mode_16_16 = {
- {
- { 8, bark_tab_s16_128, 10, tab.fcb16s , 1, 5, tab.cb1616s0, tab.cb1616s1, 16},
- { 2, bark_tab_m16_512, 20, tab.fcb16m , 2, 5, tab.cb1616m0, tab.cb1616m1, 15},
- { 1, bark_tab_l16_1024,30, tab.fcb16l , 3, 6, tab.cb1616l0, tab.cb1616l1, 16}
- },
- 1024, 16, tab.lsp16, 1, 6, 4, 3, tab.shape16 , 9, 56, 60, 7, 180
-};
-
-static const ModeTab mode_22_20 = {
- {
- { 8, bark_tab_s22_128, 10, tab.fcb22s_1, 1, 6, tab.cb2220s0, tab.cb2220s1, 18},
- { 2, bark_tab_m22_512, 20, tab.fcb22m_1, 2, 6, tab.cb2220m0, tab.cb2220m1, 17},
- { 1, bark_tab_l22_1024,32, tab.fcb22l_1, 4, 6, tab.cb2220l0, tab.cb2220l1, 18}
- },
- 1024, 16, tab.lsp22_1, 1, 6, 4, 3, tab.shape22_1, 9, 56, 36, 7, 144
-};
-
-static const ModeTab mode_22_24 = {
- {
- { 8, bark_tab_s22_128, 10, tab.fcb22s_1, 1, 6, tab.cb2224s0, tab.cb2224s1, 15},
- { 2, bark_tab_m22_512, 20, tab.fcb22m_1, 2, 6, tab.cb2224m0, tab.cb2224m1, 14},
- { 1, bark_tab_l22_1024,32, tab.fcb22l_1, 4, 6, tab.cb2224l0, tab.cb2224l1, 15}
- },
- 1024, 16, tab.lsp22_1, 1, 6, 4, 3, tab.shape22_1, 9, 56, 36, 7, 144
-};
-
-static const ModeTab mode_22_32 = {
- {
- { 4, bark_tab_s22_128, 10, tab.fcb22s_2, 1, 6, tab.cb2232s0, tab.cb2232s1, 11},
- { 2, bark_tab_m22_256, 20, tab.fcb22m_2, 2, 6, tab.cb2232m0, tab.cb2232m1, 11},
- { 1, bark_tab_l22_512, 32, tab.fcb22l_2, 4, 6, tab.cb2232l0, tab.cb2232l1, 12}
- },
- 512 , 16, tab.lsp22_2, 1, 6, 4, 4, tab.shape22_2, 9, 56, 36, 7, 72
-};
-
-static const ModeTab mode_44_40 = {
- {
- {16, bark_tab_s44_128, 10, tab.fcb44s , 1, 6, tab.cb4440s0, tab.cb4440s1, 18},
- { 4, bark_tab_m44_512, 20, tab.fcb44m , 2, 6, tab.cb4440m0, tab.cb4440m1, 17},
- { 1, bark_tab_l44_2048,40, tab.fcb44l , 4, 6, tab.cb4440l0, tab.cb4440l1, 17}
- },
- 2048, 20, tab.lsp44, 1, 6, 4, 4, tab.shape44 , 9, 84, 54, 7, 432
-};
-
-static const ModeTab mode_44_48 = {
- {
- {16, bark_tab_s44_128, 10, tab.fcb44s , 1, 6, tab.cb4448s0, tab.cb4448s1, 15},
- { 4, bark_tab_m44_512, 20, tab.fcb44m , 2, 6, tab.cb4448m0, tab.cb4448m1, 14},
- { 1, bark_tab_l44_2048,40, tab.fcb44l , 4, 6, tab.cb4448l0, tab.cb4448l1, 14}
- },
- 2048, 20, tab.lsp44, 1, 6, 4, 4, tab.shape44 , 9, 84, 54, 7, 432
-};
-
-typedef struct TwinContext {
- AVCodecContext *avctx;
- DSPContext dsp;
- FFTContext mdct_ctx[3];
-
- const ModeTab *mtab;
-
- // history
- float lsp_hist[2][20]; ///< LSP coefficients of the last frame
- float bark_hist[3][2][40]; ///< BSE coefficients of last frame
-
- // bitstream parameters
- int16_t permut[4][4096];
- uint8_t length[4][2]; ///< main codebook stride
- uint8_t length_change[4];
- uint8_t bits_main_spec[2][4][2]; ///< bits for the main codebook
- int bits_main_spec_change[4];
- int n_div[4];
-
- float *spectrum;
- float *curr_frame; ///< non-interleaved output
- float *prev_frame; ///< non-interleaved previous frame
- int last_block_pos[2];
-
- float *cos_tabs[3];
-
- // scratch buffers
- float *tmp_buf;
-} TwinContext;
-
-#define PPC_SHAPE_CB_SIZE 64
-#define PPC_SHAPE_LEN_MAX 60
-#define SUB_AMP_MAX 4500.0
-#define MULAW_MU 100.0
-#define GAIN_BITS 8
-#define AMP_MAX 13000.0
-#define SUB_GAIN_BITS 5
-#define WINDOW_TYPE_BITS 4
-#define PGAIN_MU 200
-#define LSP_COEFS_MAX 20
-#define LSP_SPLIT_MAX 4
-#define CHANNELS_MAX 2
-#define SUBBLOCKS_MAX 16
-#define BARK_N_COEF_MAX 4
-
-/** @note not speed critical, hence not optimized */
-static void memset_float(float *buf, float val, int size)
-{
- while (size--)
- *buf++ = val;
-}