LCOV - code coverage report
Current view: top level - libavcodec - opus_celt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 511 533 95.9 %
Date: 2017-12-12 11:08:38 Functions: 15 15 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       29869 : static void celt_decode_allocation(CeltFrame *f, OpusRangeCoder *rc)
     147             : {
     148             :     // approx. maximum bit allocation for each band before boost/trim
     149             :     int cap[CELT_MAX_BANDS];
     150             :     int boost[CELT_MAX_BANDS];
     151             :     int threshold[CELT_MAX_BANDS];
     152             :     int bits1[CELT_MAX_BANDS];
     153             :     int bits2[CELT_MAX_BANDS];
     154             :     int trim_offset[CELT_MAX_BANDS];
     155             : 
     156       29869 :     int skip_start_band = f->start_band;
     157       29869 :     int dynalloc       = 6;
     158       29869 :     int alloctrim      = 5;
     159       29869 :     int extrabits      = 0;
     160             : 
     161       29869 :     int skip_bit             = 0;
     162       29869 :     int intensity_stereo_bit = 0;
     163       29869 :     int dual_stereo_bit      = 0;
     164             : 
     165             :     int remaining, bandbits;
     166             :     int low, high, total, done;
     167             :     int totalbits;
     168             :     int consumed;
     169             :     int i, j;
     170             : 
     171       29869 :     consumed = opus_rc_tell(rc);
     172             : 
     173             :     /* obtain spread flag */
     174       29869 :     f->spread = CELT_SPREAD_NORMAL;
     175       29869 :     if (consumed + 4 <= f->framebits)
     176       29735 :         f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
     177             : 
     178             :     /* generate static allocation caps */
     179      657118 :     for (i = 0; i < CELT_MAX_BANDS; i++) {
     180     1254498 :         cap[i] = (ff_celt_static_caps[f->size][f->channels - 1][i] + 64)
     181      627249 :                  * ff_celt_freq_range[i] << (f->channels - 1) << f->size >> 2;
     182             :     }
     183             : 
     184             :     /* obtain band boost */
     185       29869 :     totalbits = f->framebits << 3; // convert to 1/8 bits
     186       29869 :     consumed = opus_rc_tell_frac(rc);
     187      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     188             :         int quanta, band_dynalloc;
     189             : 
     190      508034 :         boost[i] = 0;
     191             : 
     192      508034 :         quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
     193      508034 :         quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
     194      508034 :         band_dynalloc = dynalloc;
     195     1032505 :         while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {
     196      521981 :             int add = ff_opus_rc_dec_log(rc, band_dynalloc);
     197      521981 :             consumed = opus_rc_tell_frac(rc);
     198      521981 :             if (!add)
     199      505544 :                 break;
     200             : 
     201       16437 :             boost[i]     += quanta;
     202       16437 :             totalbits    -= quanta;
     203       16437 :             band_dynalloc = 1;
     204             :         }
     205             :         /* dynalloc is more likely to occur if it's already been used for earlier bands */
     206      508034 :         if (boost[i])
     207       12537 :             dynalloc = FFMAX(2, dynalloc - 1);
     208             :     }
     209             : 
     210             :     /* obtain allocation trim */
     211       29869 :     if (consumed + (6 << 3) <= totalbits)
     212       29731 :         alloctrim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
     213             : 
     214             :     /* anti-collapse bit reservation */
     215       29869 :     totalbits = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
     216       29869 :     f->anticollapse_needed = 0;
     217       32995 :     if (f->blocks > 1 && f->size >= 2 &&
     218        3126 :         totalbits >= ((f->size + 2) << 3))
     219        3123 :         f->anticollapse_needed = 1 << 3;
     220       29869 :     totalbits -= f->anticollapse_needed;
     221             : 
     222             :     /* band skip bit reservation */
     223       29869 :     if (totalbits >= 1 << 3)
     224       29737 :         skip_bit = 1 << 3;
     225       29869 :     totalbits -= skip_bit;
     226             : 
     227             :     /* intensity/dual stereo bit reservation */
     228       29869 :     if (f->channels == 2) {
     229       18731 :         intensity_stereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
     230       18731 :         if (intensity_stereo_bit <= totalbits) {
     231       18660 :             totalbits -= intensity_stereo_bit;
     232       18660 :             if (totalbits >= 1 << 3) {
     233       18656 :                 dual_stereo_bit = 1 << 3;
     234       18656 :                 totalbits -= 1 << 3;
     235             :             }
     236             :         } else
     237          71 :             intensity_stereo_bit = 0;
     238             :     }
     239             : 
     240      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     241      508034 :         int trim     = alloctrim - 5 - f->size;
     242      508034 :         int band     = ff_celt_freq_range[i] * (f->end_band - i - 1);
     243      508034 :         int duration = f->size + 3;
     244      508034 :         int scale    = duration + f->channels - 1;
     245             : 
     246             :         /* PVQ minimum allocation threshold, below this value the band is
     247             :          * skipped */
     248      508034 :         threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
     249             :                              f->channels << 3);
     250             : 
     251      508034 :         trim_offset[i] = trim * (band << scale) >> 6;
     252             : 
     253      508034 :         if (ff_celt_freq_range[i] << f->size == 1)
     254      109616 :             trim_offset[i] -= f->channels << 3;
     255             :     }
     256             : 
     257             :     /* bisection */
     258       29869 :     low  = 1;
     259       29869 :     high = CELT_VECTORS - 1;
     260      171283 :     while (low <= high) {
     261      111545 :         int center = (low + high) >> 1;
     262      111545 :         done = total = 0;
     263             : 
     264     2012955 :         for (i = f->end_band - 1; i >= f->start_band; i--) {
     265     3802820 :             bandbits = ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]
     266     1901410 :                        << (f->channels - 1) << f->size >> 2;
     267             : 
     268     1901410 :             if (bandbits)
     269     1811644 :                 bandbits = FFMAX(0, bandbits + trim_offset[i]);
     270     1901410 :             bandbits += boost[i];
     271             : 
     272     1901410 :             if (bandbits >= threshold[i] || done) {
     273     1751278 :                 done = 1;
     274     1751278 :                 total += FFMIN(bandbits, cap[i]);
     275      150132 :             } else if (bandbits >= f->channels << 3)
     276       31467 :                 total += f->channels << 3;
     277             :         }
     278             : 
     279      111545 :         if (total > totalbits)
     280       39866 :             high = center - 1;
     281             :         else
     282       71679 :             low = center + 1;
     283             :     }
     284       29869 :     high = low--;
     285             : 
     286      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     287     1016068 :         bits1[i] = ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]
     288      508034 :                    << (f->channels - 1) << f->size >> 2;
     289      978297 :         bits2[i] = high >= CELT_VECTORS ? cap[i] :
     290      470263 :                    ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]
     291      470263 :                    << (f->channels - 1) << f->size >> 2;
     292             : 
     293      508034 :         if (bits1[i])
     294      464983 :             bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);
     295      508034 :         if (bits2[i])
     296      496152 :             bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);
     297      508034 :         if (low)
     298      485717 :             bits1[i] += boost[i];
     299      508034 :         bits2[i] += boost[i];
     300             : 
     301      508034 :         if (boost[i])
     302       12537 :             skip_start_band = i;
     303      508034 :         bits2[i] = FFMAX(0, bits2[i] - bits1[i]);
     304             :     }
     305             : 
     306             :     /* bisection */
     307       29869 :     low  = 0;
     308       29869 :     high = 1 << CELT_ALLOC_STEPS;
     309      209083 :     for (i = 0; i < CELT_ALLOC_STEPS; i++) {
     310      179214 :         int center = (low + high) >> 1;
     311      179214 :         done = total = 0;
     312             : 
     313     3227418 :         for (j = f->end_band - 1; j >= f->start_band; j--) {
     314     3048204 :             bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
     315             : 
     316     3048204 :             if (bandbits >= threshold[j] || done) {
     317     2838198 :                 done = 1;
     318     2838198 :                 total += FFMIN(bandbits, cap[j]);
     319      210006 :             } else if (bandbits >= f->channels << 3)
     320       52380 :                 total += f->channels << 3;
     321             :         }
     322      179214 :         if (total > totalbits)
     323       85568 :             high = center;
     324             :         else
     325       93646 :             low = center;
     326             :     }
     327             : 
     328       29869 :     done = total = 0;
     329      537903 :     for (i = f->end_band - 1; i >= f->start_band; i--) {
     330      508034 :         bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
     331             : 
     332      508034 :         if (bandbits >= threshold[i] || done)
     333      472034 :             done = 1;
     334             :         else
     335       72000 :             bandbits = (bandbits >= f->channels << 3) ?
     336       36000 :                        f->channels << 3 : 0;
     337             : 
     338      508034 :         bandbits     = FFMIN(bandbits, cap[i]);
     339      508034 :         f->pulses[i] = bandbits;
     340      508034 :         total      += bandbits;
     341             :     }
     342             : 
     343             :     /* band skipping */
     344       80549 :     for (f->coded_bands = f->end_band; ; f->coded_bands--) {
     345             :         int allocation;
     346       80549 :         j = f->coded_bands - 1;
     347             : 
     348       80549 :         if (j == skip_start_band) {
     349             :             /* all remaining bands are not skipped */
     350        1645 :             totalbits += skip_bit;
     351        1645 :             break;
     352             :         }
     353             : 
     354             :         /* determine the number of bits available for coding "do not skip" markers */
     355       78904 :         remaining   = totalbits - total;
     356       78904 :         bandbits    = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
     357       78904 :         remaining  -= bandbits  * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
     358      157808 :         allocation  = f->pulses[j] + bandbits * ff_celt_freq_range[j]
     359       78904 :                       + FFMAX(0, remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]));
     360             : 
     361             :         /* a "do not skip" marker is only coded if the allocation is
     362             :            above the chosen threshold */
     363       78904 :         if (allocation >= FFMAX(threshold[j], (f->channels + 1) <<3 )) {
     364       38108 :             if (ff_opus_rc_dec_log(rc, 1))
     365       28224 :                 break;
     366             : 
     367        9884 :             total      += 1 << 3;
     368        9884 :             allocation -= 1 << 3;
     369             :         }
     370             : 
     371             :         /* the band is skipped, so reclaim its bits */
     372       50680 :         total -= f->pulses[j];
     373       50680 :         if (intensity_stereo_bit) {
     374       34519 :             total -= intensity_stereo_bit;
     375       34519 :             intensity_stereo_bit = ff_celt_log2_frac[j - f->start_band];
     376       34519 :             total += intensity_stereo_bit;
     377             :         }
     378             : 
     379      101360 :         total += f->pulses[j] = (allocation >= f->channels << 3) ?
     380       50680 :                               f->channels << 3 : 0;
     381             :     }
     382             : 
     383             :     /* obtain stereo flags */
     384       29869 :     f->intensity_stereo = 0;
     385       29869 :     f->dual_stereo      = 0;
     386       29869 :     if (intensity_stereo_bit)
     387       37320 :         f->intensity_stereo = f->start_band +
     388       18660 :                           ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
     389       29869 :     if (f->intensity_stereo <= f->start_band)
     390       13153 :         totalbits += dual_stereo_bit; /* no intensity stereo means no dual stereo */
     391       16716 :     else if (dual_stereo_bit)
     392       16716 :         f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
     393             : 
     394             :     /* supply the remaining bits in this frame to lower bands */
     395       29869 :     remaining = totalbits - total;
     396       29869 :     bandbits  = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
     397       29869 :     remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
     398      487223 :     for (i = f->start_band; i < f->coded_bands; i++) {
     399      457354 :         int bits = FFMIN(remaining, ff_celt_freq_range[i]);
     400             : 
     401      457354 :         f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
     402      457354 :         remaining    -= bits;
     403             :     }
     404             : 
     405      487223 :     for (i = f->start_band; i < f->coded_bands; i++) {
     406      457354 :         int N = ff_celt_freq_range[i] << f->size;
     407      457354 :         int prev_extra = extrabits;
     408      457354 :         f->pulses[i] += extrabits;
     409             : 
     410      457354 :         if (N > 1) {
     411             :             int dof;        // degrees of freedom
     412             :             int temp;       // dof * channels * log(dof)
     413             :             int offset;     // fine energy quantization offset, i.e.
     414             :                             // extra bits assigned over the standard
     415             :                             // totalbits/dof
     416             :             int fine_bits, max_bits;
     417             : 
     418      350835 :             extrabits = FFMAX(0, f->pulses[i] - cap[i]);
     419      350835 :             f->pulses[i] -= extrabits;
     420             : 
     421             :             /* intensity stereo makes use of an extra degree of freedom */
     422      701670 :             dof = N * f->channels
     423      350835 :                   + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
     424      350835 :             temp = dof * (ff_celt_log_freq_range[i] + (f->size<<3));
     425      350835 :             offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
     426      350835 :             if (N == 2) /* dof=2 is the only case that doesn't fit the model */
     427       83650 :                 offset += dof<<1;
     428             : 
     429             :             /* grant an additional bias for the first and second pulses */
     430      350835 :             if (f->pulses[i] + offset < 2 * (dof << 3))
     431      247060 :                 offset += temp >> 2;
     432      103775 :             else if (f->pulses[i] + offset < 3 * (dof << 3))
     433       49959 :                 offset += temp >> 3;
     434             : 
     435      350835 :             fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
     436      350835 :             max_bits  = FFMIN((f->pulses[i]>>3) >> (f->channels - 1),
     437             :                               CELT_MAX_FINE_BITS);
     438             : 
     439      350835 :             max_bits  = FFMAX(max_bits, 0);
     440             : 
     441      350835 :             f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
     442             : 
     443             :             /* if fine_bits was rounded down or capped,
     444             :                give priority for the final fine energy pass */
     445      350835 :             f->fine_priority[i] = (f->fine_bits[i] * (dof<<3) >= f->pulses[i] + offset);
     446             : 
     447             :             /* the remaining bits are assigned to PVQ */
     448      350835 :             f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
     449             :         } else {
     450             :             /* all bits go to fine energy except for the sign bit */
     451      106519 :             extrabits = FFMAX(0, f->pulses[i] - (f->channels << 3));
     452      106519 :             f->pulses[i] -= extrabits;
     453      106519 :             f->fine_bits[i] = 0;
     454      106519 :             f->fine_priority[i] = 1;
     455             :         }
     456             : 
     457             :         /* hand back a limited number of extra fine energy bits to this band */
     458      457354 :         if (extrabits > 0) {
     459      123173 :             int fineextra = FFMIN(extrabits >> (f->channels + 2),
     460             :                                   CELT_MAX_FINE_BITS - f->fine_bits[i]);
     461      123173 :             f->fine_bits[i] += fineextra;
     462             : 
     463      123173 :             fineextra <<= f->channels + 2;
     464      123173 :             f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
     465      123173 :             extrabits -= fineextra;
     466             :         }
     467             :     }
     468       29869 :     f->remaining = extrabits;
     469             : 
     470             :     /* skipped bands dedicate all of their bits for fine energy */
     471       80549 :     for (; i < f->end_band; i++) {
     472       50680 :         f->fine_bits[i]     = f->pulses[i] >> (f->channels - 1) >> 3;
     473       50680 :         f->pulses[i]        = 0;
     474       50680 :         f->fine_priority[i] = f->fine_bits[i] < 1;
     475             :     }
     476       29869 : }
     477             : 
     478       48600 : static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
     479             : {
     480             :     int i, j;
     481             : 
     482      898292 :     for (i = f->start_band; i < f->end_band; i++) {
     483      849692 :         float *dst = data + (ff_celt_freq_bands[i] << f->size);
     484      849692 :         float log_norm = block->energy[i] + ff_celt_mean_energy[i];
     485      849692 :         float norm = exp2f(FFMIN(log_norm, 32.0f));
     486             : 
     487    13154552 :         for (j = 0; j < ff_celt_freq_range[i] << f->size; j++)
     488    12304860 :             dst[j] *= norm;
     489             :     }
     490       48600 : }
     491             : 
     492       89012 : static void celt_postfilter_apply_transition(CeltBlock *block, float *data)
     493             : {
     494       89012 :     const int T0 = block->pf_period_old;
     495       89012 :     const int T1 = block->pf_period;
     496             : 
     497             :     float g00, g01, g02;
     498             :     float g10, g11, g12;
     499             : 
     500             :     float x0, x1, x2, x3, x4;
     501             : 
     502             :     int i;
     503             : 
     504      130582 :     if (block->pf_gains[0]     == 0.0 &&
     505       41570 :         block->pf_gains_old[0] == 0.0)
     506       37390 :         return;
     507             : 
     508       51622 :     g00 = block->pf_gains_old[0];
     509       51622 :     g01 = block->pf_gains_old[1];
     510       51622 :     g02 = block->pf_gains_old[2];
     511       51622 :     g10 = block->pf_gains[0];
     512       51622 :     g11 = block->pf_gains[1];
     513       51622 :     g12 = block->pf_gains[2];
     514             : 
     515       51622 :     x1 = data[-T1 + 1];
     516       51622 :     x2 = data[-T1];
     517       51622 :     x3 = data[-T1 - 1];
     518       51622 :     x4 = data[-T1 - 2];
     519             : 
     520     6246262 :     for (i = 0; i < CELT_OVERLAP; i++) {
     521     6194640 :         float w = ff_celt_window2[i];
     522     6194640 :         x0 = data[i - T1 + 2];
     523             : 
     524    18583920 :         data[i] +=  (1.0 - w) * g00 * data[i - T0]                          +
     525    12389280 :                     (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
     526    12389280 :                     (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
     527    12389280 :                     w         * g10 * x2                                    +
     528    12389280 :                     w         * g11 * (x1 + x3)                             +
     529     6194640 :                     w         * g12 * (x0 + x4);
     530     6194640 :         x4 = x3;
     531     6194640 :         x3 = x2;
     532     6194640 :         x2 = x1;
     533     6194640 :         x1 = x0;
     534             :     }
     535             : }
     536             : 
     537       32334 : static void celt_postfilter_apply(CeltBlock *block, float *data, int len)
     538             : {
     539       32334 :     const int T = block->pf_period;
     540             :     float g0, g1, g2;
     541             :     float x0, x1, x2, x3, x4;
     542             :     int i;
     543             : 
     544       32334 :     if (block->pf_gains[0] == 0.0 || len <= 0)
     545       22210 :         return;
     546             : 
     547       10124 :     g0 = block->pf_gains[0];
     548       10124 :     g1 = block->pf_gains[1];
     549       10124 :     g2 = block->pf_gains[2];
     550             : 
     551       10124 :     x4 = data[-T - 2];
     552       10124 :     x3 = data[-T - 1];
     553       10124 :     x2 = data[-T];
     554       10124 :     x1 = data[-T + 1];
     555             : 
     556     5737484 :     for (i = 0; i < len; i++) {
     557     5727360 :         x0 = data[i - T + 2];
     558    17182080 :         data[i] += g0 * x2        +
     559    11454720 :                    g1 * (x1 + x3) +
     560     5727360 :                    g2 * (x0 + x4);
     561     5727360 :         x4 = x3;
     562     5727360 :         x3 = x2;
     563     5727360 :         x2 = x1;
     564     5727360 :         x1 = x0;
     565             :     }
     566             : }
     567             : 
     568       56678 : static void celt_postfilter(CeltFrame *f, CeltBlock *block)
     569             : {
     570       56678 :     int len = f->blocksize * f->blocks;
     571             : 
     572       56678 :     celt_postfilter_apply_transition(block, block->buf + 1024);
     573             : 
     574       56678 :     block->pf_period_old = block->pf_period;
     575       56678 :     memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
     576             : 
     577       56678 :     block->pf_period = block->pf_period_new;
     578       56678 :     memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains));
     579             : 
     580       56678 :     if (len > CELT_OVERLAP) {
     581       32334 :         celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP);
     582       32334 :         celt_postfilter_apply(block, block->buf + 1024 + 2 * CELT_OVERLAP,
     583             :                               len - 2 * CELT_OVERLAP);
     584             : 
     585       32334 :         block->pf_period_old = block->pf_period;
     586       32334 :         memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
     587             :     }
     588             : 
     589       56678 :     memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
     590       56678 : }
     591             : 
     592       29869 : static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
     593             : {
     594             :     int i;
     595             : 
     596       29869 :     memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new));
     597       29869 :     memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new));
     598             : 
     599       29869 :     if (f->start_band == 0 && consumed + 16 <= f->framebits) {
     600       24809 :         int has_postfilter = ff_opus_rc_dec_log(rc, 1);
     601       24809 :         if (has_postfilter) {
     602             :             float gain;
     603             :             int tapset, octave, period;
     604             : 
     605       15873 :             octave = ff_opus_rc_dec_uint(rc, 6);
     606       15873 :             period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
     607       15873 :             gain   = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
     608       31746 :             tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ?
     609       15873 :                      ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0;
     610             : 
     611       47619 :             for (i = 0; i < 2; i++) {
     612       31746 :                 CeltBlock *block = &f->block[i];
     613             : 
     614       31746 :                 block->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD);
     615       31746 :                 block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0];
     616       31746 :                 block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1];
     617       31746 :                 block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2];
     618             :             }
     619             :         }
     620             : 
     621       24809 :         consumed = opus_rc_tell(rc);
     622             :     }
     623             : 
     624       29869 :     return consumed;
     625             : }
     626             : 
     627        2750 : static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
     628             : {
     629             :     int i, j, k;
     630             : 
     631       37875 :     for (i = f->start_band; i < f->end_band; i++) {
     632       35125 :         int renormalize = 0;
     633             :         float *xptr;
     634             :         float prev[2];
     635             :         float Ediff, r;
     636             :         float thresh, sqrt_1;
     637             :         int depth;
     638             : 
     639             :         /* depth in 1/8 bits */
     640       35125 :         depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size);
     641       35125 :         thresh = exp2f(-1.0 - 0.125f * depth);
     642       35125 :         sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size);
     643             : 
     644       35125 :         xptr = X + (ff_celt_freq_bands[i] << f->size);
     645             : 
     646       35125 :         prev[0] = block->prev_energy[0][i];
     647       35125 :         prev[1] = block->prev_energy[1][i];
     648       35125 :         if (f->channels == 1) {
     649        9975 :             CeltBlock *block1 = &f->block[1];
     650             : 
     651        9975 :             prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]);
     652        9975 :             prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]);
     653             :         }
     654       35125 :         Ediff = block->energy[i] - FFMIN(prev[0], prev[1]);
     655       35125 :         Ediff = FFMAX(0, Ediff);
     656             : 
     657             :         /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
     658             :         short blocks don't have the same energy as long */
     659       35125 :         r = exp2f(1 - Ediff);
     660       35125 :         if (f->size == 3)
     661       23564 :             r *= M_SQRT2;
     662       35125 :         r = FFMIN(thresh, r) * sqrt_1;
     663      269881 :         for (k = 0; k < 1 << f->size; k++) {
     664             :             /* Detect collapse */
     665      234756 :             if (!(block->collapse_masks[i] & 1 << k)) {
     666             :                 /* Fill with noise */
     667      115623 :                 for (j = 0; j < ff_celt_freq_range[i]; j++)
     668       95928 :                     xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r;
     669       19695 :                 renormalize = 1;
     670             :             }
     671             :         }
     672             : 
     673             :         /* We just added some energy, so we need to renormalize */
     674       35125 :         if (renormalize)
     675        8458 :             celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f);
     676             :     }
     677        2750 : }
     678             : 
     679       29869 : int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc,
     680             :                          float **output, int channels, int frame_size,
     681             :                          int start_band,  int end_band)
     682             : {
     683       29869 :     int i, j, downmix = 0;
     684             :     int consumed;           // bits of entropy consumed thus far for this frame
     685             :     MDCT15Context *imdct;
     686             : 
     687       29869 :     if (channels != 1 && channels != 2) {
     688           0 :         av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
     689             :                channels);
     690           0 :         return AVERROR_INVALIDDATA;
     691             :     }
     692       29869 :     if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) {
     693           0 :         av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
     694             :                start_band, end_band);
     695           0 :         return AVERROR_INVALIDDATA;
     696             :     }
     697             : 
     698       29869 :     f->silence        = 0;
     699       29869 :     f->transient      = 0;
     700       29869 :     f->anticollapse   = 0;
     701       29869 :     f->flushed        = 0;
     702       29869 :     f->channels       = channels;
     703       29869 :     f->start_band     = start_band;
     704       29869 :     f->end_band       = end_band;
     705       29869 :     f->framebits      = rc->rb.bytes * 8;
     706             : 
     707       29869 :     f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
     708       59738 :     if (f->size > CELT_MAX_LOG_BLOCKS ||
     709       29869 :         frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) {
     710           0 :         av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
     711             :                frame_size);
     712           0 :         return AVERROR_INVALIDDATA;
     713             :     }
     714             : 
     715       29869 :     if (!f->output_channels)
     716           0 :         f->output_channels = channels;
     717             : 
     718       78469 :     for (i = 0; i < f->channels; i++) {
     719       48600 :         memset(f->block[i].coeffs,         0, sizeof(f->block[i].coeffs));
     720       48600 :         memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks));
     721             :     }
     722             : 
     723       29869 :     consumed = opus_rc_tell(rc);
     724             : 
     725             :     /* obtain silence flag */
     726       29869 :     if (consumed >= f->framebits)
     727           0 :         f->silence = 1;
     728       29869 :     else if (consumed == 1)
     729       24934 :         f->silence = ff_opus_rc_dec_log(rc, 15);
     730             : 
     731             : 
     732       29869 :     if (f->silence) {
     733         125 :         consumed = f->framebits;
     734         125 :         rc->total_bits += f->framebits - opus_rc_tell(rc);
     735             :     }
     736             : 
     737             :     /* obtain post-filter options */
     738       29869 :     consumed = parse_postfilter(f, rc, consumed);
     739             : 
     740             :     /* obtain transient flag */
     741       29869 :     if (f->size != 0 && consumed+3 <= f->framebits)
     742       16079 :         f->transient = ff_opus_rc_dec_log(rc, 3);
     743             : 
     744       29869 :     f->blocks    = f->transient ? 1 << f->size : 1;
     745       29869 :     f->blocksize = frame_size / f->blocks;
     746             : 
     747       29869 :     imdct = f->imdct[f->transient ? 0 : f->size];
     748             : 
     749       29869 :     if (channels == 1) {
     750      245036 :         for (i = 0; i < CELT_MAX_BANDS; i++)
     751      233898 :             f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]);
     752             :     }
     753             : 
     754       29869 :     celt_decode_coarse_energy(f, rc);
     755       29869 :     celt_decode_tf_changes   (f, rc);
     756       29869 :     celt_decode_allocation   (f, rc);
     757       29869 :     celt_decode_fine_energy  (f, rc);
     758       29869 :     ff_celt_quant_bands      (f, rc);
     759             : 
     760       29869 :     if (f->anticollapse_needed)
     761        3123 :         f->anticollapse = ff_opus_rc_get_raw(rc, 1);
     762             : 
     763       29869 :     celt_decode_final_energy(f, rc);
     764             : 
     765             :     /* apply anti-collapse processing and denormalization to
     766             :      * each coded channel */
     767       78469 :     for (i = 0; i < f->channels; i++) {
     768       48600 :         CeltBlock *block = &f->block[i];
     769             : 
     770       48600 :         if (f->anticollapse)
     771        2750 :             process_anticollapse(f, block, f->block[i].coeffs);
     772             : 
     773       48600 :         celt_denormalize(f, block, f->block[i].coeffs);
     774             :     }
     775             : 
     776             :     /* stereo -> mono downmix */
     777       29869 :     if (f->output_channels < f->channels) {
     778           0 :         f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16));
     779           0 :         downmix = 1;
     780       29869 :     } else if (f->output_channels > f->channels)
     781        8078 :         memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float));
     782             : 
     783       29869 :     if (f->silence) {
     784         375 :         for (i = 0; i < 2; i++) {
     785         250 :             CeltBlock *block = &f->block[i];
     786             : 
     787        5500 :             for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++)
     788        5250 :                 block->energy[j] = CELT_ENERGY_SILENCE;
     789             :         }
     790         125 :         memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs));
     791         125 :         memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs));
     792             :     }
     793             : 
     794             :     /* transform and output for each output channel */
     795       86547 :     for (i = 0; i < f->output_channels; i++) {
     796       56678 :         CeltBlock *block = &f->block[i];
     797       56678 :         float m = block->emph_coeff;
     798             : 
     799             :         /* iMDCT and overlap-add */
     800      149134 :         for (j = 0; j < f->blocks; j++) {
     801       92456 :             float *dst  = block->buf + 1024 + j * f->blocksize;
     802             : 
     803      184912 :             imdct->imdct_half(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j,
     804       92456 :                               f->blocks);
     805       92456 :             f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
     806             :                                        ff_celt_window, CELT_OVERLAP / 2);
     807             :         }
     808             : 
     809       56678 :         if (downmix)
     810           0 :             f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size);
     811             : 
     812             :         /* postfilter */
     813       56678 :         celt_postfilter(f, block);
     814             : 
     815             :         /* deemphasis and output scaling */
     816    22893158 :         for (j = 0; j < frame_size; j++) {
     817    22836480 :             const float tmp = block->buf[1024 - frame_size + j] + m;
     818    22836480 :             m = tmp * CELT_EMPH_COEFF;
     819    22836480 :             output[i][j] = tmp;
     820             :         }
     821             : 
     822       56678 :         block->emph_coeff = m;
     823             :     }
     824             : 
     825       29869 :     if (channels == 1)
     826       11138 :         memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy));
     827             : 
     828       89607 :     for (i = 0; i < 2; i++ ) {
     829       59738 :         CeltBlock *block = &f->block[i];
     830             : 
     831       59738 :         if (!f->transient) {
     832       51720 :             memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0]));
     833       51720 :             memcpy(block->prev_energy[0], block->energy,         sizeof(block->prev_energy[0]));
     834             :         } else {
     835      176396 :             for (j = 0; j < CELT_MAX_BANDS; j++)
     836      168378 :                 block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]);
     837             :         }
     838             : 
     839      227528 :         for (j = 0; j < f->start_band; j++) {
     840      167790 :             block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
     841      167790 :             block->energy[j]         = 0.0;
     842             :         }
     843      130378 :         for (j = f->end_band; j < CELT_MAX_BANDS; j++) {
     844       70640 :             block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
     845       70640 :             block->energy[j]         = 0.0;
     846             :         }
     847             :     }
     848             : 
     849       29869 :     f->seed = rc->range;
     850             : 
     851       29869 :     return 0;
     852             : }
     853             : 
     854        4597 : void ff_celt_flush(CeltFrame *f)
     855             : {
     856             :     int i, j;
     857             : 
     858        4597 :     if (f->flushed)
     859        4533 :         return;
     860             : 
     861         192 :     for (i = 0; i < 2; i++) {
     862         128 :         CeltBlock *block = &f->block[i];
     863             : 
     864        2816 :         for (j = 0; j < CELT_MAX_BANDS; j++)
     865        2688 :             block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE;
     866             : 
     867         128 :         memset(block->energy, 0, sizeof(block->energy));
     868         128 :         memset(block->buf,    0, sizeof(block->buf));
     869             : 
     870         128 :         memset(block->pf_gains,     0, sizeof(block->pf_gains));
     871         128 :         memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
     872         128 :         memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));
     873             : 
     874         128 :         block->emph_coeff = 0.0;
     875             :     }
     876          64 :     f->seed = 0;
     877             : 
     878          64 :     f->flushed = 1;
     879             : }
     880             : 
     881          41 : void ff_celt_free(CeltFrame **f)
     882             : {
     883          41 :     CeltFrame *frm = *f;
     884             :     int i;
     885             : 
     886          41 :     if (!frm)
     887           0 :         return;
     888             : 
     889         205 :     for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
     890         164 :         ff_mdct15_uninit(&frm->imdct[i]);
     891             : 
     892          41 :     ff_celt_pvq_uninit(&frm->pvq);
     893             : 
     894          41 :     av_freep(&frm->dsp);
     895          41 :     av_freep(f);
     896             : }
     897             : 
     898          41 : int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
     899             :                  int apply_phase_inv)
     900             : {
     901             :     CeltFrame *frm;
     902             :     int i, ret;
     903             : 
     904          41 :     if (output_channels != 1 && output_channels != 2) {
     905           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
     906             :                output_channels);
     907           0 :         return AVERROR(EINVAL);
     908             :     }
     909             : 
     910          41 :     frm = av_mallocz(sizeof(*frm));
     911          41 :     if (!frm)
     912           0 :         return AVERROR(ENOMEM);
     913             : 
     914          41 :     frm->avctx           = avctx;
     915          41 :     frm->output_channels = output_channels;
     916          41 :     frm->apply_phase_inv = apply_phase_inv;
     917             : 
     918         205 :     for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
     919         164 :         if ((ret = ff_mdct15_init(&frm->imdct[i], 1, i + 3, -1.0f/32768)) < 0)
     920           0 :             goto fail;
     921             : 
     922          41 :     if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0)
     923           0 :         goto fail;
     924             : 
     925          41 :     frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     926          41 :     if (!frm->dsp) {
     927           0 :         ret = AVERROR(ENOMEM);
     928           0 :         goto fail;
     929             :     }
     930             : 
     931          41 :     ff_celt_flush(frm);
     932             : 
     933          41 :     *f = frm;
     934             : 
     935          41 :     return 0;
     936           0 : fail:
     937           0 :     ff_celt_free(&frm);
     938           0 :     return ret;
     939             : }

Generated by: LCOV version 1.13