Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/opus_celt.c |
Date: | 2022-07-07 01:21:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 293 | 315 | 93.0% |
Branches: | 190 | 208 | 91.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2012 Andrew D'Addesio | ||
3 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
4 | * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com> | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file | ||
25 | * Opus CELT decoder | ||
26 | */ | ||
27 | |||
28 | #include "opus_celt.h" | ||
29 | #include "opustab.h" | ||
30 | #include "opus_pvq.h" | ||
31 | |||
32 | /* Use the 2D z-transform to apply prediction in both the time domain (alpha) | ||
33 | * and the frequency domain (beta) */ | ||
34 | 29890 | static void celt_decode_coarse_energy(CeltFrame *f, OpusRangeCoder *rc) | |
35 | { | ||
36 | int i, j; | ||
37 | 29890 | float prev[2] = { 0 }; | |
38 | 29890 | float alpha = ff_celt_alpha_coef[f->size]; | |
39 | 29890 | float beta = ff_celt_beta_coef[f->size]; | |
40 | 29890 | const uint8_t *model = ff_celt_coarse_energy_dist[f->size][0]; | |
41 | |||
42 | /* intra frame */ | ||
43 |
4/4✓ Branch 1 taken 29764 times.
✓ Branch 2 taken 126 times.
✓ Branch 4 taken 5879 times.
✓ Branch 5 taken 23885 times.
|
29890 | if (opus_rc_tell(rc) + 3 <= f->framebits && ff_opus_rc_dec_log(rc, 3)) { |
44 | 5879 | alpha = 0.0f; | |
45 | 5879 | beta = 1.0f - (4915.0f/32768.0f); | |
46 | 5879 | model = ff_celt_coarse_energy_dist[f->size][1]; | |
47 | } | ||
48 | |||
49 |
2/2✓ Branch 0 taken 627690 times.
✓ Branch 1 taken 29890 times.
|
657580 | for (i = 0; i < CELT_MAX_BANDS; i++) { |
50 |
2/2✓ Branch 0 taken 1021314 times.
✓ Branch 1 taken 627690 times.
|
1649004 | for (j = 0; j < f->channels; j++) { |
51 | 1021314 | CeltBlock *block = &f->block[j]; | |
52 | float value; | ||
53 | int available; | ||
54 | |||
55 |
4/4✓ Branch 0 taken 897452 times.
✓ Branch 1 taken 123862 times.
✓ Branch 2 taken 47132 times.
✓ Branch 3 taken 850320 times.
|
1021314 | if (i < f->start_band || i >= f->end_band) { |
56 | 170994 | block->energy[i] = 0.0; | |
57 | 170994 | continue; | |
58 | } | ||
59 | |||
60 | 850320 | available = f->framebits - opus_rc_tell(rc); | |
61 |
2/2✓ Branch 0 taken 846484 times.
✓ Branch 1 taken 3836 times.
|
850320 | if (available >= 15) { |
62 | /* decode using a Laplace distribution */ | ||
63 | 846484 | int k = FFMIN(i, 20) << 1; | |
64 | 846484 | value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6); | |
65 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 3723 times.
|
3836 | } else if (available >= 2) { |
66 | 113 | int x = ff_opus_rc_dec_cdf(rc, ff_celt_model_energy_small); | |
67 | 113 | value = (x>>1) ^ -(x&1); | |
68 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3720 times.
|
3723 | } else if (available >= 1) { |
69 | 3 | value = -(float)ff_opus_rc_dec_log(rc, 1); | |
70 | 3720 | } else value = -1; | |
71 | |||
72 |
2/2✓ Branch 0 taken 119050 times.
✓ Branch 1 taken 731270 times.
|
850320 | block->energy[i] = FFMAX(-9.0f, block->energy[i]) * alpha + prev[j] + value; |
73 | 850320 | prev[j] += beta * value; | |
74 | } | ||
75 | } | ||
76 | 29890 | } | |
77 | |||
78 | 29890 | static void celt_decode_fine_energy(CeltFrame *f, OpusRangeCoder *rc) | |
79 | { | ||
80 | int i; | ||
81 |
2/2✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
|
538296 | for (i = f->start_band; i < f->end_band; i++) { |
82 | int j; | ||
83 |
2/2✓ Branch 0 taken 95389 times.
✓ Branch 1 taken 413017 times.
|
508406 | if (!f->fine_bits[i]) |
84 | 95389 | continue; | |
85 | |||
86 |
2/2✓ Branch 0 taken 681824 times.
✓ Branch 1 taken 413017 times.
|
1094841 | for (j = 0; j < f->channels; j++) { |
87 | 681824 | CeltBlock *block = &f->block[j]; | |
88 | int q2; | ||
89 | float offset; | ||
90 | 681824 | q2 = ff_opus_rc_get_raw(rc, f->fine_bits[i]); | |
91 | 681824 | offset = (q2 + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f - 0.5f; | |
92 | 681824 | block->energy[i] += offset; | |
93 | } | ||
94 | } | ||
95 | 29890 | } | |
96 | |||
97 | 29890 | static void celt_decode_final_energy(CeltFrame *f, OpusRangeCoder *rc) | |
98 | { | ||
99 | int priority, i, j; | ||
100 | 29890 | int bits_left = f->framebits - opus_rc_tell(rc); | |
101 | |||
102 |
2/2✓ Branch 0 taken 59780 times.
✓ Branch 1 taken 29890 times.
|
89670 | for (priority = 0; priority < 2; priority++) { |
103 |
4/4✓ Branch 0 taken 156364 times.
✓ Branch 1 taken 6189 times.
✓ Branch 2 taken 102773 times.
✓ Branch 3 taken 53591 times.
|
162553 | for (i = f->start_band; i < f->end_band && bits_left >= f->channels; i++) { |
104 |
4/4✓ Branch 0 taken 55037 times.
✓ Branch 1 taken 47736 times.
✓ Branch 2 taken 17685 times.
✓ Branch 3 taken 37352 times.
|
102773 | if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS) |
105 | 65421 | continue; | |
106 | |||
107 |
2/2✓ Branch 0 taken 58847 times.
✓ Branch 1 taken 37352 times.
|
96199 | for (j = 0; j < f->channels; j++) { |
108 | int q2; | ||
109 | float offset; | ||
110 | 58847 | q2 = ff_opus_rc_get_raw(rc, 1); | |
111 | 58847 | offset = (q2 - 0.5f) * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f; | |
112 | 58847 | f->block[j].energy[i] += offset; | |
113 | 58847 | bits_left--; | |
114 | } | ||
115 | } | ||
116 | } | ||
117 | 29890 | } | |
118 | |||
119 | 29890 | static void celt_decode_tf_changes(CeltFrame *f, OpusRangeCoder *rc) | |
120 | { | ||
121 | 29890 | int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit; | |
122 |
2/2✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 25874 times.
|
29890 | int consumed, bits = f->transient ? 2 : 4; |
123 | |||
124 | 29890 | consumed = opus_rc_tell(rc); | |
125 |
4/4✓ Branch 0 taken 16184 times.
✓ Branch 1 taken 13706 times.
✓ Branch 2 taken 16087 times.
✓ Branch 3 taken 97 times.
|
29890 | tf_select_bit = (f->size != 0 && consumed+bits+1 <= f->framebits); |
126 | |||
127 |
2/2✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
|
538296 | for (i = f->start_band; i < f->end_band; i++) { |
128 |
2/2✓ Branch 0 taken 505909 times.
✓ Branch 1 taken 2497 times.
|
508406 | if (consumed+bits+tf_select_bit <= f->framebits) { |
129 | 505909 | diff ^= ff_opus_rc_dec_log(rc, bits); | |
130 | 505909 | consumed = opus_rc_tell(rc); | |
131 | 505909 | tf_changed |= diff; | |
132 | } | ||
133 | 508406 | f->tf_change[i] = diff; | |
134 |
2/2✓ Branch 0 taken 60363 times.
✓ Branch 1 taken 448043 times.
|
508406 | bits = f->transient ? 4 : 5; |
135 | } | ||
136 | |||
137 |
2/2✓ Branch 0 taken 16087 times.
✓ Branch 1 taken 13803 times.
|
29890 | if (tf_select_bit && ff_celt_tf_select[f->size][f->transient][0][tf_changed] != |
138 |
2/2✓ Branch 0 taken 8096 times.
✓ Branch 1 taken 7991 times.
|
16087 | ff_celt_tf_select[f->size][f->transient][1][tf_changed]) |
139 | 8096 | tf_select = ff_opus_rc_dec_log(rc, 1); | |
140 | |||
141 |
2/2✓ Branch 0 taken 508406 times.
✓ Branch 1 taken 29890 times.
|
538296 | for (i = f->start_band; i < f->end_band; i++) { |
142 | 508406 | f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]]; | |
143 | } | ||
144 | 29890 | } | |
145 | |||
146 | 48634 | static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data) | |
147 | { | ||
148 | int i, j; | ||
149 | |||
150 |
2/2✓ Branch 0 taken 850320 times.
✓ Branch 1 taken 48634 times.
|
898954 | for (i = f->start_band; i < f->end_band; i++) { |
151 | 850320 | float *dst = data + (ff_celt_freq_bands[i] << f->size); | |
152 | 850320 | float log_norm = block->energy[i] + ff_celt_mean_energy[i]; | |
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 850320 times.
|
850320 | float norm = exp2f(FFMIN(log_norm, 32.0f)); |
154 | |||
155 |
2/2✓ Branch 0 taken 12325540 times.
✓ Branch 1 taken 850320 times.
|
13175860 | for (j = 0; j < ff_celt_freq_range[i] << f->size; j++) |
156 | 12325540 | dst[j] *= norm; | |
157 | } | ||
158 | 48634 | } | |
159 | |||
160 | 89080 | static void celt_postfilter_apply_transition(CeltBlock *block, float *data) | |
161 | { | ||
162 | 89080 | const int T0 = block->pf_period_old; | |
163 | 89080 | const int T1 = block->pf_period; | |
164 | |||
165 | float g00, g01, g02; | ||
166 | float g10, g11, g12; | ||
167 | |||
168 | float x0, x1, x2, x3, x4; | ||
169 | |||
170 | int i; | ||
171 | |||
172 |
2/2✓ Branch 0 taken 41636 times.
✓ Branch 1 taken 47444 times.
|
89080 | if (block->pf_gains[0] == 0.0 && |
173 |
2/2✓ Branch 0 taken 37456 times.
✓ Branch 1 taken 4180 times.
|
41636 | block->pf_gains_old[0] == 0.0) |
174 | 37456 | return; | |
175 | |||
176 | 51624 | g00 = block->pf_gains_old[0]; | |
177 | 51624 | g01 = block->pf_gains_old[1]; | |
178 | 51624 | g02 = block->pf_gains_old[2]; | |
179 | 51624 | g10 = block->pf_gains[0]; | |
180 | 51624 | g11 = block->pf_gains[1]; | |
181 | 51624 | g12 = block->pf_gains[2]; | |
182 | |||
183 | 51624 | x1 = data[-T1 + 1]; | |
184 | 51624 | x2 = data[-T1]; | |
185 | 51624 | x3 = data[-T1 - 1]; | |
186 | 51624 | x4 = data[-T1 - 2]; | |
187 | |||
188 |
2/2✓ Branch 0 taken 6194880 times.
✓ Branch 1 taken 51624 times.
|
6246504 | for (i = 0; i < CELT_OVERLAP; i++) { |
189 | 6194880 | float w = ff_celt_window2[i]; | |
190 | 6194880 | x0 = data[i - T1 + 2]; | |
191 | |||
192 | 6194880 | data[i] += (1.0 - w) * g00 * data[i - T0] + | |
193 | 6194880 | (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) + | |
194 | 6194880 | (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) + | |
195 | 6194880 | w * g10 * x2 + | |
196 | 6194880 | w * g11 * (x1 + x3) + | |
197 | 6194880 | w * g12 * (x0 + x4); | |
198 | 6194880 | x4 = x3; | |
199 | 6194880 | x3 = x2; | |
200 | 6194880 | x2 = x1; | |
201 | 6194880 | x1 = x0; | |
202 | } | ||
203 | } | ||
204 | |||
205 | 56715 | static void celt_postfilter(CeltFrame *f, CeltBlock *block) | |
206 | { | ||
207 | 56715 | int len = f->blocksize * f->blocks; | |
208 | 56715 | const int filter_len = len - 2 * CELT_OVERLAP; | |
209 | |||
210 | 56715 | celt_postfilter_apply_transition(block, block->buf + 1024); | |
211 | |||
212 | 56715 | block->pf_period_old = block->pf_period; | |
213 | 56715 | memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains)); | |
214 | |||
215 | 56715 | block->pf_period = block->pf_period_new; | |
216 | 56715 | memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains)); | |
217 | |||
218 |
2/2✓ Branch 0 taken 32365 times.
✓ Branch 1 taken 24350 times.
|
56715 | if (len > CELT_OVERLAP) { |
219 | 32365 | celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP); | |
220 | |||
221 |
4/4✓ Branch 0 taken 16740 times.
✓ Branch 1 taken 15625 times.
✓ Branch 2 taken 10126 times.
✓ Branch 3 taken 6614 times.
|
32365 | if (block->pf_gains[0] > FLT_EPSILON && filter_len > 0) |
222 | 10126 | f->opusdsp.postfilter(block->buf + 1024 + 2 * CELT_OVERLAP, | |
223 | 10126 | block->pf_period, block->pf_gains, | |
224 | filter_len); | ||
225 | |||
226 | 32365 | block->pf_period_old = block->pf_period; | |
227 | 32365 | memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains)); | |
228 | } | ||
229 | |||
230 | 56715 | memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float)); | |
231 | 56715 | } | |
232 | |||
233 | 29890 | static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed) | |
234 | { | ||
235 | int i; | ||
236 | |||
237 | 29890 | memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new)); | |
238 | 29890 | memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new)); | |
239 | |||
240 |
4/4✓ Branch 0 taken 24952 times.
✓ Branch 1 taken 4938 times.
✓ Branch 2 taken 24826 times.
✓ Branch 3 taken 126 times.
|
29890 | if (f->start_band == 0 && consumed + 16 <= f->framebits) { |
241 | 24826 | int has_postfilter = ff_opus_rc_dec_log(rc, 1); | |
242 |
2/2✓ Branch 0 taken 15874 times.
✓ Branch 1 taken 8952 times.
|
24826 | if (has_postfilter) { |
243 | float gain; | ||
244 | int tapset, octave, period; | ||
245 | |||
246 | 15874 | octave = ff_opus_rc_dec_uint(rc, 6); | |
247 | 15874 | period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1; | |
248 | 15874 | gain = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1); | |
249 | 15874 | tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ? | |
250 |
1/2✓ Branch 0 taken 15874 times.
✗ Branch 1 not taken.
|
15874 | ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0; |
251 | |||
252 |
2/2✓ Branch 0 taken 31748 times.
✓ Branch 1 taken 15874 times.
|
47622 | for (i = 0; i < 2; i++) { |
253 | 31748 | CeltBlock *block = &f->block[i]; | |
254 | |||
255 | 31748 | block->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD); | |
256 | 31748 | block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0]; | |
257 | 31748 | block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1]; | |
258 | 31748 | block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2]; | |
259 | } | ||
260 | } | ||
261 | |||
262 | 24826 | consumed = opus_rc_tell(rc); | |
263 | } | ||
264 | |||
265 | 29890 | return consumed; | |
266 | } | ||
267 | |||
268 | 2760 | static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X) | |
269 | { | ||
270 | int i, j, k; | ||
271 | |||
272 |
2/2✓ Branch 0 taken 35299 times.
✓ Branch 1 taken 2760 times.
|
38059 | for (i = f->start_band; i < f->end_band; i++) { |
273 | 35299 | int renormalize = 0; | |
274 | float *xptr; | ||
275 | float prev[2]; | ||
276 | float Ediff, r; | ||
277 | float thresh, sqrt_1; | ||
278 | int depth; | ||
279 | |||
280 | /* depth in 1/8 bits */ | ||
281 | 35299 | depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size); | |
282 | 35299 | thresh = exp2f(-1.0 - 0.125f * depth); | |
283 | 35299 | sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size); | |
284 | |||
285 | 35299 | xptr = X + (ff_celt_freq_bands[i] << f->size); | |
286 | |||
287 | 35299 | prev[0] = block->prev_energy[0][i]; | |
288 | 35299 | prev[1] = block->prev_energy[1][i]; | |
289 |
2/2✓ Branch 0 taken 10023 times.
✓ Branch 1 taken 25276 times.
|
35299 | if (f->channels == 1) { |
290 | 10023 | CeltBlock *block1 = &f->block[1]; | |
291 | |||
292 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10009 times.
|
10023 | prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]); |
293 |
2/2✓ Branch 0 taken 137 times.
✓ Branch 1 taken 9886 times.
|
10023 | prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]); |
294 | } | ||
295 |
2/2✓ Branch 0 taken 16466 times.
✓ Branch 1 taken 18833 times.
|
35299 | Ediff = block->energy[i] - FFMIN(prev[0], prev[1]); |
296 |
2/2✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 32547 times.
|
35299 | Ediff = FFMAX(0, Ediff); |
297 | |||
298 | /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because | ||
299 | short blocks don't have the same energy as long */ | ||
300 | 35299 | r = exp2f(1 - Ediff); | |
301 |
2/2✓ Branch 0 taken 23738 times.
✓ Branch 1 taken 11561 times.
|
35299 | if (f->size == 3) |
302 | 23738 | r *= M_SQRT2; | |
303 |
2/2✓ Branch 0 taken 5652 times.
✓ Branch 1 taken 29647 times.
|
35299 | r = FFMIN(thresh, r) * sqrt_1; |
304 |
2/2✓ Branch 0 taken 236148 times.
✓ Branch 1 taken 35299 times.
|
271447 | for (k = 0; k < 1 << f->size; k++) { |
305 | /* Detect collapse */ | ||
306 |
2/2✓ Branch 0 taken 20169 times.
✓ Branch 1 taken 215979 times.
|
236148 | if (!(block->collapse_masks[i] & 1 << k)) { |
307 | /* Fill with noise */ | ||
308 |
2/2✓ Branch 0 taken 97755 times.
✓ Branch 1 taken 20169 times.
|
117924 | for (j = 0; j < ff_celt_freq_range[i]; j++) |
309 |
2/2✓ Branch 1 taken 48764 times.
✓ Branch 2 taken 48991 times.
|
97755 | xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r; |
310 | 20169 | renormalize = 1; | |
311 | } | ||
312 | } | ||
313 | |||
314 | /* We just added some energy, so we need to renormalize */ | ||
315 |
2/2✓ Branch 0 taken 8632 times.
✓ Branch 1 taken 26667 times.
|
35299 | if (renormalize) |
316 | 8632 | celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f); | |
317 | } | ||
318 | 2760 | } | |
319 | |||
320 | 29890 | int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, | |
321 | float **output, int channels, int frame_size, | ||
322 | int start_band, int end_band) | ||
323 | { | ||
324 | 29890 | int i, j, downmix = 0; | |
325 | int consumed; // bits of entropy consumed thus far for this frame | ||
326 | MDCT15Context *imdct; | ||
327 | |||
328 |
3/4✓ Branch 0 taken 18744 times.
✓ Branch 1 taken 11146 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18744 times.
|
29890 | if (channels != 1 && channels != 2) { |
329 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n", | |
330 | channels); | ||
331 | ✗ | return AVERROR_INVALIDDATA; | |
332 | } | ||
333 |
3/6✓ Branch 0 taken 29890 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29890 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 29890 times.
|
29890 | if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) { |
334 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n", | |
335 | start_band, end_band); | ||
336 | ✗ | return AVERROR_INVALIDDATA; | |
337 | } | ||
338 | |||
339 | 29890 | f->silence = 0; | |
340 | 29890 | f->transient = 0; | |
341 | 29890 | f->anticollapse = 0; | |
342 | 29890 | f->flushed = 0; | |
343 | 29890 | f->channels = channels; | |
344 | 29890 | f->start_band = start_band; | |
345 | 29890 | f->end_band = end_band; | |
346 | 29890 | f->framebits = rc->rb.bytes * 8; | |
347 | |||
348 | 29890 | f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE); | |
349 |
1/2✓ Branch 0 taken 29890 times.
✗ Branch 1 not taken.
|
29890 | if (f->size > CELT_MAX_LOG_BLOCKS || |
350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29890 times.
|
29890 | frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) { |
351 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n", | |
352 | frame_size); | ||
353 | ✗ | return AVERROR_INVALIDDATA; | |
354 | } | ||
355 | |||
356 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29890 times.
|
29890 | if (!f->output_channels) |
357 | ✗ | f->output_channels = channels; | |
358 | |||
359 |
2/2✓ Branch 0 taken 48634 times.
✓ Branch 1 taken 29890 times.
|
78524 | for (i = 0; i < f->channels; i++) { |
360 | 48634 | memset(f->block[i].coeffs, 0, sizeof(f->block[i].coeffs)); | |
361 | 48634 | memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks)); | |
362 | } | ||
363 | |||
364 | 29890 | consumed = opus_rc_tell(rc); | |
365 | |||
366 | /* obtain silence flag */ | ||
367 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29890 times.
|
29890 | if (consumed >= f->framebits) |
368 | ✗ | f->silence = 1; | |
369 |
2/2✓ Branch 0 taken 24952 times.
✓ Branch 1 taken 4938 times.
|
29890 | else if (consumed == 1) |
370 | 24952 | f->silence = ff_opus_rc_dec_log(rc, 15); | |
371 | |||
372 | |||
373 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 29764 times.
|
29890 | if (f->silence) { |
374 | 126 | consumed = f->framebits; | |
375 | 126 | rc->total_bits += f->framebits - opus_rc_tell(rc); | |
376 | } | ||
377 | |||
378 | /* obtain post-filter options */ | ||
379 | 29890 | consumed = parse_postfilter(f, rc, consumed); | |
380 | |||
381 | /* obtain transient flag */ | ||
382 |
4/4✓ Branch 0 taken 16184 times.
✓ Branch 1 taken 13706 times.
✓ Branch 2 taken 16095 times.
✓ Branch 3 taken 89 times.
|
29890 | if (f->size != 0 && consumed+3 <= f->framebits) |
383 | 16095 | f->transient = ff_opus_rc_dec_log(rc, 3); | |
384 | |||
385 |
2/2✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 25874 times.
|
29890 | f->blocks = f->transient ? 1 << f->size : 1; |
386 | 29890 | f->blocksize = frame_size / f->blocks; | |
387 | |||
388 |
2/2✓ Branch 0 taken 25874 times.
✓ Branch 1 taken 4016 times.
|
29890 | imdct = f->imdct[f->transient ? 0 : f->size]; |
389 | |||
390 |
2/2✓ Branch 0 taken 11146 times.
✓ Branch 1 taken 18744 times.
|
29890 | if (channels == 1) { |
391 |
2/2✓ Branch 0 taken 234066 times.
✓ Branch 1 taken 11146 times.
|
245212 | for (i = 0; i < CELT_MAX_BANDS; i++) |
392 |
2/2✓ Branch 0 taken 393 times.
✓ Branch 1 taken 233673 times.
|
234066 | f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]); |
393 | } | ||
394 | |||
395 | 29890 | celt_decode_coarse_energy(f, rc); | |
396 | 29890 | celt_decode_tf_changes (f, rc); | |
397 | 29890 | ff_celt_bitalloc (f, rc, 0); | |
398 | 29890 | celt_decode_fine_energy (f, rc); | |
399 | 29890 | ff_celt_quant_bands (f, rc); | |
400 | |||
401 |
2/2✓ Branch 0 taken 3130 times.
✓ Branch 1 taken 26760 times.
|
29890 | if (f->anticollapse_needed) |
402 | 3130 | f->anticollapse = ff_opus_rc_get_raw(rc, 1); | |
403 | |||
404 | 29890 | celt_decode_final_energy(f, rc); | |
405 | |||
406 | /* apply anti-collapse processing and denormalization to | ||
407 | * each coded channel */ | ||
408 |
2/2✓ Branch 0 taken 48634 times.
✓ Branch 1 taken 29890 times.
|
78524 | for (i = 0; i < f->channels; i++) { |
409 | 48634 | CeltBlock *block = &f->block[i]; | |
410 | |||
411 |
2/2✓ Branch 0 taken 2760 times.
✓ Branch 1 taken 45874 times.
|
48634 | if (f->anticollapse) |
412 | 2760 | process_anticollapse(f, block, f->block[i].coeffs); | |
413 | |||
414 | 48634 | celt_denormalize(f, block, f->block[i].coeffs); | |
415 | } | ||
416 | |||
417 | /* stereo -> mono downmix */ | ||
418 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29890 times.
|
29890 | if (f->output_channels < f->channels) { |
419 | ✗ | f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16)); | |
420 | ✗ | downmix = 1; | |
421 |
2/2✓ Branch 0 taken 8081 times.
✓ Branch 1 taken 21809 times.
|
29890 | } else if (f->output_channels > f->channels) |
422 | 8081 | memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float)); | |
423 | |||
424 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 29764 times.
|
29890 | if (f->silence) { |
425 |
2/2✓ Branch 0 taken 252 times.
✓ Branch 1 taken 126 times.
|
378 | for (i = 0; i < 2; i++) { |
426 | 252 | CeltBlock *block = &f->block[i]; | |
427 | |||
428 |
2/2✓ Branch 0 taken 5292 times.
✓ Branch 1 taken 252 times.
|
5544 | for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++) |
429 | 5292 | block->energy[j] = CELT_ENERGY_SILENCE; | |
430 | } | ||
431 | 126 | memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs)); | |
432 | 126 | memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs)); | |
433 | } | ||
434 | |||
435 | /* transform and output for each output channel */ | ||
436 |
2/2✓ Branch 0 taken 56715 times.
✓ Branch 1 taken 29890 times.
|
86605 | for (i = 0; i < f->output_channels; i++) { |
437 | 56715 | CeltBlock *block = &f->block[i]; | |
438 | |||
439 | /* iMDCT and overlap-add */ | ||
440 |
2/2✓ Branch 0 taken 92584 times.
✓ Branch 1 taken 56715 times.
|
149299 | for (j = 0; j < f->blocks; j++) { |
441 | 92584 | float *dst = block->buf + 1024 + j * f->blocksize; | |
442 | |||
443 | 92584 | imdct->imdct_half(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j, | |
444 | 92584 | f->blocks); | |
445 | 92584 | f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2, | |
446 | ff_celt_window, CELT_OVERLAP / 2); | ||
447 | } | ||
448 | |||
449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56715 times.
|
56715 | if (downmix) |
450 | ✗ | f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size); | |
451 | |||
452 | /* postfilter */ | ||
453 | 56715 | celt_postfilter(f, block); | |
454 | |||
455 | /* deemphasis */ | ||
456 | 56715 | block->emph_coeff = f->opusdsp.deemphasis(output[i], | |
457 | 56715 | &block->buf[1024 - frame_size], | |
458 | block->emph_coeff, frame_size); | ||
459 | } | ||
460 | |||
461 |
2/2✓ Branch 0 taken 11146 times.
✓ Branch 1 taken 18744 times.
|
29890 | if (channels == 1) |
462 | 11146 | memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy)); | |
463 | |||
464 |
2/2✓ Branch 0 taken 59780 times.
✓ Branch 1 taken 29890 times.
|
89670 | for (i = 0; i < 2; i++ ) { |
465 | 59780 | CeltBlock *block = &f->block[i]; | |
466 | |||
467 |
2/2✓ Branch 0 taken 51748 times.
✓ Branch 1 taken 8032 times.
|
59780 | if (!f->transient) { |
468 | 51748 | memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0])); | |
469 | 51748 | memcpy(block->prev_energy[0], block->energy, sizeof(block->prev_energy[0])); | |
470 | } else { | ||
471 |
2/2✓ Branch 0 taken 168672 times.
✓ Branch 1 taken 8032 times.
|
176704 | for (j = 0; j < CELT_MAX_BANDS; j++) |
472 |
2/2✓ Branch 0 taken 19438 times.
✓ Branch 1 taken 149234 times.
|
168672 | block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]); |
473 | } | ||
474 | |||
475 |
2/2✓ Branch 0 taken 167892 times.
✓ Branch 1 taken 59780 times.
|
227672 | for (j = 0; j < f->start_band; j++) { |
476 | 167892 | block->prev_energy[0][j] = CELT_ENERGY_SILENCE; | |
477 | 167892 | block->energy[j] = 0.0; | |
478 | } | ||
479 |
2/2✓ Branch 0 taken 70676 times.
✓ Branch 1 taken 59780 times.
|
130456 | for (j = f->end_band; j < CELT_MAX_BANDS; j++) { |
480 | 70676 | block->prev_energy[0][j] = CELT_ENERGY_SILENCE; | |
481 | 70676 | block->energy[j] = 0.0; | |
482 | } | ||
483 | } | ||
484 | |||
485 | 29890 | f->seed = rc->range; | |
486 | |||
487 | 29890 | return 0; | |
488 | } | ||
489 | |||
490 | 4612 | void ff_celt_flush(CeltFrame *f) | |
491 | { | ||
492 | int i, j; | ||
493 | |||
494 |
2/2✓ Branch 0 taken 4541 times.
✓ Branch 1 taken 71 times.
|
4612 | if (f->flushed) |
495 | 4541 | return; | |
496 | |||
497 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
|
213 | for (i = 0; i < 2; i++) { |
498 | 142 | CeltBlock *block = &f->block[i]; | |
499 | |||
500 |
2/2✓ Branch 0 taken 2982 times.
✓ Branch 1 taken 142 times.
|
3124 | for (j = 0; j < CELT_MAX_BANDS; j++) |
501 | 2982 | block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE; | |
502 | |||
503 | 142 | memset(block->energy, 0, sizeof(block->energy)); | |
504 | 142 | memset(block->buf, 0, sizeof(block->buf)); | |
505 | |||
506 | 142 | memset(block->pf_gains, 0, sizeof(block->pf_gains)); | |
507 | 142 | memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old)); | |
508 | 142 | memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new)); | |
509 | |||
510 | /* libopus uses CELT_EMPH_COEFF on init, but 0 is better since there's | ||
511 | * a lesser discontinuity when seeking. | ||
512 | * The deemphasis functions differ from libopus in that they require | ||
513 | * an initial state divided by the coefficient. */ | ||
514 | 142 | block->emph_coeff = 0.0f / CELT_EMPH_COEFF; | |
515 | } | ||
516 | 71 | f->seed = 0; | |
517 | |||
518 | 71 | f->flushed = 1; | |
519 | } | ||
520 | |||
521 | 48 | void ff_celt_free(CeltFrame **f) | |
522 | { | ||
523 | 48 | CeltFrame *frm = *f; | |
524 | int i; | ||
525 | |||
526 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (!frm) |
527 | ✗ | return; | |
528 | |||
529 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
|
240 | for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++) |
530 | 192 | ff_mdct15_uninit(&frm->imdct[i]); | |
531 | |||
532 | 48 | ff_celt_pvq_uninit(&frm->pvq); | |
533 | |||
534 | 48 | av_freep(&frm->dsp); | |
535 | 48 | av_freep(f); | |
536 | } | ||
537 | |||
538 | 48 | int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, | |
539 | int apply_phase_inv) | ||
540 | { | ||
541 | CeltFrame *frm; | ||
542 | int i, ret; | ||
543 | |||
544 |
3/4✓ Branch 0 taken 37 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
|
48 | if (output_channels != 1 && output_channels != 2) { |
545 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", | |
546 | output_channels); | ||
547 | ✗ | return AVERROR(EINVAL); | |
548 | } | ||
549 | |||
550 | 48 | frm = av_mallocz(sizeof(*frm)); | |
551 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (!frm) |
552 | ✗ | return AVERROR(ENOMEM); | |
553 | |||
554 | 48 | frm->avctx = avctx; | |
555 | 48 | frm->output_channels = output_channels; | |
556 | 48 | frm->apply_phase_inv = apply_phase_inv; | |
557 | |||
558 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
|
240 | for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++) |
559 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 192 times.
|
192 | if ((ret = ff_mdct15_init(&frm->imdct[i], 1, i + 3, -1.0f/32768)) < 0) |
560 | ✗ | goto fail; | |
561 | |||
562 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
|
48 | if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0) |
563 | ✗ | goto fail; | |
564 | |||
565 | 48 | frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); | |
566 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (!frm->dsp) { |
567 | ✗ | ret = AVERROR(ENOMEM); | |
568 | ✗ | goto fail; | |
569 | } | ||
570 | |||
571 | 48 | ff_opus_dsp_init(&frm->opusdsp); | |
572 | 48 | ff_celt_flush(frm); | |
573 | |||
574 | 48 | *f = frm; | |
575 | |||
576 | 48 | return 0; | |
577 | ✗ | fail: | |
578 | ✗ | ff_celt_free(&frm); | |
579 | ✗ | return ret; | |
580 | } | ||
581 |