FFmpeg
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 #include "libavutil/float_dsp.h"
30 
31 #define BITSTREAM_READER_LE
32 #include "avcodec.h"
33 #include "fft.h"
34 #include "get_bits.h"
35 #include "internal.h"
36 #include "lsp.h"
37 #include "sinewin.h"
38 
39 #include "twinvq.h"
40 #include "metasound_data.h"
41 
42 static void add_peak(float period, int width, const float *shape,
43  float ppc_gain, float *speech, int len)
44 {
45  int i, j, center;
46  const float *shape_end = shape + len;
47 
48  // First peak centered around zero
49  for (i = 0; i < width / 2; i++)
50  speech[i] += ppc_gain * *shape++;
51 
52  for (i = 1; i < ROUNDED_DIV(len, width); i++) {
53  center = (int)(i * period + 0.5);
54  for (j = -width / 2; j < (width + 1) / 2; j++)
55  speech[j + center] += ppc_gain * *shape++;
56  }
57 
58  // For the last block, be careful not to go beyond the end of the buffer
59  center = (int)(i * period + 0.5);
60  for (j = -width / 2; j < (width + 1) / 2 && shape < shape_end; j++)
61  speech[j + center] += ppc_gain * *shape++;
62 }
63 
64 static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef,
65  const float *shape, float *speech)
66 {
67  const TwinVQModeTab *mtab = tctx->mtab;
68  int isampf = tctx->avctx->sample_rate / 1000;
69  int ibps = tctx->avctx->bit_rate / (1000 * tctx->avctx->channels);
70  int width;
71 
72  float ratio = (float)mtab->size / isampf;
73  float min_period, max_period, period_range, period;
74  float some_mult;
75 
76  float pgain_base, pgain_step, ppc_gain;
77 
78  if (tctx->avctx->channels == 1) {
79  min_period = log2(ratio * 0.2);
80  max_period = min_period + log2(6);
81  } else {
82  min_period = (int)(ratio * 0.2 * 400 + 0.5) / 400.0;
83  max_period = (int)(ratio * 0.2 * 400 * 6 + 0.5) / 400.0;
84  }
85  period_range = max_period - min_period;
86  period = min_period + period_coef * period_range /
87  ((1 << mtab->ppc_period_bit) - 1);
88  if (tctx->avctx->channels == 1)
89  period = powf(2.0, period);
90  else
91  period = (int)(period * 400 + 0.5) / 400.0;
92 
93  switch (isampf) {
94  case 8: some_mult = 2.0; break;
95  case 11: some_mult = 3.0; break;
96  case 16: some_mult = 3.0; break;
97  case 22: some_mult = ibps == 32 ? 2.0 : 4.0; break;
98  case 44: some_mult = 8.0; break;
99  default: some_mult = 4.0;
100  }
101 
102  width = (int)(some_mult / (mtab->size / period) * mtab->ppc_shape_len);
103  if (isampf == 22 && ibps == 32)
104  width = (int)((2.0 / period + 1) * width + 0.5);
105 
106  pgain_base = tctx->avctx->channels == 2 ? 25000.0 : 20000.0;
107  pgain_step = pgain_base / ((1 << mtab->pgain_bit) - 1);
108  ppc_gain = 1.0 / 8192 *
109  twinvq_mulawinv(pgain_step * g_coef + pgain_step / 2,
110  pgain_base, TWINVQ_PGAIN_MU);
111 
112  add_peak(period, width, shape, ppc_gain, speech, mtab->ppc_shape_len);
113 }
114 
115 static void dec_bark_env(TwinVQContext *tctx, const uint8_t *in, int use_hist,
116  int ch, float *out, float gain,
117  enum TwinVQFrameType ftype)
118 {
119  const TwinVQModeTab *mtab = tctx->mtab;
120  int i, j;
121  float *hist = tctx->bark_hist[ftype][ch];
122  float val = ((const float []) { 0.4, 0.35, 0.28 })[ftype];
123  int bark_n_coef = mtab->fmode[ftype].bark_n_coef;
124  int fw_cb_len = mtab->fmode[ftype].bark_env_size / bark_n_coef;
125  int idx = 0;
126 
127  if (tctx->avctx->channels == 1)
128  val = 0.5;
129  for (i = 0; i < fw_cb_len; i++)
130  for (j = 0; j < bark_n_coef; j++, idx++) {
131  float tmp2 = mtab->fmode[ftype].bark_cb[fw_cb_len * in[j] + i] *
132  (1.0 / 2048);
133  float st;
134 
135  if (tctx->avctx->channels == 1)
136  st = use_hist ?
137  tmp2 + val * hist[idx] + 1.0 : tmp2 + 1.0;
138  else
139  st = use_hist ? (1.0 - val) * tmp2 + val * hist[idx] + 1.0
140  : tmp2 + 1.0;
141 
142  hist[idx] = tmp2;
143  if (st < 0.1)
144  st = 0.1;
145 
146  twinvq_memset_float(out, st * gain,
147  mtab->fmode[ftype].bark_tab[idx]);
148  out += mtab->fmode[ftype].bark_tab[idx];
149  }
150 }
151 
152 static void read_cb_data(TwinVQContext *tctx, GetBitContext *gb,
153  uint8_t *dst, enum TwinVQFrameType ftype)
154 {
155  int i;
156 
157  for (i = 0; i < tctx->n_div[ftype]; i++) {
158  int bs_second_part = (i >= tctx->bits_main_spec_change[ftype]);
159 
160  *dst++ = get_bits(gb, tctx->bits_main_spec[0][ftype][bs_second_part]);
161  *dst++ = get_bits(gb, tctx->bits_main_spec[1][ftype][bs_second_part]);
162  }
163 }
164 
166  const uint8_t *buf, int buf_size)
167 {
169  const TwinVQModeTab *mtab = tctx->mtab;
170  int channels = tctx->avctx->channels;
171  int sub;
172  GetBitContext gb;
173  int i, j, k, ret;
174 
175  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
176  return ret;
177 
178  for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
179  tctx->cur_frame++) {
180  bits = tctx->bits + tctx->cur_frame;
181 
182  bits->window_type = get_bits(&gb, TWINVQ_WINDOW_TYPE_BITS);
183 
184  if (bits->window_type > 8) {
185  av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\n");
186  return AVERROR_INVALIDDATA;
187  }
188 
190 
191  sub = mtab->fmode[bits->ftype].sub;
192 
193  if (bits->ftype != TWINVQ_FT_SHORT && !tctx->is_6kbps)
194  get_bits(&gb, 2);
195 
196  read_cb_data(tctx, &gb, bits->main_coeffs, bits->ftype);
197 
198  for (i = 0; i < channels; i++)
199  for (j = 0; j < sub; j++)
200  for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)
201  bits->bark1[i][j][k] =
202  get_bits(&gb, mtab->fmode[bits->ftype].bark_n_bit);
203 
204  for (i = 0; i < channels; i++)
205  for (j = 0; j < sub; j++)
206  bits->bark_use_hist[i][j] = get_bits1(&gb);
207 
208  if (bits->ftype == TWINVQ_FT_LONG) {
209  for (i = 0; i < channels; i++)
210  bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
211  } else {
212  for (i = 0; i < channels; i++) {
213  bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
214  for (j = 0; j < sub; j++)
215  bits->sub_gain_bits[i * sub + j] =
217  }
218  }
219 
220  for (i = 0; i < channels; i++) {
221  bits->lpc_hist_idx[i] = get_bits(&gb, mtab->lsp_bit0);
222  bits->lpc_idx1[i] = get_bits(&gb, mtab->lsp_bit1);
223 
224  for (j = 0; j < mtab->lsp_split; j++)
225  bits->lpc_idx2[i][j] = get_bits(&gb, mtab->lsp_bit2);
226  }
227 
228  if (bits->ftype == TWINVQ_FT_LONG) {
229  read_cb_data(tctx, &gb, bits->ppc_coeffs, 3);
230  for (i = 0; i < channels; i++) {
231  bits->p_coef[i] = get_bits(&gb, mtab->ppc_period_bit);
232  bits->g_coef[i] = get_bits(&gb, mtab->pgain_bit);
233  }
234  }
235 
236  // subframes are aligned to nibbles
237  if (get_bits_count(&gb) & 3)
238  skip_bits(&gb, 4 - (get_bits_count(&gb) & 3));
239  }
240 
241  return (get_bits_count(&gb) + 7) / 8;
242 }
243 
244 typedef struct MetasoundProps {
245  uint32_t tag;
246  int bit_rate;
247  int channels;
250 
251 static const MetasoundProps codec_props[] = {
252  { MKTAG('V','X','0','3'), 6, 1, 8000 },
253  { MKTAG('V','X','0','4'), 12, 2, 8000 },
254 
255  { MKTAG('V','O','X','i'), 8, 1, 8000 },
256  { MKTAG('V','O','X','j'), 10, 1, 11025 },
257  { MKTAG('V','O','X','k'), 16, 1, 16000 },
258  { MKTAG('V','O','X','L'), 24, 1, 22050 },
259  { MKTAG('V','O','X','q'), 32, 1, 44100 },
260  { MKTAG('V','O','X','r'), 40, 1, 44100 },
261  { MKTAG('V','O','X','s'), 48, 1, 44100 },
262  { MKTAG('V','O','X','t'), 16, 2, 8000 },
263  { MKTAG('V','O','X','u'), 20, 2, 11025 },
264  { MKTAG('V','O','X','v'), 32, 2, 16000 },
265  { MKTAG('V','O','X','w'), 48, 2, 22050 },
266  { MKTAG('V','O','X','x'), 64, 2, 44100 },
267  { MKTAG('V','O','X','y'), 80, 2, 44100 },
268  { MKTAG('V','O','X','z'), 96, 2, 44100 },
269 
270  { 0, 0, 0, 0 }
271 };
272 
274 {
275  int isampf, ibps;
276  TwinVQContext *tctx = avctx->priv_data;
277  uint32_t tag;
278  const MetasoundProps *props = codec_props;
279 
280  if (!avctx->extradata || avctx->extradata_size < 16) {
281  av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\n");
282  return AVERROR_INVALIDDATA;
283  }
284 
285  tag = AV_RL32(avctx->extradata + 12);
286 
287  for (;;) {
288  if (!props->tag) {
289  av_log(avctx, AV_LOG_ERROR, "Could not find tag %08"PRIX32"\n", tag);
290  return AVERROR_INVALIDDATA;
291  }
292  if (props->tag == tag) {
293  avctx->sample_rate = props->sample_rate;
294  avctx->channels = props->channels;
295  avctx->bit_rate = props->bit_rate * 1000;
296  isampf = avctx->sample_rate / 1000;
297  break;
298  }
299  props++;
300  }
301 
302  if (avctx->channels <= 0 || avctx->channels > TWINVQ_CHANNELS_MAX) {
303  av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
304  avctx->channels);
305  return AVERROR_INVALIDDATA;
306  }
307  avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
309 
310  ibps = avctx->bit_rate / (1000 * avctx->channels);
311 
312  switch ((avctx->channels << 16) + (isampf << 8) + ibps) {
313  case (1 << 16) + ( 8 << 8) + 6:
314  tctx->mtab = &ff_metasound_mode0806;
315  break;
316  case (2 << 16) + ( 8 << 8) + 6:
317  tctx->mtab = &ff_metasound_mode0806s;
318  break;
319  case (1 << 16) + ( 8 << 8) + 8:
320  tctx->mtab = &ff_metasound_mode0808;
321  break;
322  case (2 << 16) + ( 8 << 8) + 8:
323  tctx->mtab = &ff_metasound_mode0808s;
324  break;
325  case (1 << 16) + (11 << 8) + 10:
326  tctx->mtab = &ff_metasound_mode1110;
327  break;
328  case (2 << 16) + (11 << 8) + 10:
329  tctx->mtab = &ff_metasound_mode1110s;
330  break;
331  case (1 << 16) + (16 << 8) + 16:
332  tctx->mtab = &ff_metasound_mode1616;
333  break;
334  case (2 << 16) + (16 << 8) + 16:
335  tctx->mtab = &ff_metasound_mode1616s;
336  break;
337  case (1 << 16) + (22 << 8) + 24:
338  tctx->mtab = &ff_metasound_mode2224;
339  break;
340  case (2 << 16) + (22 << 8) + 24:
341  tctx->mtab = &ff_metasound_mode2224s;
342  break;
343  case (1 << 16) + (44 << 8) + 32:
344  tctx->mtab = &ff_metasound_mode4432;
345  break;
346  case (2 << 16) + (44 << 8) + 32:
347  tctx->mtab = &ff_metasound_mode4432s;
348  break;
349  case (1 << 16) + (44 << 8) + 40:
350  tctx->mtab = &ff_metasound_mode4440;
351  break;
352  case (2 << 16) + (44 << 8) + 40:
353  tctx->mtab = &ff_metasound_mode4440s;
354  break;
355  case (1 << 16) + (44 << 8) + 48:
356  tctx->mtab = &ff_metasound_mode4448;
357  break;
358  case (2 << 16) + (44 << 8) + 48:
359  tctx->mtab = &ff_metasound_mode4448s;
360  break;
361  default:
362  av_log(avctx, AV_LOG_ERROR,
363  "This version does not support %d kHz - %d kbit/s/ch mode.\n",
364  isampf, ibps);
365  return AVERROR(ENOSYS);
366  }
367 
370  tctx->dec_bark_env = dec_bark_env;
371  tctx->decode_ppc = decode_ppc;
372  tctx->frame_size = avctx->bit_rate * tctx->mtab->size
373  / avctx->sample_rate;
374  tctx->is_6kbps = ibps == 6;
375 
376  return ff_twinvq_decode_init(avctx);
377 }
378 
380  .name = "metasound",
381  .long_name = NULL_IF_CONFIG_SMALL("Voxware MetaSound"),
382  .type = AVMEDIA_TYPE_AUDIO,
383  .id = AV_CODEC_ID_METASOUND,
384  .priv_data_size = sizeof(TwinVQContext),
386  .close = ff_twinvq_decode_close,
388  .capabilities = AV_CODEC_CAP_DR1,
389  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
391 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
MetasoundProps::channels
int channels
Definition: metasound.c:247
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
TwinVQContext::mtab
const TwinVQModeTab * mtab
Definition: twinvq.h:142
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_metasound_mode4440s
const TwinVQModeTab ff_metasound_mode4440s
Definition: metasound_data.c:15135
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2276
TwinVQFrameMode::sub
uint8_t sub
Number subblocks in each frame.
Definition: twinvq.h:67
out
FILE * out
Definition: movenc.c:54
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2225
TwinVQContext::bits_main_spec
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
Definition: twinvq.h:154
codec_props
static const MetasoundProps codec_props[]
Definition: metasound.c:251
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:686
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
MetasoundProps::tag
uint32_t tag
Definition: metasound.c:245
TWINVQ_GAIN_BITS
#define TWINVQ_GAIN_BITS
Definition: twinvq.h:50
TwinVQContext::bits
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
Definition: twinvq.h:170
internal.h
ff_metasound_mode2224s
const TwinVQModeTab ff_metasound_mode2224s
Definition: metasound_data.c:15099
ff_metasound_mode0806s
const TwinVQModeTab ff_metasound_mode0806s
Definition: metasound_data.c:15027
TwinVQModeTab::ppc_shape_len
uint8_t ppc_shape_len
size of PPC shape CB
Definition: twinvq.h:130
TwinVQModeTab::lsp_bit1
uint8_t lsp_bit1
Definition: twinvq.h:120
ff_twinvq_decode_init
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
Definition: twinvq.c:770
channels
channels
Definition: aptx.c:30
TwinVQModeTab::pgain_bit
uint8_t pgain_bit
bits for PPC gain
Definition: twinvq.h:131
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
TWINVQ_WINDOW_TYPE_BITS
#define TWINVQ_WINDOW_TYPE_BITS
Definition: twinvq.h:53
TwinVQFrameMode::bark_n_coef
uint8_t bark_n_coef
number of BSE CB coefficients to read
Definition: twinvq.h:74
TwinVQFrameType
TwinVQFrameType
Definition: twinvq.h:39
read_cb_data
static void read_cb_data(TwinVQContext *tctx, GetBitContext *gb, uint8_t *dst, enum TwinVQFrameType ftype)
Definition: metasound.c:152
GetBitContext
Definition: get_bits.h:61
TWINVQ_PGAIN_MU
#define TWINVQ_PGAIN_MU
Definition: twinvq.h:54
TwinVQModeTab::size
uint16_t size
frame size in samples
Definition: twinvq.h:114
ff_metasound_mode4448s
const TwinVQModeTab ff_metasound_mode4448s
Definition: metasound_data.c:15153
twinvq_mulawinv
static float twinvq_mulawinv(float y, float clip, float mu)
Definition: twinvq.h:192
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
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:176
ff_twinvq_decode_frame
int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: twinvq.c:476
TwinVQContext::n_div
int n_div[4]
Definition: twinvq.h:156
TwinVQContext::decode_ppc
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
Definition: twinvq.h:179
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
av_cold
#define av_cold
Definition: attributes.h:84
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
ff_metasound_mode1110
const TwinVQModeTab ff_metasound_mode1110
Definition: metasound_data.c:15054
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:1667
width
#define width
ff_metasound_mode1110s
const TwinVQModeTab ff_metasound_mode1110s
Definition: metasound_data.c:15063
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
TwinVQContext::codec
enum TwinVQCodec codec
Definition: twinvq.h:172
bits
uint8_t bits
Definition: vp3data.h:202
TwinVQModeTab
Parameters and tables that are different for every combination of bitrate/sample rate.
Definition: twinvq.h:111
add_peak
static void add_peak(float period, int width, const float *shape, float ppc_gain, float *speech, int len)
Definition: metasound.c:42
ff_metasound_mode4432
const TwinVQModeTab ff_metasound_mode4432
Definition: metasound_data.c:15108
get_bits.h
TwinVQModeTab::lsp_bit2
uint8_t lsp_bit2
Definition: twinvq.h:121
TwinVQFrameMode::bark_tab
const uint16_t * bark_tab
Definition: twinvq.h:68
twinvq_memset_float
static void twinvq_memset_float(float *buf, float val, int size)
Definition: twinvq.h:186
if
if(ret)
Definition: filter_design.txt:179
TwinVQFrameMode::bark_cb
const int16_t * bark_cb
codebook for the bark scale envelope (BSE)
Definition: twinvq.h:73
ff_twinvq_decode_close
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
Definition: twinvq.c:751
ff_metasound_mode0808
const TwinVQModeTab ff_metasound_mode0808
Definition: metasound_data.c:15036
period
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
Definition: writing_filters.txt:89
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1615
ROUNDED_DIV
#define ROUNDED_DIV(a, b)
Definition: common.h:56
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
MetasoundProps::sample_rate
int sample_rate
Definition: metasound.c:248
MetasoundProps
Definition: metasound.c:244
ff_metasound_mode1616s
const TwinVQModeTab ff_metasound_mode1616s
Definition: metasound_data.c:15081
TwinVQContext::is_6kbps
int is_6kbps
Definition: twinvq.h:144
float_dsp.h
TwinVQContext::avctx
AVCodecContext * avctx
Definition: twinvq.h:138
TwinVQModeTab::lsp_bit0
uint8_t lsp_bit0
Definition: twinvq.h:119
TWINVQ_FT_LONG
@ TWINVQ_FT_LONG
Long frame (single sub-block + PPC)
Definition: twinvq.h:42
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
powf
#define powf(x, y)
Definition: libm.h:50
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
TwinVQFrameMode::bark_n_bit
uint8_t bark_n_bit
number of bits of the BSE coefs
Definition: twinvq.h:75
TwinVQModeTab::ppc_period_bit
uint8_t ppc_period_bit
number of the bits for the PPC period value
Definition: twinvq.h:127
TwinVQContext::bits_main_spec_change
int bits_main_spec_change[4]
Definition: twinvq.h:155
ff_metasound_mode4440
const TwinVQModeTab ff_metasound_mode4440
Definition: metasound_data.c:15126
TWINVQ_CHANNELS_MAX
#define TWINVQ_CHANNELS_MAX
Definition: twinvq.h:57
val
const char const char void * val
Definition: avisynth_c.h:863
ff_metasound_mode1616
const TwinVQModeTab ff_metasound_mode1616
Definition: metasound_data.c:15072
ff_metasound_mode4432s
const TwinVQModeTab ff_metasound_mode4432s
Definition: metasound_data.c:15117
sinewin.h
ff_metasound_mode0806
const TwinVQModeTab ff_metasound_mode0806
Definition: metasound_data.c:15018
TwinVQContext::read_bitstream
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
Definition: twinvq.h:174
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
ff_metasound_mode0808s
const TwinVQModeTab ff_metasound_mode0808s
Definition: metasound_data.c:15045
TWINVQ_SUB_GAIN_BITS
#define TWINVQ_SUB_GAIN_BITS
Definition: twinvq.h:52
metasound_data.h
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
TWINVQ_CODEC_METASOUND
@ TWINVQ_CODEC_METASOUND
Definition: twinvq.h:36
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
TwinVQContext::frame_size
int frame_size
Definition: twinvq.h:169
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
ff_metasound_mode4448
const TwinVQModeTab ff_metasound_mode4448
Definition: metasound_data.c:15144
TwinVQContext::bark_hist
float bark_hist[3][2][40]
BSE coefficients of last frame.
Definition: twinvq.h:148
TwinVQFrameData
Definition: twinvq.h:86
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint8_t
uint8_t
Definition: audio_convert.c:194
ff_metasound_mode2224
const TwinVQModeTab ff_metasound_mode2224
Definition: metasound_data.c:15090
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
metasound_decode_init
static av_cold int metasound_decode_init(AVCodecContext *avctx)
Definition: metasound.c:273
len
int len
Definition: vorbis_enc_data.h:452
AV_CODEC_ID_METASOUND
@ AV_CODEC_ID_METASOUND
Definition: avcodec.h:627
log2
#define log2(x)
Definition: libm.h:404
avcodec.h
tag
uint32_t tag
Definition: movenc.c:1496
ret
ret
Definition: filter_design.txt:187
TwinVQContext::cur_frame
int cur_frame
Definition: twinvq.h:169
ff_metasound_decoder
AVCodec ff_metasound_decoder
Definition: metasound.c:379
lsp.h
decode_ppc
static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
Definition: metasound.c:64
TWINVQ_FT_SHORT
@ TWINVQ_FT_SHORT
Short frame (divided in n sub-blocks)
Definition: twinvq.h:40
twinvq.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
fft.h
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
channel_layout.h
TwinVQContext::frames_per_packet
int frames_per_packet
Definition: twinvq.h:169
metasound_read_bitstream
static int metasound_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx, const uint8_t *buf, int buf_size)
Definition: metasound.c:165
MetasoundProps::bit_rate
int bit_rate
Definition: metasound.c:246
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
TwinVQFrameData::window_type
int window_type
Definition: twinvq.h:87
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
dec_bark_env
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:115
TwinVQContext
Definition: twinvq.h:137
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int
int
Definition: ffmpeg_filter.c:191
TwinVQModeTab::lsp_split
uint8_t lsp_split
number of CB entries for the LSP decoding
Definition: twinvq.h:123
ff_twinvq_wtype_to_ftype_table
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
Definition: twinvq.c:470
TwinVQModeTab::fmode
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
Definition: twinvq.h:112
TwinVQFrameMode::bark_env_size
uint8_t bark_env_size
number of distinct bark scale envelope values
Definition: twinvq.h:71