3 * Copyright (c) 2009 Vitor Sessak
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/float_dsp.h"
35 * Evaluate a single LPC amplitude spectrum envelope coefficient from the line
38 * @param lsp a vector of the cosine of the LSP values
39 * @param cos_val cos(PI*i/N) where i is the index of the LPC amplitude
40 * @param order the order of the LSP (and the size of the *lsp buffer). Must
41 * be a multiple of four.
42 * @return the LPC value
44 * @todo reuse code from Vorbis decoder: vorbis_floor0_decode
46 static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
51 float two_cos_w = 2.0f * cos_val;
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;
58 q *= lsp[j + 2] - two_cos_w;
59 p *= lsp[j + 3] - two_cos_w;
62 p *= p * (2.0f - two_cos_w);
63 q *= q * (2.0f + two_cos_w);
69 * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
71 static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
74 const TwinVQModeTab *mtab = tctx->mtab;
75 int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
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);
84 static void interpolate(float *out, float v1, float v2, int size)
87 float step = (v1 - v2) / (size + 1);
89 for (i = 0; i < size; i++) {
95 static inline float get_cos(int idx, int part, const float *cos_tab, int size)
97 return part ? -cos_tab[size - idx - 1]
102 * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
103 * Probably for speed reasons, the coefficients are evaluated as
104 * siiiibiiiisiiiibiiiisiiiibiiiisiiiibiiiis ...
105 * where s is an evaluated value, i is a value interpolated from the others
106 * and b might be either calculated or interpolated, depending on an
107 * unexplained condition.
109 * @param step the size of a block "siiiibiiii"
110 * @param in the cosine of the LSP data
111 * @param part is 0 for 0...PI (positive cosine values) and 1 for PI...2PI
112 * (negative cosine values)
113 * @param size the size of the whole output
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)
121 const TwinVQModeTab *mtab = tctx->mtab;
122 const float *cos_tab = tctx->cos_tabs[ftype];
125 for (i = 0; i < size; i += step)
127 eval_lpc_spectrum(in,
128 get_cos(i, part, cos_tab, size),
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);
138 eval_lpc_spectrum(in,
139 get_cos(i - step / 2, part, cos_tab, size),
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);
148 interpolate(out + size - 2 * step + 1, out[size - step],
149 out[size - 2 * step], step - 1);
152 static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype,
153 const float *buf, float *lpc,
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,
160 interpolate(lpc + size / 2 - step + 1, lpc[size / 2],
161 lpc[size / 2 - step], step);
163 twinvq_memset_float(lpc + size - 2 * step + 1, lpc[size - 2 * step],
168 * Inverse quantization. Read CB coefficients for cb1 and cb2 from the
169 * bitstream, sum the corresponding vectors and write the result to *out
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)
179 for (i = 0; i < tctx->n_div[ftype]; i++) {
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]);
187 int bits = tctx->bits_main_spec[0][ftype][bitstream_second_part];
195 bits = tctx->bits_main_spec[1][ftype][bitstream_second_part];
203 tab0 = cb0 + tmp0 * cb_len;
204 tab1 = cb1 + tmp1 * cb_len;
206 for (j = 0; j < length; j++)
207 out[tctx->permut[ftype][pos + j]] = sign0 * tab0[j] +
214 static void dec_gain(TwinVQContext *tctx,
215 enum TwinVQFrameType ftype, float *out)
217 const TwinVQModeTab *mtab = tctx->mtab;
218 const TwinVQFrameData *bits = &tctx->bits;
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);
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],
228 TWINVQ_AMP_MAX, TWINVQ_MULAW_MU);
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],
233 TWINVQ_AMP_MAX, TWINVQ_MULAW_MU);
235 for (j = 0; j < sub; j++)
237 val * twinvq_mulawinv(sub_step * 0.5 +
238 sub_step * bits->sub_gain_bits[i * sub + j],
239 TWINVQ_SUB_AMP_MAX, TWINVQ_MULAW_MU);
245 * Rearrange the LSP coefficients so that they have a minimum distance of
246 * min_dist. This function does it exactly as described in section of 3.2.4
247 * of the G.729 specification (but interestingly is different from what the
248 * reference decoder actually does).
250 static void rearrange_lsp(int order, float *lsp, float min_dist)
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;
258 lsp[i - 1] = avg - min_dist2;
259 lsp[i] = avg + min_dist2;
263 static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2,
264 int lpc_hist_idx, float *lsp, float *hist)
266 const TwinVQModeTab *mtab = tctx->mtab;
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;
273 const int8_t funny_rounding[4] = {
275 mtab->lsp_split == 4 ? -2 : 1,
276 mtab->lsp_split == 4 ? -2 : 1,
281 for (i = 0; i < mtab->lsp_split; i++) {
282 int chunk_end = ((i + 1) * mtab->n_lsp + funny_rounding[i]) /
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];
289 rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
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];
295 lsp[i] = lsp[i] * tmp1 + tmp2;
298 rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
299 rearrange_lsp(mtab->n_lsp, lsp, 0.000095);
300 ff_sort_nearly_sorted_floats(lsp, mtab->n_lsp);
303 static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp,
304 enum TwinVQFrameType ftype, float *lpc)
307 int size = tctx->mtab->size / tctx->mtab->fmode[ftype].sub;
309 for (i = 0; i < tctx->mtab->n_lsp; i++)
310 lsp[i] = 2 * cos(lsp[i]);
314 eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 8);
316 case TWINVQ_FT_MEDIUM:
317 eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 2);
319 case TWINVQ_FT_SHORT:
320 eval_lpcenv(tctx, lsp, lpc);
325 static const uint8_t wtype_to_wsize[] = { 0, 0, 2, 2, 2, 1, 0, 1, 1 };
327 static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype,
328 int wtype, float *in, float *prev, int ch)
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;
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),
345 wsize = types_sizes[wtype_to_wsize[wtype]];
347 prev_buf = prev + (size - bsize) / 2;
349 for (j = 0; j < mtab->fmode[ftype].sub; j++) {
350 int sub_wtype = ftype == TWINVQ_FT_MEDIUM ? 8 : wtype;
352 if (!j && wtype == 4)
354 else if (j == mtab->fmode[ftype].sub - 1 && wtype == 7)
357 wsize = types_sizes[wtype_to_wsize[sub_wtype]];
359 mdct->imdct_half(mdct, buf1 + bsize * j, in + bsize * j);
361 tctx->fdsp.vector_fmul_window(out2, prev_buf + (bsize - wsize) / 2,
363 ff_sine_windows[av_log2(wsize)],
367 memcpy(out2, buf1 + bsize * j + wsize / 2,
368 (bsize - wsize / 2) * sizeof(float));
370 out2 += ftype == TWINVQ_FT_MEDIUM ? (bsize - wsize) / 2 : bsize - wsize;
372 prev_buf = buf1 + bsize * j + bsize / 2;
375 tctx->last_block_pos[ch] = (size + first_wsize) / 2;
378 static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype,
379 int wtype, float **out)
381 const TwinVQModeTab *mtab = tctx->mtab;
382 float *prev_buf = tctx->prev_frame + tctx->last_block_pos[0];
385 for (i = 0; i < tctx->avctx->channels; i++)
386 imdct_and_window(tctx, ftype, wtype,
387 tctx->spectrum + i * mtab->size,
388 prev_buf + 2 * i * mtab->size,
394 size2 = tctx->last_block_pos[0];
395 size1 = mtab->size - size2;
397 memcpy(&out[0][0], prev_buf, size1 * sizeof(out[0][0]));
398 memcpy(&out[0][size1], tctx->curr_frame, size2 * sizeof(out[0][0]));
400 if (tctx->avctx->channels == 2) {
401 memcpy(&out[1][0], &prev_buf[2 * mtab->size],
402 size1 * sizeof(out[1][0]));
403 memcpy(&out[1][size1], &tctx->curr_frame[2 * mtab->size],
404 size2 * sizeof(out[1][0]));
405 tctx->fdsp.butterflies_float(out[0], out[1], mtab->size);
409 static void read_and_decode_spectrum(TwinVQContext *tctx, float *out,
410 enum TwinVQFrameType ftype)
412 const TwinVQModeTab *mtab = tctx->mtab;
413 TwinVQFrameData *bits = &tctx->bits;
414 int channels = tctx->avctx->channels;
415 int sub = mtab->fmode[ftype].sub;
416 int block_size = mtab->size / sub;
417 float gain[TWINVQ_CHANNELS_MAX * TWINVQ_SUBBLOCKS_MAX];
418 float ppc_shape[TWINVQ_PPC_SHAPE_LEN_MAX * TWINVQ_CHANNELS_MAX * 4];
422 dequant(tctx, bits->main_coeffs, out, ftype,
423 mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
424 mtab->fmode[ftype].cb_len_read);
426 dec_gain(tctx, ftype, gain);
428 if (ftype == TWINVQ_FT_LONG) {
429 int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len * channels - 1) /
431 dequant(tctx, bits->ppc_coeffs, ppc_shape,
432 TWINVQ_FT_PPC, mtab->ppc_shape_cb,
433 mtab->ppc_shape_cb + cb_len_p * TWINVQ_PPC_SHAPE_CB_SIZE,
437 for (i = 0; i < channels; i++) {
438 float *chunk = out + mtab->size * i;
439 float lsp[TWINVQ_LSP_COEFS_MAX];
441 for (j = 0; j < sub; j++) {
442 tctx->dec_bark_env(tctx, bits->bark1[i][j],
443 bits->bark_use_hist[i][j], i,
444 tctx->tmp_buf, gain[sub * i + j], ftype);
446 tctx->fdsp.vector_fmul(chunk + block_size * j,
447 chunk + block_size * j,
448 tctx->tmp_buf, block_size);
451 if (ftype == TWINVQ_FT_LONG)
452 tctx->decode_ppc(tctx, bits->p_coef[i], bits->g_coef[i],
453 ppc_shape + i * mtab->ppc_shape_len, chunk);
455 decode_lsp(tctx, bits->lpc_idx1[i], bits->lpc_idx2[i],
456 bits->lpc_hist_idx[i], lsp, tctx->lsp_hist[i]);
458 dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
460 for (j = 0; j < mtab->fmode[ftype].sub; j++) {
461 tctx->fdsp.vector_fmul(chunk, chunk, tctx->tmp_buf, block_size);
467 const enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[] = {
468 TWINVQ_FT_LONG, TWINVQ_FT_LONG, TWINVQ_FT_SHORT, TWINVQ_FT_LONG,
469 TWINVQ_FT_MEDIUM, TWINVQ_FT_LONG, TWINVQ_FT_LONG, TWINVQ_FT_MEDIUM,
473 int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
474 int *got_frame_ptr, AVPacket *avpkt)
476 AVFrame *frame = data;
477 const uint8_t *buf = avpkt->data;
478 int buf_size = avpkt->size;
479 TwinVQContext *tctx = avctx->priv_data;
480 const TwinVQModeTab *mtab = tctx->mtab;
484 /* get output buffer */
485 if (tctx->discarded_packets >= 2) {
486 frame->nb_samples = mtab->size;
487 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
489 out = (float **)frame->extended_data;
492 if (buf_size < avctx->block_align) {
493 av_log(avctx, AV_LOG_ERROR,
494 "Frame too small (%d bytes). Truncated file?\n", buf_size);
495 return AVERROR(EINVAL);
498 if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
501 read_and_decode_spectrum(tctx, tctx->spectrum, tctx->bits.ftype);
503 imdct_output(tctx, tctx->bits.ftype, tctx->bits.window_type, out);
505 FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
507 if (tctx->discarded_packets < 2) {
508 tctx->discarded_packets++;
519 * Init IMDCT and windowing tables
521 static av_cold int init_mdct_win(TwinVQContext *tctx)
524 const TwinVQModeTab *mtab = tctx->mtab;
525 int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
526 int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
527 int channels = tctx->avctx->channels;
528 float norm = channels == 1 ? 2.0 : 1.0;
530 for (i = 0; i < 3; i++) {
531 int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
532 if ((ret = ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
533 -sqrt(norm / bsize) / (1 << 15))))
537 FF_ALLOC_OR_GOTO(tctx->avctx, tctx->tmp_buf,
538 mtab->size * sizeof(*tctx->tmp_buf), alloc_fail);
540 FF_ALLOC_OR_GOTO(tctx->avctx, tctx->spectrum,
541 2 * mtab->size * channels * sizeof(*tctx->spectrum),
543 FF_ALLOC_OR_GOTO(tctx->avctx, tctx->curr_frame,
544 2 * mtab->size * channels * sizeof(*tctx->curr_frame),
546 FF_ALLOC_OR_GOTO(tctx->avctx, tctx->prev_frame,
547 2 * mtab->size * channels * sizeof(*tctx->prev_frame),
550 for (i = 0; i < 3; i++) {
551 int m = 4 * mtab->size / mtab->fmode[i].sub;
552 double freq = 2 * M_PI / m;
553 FF_ALLOC_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
554 (m / 4) * sizeof(*tctx->cos_tabs[i]), alloc_fail);
556 for (j = 0; j <= m / 8; j++)
557 tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
558 for (j = 1; j < m / 8; j++)
559 tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
562 ff_init_ff_sine_windows(av_log2(size_m));
563 ff_init_ff_sine_windows(av_log2(size_s / 2));
564 ff_init_ff_sine_windows(av_log2(mtab->size));
569 return AVERROR(ENOMEM);
573 * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
574 * each line do a cyclic permutation, i.e.
575 * abcdefghijklm -> defghijklmabc
576 * where the amount to be shifted is evaluated depending on the column.
578 static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
580 const uint8_t line_len[2], int length_div,
581 enum TwinVQFrameType ftype)
585 for (i = 0; i < line_len[0]; i++) {
588 if (num_blocks == 1 ||
589 (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
590 (ftype != TWINVQ_FT_LONG && num_vect & 1) ||
593 } else if (ftype == TWINVQ_FT_LONG) {
598 for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
599 tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
604 * Interpret the input data as in the following table:
615 * and transpose it, giving the output
616 * aiqxbjr1cks2dlt3emu4fvn5gow6hp
618 static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
619 const uint8_t line_len[2], int length_div)
624 for (i = 0; i < num_vect; i++)
625 for (j = 0; j < line_len[i >= length_div]; j++)
626 out[cont++] = in[j * num_vect + i];
629 static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
631 int block_size = size / n_blocks;
634 for (i = 0; i < size; i++)
635 out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
638 static av_cold void construct_perm_table(TwinVQContext *tctx,
639 enum TwinVQFrameType ftype)
641 int block_size, size;
642 const TwinVQModeTab *mtab = tctx->mtab;
643 int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
645 if (ftype == TWINVQ_FT_PPC) {
646 size = tctx->avctx->channels;
647 block_size = mtab->ppc_shape_len;
649 size = tctx->avctx->channels * mtab->fmode[ftype].sub;
650 block_size = mtab->size / mtab->fmode[ftype].sub;
653 permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
654 block_size, tctx->length[ftype],
655 tctx->length_change[ftype], ftype);
657 transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
658 tctx->length[ftype], tctx->length_change[ftype]);
660 linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
664 static av_cold void init_bitstream_params(TwinVQContext *tctx)
666 const TwinVQModeTab *mtab = tctx->mtab;
667 int n_ch = tctx->avctx->channels;
668 int total_fr_bits = tctx->avctx->bit_rate * mtab->size /
669 tctx->avctx->sample_rate;
671 int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
672 mtab->lsp_split * mtab->lsp_bit2);
674 int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
675 mtab->ppc_period_bit);
677 int bsize_no_main_cb[3], bse_bits[3], i;
678 enum TwinVQFrameType frametype;
680 for (i = 0; i < 3; i++)
681 // +1 for history usage switch
683 (mtab->fmode[i].bark_n_coef *
684 mtab->fmode[i].bark_n_bit + 1);
686 bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
687 TWINVQ_WINDOW_TYPE_BITS + n_ch * TWINVQ_GAIN_BITS;
689 for (i = 0; i < 2; i++)
690 bsize_no_main_cb[i] =
691 lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
692 TWINVQ_WINDOW_TYPE_BITS +
693 mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
695 if (tctx->codec == TWINVQ_CODEC_METASOUND) {
696 bsize_no_main_cb[1] += 2;
697 bsize_no_main_cb[2] += 2;
700 // The remaining bits are all used for the main spectrum coefficients
701 for (i = 0; i < 4; i++) {
702 int bit_size, vect_size;
703 int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
705 bit_size = n_ch * mtab->ppc_shape_bit;
706 vect_size = n_ch * mtab->ppc_shape_len;
708 bit_size = total_fr_bits - bsize_no_main_cb[i];
709 vect_size = n_ch * mtab->size;
712 tctx->n_div[i] = (bit_size + 13) / 14;
714 rounded_up = (bit_size + tctx->n_div[i] - 1) /
716 rounded_down = (bit_size) / tctx->n_div[i];
717 num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
718 num_rounded_up = tctx->n_div[i] - num_rounded_down;
719 tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;
720 tctx->bits_main_spec[1][i][0] = rounded_up / 2;
721 tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;
722 tctx->bits_main_spec[1][i][1] = rounded_down / 2;
723 tctx->bits_main_spec_change[i] = num_rounded_up;
725 rounded_up = (vect_size + tctx->n_div[i] - 1) /
727 rounded_down = (vect_size) / tctx->n_div[i];
728 num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
729 num_rounded_up = tctx->n_div[i] - num_rounded_down;
730 tctx->length[i][0] = rounded_up;
731 tctx->length[i][1] = rounded_down;
732 tctx->length_change[i] = num_rounded_up;
735 for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
736 construct_perm_table(tctx, frametype);
739 av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
741 TwinVQContext *tctx = avctx->priv_data;
744 for (i = 0; i < 3; i++) {
745 ff_mdct_end(&tctx->mdct_ctx[i]);
746 av_free(tctx->cos_tabs[i]);
749 av_free(tctx->curr_frame);
750 av_free(tctx->spectrum);
751 av_free(tctx->prev_frame);
752 av_free(tctx->tmp_buf);
757 av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
760 TwinVQContext *tctx = avctx->priv_data;
763 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
765 avpriv_float_dsp_init(&tctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
766 if ((ret = init_mdct_win(tctx))) {
767 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
768 ff_twinvq_decode_close(avctx);
771 init_bitstream_params(tctx);
773 twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
774 FF_ARRAY_ELEMS(tctx->bark_hist));