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