| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Opus decoder/encoder CELT functions | ||
| 3 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 4 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 5 | * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com> | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #ifndef AVCODEC_OPUS_CELT_H | ||
| 25 | #define AVCODEC_OPUS_CELT_H | ||
| 26 | |||
| 27 | #include <stdint.h> | ||
| 28 | |||
| 29 | #include "libavcodec/avcodec.h" | ||
| 30 | |||
| 31 | #include "dsp.h" | ||
| 32 | #include "rc.h" | ||
| 33 | |||
| 34 | #include "libavutil/float_dsp.h" | ||
| 35 | #include "libavutil/libm.h" | ||
| 36 | #include "libavutil/mem_internal.h" | ||
| 37 | #include "libavutil/tx.h" | ||
| 38 | |||
| 39 | #define CELT_SHORT_BLOCKSIZE 120 | ||
| 40 | #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE | ||
| 41 | #define CELT_MAX_LOG_BLOCKS 3 | ||
| 42 | #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS)) | ||
| 43 | #define CELT_MAX_BANDS 21 | ||
| 44 | |||
| 45 | #define CELT_VECTORS 11 | ||
| 46 | #define CELT_ALLOC_STEPS 6 | ||
| 47 | #define CELT_FINE_OFFSET 21 | ||
| 48 | #define CELT_MAX_FINE_BITS 8 | ||
| 49 | #define CELT_NORM_SCALE 16384 | ||
| 50 | #define CELT_QTHETA_OFFSET 4 | ||
| 51 | #define CELT_QTHETA_OFFSET_TWOPHASE 16 | ||
| 52 | #define CELT_POSTFILTER_MINPERIOD 15 | ||
| 53 | #define CELT_ENERGY_SILENCE (-28.0f) | ||
| 54 | |||
| 55 | enum CeltSpread { | ||
| 56 | CELT_SPREAD_NONE, | ||
| 57 | CELT_SPREAD_LIGHT, | ||
| 58 | CELT_SPREAD_NORMAL, | ||
| 59 | CELT_SPREAD_AGGRESSIVE | ||
| 60 | }; | ||
| 61 | |||
| 62 | enum CeltBlockSize { | ||
| 63 | CELT_BLOCK_120, | ||
| 64 | CELT_BLOCK_240, | ||
| 65 | CELT_BLOCK_480, | ||
| 66 | CELT_BLOCK_960, | ||
| 67 | |||
| 68 | CELT_BLOCK_NB | ||
| 69 | }; | ||
| 70 | |||
| 71 | typedef struct CeltBlock { | ||
| 72 | float energy[CELT_MAX_BANDS]; | ||
| 73 | float lin_energy[CELT_MAX_BANDS]; | ||
| 74 | float error_energy[CELT_MAX_BANDS]; | ||
| 75 | float prev_energy[2][CELT_MAX_BANDS]; | ||
| 76 | |||
| 77 | uint8_t collapse_masks[CELT_MAX_BANDS]; | ||
| 78 | |||
| 79 | /* buffer for mdct output + postfilter */ | ||
| 80 | DECLARE_ALIGNED(32, float, buf)[2048]; | ||
| 81 | DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE]; | ||
| 82 | |||
| 83 | /* Used by the encoder */ | ||
| 84 | DECLARE_ALIGNED(32, float, overlap)[FFALIGN(CELT_OVERLAP, 16)]; | ||
| 85 | DECLARE_ALIGNED(32, float, samples)[FFALIGN(CELT_MAX_FRAME_SIZE, 16)]; | ||
| 86 | |||
| 87 | /* postfilter parameters */ | ||
| 88 | int pf_period_new; | ||
| 89 | float pf_gains_new[3]; | ||
| 90 | int pf_period; | ||
| 91 | float pf_gains[3]; | ||
| 92 | int pf_period_old; | ||
| 93 | float pf_gains_old[3]; | ||
| 94 | |||
| 95 | float emph_coeff; | ||
| 96 | } CeltBlock; | ||
| 97 | |||
| 98 | typedef struct CeltFrame { | ||
| 99 | // constant values that do not change during context lifetime | ||
| 100 | AVCodecContext *avctx; | ||
| 101 | AVTXContext *tx[4]; | ||
| 102 | av_tx_fn tx_fn[4]; | ||
| 103 | AVFloatDSPContext *dsp; | ||
| 104 | CeltBlock block[2]; | ||
| 105 | struct CeltPVQ *pvq; | ||
| 106 | OpusDSP opusdsp; | ||
| 107 | int channels; | ||
| 108 | int output_channels; | ||
| 109 | int apply_phase_inv; | ||
| 110 | |||
| 111 | enum CeltBlockSize size; | ||
| 112 | int start_band; | ||
| 113 | int end_band; | ||
| 114 | int coded_bands; | ||
| 115 | int transient; | ||
| 116 | int pfilter; | ||
| 117 | int skip_band_floor; | ||
| 118 | int tf_select; | ||
| 119 | int alloc_trim; | ||
| 120 | int alloc_boost[CELT_MAX_BANDS]; | ||
| 121 | int blocks; /* number of iMDCT blocks in the frame, depends on transient */ | ||
| 122 | int blocksize; /* size of each block */ | ||
| 123 | int silence; /* Frame is filled with silence */ | ||
| 124 | int anticollapse_needed; /* Whether to expect an anticollapse bit */ | ||
| 125 | int anticollapse; /* Encoded anticollapse bit */ | ||
| 126 | int intensity_stereo; | ||
| 127 | int dual_stereo; | ||
| 128 | int flushed; | ||
| 129 | uint32_t seed; | ||
| 130 | enum CeltSpread spread; | ||
| 131 | |||
| 132 | /* Encoder PF coeffs */ | ||
| 133 | int pf_octave; | ||
| 134 | int pf_period; | ||
| 135 | int pf_tapset; | ||
| 136 | float pf_gain; | ||
| 137 | |||
| 138 | /* Bit allocation */ | ||
| 139 | int framebits; | ||
| 140 | int remaining; | ||
| 141 | int remaining2; | ||
| 142 | int caps [CELT_MAX_BANDS]; | ||
| 143 | int fine_bits [CELT_MAX_BANDS]; | ||
| 144 | int fine_priority[CELT_MAX_BANDS]; | ||
| 145 | int pulses [CELT_MAX_BANDS]; | ||
| 146 | int tf_change [CELT_MAX_BANDS]; | ||
| 147 | } CeltFrame; | ||
| 148 | |||
| 149 | /* LCG for noise generation */ | ||
| 150 | 2097968 | static av_always_inline uint32_t celt_rng(CeltFrame *f) | |
| 151 | { | ||
| 152 | 2097968 | f->seed = 1664525 * f->seed + 1013904223; | |
| 153 | 2097968 | return f->seed; | |
| 154 | } | ||
| 155 | |||
| 156 | 63379 | static av_always_inline void celt_renormalize_vector(float *X, int N, float gain) | |
| 157 | { | ||
| 158 | int i; | ||
| 159 | 63379 | float g = 1e-15f; | |
| 160 |
2/2✓ Branch 0 taken 2283710 times.
✓ Branch 1 taken 63379 times.
|
2347089 | for (i = 0; i < N; i++) |
| 161 | 2283710 | g += X[i] * X[i]; | |
| 162 | 63379 | g = gain / sqrtf(g); | |
| 163 | |||
| 164 |
2/2✓ Branch 0 taken 2283710 times.
✓ Branch 1 taken 63379 times.
|
2347089 | for (i = 0; i < N; i++) |
| 165 | 2283710 | X[i] *= g; | |
| 166 | 63379 | } | |
| 167 | |||
| 168 | int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, | ||
| 169 | int apply_phase_inv); | ||
| 170 | |||
| 171 | void ff_celt_free(CeltFrame **f); | ||
| 172 | |||
| 173 | void ff_celt_flush(CeltFrame *f); | ||
| 174 | |||
| 175 | int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, | ||
| 176 | int coded_channels, int frame_size, int startband, int endband); | ||
| 177 | |||
| 178 | /* Encode or decode CELT bands */ | ||
| 179 | void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc); | ||
| 180 | |||
| 181 | /* Encode or decode CELT bitallocation */ | ||
| 182 | void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode); | ||
| 183 | |||
| 184 | #endif /* AVCODEC_OPUS_CELT_H */ | ||
| 185 |