Libav
twinvq.c
Go to the documentation of this file.
1 /*
2  * TwinVQ decoder
3  * Copyright (c) 2009 Vitor Sessak
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/float_dsp.h"
27 #include "avcodec.h"
28 #include "fft.h"
29 #include "internal.h"
30 #include "lsp.h"
31 #include "sinewin.h"
32 #include "twinvq.h"
33 
46 static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
47 {
48  int j;
49  float p = 0.5f;
50  float q = 0.5f;
51  float two_cos_w = 2.0f * cos_val;
52 
53  for (j = 0; j + 1 < order; j += 2 * 2) {
54  // Unroll the loop once since order is a multiple of four
55  q *= lsp[j] - two_cos_w;
56  p *= lsp[j + 1] - two_cos_w;
57 
58  q *= lsp[j + 2] - two_cos_w;
59  p *= lsp[j + 3] - two_cos_w;
60  }
61 
62  p *= p * (2.0f - two_cos_w);
63  q *= q * (2.0f + two_cos_w);
64 
65  return 0.5 / (p + q);
66 }
67 
71 static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
72 {
73  int i;
74  const TwinVQModeTab *mtab = tctx->mtab;
75  int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
76 
77  for (i = 0; i < size_s / 2; i++) {
78  float cos_i = tctx->cos_tabs[0][i];
79  lpc[i] = eval_lpc_spectrum(cos_vals, cos_i, mtab->n_lsp);
80  lpc[size_s - i - 1] = eval_lpc_spectrum(cos_vals, -cos_i, mtab->n_lsp);
81  }
82 }
83 
84 static void interpolate(float *out, float v1, float v2, int size)
85 {
86  int i;
87  float step = (v1 - v2) / (size + 1);
88 
89  for (i = 0; i < size; i++) {
90  v2 += step;
91  out[i] = v2;
92  }
93 }
94 
95 static inline float get_cos(int idx, int part, const float *cos_tab, int size)
96 {
97  return part ? -cos_tab[size - idx - 1]
98  : cos_tab[idx];
99 }
100 
115 static inline void eval_lpcenv_or_interp(TwinVQContext *tctx,
116  enum TwinVQFrameType ftype,
117  float *out, const float *in,
118  int size, int step, int part)
119 {
120  int i;
121  const TwinVQModeTab *mtab = tctx->mtab;
122  const float *cos_tab = tctx->cos_tabs[ftype];
123 
124  // Fill the 's'
125  for (i = 0; i < size; i += step)
126  out[i] =
128  get_cos(i, part, cos_tab, size),
129  mtab->n_lsp);
130 
131  // Fill the 'iiiibiiii'
132  for (i = step; i <= size - 2 * step; i += step) {
133  if (out[i + step] + out[i - step] > 1.95 * out[i] ||
134  out[i + step] >= out[i - step]) {
135  interpolate(out + i - step + 1, out[i], out[i - step], step - 1);
136  } else {
137  out[i - step / 2] =
139  get_cos(i - step / 2, part, cos_tab, size),
140  mtab->n_lsp);
141  interpolate(out + i - step + 1, out[i - step / 2],
142  out[i - step], step / 2 - 1);
143  interpolate(out + i - step / 2 + 1, out[i],
144  out[i - step / 2], step / 2 - 1);
145  }
146  }
147 
148  interpolate(out + size - 2 * step + 1, out[size - step],
149  out[size - 2 * step], step - 1);
150 }
151 
152 static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype,
153  const float *buf, float *lpc,
154  int size, int step)
155 {
156  eval_lpcenv_or_interp(tctx, ftype, lpc, buf, size / 2, step, 0);
157  eval_lpcenv_or_interp(tctx, ftype, lpc + size / 2, buf, size / 2,
158  2 * step, 1);
159 
160  interpolate(lpc + size / 2 - step + 1, lpc[size / 2],
161  lpc[size / 2 - step], step);
162 
163  twinvq_memset_float(lpc + size - 2 * step + 1, lpc[size - 2 * step],
164  2 * step - 1);
165 }
166 
172 static void dequant(TwinVQContext *tctx, const uint8_t *cb_bits, float *out,
173  enum TwinVQFrameType ftype,
174  const int16_t *cb0, const int16_t *cb1, int cb_len)
175 {
176  int pos = 0;
177  int i, j;
178 
179  for (i = 0; i < tctx->n_div[ftype]; i++) {
180  int tmp0, tmp1;
181  int sign0 = 1;
182  int sign1 = 1;
183  const int16_t *tab0, *tab1;
184  int length = tctx->length[ftype][i >= tctx->length_change[ftype]];
185  int bitstream_second_part = (i >= tctx->bits_main_spec_change[ftype]);
186 
187  int bits = tctx->bits_main_spec[0][ftype][bitstream_second_part];
188  tmp0 = *cb_bits++;
189  if (bits == 7) {
190  if (tmp0 & 0x40)
191  sign0 = -1;
192  tmp0 &= 0x3F;
193  }
194 
195  bits = tctx->bits_main_spec[1][ftype][bitstream_second_part];
196  tmp1 = *cb_bits++;
197  if (bits == 7) {
198  if (tmp1 & 0x40)
199  sign1 = -1;
200  tmp1 &= 0x3F;
201  }
202 
203  tab0 = cb0 + tmp0 * cb_len;
204  tab1 = cb1 + tmp1 * cb_len;
205 
206  for (j = 0; j < length; j++)
207  out[tctx->permut[ftype][pos + j]] = sign0 * tab0[j] +
208  sign1 * tab1[j];
209 
210  pos += length;
211  }
212 }
213 
214 static void dec_gain(TwinVQContext *tctx,
215  enum TwinVQFrameType ftype, float *out)
216 {
217  const TwinVQModeTab *mtab = tctx->mtab;
218  const TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
219  int i, j;
220  int sub = mtab->fmode[ftype].sub;
221  float step = TWINVQ_AMP_MAX / ((1 << TWINVQ_GAIN_BITS) - 1);
222  float sub_step = TWINVQ_SUB_AMP_MAX / ((1 << TWINVQ_SUB_GAIN_BITS) - 1);
223 
224  if (ftype == TWINVQ_FT_LONG) {
225  for (i = 0; i < tctx->avctx->channels; i++)
226  out[i] = (1.0 / (1 << 13)) *
227  twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
229  } else {
230  for (i = 0; i < tctx->avctx->channels; i++) {
231  float val = (1.0 / (1 << 23)) *
232  twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
234 
235  for (j = 0; j < sub; j++)
236  out[i * sub + j] =
237  val * twinvq_mulawinv(sub_step * 0.5 +
238  sub_step * bits->sub_gain_bits[i * sub + j],
240  }
241  }
242 }
243 
250 static void rearrange_lsp(int order, float *lsp, float min_dist)
251 {
252  int i;
253  float min_dist2 = min_dist * 0.5;
254  for (i = 1; i < order; i++)
255  if (lsp[i] - lsp[i - 1] < min_dist) {
256  float avg = (lsp[i] + lsp[i - 1]) * 0.5;
257 
258  lsp[i - 1] = avg - min_dist2;
259  lsp[i] = avg + min_dist2;
260  }
261 }
262 
263 static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2,
264  int lpc_hist_idx, float *lsp, float *hist)
265 {
266  const TwinVQModeTab *mtab = tctx->mtab;
267  int i, j;
268 
269  const float *cb = mtab->lspcodebook;
270  const float *cb2 = cb + (1 << mtab->lsp_bit1) * mtab->n_lsp;
271  const float *cb3 = cb2 + (1 << mtab->lsp_bit2) * mtab->n_lsp;
272 
273  const int8_t funny_rounding[4] = {
274  -2,
275  mtab->lsp_split == 4 ? -2 : 1,
276  mtab->lsp_split == 4 ? -2 : 1,
277  0
278  };
279 
280  j = 0;
281  for (i = 0; i < mtab->lsp_split; i++) {
282  int chunk_end = ((i + 1) * mtab->n_lsp + funny_rounding[i]) /
283  mtab->lsp_split;
284  for (; j < chunk_end; j++)
285  lsp[j] = cb[lpc_idx1 * mtab->n_lsp + j] +
286  cb2[lpc_idx2[i] * mtab->n_lsp + j];
287  }
288 
289  rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
290 
291  for (i = 0; i < mtab->n_lsp; i++) {
292  float tmp1 = 1.0 - cb3[lpc_hist_idx * mtab->n_lsp + i];
293  float tmp2 = hist[i] * cb3[lpc_hist_idx * mtab->n_lsp + i];
294  hist[i] = lsp[i];
295  lsp[i] = lsp[i] * tmp1 + tmp2;
296  }
297 
298  rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
299  rearrange_lsp(mtab->n_lsp, lsp, 0.000095);
301 }
302 
303 static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp,
304  enum TwinVQFrameType ftype, float *lpc)
305 {
306  int i;
307  int size = tctx->mtab->size / tctx->mtab->fmode[ftype].sub;
308 
309  for (i = 0; i < tctx->mtab->n_lsp; i++)
310  lsp[i] = 2 * cos(lsp[i]);
311 
312  switch (ftype) {
313  case TWINVQ_FT_LONG:
314  eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 8);
315  break;
316  case TWINVQ_FT_MEDIUM:
317  eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 2);
318  break;
319  case TWINVQ_FT_SHORT:
320  eval_lpcenv(tctx, lsp, lpc);
321  break;
322  }
323 }
324 
325 static const uint8_t wtype_to_wsize[] = { 0, 0, 2, 2, 2, 1, 0, 1, 1 };
326 
327 static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype,
328  int wtype, float *in, float *prev, int ch)
329 {
330  FFTContext *mdct = &tctx->mdct_ctx[ftype];
331  const TwinVQModeTab *mtab = tctx->mtab;
332  int bsize = mtab->size / mtab->fmode[ftype].sub;
333  int size = mtab->size;
334  float *buf1 = tctx->tmp_buf;
335  int j, first_wsize, wsize; // Window size
336  float *out = tctx->curr_frame + 2 * ch * mtab->size;
337  float *out2 = out;
338  float *prev_buf;
339  int types_sizes[] = {
340  mtab->size / mtab->fmode[TWINVQ_FT_LONG].sub,
341  mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub,
342  mtab->size / (mtab->fmode[TWINVQ_FT_SHORT].sub * 2),
343  };
344 
345  wsize = types_sizes[wtype_to_wsize[wtype]];
346  first_wsize = wsize;
347  prev_buf = prev + (size - bsize) / 2;
348 
349  for (j = 0; j < mtab->fmode[ftype].sub; j++) {
350  int sub_wtype = ftype == TWINVQ_FT_MEDIUM ? 8 : wtype;
351 
352  if (!j && wtype == 4)
353  sub_wtype = 4;
354  else if (j == mtab->fmode[ftype].sub - 1 && wtype == 7)
355  sub_wtype = 7;
356 
357  wsize = types_sizes[wtype_to_wsize[sub_wtype]];
358 
359  mdct->imdct_half(mdct, buf1 + bsize * j, in + bsize * j);
360 
361  tctx->fdsp.vector_fmul_window(out2, prev_buf + (bsize - wsize) / 2,
362  buf1 + bsize * j,
363  ff_sine_windows[av_log2(wsize)],
364  wsize / 2);
365  out2 += wsize;
366 
367  memcpy(out2, buf1 + bsize * j + wsize / 2,
368  (bsize - wsize / 2) * sizeof(float));
369 
370  out2 += ftype == TWINVQ_FT_MEDIUM ? (bsize - wsize) / 2 : bsize - wsize;
371 
372  prev_buf = buf1 + bsize * j + bsize / 2;
373  }
374 
375  tctx->last_block_pos[ch] = (size + first_wsize) / 2;
376 }
377 
378 static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype,
379  int wtype, float **out, int offset)
380 {
381  const TwinVQModeTab *mtab = tctx->mtab;
382  float *prev_buf = tctx->prev_frame + tctx->last_block_pos[0];
383  int size1, size2, i;
384  float *out1, *out2;
385 
386  for (i = 0; i < tctx->avctx->channels; i++)
387  imdct_and_window(tctx, ftype, wtype,
388  tctx->spectrum + i * mtab->size,
389  prev_buf + 2 * i * mtab->size,
390  i);
391 
392  if (!out)
393  return;
394 
395  size2 = tctx->last_block_pos[0];
396  size1 = mtab->size - size2;
397 
398  out1 = &out[0][0] + offset;
399  memcpy(out1, prev_buf, size1 * sizeof(*out1));
400  memcpy(out1 + size1, tctx->curr_frame, size2 * sizeof(*out1));
401 
402  if (tctx->avctx->channels == 2) {
403  out2 = &out[1][0] + offset;
404  memcpy(out2, &prev_buf[2 * mtab->size],
405  size1 * sizeof(*out2));
406  memcpy(out2 + size1, &tctx->curr_frame[2 * mtab->size],
407  size2 * sizeof(*out2));
408  tctx->fdsp.butterflies_float(out1, out2, mtab->size);
409  }
410 }
411 
412 static void read_and_decode_spectrum(TwinVQContext *tctx, float *out,
413  enum TwinVQFrameType ftype)
414 {
415  const TwinVQModeTab *mtab = tctx->mtab;
416  TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
417  int channels = tctx->avctx->channels;
418  int sub = mtab->fmode[ftype].sub;
419  int block_size = mtab->size / sub;
421  float ppc_shape[TWINVQ_PPC_SHAPE_LEN_MAX * TWINVQ_CHANNELS_MAX * 4];
422 
423  int i, j;
424 
425  dequant(tctx, bits->main_coeffs, out, ftype,
426  mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
427  mtab->fmode[ftype].cb_len_read);
428 
429  dec_gain(tctx, ftype, gain);
430 
431  if (ftype == TWINVQ_FT_LONG) {
432  int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len * channels - 1) /
433  tctx->n_div[3];
434  dequant(tctx, bits->ppc_coeffs, ppc_shape,
436  mtab->ppc_shape_cb + cb_len_p * TWINVQ_PPC_SHAPE_CB_SIZE,
437  cb_len_p);
438  }
439 
440  for (i = 0; i < channels; i++) {
441  float *chunk = out + mtab->size * i;
442  float lsp[TWINVQ_LSP_COEFS_MAX];
443 
444  for (j = 0; j < sub; j++) {
445  tctx->dec_bark_env(tctx, bits->bark1[i][j],
446  bits->bark_use_hist[i][j], i,
447  tctx->tmp_buf, gain[sub * i + j], ftype);
448 
449  tctx->fdsp.vector_fmul(chunk + block_size * j,
450  chunk + block_size * j,
451  tctx->tmp_buf, block_size);
452  }
453 
454  if (ftype == TWINVQ_FT_LONG)
455  tctx->decode_ppc(tctx, bits->p_coef[i], bits->g_coef[i],
456  ppc_shape + i * mtab->ppc_shape_len, chunk);
457 
458  decode_lsp(tctx, bits->lpc_idx1[i], bits->lpc_idx2[i],
459  bits->lpc_hist_idx[i], lsp, tctx->lsp_hist[i]);
460 
461  dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
462 
463  for (j = 0; j < mtab->fmode[ftype].sub; j++) {
464  tctx->fdsp.vector_fmul(chunk, chunk, tctx->tmp_buf, block_size);
465  chunk += block_size;
466  }
467  }
468 }
469 
473  TWINVQ_FT_MEDIUM
474 };
475 
477  int *got_frame_ptr, AVPacket *avpkt)
478 {
479  AVFrame *frame = data;
480  const uint8_t *buf = avpkt->data;
481  int buf_size = avpkt->size;
482  TwinVQContext *tctx = avctx->priv_data;
483  const TwinVQModeTab *mtab = tctx->mtab;
484  float **out = NULL;
485  int ret;
486 
487  /* get output buffer */
488  if (tctx->discarded_packets >= 2) {
489  frame->nb_samples = mtab->size * tctx->frames_per_packet;
490  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
491  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
492  return ret;
493  }
494  out = (float **)frame->extended_data;
495  }
496 
497  if (buf_size < avctx->block_align) {
498  av_log(avctx, AV_LOG_ERROR,
499  "Frame too small (%d bytes). Truncated file?\n", buf_size);
500  return AVERROR(EINVAL);
501  }
502 
503  if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
504  return ret;
505 
506  for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
507  tctx->cur_frame++) {
509  tctx->bits[tctx->cur_frame].ftype);
510 
511  imdct_output(tctx, tctx->bits[tctx->cur_frame].ftype,
512  tctx->bits[tctx->cur_frame].window_type, out,
513  tctx->cur_frame * mtab->size);
514 
515  FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
516  }
517 
518  if (tctx->discarded_packets < 2) {
519  tctx->discarded_packets++;
520  *got_frame_ptr = 0;
521  return buf_size;
522  }
523 
524  *got_frame_ptr = 1;
525 
526  // VQF can deliver packets 1 byte greater than block align
527  if (buf_size == avctx->block_align + 1)
528  return buf_size;
529  return avctx->block_align;
530 }
531 
536 {
537  int i, j, ret;
538  const TwinVQModeTab *mtab = tctx->mtab;
539  int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
540  int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
541  int channels = tctx->avctx->channels;
542  float norm = channels == 1 ? 2.0 : 1.0;
543 
544  for (i = 0; i < 3; i++) {
545  int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
546  if ((ret = ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
547  -sqrt(norm / bsize) / (1 << 15))))
548  return ret;
549  }
550 
551  FF_ALLOC_OR_GOTO(tctx->avctx, tctx->tmp_buf,
552  mtab->size * sizeof(*tctx->tmp_buf), alloc_fail);
553 
554  FF_ALLOC_OR_GOTO(tctx->avctx, tctx->spectrum,
555  2 * mtab->size * channels * sizeof(*tctx->spectrum),
556  alloc_fail);
557  FF_ALLOC_OR_GOTO(tctx->avctx, tctx->curr_frame,
558  2 * mtab->size * channels * sizeof(*tctx->curr_frame),
559  alloc_fail);
560  FF_ALLOC_OR_GOTO(tctx->avctx, tctx->prev_frame,
561  2 * mtab->size * channels * sizeof(*tctx->prev_frame),
562  alloc_fail);
563 
564  for (i = 0; i < 3; i++) {
565  int m = 4 * mtab->size / mtab->fmode[i].sub;
566  double freq = 2 * M_PI / m;
567  FF_ALLOC_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
568  (m / 4) * sizeof(*tctx->cos_tabs[i]), alloc_fail);
569 
570  for (j = 0; j <= m / 8; j++)
571  tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
572  for (j = 1; j < m / 8; j++)
573  tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
574  }
575 
577  ff_init_ff_sine_windows(av_log2(size_s / 2));
579 
580  return 0;
581 
582 alloc_fail:
583  return AVERROR(ENOMEM);
584 }
585 
592 static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
593  int block_size,
594  const uint8_t line_len[2], int length_div,
595  enum TwinVQFrameType ftype)
596 {
597  int i, j;
598 
599  for (i = 0; i < line_len[0]; i++) {
600  int shift;
601 
602  if (num_blocks == 1 ||
603  (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
604  (ftype != TWINVQ_FT_LONG && num_vect & 1) ||
605  i == line_len[1]) {
606  shift = 0;
607  } else if (ftype == TWINVQ_FT_LONG) {
608  shift = i;
609  } else
610  shift = i * i;
611 
612  for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
613  tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
614  }
615 }
616 
632 static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
633  const uint8_t line_len[2], int length_div)
634 {
635  int i, j;
636  int cont = 0;
637 
638  for (i = 0; i < num_vect; i++)
639  for (j = 0; j < line_len[i >= length_div]; j++)
640  out[cont++] = in[j * num_vect + i];
641 }
642 
643 static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
644 {
645  int block_size = size / n_blocks;
646  int i;
647 
648  for (i = 0; i < size; i++)
649  out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
650 }
651 
653  enum TwinVQFrameType ftype)
654 {
655  int block_size, size;
656  const TwinVQModeTab *mtab = tctx->mtab;
657  int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
658 
659  if (ftype == TWINVQ_FT_PPC) {
660  size = tctx->avctx->channels;
661  block_size = mtab->ppc_shape_len;
662  } else {
663  size = tctx->avctx->channels * mtab->fmode[ftype].sub;
664  block_size = mtab->size / mtab->fmode[ftype].sub;
665  }
666 
667  permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
668  block_size, tctx->length[ftype],
669  tctx->length_change[ftype], ftype);
670 
671  transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
672  tctx->length[ftype], tctx->length_change[ftype]);
673 
674  linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
675  size * block_size);
676 }
677 
679 {
680  const TwinVQModeTab *mtab = tctx->mtab;
681  int n_ch = tctx->avctx->channels;
682  int total_fr_bits = tctx->avctx->bit_rate * mtab->size /
683  tctx->avctx->sample_rate;
684 
685  int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
686  mtab->lsp_split * mtab->lsp_bit2);
687 
688  int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
689  mtab->ppc_period_bit);
690 
691  int bsize_no_main_cb[3], bse_bits[3], i;
692  enum TwinVQFrameType frametype;
693 
694  for (i = 0; i < 3; i++)
695  // +1 for history usage switch
696  bse_bits[i] = n_ch *
697  (mtab->fmode[i].bark_n_coef *
698  mtab->fmode[i].bark_n_bit + 1);
699 
700  bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
702 
703  for (i = 0; i < 2; i++)
704  bsize_no_main_cb[i] =
705  lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
707  mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
708 
709  if (tctx->codec == TWINVQ_CODEC_METASOUND && !tctx->is_6kbps) {
710  bsize_no_main_cb[1] += 2;
711  bsize_no_main_cb[2] += 2;
712  }
713 
714  // The remaining bits are all used for the main spectrum coefficients
715  for (i = 0; i < 4; i++) {
716  int bit_size, vect_size;
717  int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
718  if (i == 3) {
719  bit_size = n_ch * mtab->ppc_shape_bit;
720  vect_size = n_ch * mtab->ppc_shape_len;
721  } else {
722  bit_size = total_fr_bits - bsize_no_main_cb[i];
723  vect_size = n_ch * mtab->size;
724  }
725 
726  tctx->n_div[i] = (bit_size + 13) / 14;
727 
728  rounded_up = (bit_size + tctx->n_div[i] - 1) /
729  tctx->n_div[i];
730  rounded_down = (bit_size) / tctx->n_div[i];
731  num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
732  num_rounded_up = tctx->n_div[i] - num_rounded_down;
733  tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;
734  tctx->bits_main_spec[1][i][0] = rounded_up / 2;
735  tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;
736  tctx->bits_main_spec[1][i][1] = rounded_down / 2;
737  tctx->bits_main_spec_change[i] = num_rounded_up;
738 
739  rounded_up = (vect_size + tctx->n_div[i] - 1) /
740  tctx->n_div[i];
741  rounded_down = (vect_size) / tctx->n_div[i];
742  num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
743  num_rounded_up = tctx->n_div[i] - num_rounded_down;
744  tctx->length[i][0] = rounded_up;
745  tctx->length[i][1] = rounded_down;
746  tctx->length_change[i] = num_rounded_up;
747  }
748 
749  for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
750  construct_perm_table(tctx, frametype);
751 }
752 
754 {
755  TwinVQContext *tctx = avctx->priv_data;
756  int i;
757 
758  for (i = 0; i < 3; i++) {
759  ff_mdct_end(&tctx->mdct_ctx[i]);
760  av_free(tctx->cos_tabs[i]);
761  }
762 
763  av_free(tctx->curr_frame);
764  av_free(tctx->spectrum);
765  av_free(tctx->prev_frame);
766  av_free(tctx->tmp_buf);
767 
768  return 0;
769 }
770 
772 {
773  int ret;
774  TwinVQContext *tctx = avctx->priv_data;
775 
776  tctx->avctx = avctx;
778 
779  if (!avctx->block_align) {
780  avctx->block_align = tctx->frame_size + 7 >> 3;
781  } else if (avctx->block_align * 8 < tctx->frame_size) {
782  av_log(avctx, AV_LOG_ERROR, "Block align is %d bits, expected %d\n",
783  avctx->block_align * 8, tctx->frame_size);
784  return AVERROR_INVALIDDATA;
785  }
786  tctx->frames_per_packet = avctx->block_align * 8 / tctx->frame_size;
788  av_log(avctx, AV_LOG_ERROR, "Too many frames per packet (%d)\n",
789  tctx->frames_per_packet);
790  return AVERROR_INVALIDDATA;
791  }
792 
794  if ((ret = init_mdct_win(tctx))) {
795  av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
796  ff_twinvq_decode_close(avctx);
797  return ret;
798  }
799  init_bitstream_params(tctx);
800 
801  twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
802  FF_ARRAY_ELEMS(tctx->bark_hist));
803 
804  return 0;
805 }
float, planar
Definition: samplefmt.h:72
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
uint8_t bark_n_bit
number of bits of the BSE coefs
Definition: twinvq.h:75
int size
static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
Definition: twinvq.c:643
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
uint8_t length_change[4]
Definition: twinvq.h:153
int discarded_packets
Definition: twinvq.h:162
int window_type
Definition: twinvq.h:87
uint8_t ppc_coeffs[TWINVQ_PPC_SHAPE_LEN_MAX]
Definition: twinvq.h:91
int bits_main_spec_change[4]
Definition: twinvq.h:155
const TwinVQModeTab * mtab
Definition: twinvq.h:142
TwinVQFrameData bits[TWINVQ_MAX_FRAMES_PER_PACKET]
Definition: twinvq.h:170
int p_coef[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:103
uint8_t cb_len_read
number of spectrum coefficients to read
Definition: twinvq.h:83
Medium frame (divided in m
Definition: twinvq.h:41
uint8_t bark_n_coef
number of BSE CB coefficients to read
Definition: twinvq.h:74
int size
Definition: avcodec.h:974
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
AVFloatDSPContext fdsp
Definition: twinvq.h:139
uint16_t size
frame size in samples
Definition: twinvq.h:114
uint8_t lsp_bit0
Definition: twinvq.h:119
uint8_t bark_use_hist[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX]
Definition: twinvq.h:97
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1828
uint8_t lpc_idx1[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:99
static const uint8_t wtype_to_wsize[]
Definition: twinvq.c:325
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
Definition: twinvq.c:470
uint8_t sub_gain_bits[TWINVQ_CHANNELS_MAX *TWINVQ_SUBBLOCKS_MAX]
Definition: twinvq.h:94
Short frame (divided in n sub-blocks)
Definition: twinvq.h:40
void(* decode_ppc)(struct TwinVQContext *tctx, int period_coef, int g_coef, const float *shape, float *speech)
Definition: twinvq.h:179
uint8_t lpc_idx2[TWINVQ_CHANNELS_MAX][TWINVQ_LSP_SPLIT_MAX]
Definition: twinvq.h:100
uint8_t bits
Definition: crc.c:251
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
const int16_t * ppc_shape_cb
PPC shape CB.
Definition: twinvq.h:124
int g_coef[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:104
uint8_t ppc_period_bit
number of the bits for the PPC period value
Definition: twinvq.h:127
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
Definition: twinvq.c:753
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
uint8_t gain_bits[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:93
#define TWINVQ_SUBBLOCKS_MAX
Definition: twinvq.h:58
#define TWINVQ_MULAW_MU
Definition: twinvq.h:49
static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float **out, int offset)
Definition: twinvq.c:378
#define TWINVQ_GAIN_BITS
Definition: twinvq.h:50
TwinVQFrameType
Definition: twinvq.h:39
static void interpolate(float *out, float v1, float v2, int size)
Definition: twinvq.c:84
const char data[16]
Definition: mxf.c:70
int last_block_pos[2]
Definition: twinvq.h:161
#define TWINVQ_WINDOW_TYPE_BITS
Definition: twinvq.h:53
Parameters and tables that are different for every combination of bitrate/sample rate.
Definition: twinvq.h:111
uint8_t lpc_hist_idx[TWINVQ_CHANNELS_MAX]
Definition: twinvq.h:101
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:658
static void dequant(TwinVQContext *tctx, const uint8_t *cb_bits, float *out, enum TwinVQFrameType ftype, const int16_t *cb0, const int16_t *cb1, int cb_len)
Inverse quantization.
Definition: twinvq.c:172
int cur_frame
Definition: twinvq.h:169
Long frame (single sub-block + PPC)
Definition: twinvq.h:42
static float twinvq_mulawinv(float y, float clip, float mu)
Definition: twinvq.h:192
int frame_size
Definition: twinvq.h:169
static av_cold void init_bitstream_params(TwinVQContext *tctx)
Definition: twinvq.c:678
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:103
float * tmp_buf
Definition: twinvq.h:167
uint8_t main_coeffs[1024]
Definition: twinvq.h:90
#define TWINVQ_MAX_FRAMES_PER_PACKET
Definition: twinvq.h:61
float * cos_tabs[3]
Definition: twinvq.h:164
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
#define TWINVQ_PPC_SHAPE_CB_SIZE
Definition: twinvq.h:46
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2, int lpc_hist_idx, float *lsp, float *hist)
Definition: twinvq.c:263
float * spectrum
Definition: twinvq.h:158
static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype, const float *buf, float *lpc, int size, int step)
Definition: twinvq.c:152
#define ff_mdct_init
Definition: fft.h:151
Libavcodec external API header.
const int16_t * cb0
main codebooks for spectrum data
Definition: twinvq.h:79
static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp, enum TwinVQFrameType ftype, float *lpc)
Definition: twinvq.c:303
Definition: fft.h:73
int bit_rate
the average bitrate
Definition: avcodec.h:1114
uint8_t sub
Number subblocks in each frame.
Definition: twinvq.h:67
uint8_t bits_main_spec[2][4][2]
bits for the main codebook
Definition: twinvq.h:154
#define FF_ARRAY_ELEMS(a)
Definition: common.h:61
static void twinvq_memset_float(float *buf, float val, int size)
Definition: twinvq.h:186
uint8_t n_lsp
number of lsp coefficients
Definition: twinvq.h:115
int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: twinvq.c:476
static void rearrange_lsp(int order, float *lsp, float min_dist)
Rearrange the LSP coefficients so that they have a minimum distance of min_dist.
Definition: twinvq.c:250
int is_6kbps
Definition: twinvq.h:144
int n_div[4]
Definition: twinvq.h:156
if(ac->has_optimized_func)
uint8_t ppc_shape_bit
number of bits of the PPC shape CB coeffs
Definition: twinvq.h:129
int(* read_bitstream)(AVCodecContext *avctx, struct TwinVQContext *tctx, const uint8_t *buf, int buf_size)
Definition: twinvq.h:174
NULL
Definition: eval.c:55
#define av_cold
Definition: attributes.h:66
static const int16_t cos_tab[COS_TBL_SIZE]
Definition: g723_1_data.h:59
float bark_hist[3][2][40]
BSE coefficients of last frame.
Definition: twinvq.h:148
static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks, int block_size, const uint8_t line_len[2], int length_div, enum TwinVQFrameType ftype)
Interpret the data as if it were a num_blocks x line_len[0] matrix and for each line do a cyclic perm...
Definition: twinvq.c:592
static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
Definition: twinvq.c:71
int sample_rate
samples per second
Definition: avcodec.h:1791
Periodic Peak Component (part of the long frame)
Definition: twinvq.h:43
main external API structure.
Definition: avcodec.h:1050
enum TwinVQFrameType ftype
Definition: twinvq.h:88
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
int frames_per_packet
Definition: twinvq.h:169
const int16_t * tab1
Definition: mace.c:144
const int16_t * cb1
Definition: twinvq.h:80
struct TwinVQFrameMode fmode[3]
frame type-dependant parameters
Definition: twinvq.h:112
uint8_t * data
Definition: avcodec.h:973
int16_t permut[4][4096]
Definition: twinvq.h:151
SINETABLE_CONST float *const ff_sine_windows[14]
static void read_and_decode_spectrum(TwinVQContext *tctx, float *out, enum TwinVQFrameType ftype)
Definition: twinvq.c:412
#define TWINVQ_PPC_SHAPE_LEN_MAX
Definition: twinvq.h:47
float * prev_frame
non-interleaved previous frame
Definition: twinvq.h:160
static int step
Definition: avplay.c:247
uint8_t pgain_bit
bits for PPC gain
Definition: twinvq.h:131
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:93
static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
Evaluate a single LPC amplitude spectrum envelope coefficient from the line spectrum pairs...
Definition: twinvq.c:46
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
Definition: twinvq.c:327
void * priv_data
Definition: avcodec.h:1092
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
const float * lspcodebook
Definition: twinvq.h:116
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
uint8_t lsp_bit1
Definition: twinvq.h:120
enum TwinVQCodec codec
Definition: twinvq.h:172
static av_cold int init_mdct_win(TwinVQContext *tctx)
Init IMDCT and windowing tables.
Definition: twinvq.c:535
float lsp_hist[2][20]
LSP coefficients of the last frame.
Definition: twinvq.h:147
#define TWINVQ_LSP_COEFS_MAX
Definition: twinvq.h:55
#define FFSWAP(type, a, b)
Definition: common.h:60
common internal api header.
#define ff_mdct_end
Definition: fft.h:152
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:117
uint8_t ppc_shape_len
size of PPC shape CB
Definition: twinvq.h:130
#define TWINVQ_SUB_GAIN_BITS
Definition: twinvq.h:52
void ff_sort_nearly_sorted_floats(float *vals, int len)
Sort values in ascending order.
Definition: lsp.c:220
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
int channels
number of audio channels
Definition: avcodec.h:1792
#define av_log2
Definition: intmath.h:85
static void transpose_perm(int16_t *out, int16_t *in, int num_vect, const uint8_t line_len[2], int length_div)
Interpret the input data as in the following table:
Definition: twinvq.c:632
static const struct twinvq_data tab
static void eval_lpcenv_or_interp(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out, const float *in, int size, int step, int part)
Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
Definition: twinvq.c:115
static float get_cos(int idx, int part, const float *cos_tab, int size)
Definition: twinvq.c:95
void(* butterflies_float)(float *restrict v1, float *restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: float_dsp.h:148
FFTContext mdct_ctx[3]
Definition: twinvq.h:140
float * curr_frame
non-interleaved output
Definition: twinvq.h:159
#define TWINVQ_AMP_MAX
Definition: twinvq.h:51
static void dec_gain(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out)
Definition: twinvq.c:214
#define TWINVQ_CHANNELS_MAX
Definition: twinvq.h:57
uint8_t length[4][2]
main codebook stride
Definition: twinvq.h:152
This structure stores compressed data.
Definition: avcodec.h:950
static av_cold void construct_perm_table(TwinVQContext *tctx, enum TwinVQFrameType ftype)
Definition: twinvq.c:652
#define TWINVQ_SUB_AMP_MAX
Definition: twinvq.h:48
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
AVCodecContext * avctx
Definition: twinvq.h:138
uint8_t lsp_bit2
Definition: twinvq.h:121
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
uint8_t lsp_split
number of CB entries for the LSP decoding
Definition: twinvq.h:123
uint8_t bark1[TWINVQ_CHANNELS_MAX][TWINVQ_SUBBLOCKS_MAX][TWINVQ_BARK_N_COEF_MAX]
Definition: twinvq.h:96
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
Definition: twinvq.c:771