FFmpeg
Loading...
Searching...
No Matches
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 FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <math.h>
23#include <stdint.h>
24
26#include "libavutil/float_dsp.h"
27#include "libavutil/mem.h"
28#include "avcodec.h"
29#include "decode.h"
30#include "lsp.h"
32#include "sinewin.h"
33#include "twinvq.h"
34
35/**
36 * Evaluate a single LPC amplitude spectrum envelope coefficient from the line
37 * spectrum pairs.
38 *
39 * @param lsp a vector of the cosine of the LSP values
40 * @param cos_val cos(PI*i/N) where i is the index of the LPC amplitude
41 * @param order the order of the LSP (and the size of the *lsp buffer). Must
42 * be a multiple of four.
43 * @return the LPC value
44 *
45 * @todo reuse code from Vorbis decoder: vorbis_floor0_decode
46 */
47static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
48{
49 int j;
50 float p = 0.5f;
51 float q = 0.5f;
52 float two_cos_w = 2.0f * cos_val;
53
54 for (j = 0; j + 1 < order; j += 2 * 2) {
55 // Unroll the loop once since order is a multiple of four
56 q *= lsp[j] - two_cos_w;
57 p *= lsp[j + 1] - two_cos_w;
58
59 q *= lsp[j + 2] - two_cos_w;
60 p *= lsp[j + 3] - two_cos_w;
61 }
62
63 p *= p * (2.0f - two_cos_w);
64 q *= q * (2.0f + two_cos_w);
65
66 return 0.5 / (p + q);
67}
68
69/**
70 * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
71 */
72static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
73{
74 int i;
75 const TwinVQModeTab *mtab = tctx->mtab;
76 int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
77
78 for (i = 0; i < size_s / 2; i++) {
79 float cos_i = tctx->cos_tabs[0][i];
80 lpc[i] = eval_lpc_spectrum(cos_vals, cos_i, mtab->n_lsp);
81 lpc[size_s - i - 1] = eval_lpc_spectrum(cos_vals, -cos_i, mtab->n_lsp);
82 }
83}
84
85static void interpolate(float *out, float v1, float v2, int size)
86{
87 int i;
88 float step = (v1 - v2) / (size + 1);
89
90 for (i = 0; i < size; i++) {
91 v2 += step;
92 out[i] = v2;
93 }
94}
95
96static inline float get_cos(int idx, int part, const float *cos_tab, int size)
97{
98 return part ? -cos_tab[size - idx - 1]
99 : cos_tab[idx];
100}
101
102/**
103 * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
104 * Probably for speed reasons, the coefficients are evaluated as
105 * siiiibiiiisiiiibiiiisiiiibiiiisiiiibiiiis ...
106 * where s is an evaluated value, i is a value interpolated from the others
107 * and b might be either calculated or interpolated, depending on an
108 * unexplained condition.
109 *
110 * @param step the size of a block "siiiibiiii"
111 * @param in the cosine of the LSP data
112 * @param part is 0 for 0...PI (positive cosine values) and 1 for PI...2PI
113 * (negative cosine values)
114 * @param size the size of the whole output
115 */
116static inline void eval_lpcenv_or_interp(TwinVQContext *tctx,
118 float *out, const float *in,
119 int size, int step, int part)
120{
121 int i;
122 const TwinVQModeTab *mtab = tctx->mtab;
123 const float *cos_tab = tctx->cos_tabs[ftype];
124
125 // Fill the 's'
126 for (i = 0; i < size; i += step)
127 out[i] =
129 get_cos(i, part, cos_tab, size),
130 mtab->n_lsp);
131
132 // Fill the 'iiiibiiii'
133 for (i = step; i <= size - 2 * step; i += step) {
134 if (out[i + step] + out[i - step] > 1.95 * out[i] ||
135 out[i + step] >= out[i - step]) {
136 interpolate(out + i - step + 1, out[i], out[i - step], step - 1);
137 } else {
138 out[i - step / 2] =
140 get_cos(i - step / 2, part, cos_tab, size),
141 mtab->n_lsp);
142 interpolate(out + i - step + 1, out[i - step / 2],
143 out[i - step], step / 2 - 1);
144 interpolate(out + i - step / 2 + 1, out[i],
145 out[i - step / 2], step / 2 - 1);
146 }
147 }
148
149 interpolate(out + size - 2 * step + 1, out[size - step],
150 out[size - 2 * step], step - 1);
151}
152
154 const float *buf, float *lpc,
155 int size, int step)
156{
157 eval_lpcenv_or_interp(tctx, ftype, lpc, buf, size / 2, step, 0);
158 eval_lpcenv_or_interp(tctx, ftype, lpc + size / 2, buf, size / 2,
159 2 * step, 1);
160
161 interpolate(lpc + size / 2 - step + 1, lpc[size / 2],
162 lpc[size / 2 - step], step);
163
164 twinvq_memset_float(lpc + size - 2 * step + 1, lpc[size - 2 * step],
165 2 * step - 1);
166}
167
168/**
169 * Inverse quantization. Read CB coefficients for cb1 and cb2 from the
170 * bitstream, sum the corresponding vectors and write the result to *out
171 * after permutation.
172 */
173static void dequant(TwinVQContext *tctx, const uint8_t *cb_bits, float *out,
175 const int16_t *cb0, const int16_t *cb1, int cb_len)
176{
177 int pos = 0;
178 int i, j;
179
180 for (i = 0; i < tctx->n_div[ftype]; i++) {
181 int tmp0, tmp1;
182 int sign0 = 1;
183 int sign1 = 1;
184 const int16_t *tab0, *tab1;
185 int length = tctx->length[ftype][i >= tctx->length_change[ftype]];
186 int bitstream_second_part = (i >= tctx->bits_main_spec_change[ftype]);
187
188 int bits = tctx->bits_main_spec[0][ftype][bitstream_second_part];
189 tmp0 = *cb_bits++;
190 if (bits == 7) {
191 if (tmp0 & 0x40)
192 sign0 = -1;
193 tmp0 &= 0x3F;
194 }
195
196 bits = tctx->bits_main_spec[1][ftype][bitstream_second_part];
197 tmp1 = *cb_bits++;
198 if (bits == 7) {
199 if (tmp1 & 0x40)
200 sign1 = -1;
201 tmp1 &= 0x3F;
202 }
203
204 tab0 = cb0 + tmp0 * cb_len;
205 tab1 = cb1 + tmp1 * cb_len;
206
207 for (j = 0; j < length; j++)
208 out[tctx->permut[ftype][pos + j]] = sign0 * tab0[j] +
209 sign1 * tab1[j];
210
211 pos += length;
212 }
213}
214
215static void dec_gain(TwinVQContext *tctx,
216 enum TwinVQFrameType ftype, float *out)
217{
218 const TwinVQModeTab *mtab = tctx->mtab;
219 const TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
220 int i, j;
221 int channels = tctx->avctx->ch_layout.nb_channels;
222 int sub = mtab->fmode[ftype].sub;
223 float step = TWINVQ_AMP_MAX / ((1 << TWINVQ_GAIN_BITS) - 1);
224 float sub_step = TWINVQ_SUB_AMP_MAX / ((1 << TWINVQ_SUB_GAIN_BITS) - 1);
225
226 if (ftype == TWINVQ_FT_LONG) {
227 for (i = 0; i < channels; i++)
228 out[i] = (1.0 / (1 << 13)) *
229 twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
231 } else {
232 for (i = 0; i < channels; i++) {
233 float val = (1.0 / (1 << 23)) *
234 twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
236
237 for (j = 0; j < sub; j++)
238 out[i * sub + j] =
239 val * twinvq_mulawinv(sub_step * 0.5 +
240 sub_step * bits->sub_gain_bits[i * sub + j],
242 }
243 }
244}
245
246/**
247 * Rearrange the LSP coefficients so that they have a minimum distance of
248 * min_dist. This function does it exactly as described in section of 3.2.4
249 * of the G.729 specification (but interestingly is different from what the
250 * reference decoder actually does).
251 */
252static void rearrange_lsp(int order, float *lsp, float min_dist)
253{
254 int i;
255 float min_dist2 = min_dist * 0.5;
256 for (i = 1; i < order; i++)
257 if (lsp[i] - lsp[i - 1] < min_dist) {
258 float avg = (lsp[i] + lsp[i - 1]) * 0.5;
259
260 lsp[i - 1] = avg - min_dist2;
261 lsp[i] = avg + min_dist2;
262 }
263}
264
265static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2,
266 int lpc_hist_idx, float *lsp, float *hist)
267{
268 const TwinVQModeTab *mtab = tctx->mtab;
269 int i, j;
270
271 const float *cb = mtab->lspcodebook;
272 const float *cb2 = cb + (1 << mtab->lsp_bit1) * mtab->n_lsp;
273 const float *cb3 = cb2 + (1 << mtab->lsp_bit2) * mtab->n_lsp;
274
275 const int8_t funny_rounding[4] = {
276 -2,
277 mtab->lsp_split == 4 ? -2 : 1,
278 mtab->lsp_split == 4 ? -2 : 1,
279 0
280 };
281
282 j = 0;
283 for (i = 0; i < mtab->lsp_split; i++) {
284 int chunk_end = ((i + 1) * mtab->n_lsp + funny_rounding[i]) /
285 mtab->lsp_split;
286 for (; j < chunk_end; j++)
287 lsp[j] = cb[lpc_idx1 * mtab->n_lsp + j] +
288 cb2[lpc_idx2[i] * mtab->n_lsp + j];
289 }
290
291 rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
292
293 for (i = 0; i < mtab->n_lsp; i++) {
294 float tmp1 = 1.0 - cb3[lpc_hist_idx * mtab->n_lsp + i];
295 float tmp2 = hist[i] * cb3[lpc_hist_idx * mtab->n_lsp + i];
296 hist[i] = lsp[i];
297 lsp[i] = lsp[i] * tmp1 + tmp2;
298 }
299
300 rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
301 rearrange_lsp(mtab->n_lsp, lsp, 0.000095);
303}
304
305static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp,
306 enum TwinVQFrameType ftype, float *lpc)
307{
308 int i;
309 int size = tctx->mtab->size / tctx->mtab->fmode[ftype].sub;
310
311 for (i = 0; i < tctx->mtab->n_lsp; i++)
312 lsp[i] = 2 * cos(lsp[i]);
313
314 switch (ftype) {
315 case TWINVQ_FT_LONG:
316 eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 8);
317 break;
318 case TWINVQ_FT_MEDIUM:
319 eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 2);
320 break;
321 case TWINVQ_FT_SHORT:
322 eval_lpcenv(tctx, lsp, lpc);
323 break;
324 }
325}
326
327static const uint8_t wtype_to_wsize[] = { 0, 0, 2, 2, 2, 1, 0, 1, 1 };
328
330 int wtype, float *in, float *prev, int ch)
331{
332 AVTXContext *tx = tctx->tx[ftype];
333 av_tx_fn tx_fn = tctx->tx_fn[ftype];
334 const TwinVQModeTab *mtab = tctx->mtab;
335 int bsize = mtab->size / mtab->fmode[ftype].sub;
336 int size = mtab->size;
337 float *buf1 = tctx->tmp_buf;
338 int j, first_wsize, wsize; // Window size
339 float *out = tctx->curr_frame + 2 * ch * mtab->size;
340 float *out2 = out;
341 float *prev_buf;
342 int types_sizes[] = {
343 mtab->size / mtab->fmode[TWINVQ_FT_LONG].sub,
344 mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub,
345 mtab->size / (mtab->fmode[TWINVQ_FT_SHORT].sub * 2),
346 };
347
348 wsize = types_sizes[wtype_to_wsize[wtype]];
349 first_wsize = wsize;
350 prev_buf = prev + (size - bsize) / 2;
351
352 for (j = 0; j < mtab->fmode[ftype].sub; j++) {
353 int sub_wtype = ftype == TWINVQ_FT_MEDIUM ? 8 : wtype;
354
355 if (!j && wtype == 4)
356 sub_wtype = 4;
357 else if (j == mtab->fmode[ftype].sub - 1 && wtype == 7)
358 sub_wtype = 7;
359
360 wsize = types_sizes[wtype_to_wsize[sub_wtype]];
361
362 tx_fn(tx, buf1 + bsize * j, in + bsize * j, sizeof(float));
363
364 tctx->fdsp->vector_fmul_window(out2, prev_buf + (bsize - wsize) / 2,
365 buf1 + bsize * j,
366 ff_sine_windows[av_log2(wsize)],
367 wsize / 2);
368 out2 += wsize;
369
370 memcpy(out2, buf1 + bsize * j + wsize / 2,
371 (bsize - wsize / 2) * sizeof(float));
372
373 out2 += ftype == TWINVQ_FT_MEDIUM ? (bsize - wsize) / 2 : bsize - wsize;
374
375 prev_buf = buf1 + bsize * j + bsize / 2;
376 }
377
378 tctx->last_block_pos[ch] = (size + first_wsize) / 2;
379}
380
382 int wtype, float **out, int offset)
383{
384 const TwinVQModeTab *mtab = tctx->mtab;
385 float *prev_buf = tctx->prev_frame + tctx->last_block_pos[0];
386 int channels = tctx->avctx->ch_layout.nb_channels;
387 int size1, size2, i;
388 float *out1, *out2;
389
390 for (i = 0; i < channels; i++)
391 imdct_and_window(tctx, ftype, wtype,
392 tctx->spectrum + i * mtab->size,
393 prev_buf + 2 * i * mtab->size,
394 i);
395
396 if (!out)
397 return;
398
399 size2 = tctx->last_block_pos[0];
400 size1 = mtab->size - size2;
401
402 out1 = &out[0][0] + offset;
403 memcpy(out1, prev_buf, size1 * sizeof(*out1));
404 memcpy(out1 + size1, tctx->curr_frame, size2 * sizeof(*out1));
405
406 if (channels == 2) {
407 out2 = &out[1][0] + offset;
408 memcpy(out2, &prev_buf[2 * mtab->size],
409 size1 * sizeof(*out2));
410 memcpy(out2 + size1, &tctx->curr_frame[2 * mtab->size],
411 size2 * sizeof(*out2));
412 tctx->fdsp->butterflies_float(out1, out2, mtab->size);
413 }
414}
415
418{
419 const TwinVQModeTab *mtab = tctx->mtab;
420 TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
421 int channels = tctx->avctx->ch_layout.nb_channels;
422 int sub = mtab->fmode[ftype].sub;
423 int block_size = mtab->size / sub;
425 float ppc_shape[TWINVQ_PPC_SHAPE_LEN_MAX * TWINVQ_CHANNELS_MAX * 4];
426
427 int i, j;
428
429 dequant(tctx, bits->main_coeffs, out, ftype,
430 mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
431 mtab->fmode[ftype].cb_len_read);
432
433 dec_gain(tctx, ftype, gain);
434
435 if (ftype == TWINVQ_FT_LONG) {
436 int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len * channels - 1) /
437 tctx->n_div[3];
438 dequant(tctx, bits->ppc_coeffs, ppc_shape,
440 mtab->ppc_shape_cb + cb_len_p * TWINVQ_PPC_SHAPE_CB_SIZE,
441 cb_len_p);
442 }
443
444 for (i = 0; i < channels; i++) {
445 float *chunk = out + mtab->size * i;
446 float lsp[TWINVQ_LSP_COEFS_MAX];
447
448 for (j = 0; j < sub; j++) {
449 tctx->dec_bark_env(tctx, bits->bark1[i][j],
450 bits->bark_use_hist[i][j], i,
451 tctx->tmp_buf, gain[sub * i + j], ftype);
452
453 tctx->fdsp->vector_fmul(chunk + block_size * j,
454 chunk + block_size * j,
455 tctx->tmp_buf, block_size);
456 }
457
458 if (ftype == TWINVQ_FT_LONG)
459 tctx->decode_ppc(tctx, bits->p_coef[i], bits->g_coef[i],
460 ppc_shape + i * mtab->ppc_shape_len, chunk);
461
462 decode_lsp(tctx, bits->lpc_idx1[i], bits->lpc_idx2[i],
463 bits->lpc_hist_idx[i], lsp, tctx->lsp_hist[i]);
464
465 dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
466
467 for (j = 0; j < mtab->fmode[ftype].sub; j++) {
468 tctx->fdsp->vector_fmul(chunk, chunk, tctx->tmp_buf, block_size);
469 chunk += block_size;
470 }
471 }
472}
473
479
481 int *got_frame_ptr, AVPacket *avpkt)
482{
483 const uint8_t *buf = avpkt->data;
484 int buf_size = avpkt->size;
485 TwinVQContext *tctx = avctx->priv_data;
486 const TwinVQModeTab *mtab = tctx->mtab;
487 float **out = NULL;
488 int ret;
489
490 /* get output buffer */
491 if (tctx->discarded_packets >= 2) {
492 frame->nb_samples = mtab->size * tctx->frames_per_packet;
493 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
494 return ret;
495 out = (float **)frame->extended_data;
496 }
497
498 if (buf_size < avctx->block_align) {
499 av_log(avctx, AV_LOG_ERROR,
500 "Frame too small (%d bytes). Truncated file?\n", buf_size);
501 return AVERROR(EINVAL);
502 }
503
504 if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
505 return ret;
506
507 for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
508 tctx->cur_frame++) {
510 tctx->bits[tctx->cur_frame].ftype);
511
512 imdct_output(tctx, tctx->bits[tctx->cur_frame].ftype,
513 tctx->bits[tctx->cur_frame].window_type, out,
514 tctx->cur_frame * mtab->size);
515
516 FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
517 }
518
519 if (tctx->discarded_packets < 2) {
520 tctx->discarded_packets++;
521 *got_frame_ptr = 0;
522 return buf_size;
523 }
524
525 *got_frame_ptr = 1;
526
527 // VQF can deliver packets 1 byte greater than block align
528 if (buf_size == avctx->block_align + 1)
529 return buf_size;
530 return avctx->block_align;
531}
532
533/**
534 * Init IMDCT and windowing tables
535 */
537{
538 int i, j, ret;
539 const TwinVQModeTab *mtab = tctx->mtab;
540 int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
541 int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
542 int channels = tctx->avctx->ch_layout.nb_channels;
543 float norm = channels == 1 ? 2.0 : 1.0;
544 int table_size = 2 * mtab->size * channels;
545
546 for (i = 0; i < 3; i++) {
547 int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
548 const float scale = -sqrt(norm / bsize) / (1 << 15);
549 if ((ret = av_tx_init(&tctx->tx[i], &tctx->tx_fn[i], AV_TX_FLOAT_MDCT,
550 1, bsize, &scale, 0)))
551 return ret;
552 }
553
554 if (!FF_ALLOC_TYPED_ARRAY(tctx->tmp_buf, mtab->size) ||
555 !FF_ALLOC_TYPED_ARRAY(tctx->spectrum, table_size) ||
556 !FF_ALLOC_TYPED_ARRAY(tctx->curr_frame, table_size) ||
557 !FF_ALLOC_TYPED_ARRAY(tctx->prev_frame, table_size))
558 return AVERROR(ENOMEM);
559
560 for (i = 0; i < 3; i++) {
561 int m = 4 * mtab->size / mtab->fmode[i].sub;
562 double freq = 2 * M_PI / m;
563 if (!FF_ALLOC_TYPED_ARRAY(tctx->cos_tabs[i], m / 4))
564 return AVERROR(ENOMEM);
565 for (j = 0; j <= m / 8; j++)
566 tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
567 for (j = 1; j < m / 8; j++)
568 tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
569 }
570
572 ff_init_ff_sine_windows(av_log2(size_s / 2));
574
575 return 0;
576}
577
578/**
579 * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
580 * each line do a cyclic permutation, i.e.
581 * abcdefghijklm -> defghijklmabc
582 * where the amount to be shifted is evaluated depending on the column.
583 */
584static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
585 int block_size,
586 const uint8_t line_len[2], int length_div,
588{
589 int i, j;
590
591 for (i = 0; i < line_len[0]; i++) {
592 int shift;
593
594 if (num_blocks == 1 ||
595 (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
596 (ftype != TWINVQ_FT_LONG && num_vect & 1) ||
597 i == line_len[1]) {
598 shift = 0;
599 } else if (ftype == TWINVQ_FT_LONG) {
600 shift = i;
601 } else
602 shift = i * i;
603
604 for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
605 tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
606 }
607}
608
609/**
610 * Interpret the input data as in the following table:
611 *
612 * @verbatim
613 *
614 * abcdefgh
615 * ijklmnop
616 * qrstuvw
617 * x123456
618 *
619 * @endverbatim
620 *
621 * and transpose it, giving the output
622 * aiqxbjr1cks2dlt3emu4fvn5gow6hp
623 */
624static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
625 const uint8_t line_len[2], int length_div)
626{
627 int i, j;
628 int cont = 0;
629
630 for (i = 0; i < num_vect; i++)
631 for (j = 0; j < line_len[i >= length_div]; j++)
632 out[cont++] = in[j * num_vect + i];
633}
634
635static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
636{
637 int block_size = size / n_blocks;
638 int i;
639
640 for (i = 0; i < size; i++)
641 out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
642}
643
646{
647 int block_size, size;
648 const TwinVQModeTab *mtab = tctx->mtab;
649 int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
650
651 if (ftype == TWINVQ_FT_PPC) {
653 block_size = mtab->ppc_shape_len;
654 } else {
655 size = tctx->avctx->ch_layout.nb_channels * mtab->fmode[ftype].sub;
656 block_size = mtab->size / mtab->fmode[ftype].sub;
657 }
658
659 permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
660 block_size, tctx->length[ftype],
661 tctx->length_change[ftype], ftype);
662
663 transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
664 tctx->length[ftype], tctx->length_change[ftype]);
665
666 linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
667 size * block_size);
668}
669
671{
672 const TwinVQModeTab *mtab = tctx->mtab;
673 int n_ch = tctx->avctx->ch_layout.nb_channels;
674 int total_fr_bits = tctx->avctx->bit_rate * mtab->size /
675 tctx->avctx->sample_rate;
676
677 int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
678 mtab->lsp_split * mtab->lsp_bit2);
679
680 int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
681 mtab->ppc_period_bit);
682
683 int bsize_no_main_cb[3], bse_bits[3], i;
684 enum TwinVQFrameType frametype;
685
686 for (i = 0; i < 3; i++)
687 // +1 for history usage switch
688 bse_bits[i] = n_ch *
689 (mtab->fmode[i].bark_n_coef *
690 mtab->fmode[i].bark_n_bit + 1);
691
692 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
694
695 for (i = 0; i < 2; i++)
696 bsize_no_main_cb[i] =
697 lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
699 mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
700
701 if (tctx->codec == TWINVQ_CODEC_METASOUND && !tctx->is_6kbps) {
702 bsize_no_main_cb[1] += 2;
703 bsize_no_main_cb[2] += 2;
704 }
705
706 // The remaining bits are all used for the main spectrum coefficients
707 for (i = 0; i < 4; i++) {
708 int bit_size, vect_size;
709 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
710 if (i == 3) {
711 bit_size = n_ch * mtab->ppc_shape_bit;
712 vect_size = n_ch * mtab->ppc_shape_len;
713 } else {
714 bit_size = total_fr_bits - bsize_no_main_cb[i];
715 vect_size = n_ch * mtab->size;
716 }
717
718 tctx->n_div[i] = (bit_size + 13) / 14;
719
720 rounded_up = (bit_size + tctx->n_div[i] - 1) /
721 tctx->n_div[i];
722 rounded_down = (bit_size) / tctx->n_div[i];
723 num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
724 num_rounded_up = tctx->n_div[i] - num_rounded_down;
725 tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;
726 tctx->bits_main_spec[1][i][0] = rounded_up / 2;
727 tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;
728 tctx->bits_main_spec[1][i][1] = rounded_down / 2;
729 tctx->bits_main_spec_change[i] = num_rounded_up;
730
731 rounded_up = (vect_size + tctx->n_div[i] - 1) /
732 tctx->n_div[i];
733 rounded_down = (vect_size) / tctx->n_div[i];
734 num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
735 num_rounded_up = tctx->n_div[i] - num_rounded_down;
736 tctx->length[i][0] = rounded_up;
737 tctx->length[i][1] = rounded_down;
738 tctx->length_change[i] = num_rounded_up;
739 }
740
741 for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
742 construct_perm_table(tctx, frametype);
743}
744
746{
747 TwinVQContext *tctx = avctx->priv_data;
748 int i;
749
750 for (i = 0; i < 3; i++) {
751 av_tx_uninit(&tctx->tx[i]);
752 av_freep(&tctx->cos_tabs[i]);
753 }
754
755 av_freep(&tctx->curr_frame);
756 av_freep(&tctx->spectrum);
757 av_freep(&tctx->prev_frame);
758 av_freep(&tctx->tmp_buf);
759 av_freep(&tctx->fdsp);
760
761 return 0;
762}
763
765{
766 int ret;
767 TwinVQContext *tctx = avctx->priv_data;
768 int64_t frames_per_packet;
769
770 tctx->avctx = avctx;
772
773 if (!avctx->block_align) {
774 avctx->block_align = tctx->frame_size + 7 >> 3;
775 }
776 frames_per_packet = avctx->block_align * 8LL / tctx->frame_size;
777 if (frames_per_packet <= 0) {
778 av_log(avctx, AV_LOG_ERROR, "Block align is %"PRId64" bits, expected %d\n",
779 avctx->block_align * (int64_t)8, tctx->frame_size);
780 return AVERROR_INVALIDDATA;
781 }
782 if (frames_per_packet > TWINVQ_MAX_FRAMES_PER_PACKET) {
783 av_log(avctx, AV_LOG_ERROR, "Too many frames per packet (%"PRId64")\n",
784 frames_per_packet);
785 return AVERROR_INVALIDDATA;
786 }
787 tctx->frames_per_packet = frames_per_packet;
788
790 if (!tctx->fdsp)
791 return AVERROR(ENOMEM);
792 if ((ret = init_mdct_win(tctx))) {
793 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
794 return ret;
795 }
797
798 twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
800
801 return 0;
802}
#define ftype
static double val(void *priv, double ch)
Definition aeval.c:77
static FILE * out
channels
Definition aptx.h:31
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define avg(a, b, c, d)
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static float cos_tab[256]
Definition dca_lbr.c:122
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
static const uint8_t bits[8]
Definition fastaudio.c:100
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#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
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition internal.h:71
void ff_sort_nearly_sorted_floats(float *vals, int len)
Sort values in ascending order.
Definition lsp.c:245
const int16_t * tab1
Definition mace.c:145
#define FFSWAP(type, a, b)
Definition macros.h:52
#define M_PI
Definition mathematics.h:67
Memory handling functions.
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
SINETABLE_CONST float *const ff_sine_windows[]
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
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
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition avcodec.h:1075
void * priv_data
Definition avcodec.h:470
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:164
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:119
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
Definition float_dsp.h:38
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
enum TwinVQCodec codec
Definition twinvq.h:173
int last_block_pos[2]
Definition twinvq.h:162
float bark_hist[3][2][40]
BSE coefficients of last frame.
Definition twinvq.h:149
AVCodecContext * avctx
Definition twinvq.h:138
uint8_t length[4][2]
main codebook stride
Definition twinvq.h:153
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
av_tx_fn tx_fn[3]
Definition twinvq.h:141
int16_t permut[4][4096]
Definition twinvq.h:152
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
AVTXContext * tx[3]
Definition twinvq.h:140
float * spectrum
Definition twinvq.h:159
float * tmp_buf
Definition twinvq.h:168
int n_div[4]
Definition twinvq.h:157
float lsp_hist[2][20]
LSP coefficients of the last frame.
Definition twinvq.h:148
int discarded_packets
Definition twinvq.h:163
float * cos_tabs[3]
Definition twinvq.h:165
float * prev_frame
non-interleaved previous frame
Definition twinvq.h:161
uint8_t length_change[4]
Definition twinvq.h:154
AVFloatDSPContext * fdsp
Definition twinvq.h:139
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
float * curr_frame
non-interleaved output
Definition twinvq.h:160
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
enum TwinVQFrameType ftype
Definition twinvq.h:88
int window_type
Definition twinvq.h:87
uint8_t bark_n_bit
number of bits of the BSE coefs
Definition twinvq.h:75
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
uint8_t cb_len_read
number of spectrum coefficients to read
Definition twinvq.h:83
const int16_t * cb1
Definition twinvq.h:80
const int16_t * cb0
main codebooks for spectrum data
Definition twinvq.h:79
Parameters and tables that are different for every combination of bitrate/sample rate.
Definition twinvq.h:111
uint8_t ppc_shape_bit
number of bits of the PPC shape CB coeffs
Definition twinvq.h:129
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
uint8_t n_lsp
number of lsp coefficients
Definition twinvq.h:115
struct TwinVQFrameMode fmode[3]
frame type-dependent parameters
Definition twinvq.h:112
const float * lspcodebook
Definition twinvq.h:116
uint8_t ppc_period_bit
number of the bits for the PPC period value
Definition twinvq.h:127
const int16_t * ppc_shape_cb
PPC shape CB.
Definition twinvq.h:124
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_freep(p)
#define av_log(a,...)
static av_cold void construct_perm_table(TwinVQContext *tctx, enum TwinVQFrameType ftype)
Definition twinvq.c:644
static av_cold int init_mdct_win(TwinVQContext *tctx)
Init IMDCT and windowing tables.
Definition twinvq.c:536
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:624
enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[]
Definition twinvq.c:474
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:72
static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp, enum TwinVQFrameType ftype, float *lpc)
Definition twinvq.c:305
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
Definition twinvq.c:329
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:116
int ff_twinvq_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition twinvq.c:480
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:173
static void dec_gain(TwinVQContext *tctx, enum TwinVQFrameType ftype, float *out)
Definition twinvq.c:215
static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2, int lpc_hist_idx, float *lsp, float *hist)
Definition twinvq.c:265
static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
Definition twinvq.c:635
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:584
static av_cold void init_bitstream_params(TwinVQContext *tctx)
Definition twinvq.c:670
static float get_cos(int idx, int part, const float *cos_tab, int size)
Definition twinvq.c:96
static const uint8_t wtype_to_wsize[]
Definition twinvq.c:327
av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
Definition twinvq.c:745
static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float **out, int offset)
Definition twinvq.c:381
static void interpolate(float *out, float v1, float v2, int size)
Definition twinvq.c:85
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:47
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:252
av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
Requires the caller to call ff_twinvq_decode_close() upon failure.
Definition twinvq.c:764
static void read_and_decode_spectrum(TwinVQContext *tctx, float *out, enum TwinVQFrameType ftype)
Definition twinvq.c:416
static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype, const float *buf, float *lpc, int size, int step)
Definition twinvq.c:153
#define TWINVQ_PPC_SHAPE_LEN_MAX
Definition twinvq.h:47
#define TWINVQ_CHANNELS_MAX
Definition twinvq.h:57
#define TWINVQ_MAX_FRAMES_PER_PACKET
Definition twinvq.h:61
#define TWINVQ_SUB_GAIN_BITS
Definition twinvq.h:52
#define TWINVQ_MULAW_MU
Definition twinvq.h:49
#define TWINVQ_WINDOW_TYPE_BITS
Definition twinvq.h:53
@ TWINVQ_CODEC_METASOUND
Definition twinvq.h:36
#define TWINVQ_PPC_SHAPE_CB_SIZE
Definition twinvq.h:46
#define TWINVQ_LSP_COEFS_MAX
Definition twinvq.h:55
TwinVQFrameType
Definition twinvq.h:39
@ TWINVQ_FT_MEDIUM
Medium frame (divided in m<n sub-blocks)
Definition twinvq.h:41
@ 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
@ TWINVQ_FT_PPC
Periodic Peak Component (part of the long frame)
Definition twinvq.h:43
#define TWINVQ_GAIN_BITS
Definition twinvq.h:50
#define TWINVQ_SUBBLOCKS_MAX
Definition twinvq.h:58
#define TWINVQ_AMP_MAX
Definition twinvq.h:51
#define TWINVQ_SUB_AMP_MAX
Definition twinvq.h:48
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 size
static const struct twinvq_data tab
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static int chunk_end(AVFormatContext *s, int flush)
Definition webm_chunk.c:174