LCOV - code coverage report
Current view: top level - libavcodec - twinvq.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 393 424 92.7 %
Date: 2017-12-18 06:23:41 Functions: 23 23 100.0 %

          Line data    Source code
       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             : 
      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             : 
      34             : /**
      35             :  * Evaluate a single LPC amplitude spectrum envelope coefficient from the line
      36             :  * spectrum pairs.
      37             :  *
      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
      43             :  *
      44             :  * @todo reuse code from Vorbis decoder: vorbis_floor0_decode
      45             :  */
      46      261594 : static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
      47             : {
      48             :     int j;
      49      261594 :     float p         = 0.5f;
      50      261594 :     float q         = 0.5f;
      51      261594 :     float two_cos_w = 2.0f * cos_val;
      52             : 
      53     1307970 :     for (j = 0; j + 1 < order; j += 2 * 2) {
      54             :         // Unroll the loop once since order is a multiple of four
      55     1046376 :         q *= lsp[j]     - two_cos_w;
      56     1046376 :         p *= lsp[j + 1] - two_cos_w;
      57             : 
      58     1046376 :         q *= lsp[j + 2] - two_cos_w;
      59     1046376 :         p *= lsp[j + 3] - two_cos_w;
      60             :     }
      61             : 
      62      261594 :     p *= p * (2.0f - two_cos_w);
      63      261594 :     q *= q * (2.0f + two_cos_w);
      64             : 
      65      261594 :     return 0.5 / (p + q);
      66             : }
      67             : 
      68             : /**
      69             :  * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
      70             :  */
      71           1 : static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
      72             : {
      73             :     int i;
      74           1 :     const TwinVQModeTab *mtab = tctx->mtab;
      75           1 :     int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
      76             : 
      77          65 :     for (i = 0; i < size_s / 2; i++) {
      78          64 :         float cos_i = tctx->cos_tabs[0][i];
      79          64 :         lpc[i]              = eval_lpc_spectrum(cos_vals,  cos_i, mtab->n_lsp);
      80          64 :         lpc[size_s - i - 1] = eval_lpc_spectrum(cos_vals, -cos_i, mtab->n_lsp);
      81             :     }
      82           1 : }
      83             : 
      84      258879 : static void interpolate(float *out, float v1, float v2, int size)
      85             : {
      86             :     int i;
      87      258879 :     float step = (v1 - v2) / (size + 1);
      88             : 
      89     2554283 :     for (i = 0; i < size; i++) {
      90     2295404 :         v2    += step;
      91     2295404 :         out[i] = v2;
      92             :     }
      93      258879 : }
      94             : 
      95      261466 : static inline float get_cos(int idx, int part, const float *cos_tab, int size)
      96             : {
      97       87599 :     return part ? -cos_tab[size - idx - 1]
      98      349065 :                 :  cos_tab[idx];
      99             : }
     100             : 
     101             : /**
     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.
     108             :  *
     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
     114             :  */
     115        5174 : 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        5174 :     const TwinVQModeTab *mtab = tctx->mtab;
     122        5174 :     const float *cos_tab      = tctx->cos_tabs[ftype];
     123             : 
     124             :     // Fill the 's'
     125      264278 :     for (i = 0; i < size; i += step)
     126      518208 :         out[i] =
     127      518208 :             eval_lpc_spectrum(in,
     128             :                               get_cos(i, part, cos_tab, size),
     129      259104 :                               mtab->n_lsp);
     130             : 
     131             :     // Fill the 'iiiibiiii'
     132      253930 :     for (i = step; i <= size - 2 * step; i += step) {
     133      252139 :         if (out[i + step] + out[i - step] > 1.95 * out[i] ||
     134        3383 :             out[i + step]                 >= out[i - step]) {
     135      246394 :             interpolate(out + i - step + 1, out[i], out[i - step], step - 1);
     136             :         } else {
     137        4724 :             out[i - step / 2] =
     138        7086 :                 eval_lpc_spectrum(in,
     139        2362 :                                   get_cos(i - step / 2, part, cos_tab, size),
     140        2362 :                                   mtab->n_lsp);
     141        4724 :             interpolate(out + i - step + 1, out[i - step / 2],
     142        4724 :                         out[i - step], step / 2 - 1);
     143        4724 :             interpolate(out + i - step / 2 + 1, out[i],
     144        4724 :                         out[i - step / 2], step / 2 - 1);
     145             :         }
     146             :     }
     147             : 
     148       10348 :     interpolate(out + size - 2 * step + 1, out[size - step],
     149        5174 :                 out[size - 2 * step], step - 1);
     150        5174 : }
     151             : 
     152        2587 : static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype,
     153             :                                const float *buf, float *lpc,
     154             :                                int size, int step)
     155             : {
     156        2587 :     eval_lpcenv_or_interp(tctx, ftype, lpc, buf, size / 2, step, 0);
     157        2587 :     eval_lpcenv_or_interp(tctx, ftype, lpc + size / 2, buf, size / 2,
     158             :                           2 * step, 1);
     159             : 
     160        2587 :     interpolate(lpc + size / 2 - step + 1, lpc[size / 2],
     161        2587 :                 lpc[size / 2 - step], step);
     162             : 
     163        2587 :     twinvq_memset_float(lpc + size - 2 * step + 1, lpc[size - 2 * step],
     164        2587 :                         2 * step - 1);
     165        2587 : }
     166             : 
     167             : /**
     168             :  * Inverse quantization. Read CB coefficients for cb1 and cb2 from the
     169             :  * bitstream, sum the corresponding vectors and write the result to *out
     170             :  * after permutation.
     171             :  */
     172        5063 : 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        5063 :     int pos = 0;
     177             :     int i, j;
     178             : 
     179      165515 :     for (i = 0; i < tctx->n_div[ftype]; i++) {
     180             :         int tmp0, tmp1;
     181      160452 :         int sign0 = 1;
     182      160452 :         int sign1 = 1;
     183             :         const int16_t *tab0, *tab1;
     184      160452 :         int length = tctx->length[ftype][i >= tctx->length_change[ftype]];
     185      160452 :         int bitstream_second_part = (i >= tctx->bits_main_spec_change[ftype]);
     186             : 
     187      160452 :         int bits = tctx->bits_main_spec[0][ftype][bitstream_second_part];
     188      160452 :         tmp0 = *cb_bits++;
     189      160452 :         if (bits == 7) {
     190      160452 :             if (tmp0 & 0x40)
     191       80716 :                 sign0 = -1;
     192      160452 :             tmp0 &= 0x3F;
     193             :         }
     194             : 
     195      160452 :         bits = tctx->bits_main_spec[1][ftype][bitstream_second_part];
     196      160452 :         tmp1 = *cb_bits++;
     197      160452 :         if (bits == 7) {
     198      129957 :             if (tmp1 & 0x40)
     199       64827 :                 sign1 = -1;
     200      129957 :             tmp1 &= 0x3F;
     201             :         }
     202             : 
     203      160452 :         tab0 = cb0 + tmp0 * cb_len;
     204      160452 :         tab1 = cb1 + tmp1 * cb_len;
     205             : 
     206     2899664 :         for (j = 0; j < length; j++)
     207     5478424 :             out[tctx->permut[ftype][pos + j]] = sign0 * tab0[j] +
     208     2739212 :                                                 sign1 * tab1[j];
     209             : 
     210      160452 :         pos += length;
     211             :     }
     212        5063 : }
     213             : 
     214        2588 : static void dec_gain(TwinVQContext *tctx,
     215             :                      enum TwinVQFrameType ftype, float *out)
     216             : {
     217        2588 :     const TwinVQModeTab   *mtab =  tctx->mtab;
     218        2588 :     const TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
     219             :     int i, j;
     220        2588 :     int sub        = mtab->fmode[ftype].sub;
     221        2588 :     float step     = TWINVQ_AMP_MAX     / ((1 << TWINVQ_GAIN_BITS)     - 1);
     222        2588 :     float sub_step = TWINVQ_SUB_AMP_MAX / ((1 << TWINVQ_SUB_GAIN_BITS) - 1);
     223             : 
     224        2588 :     if (ftype == TWINVQ_FT_LONG) {
     225        4950 :         for (i = 0; i < tctx->avctx->channels; i++)
     226        2475 :             out[i] = (1.0 / (1 << 13)) *
     227        2475 :                      twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
     228             :                                      TWINVQ_AMP_MAX, TWINVQ_MULAW_MU);
     229             :     } else {
     230         226 :         for (i = 0; i < tctx->avctx->channels; i++) {
     231         113 :             float val = (1.0 / (1 << 23)) *
     232         113 :                         twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
     233             :                                         TWINVQ_AMP_MAX, TWINVQ_MULAW_MU);
     234             : 
     235         345 :             for (j = 0; j < sub; j++)
     236         464 :                 out[i * sub + j] =
     237         696 :                     val * twinvq_mulawinv(sub_step * 0.5 +
     238         232 :                                           sub_step * bits->sub_gain_bits[i * sub + j],
     239             :                                           TWINVQ_SUB_AMP_MAX, TWINVQ_MULAW_MU);
     240             :         }
     241             :     }
     242        2588 : }
     243             : 
     244             : /**
     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).
     249             :  */
     250        7764 : static void rearrange_lsp(int order, float *lsp, float min_dist)
     251             : {
     252             :     int i;
     253        7764 :     float min_dist2 = min_dist * 0.5;
     254      124224 :     for (i = 1; i < order; i++)
     255      116460 :         if (lsp[i] - lsp[i - 1] < min_dist) {
     256           0 :             float avg = (lsp[i] + lsp[i - 1]) * 0.5;
     257             : 
     258           0 :             lsp[i - 1] = avg - min_dist2;
     259           0 :             lsp[i]     = avg + min_dist2;
     260             :         }
     261        7764 : }
     262             : 
     263        2588 : static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2,
     264             :                        int lpc_hist_idx, float *lsp, float *hist)
     265             : {
     266        2588 :     const TwinVQModeTab *mtab = tctx->mtab;
     267             :     int i, j;
     268             : 
     269        2588 :     const float *cb  = mtab->lspcodebook;
     270        2588 :     const float *cb2 = cb  + (1 << mtab->lsp_bit1) * mtab->n_lsp;
     271        2588 :     const float *cb3 = cb2 + (1 << mtab->lsp_bit2) * mtab->n_lsp;
     272             : 
     273        7764 :     const int8_t funny_rounding[4] = {
     274             :         -2,
     275        2588 :         mtab->lsp_split == 4 ? -2 : 1,
     276        2588 :         mtab->lsp_split == 4 ? -2 : 1,
     277             :         0
     278             :     };
     279             : 
     280        2588 :     j = 0;
     281       10352 :     for (i = 0; i < mtab->lsp_split; i++) {
     282       15528 :         int chunk_end = ((i + 1) * mtab->n_lsp + funny_rounding[i]) /
     283        7764 :                         mtab->lsp_split;
     284       49172 :         for (; j < chunk_end; j++)
     285       82816 :             lsp[j] = cb[lpc_idx1     * mtab->n_lsp + j] +
     286       41408 :                      cb2[lpc_idx2[i] * mtab->n_lsp + j];
     287             :     }
     288             : 
     289        2588 :     rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
     290             : 
     291       43996 :     for (i = 0; i < mtab->n_lsp; i++) {
     292       41408 :         float tmp1 = 1.0     - cb3[lpc_hist_idx * mtab->n_lsp + i];
     293       41408 :         float tmp2 = hist[i] * cb3[lpc_hist_idx * mtab->n_lsp + i];
     294       41408 :         hist[i] = lsp[i];
     295       41408 :         lsp[i]  = lsp[i] * tmp1 + tmp2;
     296             :     }
     297             : 
     298        2588 :     rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
     299        2588 :     rearrange_lsp(mtab->n_lsp, lsp, 0.000095);
     300        2588 :     ff_sort_nearly_sorted_floats(lsp, mtab->n_lsp);
     301        2588 : }
     302             : 
     303        2588 : static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp,
     304             :                                  enum TwinVQFrameType ftype, float *lpc)
     305             : {
     306             :     int i;
     307        2588 :     int size = tctx->mtab->size / tctx->mtab->fmode[ftype].sub;
     308             : 
     309       43996 :     for (i = 0; i < tctx->mtab->n_lsp; i++)
     310       41408 :         lsp[i] = 2 * cos(lsp[i]);
     311             : 
     312        2588 :     switch (ftype) {
     313        2475 :     case TWINVQ_FT_LONG:
     314        2475 :         eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 8);
     315        2475 :         break;
     316         112 :     case TWINVQ_FT_MEDIUM:
     317         112 :         eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 2);
     318         112 :         break;
     319           1 :     case TWINVQ_FT_SHORT:
     320           1 :         eval_lpcenv(tctx, lsp, lpc);
     321           1 :         break;
     322             :     }
     323        2588 : }
     324             : 
     325             : static const uint8_t wtype_to_wsize[] = { 0, 0, 2, 2, 2, 1, 0, 1, 1 };
     326             : 
     327        2588 : static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype,
     328             :                              int wtype, float *in, float *prev, int ch)
     329             : {
     330        2588 :     FFTContext *mdct = &tctx->mdct_ctx[ftype];
     331        2588 :     const TwinVQModeTab *mtab = tctx->mtab;
     332        2588 :     int bsize = mtab->size / mtab->fmode[ftype].sub;
     333        2588 :     int size  = mtab->size;
     334        2588 :     float *buf1 = tctx->tmp_buf;
     335             :     int j, first_wsize, wsize; // Window size
     336        2588 :     float *out  = tctx->curr_frame + 2 * ch * mtab->size;
     337        2588 :     float *out2 = out;
     338             :     float *prev_buf;
     339        7764 :     int types_sizes[] = {
     340        2588 :         mtab->size /  mtab->fmode[TWINVQ_FT_LONG].sub,
     341        2588 :         mtab->size /  mtab->fmode[TWINVQ_FT_MEDIUM].sub,
     342        2588 :         mtab->size / (mtab->fmode[TWINVQ_FT_SHORT].sub * 2),
     343             :     };
     344             : 
     345        2588 :     wsize       = types_sizes[wtype_to_wsize[wtype]];
     346        2588 :     first_wsize = wsize;
     347        2588 :     prev_buf    = prev + (size - bsize) / 2;
     348             : 
     349        5295 :     for (j = 0; j < mtab->fmode[ftype].sub; j++) {
     350        2707 :         int sub_wtype = ftype == TWINVQ_FT_MEDIUM ? 8 : wtype;
     351             : 
     352        2707 :         if (!j && wtype == 4)
     353           0 :             sub_wtype = 4;
     354        2707 :         else if (j == mtab->fmode[ftype].sub - 1 && wtype == 7)
     355           0 :             sub_wtype = 7;
     356             : 
     357        2707 :         wsize = types_sizes[wtype_to_wsize[sub_wtype]];
     358             : 
     359        2707 :         mdct->imdct_half(mdct, buf1 + bsize * j, in + bsize * j);
     360             : 
     361        5414 :         tctx->fdsp->vector_fmul_window(out2, prev_buf + (bsize - wsize) / 2,
     362        2707 :                                       buf1 + bsize * j,
     363        2707 :                                       ff_sine_windows[av_log2(wsize)],
     364             :                                       wsize / 2);
     365        2707 :         out2 += wsize;
     366             : 
     367        2707 :         memcpy(out2, buf1 + bsize * j + wsize / 2,
     368        2707 :                (bsize - wsize / 2) * sizeof(float));
     369             : 
     370        2707 :         out2 += ftype == TWINVQ_FT_MEDIUM ? (bsize - wsize) / 2 : bsize - wsize;
     371             : 
     372        2707 :         prev_buf = buf1 + bsize * j + bsize / 2;
     373             :     }
     374             : 
     375        2588 :     tctx->last_block_pos[ch] = (size + first_wsize) / 2;
     376        2588 : }
     377             : 
     378        2588 : static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype,
     379             :                          int wtype, float **out, int offset)
     380             : {
     381        2588 :     const TwinVQModeTab *mtab = tctx->mtab;
     382        2588 :     float *prev_buf           = tctx->prev_frame + tctx->last_block_pos[0];
     383             :     int size1, size2, i;
     384             :     float *out1, *out2;
     385             : 
     386        5176 :     for (i = 0; i < tctx->avctx->channels; i++)
     387        7764 :         imdct_and_window(tctx, ftype, wtype,
     388        2588 :                          tctx->spectrum + i * mtab->size,
     389        2588 :                          prev_buf + 2 * i * mtab->size,
     390             :                          i);
     391             : 
     392        2588 :     if (!out)
     393           2 :         return;
     394             : 
     395        2586 :     size2 = tctx->last_block_pos[0];
     396        2586 :     size1 = mtab->size - size2;
     397             : 
     398        2586 :     out1 = &out[0][0] + offset;
     399        2586 :     memcpy(out1,         prev_buf,         size1 * sizeof(*out1));
     400        2586 :     memcpy(out1 + size1, tctx->curr_frame, size2 * sizeof(*out1));
     401             : 
     402        2586 :     if (tctx->avctx->channels == 2) {
     403           0 :         out2 = &out[1][0] + offset;
     404           0 :         memcpy(out2, &prev_buf[2 * mtab->size],
     405             :                size1 * sizeof(*out2));
     406           0 :         memcpy(out2 + size1, &tctx->curr_frame[2 * mtab->size],
     407             :                size2 * sizeof(*out2));
     408           0 :         tctx->fdsp->butterflies_float(out1, out2, mtab->size);
     409             :     }
     410             : }
     411             : 
     412        2588 : static void read_and_decode_spectrum(TwinVQContext *tctx, float *out,
     413             :                                      enum TwinVQFrameType ftype)
     414             : {
     415        2588 :     const TwinVQModeTab *mtab = tctx->mtab;
     416        2588 :     TwinVQFrameData *bits     = &tctx->bits[tctx->cur_frame];
     417        2588 :     int channels              = tctx->avctx->channels;
     418        2588 :     int sub        = mtab->fmode[ftype].sub;
     419        2588 :     int block_size = mtab->size / sub;
     420             :     float gain[TWINVQ_CHANNELS_MAX * TWINVQ_SUBBLOCKS_MAX];
     421             :     float ppc_shape[TWINVQ_PPC_SHAPE_LEN_MAX * TWINVQ_CHANNELS_MAX * 4];
     422             : 
     423             :     int i, j;
     424             : 
     425        2588 :     dequant(tctx, bits->main_coeffs, out, ftype,
     426             :             mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
     427        2588 :             mtab->fmode[ftype].cb_len_read);
     428             : 
     429        2588 :     dec_gain(tctx, ftype, gain);
     430             : 
     431        2588 :     if (ftype == TWINVQ_FT_LONG) {
     432        4950 :         int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len * channels - 1) /
     433        2475 :                        tctx->n_div[3];
     434        2475 :         dequant(tctx, bits->ppc_coeffs, ppc_shape,
     435             :                 TWINVQ_FT_PPC, mtab->ppc_shape_cb,
     436        2475 :                 mtab->ppc_shape_cb + cb_len_p * TWINVQ_PPC_SHAPE_CB_SIZE,
     437             :                 cb_len_p);
     438             :     }
     439             : 
     440        5176 :     for (i = 0; i < channels; i++) {
     441        2588 :         float *chunk = out + mtab->size * i;
     442             :         float lsp[TWINVQ_LSP_COEFS_MAX];
     443             : 
     444        5295 :         for (j = 0; j < sub; j++) {
     445        8121 :             tctx->dec_bark_env(tctx, bits->bark1[i][j],
     446        2707 :                                bits->bark_use_hist[i][j], i,
     447        2707 :                                tctx->tmp_buf, gain[sub * i + j], ftype);
     448             : 
     449        5414 :             tctx->fdsp->vector_fmul(chunk + block_size * j,
     450        2707 :                                    chunk + block_size * j,
     451        2707 :                                    tctx->tmp_buf, block_size);
     452             :         }
     453             : 
     454        2588 :         if (ftype == TWINVQ_FT_LONG)
     455        4950 :             tctx->decode_ppc(tctx, bits->p_coef[i], bits->g_coef[i],
     456        2475 :                              ppc_shape + i * mtab->ppc_shape_len, chunk);
     457             : 
     458        2588 :         decode_lsp(tctx, bits->lpc_idx1[i], bits->lpc_idx2[i],
     459        2588 :                    bits->lpc_hist_idx[i], lsp, tctx->lsp_hist[i]);
     460             : 
     461        2588 :         dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
     462             : 
     463        5295 :         for (j = 0; j < mtab->fmode[ftype].sub; j++) {
     464        2707 :             tctx->fdsp->vector_fmul(chunk, chunk, tctx->tmp_buf, block_size);
     465        2707 :             chunk += block_size;
     466             :         }
     467             :     }
     468        2588 : }
     469             : 
     470             : const enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[] = {
     471             :     TWINVQ_FT_LONG,   TWINVQ_FT_LONG, TWINVQ_FT_SHORT, TWINVQ_FT_LONG,
     472             :     TWINVQ_FT_MEDIUM, TWINVQ_FT_LONG, TWINVQ_FT_LONG,  TWINVQ_FT_MEDIUM,
     473             :     TWINVQ_FT_MEDIUM
     474             : };
     475             : 
     476        2588 : int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
     477             :                            int *got_frame_ptr, AVPacket *avpkt)
     478             : {
     479        2588 :     AVFrame *frame     = data;
     480        2588 :     const uint8_t *buf = avpkt->data;
     481        2588 :     int buf_size       = avpkt->size;
     482        2588 :     TwinVQContext *tctx = avctx->priv_data;
     483        2588 :     const TwinVQModeTab *mtab = tctx->mtab;
     484        2588 :     float **out = NULL;
     485             :     int ret;
     486             : 
     487             :     /* get output buffer */
     488        2588 :     if (tctx->discarded_packets >= 2) {
     489        2586 :         frame->nb_samples = mtab->size * tctx->frames_per_packet;
     490        2586 :         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     491           0 :             return ret;
     492        2586 :         out = (float **)frame->extended_data;
     493             :     }
     494             : 
     495        2588 :     if (buf_size < avctx->block_align) {
     496           0 :         av_log(avctx, AV_LOG_ERROR,
     497             :                "Frame too small (%d bytes). Truncated file?\n", buf_size);
     498           0 :         return AVERROR(EINVAL);
     499             :     }
     500             : 
     501        2588 :     if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
     502           0 :         return ret;
     503             : 
     504        7764 :     for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
     505        2588 :          tctx->cur_frame++) {
     506        2588 :         read_and_decode_spectrum(tctx, tctx->spectrum,
     507        2588 :                                  tctx->bits[tctx->cur_frame].ftype);
     508             : 
     509        5176 :         imdct_output(tctx, tctx->bits[tctx->cur_frame].ftype,
     510        2588 :                      tctx->bits[tctx->cur_frame].window_type, out,
     511        2588 :                      tctx->cur_frame * mtab->size);
     512             : 
     513        2588 :         FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
     514             :     }
     515             : 
     516        2588 :     if (tctx->discarded_packets < 2) {
     517           2 :         tctx->discarded_packets++;
     518           2 :         *got_frame_ptr = 0;
     519           2 :         return buf_size;
     520             :     }
     521             : 
     522        2586 :     *got_frame_ptr = 1;
     523             : 
     524             :     // VQF can deliver packets 1 byte greater than block align
     525        2586 :     if (buf_size == avctx->block_align + 1)
     526        2586 :         return buf_size;
     527           0 :     return avctx->block_align;
     528             : }
     529             : 
     530             : /**
     531             :  * Init IMDCT and windowing tables
     532             :  */
     533           3 : static av_cold int init_mdct_win(TwinVQContext *tctx)
     534             : {
     535             :     int i, j, ret;
     536           3 :     const TwinVQModeTab *mtab = tctx->mtab;
     537           3 :     int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
     538           3 :     int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
     539           3 :     int channels = tctx->avctx->channels;
     540           3 :     float norm = channels == 1 ? 2.0 : 1.0;
     541             : 
     542          12 :     for (i = 0; i < 3; i++) {
     543           9 :         int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
     544           9 :         if ((ret = ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
     545           9 :                                 -sqrt(norm / bsize) / (1 << 15))))
     546           0 :             return ret;
     547             :     }
     548             : 
     549           3 :     FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->tmp_buf,
     550             :                      mtab->size, sizeof(*tctx->tmp_buf), alloc_fail);
     551             : 
     552           3 :     FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->spectrum,
     553             :                      2 * mtab->size, channels * sizeof(*tctx->spectrum),
     554             :                      alloc_fail);
     555           3 :     FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->curr_frame,
     556             :                      2 * mtab->size, channels * sizeof(*tctx->curr_frame),
     557             :                      alloc_fail);
     558           3 :     FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->prev_frame,
     559             :                      2 * mtab->size, channels * sizeof(*tctx->prev_frame),
     560             :                      alloc_fail);
     561             : 
     562          12 :     for (i = 0; i < 3; i++) {
     563           9 :         int m       = 4 * mtab->size / mtab->fmode[i].sub;
     564           9 :         double freq = 2 * M_PI / m;
     565           9 :         FF_ALLOC_ARRAY_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
     566             :                          (m / 4), sizeof(*tctx->cos_tabs[i]), alloc_fail);
     567             : 
     568        2514 :         for (j = 0; j <= m / 8; j++)
     569        2505 :             tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
     570        2496 :         for (j = 1; j < m / 8; j++)
     571        2487 :             tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
     572             :     }
     573             : 
     574           3 :     ff_init_ff_sine_windows(av_log2(size_m));
     575           3 :     ff_init_ff_sine_windows(av_log2(size_s / 2));
     576           3 :     ff_init_ff_sine_windows(av_log2(mtab->size));
     577             : 
     578           3 :     return 0;
     579             : 
     580           0 : alloc_fail:
     581           0 :     return AVERROR(ENOMEM);
     582             : }
     583             : 
     584             : /**
     585             :  * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
     586             :  * each line do a cyclic permutation, i.e.
     587             :  * abcdefghijklm -> defghijklmabc
     588             :  * where the amount to be shifted is evaluated depending on the column.
     589             :  */
     590          12 : static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
     591             :                               int block_size,
     592             :                               const uint8_t line_len[2], int length_div,
     593             :                               enum TwinVQFrameType ftype)
     594             : {
     595             :     int i, j;
     596             : 
     597         198 :     for (i = 0; i < line_len[0]; i++) {
     598             :         int shift;
     599             : 
     600         186 :         if (num_blocks == 1                                    ||
     601           0 :             (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
     602         210 :             (ftype != TWINVQ_FT_LONG && num_vect & 1)          ||
     603         105 :             i == line_len[1]) {
     604          87 :             shift = 0;
     605          99 :         } else if (ftype == TWINVQ_FT_LONG) {
     606           0 :             shift = i;
     607             :         } else
     608          99 :             shift = i * i;
     609             : 
     610        9510 :         for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
     611        9324 :             tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
     612             :     }
     613          12 : }
     614             : 
     615             : /**
     616             :  * Interpret the input data as in the following table:
     617             :  *
     618             :  * @verbatim
     619             :  *
     620             :  * abcdefgh
     621             :  * ijklmnop
     622             :  * qrstuvw
     623             :  * x123456
     624             :  *
     625             :  * @endverbatim
     626             :  *
     627             :  * and transpose it, giving the output
     628             :  * aiqxbjr1cks2dlt3emu4fvn5gow6hp
     629             :  */
     630          12 : static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
     631             :                            const uint8_t line_len[2], int length_div)
     632             : {
     633             :     int i, j;
     634          12 :     int cont = 0;
     635             : 
     636         558 :     for (i = 0; i < num_vect; i++)
     637        9870 :         for (j = 0; j < line_len[i >= length_div]; j++)
     638        9324 :             out[cont++] = in[j * num_vect + i];
     639          12 : }
     640             : 
     641          12 : static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
     642             : {
     643          12 :     int block_size = size / n_blocks;
     644             :     int i;
     645             : 
     646        9336 :     for (i = 0; i < size; i++)
     647        9324 :         out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
     648          12 : }
     649             : 
     650          12 : static av_cold void construct_perm_table(TwinVQContext *tctx,
     651             :                                          enum TwinVQFrameType ftype)
     652             : {
     653             :     int block_size, size;
     654          12 :     const TwinVQModeTab *mtab = tctx->mtab;
     655          12 :     int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
     656             : 
     657          12 :     if (ftype == TWINVQ_FT_PPC) {
     658           3 :         size       = tctx->avctx->channels;
     659           3 :         block_size = mtab->ppc_shape_len;
     660             :     } else {
     661           9 :         size       = tctx->avctx->channels * mtab->fmode[ftype].sub;
     662           9 :         block_size = mtab->size / mtab->fmode[ftype].sub;
     663             :     }
     664             : 
     665          24 :     permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
     666          12 :                       block_size, tctx->length[ftype],
     667          12 :                       tctx->length_change[ftype], ftype);
     668             : 
     669          24 :     transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
     670          24 :                    tctx->length[ftype], tctx->length_change[ftype]);
     671             : 
     672          12 :     linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
     673             :                 size * block_size);
     674          12 : }
     675             : 
     676           3 : static av_cold void init_bitstream_params(TwinVQContext *tctx)
     677             : {
     678           3 :     const TwinVQModeTab *mtab = tctx->mtab;
     679           3 :     int n_ch                  = tctx->avctx->channels;
     680           6 :     int total_fr_bits         = tctx->avctx->bit_rate * mtab->size /
     681           3 :                                 tctx->avctx->sample_rate;
     682             : 
     683           6 :     int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
     684           3 :                                      mtab->lsp_split * mtab->lsp_bit2);
     685             : 
     686           6 :     int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
     687           3 :                            mtab->ppc_period_bit);
     688             : 
     689             :     int bsize_no_main_cb[3], bse_bits[3], i;
     690             :     enum TwinVQFrameType frametype;
     691             : 
     692          12 :     for (i = 0; i < 3; i++)
     693             :         // +1 for history usage switch
     694           9 :         bse_bits[i] = n_ch *
     695          18 :                       (mtab->fmode[i].bark_n_coef *
     696          18 :                        mtab->fmode[i].bark_n_bit + 1);
     697             : 
     698           6 :     bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
     699           3 :                           TWINVQ_WINDOW_TYPE_BITS + n_ch * TWINVQ_GAIN_BITS;
     700             : 
     701           9 :     for (i = 0; i < 2; i++)
     702           6 :         bsize_no_main_cb[i] =
     703           6 :             lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
     704           6 :             TWINVQ_WINDOW_TYPE_BITS +
     705           6 :             mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
     706             : 
     707           3 :     if (tctx->codec == TWINVQ_CODEC_METASOUND && !tctx->is_6kbps) {
     708           0 :         bsize_no_main_cb[1] += 2;
     709           0 :         bsize_no_main_cb[2] += 2;
     710             :     }
     711             : 
     712             :     // The remaining bits are all used for the main spectrum coefficients
     713          15 :     for (i = 0; i < 4; i++) {
     714             :         int bit_size, vect_size;
     715             :         int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
     716          12 :         if (i == 3) {
     717           3 :             bit_size  = n_ch * mtab->ppc_shape_bit;
     718           3 :             vect_size = n_ch * mtab->ppc_shape_len;
     719             :         } else {
     720           9 :             bit_size  = total_fr_bits - bsize_no_main_cb[i];
     721           9 :             vect_size = n_ch * mtab->size;
     722             :         }
     723             : 
     724          12 :         tctx->n_div[i] = (bit_size + 13) / 14;
     725             : 
     726          24 :         rounded_up                     = (bit_size + tctx->n_div[i] - 1) /
     727          12 :                                          tctx->n_div[i];
     728          12 :         rounded_down                   = (bit_size) / tctx->n_div[i];
     729          12 :         num_rounded_down               = rounded_up * tctx->n_div[i] - bit_size;
     730          12 :         num_rounded_up                 = tctx->n_div[i] - num_rounded_down;
     731          12 :         tctx->bits_main_spec[0][i][0]  = (rounded_up + 1)   / 2;
     732          12 :         tctx->bits_main_spec[1][i][0]  =  rounded_up        / 2;
     733          12 :         tctx->bits_main_spec[0][i][1]  = (rounded_down + 1) / 2;
     734          12 :         tctx->bits_main_spec[1][i][1]  =  rounded_down      / 2;
     735          12 :         tctx->bits_main_spec_change[i] = num_rounded_up;
     736             : 
     737          24 :         rounded_up             = (vect_size + tctx->n_div[i] - 1) /
     738          12 :                                  tctx->n_div[i];
     739          12 :         rounded_down           = (vect_size) / tctx->n_div[i];
     740          12 :         num_rounded_down       = rounded_up * tctx->n_div[i] - vect_size;
     741          12 :         num_rounded_up         = tctx->n_div[i] - num_rounded_down;
     742          12 :         tctx->length[i][0]     = rounded_up;
     743          12 :         tctx->length[i][1]     = rounded_down;
     744          12 :         tctx->length_change[i] = num_rounded_up;
     745             :     }
     746             : 
     747          15 :     for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
     748          12 :         construct_perm_table(tctx, frametype);
     749           3 : }
     750             : 
     751           3 : av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
     752             : {
     753           3 :     TwinVQContext *tctx = avctx->priv_data;
     754             :     int i;
     755             : 
     756          12 :     for (i = 0; i < 3; i++) {
     757           9 :         ff_mdct_end(&tctx->mdct_ctx[i]);
     758           9 :         av_freep(&tctx->cos_tabs[i]);
     759             :     }
     760             : 
     761           3 :     av_freep(&tctx->curr_frame);
     762           3 :     av_freep(&tctx->spectrum);
     763           3 :     av_freep(&tctx->prev_frame);
     764           3 :     av_freep(&tctx->tmp_buf);
     765           3 :     av_freep(&tctx->fdsp);
     766             : 
     767           3 :     return 0;
     768             : }
     769             : 
     770           3 : av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
     771             : {
     772             :     int ret;
     773           3 :     TwinVQContext *tctx = avctx->priv_data;
     774             : 
     775           3 :     tctx->avctx       = avctx;
     776           3 :     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
     777             : 
     778           3 :     if (!avctx->block_align) {
     779           2 :         avctx->block_align = tctx->frame_size + 7 >> 3;
     780           1 :     } else if (avctx->block_align * 8 < tctx->frame_size) {
     781           0 :         av_log(avctx, AV_LOG_ERROR, "Block align is %d bits, expected %d\n",
     782           0 :                avctx->block_align * 8, tctx->frame_size);
     783           0 :         return AVERROR_INVALIDDATA;
     784             :     }
     785           3 :     tctx->frames_per_packet = avctx->block_align * 8 / tctx->frame_size;
     786           3 :     if (tctx->frames_per_packet > TWINVQ_MAX_FRAMES_PER_PACKET) {
     787           0 :         av_log(avctx, AV_LOG_ERROR, "Too many frames per packet (%d)\n",
     788             :                tctx->frames_per_packet);
     789           0 :         return AVERROR_INVALIDDATA;
     790             :     }
     791             : 
     792           3 :     tctx->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     793           3 :     if (!tctx->fdsp) {
     794           0 :         ff_twinvq_decode_close(avctx);
     795           0 :         return AVERROR(ENOMEM);
     796             :     }
     797           3 :     if ((ret = init_mdct_win(tctx))) {
     798           0 :         av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
     799           0 :         ff_twinvq_decode_close(avctx);
     800           0 :         return ret;
     801             :     }
     802           3 :     init_bitstream_params(tctx);
     803             : 
     804           3 :     twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
     805             :                         FF_ARRAY_ELEMS(tctx->bark_hist));
     806             : 
     807           3 :     return 0;
     808             : }

Generated by: LCOV version 1.13