FFmpeg
twinvq.h
Go to the documentation of this file.
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 
36 };
37 
39  TWINVQ_FT_SHORT = 0, ///< Short frame (divided in n sub-blocks)
40  TWINVQ_FT_MEDIUM, ///< Medium frame (divided in m<n sub-blocks)
41  TWINVQ_FT_LONG, ///< Long frame (single sub-block + PPC)
42  TWINVQ_FT_PPC, ///< Periodic Peak Component (part of the long frame)
43 };
44 
45 #define TWINVQ_PPC_SHAPE_CB_SIZE 64
46 #define TWINVQ_PPC_SHAPE_LEN_MAX 60
47 #define TWINVQ_SUB_AMP_MAX 4500.0
48 #define TWINVQ_MULAW_MU 100.0
49 #define TWINVQ_GAIN_BITS 8
50 #define TWINVQ_AMP_MAX 13000.0
51 #define TWINVQ_SUB_GAIN_BITS 5
52 #define TWINVQ_WINDOW_TYPE_BITS 4
53 #define TWINVQ_PGAIN_MU 200
54 #define TWINVQ_LSP_COEFS_MAX 20
55 #define TWINVQ_LSP_SPLIT_MAX 4
56 #define TWINVQ_CHANNELS_MAX 2
57 #define TWINVQ_SUBBLOCKS_MAX 16
58 #define TWINVQ_BARK_N_COEF_MAX 4
59 
60 #define TWINVQ_MAX_FRAMES_PER_PACKET 2
61 
62 /**
63  * Parameters and tables that are different for each frame type
64  */
66  uint8_t sub; ///< Number subblocks in each frame
67  const uint16_t *bark_tab;
68 
69  /** number of distinct bark scale envelope values */
70  uint8_t bark_env_size;
71 
72  const int16_t *bark_cb; ///< codebook for the bark scale envelope (BSE)
73  uint8_t bark_n_coef;///< number of BSE CB coefficients to read
74  uint8_t bark_n_bit; ///< number of bits of the BSE coefs
75 
76  //@{
77  /** main codebooks for spectrum data */
78  const int16_t *cb0;
79  const int16_t *cb1;
80  //@}
81 
82  uint8_t cb_len_read; ///< number of spectrum coefficients to read
83 };
84 
85 typedef struct TwinVQFrameData {
88 
89  uint8_t main_coeffs[1024];
91 
94 
97 
101 
105 
106 /**
107  * Parameters and tables that are different for every combination of
108  * bitrate/sample rate
109  */
110 typedef struct TwinVQModeTab {
111  struct TwinVQFrameMode fmode[3]; ///< frame type-dependent parameters
112 
113  uint16_t size; ///< frame size in samples
114  uint8_t n_lsp; ///< number of lsp coefficients
115  const float *lspcodebook;
116 
117  /* number of bits of the different LSP CB coefficients */
118  uint8_t lsp_bit0;
119  uint8_t lsp_bit1;
120  uint8_t lsp_bit2;
121 
122  uint8_t lsp_split; ///< number of CB entries for the LSP decoding
123  const int16_t *ppc_shape_cb; ///< PPC shape CB
124 
125  /** number of the bits for the PPC period value */
126  uint8_t ppc_period_bit;
127 
128  uint8_t ppc_shape_bit; ///< number of bits of the PPC shape CB coeffs
129  uint8_t ppc_shape_len; ///< size of PPC shape CB
130  uint8_t pgain_bit; ///< bits for PPC gain
131 
132  /** constant for peak period to peak width conversion */
133  uint16_t peak_per2wid;
134 } TwinVQModeTab;
135 
136 typedef struct TwinVQContext {
140 
142 
143  int is_6kbps;
144 
145  // history
146  float lsp_hist[2][20]; ///< LSP coefficients of the last frame
147  float bark_hist[3][2][40]; ///< BSE coefficients of last frame
148 
149  // bitstream parameters
150  int16_t permut[4][4096];
151  uint8_t length[4][2]; ///< main codebook stride
152  uint8_t length_change[4];
153  uint8_t bits_main_spec[2][4][2]; ///< bits for the main codebook
155  int n_div[4];
156 
157  float *spectrum;
158  float *curr_frame; ///< non-interleaved output
159  float *prev_frame; ///< non-interleaved previous frame
162 
163  float *cos_tabs[3];
164 
165  // scratch buffers
166  float *tmp_buf;
167 
170 
172 
174  const uint8_t *buf, int buf_size);
175  void (*dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in,
176  int use_hist, int ch, float *out, float gain,
177  enum TwinVQFrameType ftype);
178  void (*decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef,
179  const float *shape, float *speech);
180 } TwinVQContext;
181 
183 
184 /** @note not speed critical, hence not optimized */
185 static inline void twinvq_memset_float(float *buf, float val, int size)
186 {
187  while (size--)
188  *buf++ = val;
189 }
190 
191 static inline float twinvq_mulawinv(float y, float clip, float mu)
192 {
193  y = av_clipf(y / clip, -1, 1);
194  return clip * FFSIGN(y) * (exp(log(1 + mu) * fabs(y)) - 1) / mu;
195 }
196 
198  int *got_frame_ptr, AVPacket *avpkt);
200 /** Requires the caller to call ff_twinvq_decode_close() upon failure. */
202 
203 #endif /* AVCODEC_TWINVQ_H */
TwinVQModeTab::lspcodebook
const float * lspcodebook
Definition: twinvq.h:115
TwinVQContext::mtab
const TwinVQModeTab * mtab
Definition: twinvq.h:141
TwinVQFrameMode::sub
uint8_t sub
Number subblocks in each frame.
Definition: twinvq.h:66
out
FILE * out
Definition: movenc.c:54
TwinVQContext::bits_main_spec
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
Definition: twinvq.h:153
TwinVQFrameData::bark1
uint8_t bark1[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX][TWINVQ_BARK_N_COEF_MAX]
Definition: twinvq.h:95
TwinVQFrameData::lpc_idx2
uint8_t lpc_idx2[TWINVQ_CHANNELS_MAX][TWINVQ_LSP_SPLIT_MAX]
Definition: twinvq.h:99
TwinVQContext::tmp_buf
float * tmp_buf
Definition: twinvq.h:166
TwinVQContext::curr_frame
float * curr_frame
non-interleaved output
Definition: twinvq.h:158
TwinVQFrameData::bark_use_hist
uint8_t bark_use_hist[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX]
Definition: twinvq.h:96
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
TwinVQContext::bits
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
Definition: twinvq.h:169
TwinVQModeTab::ppc_shape_len
uint8_t ppc_shape_len
size of PPC shape CB
Definition: twinvq.h:129
TwinVQModeTab::lsp_bit1
uint8_t lsp_bit1
Definition: twinvq.h:119
TwinVQContext::permut
int16_t permut[4][4096]
Definition: twinvq.h:150
TwinVQModeTab::pgain_bit
uint8_t pgain_bit
bits for PPC gain
Definition: twinvq.h:130
TWINVQ_CODEC_VQF
@ TWINVQ_CODEC_VQF
Definition: twinvq.h:34
TwinVQFrameData::ppc_coeffs
uint8_t ppc_coeffs[TWINVQ_PPC_SHAPE_LEN_MAX]
Definition: twinvq.h:90
TwinVQFrameMode::cb0
const int16_t * cb0
main codebooks for spectrum data
Definition: twinvq.h:78
TwinVQContext::last_block_pos
int last_block_pos[2]
Definition: twinvq.h:160
TwinVQFrameMode::cb1
const int16_t * cb1
Definition: twinvq.h:79
TwinVQFrameMode::bark_n_coef
uint8_t bark_n_coef
number of BSE CB coefficients to read
Definition: twinvq.h:73
TwinVQFrameType
TwinVQFrameType
Definition: twinvq.h:38
FFSIGN
#define FFSIGN(a)
Definition: common.h:65
TwinVQFrameData::sub_gain_bits
uint8_t sub_gain_bits[TWINVQ_CHANNELS_MAX *TWINVQ_SUBBLOCKS_MAX]
Definition: twinvq.h:93
val
static double val(void *priv, double ch)
Definition: aeval.c:77
TwinVQModeTab::size
uint16_t size
frame size in samples
Definition: twinvq.h:113
twinvq_mulawinv
static float twinvq_mulawinv(float y, float clip, float mu)
Definition: twinvq.h:191
TwinVQContext::dec_bark_env
void(* dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum TwinVQFrameType ftype)
Definition: twinvq.h:175
TwinVQContext::n_div
int n_div[4]
Definition: twinvq.h:155
TwinVQContext::decode_ppc
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
Definition: twinvq.h:178
ftype
#define ftype
Definition: afir_template.c:39
TwinVQContext::fdsp
AVFloatDSPContext * fdsp
Definition: twinvq.h:138
TwinVQFrameData::g_coef
int g_coef[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:103
ff_twinvq_decode_init
int ff_twinvq_decode_init(AVCodecContext *avctx)
Requires the caller to call ff_twinvq_decode_close() upon failure.
Definition: twinvq.c:761
TwinVQContext::lsp_hist
float lsp_hist[2][20]
LSP coefficients of the last frame.
Definition: twinvq.h:146
clip
clip
Definition: af_crystalizer.c:122
TwinVQModeTab::ppc_shape_bit
uint8_t ppc_shape_bit
number of bits of the PPC shape CB coeffs
Definition: twinvq.h:128
TwinVQFrameData::lpc_idx1
uint8_t lpc_idx1[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:98
TWINVQ_BARK_N_COEF_MAX
#define TWINVQ_BARK_N_COEF_MAX
Definition: twinvq.h:58
TwinVQContext::codec
enum TwinVQCodec codec
Definition: twinvq.h:171
TwinVQContext::length_change
uint8_t length_change[4]
Definition: twinvq.h:152
TwinVQFrameData::lpc_hist_idx
uint8_t lpc_hist_idx[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:100
ff_twinvq_decode_frame
int ff_twinvq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: twinvq.c:478
TWINVQ_FT_MEDIUM
@ TWINVQ_FT_MEDIUM
Medium frame (divided in m<n sub-blocks)
Definition: twinvq.h:40
TwinVQFrameMode
Parameters and tables that are different for each frame type.
Definition: twinvq.h:65
TwinVQModeTab
Parameters and tables that are different for every combination of bitrate/sample rate.
Definition: twinvq.h:110
TwinVQModeTab::lsp_bit2
uint8_t lsp_bit2
Definition: twinvq.h:120
TwinVQFrameMode::bark_tab
const uint16_t * bark_tab
Definition: twinvq.h:67
twinvq_memset_float
static void twinvq_memset_float(float *buf, float val, int size)
Definition: twinvq.h:185
TwinVQContext::prev_frame
float * prev_frame
non-interleaved previous frame
Definition: twinvq.h:159
TWINVQ_FT_PPC
@ TWINVQ_FT_PPC
Periodic Peak Component (part of the long frame)
Definition: twinvq.h:42
TwinVQFrameMode::bark_cb
const int16_t * bark_cb
codebook for the bark scale envelope (BSE)
Definition: twinvq.h:72
TwinVQContext::length
uint8_t length[4][2]
main codebook stride
Definition: twinvq.h:151
TwinVQFrameData::gain_bits
uint8_t gain_bits[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:92
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
TwinVQContext::mdct_ctx
FFTContext mdct_ctx[3]
Definition: twinvq.h:139
av_clipf
av_clipf
Definition: af_crystalizer.c:122
TwinVQContext::is_6kbps
int is_6kbps
Definition: twinvq.h:143
exp
int8_t exp
Definition: eval.c:72
float_dsp.h
TwinVQFrameData::main_coeffs
uint8_t main_coeffs[1024]
Definition: twinvq.h:89
TwinVQContext::avctx
AVCodecContext * avctx
Definition: twinvq.h:137
TwinVQModeTab::lsp_bit0
uint8_t lsp_bit0
Definition: twinvq.h:118
TWINVQ_FT_LONG
@ TWINVQ_FT_LONG
Long frame (single sub-block + PPC)
Definition: twinvq.h:41
TwinVQFrameMode::bark_n_bit
uint8_t bark_n_bit
number of bits of the BSE coefs
Definition: twinvq.h:74
size
int size
Definition: twinvq_data.h:10344
TwinVQModeTab::ppc_period_bit
uint8_t ppc_period_bit
number of the bits for the PPC period value
Definition: twinvq.h:126
TwinVQContext::bits_main_spec_change
int bits_main_spec_change[4]
Definition: twinvq.h:154
TWINVQ_PPC_SHAPE_LEN_MAX
#define TWINVQ_PPC_SHAPE_LEN_MAX
Definition: twinvq.h:46
TWINVQ_CHANNELS_MAX
#define TWINVQ_CHANNELS_MAX
Definition: twinvq.h:56
TwinVQModeTab::ppc_shape_cb
const int16_t * ppc_shape_cb
PPC shape CB.
Definition: twinvq.h:123
AVFloatDSPContext
Definition: float_dsp.h:24
TwinVQModeTab::peak_per2wid
uint16_t peak_per2wid
constant for peak period to peak width conversion
Definition: twinvq.h:133
TwinVQContext::read_bitstream
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
Definition: twinvq.h:173
TwinVQContext::discarded_packets
int discarded_packets
Definition: twinvq.h:161
TWINVQ_CODEC_METASOUND
@ TWINVQ_CODEC_METASOUND
Definition: twinvq.h:35
FFTContext
Definition: fft.h:75
TwinVQContext::frame_size
int frame_size
Definition: twinvq.h:168
TwinVQContext::bark_hist
float bark_hist[3][2][40]
BSE coefficients of last frame.
Definition: twinvq.h:147
TwinVQFrameData
Definition: twinvq.h:85
common.h
TwinVQContext::cos_tabs
float * cos_tabs[3]
Definition: twinvq.h:163
ff_twinvq_decode_close
int ff_twinvq_decode_close(AVCodecContext *avctx)
Definition: twinvq.c:742
avcodec.h
TwinVQContext::cur_frame
int cur_frame
Definition: twinvq.h:168
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
TWINVQ_FT_SHORT
@ TWINVQ_FT_SHORT
Short frame (divided in n sub-blocks)
Definition: twinvq.h:39
fft.h
AVCodecContext
main external API structure.
Definition: avcodec.h:389
TwinVQFrameMode::cb_len_read
uint8_t cb_len_read
number of spectrum coefficients to read
Definition: twinvq.h:82
TwinVQFrameData::ftype
enum TwinVQFrameType ftype
Definition: twinvq.h:87
TwinVQContext::frames_per_packet
int frames_per_packet
Definition: twinvq.h:168
AVPacket
This structure stores compressed data.
Definition: packet.h:351
TWINVQ_SUBBLOCKS_MAX
#define TWINVQ_SUBBLOCKS_MAX
Definition: twinvq.h:57
TwinVQContext::spectrum
float * spectrum
Definition: twinvq.h:157
TwinVQFrameData::window_type
int window_type
Definition: twinvq.h:86
TWINVQ_MAX_FRAMES_PER_PACKET
#define TWINVQ_MAX_FRAMES_PER_PACKET
Definition: twinvq.h:60
ff_twinvq_wtype_to_ftype_table
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
Definition: twinvq.c:472
TwinVQFrameData::p_coef
int p_coef[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:102
TwinVQContext
Definition: twinvq.h:136
TWINVQ_LSP_SPLIT_MAX
#define TWINVQ_LSP_SPLIT_MAX
Definition: twinvq.h:55
int
int
Definition: ffmpeg_filter.c:153
TwinVQModeTab::lsp_split
uint8_t lsp_split
number of CB entries for the LSP decoding
Definition: twinvq.h:122
TwinVQModeTab::fmode
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
Definition: twinvq.h:111
TwinVQFrameMode::bark_env_size
uint8_t bark_env_size
number of distinct bark scale envelope values
Definition: twinvq.h:70
TwinVQModeTab::n_lsp
uint8_t n_lsp
number of lsp coefficients
Definition: twinvq.h:114
TwinVQCodec
TwinVQCodec
Definition: twinvq.h:33