LCOV - code coverage report
Current view: top level - libavcodec - opus.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 427 506 84.4 %
Date: 2018-05-20 11:54:08 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Andrew D'Addesio
       3             :  * Copyright (c) 2013-2014 Mozilla Corporation
       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             : /**
      23             :  * @file
      24             :  * Opus decoder/parser shared code
      25             :  */
      26             : 
      27             : #include <stdint.h>
      28             : 
      29             : #include "libavutil/error.h"
      30             : #include "libavutil/ffmath.h"
      31             : 
      32             : #include "opus_celt.h"
      33             : #include "opustab.h"
      34             : #include "vorbis.h"
      35             : 
      36             : static const uint16_t opus_frame_duration[32] = {
      37             :     480, 960, 1920, 2880,
      38             :     480, 960, 1920, 2880,
      39             :     480, 960, 1920, 2880,
      40             :     480, 960,
      41             :     480, 960,
      42             :     120, 240,  480,  960,
      43             :     120, 240,  480,  960,
      44             :     120, 240,  480,  960,
      45             :     120, 240,  480,  960,
      46             : };
      47             : 
      48             : /**
      49             :  * Read a 1- or 2-byte frame length
      50             :  */
      51       21805 : static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
      52             : {
      53             :     int val;
      54             : 
      55       21805 :     if (*ptr >= end)
      56           0 :         return AVERROR_INVALIDDATA;
      57       21805 :     val = *(*ptr)++;
      58       21805 :     if (val >= 252) {
      59         896 :         if (*ptr >= end)
      60           0 :             return AVERROR_INVALIDDATA;
      61         896 :         val += 4 * *(*ptr)++;
      62             :     }
      63       21805 :     return val;
      64             : }
      65             : 
      66             : /**
      67             :  * Read a multi-byte length (used for code 3 packet padding size)
      68             :  */
      69        1320 : static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
      70             : {
      71        1320 :     int val = 0;
      72             :     int next;
      73             : 
      74             :     while (1) {
      75        2232 :         if (*ptr >= end || val > INT_MAX - 254)
      76           0 :             return AVERROR_INVALIDDATA;
      77        1776 :         next = *(*ptr)++;
      78        1776 :         val += next;
      79        1776 :         if (next < 255)
      80        1320 :             break;
      81             :         else
      82         456 :             val--;
      83             :     }
      84        1320 :     return val;
      85             : }
      86             : 
      87             : /**
      88             :  * Parse Opus packet info from raw packet data
      89             :  */
      90       48847 : int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
      91             :                          int self_delimiting)
      92             : {
      93       48847 :     const uint8_t *ptr = buf;
      94       48847 :     const uint8_t *end = buf + buf_size;
      95       48847 :     int padding = 0;
      96             :     int frame_bytes, i;
      97             : 
      98       48847 :     if (buf_size < 1)
      99           0 :         goto fail;
     100             : 
     101             :     /* TOC byte */
     102       48847 :     i = *ptr++;
     103       48847 :     pkt->code   = (i     ) & 0x3;
     104       48847 :     pkt->stereo = (i >> 2) & 0x1;
     105       48847 :     pkt->config = (i >> 3) & 0x1F;
     106             : 
     107             :     /* code 2 and code 3 packets have at least 1 byte after the TOC */
     108       48847 :     if (pkt->code >= 2 && buf_size < 2)
     109           0 :         goto fail;
     110             : 
     111       48847 :     switch (pkt->code) {
     112       40009 :     case 0:
     113             :         /* 1 frame */
     114       40009 :         pkt->frame_count = 1;
     115       40009 :         pkt->vbr         = 0;
     116             : 
     117       40009 :         if (self_delimiting) {
     118        6885 :             int len = xiph_lacing_16bit(&ptr, end);
     119        6885 :             if (len < 0 || len > end - ptr)
     120             :                 goto fail;
     121        6885 :             end      = ptr + len;
     122        6885 :             buf_size = end - buf;
     123             :         }
     124             : 
     125       40009 :         frame_bytes = end - ptr;
     126       40009 :         if (frame_bytes > MAX_FRAME_SIZE)
     127           0 :             goto fail;
     128       40009 :         pkt->frame_offset[0] = ptr - buf;
     129       40009 :         pkt->frame_size[0]   = frame_bytes;
     130       40009 :         break;
     131         640 :     case 1:
     132             :         /* 2 frames, equal size */
     133         640 :         pkt->frame_count = 2;
     134         640 :         pkt->vbr         = 0;
     135             : 
     136         640 :         if (self_delimiting) {
     137           0 :             int len = xiph_lacing_16bit(&ptr, end);
     138           0 :             if (len < 0 || 2 * len > end - ptr)
     139             :                 goto fail;
     140           0 :             end      = ptr + 2 * len;
     141           0 :             buf_size = end - buf;
     142             :         }
     143             : 
     144         640 :         frame_bytes = end - ptr;
     145        1280 :         if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
     146             :             goto fail;
     147         640 :         pkt->frame_offset[0] = ptr - buf;
     148         640 :         pkt->frame_size[0]   = frame_bytes >> 1;
     149         640 :         pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
     150         640 :         pkt->frame_size[1]   = frame_bytes >> 1;
     151         640 :         break;
     152        1998 :     case 2:
     153             :         /* 2 frames, different sizes */
     154        1998 :         pkt->frame_count = 2;
     155        1998 :         pkt->vbr         = 1;
     156             : 
     157             :         /* read 1st frame size */
     158        1998 :         frame_bytes = xiph_lacing_16bit(&ptr, end);
     159        1998 :         if (frame_bytes < 0)
     160           0 :             goto fail;
     161             : 
     162        1998 :         if (self_delimiting) {
     163           0 :             int len = xiph_lacing_16bit(&ptr, end);
     164           0 :             if (len < 0 || len + frame_bytes > end - ptr)
     165             :                 goto fail;
     166           0 :             end      = ptr + frame_bytes + len;
     167           0 :             buf_size = end - buf;
     168             :         }
     169             : 
     170        1998 :         pkt->frame_offset[0] = ptr - buf;
     171        1998 :         pkt->frame_size[0]   = frame_bytes;
     172             : 
     173             :         /* calculate 2nd frame size */
     174        1998 :         frame_bytes = end - ptr - pkt->frame_size[0];
     175        3996 :         if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
     176             :             goto fail;
     177        1998 :         pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
     178        1998 :         pkt->frame_size[1]   = frame_bytes;
     179        1998 :         break;
     180        6200 :     case 3:
     181             :         /* 1 to 48 frames, can be different sizes */
     182        6200 :         i = *ptr++;
     183        6200 :         pkt->frame_count = (i     ) & 0x3F;
     184        6200 :         padding          = (i >> 6) & 0x01;
     185        6200 :         pkt->vbr         = (i >> 7) & 0x01;
     186             : 
     187       12400 :         if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
     188             :             goto fail;
     189             : 
     190             :         /* read padding size */
     191        6200 :         if (padding) {
     192        1320 :             padding = xiph_lacing_full(&ptr, end);
     193        1320 :             if (padding < 0)
     194           0 :                 goto fail;
     195             :         }
     196             : 
     197             :         /* read frame sizes */
     198        6200 :         if (pkt->vbr) {
     199             :             /* for VBR, all frames except the final one have their size coded
     200             :                in the bitstream. the last frame size is implicit. */
     201        4350 :             int total_bytes = 0;
     202       17272 :             for (i = 0; i < pkt->frame_count - 1; i++) {
     203       12922 :                 frame_bytes = xiph_lacing_16bit(&ptr, end);
     204       12922 :                 if (frame_bytes < 0)
     205           0 :                     goto fail;
     206       12922 :                 pkt->frame_size[i] = frame_bytes;
     207       12922 :                 total_bytes += frame_bytes;
     208             :             }
     209             : 
     210        4350 :             if (self_delimiting) {
     211           0 :                 int len = xiph_lacing_16bit(&ptr, end);
     212           0 :                 if (len < 0 || len + total_bytes + padding > end - ptr)
     213             :                     goto fail;
     214           0 :                 end      = ptr + total_bytes + len + padding;
     215           0 :                 buf_size = end - buf;
     216             :             }
     217             : 
     218        4350 :             frame_bytes = end - ptr - padding;
     219        4350 :             if (total_bytes > frame_bytes)
     220           0 :                 goto fail;
     221        4350 :             pkt->frame_offset[0] = ptr - buf;
     222       17272 :             for (i = 1; i < pkt->frame_count; i++)
     223       12922 :                 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
     224        4350 :             pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
     225             :         } else {
     226             :             /* for CBR, the remaining packet bytes are divided evenly between
     227             :                the frames */
     228        1850 :             if (self_delimiting) {
     229           0 :                 frame_bytes = xiph_lacing_16bit(&ptr, end);
     230           0 :                 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
     231             :                     goto fail;
     232           0 :                 end      = ptr + pkt->frame_count * frame_bytes + padding;
     233           0 :                 buf_size = end - buf;
     234             :             } else {
     235        1850 :                 frame_bytes = end - ptr - padding;
     236        3700 :                 if (frame_bytes % pkt->frame_count ||
     237        1850 :                     frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
     238             :                     goto fail;
     239        1850 :                 frame_bytes /= pkt->frame_count;
     240             :             }
     241             : 
     242        1850 :             pkt->frame_offset[0] = ptr - buf;
     243        1850 :             pkt->frame_size[0]   = frame_bytes;
     244        2630 :             for (i = 1; i < pkt->frame_count; i++) {
     245         780 :                 pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
     246         780 :                 pkt->frame_size[i]   = frame_bytes;
     247             :             }
     248             :         }
     249             :     }
     250             : 
     251       48847 :     pkt->packet_size = buf_size;
     252       48847 :     pkt->data_size   = pkt->packet_size - padding;
     253             : 
     254             :     /* total packet duration cannot be larger than 120ms */
     255       48847 :     pkt->frame_duration = opus_frame_duration[pkt->config];
     256       48847 :     if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
     257           0 :         goto fail;
     258             : 
     259             :     /* set mode and bandwidth */
     260       48847 :     if (pkt->config < 12) {
     261        9052 :         pkt->mode = OPUS_MODE_SILK;
     262        9052 :         pkt->bandwidth = pkt->config >> 2;
     263       39795 :     } else if (pkt->config < 16) {
     264        9747 :         pkt->mode = OPUS_MODE_HYBRID;
     265        9747 :         pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
     266             :     } else {
     267       30048 :         pkt->mode = OPUS_MODE_CELT;
     268       30048 :         pkt->bandwidth = (pkt->config - 16) >> 2;
     269             :         /* skip medium band */
     270       30048 :         if (pkt->bandwidth)
     271       26206 :             pkt->bandwidth++;
     272             :     }
     273             : 
     274       48847 :     return 0;
     275             : 
     276           0 : fail:
     277           0 :     memset(pkt, 0, sizeof(*pkt));
     278           0 :     return AVERROR_INVALIDDATA;
     279             : }
     280             : 
     281         171 : static int channel_reorder_vorbis(int nb_channels, int channel_idx)
     282             : {
     283         171 :     return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
     284             : }
     285             : 
     286         143 : static int channel_reorder_unknown(int nb_channels, int channel_idx)
     287             : {
     288         143 :     return channel_idx;
     289             : }
     290             : 
     291          73 : av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
     292             :                                     OpusContext *s)
     293             : {
     294             :     static const uint8_t default_channel_map[2] = { 0, 1 };
     295             : 
     296          73 :     int (*channel_reorder)(int, int) = channel_reorder_unknown;
     297             : 
     298             :     const uint8_t *extradata, *channel_map;
     299             :     int extradata_size;
     300             :     int version, channels, map_type, streams, stereo_streams, i, j;
     301             :     uint64_t layout;
     302             : 
     303          73 :     if (!avctx->extradata) {
     304           1 :         if (avctx->channels > 2) {
     305           0 :             av_log(avctx, AV_LOG_ERROR,
     306             :                    "Multichannel configuration without extradata.\n");
     307           0 :             return AVERROR(EINVAL);
     308             :         }
     309           1 :         extradata      = opus_default_extradata;
     310           1 :         extradata_size = sizeof(opus_default_extradata);
     311             :     } else {
     312          72 :         extradata = avctx->extradata;
     313          72 :         extradata_size = avctx->extradata_size;
     314             :     }
     315             : 
     316          73 :     if (extradata_size < 19) {
     317           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
     318             :                extradata_size);
     319           0 :         return AVERROR_INVALIDDATA;
     320             :     }
     321             : 
     322          73 :     version = extradata[8];
     323          73 :     if (version > 15) {
     324           0 :         avpriv_request_sample(avctx, "Extradata version %d", version);
     325           0 :         return AVERROR_PATCHWELCOME;
     326             :     }
     327             : 
     328          73 :     avctx->delay = AV_RL16(extradata + 10);
     329             : 
     330          73 :     channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
     331          73 :     if (!channels) {
     332           0 :         av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n");
     333           0 :         return AVERROR_INVALIDDATA;
     334             :     }
     335             : 
     336          73 :     s->gain_i = AV_RL16(extradata + 16);
     337          73 :     if (s->gain_i)
     338           0 :         s->gain = ff_exp10(s->gain_i / (20.0 * 256));
     339             : 
     340          73 :     map_type = extradata[18];
     341          73 :     if (!map_type) {
     342          67 :         if (channels > 2) {
     343           0 :             av_log(avctx, AV_LOG_ERROR,
     344             :                    "Channel mapping 0 is only specified for up to 2 channels\n");
     345           0 :             return AVERROR_INVALIDDATA;
     346             :         }
     347          67 :         layout         = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
     348          67 :         streams        = 1;
     349          67 :         stereo_streams = channels - 1;
     350          67 :         channel_map    = default_channel_map;
     351           6 :     } else if (map_type == 1 || map_type == 2 || map_type == 255) {
     352           6 :         if (extradata_size < 21 + channels) {
     353           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
     354             :                    extradata_size);
     355           0 :             return AVERROR_INVALIDDATA;
     356             :         }
     357             : 
     358           6 :         streams        = extradata[19];
     359           6 :         stereo_streams = extradata[20];
     360          12 :         if (!streams || stereo_streams > streams ||
     361           6 :             streams + stereo_streams > 255) {
     362           0 :             av_log(avctx, AV_LOG_ERROR,
     363             :                    "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
     364           0 :             return AVERROR_INVALIDDATA;
     365             :         }
     366             : 
     367           6 :         if (map_type == 1) {
     368           6 :             if (channels > 8) {
     369           0 :                 av_log(avctx, AV_LOG_ERROR,
     370             :                        "Channel mapping 1 is only specified for up to 8 channels\n");
     371           0 :                 return AVERROR_INVALIDDATA;
     372             :             }
     373           6 :             layout = ff_vorbis_channel_layouts[channels - 1];
     374           6 :             channel_reorder = channel_reorder_vorbis;
     375           0 :         } else if (map_type == 2) {
     376           0 :             int ambisonic_order = ff_sqrt(channels) - 1;
     377           0 :             if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
     378           0 :                 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
     379           0 :                 av_log(avctx, AV_LOG_ERROR,
     380             :                        "Channel mapping 2 is only specified for channel counts"
     381             :                        " which can be written as (n + 1)^2 or (n + 1)^2 + 2"
     382             :                        " for nonnegative integer n\n");
     383           0 :                 return AVERROR_INVALIDDATA;
     384             :             }
     385           0 :             if (channels > 227) {
     386           0 :                 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
     387           0 :                 return AVERROR_INVALIDDATA;
     388             :             }
     389           0 :             layout = 0;
     390             :         } else
     391           0 :             layout = 0;
     392             : 
     393           6 :         channel_map = extradata + 21;
     394             :     } else {
     395           0 :         avpriv_request_sample(avctx, "Mapping type %d", map_type);
     396           0 :         return AVERROR_PATCHWELCOME;
     397             :     }
     398             : 
     399          73 :     s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
     400          73 :     if (!s->channel_maps)
     401           0 :         return AVERROR(ENOMEM);
     402             : 
     403         220 :     for (i = 0; i < channels; i++) {
     404         147 :         ChannelMap *map = &s->channel_maps[i];
     405         147 :         uint8_t     idx = channel_map[channel_reorder(channels, i)];
     406             : 
     407         147 :         if (idx == 255) {
     408           0 :             map->silence = 1;
     409           0 :             continue;
     410         147 :         } else if (idx >= streams + stereo_streams) {
     411           0 :             av_log(avctx, AV_LOG_ERROR,
     412             :                    "Invalid channel map for output channel %d: %d\n", i, idx);
     413           0 :             av_freep(&s->channel_maps);
     414           0 :             return AVERROR_INVALIDDATA;
     415             :         }
     416             : 
     417             :         /* check that we did not see this index yet */
     418         147 :         map->copy = 0;
     419         314 :         for (j = 0; j < i; j++)
     420         167 :             if (channel_map[channel_reorder(channels, j)] == idx) {
     421           0 :                 map->copy     = 1;
     422           0 :                 map->copy_idx = j;
     423           0 :                 break;
     424             :             }
     425             : 
     426         147 :         if (idx < 2 * stereo_streams) {
     427         106 :             map->stream_idx  = idx / 2;
     428         106 :             map->channel_idx = idx & 1;
     429             :         } else {
     430          41 :             map->stream_idx  = idx - stereo_streams;
     431          41 :             map->channel_idx = 0;
     432             :         }
     433             :     }
     434             : 
     435          73 :     avctx->channels       = channels;
     436          73 :     avctx->channel_layout = layout;
     437          73 :     s->nb_streams         = streams;
     438          73 :     s->nb_stereo_streams  = stereo_streams;
     439             : 
     440          73 :     return 0;
     441             : }
     442             : 
     443       29869 : void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
     444             : {
     445             :     float lowband_scratch[8 * 22];
     446             :     float norm1[2 * 8 * 100];
     447       29869 :     float *norm2 = norm1 + 8 * 100;
     448             : 
     449       29869 :     int totalbits = (f->framebits << 3) - f->anticollapse_needed;
     450             : 
     451       29869 :     int update_lowband = 1;
     452       29869 :     int lowband_offset = 0;
     453             : 
     454             :     int i, j;
     455             : 
     456      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     457      508034 :         uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
     458      508034 :         int band_offset = ff_celt_freq_bands[i] << f->size;
     459      508034 :         int band_size   = ff_celt_freq_range[i] << f->size;
     460      508034 :         float *X = f->block[0].coeffs + band_offset;
     461      508034 :         float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL;
     462             :         float *norm_loc1, *norm_loc2;
     463             : 
     464      508034 :         int consumed = opus_rc_tell_frac(rc);
     465      508034 :         int effective_lowband = -1;
     466      508034 :         int b = 0;
     467             : 
     468             :         /* Compute how many bits we want to allocate to this band */
     469      508034 :         if (i != f->start_band)
     470      478165 :             f->remaining -= consumed;
     471      508034 :         f->remaining2 = totalbits - consumed - 1;
     472      508034 :         if (i <= f->coded_bands - 1) {
     473      457354 :             int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
     474      457354 :             b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14);
     475             :         }
     476             : 
     477      542838 :         if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] ||
     478      512969 :             i == f->start_band + 1) && (update_lowband || lowband_offset == 0))
     479      387139 :             lowband_offset = i;
     480             : 
     481      508034 :         if (i == f->start_band + 1) {
     482             :             /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into
     483             :             the second to ensure the second band never has to use the LCG. */
     484       29869 :             int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size;
     485             : 
     486       29869 :             memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float));
     487             : 
     488       29869 :             if (f->channels == 2)
     489       18731 :                 memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float));
     490             :         }
     491             : 
     492             :         /* Get a conservative estimate of the collapse_mask's for the bands we're
     493             :            going to be folding from. */
     494      579082 :         if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE ||
     495      142096 :                                     f->blocks > 1 || f->tf_change[i] < 0)) {
     496             :             int foldstart, foldend;
     497             : 
     498             :             /* This ensures we never repeat spectral content within one band */
     499      418948 :             effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
     500             :                                       ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
     501      418948 :             foldstart = lowband_offset;
     502      418948 :             while (ff_celt_freq_bands[--foldstart] > effective_lowband);
     503      418948 :             foldend = lowband_offset - 1;
     504      418948 :             while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]);
     505             : 
     506      418948 :             cm[0] = cm[1] = 0;
     507     1064612 :             for (j = foldstart; j < foldend; j++) {
     508      645664 :                 cm[0] |= f->block[0].collapse_masks[j];
     509      645664 :                 cm[1] |= f->block[f->channels - 1].collapse_masks[j];
     510             :             }
     511             :         }
     512             : 
     513      508034 :         if (f->dual_stereo && i == f->intensity_stereo) {
     514             :             /* Switch off dual stereo to do intensity */
     515        1670 :             f->dual_stereo = 0;
     516      570836 :             for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++)
     517      569166 :                 norm1[j] = (norm1[j] + norm2[j]) / 2;
     518             :         }
     519             : 
     520      508034 :         norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL;
     521      508034 :         norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL;
     522             : 
     523      508034 :         if (f->dual_stereo) {
     524      175395 :             cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1,
     525       70158 :                                        f->blocks, norm_loc1, f->size,
     526       35079 :                                        norm1 + band_offset, 0, 1.0f,
     527       35079 :                                        lowband_scratch, cm[0]);
     528             : 
     529      175395 :             cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1,
     530       70158 :                                        f->blocks, norm_loc2, f->size,
     531       35079 :                                        norm2 + band_offset, 0, 1.0f,
     532       35079 :                                        lowband_scratch, cm[1]);
     533             :         } else {
     534     2364775 :             cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X,    Y, band_size, b >> 0,
     535      945910 :                                        f->blocks, norm_loc1, f->size,
     536      472955 :                                        norm1 + band_offset, 0, 1.0f,
     537      472955 :                                        lowband_scratch, cm[0] | cm[1]);
     538      472955 :             cm[1] = cm[0];
     539             :         }
     540             : 
     541      508034 :         f->block[0].collapse_masks[i]               = (uint8_t)cm[0];
     542      508034 :         f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
     543      508034 :         f->remaining += f->pulses[i] + consumed;
     544             : 
     545             :         /* Update the folding position only as long as we have 1 bit/sample depth */
     546      508034 :         update_lowband = (b > band_size << 3);
     547             :     }
     548       29869 : }
     549             : 
     550             : #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2)
     551             : 
     552       29869 : void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
     553             : {
     554             :     int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
     555       29869 :     int skip_startband      = f->start_band;
     556       29869 :     int skip_bit            = 0;
     557       29869 :     int intensitystereo_bit = 0;
     558       29869 :     int dualstereo_bit      = 0;
     559       29869 :     int dynalloc            = 6;
     560       29869 :     int extrabits           = 0;
     561             : 
     562       29869 :     int boost[CELT_MAX_BANDS] = { 0 };
     563             :     int trim_offset[CELT_MAX_BANDS];
     564             :     int threshold[CELT_MAX_BANDS];
     565             :     int bits1[CELT_MAX_BANDS];
     566             :     int bits2[CELT_MAX_BANDS];
     567             : 
     568             :     /* Spread */
     569       29869 :     if (opus_rc_tell(rc) + 4 <= f->framebits) {
     570       29735 :         if (encode)
     571           0 :             ff_opus_rc_enc_cdf(rc, f->spread, ff_celt_model_spread);
     572             :         else
     573       29735 :             f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
     574             :     } else {
     575         134 :         f->spread = CELT_SPREAD_NORMAL;
     576             :     }
     577             : 
     578             :     /* Initialize static allocation caps */
     579      657118 :     for (i = 0; i < CELT_MAX_BANDS; i++)
     580      627249 :         f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]);
     581             : 
     582             :     /* Band boosts */
     583       29869 :     tbits_8ths = f->framebits << 3;
     584      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     585      508034 :         int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
     586      508034 :         int b_dynalloc = dynalloc;
     587      508034 :         int boost_amount = f->alloc_boost[i];
     588      508034 :         quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
     589             : 
     590     1032505 :         while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < f->caps[i]) {
     591             :             int is_boost;
     592      521981 :             if (encode) {
     593           0 :                 is_boost = boost_amount--;
     594           0 :                 ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
     595             :             } else {
     596      521981 :                 is_boost = ff_opus_rc_dec_log(rc, b_dynalloc);
     597             :             }
     598             : 
     599      521981 :             if (!is_boost)
     600      505544 :                 break;
     601             : 
     602       16437 :             boost[i]   += quanta;
     603       16437 :             tbits_8ths -= quanta;
     604             : 
     605       16437 :             b_dynalloc = 1;
     606             :         }
     607             : 
     608      508034 :         if (boost[i])
     609       12537 :             dynalloc = FFMAX(dynalloc - 1, 2);
     610             :     }
     611             : 
     612             :     /* Allocation trim */
     613       29869 :     if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
     614       29731 :         if (encode)
     615           0 :             ff_opus_rc_enc_cdf(rc, f->alloc_trim, ff_celt_model_alloc_trim);
     616             :         else
     617       29731 :             f->alloc_trim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
     618             : 
     619             :     /* Anti-collapse bit reservation */
     620       29869 :     tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
     621       29869 :     f->anticollapse_needed = 0;
     622       29869 :     if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3))
     623        3123 :         f->anticollapse_needed = 1 << 3;
     624       29869 :     tbits_8ths -= f->anticollapse_needed;
     625             : 
     626             :     /* Band skip bit reservation */
     627       29869 :     if (tbits_8ths >= 1 << 3)
     628       29737 :         skip_bit = 1 << 3;
     629       29869 :     tbits_8ths -= skip_bit;
     630             : 
     631             :     /* Intensity/dual stereo bit reservation */
     632       29869 :     if (f->channels == 2) {
     633       18731 :         intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
     634       18731 :         if (intensitystereo_bit <= tbits_8ths) {
     635       18660 :             tbits_8ths -= intensitystereo_bit;
     636       18660 :             if (tbits_8ths >= 1 << 3) {
     637       18656 :                 dualstereo_bit = 1 << 3;
     638       18656 :                 tbits_8ths -= 1 << 3;
     639             :             }
     640             :         } else {
     641          71 :             intensitystereo_bit = 0;
     642             :         }
     643             :     }
     644             : 
     645             :     /* Trim offsets */
     646      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     647      508034 :         int trim     = f->alloc_trim - 5 - f->size;
     648      508034 :         int band     = ff_celt_freq_range[i] * (f->end_band - i - 1);
     649      508034 :         int duration = f->size + 3;
     650      508034 :         int scale    = duration + f->channels - 1;
     651             : 
     652             :         /* PVQ minimum allocation threshold, below this value the band is
     653             :          * skipped */
     654      508034 :         threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
     655             :                              f->channels << 3);
     656             : 
     657      508034 :         trim_offset[i] = trim * (band << scale) >> 6;
     658             : 
     659      508034 :         if (ff_celt_freq_range[i] << f->size == 1)
     660      109616 :             trim_offset[i] -= f->channels << 3;
     661             :     }
     662             : 
     663             :     /* Bisection */
     664       29869 :     low  = 1;
     665       29869 :     high = CELT_VECTORS - 1;
     666      171283 :     while (low <= high) {
     667      111545 :         int center = (low + high) >> 1;
     668      111545 :         done = total = 0;
     669             : 
     670     2012955 :         for (i = f->end_band - 1; i >= f->start_band; i--) {
     671     1901410 :             bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]);
     672             : 
     673     1901410 :             if (bandbits)
     674     1811644 :                 bandbits = FFMAX(bandbits + trim_offset[i], 0);
     675     1901410 :             bandbits += boost[i];
     676             : 
     677     1901410 :             if (bandbits >= threshold[i] || done) {
     678     1750453 :                 done = 1;
     679     1750453 :                 total += FFMIN(bandbits, f->caps[i]);
     680      150957 :             } else if (bandbits >= f->channels << 3) {
     681       31438 :                 total += f->channels << 3;
     682             :             }
     683             :         }
     684             : 
     685      111545 :         if (total > tbits_8ths)
     686       39866 :             high = center - 1;
     687             :         else
     688       71679 :             low = center + 1;
     689             :     }
     690       29869 :     high = low--;
     691             : 
     692             :     /* Bisection */
     693      537903 :     for (i = f->start_band; i < f->end_band; i++) {
     694      508034 :         bits1[i] = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]);
     695      978297 :         bits2[i] = high >= CELT_VECTORS ? f->caps[i] :
     696      470263 :                    NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]);
     697             : 
     698      508034 :         if (bits1[i])
     699      464983 :             bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0);
     700      508034 :         if (bits2[i])
     701      496152 :             bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0);
     702             : 
     703      508034 :         if (low)
     704      485717 :             bits1[i] += boost[i];
     705      508034 :         bits2[i] += boost[i];
     706             : 
     707      508034 :         if (boost[i])
     708       12537 :             skip_startband = i;
     709      508034 :         bits2[i] = FFMAX(bits2[i] - bits1[i], 0);
     710             :     }
     711             : 
     712             :     /* Bisection */
     713       29869 :     low  = 0;
     714       29869 :     high = 1 << CELT_ALLOC_STEPS;
     715      209083 :     for (i = 0; i < CELT_ALLOC_STEPS; i++) {
     716      179214 :         int center = (low + high) >> 1;
     717      179214 :         done = total = 0;
     718             : 
     719     3227418 :         for (j = f->end_band - 1; j >= f->start_band; j--) {
     720     3048204 :             bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
     721             : 
     722     3048204 :             if (bandbits >= threshold[j] || done) {
     723     2837209 :                 done = 1;
     724     2837209 :                 total += FFMIN(bandbits, f->caps[j]);
     725      210995 :             } else if (bandbits >= f->channels << 3)
     726       52284 :                 total += f->channels << 3;
     727             :         }
     728      179214 :         if (total > tbits_8ths)
     729       85578 :             high = center;
     730             :         else
     731       93636 :             low = center;
     732             :     }
     733             : 
     734             :     /* Bisection */
     735       29869 :     done = total = 0;
     736      537903 :     for (i = f->end_band - 1; i >= f->start_band; i--) {
     737      508034 :         bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
     738             : 
     739      508034 :         if (bandbits >= threshold[i] || done)
     740      472034 :             done = 1;
     741             :         else
     742       72000 :             bandbits = (bandbits >= f->channels << 3) ?
     743       36000 :             f->channels << 3 : 0;
     744             : 
     745      508034 :         bandbits     = FFMIN(bandbits, f->caps[i]);
     746      508034 :         f->pulses[i] = bandbits;
     747      508034 :         total      += bandbits;
     748             :     }
     749             : 
     750             :     /* Band skipping */
     751       80549 :     for (f->coded_bands = f->end_band; ; f->coded_bands--) {
     752             :         int allocation;
     753       80549 :         j = f->coded_bands - 1;
     754             : 
     755       80549 :         if (j == skip_startband) {
     756             :             /* all remaining bands are not skipped */
     757        1645 :             tbits_8ths += skip_bit;
     758        1645 :             break;
     759             :         }
     760             : 
     761             :         /* determine the number of bits available for coding "do not skip" markers */
     762       78904 :         remaining   = tbits_8ths - total;
     763       78904 :         bandbits    = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
     764       78904 :         remaining  -= bandbits  * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
     765       78904 :         allocation  = f->pulses[j] + bandbits * ff_celt_freq_range[j];
     766       78904 :         allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0);
     767             : 
     768             :         /* a "do not skip" marker is only coded if the allocation is
     769             :          * above the chosen threshold */
     770       78904 :         if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
     771             :             int do_not_skip;
     772       38108 :             if (encode) {
     773           0 :                 do_not_skip = f->coded_bands <= f->skip_band_floor;
     774           0 :                 ff_opus_rc_enc_log(rc, do_not_skip, 1);
     775             :             } else {
     776       38108 :                 do_not_skip = ff_opus_rc_dec_log(rc, 1);
     777             :             }
     778             : 
     779       38108 :             if (do_not_skip)
     780       28224 :                 break;
     781             : 
     782        9884 :             total      += 1 << 3;
     783        9884 :             allocation -= 1 << 3;
     784             :         }
     785             : 
     786             :         /* the band is skipped, so reclaim its bits */
     787       50680 :         total -= f->pulses[j];
     788       50680 :         if (intensitystereo_bit) {
     789       34519 :             total -= intensitystereo_bit;
     790       34519 :             intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
     791       34519 :             total += intensitystereo_bit;
     792             :         }
     793             : 
     794       50680 :         total += f->pulses[j] = (allocation >= f->channels << 3) ? f->channels << 3 : 0;
     795             :     }
     796             : 
     797             :     /* IS start band */
     798       29869 :     if (encode) {
     799           0 :         if (intensitystereo_bit) {
     800           0 :             f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
     801           0 :             ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
     802             :         }
     803             :     } else {
     804       29869 :         f->intensity_stereo = f->dual_stereo = 0;
     805       29869 :         if (intensitystereo_bit)
     806       18660 :             f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
     807             :     }
     808             : 
     809             :     /* DS flag */
     810       29869 :     if (f->intensity_stereo <= f->start_band)
     811       13153 :         tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
     812       16716 :     else if (dualstereo_bit)
     813       16716 :         if (encode)
     814           0 :             ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
     815             :         else
     816       16716 :             f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
     817             : 
     818             :     /* Supply the remaining bits in this frame to lower bands */
     819       29869 :     remaining = tbits_8ths - total;
     820       29869 :     bandbits  = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
     821       29869 :     remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
     822      487223 :     for (i = f->start_band; i < f->coded_bands; i++) {
     823      457354 :         const int bits = FFMIN(remaining, ff_celt_freq_range[i]);
     824      457354 :         f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
     825      457354 :         remaining    -= bits;
     826             :     }
     827             : 
     828             :     /* Finally determine the allocation */
     829      487223 :     for (i = f->start_band; i < f->coded_bands; i++) {
     830      457354 :         int N = ff_celt_freq_range[i] << f->size;
     831      457354 :         int prev_extra = extrabits;
     832      457354 :         f->pulses[i] += extrabits;
     833             : 
     834      457354 :         if (N > 1) {
     835             :             int dof;        /* degrees of freedom */
     836             :             int temp;       /* dof * channels * log(dof) */
     837             :             int fine_bits;
     838             :             int max_bits;
     839             :             int offset;     /* fine energy quantization offset, i.e.
     840             :                              * extra bits assigned over the standard
     841             :                              * totalbits/dof */
     842             : 
     843      350835 :             extrabits = FFMAX(f->pulses[i] - f->caps[i], 0);
     844      350835 :             f->pulses[i] -= extrabits;
     845             : 
     846             :             /* intensity stereo makes use of an extra degree of freedom */
     847      350835 :             dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
     848      350835 :             temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
     849      350835 :             offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
     850      350835 :             if (N == 2) /* dof=2 is the only case that doesn't fit the model */
     851       83650 :                 offset += dof << 1;
     852             : 
     853             :             /* grant an additional bias for the first and second pulses */
     854      350835 :             if (f->pulses[i] + offset < 2 * (dof << 3))
     855      247060 :                 offset += temp >> 2;
     856      103775 :             else if (f->pulses[i] + offset < 3 * (dof << 3))
     857       49959 :                 offset += temp >> 3;
     858             : 
     859      350835 :             fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
     860      350835 :             max_bits  = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
     861      350835 :             max_bits  = FFMAX(max_bits, 0);
     862      350835 :             f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
     863             : 
     864             :             /* If fine_bits was rounded down or capped,
     865             :              * give priority for the final fine energy pass */
     866      350835 :             f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
     867             : 
     868             :             /* the remaining bits are assigned to PVQ */
     869      350835 :             f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
     870             :         } else {
     871             :             /* all bits go to fine energy except for the sign bit */
     872      106519 :             extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0);
     873      106519 :             f->pulses[i] -= extrabits;
     874      106519 :             f->fine_bits[i] = 0;
     875      106519 :             f->fine_priority[i] = 1;
     876             :         }
     877             : 
     878             :         /* hand back a limited number of extra fine energy bits to this band */
     879      457354 :         if (extrabits > 0) {
     880      123173 :             int fineextra = FFMIN(extrabits >> (f->channels + 2),
     881             :                                   CELT_MAX_FINE_BITS - f->fine_bits[i]);
     882      123173 :             f->fine_bits[i] += fineextra;
     883             : 
     884      123173 :             fineextra <<= f->channels + 2;
     885      123173 :             f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
     886      123173 :             extrabits -= fineextra;
     887             :         }
     888             :     }
     889       29869 :     f->remaining = extrabits;
     890             : 
     891             :     /* skipped bands dedicate all of their bits for fine energy */
     892       80549 :     for (; i < f->end_band; i++) {
     893       50680 :         f->fine_bits[i]     = f->pulses[i] >> (f->channels - 1) >> 3;
     894       50680 :         f->pulses[i]        = 0;
     895       50680 :         f->fine_priority[i] = f->fine_bits[i] < 1;
     896             :     }
     897       29869 : }

Generated by: LCOV version 1.13