../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
299 / 379
78.9%
Branches:
142 / 186
76.3%
Functions:
15 / 17
88.2%

libavcodec/opus/enc_psy.c
Line Branch Exec Source
1 /*
2 * Opus encoder
3 * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
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 #include <float.h>
23
24 #include "libavutil/mem.h"
25 #include "enc_psy.h"
26 #include "celt.h"
27 #include "pvq.h"
28 #include "tab.h"
29 #include "libavfilter/window_func.h"
30
31 ✗ static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band,
32 float *bits, float lambda)
33 {
34 ✗ int i, b = 0;
35 ✗ uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
36 ✗ const int band_size = ff_celt_freq_range[band] << f->size;
37 float buf[176 * 2], lowband_scratch[176], norm1[176], norm2[176];
38 ✗ float dist, cost, err_x = 0.0f, err_y = 0.0f;
39 ✗ float *X = buf;
40 ✗ float *X_orig = f->block[0].coeffs + (ff_celt_freq_bands[band] << f->size);
41
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ float *Y = (f->channels == 2) ? &buf[176] : NULL;
42 ✗ float *Y_orig = f->block[1].coeffs + (ff_celt_freq_bands[band] << f->size);
43 ✗ OPUS_RC_CHECKPOINT_SPAWN(rc);
44
45 ✗ memcpy(X, X_orig, band_size*sizeof(float));
46
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (Y)
47 ✗ memcpy(Y, Y_orig, band_size*sizeof(float));
48
49 ✗ f->remaining2 = ((f->framebits << 3) - f->anticollapse_needed) - opus_rc_tell_frac(rc) - 1;
50
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 14 not taken.
✗ if (band <= f->coded_bands - 1) {
51 ✗ int curr_balance = f->remaining / FFMIN(3, f->coded_bands - band);
52
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[band] + curr_balance), 14);
53 }
54
55
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 17 not taken.
✗ if (f->dual_stereo) {
56 ✗ pvq->quant_band(pvq, f, rc, band, X, NULL, band_size, b / 2, f->blocks, NULL,
57 ✗ f->size, norm1, 0, 1.0f, lowband_scratch, cm[0]);
58
59 ✗ pvq->quant_band(pvq, f, rc, band, Y, NULL, band_size, b / 2, f->blocks, NULL,
60 ✗ f->size, norm2, 0, 1.0f, lowband_scratch, cm[1]);
61 } else {
62 ✗ pvq->quant_band(pvq, f, rc, band, X, Y, band_size, b, f->blocks, NULL, f->size,
63 ✗ norm1, 0, 1.0f, lowband_scratch, cm[0] | cm[1]);
64 }
65
66
0/2
✗ Branch 22 → 19 not taken.
✗ Branch 22 → 23 not taken.
✗ for (i = 0; i < band_size; i++) {
67 ✗ err_x += (X[i] - X_orig[i])*(X[i] - X_orig[i]);
68
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ if (Y)
69 ✗ err_y += (Y[i] - Y_orig[i])*(Y[i] - Y_orig[i]);
70 }
71
72 ✗ dist = sqrtf(err_x) + sqrtf(err_y);
73 ✗ cost = OPUS_RC_CHECKPOINT_BITS(rc)/8.0f;
74 ✗ *bits += cost;
75
76 ✗ OPUS_RC_CHECKPOINT_ROLLBACK(rc);
77
78 ✗ return lambda*dist*cost;
79 }
80
81 /* Populate metrics without taking into consideration neighbouring steps */
82 202 static void step_collect_psy_metrics(OpusPsyContext *s, int index)
83 {
84 202 int silence = 0, ch, i, j;
85 /* The MDCT analysis covers 2 * OPUS_BLOCK_SIZE(bsize_analysis) samples.
86 * Each bufqueue entry holds avctx->frame_size samples (120 historically,
87 * 960 after c3aea7628c for default settings). steps_per_half is how many
88 * bufqueue entries fill one MDCT half-window.
89 *
90 * bufqueue[0] is reserved for the previous packet's overlap (initially the
91 * empty padding frame). The audio that step N analyzes starts at
92 * bufqueue[index + 1]; bufqueue[index] is its preceding overlap. */
93 202 const int half_samples = OPUS_BLOCK_SIZE(s->bsize_analysis);
94 202 const int step_samples = s->avctx->frame_size;
95 202 const int steps_per_half = half_samples / step_samples;
96 202 OpusPsyStep *st = s->steps[index];
97
98 202 st->index = index;
99
100
2/2
✓ Branch 19 → 3 taken 204 times.
✓ Branch 19 → 20 taken 202 times.
406 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
101 204 memset(s->scratch, 0, sizeof(float) * (half_samples << 1));
102
103
2/2
✓ Branch 6 → 4 taken 204 times.
✓ Branch 6 → 7 taken 204 times.
408 for (i = 1; i <= FFMIN(steps_per_half, index + 1); i++) {
104 204 const int offset = (steps_per_half - i) * step_samples;
105 204 AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index + 1 - i);
106 204 memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
107 }
108
2/2
✓ Branch 12 → 8 taken 204 times.
✓ Branch 12 → 13 taken 204 times.
408 for (i = 0; i < steps_per_half; i++) {
109
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 204 times.
204 if (index + 1 + i >= s->bufqueue->available)
110 ✗ break;
111 204 const int offset = (steps_per_half + i) * step_samples;
112 204 AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index + 1 + i);
113 204 memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
114 }
115
116 204 s->dsp->vector_fmul(s->scratch, s->scratch, s->window[s->bsize_analysis],
117 204 (OPUS_BLOCK_SIZE(s->bsize_analysis) << 1));
118
119 204 s->mdct_fn[s->bsize_analysis](s->mdct[s->bsize_analysis], st->coeffs[ch],
120 204 s->scratch, sizeof(float));
121
122
2/2
✓ Branch 17 → 16 taken 4284 times.
✓ Branch 17 → 18 taken 204 times.
4488 for (i = 0; i < CELT_MAX_BANDS; i++)
123 4284 st->bands[ch][i] = &st->coeffs[ch][ff_celt_freq_bands[i] << s->bsize_analysis];
124 }
125
126
2/2
✓ Branch 31 → 21 taken 204 times.
✓ Branch 31 → 32 taken 202 times.
406 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
127
2/2
✓ Branch 29 → 22 taken 4284 times.
✓ Branch 29 → 30 taken 204 times.
4488 for (i = 0; i < CELT_MAX_BANDS; i++) {
128 4284 float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
129 4284 const int range = ff_celt_freq_range[i] << s->bsize_analysis;
130 4284 const float *coeffs = st->bands[ch][i];
131
2/2
✓ Branch 24 → 23 taken 163200 times.
✓ Branch 24 → 25 taken 4284 times.
167484 for (j = 0; j < range; j++)
132 163200 energy += coeffs[j]*coeffs[j];
133
134 4284 st->energy[ch][i] += sqrtf(energy);
135 4284 silence |= !!st->energy[ch][i];
136 4284 avg_c_s = energy / range;
137
138
2/2
✓ Branch 27 → 26 taken 163200 times.
✓ Branch 27 → 28 taken 4284 times.
167484 for (j = 0; j < range; j++) {
139 163200 const float c_s = coeffs[j]*coeffs[j];
140 163200 dist_dev += (avg_c_s - c_s)*(avg_c_s - c_s);
141 }
142
143 4284 st->tone[ch][i] += sqrtf(dist_dev);
144 }
145 }
146
147 202 st->silence = !silence;
148
149
2/2
✓ Branch 32 → 33 taken 2 times.
✓ Branch 32 → 39 taken 200 times.
202 if (s->avctx->ch_layout.nb_channels > 1) {
150
2/2
✓ Branch 38 → 34 taken 42 times.
✓ Branch 38 → 39 taken 2 times.
44 for (i = 0; i < CELT_MAX_BANDS; i++) {
151 42 float incompat = 0.0f;
152 42 const float *coeffs1 = st->bands[0][i];
153 42 const float *coeffs2 = st->bands[1][i];
154 42 const int range = ff_celt_freq_range[i] << s->bsize_analysis;
155
2/2
✓ Branch 36 → 35 taken 1600 times.
✓ Branch 36 → 37 taken 42 times.
1642 for (j = 0; j < range; j++)
156 1600 incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
157 42 st->stereo[i] = sqrtf(incompat);
158 }
159 }
160
161
2/2
✓ Branch 53 → 40 taken 204 times.
✓ Branch 53 → 54 taken 202 times.
406 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
162
2/2
✓ Branch 51 → 41 taken 4284 times.
✓ Branch 51 → 52 taken 204 times.
4488 for (i = 0; i < CELT_MAX_BANDS; i++) {
163 4284 OpusBandExcitation *ex = &s->ex[ch][i];
164 4284 float bp_e = bessel_filter(&s->bfilter_lo[ch][i], st->energy[ch][i]);
165 4284 bp_e = bessel_filter(&s->bfilter_hi[ch][i], bp_e);
166 4284 bp_e *= bp_e;
167
2/2
✓ Branch 43 → 44 taken 2454 times.
✓ Branch 43 → 45 taken 1830 times.
4284 if (bp_e > ex->excitation) {
168 2454 st->change_amp[ch][i] = bp_e - ex->excitation;
169 2454 st->total_change += st->change_amp[ch][i];
170 2454 ex->excitation = ex->excitation_init = bp_e;
171 2454 ex->excitation_dist = 0.0f;
172 }
173
2/2
✓ Branch 45 → 46 taken 4200 times.
✓ Branch 45 → 50 taken 84 times.
4284 if (ex->excitation > 0.0f) {
174 4200 ex->excitation -= av_clipf((1/expf(ex->excitation_dist)), ex->excitation_init/20, ex->excitation_init/1.09);
175
2/2
✓ Branch 46 → 47 taken 3762 times.
✓ Branch 46 → 48 taken 438 times.
4200 ex->excitation = FFMAX(ex->excitation, 0.0f);
176 4200 ex->excitation_dist += 1.0f;
177 }
178 }
179 }
180 202 }
181
182 1390 static void search_for_change_points(OpusPsyContext *s, float tgt_change,
183 int offset_s, int offset_e, int resolution,
184 int level)
185 {
186 int i;
187 1390 float c_change = 0.0f;
188
2/2
✓ Branch 2 → 3 taken 341 times.
✓ Branch 2 → 4 taken 1049 times.
1390 if ((offset_e - offset_s) <= resolution)
189 341 return;
190
2/2
✓ Branch 8 → 5 taken 5748 times.
✓ Branch 8 → 9 taken 457 times.
6205 for (i = offset_s; i < offset_e; i++) {
191 5748 c_change += s->steps[i]->total_change;
192
2/2
✓ Branch 5 → 6 taken 592 times.
✓ Branch 5 → 7 taken 5156 times.
5748 if (c_change > tgt_change)
193 592 break;
194 }
195
2/2
✓ Branch 9 → 10 taken 457 times.
✓ Branch 9 → 11 taken 592 times.
1049 if (i == offset_e)
196 457 return;
197 592 search_for_change_points(s, tgt_change / 2.0f, offset_s, i + 0, resolution, level + 1);
198 592 s->inflection_points[s->inflection_points_count++] = i;
199 592 search_for_change_points(s, tgt_change / 2.0f, i + 1, offset_e, resolution, level + 1);
200 }
201
202 2 static int flush_silent_frames(OpusPsyContext *s)
203 {
204 /* buffered_steps and silent_frames count psy steps, each of which is
205 * avctx->frame_size samples (120 historically, up to 960 after
206 * c3aea7628c). The CELT framesize fsize we pick must consume at least
207 * one psy step per emitted packet, otherwise postencode_update would
208 * compute steps_out = 0 and the psy state would stall while bufqueue
209 * and afq drain. */
210 2 const int step_samples = s->avctx->frame_size;
211 int fsize, silent_frames;
212
213
2/2
✓ Branch 6 → 3 taken 3 times.
✓ Branch 6 → 7 taken 2 times.
5 for (silent_frames = 0; silent_frames < s->buffered_steps; silent_frames++)
214
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3 times.
3 if (!s->steps[silent_frames]->silence)
215 ✗ break;
216
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 2 times.
2 if (--silent_frames < 0)
217 ✗ return 0;
218
219
2/2
✓ Branch 15 → 10 taken 4 times.
✓ Branch 15 → 16 taken 1 time.
5 for (fsize = CELT_BLOCK_960; fsize > CELT_BLOCK_120; fsize--) {
220 4 const int packet_samples = OPUS_BLOCK_SIZE(fsize);
221 4 const int steps_per_packet = packet_samples / step_samples;
222
223
4/4
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 12 taken 2 times.
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 13 taken 1 time.
4 if (steps_per_packet < 1 || silent_frames < steps_per_packet)
224 3 continue;
225 /* 48 * CELT_SHORT_BLOCKSIZE = 5760 samples = 120 ms; matches the
226 * historical (48 >> fsize) cap for frame_size = 120. */
227 1 s->p.frames = FFMIN(silent_frames / steps_per_packet,
228 (48 * CELT_SHORT_BLOCKSIZE) / packet_samples);
229 1 s->p.framesize = fsize;
230 1 return 1;
231 }
232
233 1 return 0;
234 }
235
236 /* Main function which decides frame size and frames per current packet */
237 206 static void psy_output_groups(OpusPsyContext *s)
238 {
239 206 int max_delay_samples = (s->options->max_delay_ms*s->avctx->sample_rate)/1000;
240
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 206 times.
206 int max_bsize = FFMIN(OPUS_SAMPLES_TO_BLOCK_SIZE(max_delay_samples), CELT_BLOCK_960);
241
242 /* These don't change for now */
243 206 s->p.mode = OPUS_MODE_CELT;
244 206 s->p.bandwidth = OPUS_BANDWIDTH_FULLBAND;
245
246 /* Flush silent frames ASAP */
247
4/4
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 9 taken 204 times.
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 1 time.
206 if (s->steps[0]->silence && flush_silent_frames(s))
248 1 return;
249
250 205 s->p.framesize = FFMIN(max_bsize, CELT_BLOCK_960);
251 205 s->p.frames = 1;
252 }
253
254 276 int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
255 {
256 int i;
257 276 float total_energy_change = 0.0f;
258
259
3/4
✓ Branch 2 → 3 taken 276 times.
✗ Branch 2 → 9 not taken.
✓ Branch 3 → 4 taken 202 times.
✓ Branch 3 → 9 taken 74 times.
276 if (s->buffered_steps < s->max_steps && !s->eof) {
260 /* awin counts how many bufqueue entries fill one MDCT half-window.
261 * With frame_size=120 this is 8 (matches the historical 1 << bsize_analysis);
262 * with frame_size=960 it is 1, so we collect every call. */
263 202 const int awin = OPUS_BLOCK_SIZE(s->bsize_analysis) / s->avctx->frame_size;
264
1/2
✓ Branch 4 → 5 taken 202 times.
✗ Branch 4 → 7 not taken.
202 if (++s->steps_to_process >= awin) {
265 202 step_collect_psy_metrics(s, s->buffered_steps - awin + 1);
266 202 s->steps_to_process = 0;
267 }
268
2/2
✓ Branch 7 → 8 taken 70 times.
✓ Branch 7 → 9 taken 132 times.
202 if ((++s->buffered_steps) < s->max_steps)
269 70 return 1;
270 }
271
272
2/2
✓ Branch 11 → 10 taken 2991 times.
✓ Branch 11 → 12 taken 206 times.
3197 for (i = 0; i < s->buffered_steps; i++)
273 2991 total_energy_change += s->steps[i]->total_change;
274
275 206 search_for_change_points(s, total_energy_change / 2.0f, 0,
276 s->buffered_steps, 1, 0);
277
278 206 psy_output_groups(s);
279
280 206 p->frames = s->p.frames;
281 206 p->framesize = s->p.framesize;
282 206 p->mode = s->p.mode;
283 206 p->bandwidth = s->p.bandwidth;
284
285 206 return 0;
286 }
287
288 206 void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
289 {
290 206 int i, neighbouring_points = 0, start_offset = 0;
291 206 int steps_per_frame = OPUS_BLOCK_SIZE(s->p.framesize) / s->avctx->frame_size;
292 206 int step_offset = steps_per_frame*index;
293 206 int silence = 1;
294
295
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 206 times.
206 f->start_band = (s->p.mode == OPUS_MODE_HYBRID) ? 17 : 0;
296 206 f->end_band = ff_celt_band_end[s->p.bandwidth];
297 206 f->channels = s->avctx->ch_layout.nb_channels;
298 206 f->size = s->p.framesize;
299
300
2/2
✓ Branch 7 → 6 taken 206 times.
✓ Branch 7 → 8 taken 206 times.
412 for (i = 0; i < steps_per_frame; i++)
301 206 silence &= s->steps[index * steps_per_frame + i]->silence;
302
303 206 f->silence = silence;
304
2/2
✓ Branch 8 → 9 taken 2 times.
✓ Branch 8 → 10 taken 204 times.
206 if (f->silence) {
305 /* Explicitly signal silence in a 2 byte frame, like libopus does.
306 * A zero length frame would instead signal a lost frame to decoders,
307 * which conceal it by extrapolating the preceding audio. */
308 2 f->framebits = 16;
309 2 f->intensity_stereo = f->end_band; /* Read by postencode_update for avg_is_band */
310 2 return;
311 }
312
313
2/2
✓ Branch 14 → 11 taken 196 times.
✓ Branch 14 → 15 taken 8 times.
204 for (i = 0; i < s->inflection_points_count; i++) {
314
1/2
✓ Branch 11 → 12 taken 196 times.
✗ Branch 11 → 13 not taken.
196 if (s->inflection_points[i] >= step_offset) {
315 196 start_offset = i;
316 196 break;
317 }
318 }
319
320
2/2
✓ Branch 19 → 16 taken 196 times.
✓ Branch 19 → 20 taken 204 times.
400 for (i = start_offset; i < FFMIN(steps_per_frame, s->inflection_points_count - start_offset); i++) {
321
2/2
✓ Branch 16 → 17 taken 28 times.
✓ Branch 16 → 18 taken 168 times.
196 if (s->inflection_points[i] < (step_offset + steps_per_frame)) {
322 28 neighbouring_points++;
323 }
324 }
325
326 /* Transient flagging */
327 204 f->transient = neighbouring_points > 0;
328
2/2
✓ Branch 20 → 21 taken 28 times.
✓ Branch 20 → 22 taken 176 times.
204 f->blocks = f->transient ? OPUS_BLOCK_SIZE(s->p.framesize)/CELT_OVERLAP : 1;
329
330 /* Some sane defaults */
331 204 f->pfilter = 0;
332 204 f->pf_gain = 0.5f;
333 204 f->pf_octave = 2;
334 204 f->pf_period = 1;
335 204 f->pf_tapset = 2;
336
337 /* More sane defaults */
338 204 f->tf_select = 0;
339 204 f->anticollapse = 1;
340 204 f->alloc_trim = 5;
341 204 f->skip_band_floor = f->end_band;
342 204 f->intensity_stereo = f->end_band;
343 204 f->dual_stereo = 0;
344 204 f->spread = CELT_SPREAD_NORMAL;
345 204 memset(f->tf_change, 0, sizeof(int)*CELT_MAX_BANDS);
346 204 memset(f->alloc_boost, 0, sizeof(int)*CELT_MAX_BANDS);
347 }
348
349 204 static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start,
350 CeltFrame *f_out)
351 {
352 int i, f, ch;
353 204 int frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
354 204 int steps_per_frame = frame_size / s->avctx->frame_size;
355 204 float rate, frame_bits = 0;
356
357 /* Used for the global ROTATE flag */
358 204 float tonal = 0.0f;
359
360 /* Pseudo-weights */
361 204 float band_score[CELT_MAX_BANDS] = { 0 };
362 204 float max_score = 1.0f;
363
364 /* Pass one - one loop around each band, computing unquant stuff */
365
2/2
✓ Branch 10 → 3 taken 4284 times.
✓ Branch 10 → 11 taken 204 times.
4488 for (i = 0; i < CELT_MAX_BANDS; i++) {
366 4284 float weight = 0.0f;
367 4284 float tonal_contrib = 0.0f;
368
2/2
✓ Branch 8 → 4 taken 4284 times.
✓ Branch 8 → 9 taken 4284 times.
8568 for (f = 0; f < steps_per_frame; f++) {
369 4284 weight = start[f]->stereo[i];
370
2/2
✓ Branch 6 → 5 taken 4284 times.
✓ Branch 6 → 7 taken 4284 times.
8568 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
371 4284 weight += start[f]->change_amp[ch][i] + start[f]->tone[ch][i] + start[f]->energy[ch][i];
372 4284 tonal_contrib += start[f]->tone[ch][i];
373 }
374 }
375 4284 tonal += tonal_contrib;
376 4284 band_score[i] = weight;
377 }
378
379 204 tonal /= (float)CELT_MAX_BANDS;
380
381
2/2
✓ Branch 15 → 12 taken 4284 times.
✓ Branch 15 → 16 taken 204 times.
4488 for (i = 0; i < CELT_MAX_BANDS; i++) {
382
2/2
✓ Branch 12 → 13 taken 700 times.
✓ Branch 12 → 14 taken 3584 times.
4284 if (band_score[i] > max_score)
383 700 max_score = band_score[i];
384 }
385
386
2/2
✓ Branch 18 → 17 taken 4284 times.
✓ Branch 18 → 19 taken 204 times.
4488 for (i = 0; i < CELT_MAX_BANDS; i++) {
387 4284 f_out->alloc_boost[i] = (int)((band_score[i]/max_score)*3.0f);
388 //TODO: implements frame_bits adjustment.
389 }
390
391 204 tonal /= 1333136.0f;
392 204 f_out->spread = av_clip_uintp2(lrintf(tonal), 2);
393
394 204 rate = ((float)s->avctx->bit_rate) + frame_bits*frame_size*16;
395 204 rate *= s->lambda;
396 204 rate /= s->avctx->sample_rate/frame_size;
397
398 204 f_out->framebits = lrintf(rate);
399 204 f_out->framebits = FFMIN(f_out->framebits, OPUS_MAX_FRAME_SIZE * 8);
400 204 f_out->framebits = FFALIGN(f_out->framebits, 8);
401 204 }
402
403 ✗ static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
404 {
405 ✗ int i, tdist = 0.0f;
406 OpusRangeCoder dump;
407
408 ✗ ff_opus_rc_enc_init(&dump);
409 ✗ ff_celt_bitalloc(f, &dump, 1);
410
411
0/2
✗ Branch 7 → 5 not taken.
✗ Branch 7 → 8 not taken.
✗ for (i = 0; i < CELT_MAX_BANDS; i++) {
412 ✗ float bits = 0.0f;
413 ✗ float dist = pvq_band_cost(f->pvq, f, &dump, i, &bits, s->lambda);
414 ✗ tdist += dist;
415 }
416
417 ✗ *total_dist = tdist;
418
419 ✗ return 0;
420 }
421
422 204 static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
423 {
424 float td1, td2;
425 204 f->dual_stereo = 0;
426
427
1/2
✓ Branch 2 → 3 taken 204 times.
✗ Branch 2 → 4 not taken.
204 if (s->avctx->ch_layout.nb_channels < 2)
428 204 return;
429
430 ✗ bands_dist(s, f, &td1);
431 ✗ f->dual_stereo = 1;
432 ✗ bands_dist(s, f, &td2);
433
434 ✗ f->dual_stereo = td2 < td1;
435 ✗ s->dual_stereo_used += td2 < td1;
436 }
437
438 204 static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
439 {
440 204 int i, best_band = CELT_MAX_BANDS - 1;
441 204 float dist, best_dist = FLT_MAX;
442 /* TODO: fix, make some heuristic up here using the lambda value */
443 204 float end_band = 0;
444
445
1/2
✓ Branch 2 → 3 taken 204 times.
✗ Branch 2 → 4 not taken.
204 if (s->avctx->ch_layout.nb_channels < 2)
446 204 return;
447
448
0/2
✗ Branch 9 → 5 not taken.
✗ Branch 9 → 10 not taken.
✗ for (i = f->end_band; i >= end_band; i--) {
449 ✗ f->intensity_stereo = i;
450 ✗ bands_dist(s, f, &dist);
451
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (best_dist > dist) {
452 ✗ best_dist = dist;
453 ✗ best_band = i;
454 }
455 }
456
457 ✗ f->intensity_stereo = best_band;
458 ✗ s->avg_is_band = (s->avg_is_band + f->intensity_stereo)/2.0f;
459 }
460
461 204 static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
462 {
463 204 int i, j, k, cway, config[2][CELT_MAX_BANDS] = { { 0 } };
464 204 int steps_per_frame = OPUS_BLOCK_SIZE(f->size) / s->avctx->frame_size;
465 204 float score[2] = { 0 };
466
467
2/2
✓ Branch 25 → 3 taken 408 times.
✓ Branch 25 → 26 taken 204 times.
612 for (cway = 0; cway < 2; cway++) {
468 int mag[2];
469
2/2
✓ Branch 3 → 4 taken 56 times.
✓ Branch 3 → 5 taken 352 times.
408 int base = f->transient ? 120 : 960;
470
471
2/2
✓ Branch 11 → 7 taken 816 times.
✓ Branch 11 → 12 taken 408 times.
1224 for (i = 0; i < 2; i++) {
472 816 int c = ff_celt_tf_select[f->size][f->transient][cway][i];
473
2/2
✓ Branch 7 → 8 taken 380 times.
✓ Branch 7 → 9 taken 436 times.
816 mag[i] = c < 0 ? base >> FFABS(c) : base << FFABS(c);
474 }
475
476
2/2
✓ Branch 23 → 13 taken 8568 times.
✓ Branch 23 → 24 taken 408 times.
8976 for (i = 0; i < CELT_MAX_BANDS; i++) {
477 8568 float iscore0 = 0.0f;
478 8568 float iscore1 = 0.0f;
479
2/2
✓ Branch 18 → 14 taken 8568 times.
✓ Branch 18 → 19 taken 8568 times.
17136 for (j = 0; j < steps_per_frame; j++) {
480
2/2
✓ Branch 16 → 15 taken 8568 times.
✓ Branch 16 → 17 taken 8568 times.
17136 for (k = 0; k < s->avctx->ch_layout.nb_channels; k++) {
481 8568 iscore0 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[0];
482 8568 iscore1 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[1];
483 }
484 }
485 8568 config[cway][i] = FFABS(iscore0 - 1.0f) < FFABS(iscore1 - 1.0f);
486
2/2
✓ Branch 19 → 20 taken 302 times.
✓ Branch 19 → 21 taken 8266 times.
8568 score[cway] += config[cway][i] ? iscore1 : iscore0;
487 }
488 }
489
490 204 f->tf_select = score[0] < score[1];
491 204 memcpy(f->tf_change, config[f->tf_select], sizeof(int)*CELT_MAX_BANDS);
492
493 204 return 0;
494 }
495
496 204 int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
497 {
498 204 int start_transient_flag = f->transient;
499 204 int steps_per_frame = OPUS_BLOCK_SIZE(s->p.framesize) / s->avctx->frame_size;
500 204 OpusPsyStep **start = &s->steps[index * steps_per_frame];
501
502
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 204 times.
204 if (f->silence)
503 ✗ return 0;
504
505 204 celt_gauge_psy_weight(s, start, f);
506 204 celt_search_for_intensity(s, f);
507 204 celt_search_for_dual_stereo(s, f);
508 204 celt_search_for_tf(s, start, f);
509
510
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 13 taken 204 times.
204 if (f->transient != start_transient_flag) {
511
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ f->blocks = f->transient ? OPUS_BLOCK_SIZE(s->p.framesize)/CELT_OVERLAP : 1;
512 ✗ return 1;
513 }
514
515 204 return 0;
516 }
517
518 206 void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f)
519 {
520 206 int i, frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
521 206 int steps_out = s->p.frames*(frame_size/s->avctx->frame_size);
522 void *tmp[FF_BUFQUEUE_SIZE];
523 float ideal_fbits;
524
525
2/2
✓ Branch 4 → 3 taken 206 times.
✓ Branch 4 → 5 taken 206 times.
412 for (i = 0; i < steps_out; i++)
526 206 memset(s->steps[i], 0, sizeof(OpusPsyStep));
527
528
2/2
✓ Branch 7 → 6 taken 3708 times.
✓ Branch 7 → 8 taken 206 times.
3914 for (i = 0; i < s->max_steps; i++)
529 3708 tmp[i] = s->steps[i];
530
531
2/2
✓ Branch 13 → 9 taken 3708 times.
✓ Branch 13 → 14 taken 206 times.
3914 for (i = 0; i < s->max_steps; i++) {
532 3708 const int i_new = i - steps_out;
533
2/2
✓ Branch 9 → 10 taken 206 times.
✓ Branch 9 → 11 taken 3502 times.
3708 s->steps[i_new < 0 ? s->max_steps + i_new : i_new] = tmp[i];
534 }
535
536
2/2
✓ Branch 16 → 15 taken 2789 times.
✓ Branch 16 → 17 taken 206 times.
2995 for (i = steps_out; i < s->buffered_steps; i++)
537 2789 s->steps[i]->index -= steps_out;
538
539 206 ideal_fbits = s->avctx->bit_rate/(s->avctx->sample_rate/frame_size);
540
541
2/2
✓ Branch 22 → 18 taken 206 times.
✓ Branch 22 → 23 taken 206 times.
412 for (i = 0; i < s->p.frames; i++) {
542 206 s->avg_is_band += f[i].intensity_stereo;
543
3/4
✓ Branch 18 → 19 taken 204 times.
✓ Branch 18 → 21 taken 2 times.
✓ Branch 19 → 20 taken 204 times.
✗ Branch 19 → 21 not taken.
206 if (!f[i].silence && f[i].framebits > 0)
544 204 s->lambda *= ideal_fbits / f[i].framebits;
545 }
546
547 206 s->avg_is_band /= (s->p.frames + 1);
548
549 206 s->steps_to_process = 0;
550 /* At EOF, one more frame than there are psy steps is encoded to flush
551 * the last frame's MDCT overlap */
552 206 s->buffered_steps = FFMAX(s->buffered_steps - steps_out, 0);
553 206 s->total_packets_out += s->p.frames;
554 206 s->inflection_points_count = 0;
555 206 }
556
557 5 av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx,
558 struct FFBufQueue *bufqueue, OpusEncOptions *options)
559 {
560 int i, ch, ret;
561
562 5 s->lambda = 1.0f;
563 5 s->options = options;
564 5 s->avctx = avctx;
565 5 s->bufqueue = bufqueue;
566 5 s->max_steps = ceilf(s->options->max_delay_ms * avctx->sample_rate /
567 5 (1000.0f * avctx->frame_size));
568
569 5 s->bsize_analysis = CELT_BLOCK_960;
570 5 s->avg_is_band = CELT_MAX_BANDS - 1;
571 5 s->inflection_points_count = 0;
572
573 5 s->inflection_points = av_mallocz(sizeof(*s->inflection_points)*s->max_steps);
574
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5 times.
5 if (!s->inflection_points) {
575 ✗ ret = AVERROR(ENOMEM);
576 ✗ goto fail;
577 }
578
579 5 s->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
580
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 5 times.
5 if (!s->dsp) {
581 ✗ ret = AVERROR(ENOMEM);
582 ✗ goto fail;
583 }
584
585
2/2
✓ Branch 15 → 9 taken 6 times.
✓ Branch 15 → 16 taken 5 times.
11 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
586
2/2
✓ Branch 13 → 10 taken 126 times.
✓ Branch 13 → 14 taken 6 times.
132 for (i = 0; i < CELT_MAX_BANDS; i++) {
587 126 bessel_init(&s->bfilter_hi[ch][i], 1.0f, 19.0f, 100.0f, 1);
588 126 bessel_init(&s->bfilter_lo[ch][i], 1.0f, 20.0f, 100.0f, 0);
589 }
590 }
591
592
2/2
✓ Branch 21 → 17 taken 90 times.
✓ Branch 21 → 22 taken 5 times.
95 for (i = 0; i < s->max_steps; i++) {
593 90 s->steps[i] = av_mallocz(sizeof(OpusPsyStep));
594
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 90 times.
90 if (!s->steps[i]) {
595 ✗ ret = AVERROR(ENOMEM);
596 ✗ goto fail;
597 }
598 }
599
600
2/2
✓ Branch 32 → 23 taken 20 times.
✓ Branch 32 → 33 taken 5 times.
25 for (i = 0; i < CELT_BLOCK_NB; i++) {
601 float tmp;
602 20 const int len = OPUS_BLOCK_SIZE(i);
603 20 const float scale = 68 << (CELT_BLOCK_NB - 1 - i);
604 20 s->window[i] = av_malloc(2*len*sizeof(float));
605
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 20 times.
20 if (!s->window[i]) {
606 ✗ ret = AVERROR(ENOMEM);
607 ✗ goto fail;
608 }
609 20 generate_window_func(s->window[i], 2*len, WFUNC_SINE, &tmp);
610 20 ret = av_tx_init(&s->mdct[i], &s->mdct_fn[i], AV_TX_FLOAT_MDCT,
611 20 0, 15 << (i + 3), &scale, 0);
612
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 20 times.
20 if (ret < 0)
613 ✗ goto fail;
614 }
615
616 5 return 0;
617
618 ✗ fail:
619 ✗ av_freep(&s->inflection_points);
620 ✗ av_freep(&s->dsp);
621
622
0/2
✗ Branch 40 → 37 not taken.
✗ Branch 40 → 41 not taken.
✗ for (i = 0; i < CELT_BLOCK_NB; i++) {
623 ✗ av_tx_uninit(&s->mdct[i]);
624 ✗ av_freep(&s->window[i]);
625 }
626
627
0/2
✗ Branch 44 → 42 not taken.
✗ Branch 44 → 45 not taken.
✗ for (i = 0; i < s->max_steps; i++)
628 ✗ av_freep(&s->steps[i]);
629
630 ✗ return ret;
631 }
632
633 79 void ff_opus_psy_signal_eof(OpusPsyContext *s)
634 {
635 79 s->eof = 1;
636 79 }
637
638 5 av_cold int ff_opus_psy_end(OpusPsyContext *s)
639 {
640 int i;
641
642 5 av_freep(&s->inflection_points);
643 5 av_freep(&s->dsp);
644
645
2/2
✓ Branch 8 → 5 taken 20 times.
✓ Branch 8 → 9 taken 5 times.
25 for (i = 0; i < CELT_BLOCK_NB; i++) {
646 20 av_tx_uninit(&s->mdct[i]);
647 20 av_freep(&s->window[i]);
648 }
649
650
2/2
✓ Branch 12 → 10 taken 90 times.
✓ Branch 12 → 13 taken 5 times.
95 for (i = 0; i < s->max_steps; i++)
651 90 av_freep(&s->steps[i]);
652
653 5 av_log(s->avctx, AV_LOG_INFO, "Average Intensity Stereo band: %0.1f\n", s->avg_is_band);
654 5 av_log(s->avctx, AV_LOG_INFO, "Dual Stereo used: %0.2f%%\n", ((float)s->dual_stereo_used/s->total_packets_out)*100.0f);
655
656 5 return 0;
657 }
658