Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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.  
  22. #ifndef AVCODEC_TWINVQ_H
  23. #define AVCODEC_TWINVQ_H
  24.  
  25. #include <math.h>
  26. #include <stdint.h>
  27.  
  28. #include "libavutil/common.h"
  29. #include "libavutil/float_dsp.h"
  30. #include "avcodec.h"
  31. #include "fft.h"
  32. #include "internal.h"
  33.  
  34. enum TwinVQCodec {
  35.     TWINVQ_CODEC_VQF,
  36.     TWINVQ_CODEC_METASOUND,
  37. };
  38.  
  39. enum TwinVQFrameType {
  40.     TWINVQ_FT_SHORT = 0,  ///< Short frame  (divided in n   sub-blocks)
  41.     TWINVQ_FT_MEDIUM,     ///< Medium frame (divided in m<n sub-blocks)
  42.     TWINVQ_FT_LONG,       ///< Long frame   (single sub-block + PPC)
  43.     TWINVQ_FT_PPC,        ///< Periodic Peak Component (part of the long frame)
  44. };
  45.  
  46. #define TWINVQ_PPC_SHAPE_CB_SIZE 64
  47. #define TWINVQ_PPC_SHAPE_LEN_MAX 60
  48. #define TWINVQ_SUB_AMP_MAX       4500.0
  49. #define TWINVQ_MULAW_MU          100.0
  50. #define TWINVQ_GAIN_BITS         8
  51. #define TWINVQ_AMP_MAX           13000.0
  52. #define TWINVQ_SUB_GAIN_BITS     5
  53. #define TWINVQ_WINDOW_TYPE_BITS  4
  54. #define TWINVQ_PGAIN_MU          200
  55. #define TWINVQ_LSP_COEFS_MAX     20
  56. #define TWINVQ_LSP_SPLIT_MAX     4
  57. #define TWINVQ_CHANNELS_MAX      2
  58. #define TWINVQ_SUBBLOCKS_MAX     16
  59. #define TWINVQ_BARK_N_COEF_MAX   4
  60.  
  61. /**
  62.  * Parameters and tables that are different for each frame type
  63.  */
  64. struct TwinVQFrameMode {
  65.     uint8_t         sub;      ///< Number subblocks in each frame
  66.     const uint16_t *bark_tab;
  67.  
  68.     /** number of distinct bark scale envelope values */
  69.     uint8_t         bark_env_size;
  70.  
  71.     const int16_t  *bark_cb;    ///< codebook for the bark scale envelope (BSE)
  72.     uint8_t         bark_n_coef;///< number of BSE CB coefficients to read
  73.     uint8_t         bark_n_bit; ///< number of bits of the BSE coefs
  74.  
  75.     //@{
  76.     /** main codebooks for spectrum data */
  77.     const int16_t    *cb0;
  78.     const int16_t    *cb1;
  79.     //@}
  80.  
  81.     uint8_t         cb_len_read; ///< number of spectrum coefficients to read
  82. };
  83.  
  84. typedef struct TwinVQFrameData {
  85.     int     window_type;
  86.     enum TwinVQFrameType ftype;
  87.  
  88.     uint8_t main_coeffs[1024];
  89.     uint8_t ppc_coeffs[TWINVQ_PPC_SHAPE_LEN_MAX];
  90.  
  91.     uint8_t gain_bits[TWINVQ_CHANNELS_MAX];
  92.     uint8_t sub_gain_bits[TWINVQ_CHANNELS_MAX * TWINVQ_SUBBLOCKS_MAX];
  93.  
  94.     uint8_t bark1[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX][TWINVQ_BARK_N_COEF_MAX];
  95.     uint8_t bark_use_hist[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX];
  96.  
  97.     uint8_t lpc_idx1[TWINVQ_CHANNELS_MAX];
  98.     uint8_t lpc_idx2[TWINVQ_CHANNELS_MAX][TWINVQ_LSP_SPLIT_MAX];
  99.     uint8_t lpc_hist_idx[TWINVQ_CHANNELS_MAX];
  100.  
  101.     int     p_coef[TWINVQ_CHANNELS_MAX];
  102.     int     g_coef[TWINVQ_CHANNELS_MAX];
  103. } TwinVQFrameData;
  104.  
  105. /**
  106.  * Parameters and tables that are different for every combination of
  107.  * bitrate/sample rate
  108.  */
  109. typedef struct TwinVQModeTab {
  110.     struct TwinVQFrameMode fmode[3]; ///< frame type-dependant parameters
  111.  
  112.     uint16_t     size;        ///< frame size in samples
  113.     uint8_t      n_lsp;       ///< number of lsp coefficients
  114.     const float *lspcodebook;
  115.  
  116.     /* number of bits of the different LSP CB coefficients */
  117.     uint8_t      lsp_bit0;
  118.     uint8_t      lsp_bit1;
  119.     uint8_t      lsp_bit2;
  120.  
  121.     uint8_t      lsp_split;      ///< number of CB entries for the LSP decoding
  122.     const int16_t *ppc_shape_cb; ///< PPC shape CB
  123.  
  124.     /** number of the bits for the PPC period value */
  125.     uint8_t      ppc_period_bit;
  126.  
  127.     uint8_t      ppc_shape_bit;  ///< number of bits of the PPC shape CB coeffs
  128.     uint8_t      ppc_shape_len;  ///< size of PPC shape CB
  129.     uint8_t      pgain_bit;      ///< bits for PPC gain
  130.  
  131.     /** constant for peak period to peak width conversion */
  132.     uint16_t     peak_per2wid;
  133. } TwinVQModeTab;
  134.  
  135. typedef struct TwinVQContext {
  136.     AVCodecContext *avctx;
  137.     AVFloatDSPContext fdsp;
  138.     FFTContext mdct_ctx[3];
  139.  
  140.     const TwinVQModeTab *mtab;
  141.  
  142.     // history
  143.     float lsp_hist[2][20];           ///< LSP coefficients of the last frame
  144.     float bark_hist[3][2][40];       ///< BSE coefficients of last frame
  145.  
  146.     // bitstream parameters
  147.     int16_t permut[4][4096];
  148.     uint8_t length[4][2];            ///< main codebook stride
  149.     uint8_t length_change[4];
  150.     uint8_t bits_main_spec[2][4][2]; ///< bits for the main codebook
  151.     int bits_main_spec_change[4];
  152.     int n_div[4];
  153.  
  154.     float *spectrum;
  155.     float *curr_frame;               ///< non-interleaved output
  156.     float *prev_frame;               ///< non-interleaved previous frame
  157.     int last_block_pos[2];
  158.     int discarded_packets;
  159.  
  160.     float *cos_tabs[3];
  161.  
  162.     // scratch buffers
  163.     float *tmp_buf;
  164.  
  165.     TwinVQFrameData bits;
  166.  
  167.     enum TwinVQCodec codec;
  168.  
  169.     int (*read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx,
  170.                           const uint8_t *buf, int buf_size);
  171.     void (*dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in,
  172.                          int use_hist, int ch, float *out, float gain,
  173.                          enum TwinVQFrameType ftype);
  174.     void (*decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef,
  175.                        const float *shape, float *speech);
  176. } TwinVQContext;
  177.  
  178. extern const enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[];
  179.  
  180. /** @note not speed critical, hence not optimized */
  181. static inline void twinvq_memset_float(float *buf, float val, int size)
  182. {
  183.     while (size--)
  184.         *buf++ = val;
  185. }
  186.  
  187. static inline float twinvq_mulawinv(float y, float clip, float mu)
  188. {
  189.     y = av_clipf(y / clip, -1, 1);
  190.     return clip * FFSIGN(y) * (exp(log(1 + mu) * fabs(y)) - 1) / mu;
  191. }
  192.  
  193. int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
  194.                            int *got_frame_ptr, AVPacket *avpkt);
  195. av_cold int ff_twinvq_decode_close(AVCodecContext *avctx);
  196. av_cold int ff_twinvq_decode_init(AVCodecContext *avctx);
  197.  
  198. #endif /* AVCODEC_TWINVQ_DATA_H */
  199.