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 "avcodec.h" | ||
30 | #include "opusdsp.h" | ||
31 | #include "opus_rc.h" | ||
32 | |||
33 | #include "libavutil/float_dsp.h" | ||
34 | #include "libavutil/libm.h" | ||
35 | #include "libavutil/mem_internal.h" | ||
36 | #include "libavutil/tx.h" | ||
37 | |||
38 | #define CELT_SHORT_BLOCKSIZE 120 | ||
39 | #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE | ||
40 | #define CELT_MAX_LOG_BLOCKS 3 | ||
41 | #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS)) | ||
42 | #define CELT_MAX_BANDS 21 | ||
43 | |||
44 | #define CELT_VECTORS 11 | ||
45 | #define CELT_ALLOC_STEPS 6 | ||
46 | #define CELT_FINE_OFFSET 21 | ||
47 | #define CELT_MAX_FINE_BITS 8 | ||
48 | #define CELT_NORM_SCALE 16384 | ||
49 | #define CELT_QTHETA_OFFSET 4 | ||
50 | #define CELT_QTHETA_OFFSET_TWOPHASE 16 | ||
51 | #define CELT_POSTFILTER_MINPERIOD 15 | ||
52 | #define CELT_ENERGY_SILENCE (-28.0f) | ||
53 | |||
54 | enum CeltSpread { | ||
55 | CELT_SPREAD_NONE, | ||
56 | CELT_SPREAD_LIGHT, | ||
57 | CELT_SPREAD_NORMAL, | ||
58 | CELT_SPREAD_AGGRESSIVE | ||
59 | }; | ||
60 | |||
61 | enum CeltBlockSize { | ||
62 | CELT_BLOCK_120, | ||
63 | CELT_BLOCK_240, | ||
64 | CELT_BLOCK_480, | ||
65 | CELT_BLOCK_960, | ||
66 | |||
67 | CELT_BLOCK_NB | ||
68 | }; | ||
69 | |||
70 | typedef struct CeltBlock { | ||
71 | float energy[CELT_MAX_BANDS]; | ||
72 | float lin_energy[CELT_MAX_BANDS]; | ||
73 | float error_energy[CELT_MAX_BANDS]; | ||
74 | float prev_energy[2][CELT_MAX_BANDS]; | ||
75 | |||
76 | uint8_t collapse_masks[CELT_MAX_BANDS]; | ||
77 | |||
78 | /* buffer for mdct output + postfilter */ | ||
79 | DECLARE_ALIGNED(32, float, buf)[2048]; | ||
80 | DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE]; | ||
81 | |||
82 | /* Used by the encoder */ | ||
83 | DECLARE_ALIGNED(32, float, overlap)[FFALIGN(CELT_OVERLAP, 16)]; | ||
84 | DECLARE_ALIGNED(32, float, samples)[FFALIGN(CELT_MAX_FRAME_SIZE, 16)]; | ||
85 | |||
86 | /* postfilter parameters */ | ||
87 | int pf_period_new; | ||
88 | float pf_gains_new[3]; | ||
89 | int pf_period; | ||
90 | float pf_gains[3]; | ||
91 | int pf_period_old; | ||
92 | float pf_gains_old[3]; | ||
93 | |||
94 | float emph_coeff; | ||
95 | } CeltBlock; | ||
96 | |||
97 | typedef struct CeltFrame { | ||
98 | // constant values that do not change during context lifetime | ||
99 | AVCodecContext *avctx; | ||
100 | AVTXContext *tx[4]; | ||
101 | av_tx_fn tx_fn[4]; | ||
102 | AVFloatDSPContext *dsp; | ||
103 | CeltBlock block[2]; | ||
104 | struct CeltPVQ *pvq; | ||
105 | OpusDSP opusdsp; | ||
106 | int channels; | ||
107 | int output_channels; | ||
108 | int apply_phase_inv; | ||
109 | |||
110 | enum CeltBlockSize size; | ||
111 | int start_band; | ||
112 | int end_band; | ||
113 | int coded_bands; | ||
114 | int transient; | ||
115 | int pfilter; | ||
116 | int skip_band_floor; | ||
117 | int tf_select; | ||
118 | int alloc_trim; | ||
119 | int alloc_boost[CELT_MAX_BANDS]; | ||
120 | int blocks; /* number of iMDCT blocks in the frame, depends on transient */ | ||
121 | int blocksize; /* size of each block */ | ||
122 | int silence; /* Frame is filled with silence */ | ||
123 | int anticollapse_needed; /* Whether to expect an anticollapse bit */ | ||
124 | int anticollapse; /* Encoded anticollapse bit */ | ||
125 | int intensity_stereo; | ||
126 | int dual_stereo; | ||
127 | int flushed; | ||
128 | uint32_t seed; | ||
129 | enum CeltSpread spread; | ||
130 | |||
131 | /* Encoder PF coeffs */ | ||
132 | int pf_octave; | ||
133 | int pf_period; | ||
134 | int pf_tapset; | ||
135 | float pf_gain; | ||
136 | |||
137 | /* Bit allocation */ | ||
138 | int framebits; | ||
139 | int remaining; | ||
140 | int remaining2; | ||
141 | int caps [CELT_MAX_BANDS]; | ||
142 | int fine_bits [CELT_MAX_BANDS]; | ||
143 | int fine_priority[CELT_MAX_BANDS]; | ||
144 | int pulses [CELT_MAX_BANDS]; | ||
145 | int tf_change [CELT_MAX_BANDS]; | ||
146 | } CeltFrame; | ||
147 | |||
148 | /* LCG for noise generation */ | ||
149 | 2084987 | static av_always_inline uint32_t celt_rng(CeltFrame *f) | |
150 | { | ||
151 | 2084987 | f->seed = 1664525 * f->seed + 1013904223; | |
152 | 2084987 | return f->seed; | |
153 | } | ||
154 | |||
155 | 63014 | static av_always_inline void celt_renormalize_vector(float *X, int N, float gain) | |
156 | { | ||
157 | int i; | ||
158 | 63014 | float g = 1e-15f; | |
159 |
2/2✓ Branch 0 taken 2268702 times.
✓ Branch 1 taken 63014 times.
|
2331716 | for (i = 0; i < N; i++) |
160 | 2268702 | g += X[i] * X[i]; | |
161 | 63014 | g = gain / sqrtf(g); | |
162 | |||
163 |
2/2✓ Branch 0 taken 2268702 times.
✓ Branch 1 taken 63014 times.
|
2331716 | for (i = 0; i < N; i++) |
164 | 2268702 | X[i] *= g; | |
165 | 63014 | } | |
166 | |||
167 | int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, | ||
168 | int apply_phase_inv); | ||
169 | |||
170 | void ff_celt_free(CeltFrame **f); | ||
171 | |||
172 | void ff_celt_flush(CeltFrame *f); | ||
173 | |||
174 | int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, | ||
175 | int coded_channels, int frame_size, int startband, int endband); | ||
176 | |||
177 | /* Encode or decode CELT bands */ | ||
178 | void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc); | ||
179 | |||
180 | /* Encode or decode CELT bitallocation */ | ||
181 | void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode); | ||
182 | |||
183 | #endif /* AVCODEC_OPUS_CELT_H */ | ||
184 |