FFmpeg: libavcodec/twinvqdec.c Source File
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 #include <math.h>
23 #include <stdint.h>
24
31
33 {
37 },
38 512, 12,
ff_metasound_lsp8, 1, 5, 3, 3,
tab.
shape08, 8, 28, 20, 6, 40
39 };
40
42 {
46 },
47 512, 16,
ff_metasound_lsp11, 1, 6, 4, 3,
tab.
shape11, 9, 36, 30, 7, 90
48 };
49
51 {
55 },
56 512, 16,
ff_metasound_lsp11, 1, 6, 4, 3,
tab.
shape11, 9, 36, 30, 7, 90
57 };
58
60 {
64 },
65 1024, 16,
ff_metasound_lsp16, 1, 6, 4, 3,
tab.
shape16, 9, 56, 60, 7, 180
66 };
67
69 {
73 },
74 1024, 16,
ff_metasound_lsp22, 1, 6, 4, 3,
tab.
shape22_1, 9, 56, 36, 7, 144
75 };
76
78 {
82 },
83 1024, 16,
ff_metasound_lsp22, 1, 6, 4, 3,
tab.
shape22_1, 9, 56, 36, 7, 144
84 };
85
87 {
91 },
92 512, 16,
tab.
lsp22_2, 1, 6, 4, 4,
tab.
shape22_2, 9, 56, 36, 7, 72
93 };
94
96 {
100 },
101 2048, 20,
ff_metasound_lsp44, 1, 6, 4, 4,
tab.
shape44, 9, 84, 54, 7, 432
102 };
103
105 {
109 },
110 2048, 20,
ff_metasound_lsp44, 1, 6, 4, 4,
tab.
shape44, 9, 84, 54, 7, 432
111 };
112
113 /**
114 * Evaluate a * b / 400 rounded to the nearest integer. When, for example,
115 * a * b == 200 and the nearest integer is ill-defined, use a table to emulate
116 * the following broken float-based implementation used by the binary decoder:
117 *
118 * @code
119 * static int very_broken_op(int a, int b)
120 * {
121 * static float test; // Ugh, force gcc to do the division first...
122 *
123 * test = a / 400.0;
124 * return b * test + 0.5;
125 * }
126 * @endcode
127 *
128 * @note if this function is replaced by just ROUNDED_DIV(a * b, 400.0), the
129 * stddev between the original file (before encoding with Yamaha encoder) and
130 * the decoded output increases, which leads one to believe that the encoder
131 * expects exactly this broken calculation.
132 */
134 {
137 const uint8_t *rtab;
138
139 if (x % 400 ||
b % 5)
140 return x / 400;
141
142 x /= 400;
143
145 rtab =
tabs[
b / 5].tab;
147 }
148
149 /**
150 * Sum to data a periodic peak of a given period, width and shape.
151 *
152 * @param period the period of the peak divided by 400.0
153 */
155 float ppc_gain,
float *speech,
int len)
156 {
158
159 const float *shape_end = shape +
len;
160 int center;
161
162 // First peak centered around zero
164 speech[
i] += ppc_gain * *shape++;
165
168 for (j = -
width / 2; j < (
width + 1) / 2; j++)
169 speech[j + center] += ppc_gain * *shape++;
170 }
171
172 // For the last block, be careful not to go beyond the end of the buffer
174 for (j = -
width / 2; j < (
width + 1) / 2 && shape < shape_end; j++)
175 speech[j + center] += ppc_gain * *shape++;
176 }
177
179 const float *shape, float *speech)
180 {
186 int period_range = max_period - min_period;
187 float pgain_step = 25000.0 / ((1 << mtab->
pgain_bit) - 1);
188 float ppc_gain = 1.0 / 8192 *
190 pgain_step / 2,
192
193 // This is actually the period multiplied by 400. It is just linearly coded
194 // between its maximum and minimum value.
199
200 if (isampf == 22 && ibps == 32) {
201 // For some unknown reason, NTT decided to code this case differently...
204 } else
206
208 }
209
211 int ch,
float *
out,
float gain,
213 {
217 float val = ((
const float []) { 0.4, 0.35, 0.28 })[
ftype];
220 int idx = 0;
221
222 for (
i = 0;
i < fw_cb_len;
i++)
223 for (j = 0; j < bark_n_coef; j++, idx++) {
225 (1.0 / 4096);
226 float st = use_hist ? (1.0 -
val) * tmp2 +
val * hist[idx] + 1.0
227 : tmp2 + 1.0;
228
229 hist[idx] = tmp2;
230 if (st < -1.0)
231 st = 1.0;
232
235 }
236 }
237
240 {
242
245
248 }
249 }
250
252 const uint8_t *buf, int buf_size)
253 {
257 int sub;
260
264
266
267 if (
bits->window_type > 8) {
270 }
271
273
275
277
279 for (j = 0; j < sub; j++)
281 bits->bark1[
i][j][k] =
283
285 for (j = 0; j < sub; j++)
287
291 } else {
294 for (j = 0; j < sub; j++)
297 }
298 }
299
303
306 }
307
313 }
314 }
315
317 }
318
320 {
323
327 }
331
332 if (isampf < 8 || isampf > 44) {
335 }
336 switch (isampf) {
337 case 44:
339 break;
340 case 22:
342 break;
343 case 11:
345 break;
346 default:
348 break;
349 }
350
354 return -1;
355 }
358
360 if (ibps < 8 || ibps > 48) {
363 }
364
365 switch ((isampf << 8) + ibps) {
366 case (8 << 8) + 8:
368 break;
369 case (11 << 8) + 8:
371 break;
372 case (11 << 8) + 10:
374 break;
375 case (16 << 8) + 16:
377 break;
378 case (22 << 8) + 20:
380 break;
381 case (22 << 8) + 24:
383 break;
384 case (22 << 8) + 32:
386 break;
387 case (44 << 8) + 40:
389 break;
390 case (44 << 8) + 48:
392 break;
393 default:
395 "This version does not support %d kHz - %d kbit/s/ch mode.\n",
396 isampf, isampf);
397 return -1;
398 }
399
409 "VQF TwinVQ should have only one frame per packet\n");
411 }
412
414 }
415
428 };
static int twinvq_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx, const uint8_t *buf, int buf_size)
@ AV_SAMPLE_FMT_FLTP
float, planar
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
const TwinVQModeTab * mtab
uint8_t sub
Number subblocks in each frame.
static const uint16_t bark_tab_m22_256[]
int sample_rate
samples per second
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
static av_cold int twinvq_decode_init(AVCodecContext *avctx)
static int get_bits_count(const GetBitContext *s)
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
uint8_t ppc_shape_len
size of PPC shape CB
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
Requires the caller to call ff_twinvq_decode_close() upon failure.
uint8_t pgain_bit
bits for PPC gain
int nb_channels
Number of channels in this layout.
static void skip_bits(GetBitContext *s, int n)
static av_cold void close(AVCodecParserContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void add_peak(int period, int width, const float *shape, float ppc_gain, float *speech, int len)
Sum to data a periodic peak of a given period, width and shape.
#define TWINVQ_WINDOW_TYPE_BITS
AVCodec p
The public AVCodec.
AVChannelLayout ch_layout
Audio channel layout.
uint8_t bark_n_coef
number of BSE CB coefficients to read
static const TwinVQModeTab mode_16_16
static const TwinVQModeTab mode_11_08
static const struct twinvq_data tab
static const TwinVQModeTab mode_08_08
static const TwinVQModeTab mode_22_24
static double val(void *priv, double ch)
static const uint16_t bark_tab_l08_512[]
static int very_broken_op(int a, int b)
Evaluate a * b / 400 rounded to the nearest integer.
uint16_t size
frame size in samples
static float twinvq_mulawinv(float y, float clip, float mu)
void(* dec_bark_env)(struct TwinVQContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum TwinVQFrameType ftype)
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define FF_CODEC_DECODE_CB(func)
Parameters and tables that are different for every combination of bitrate/sample rate.
const uint16_t * bark_tab
static void twinvq_memset_float(float *buf, float val, int size)
#define CODEC_LONG_NAME(str)
const int16_t * bark_cb
codebook for the bark scale envelope (BSE)
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without period
int64_t bit_rate
the average bitrate
#define ROUNDED_DIV(a, b)
static unsigned int get_bits1(GetBitContext *s)
static const TwinVQModeTab mode_44_48
int ff_twinvq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
static const TwinVQModeTab mode_11_10
@ TWINVQ_FT_LONG
Long frame (single sub-block + PPC)
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
uint8_t bark_n_bit
number of bits of the BSE coefs
uint8_t ppc_period_bit
number of the bits for the PPC period value
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
int bits_main_spec_change[4]
#define TWINVQ_CHANNELS_MAX
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
#define CODEC_SAMPLEFMTS(...)
uint16_t peak_per2wid
constant for peak period to peak width conversion
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define TWINVQ_SUB_GAIN_BITS
static const uint16_t bark_tab_m08_256[]
#define i(width, name, range_min, range_max)
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
float bark_hist[3][2][40]
BSE coefficients of last frame.
static const struct @185 tabs[]
const FFCodec ff_twinvq_decoder
const char * name
Name of the codec implementation.
static const TwinVQModeTab mode_22_32
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
main external API structure.
static const uint16_t bark_tab_l22_512[]
static const TwinVQModeTab mode_22_20
static const TwinVQModeTab mode_44_40
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
static void dec_bark_env(TwinVQContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum TwinVQFrameType ftype)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void read_cb_data(TwinVQContext *tctx, GetBitContext *gb, uint8_t *dst, enum TwinVQFrameType ftype)
uint8_t lsp_split
number of CB entries for the LSP decoding
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
static const uint16_t bark_tab_s08_64[]
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
uint8_t bark_env_size
number of distinct bark scale envelope values
Generated on Tue Nov 18 2025 19:22:46 for FFmpeg by
doxygen
1.8.17