FFmpeg
Loading...
Searching...
No Matches
metasound.c
Go to the documentation of this file.
1/*
2 * Voxware MetaSound decoder
3 * Copyright (c) 2013 Konstantin Shishkov
4 * based on TwinVQ decoder
5 * Copyright (c) 2009 Vitor Sessak
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <inttypes.h>
25#include <math.h>
26#include <stdint.h>
27
29
30#define BITSTREAM_READER_LE
31#include "avcodec.h"
32#include "codec_internal.h"
33#include "get_bits.h"
34
35#include "twinvq.h"
36#include "metasound_data.h"
37
38static void add_peak(float period, int width, const float *shape,
39 float ppc_gain, float *speech, int len)
40{
41 int i, j, center;
42 const float *shape_end = shape + len;
43
44 // First peak centered around zero
45 for (i = 0; i < width / 2; i++)
46 speech[i] += ppc_gain * *shape++;
47
48 for (i = 1; i < ROUNDED_DIV(len, width); i++) {
49 center = (int)(i * period + 0.5);
50 for (j = -width / 2; j < (width + 1) / 2; j++)
51 speech[j + center] += ppc_gain * *shape++;
52 }
53
54 // For the last block, be careful not to go beyond the end of the buffer
55 center = (int)(i * period + 0.5);
56 for (j = -width / 2; j < (width + 1) / 2 && shape < shape_end; j++)
57 speech[j + center] += ppc_gain * *shape++;
58}
59
60static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef,
61 const float *shape, float *speech)
62{
63 const TwinVQModeTab *mtab = tctx->mtab;
65 int isampf = tctx->avctx->sample_rate / 1000;
66 int ibps = tctx->avctx->bit_rate / (1000 * channels);
67 int width;
68
69 float ratio = (float)mtab->size / isampf;
70 float min_period, max_period, period_range, period;
71 float some_mult;
72
73 float pgain_base, pgain_step, ppc_gain;
74
75 if (channels == 1) {
76 min_period = log2(ratio * 0.2);
77 max_period = min_period + log2(6);
78 } else {
79 min_period = (int)(ratio * 0.2 * 400 + 0.5) / 400.0;
80 max_period = (int)(ratio * 0.2 * 400 * 6 + 0.5) / 400.0;
81 }
82 period_range = max_period - min_period;
83 period = min_period + period_coef * period_range /
84 ((1 << mtab->ppc_period_bit) - 1);
85 if (channels == 1)
86 period = powf(2.0, period);
87 else
88 period = (int)(period * 400 + 0.5) / 400.0;
89
90 switch (isampf) {
91 case 8: some_mult = 2.0; break;
92 case 11: some_mult = 3.0; break;
93 case 16: some_mult = 3.0; break;
94 case 22: some_mult = ibps == 32 ? 2.0 : 4.0; break;
95 case 44: some_mult = 8.0; break;
96 default: some_mult = 4.0;
97 }
98
99 width = (int)(some_mult / (mtab->size / period) * mtab->ppc_shape_len);
100 if (isampf == 22 && ibps == 32)
101 width = (int)((2.0 / period + 1) * width + 0.5);
102
103 pgain_base = channels == 2 ? 25000.0 : 20000.0;
104 pgain_step = pgain_base / ((1 << mtab->pgain_bit) - 1);
105 ppc_gain = 1.0 / 8192 *
106 twinvq_mulawinv(pgain_step * g_coef + pgain_step / 2,
107 pgain_base, TWINVQ_PGAIN_MU);
108
109 add_peak(period, width, shape, ppc_gain, speech, mtab->ppc_shape_len);
110}
111
112static void dec_bark_env(TwinVQContext *tctx, const uint8_t *in, int use_hist,
113 int ch, float *out, float gain,
115{
116 const TwinVQModeTab *mtab = tctx->mtab;
117 int i, j;
118 float *hist = tctx->bark_hist[ftype][ch];
119 float val = ((const float []) { 0.4, 0.35, 0.28 })[ftype];
120 int bark_n_coef = mtab->fmode[ftype].bark_n_coef;
121 int fw_cb_len = mtab->fmode[ftype].bark_env_size / bark_n_coef;
122 int idx = 0;
123 int channels = tctx->avctx->ch_layout.nb_channels;
124
125 if (channels == 1)
126 val = 0.5;
127 for (i = 0; i < fw_cb_len; i++)
128 for (j = 0; j < bark_n_coef; j++, idx++) {
129 float tmp2 = mtab->fmode[ftype].bark_cb[fw_cb_len * in[j] + i] *
130 (1.0 / 2048);
131 float st;
132
133 if (channels == 1)
134 st = use_hist ?
135 tmp2 + val * hist[idx] + 1.0 : tmp2 + 1.0;
136 else
137 st = use_hist ? (1.0 - val) * tmp2 + val * hist[idx] + 1.0
138 : tmp2 + 1.0;
139
140 hist[idx] = tmp2;
141 if (st < 0.1)
142 st = 0.1;
143
144 twinvq_memset_float(out, st * gain,
145 mtab->fmode[ftype].bark_tab[idx]);
146 out += mtab->fmode[ftype].bark_tab[idx];
147 }
148}
149
151 uint8_t *dst, enum TwinVQFrameType ftype)
152{
153 int i;
154
155 for (i = 0; i < tctx->n_div[ftype]; i++) {
156 int bs_second_part = (i >= tctx->bits_main_spec_change[ftype]);
157
158 *dst++ = get_bits(gb, tctx->bits_main_spec[0][ftype][bs_second_part]);
159 *dst++ = get_bits(gb, tctx->bits_main_spec[1][ftype][bs_second_part]);
160 }
161}
162
164 const uint8_t *buf, int buf_size)
165{
167 const TwinVQModeTab *mtab = tctx->mtab;
168 int channels = tctx->avctx->ch_layout.nb_channels;
169 int sub;
170 GetBitContext gb;
171 int i, j, k, ret;
172
173 if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
174 return ret;
175
176 for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
177 tctx->cur_frame++) {
178 bits = tctx->bits + tctx->cur_frame;
179
180 bits->window_type = get_bits(&gb, TWINVQ_WINDOW_TYPE_BITS);
181
182 if (bits->window_type > 8) {
183 av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\n");
184 return AVERROR_INVALIDDATA;
185 }
186
188
189 sub = mtab->fmode[bits->ftype].sub;
190
191 if (bits->ftype != TWINVQ_FT_SHORT && !tctx->is_6kbps)
192 get_bits(&gb, 2);
193
194 read_cb_data(tctx, &gb, bits->main_coeffs, bits->ftype);
195
196 for (i = 0; i < channels; i++)
197 for (j = 0; j < sub; j++)
198 for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)
199 bits->bark1[i][j][k] =
200 get_bits(&gb, mtab->fmode[bits->ftype].bark_n_bit);
201
202 for (i = 0; i < channels; i++)
203 for (j = 0; j < sub; j++)
204 bits->bark_use_hist[i][j] = get_bits1(&gb);
205
206 if (bits->ftype == TWINVQ_FT_LONG) {
207 for (i = 0; i < channels; i++)
208 bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
209 } else {
210 for (i = 0; i < channels; i++) {
211 bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
212 for (j = 0; j < sub; j++)
213 bits->sub_gain_bits[i * sub + j] =
215 }
216 }
217
218 for (i = 0; i < channels; i++) {
219 bits->lpc_hist_idx[i] = get_bits(&gb, mtab->lsp_bit0);
220 bits->lpc_idx1[i] = get_bits(&gb, mtab->lsp_bit1);
221
222 for (j = 0; j < mtab->lsp_split; j++)
223 bits->lpc_idx2[i][j] = get_bits(&gb, mtab->lsp_bit2);
224 }
225
226 if (bits->ftype == TWINVQ_FT_LONG) {
227 read_cb_data(tctx, &gb, bits->ppc_coeffs, 3);
228 for (i = 0; i < channels; i++) {
229 bits->p_coef[i] = get_bits(&gb, mtab->ppc_period_bit);
230 bits->g_coef[i] = get_bits(&gb, mtab->pgain_bit);
231 }
232 }
233
234 // subframes are aligned to nibbles
235 if (get_bits_count(&gb) & 3)
236 skip_bits(&gb, 4 - (get_bits_count(&gb) & 3));
237 }
238
239 return (get_bits_count(&gb) + 7) / 8;
240}
241
242typedef struct MetasoundProps {
243 uint32_t tag;
248
249static const MetasoundProps codec_props[] = {
250 { MKTAG('V','X','0','3'), 6, 1, 8000 },
251 { MKTAG('V','X','0','4'), 12, 2, 8000 },
252
253 { MKTAG('V','O','X','i'), 8, 1, 8000 },
254 { MKTAG('V','O','X','j'), 10, 1, 11025 },
255 { MKTAG('V','O','X','k'), 16, 1, 16000 },
256 { MKTAG('V','O','X','L'), 24, 1, 22050 },
257 { MKTAG('V','O','X','q'), 32, 1, 44100 },
258 { MKTAG('V','O','X','r'), 40, 1, 44100 },
259 { MKTAG('V','O','X','s'), 48, 1, 44100 },
260 { MKTAG('V','O','X','t'), 16, 2, 8000 },
261 { MKTAG('V','O','X','u'), 20, 2, 11025 },
262 { MKTAG('V','O','X','v'), 32, 2, 16000 },
263 { MKTAG('V','O','X','w'), 48, 2, 22050 },
264 { MKTAG('V','O','X','x'), 64, 2, 44100 },
265 { MKTAG('V','O','X','y'), 80, 2, 44100 },
266 { MKTAG('V','O','X','z'), 96, 2, 44100 },
267
268 { 0, 0, 0, 0 }
269};
270
272{
273 int isampf, ibps;
274 TwinVQContext *tctx = avctx->priv_data;
275 uint32_t tag;
276 const MetasoundProps *props = codec_props;
277 int channels;
278
279 if (!avctx->extradata || avctx->extradata_size < 16) {
280 av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\n");
281 return AVERROR_INVALIDDATA;
282 }
283
284 tag = AV_RL32(avctx->extradata + 12);
285
286 for (;;) {
287 if (!props->tag) {
288 av_log(avctx, AV_LOG_ERROR, "Could not find tag %08"PRIX32"\n", tag);
289 return AVERROR_INVALIDDATA;
290 }
291 if (props->tag == tag) {
292 avctx->sample_rate = props->sample_rate;
293 channels = props->channels;
294 avctx->bit_rate = props->bit_rate * 1000;
295 isampf = avctx->sample_rate / 1000;
296 break;
297 }
298 props++;
299 }
300
303
304 ibps = avctx->bit_rate / (1000 * channels);
305
306 switch ((channels << 16) + (isampf << 8) + ibps) {
307 case (1 << 16) + ( 8 << 8) + 6:
308 tctx->mtab = &metasound_mode0806;
309 break;
310 case (2 << 16) + ( 8 << 8) + 6:
311 tctx->mtab = &metasound_mode0806s;
312 break;
313 case (1 << 16) + ( 8 << 8) + 8:
314 tctx->mtab = &metasound_mode0808;
315 break;
316 case (2 << 16) + ( 8 << 8) + 8:
317 tctx->mtab = &metasound_mode0808s;
318 break;
319 case (1 << 16) + (11 << 8) + 10:
320 tctx->mtab = &metasound_mode1110;
321 break;
322 case (2 << 16) + (11 << 8) + 10:
323 tctx->mtab = &metasound_mode1110s;
324 break;
325 case (1 << 16) + (16 << 8) + 16:
326 tctx->mtab = &metasound_mode1616;
327 break;
328 case (2 << 16) + (16 << 8) + 16:
329 tctx->mtab = &metasound_mode1616s;
330 break;
331 case (1 << 16) + (22 << 8) + 24:
332 tctx->mtab = &metasound_mode2224;
333 break;
334 case (2 << 16) + (22 << 8) + 24:
335 tctx->mtab = &metasound_mode2224s;
336 break;
337 case (1 << 16) + (44 << 8) + 32:
338 case (2 << 16) + (44 << 8) + 32:
339 tctx->mtab = &metasound_mode4432;
340 break;
341 case (1 << 16) + (44 << 8) + 40:
342 case (2 << 16) + (44 << 8) + 40:
343 tctx->mtab = &metasound_mode4440;
344 break;
345 case (1 << 16) + (44 << 8) + 48:
346 case (2 << 16) + (44 << 8) + 48:
347 tctx->mtab = &metasound_mode4448;
348 break;
349 default:
350 av_log(avctx, AV_LOG_ERROR,
351 "This version does not support %d kHz - %d kbit/s/ch mode.\n",
352 isampf, ibps);
353 return AVERROR(ENOSYS);
354 }
355
359 tctx->decode_ppc = decode_ppc;
360 tctx->frame_size = avctx->bit_rate * tctx->mtab->size
361 / avctx->sample_rate;
362 tctx->is_6kbps = ibps == 6;
363
364 return ff_twinvq_decode_init(avctx);
365}
366
368 .p.name = "metasound",
369 CODEC_LONG_NAME("Voxware MetaSound"),
370 .p.type = AVMEDIA_TYPE_AUDIO,
371 .p.id = AV_CODEC_ID_METASOUND,
372 .priv_data_size = sizeof(TwinVQContext),
377 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
378};
#define ftype
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_metasound_decoder
Definition metasound.c:367
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define ROUNDED_DIV(a, b)
Definition common.h:58
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static const uint8_t bits[8]
Definition fastaudio.c:100
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_METASOUND
Definition codec_id.h:516
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL32(p)
#define av_cold
Definition attributes.h:117
#define log2(x)
Definition libm.h:406
#define powf(x, y)
Definition libm.h:52
#define MKTAG(a, b, c, d)
Definition macros.h:55
static void add_peak(float period, int width, const float *shape, float ppc_gain, float *speech, int len)
Definition metasound.c:38
static void read_cb_data(TwinVQContext *tctx, GetBitContext *gb, uint8_t *dst, enum TwinVQFrameType ftype)
Definition metasound.c:150
static void dec_bark_env(TwinVQContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum TwinVQFrameType ftype)
Definition metasound.c:112
static const MetasoundProps codec_props[]
Definition metasound.c:249
static int metasound_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx, const uint8_t *buf, int buf_size)
Definition metasound.c:163
static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
Definition metasound.c:60
static av_cold int metasound_decode_init(AVCodecContext *avctx)
Definition metasound.c:271
static const TwinVQModeTab metasound_mode0808s
static const TwinVQModeTab metasound_mode4448
static const TwinVQModeTab metasound_mode4440
static const TwinVQModeTab metasound_mode2224
static const TwinVQModeTab metasound_mode1616
static const TwinVQModeTab metasound_mode1616s
static const TwinVQModeTab metasound_mode1110s
static const TwinVQModeTab metasound_mode1110
static const TwinVQModeTab metasound_mode0808
static const TwinVQModeTab metasound_mode0806s
static const TwinVQModeTab metasound_mode2224s
static const TwinVQModeTab metasound_mode4432
static const TwinVQModeTab metasound_mode0806
uint32_t tag
Definition movenc.c:2073
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
uint32_t tag
Definition metasound.c:243
enum TwinVQCodec codec
Definition twinvq.h:173
float bark_hist[3][2][40]
BSE coefficients of last frame.
Definition twinvq.h:149
AVCodecContext * avctx
Definition twinvq.h:138
const TwinVQModeTab * mtab
Definition twinvq.h:143
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
Definition twinvq.h:175
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:177
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
Definition twinvq.h:171
int n_div[4]
Definition twinvq.h:157
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
Definition twinvq.h:180
int frame_size
Definition twinvq.h:170
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
Definition twinvq.h:155
int bits_main_spec_change[4]
Definition twinvq.h:156
int frames_per_packet
Definition twinvq.h:170
int cur_frame
Definition twinvq.h:170
int window_type
Definition twinvq.h:87
uint8_t bark_n_bit
number of bits of the BSE coefs
Definition twinvq.h:75
const int16_t * bark_cb
codebook for the bark scale envelope (BSE)
Definition twinvq.h:73
uint8_t bark_env_size
number of distinct bark scale envelope values
Definition twinvq.h:71
uint8_t bark_n_coef
number of BSE CB coefficients to read
Definition twinvq.h:74
uint8_t sub
Number subblocks in each frame.
Definition twinvq.h:67
const uint16_t * bark_tab
Definition twinvq.h:68
Parameters and tables that are different for every combination of bitrate/sample rate.
Definition twinvq.h:111
uint8_t ppc_shape_len
size of PPC shape CB
Definition twinvq.h:130
uint8_t lsp_split
number of CB entries for the LSP decoding
Definition twinvq.h:123
uint8_t lsp_bit2
Definition twinvq.h:121
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
Definition twinvq.h:112
uint8_t ppc_period_bit
number of the bits for the PPC period value
Definition twinvq.h:127
uint8_t lsp_bit0
Definition twinvq.h:119
uint16_t size
frame size in samples
Definition twinvq.h:114
uint8_t lsp_bit1
Definition twinvq.h:120
uint8_t pgain_bit
bits for PPC gain
Definition twinvq.h:131
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
#define width
Definition dsp.h:89
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
Definition twinvq.c:474
int ff_twinvq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition twinvq.c:480
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
Definition twinvq.c:745
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
Requires the caller to call ff_twinvq_decode_close() upon failure.
Definition twinvq.c:764
#define TWINVQ_PGAIN_MU
Definition twinvq.h:54
#define TWINVQ_SUB_GAIN_BITS
Definition twinvq.h:52
#define TWINVQ_WINDOW_TYPE_BITS
Definition twinvq.h:53
@ TWINVQ_CODEC_METASOUND
Definition twinvq.h:36
TwinVQFrameType
Definition twinvq.h:39
@ TWINVQ_FT_LONG
Long frame (single sub-block + PPC)
Definition twinvq.h:42
@ TWINVQ_FT_SHORT
Short frame (divided in n sub-blocks)
Definition twinvq.h:40
#define TWINVQ_GAIN_BITS
Definition twinvq.h:50
static float twinvq_mulawinv(float y, float clip, float mu)
Definition twinvq.h:200
static void twinvq_memset_float(float *buf, float val, int size)
Definition twinvq.h:194
int len