twinvq.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * TwinVQ decoder
  3. * Copyright (c) 2009 Vitor Sessak
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVCODEC_TWINVQ_H
  22. #define AVCODEC_TWINVQ_H
  23. #include <math.h>
  24. #include <stdint.h>
  25. #include "libavutil/common.h"
  26. #include "libavutil/float_dsp.h"
  27. #include "avcodec.h"
  28. #include "fft.h"
  29. #include "internal.h"
  30. enum TwinVQCodec {
  31. TWINVQ_CODEC_VQF,
  32. TWINVQ_CODEC_METASOUND,
  33. };
  34. enum TwinVQFrameType {
  35. TWINVQ_FT_SHORT = 0, ///< Short frame (divided in n sub-blocks)
  36. TWINVQ_FT_MEDIUM, ///< Medium frame (divided in m<n sub-blocks)
  37. TWINVQ_FT_LONG, ///< Long frame (single sub-block + PPC)
  38. TWINVQ_FT_PPC, ///< Periodic Peak Component (part of the long frame)
  39. };
  40. #define TWINVQ_PPC_SHAPE_CB_SIZE 64
  41. #define TWINVQ_PPC_SHAPE_LEN_MAX 60
  42. #define TWINVQ_SUB_AMP_MAX 4500.0
  43. #define TWINVQ_MULAW_MU 100.0
  44. #define TWINVQ_GAIN_BITS 8
  45. #define TWINVQ_AMP_MAX 13000.0
  46. #define TWINVQ_SUB_GAIN_BITS 5
  47. #define TWINVQ_WINDOW_TYPE_BITS 4
  48. #define TWINVQ_PGAIN_MU 200
  49. #define TWINVQ_LSP_COEFS_MAX 20
  50. #define TWINVQ_LSP_SPLIT_MAX 4
  51. #define TWINVQ_CHANNELS_MAX 2
  52. #define TWINVQ_SUBBLOCKS_MAX 16
  53. #define TWINVQ_BARK_N_COEF_MAX 4
  54. #define TWINVQ_MAX_FRAMES_PER_PACKET 2
  55. /**
  56. * Parameters and tables that are different for each frame type
  57. */
  58. struct TwinVQFrameMode {
  59. uint8_t sub; ///< Number subblocks in each frame
  60. const uint16_t *bark_tab;
  61. /** number of distinct bark scale envelope values */
  62. uint8_t bark_env_size;
  63. const int16_t *bark_cb; ///< codebook for the bark scale envelope (BSE)
  64. uint8_t bark_n_coef;///< number of BSE CB coefficients to read
  65. uint8_t bark_n_bit; ///< number of bits of the BSE coefs
  66. //@{
  67. /** main codebooks for spectrum data */
  68. const int16_t *cb0;
  69. const int16_t *cb1;
  70. //@}
  71. uint8_t cb_len_read; ///< number of spectrum coefficients to read
  72. };
  73. typedef struct TwinVQFrameData {
  74. int window_type;
  75. enum TwinVQFrameType ftype;
  76. uint8_t main_coeffs[1024];
  77. uint8_t ppc_coeffs[TWINVQ_PPC_SHAPE_LEN_MAX];
  78. uint8_t gain_bits[TWINVQ_CHANNELS_MAX];
  79. uint8_t sub_gain_bits[TWINVQ_CHANNELS_MAX * TWINVQ_SUBBLOCKS_MAX];
  80. uint8_t bark1[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX][TWINVQ_BARK_N_COEF_MAX];
  81. uint8_t bark_use_hist[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX];
  82. uint8_t lpc_idx1[TWINVQ_CHANNELS_MAX];
  83. uint8_t lpc_idx2[TWINVQ_CHANNELS_MAX][TWINVQ_LSP_SPLIT_MAX];
  84. uint8_t lpc_hist_idx[TWINVQ_CHANNELS_MAX];
  85. int p_coef[TWINVQ_CHANNELS_MAX];
  86. int g_coef[TWINVQ_CHANNELS_MAX];
  87. } TwinVQFrameData;
  88. /**
  89. * Parameters and tables that are different for every combination of
  90. * bitrate/sample rate
  91. */
  92. typedef struct TwinVQModeTab {
  93. struct TwinVQFrameMode fmode[3]; ///< frame type-dependent parameters
  94. uint16_t size; ///< frame size in samples
  95. uint8_t n_lsp; ///< number of lsp coefficients
  96. const float *lspcodebook;
  97. /* number of bits of the different LSP CB coefficients */
  98. uint8_t lsp_bit0;
  99. uint8_t lsp_bit1;
  100. uint8_t lsp_bit2;
  101. uint8_t lsp_split; ///< number of CB entries for the LSP decoding
  102. const int16_t *ppc_shape_cb; ///< PPC shape CB
  103. /** number of the bits for the PPC period value */
  104. uint8_t ppc_period_bit;
  105. uint8_t ppc_shape_bit; ///< number of bits of the PPC shape CB coeffs
  106. uint8_t ppc_shape_len; ///< size of PPC shape CB
  107. uint8_t pgain_bit; ///< bits for PPC gain
  108. /** constant for peak period to peak width conversion */
  109. uint16_t peak_per2wid;
  110. } TwinVQModeTab;
  111. typedef struct TwinVQContext {
  112. AVCodecContext *avctx;
  113. AVFloatDSPContext *fdsp;
  114. FFTContext mdct_ctx[3];
  115. const TwinVQModeTab *mtab;
  116. int is_6kbps;
  117. // history
  118. float lsp_hist[2][20]; ///< LSP coefficients of the last frame
  119. float bark_hist[3][2][40]; ///< BSE coefficients of last frame
  120. // bitstream parameters
  121. int16_t permut[4][4096];
  122. uint8_t length[4][2]; ///< main codebook stride
  123. uint8_t length_change[4];
  124. uint8_t bits_main_spec[2][4][2]; ///< bits for the main codebook
  125. int bits_main_spec_change[4];
  126. int n_div[4];
  127. float *spectrum;
  128. float *curr_frame; ///< non-interleaved output
  129. float *prev_frame; ///< non-interleaved previous frame
  130. int last_block_pos[2];
  131. int discarded_packets;
  132. float *cos_tabs[3];
  133. // scratch buffers
  134. float *tmp_buf;
  135. int frame_size, frames_per_packet, cur_frame;
  136. TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET];
  137. enum TwinVQCodec codec;
  138. int (*read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx,
  139. const uint8_t *buf, int buf_size);
  140. void (*dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in,
  141. int use_hist, int ch, float *out, float gain,
  142. enum TwinVQFrameType ftype);
  143. void (*decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef,
  144. const float *shape, float *speech);
  145. } TwinVQContext;
  146. extern const enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[];
  147. /** @note not speed critical, hence not optimized */
  148. static inline void twinvq_memset_float(float *buf, float val, int size)
  149. {
  150. while (size--)
  151. *buf++ = val;
  152. }
  153. static inline float twinvq_mulawinv(float y, float clip, float mu)
  154. {
  155. y = av_clipf(y / clip, -1, 1);
  156. return clip * FFSIGN(y) * (exp(log(1 + mu) * fabs(y)) - 1) / mu;
  157. }
  158. int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
  159. int *got_frame_ptr, AVPacket *avpkt);
  160. int ff_twinvq_decode_close(AVCodecContext *avctx);
  161. int ff_twinvq_decode_init(AVCodecContext *avctx);
  162. #endif /* AVCODEC_TWINVQ_H */