LCOV - code coverage report
Current view: top level - libavcodec - opus_celt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 313 335 93.4 %
Date: 2018-02-24 03:21:38 Functions: 14 14 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Andrew D'Addesio
       3             :  * Copyright (c) 2013-2014 Mozilla Corporation
       4             :  * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Opus CELT decoder
      26             :  */
      27             : 
      28             : #include "opus_celt.h"
      29             : #include "opustab.h"
      30             : #include "opus_pvq.h"
      31             : 
      32             : /* Use the 2D z-transform to apply prediction in both the time domain (alpha)
      33             :  * and the frequency domain (beta) */
      34       29869 : static void celt_decode_coarse_energy(CeltFrame *f, OpusRangeCoder *rc)
      35             : {
      36             :     int i, j;
      37       29869 :     float prev[2] = { 0 };
      38       29869 :     float alpha = ff_celt_alpha_coef[f->size];
      39       29869 :     float beta  = ff_celt_beta_coef[f->size];
      40       29869 :     const uint8_t *model = ff_celt_coarse_energy_dist[f->size][0];
      41             : 
      42             :     /* intra frame */
      43       29869 :     if (opus_rc_tell(rc) + 3 <= f->framebits && ff_opus_rc_dec_log(rc, 3)) {
      44        5873 :         alpha = 0.0f;
      45        5873 :         beta  = 1.0f - (4915.0f/32768.0f);
      46        5873 :         model = ff_celt_coarse_energy_dist[f->size][1];
      47             :     }
      48             : 
      49      657118 :     for (i = 0; i < CELT_MAX_BANDS; i++) {
      50     1647849 :         for (j = 0; j < f->channels; j++) {
      51     1020600 :             CeltBlock *block = &f->block[j];
      52             :             float value;
      53             :             int available;
      54             : 
      55     1020600 :             if (i < f->start_band || i >= f->end_band) {
      56      170908 :                 block->energy[i] = 0.0;
      57      170908 :                 continue;
      58             :             }
      59             : 
      60      849692 :             available = f->framebits - opus_rc_tell(rc);
      61      849692 :             if (available >= 15) {
      62             :                 /* decode using a Laplace distribution */
      63      845898 :                 int k = FFMIN(i, 20) << 1;
      64      845898 :                 value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6);
      65        3794 :             } else if (available >= 2) {
      66         113 :                 int x = ff_opus_rc_dec_cdf(rc, ff_celt_model_energy_small);
      67         113 :                 value = (x>>1) ^ -(x&1);
      68        3681 :             } else if (available >= 1) {
      69           3 :                 value = -(float)ff_opus_rc_dec_log(rc, 1);
      70        3678 :             } else value = -1;
      71             : 
      72      849692 :             block->energy[i] = FFMAX(-9.0f, block->energy[i]) * alpha + prev[j] + value;
      73      849692 :             prev[j] += beta * value;
      74             :         }
      75             :     }
      76       29869 : }
      77             : 
      78       29869 : static void celt_decode_fine_energy(CeltFrame *f, OpusRangeCoder *rc)
      79             : {
      80             :     int i;
      81      537903 :     for (i = f->start_band; i < f->end_band; i++) {
      82             :         int j;
      83      508034 :         if (!f->fine_bits[i])
      84       95330 :             continue;
      85             : 
      86     1094007 :         for (j = 0; j < f->channels; j++) {
      87      681303 :             CeltBlock *block = &f->block[j];
      88             :             int q2;
      89             :             float offset;
      90      681303 :             q2 = ff_opus_rc_get_raw(rc, f->fine_bits[i]);
      91      681303 :             offset = (q2 + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f - 0.5f;
      92      681303 :             block->energy[i] += offset;
      93             :         }
      94             :     }
      95       29869 : }
      96             : 
      97       29869 : static void celt_decode_final_energy(CeltFrame *f, OpusRangeCoder *rc)
      98             : {
      99             :     int priority, i, j;
     100       29869 :     int bits_left = f->framebits - opus_rc_tell(rc);
     101             : 
     102       89607 :     for (priority = 0; priority < 2; priority++) {
     103      162460 :         for (i = f->start_band; i < f->end_band && bits_left >= f->channels; i++) {
     104      102722 :             if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS)
     105       65402 :                 continue;
     106             : 
     107       96124 :             for (j = 0; j < f->channels; j++) {
     108             :                 int q2;
     109             :                 float offset;
     110       58804 :                 q2 = ff_opus_rc_get_raw(rc, 1);
     111       58804 :                 offset = (q2 - 0.5f) * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f;
     112       58804 :                 f->block[j].energy[i] += offset;
     113       58804 :                 bits_left--;
     114             :             }
     115             :         }
     116             :     }
     117       29869 : }
     118             : 
     119       29869 : static void celt_decode_tf_changes(CeltFrame *f, OpusRangeCoder *rc)
     120             : {
     121       29869 :     int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit;
     122       29869 :     int consumed, bits = f->transient ? 2 : 4;
     123             : 
     124       29869 :     consumed = opus_rc_tell(rc);
     125       29869 :     tf_select_bit = (f->size != 0 && consumed+bits+1 <= f->framebits);
     126             : 
     127      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     128      508034 :         if (consumed+bits+tf_select_bit <= f->framebits) {
     129      505558 :             diff ^= ff_opus_rc_dec_log(rc, bits);
     130      505558 :             consumed = opus_rc_tell(rc);
     131      505558 :             tf_changed |= diff;
     132             :         }
     133      508034 :         f->tf_change[i] = diff;
     134      508034 :         bits = f->transient ? 4 : 5;
     135             :     }
     136             : 
     137       45940 :     if (tf_select_bit && ff_celt_tf_select[f->size][f->transient][0][tf_changed] !=
     138       16071 :                          ff_celt_tf_select[f->size][f->transient][1][tf_changed])
     139        8088 :         tf_select = ff_opus_rc_dec_log(rc, 1);
     140             : 
     141      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     142      508034 :         f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]];
     143             :     }
     144       29869 : }
     145             : 
     146       48600 : static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
     147             : {
     148             :     int i, j;
     149             : 
     150      898292 :     for (i = f->start_band; i < f->end_band; i++) {
     151      849692 :         float *dst = data + (ff_celt_freq_bands[i] << f->size);
     152      849692 :         float log_norm = block->energy[i] + ff_celt_mean_energy[i];
     153      849692 :         float norm = exp2f(FFMIN(log_norm, 32.0f));
     154             : 
     155    13154552 :         for (j = 0; j < ff_celt_freq_range[i] << f->size; j++)
     156    12304860 :             dst[j] *= norm;
     157             :     }
     158       48600 : }
     159             : 
     160       89012 : static void celt_postfilter_apply_transition(CeltBlock *block, float *data)
     161             : {
     162       89012 :     const int T0 = block->pf_period_old;
     163       89012 :     const int T1 = block->pf_period;
     164             : 
     165             :     float g00, g01, g02;
     166             :     float g10, g11, g12;
     167             : 
     168             :     float x0, x1, x2, x3, x4;
     169             : 
     170             :     int i;
     171             : 
     172      130582 :     if (block->pf_gains[0]     == 0.0 &&
     173       41570 :         block->pf_gains_old[0] == 0.0)
     174       37390 :         return;
     175             : 
     176       51622 :     g00 = block->pf_gains_old[0];
     177       51622 :     g01 = block->pf_gains_old[1];
     178       51622 :     g02 = block->pf_gains_old[2];
     179       51622 :     g10 = block->pf_gains[0];
     180       51622 :     g11 = block->pf_gains[1];
     181       51622 :     g12 = block->pf_gains[2];
     182             : 
     183       51622 :     x1 = data[-T1 + 1];
     184       51622 :     x2 = data[-T1];
     185       51622 :     x3 = data[-T1 - 1];
     186       51622 :     x4 = data[-T1 - 2];
     187             : 
     188     6246262 :     for (i = 0; i < CELT_OVERLAP; i++) {
     189     6194640 :         float w = ff_celt_window2[i];
     190     6194640 :         x0 = data[i - T1 + 2];
     191             : 
     192    18583920 :         data[i] +=  (1.0 - w) * g00 * data[i - T0]                          +
     193    12389280 :                     (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
     194    12389280 :                     (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
     195    12389280 :                     w         * g10 * x2                                    +
     196    12389280 :                     w         * g11 * (x1 + x3)                             +
     197     6194640 :                     w         * g12 * (x0 + x4);
     198     6194640 :         x4 = x3;
     199     6194640 :         x3 = x2;
     200     6194640 :         x2 = x1;
     201     6194640 :         x1 = x0;
     202             :     }
     203             : }
     204             : 
     205       32334 : static void celt_postfilter_apply(CeltBlock *block, float *data, int len)
     206             : {
     207       32334 :     const int T = block->pf_period;
     208             :     float g0, g1, g2;
     209             :     float x0, x1, x2, x3, x4;
     210             :     int i;
     211             : 
     212       32334 :     if (block->pf_gains[0] == 0.0 || len <= 0)
     213       22210 :         return;
     214             : 
     215       10124 :     g0 = block->pf_gains[0];
     216       10124 :     g1 = block->pf_gains[1];
     217       10124 :     g2 = block->pf_gains[2];
     218             : 
     219       10124 :     x4 = data[-T - 2];
     220       10124 :     x3 = data[-T - 1];
     221       10124 :     x2 = data[-T];
     222       10124 :     x1 = data[-T + 1];
     223             : 
     224     5737484 :     for (i = 0; i < len; i++) {
     225     5727360 :         x0 = data[i - T + 2];
     226    17182080 :         data[i] += g0 * x2        +
     227    11454720 :                    g1 * (x1 + x3) +
     228     5727360 :                    g2 * (x0 + x4);
     229     5727360 :         x4 = x3;
     230     5727360 :         x3 = x2;
     231     5727360 :         x2 = x1;
     232     5727360 :         x1 = x0;
     233             :     }
     234             : }
     235             : 
     236       56678 : static void celt_postfilter(CeltFrame *f, CeltBlock *block)
     237             : {
     238       56678 :     int len = f->blocksize * f->blocks;
     239             : 
     240       56678 :     celt_postfilter_apply_transition(block, block->buf + 1024);
     241             : 
     242       56678 :     block->pf_period_old = block->pf_period;
     243       56678 :     memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
     244             : 
     245       56678 :     block->pf_period = block->pf_period_new;
     246       56678 :     memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains));
     247             : 
     248       56678 :     if (len > CELT_OVERLAP) {
     249       32334 :         celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP);
     250       32334 :         celt_postfilter_apply(block, block->buf + 1024 + 2 * CELT_OVERLAP,
     251             :                               len - 2 * CELT_OVERLAP);
     252             : 
     253       32334 :         block->pf_period_old = block->pf_period;
     254       32334 :         memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
     255             :     }
     256             : 
     257       56678 :     memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
     258       56678 : }
     259             : 
     260       29869 : static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
     261             : {
     262             :     int i;
     263             : 
     264       29869 :     memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new));
     265       29869 :     memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new));
     266             : 
     267       29869 :     if (f->start_band == 0 && consumed + 16 <= f->framebits) {
     268       24809 :         int has_postfilter = ff_opus_rc_dec_log(rc, 1);
     269       24809 :         if (has_postfilter) {
     270             :             float gain;
     271             :             int tapset, octave, period;
     272             : 
     273       15873 :             octave = ff_opus_rc_dec_uint(rc, 6);
     274       15873 :             period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
     275       15873 :             gain   = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
     276       31746 :             tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ?
     277       15873 :                      ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0;
     278             : 
     279       47619 :             for (i = 0; i < 2; i++) {
     280       31746 :                 CeltBlock *block = &f->block[i];
     281             : 
     282       31746 :                 block->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD);
     283       31746 :                 block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0];
     284       31746 :                 block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1];
     285       31746 :                 block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2];
     286             :             }
     287             :         }
     288             : 
     289       24809 :         consumed = opus_rc_tell(rc);
     290             :     }
     291             : 
     292       29869 :     return consumed;
     293             : }
     294             : 
     295        2750 : static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
     296             : {
     297             :     int i, j, k;
     298             : 
     299       37875 :     for (i = f->start_band; i < f->end_band; i++) {
     300       35125 :         int renormalize = 0;
     301             :         float *xptr;
     302             :         float prev[2];
     303             :         float Ediff, r;
     304             :         float thresh, sqrt_1;
     305             :         int depth;
     306             : 
     307             :         /* depth in 1/8 bits */
     308       35125 :         depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size);
     309       35125 :         thresh = exp2f(-1.0 - 0.125f * depth);
     310       35125 :         sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size);
     311             : 
     312       35125 :         xptr = X + (ff_celt_freq_bands[i] << f->size);
     313             : 
     314       35125 :         prev[0] = block->prev_energy[0][i];
     315       35125 :         prev[1] = block->prev_energy[1][i];
     316       35125 :         if (f->channels == 1) {
     317        9975 :             CeltBlock *block1 = &f->block[1];
     318             : 
     319        9975 :             prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]);
     320        9975 :             prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]);
     321             :         }
     322       35125 :         Ediff = block->energy[i] - FFMIN(prev[0], prev[1]);
     323       35125 :         Ediff = FFMAX(0, Ediff);
     324             : 
     325             :         /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
     326             :         short blocks don't have the same energy as long */
     327       35125 :         r = exp2f(1 - Ediff);
     328       35125 :         if (f->size == 3)
     329       23564 :             r *= M_SQRT2;
     330       35125 :         r = FFMIN(thresh, r) * sqrt_1;
     331      269881 :         for (k = 0; k < 1 << f->size; k++) {
     332             :             /* Detect collapse */
     333      234756 :             if (!(block->collapse_masks[i] & 1 << k)) {
     334             :                 /* Fill with noise */
     335      115623 :                 for (j = 0; j < ff_celt_freq_range[i]; j++)
     336       95928 :                     xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r;
     337       19695 :                 renormalize = 1;
     338             :             }
     339             :         }
     340             : 
     341             :         /* We just added some energy, so we need to renormalize */
     342       35125 :         if (renormalize)
     343        8458 :             celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f);
     344             :     }
     345        2750 : }
     346             : 
     347       29869 : int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc,
     348             :                          float **output, int channels, int frame_size,
     349             :                          int start_band,  int end_band)
     350             : {
     351       29869 :     int i, j, downmix = 0;
     352             :     int consumed;           // bits of entropy consumed thus far for this frame
     353             :     MDCT15Context *imdct;
     354             : 
     355       29869 :     if (channels != 1 && channels != 2) {
     356           0 :         av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
     357             :                channels);
     358           0 :         return AVERROR_INVALIDDATA;
     359             :     }
     360       29869 :     if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) {
     361           0 :         av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
     362             :                start_band, end_band);
     363           0 :         return AVERROR_INVALIDDATA;
     364             :     }
     365             : 
     366       29869 :     f->silence        = 0;
     367       29869 :     f->transient      = 0;
     368       29869 :     f->anticollapse   = 0;
     369       29869 :     f->flushed        = 0;
     370       29869 :     f->channels       = channels;
     371       29869 :     f->start_band     = start_band;
     372       29869 :     f->end_band       = end_band;
     373       29869 :     f->framebits      = rc->rb.bytes * 8;
     374             : 
     375       29869 :     f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
     376       59738 :     if (f->size > CELT_MAX_LOG_BLOCKS ||
     377       29869 :         frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) {
     378           0 :         av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
     379             :                frame_size);
     380           0 :         return AVERROR_INVALIDDATA;
     381             :     }
     382             : 
     383       29869 :     if (!f->output_channels)
     384           0 :         f->output_channels = channels;
     385             : 
     386       78469 :     for (i = 0; i < f->channels; i++) {
     387       48600 :         memset(f->block[i].coeffs,         0, sizeof(f->block[i].coeffs));
     388       48600 :         memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks));
     389             :     }
     390             : 
     391       29869 :     consumed = opus_rc_tell(rc);
     392             : 
     393             :     /* obtain silence flag */
     394       29869 :     if (consumed >= f->framebits)
     395           0 :         f->silence = 1;
     396       29869 :     else if (consumed == 1)
     397       24934 :         f->silence = ff_opus_rc_dec_log(rc, 15);
     398             : 
     399             : 
     400       29869 :     if (f->silence) {
     401         125 :         consumed = f->framebits;
     402         125 :         rc->total_bits += f->framebits - opus_rc_tell(rc);
     403             :     }
     404             : 
     405             :     /* obtain post-filter options */
     406       29869 :     consumed = parse_postfilter(f, rc, consumed);
     407             : 
     408             :     /* obtain transient flag */
     409       29869 :     if (f->size != 0 && consumed+3 <= f->framebits)
     410       16079 :         f->transient = ff_opus_rc_dec_log(rc, 3);
     411             : 
     412       29869 :     f->blocks    = f->transient ? 1 << f->size : 1;
     413       29869 :     f->blocksize = frame_size / f->blocks;
     414             : 
     415       29869 :     imdct = f->imdct[f->transient ? 0 : f->size];
     416             : 
     417       29869 :     if (channels == 1) {
     418      245036 :         for (i = 0; i < CELT_MAX_BANDS; i++)
     419      233898 :             f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]);
     420             :     }
     421             : 
     422       29869 :     celt_decode_coarse_energy(f, rc);
     423       29869 :     celt_decode_tf_changes   (f, rc);
     424       29869 :     ff_celt_bitalloc         (f, rc, 0);
     425       29869 :     celt_decode_fine_energy  (f, rc);
     426       29869 :     ff_celt_quant_bands      (f, rc);
     427             : 
     428       29869 :     if (f->anticollapse_needed)
     429        3123 :         f->anticollapse = ff_opus_rc_get_raw(rc, 1);
     430             : 
     431       29869 :     celt_decode_final_energy(f, rc);
     432             : 
     433             :     /* apply anti-collapse processing and denormalization to
     434             :      * each coded channel */
     435       78469 :     for (i = 0; i < f->channels; i++) {
     436       48600 :         CeltBlock *block = &f->block[i];
     437             : 
     438       48600 :         if (f->anticollapse)
     439        2750 :             process_anticollapse(f, block, f->block[i].coeffs);
     440             : 
     441       48600 :         celt_denormalize(f, block, f->block[i].coeffs);
     442             :     }
     443             : 
     444             :     /* stereo -> mono downmix */
     445       29869 :     if (f->output_channels < f->channels) {
     446           0 :         f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16));
     447           0 :         downmix = 1;
     448       29869 :     } else if (f->output_channels > f->channels)
     449        8078 :         memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float));
     450             : 
     451       29869 :     if (f->silence) {
     452         375 :         for (i = 0; i < 2; i++) {
     453         250 :             CeltBlock *block = &f->block[i];
     454             : 
     455        5500 :             for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++)
     456        5250 :                 block->energy[j] = CELT_ENERGY_SILENCE;
     457             :         }
     458         125 :         memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs));
     459         125 :         memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs));
     460             :     }
     461             : 
     462             :     /* transform and output for each output channel */
     463       86547 :     for (i = 0; i < f->output_channels; i++) {
     464       56678 :         CeltBlock *block = &f->block[i];
     465       56678 :         float m = block->emph_coeff;
     466             : 
     467             :         /* iMDCT and overlap-add */
     468      149134 :         for (j = 0; j < f->blocks; j++) {
     469       92456 :             float *dst  = block->buf + 1024 + j * f->blocksize;
     470             : 
     471      184912 :             imdct->imdct_half(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j,
     472       92456 :                               f->blocks);
     473       92456 :             f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
     474             :                                        ff_celt_window, CELT_OVERLAP / 2);
     475             :         }
     476             : 
     477       56678 :         if (downmix)
     478           0 :             f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size);
     479             : 
     480             :         /* postfilter */
     481       56678 :         celt_postfilter(f, block);
     482             : 
     483             :         /* deemphasis and output scaling */
     484    22893158 :         for (j = 0; j < frame_size; j++) {
     485    22836480 :             const float tmp = block->buf[1024 - frame_size + j] + m;
     486    22836480 :             m = tmp * CELT_EMPH_COEFF;
     487    22836480 :             output[i][j] = tmp;
     488             :         }
     489             : 
     490       56678 :         block->emph_coeff = m;
     491             :     }
     492             : 
     493       29869 :     if (channels == 1)
     494       11138 :         memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy));
     495             : 
     496       89607 :     for (i = 0; i < 2; i++ ) {
     497       59738 :         CeltBlock *block = &f->block[i];
     498             : 
     499       59738 :         if (!f->transient) {
     500       51720 :             memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0]));
     501       51720 :             memcpy(block->prev_energy[0], block->energy,         sizeof(block->prev_energy[0]));
     502             :         } else {
     503      176396 :             for (j = 0; j < CELT_MAX_BANDS; j++)
     504      168378 :                 block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]);
     505             :         }
     506             : 
     507      227528 :         for (j = 0; j < f->start_band; j++) {
     508      167790 :             block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
     509      167790 :             block->energy[j]         = 0.0;
     510             :         }
     511      130378 :         for (j = f->end_band; j < CELT_MAX_BANDS; j++) {
     512       70640 :             block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
     513       70640 :             block->energy[j]         = 0.0;
     514             :         }
     515             :     }
     516             : 
     517       29869 :     f->seed = rc->range;
     518             : 
     519       29869 :     return 0;
     520             : }
     521             : 
     522        4597 : void ff_celt_flush(CeltFrame *f)
     523             : {
     524             :     int i, j;
     525             : 
     526        4597 :     if (f->flushed)
     527        4533 :         return;
     528             : 
     529         192 :     for (i = 0; i < 2; i++) {
     530         128 :         CeltBlock *block = &f->block[i];
     531             : 
     532        2816 :         for (j = 0; j < CELT_MAX_BANDS; j++)
     533        2688 :             block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE;
     534             : 
     535         128 :         memset(block->energy, 0, sizeof(block->energy));
     536         128 :         memset(block->buf,    0, sizeof(block->buf));
     537             : 
     538         128 :         memset(block->pf_gains,     0, sizeof(block->pf_gains));
     539         128 :         memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
     540         128 :         memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));
     541             : 
     542         128 :         block->emph_coeff = 0.0;
     543             :     }
     544          64 :     f->seed = 0;
     545             : 
     546          64 :     f->flushed = 1;
     547             : }
     548             : 
     549          41 : void ff_celt_free(CeltFrame **f)
     550             : {
     551          41 :     CeltFrame *frm = *f;
     552             :     int i;
     553             : 
     554          41 :     if (!frm)
     555           0 :         return;
     556             : 
     557         205 :     for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
     558         164 :         ff_mdct15_uninit(&frm->imdct[i]);
     559             : 
     560          41 :     ff_celt_pvq_uninit(&frm->pvq);
     561             : 
     562          41 :     av_freep(&frm->dsp);
     563          41 :     av_freep(f);
     564             : }
     565             : 
     566          41 : int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
     567             :                  int apply_phase_inv)
     568             : {
     569             :     CeltFrame *frm;
     570             :     int i, ret;
     571             : 
     572          41 :     if (output_channels != 1 && output_channels != 2) {
     573           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
     574             :                output_channels);
     575           0 :         return AVERROR(EINVAL);
     576             :     }
     577             : 
     578          41 :     frm = av_mallocz(sizeof(*frm));
     579          41 :     if (!frm)
     580           0 :         return AVERROR(ENOMEM);
     581             : 
     582          41 :     frm->avctx           = avctx;
     583          41 :     frm->output_channels = output_channels;
     584          41 :     frm->apply_phase_inv = apply_phase_inv;
     585             : 
     586         205 :     for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
     587         164 :         if ((ret = ff_mdct15_init(&frm->imdct[i], 1, i + 3, -1.0f/32768)) < 0)
     588           0 :             goto fail;
     589             : 
     590          41 :     if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0)
     591           0 :         goto fail;
     592             : 
     593          41 :     frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     594          41 :     if (!frm->dsp) {
     595           0 :         ret = AVERROR(ENOMEM);
     596           0 :         goto fail;
     597             :     }
     598             : 
     599          41 :     ff_celt_flush(frm);
     600             : 
     601          41 :     *f = frm;
     602             : 
     603          41 :     return 0;
     604           0 : fail:
     605           0 :     ff_celt_free(&frm);
     606           0 :     return ret;
     607             : }

Generated by: LCOV version 1.13