FFmpeg
Loading...
Searching...
No Matches
enc_psy.c
Go to the documentation of this file.
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"
30
31static 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 float *Y = (f->channels == 2) ? &buf[176] : NULL;
42 float *Y_orig = f->block[1].coeffs + (ff_celt_freq_bands[band] << f->size);
44
45 memcpy(X, X_orig, band_size*sizeof(float));
46 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 if (band <= f->coded_bands - 1) {
51 int curr_balance = f->remaining / FFMIN(3, f->coded_bands - band);
52 b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[band] + curr_balance), 14);
53 }
54
55 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 for (i = 0; i < band_size; i++) {
67 err_x += (X[i] - X_orig[i])*(X[i] - X_orig[i]);
68 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
77
78 return lambda*dist*cost;
79}
80
81/* Populate metrics without taking into consideration neighbouring steps */
83{
84 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 const int half_samples = OPUS_BLOCK_SIZE(s->bsize_analysis);
94 const int step_samples = s->avctx->frame_size;
95 const int steps_per_half = half_samples / step_samples;
96 OpusPsyStep *st = s->steps[index];
97
98 st->index = index;
99
100 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
101 memset(s->scratch, 0, sizeof(float) * (half_samples << 1));
102
103 for (i = 1; i <= FFMIN(steps_per_half, index + 1); i++) {
104 const int offset = (steps_per_half - i) * step_samples;
105 AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index + 1 - i);
106 memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
107 }
108 for (i = 0; i < steps_per_half; i++) {
109 if (index + 1 + i >= s->bufqueue->available)
110 break;
111 const int offset = (steps_per_half + i) * step_samples;
112 AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index + 1 + i);
113 memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
114 }
115
116 s->dsp->vector_fmul(s->scratch, s->scratch, s->window[s->bsize_analysis],
117 (OPUS_BLOCK_SIZE(s->bsize_analysis) << 1));
118
119 s->mdct_fn[s->bsize_analysis](s->mdct[s->bsize_analysis], st->coeffs[ch],
120 s->scratch, sizeof(float));
121
122 for (i = 0; i < CELT_MAX_BANDS; i++)
123 st->bands[ch][i] = &st->coeffs[ch][ff_celt_freq_bands[i] << s->bsize_analysis];
124 }
125
126 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
127 for (i = 0; i < CELT_MAX_BANDS; i++) {
128 float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
129 const int range = ff_celt_freq_range[i] << s->bsize_analysis;
130 const float *coeffs = st->bands[ch][i];
131 for (j = 0; j < range; j++)
132 energy += coeffs[j]*coeffs[j];
133
134 st->energy[ch][i] += sqrtf(energy);
135 silence |= !!st->energy[ch][i];
136 avg_c_s = energy / range;
137
138 for (j = 0; j < range; j++) {
139 const float c_s = coeffs[j]*coeffs[j];
140 dist_dev += (avg_c_s - c_s)*(avg_c_s - c_s);
141 }
142
143 st->tone[ch][i] += sqrtf(dist_dev);
144 }
145 }
146
147 st->silence = !silence;
148
149 if (s->avctx->ch_layout.nb_channels > 1) {
150 for (i = 0; i < CELT_MAX_BANDS; i++) {
151 float incompat = 0.0f;
152 const float *coeffs1 = st->bands[0][i];
153 const float *coeffs2 = st->bands[1][i];
154 const int range = ff_celt_freq_range[i] << s->bsize_analysis;
155 for (j = 0; j < range; j++)
156 incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
157 st->stereo[i] = sqrtf(incompat);
158 }
159 }
160
161 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
162 for (i = 0; i < CELT_MAX_BANDS; i++) {
163 OpusBandExcitation *ex = &s->ex[ch][i];
164 float bp_e = bessel_filter(&s->bfilter_lo[ch][i], st->energy[ch][i]);
165 bp_e = bessel_filter(&s->bfilter_hi[ch][i], bp_e);
166 bp_e *= bp_e;
167 if (bp_e > ex->excitation) {
168 st->change_amp[ch][i] = bp_e - ex->excitation;
169 st->total_change += st->change_amp[ch][i];
170 ex->excitation = ex->excitation_init = bp_e;
171 ex->excitation_dist = 0.0f;
172 }
173 if (ex->excitation > 0.0f) {
174 ex->excitation -= av_clipf((1/expf(ex->excitation_dist)), ex->excitation_init/20, ex->excitation_init/1.09);
175 ex->excitation = FFMAX(ex->excitation, 0.0f);
176 ex->excitation_dist += 1.0f;
177 }
178 }
179 }
180}
181
182static 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 float c_change = 0.0f;
188 if ((offset_e - offset_s) <= resolution)
189 return;
190 for (i = offset_s; i < offset_e; i++) {
191 c_change += s->steps[i]->total_change;
192 if (c_change > tgt_change)
193 break;
194 }
195 if (i == offset_e)
196 return;
197 search_for_change_points(s, tgt_change / 2.0f, offset_s, i + 0, resolution, level + 1);
198 s->inflection_points[s->inflection_points_count++] = i;
199 search_for_change_points(s, tgt_change / 2.0f, i + 1, offset_e, resolution, level + 1);
200}
201
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 const int step_samples = s->avctx->frame_size;
211 int fsize, silent_frames;
212
213 for (silent_frames = 0; silent_frames < s->buffered_steps; silent_frames++)
214 if (!s->steps[silent_frames]->silence)
215 break;
216 if (--silent_frames < 0)
217 return 0;
218
220 const int packet_samples = OPUS_BLOCK_SIZE(fsize);
221 const int steps_per_packet = packet_samples / step_samples;
222
223 if (steps_per_packet < 1 || silent_frames < steps_per_packet)
224 continue;
225 /* 48 * CELT_SHORT_BLOCKSIZE = 5760 samples = 120 ms; matches the
226 * historical (48 >> fsize) cap for frame_size = 120. */
227 s->p.frames = FFMIN(silent_frames / steps_per_packet,
228 (48 * CELT_SHORT_BLOCKSIZE) / packet_samples);
229 s->p.framesize = fsize;
230 return 1;
231 }
232
233 return 0;
234}
235
236/* Main function which decides frame size and frames per current packet */
238{
239 int max_delay_samples = (s->options->max_delay_ms*s->avctx->sample_rate)/1000;
240 int max_bsize = FFMIN(OPUS_SAMPLES_TO_BLOCK_SIZE(max_delay_samples), CELT_BLOCK_960);
241
242 /* These don't change for now */
243 s->p.mode = OPUS_MODE_CELT;
244 s->p.bandwidth = OPUS_BANDWIDTH_FULLBAND;
245
246 /* Flush silent frames ASAP */
247 if (s->steps[0]->silence && flush_silent_frames(s))
248 return;
249
250 s->p.framesize = FFMIN(max_bsize, CELT_BLOCK_960);
251 s->p.frames = 1;
252}
253
255{
256 int i;
257 float total_energy_change = 0.0f;
258
259 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 const int awin = OPUS_BLOCK_SIZE(s->bsize_analysis) / s->avctx->frame_size;
264 if (++s->steps_to_process >= awin) {
265 step_collect_psy_metrics(s, s->buffered_steps - awin + 1);
266 s->steps_to_process = 0;
267 }
268 if ((++s->buffered_steps) < s->max_steps)
269 return 1;
270 }
271
272 for (i = 0; i < s->buffered_steps; i++)
273 total_energy_change += s->steps[i]->total_change;
274
275 search_for_change_points(s, total_energy_change / 2.0f, 0,
276 s->buffered_steps, 1, 0);
277
279
280 p->frames = s->p.frames;
281 p->framesize = s->p.framesize;
282 p->mode = s->p.mode;
283 p->bandwidth = s->p.bandwidth;
284
285 return 0;
286}
287
289{
290 int i, neighbouring_points = 0, start_offset = 0;
291 int steps_per_frame = OPUS_BLOCK_SIZE(s->p.framesize) / s->avctx->frame_size;
292 int step_offset = steps_per_frame*index;
293 int silence = 1;
294
295 f->start_band = (s->p.mode == OPUS_MODE_HYBRID) ? 17 : 0;
296 f->end_band = ff_celt_band_end[s->p.bandwidth];
297 f->channels = s->avctx->ch_layout.nb_channels;
298 f->size = s->p.framesize;
299
300 for (i = 0; i < steps_per_frame; i++)
301 silence &= s->steps[index * steps_per_frame + i]->silence;
302
303 /* If we reach EOF with fewer collected psy steps than this packet wants
304 * to encode, the slots beyond buffered_steps were zeroed by the previous
305 * postencode_update and contain no valid analysis data. Encoding garbage
306 * with the full rate budget can overrun the range coder buffer (rng_bytes
307 * exceeds the per-frame size passed to ff_opus_rc_enc_end), so force a
308 * silent packet instead. */
309 if (s->eof && step_offset >= s->buffered_steps)
310 silence = 1;
311
312 f->silence = silence;
313 if (f->silence) {
314 f->framebits = 0; /* Otherwise the silence flag eats up 16(!) bits */
315 f->intensity_stereo = f->end_band; /* Read by postencode_update for avg_is_band */
316 return;
317 }
318
319 for (i = 0; i < s->inflection_points_count; i++) {
320 if (s->inflection_points[i] >= step_offset) {
321 start_offset = i;
322 break;
323 }
324 }
325
326 for (i = start_offset; i < FFMIN(steps_per_frame, s->inflection_points_count - start_offset); i++) {
327 if (s->inflection_points[i] < (step_offset + steps_per_frame)) {
328 neighbouring_points++;
329 }
330 }
331
332 /* Transient flagging */
333 f->transient = neighbouring_points > 0;
334 f->blocks = f->transient ? OPUS_BLOCK_SIZE(s->p.framesize)/CELT_OVERLAP : 1;
335
336 /* Some sane defaults */
337 f->pfilter = 0;
338 f->pf_gain = 0.5f;
339 f->pf_octave = 2;
340 f->pf_period = 1;
341 f->pf_tapset = 2;
342
343 /* More sane defaults */
344 f->tf_select = 0;
345 f->anticollapse = 1;
346 f->alloc_trim = 5;
347 f->skip_band_floor = f->end_band;
348 f->intensity_stereo = f->end_band;
349 f->dual_stereo = 0;
350 f->spread = CELT_SPREAD_NORMAL;
351 memset(f->tf_change, 0, sizeof(int)*CELT_MAX_BANDS);
352 memset(f->alloc_boost, 0, sizeof(int)*CELT_MAX_BANDS);
353}
354
356 CeltFrame *f_out)
357{
358 int i, f, ch;
359 int frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
360 int steps_per_frame = frame_size / s->avctx->frame_size;
361 float rate, frame_bits = 0;
362
363 /* Used for the global ROTATE flag */
364 float tonal = 0.0f;
365
366 /* Pseudo-weights */
367 float band_score[CELT_MAX_BANDS] = { 0 };
368 float max_score = 1.0f;
369
370 /* Pass one - one loop around each band, computing unquant stuff */
371 for (i = 0; i < CELT_MAX_BANDS; i++) {
372 float weight = 0.0f;
373 float tonal_contrib = 0.0f;
374 for (f = 0; f < steps_per_frame; f++) {
375 weight = start[f]->stereo[i];
376 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
377 weight += start[f]->change_amp[ch][i] + start[f]->tone[ch][i] + start[f]->energy[ch][i];
378 tonal_contrib += start[f]->tone[ch][i];
379 }
380 }
381 tonal += tonal_contrib;
382 band_score[i] = weight;
383 }
384
385 tonal /= (float)CELT_MAX_BANDS;
386
387 for (i = 0; i < CELT_MAX_BANDS; i++) {
388 if (band_score[i] > max_score)
389 max_score = band_score[i];
390 }
391
392 for (i = 0; i < CELT_MAX_BANDS; i++) {
393 f_out->alloc_boost[i] = (int)((band_score[i]/max_score)*3.0f);
394 //TODO: implements frame_bits adjustment.
395 }
396
397 tonal /= 1333136.0f;
398 f_out->spread = av_clip_uintp2(lrintf(tonal), 2);
399
400 rate = ((float)s->avctx->bit_rate) + frame_bits*frame_size*16;
401 rate *= s->lambda;
402 rate /= s->avctx->sample_rate/frame_size;
403
404 f_out->framebits = lrintf(rate);
405 f_out->framebits = FFMIN(f_out->framebits, OPUS_MAX_FRAME_SIZE * 8);
406 f_out->framebits = FFALIGN(f_out->framebits, 8);
407}
408
409static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
410{
411 int i, tdist = 0.0f;
412 OpusRangeCoder dump;
413
414 ff_opus_rc_enc_init(&dump);
415 ff_celt_bitalloc(f, &dump, 1);
416
417 for (i = 0; i < CELT_MAX_BANDS; i++) {
418 float bits = 0.0f;
419 float dist = pvq_band_cost(f->pvq, f, &dump, i, &bits, s->lambda);
420 tdist += dist;
421 }
422
423 *total_dist = tdist;
424
425 return 0;
426}
427
429{
430 float td1, td2;
431 f->dual_stereo = 0;
432
433 if (s->avctx->ch_layout.nb_channels < 2)
434 return;
435
436 bands_dist(s, f, &td1);
437 f->dual_stereo = 1;
438 bands_dist(s, f, &td2);
439
440 f->dual_stereo = td2 < td1;
441 s->dual_stereo_used += td2 < td1;
442}
443
445{
446 int i, best_band = CELT_MAX_BANDS - 1;
447 float dist, best_dist = FLT_MAX;
448 /* TODO: fix, make some heuristic up here using the lambda value */
449 float end_band = 0;
450
451 if (s->avctx->ch_layout.nb_channels < 2)
452 return;
453
454 for (i = f->end_band; i >= end_band; i--) {
455 f->intensity_stereo = i;
456 bands_dist(s, f, &dist);
457 if (best_dist > dist) {
458 best_dist = dist;
459 best_band = i;
460 }
461 }
462
463 f->intensity_stereo = best_band;
464 s->avg_is_band = (s->avg_is_band + f->intensity_stereo)/2.0f;
465}
466
468{
469 int i, j, k, cway, config[2][CELT_MAX_BANDS] = { { 0 } };
470 int steps_per_frame = OPUS_BLOCK_SIZE(f->size) / s->avctx->frame_size;
471 float score[2] = { 0 };
472
473 for (cway = 0; cway < 2; cway++) {
474 int mag[2];
475 int base = f->transient ? 120 : 960;
476
477 for (i = 0; i < 2; i++) {
478 int c = ff_celt_tf_select[f->size][f->transient][cway][i];
479 mag[i] = c < 0 ? base >> FFABS(c) : base << FFABS(c);
480 }
481
482 for (i = 0; i < CELT_MAX_BANDS; i++) {
483 float iscore0 = 0.0f;
484 float iscore1 = 0.0f;
485 for (j = 0; j < steps_per_frame; j++) {
486 for (k = 0; k < s->avctx->ch_layout.nb_channels; k++) {
487 iscore0 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[0];
488 iscore1 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[1];
489 }
490 }
491 config[cway][i] = FFABS(iscore0 - 1.0f) < FFABS(iscore1 - 1.0f);
492 score[cway] += config[cway][i] ? iscore1 : iscore0;
493 }
494 }
495
496 f->tf_select = score[0] < score[1];
497 memcpy(f->tf_change, config[f->tf_select], sizeof(int)*CELT_MAX_BANDS);
498
499 return 0;
500}
501
503{
504 int start_transient_flag = f->transient;
505 int steps_per_frame = OPUS_BLOCK_SIZE(s->p.framesize) / s->avctx->frame_size;
506 OpusPsyStep **start = &s->steps[index * steps_per_frame];
507
508 if (f->silence)
509 return 0;
510
511 celt_gauge_psy_weight(s, start, f);
514 celt_search_for_tf(s, start, f);
515
516 if (f->transient != start_transient_flag) {
517 f->blocks = f->transient ? OPUS_BLOCK_SIZE(s->p.framesize)/CELT_OVERLAP : 1;
518 return 1;
519 }
520
521 return 0;
522}
523
525{
526 int i, frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
527 int steps_out = s->p.frames*(frame_size/s->avctx->frame_size);
528 void *tmp[FF_BUFQUEUE_SIZE];
529 float ideal_fbits;
530
531 for (i = 0; i < steps_out; i++)
532 memset(s->steps[i], 0, sizeof(OpusPsyStep));
533
534 for (i = 0; i < s->max_steps; i++)
535 tmp[i] = s->steps[i];
536
537 for (i = 0; i < s->max_steps; i++) {
538 const int i_new = i - steps_out;
539 s->steps[i_new < 0 ? s->max_steps + i_new : i_new] = tmp[i];
540 }
541
542 for (i = steps_out; i < s->buffered_steps; i++)
543 s->steps[i]->index -= steps_out;
544
545 ideal_fbits = s->avctx->bit_rate/(s->avctx->sample_rate/frame_size);
546
547 for (i = 0; i < s->p.frames; i++) {
548 s->avg_is_band += f[i].intensity_stereo;
549 if (f[i].framebits > 0)
550 s->lambda *= ideal_fbits / f[i].framebits;
551 }
552
553 s->avg_is_band /= (s->p.frames + 1);
554
555 s->steps_to_process = 0;
556 s->buffered_steps -= steps_out;
557 s->total_packets_out += s->p.frames;
558 s->inflection_points_count = 0;
559}
560
562 struct FFBufQueue *bufqueue, OpusEncOptions *options)
563{
564 int i, ch, ret;
565
566 s->lambda = 1.0f;
567 s->options = options;
568 s->avctx = avctx;
569 s->bufqueue = bufqueue;
570 s->max_steps = ceilf(s->options->max_delay_ms * avctx->sample_rate /
571 (1000.0f * avctx->frame_size));
572
573 s->bsize_analysis = CELT_BLOCK_960;
574 s->avg_is_band = CELT_MAX_BANDS - 1;
575 s->inflection_points_count = 0;
576
577 s->inflection_points = av_mallocz(sizeof(*s->inflection_points)*s->max_steps);
578 if (!s->inflection_points) {
579 ret = AVERROR(ENOMEM);
580 goto fail;
581 }
582
584 if (!s->dsp) {
585 ret = AVERROR(ENOMEM);
586 goto fail;
587 }
588
589 for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
590 for (i = 0; i < CELT_MAX_BANDS; i++) {
591 bessel_init(&s->bfilter_hi[ch][i], 1.0f, 19.0f, 100.0f, 1);
592 bessel_init(&s->bfilter_lo[ch][i], 1.0f, 20.0f, 100.0f, 0);
593 }
594 }
595
596 for (i = 0; i < s->max_steps; i++) {
597 s->steps[i] = av_mallocz(sizeof(OpusPsyStep));
598 if (!s->steps[i]) {
599 ret = AVERROR(ENOMEM);
600 goto fail;
601 }
602 }
603
604 for (i = 0; i < CELT_BLOCK_NB; i++) {
605 float tmp;
606 const int len = OPUS_BLOCK_SIZE(i);
607 const float scale = 68 << (CELT_BLOCK_NB - 1 - i);
608 s->window[i] = av_malloc(2*len*sizeof(float));
609 if (!s->window[i]) {
610 ret = AVERROR(ENOMEM);
611 goto fail;
612 }
613 generate_window_func(s->window[i], 2*len, WFUNC_SINE, &tmp);
614 ret = av_tx_init(&s->mdct[i], &s->mdct_fn[i], AV_TX_FLOAT_MDCT,
615 0, 15 << (i + 3), &scale, 0);
616 if (ret < 0)
617 goto fail;
618 }
619
620 return 0;
621
622fail:
623 av_freep(&s->inflection_points);
624 av_freep(&s->dsp);
625
626 for (i = 0; i < CELT_BLOCK_NB; i++) {
627 av_tx_uninit(&s->mdct[i]);
628 av_freep(&s->window[i]);
629 }
630
631 for (i = 0; i < s->max_steps; i++)
632 av_freep(&s->steps[i]);
633
634 return ret;
635}
636
638{
639 s->eof = 1;
640}
641
643{
644 int i;
645
646 av_freep(&s->inflection_points);
647 av_freep(&s->dsp);
648
649 for (i = 0; i < CELT_BLOCK_NB; i++) {
650 av_tx_uninit(&s->mdct[i]);
651 av_freep(&s->window[i]);
652 }
653
654 for (i = 0; i < s->max_steps; i++)
655 av_freep(&s->steps[i]);
656
657 av_log(s->avctx, AV_LOG_INFO, "Average Intensity Stereo band: %0.1f\n", s->avg_is_band);
658 av_log(s->avctx, AV_LOG_INFO, "Dual Stereo used: %0.2f%%\n", ((float)s->dual_stereo_used/s->total_packets_out)*100.0f);
659
660 return 0;
661}
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
#define FF_BUFQUEUE_SIZE
#define Y
Definition boxblur.h:37
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition bufferqueue.h:87
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
Definition celt.c:137
#define CELT_SHORT_BLOCKSIZE
Definition celt.h:39
#define CELT_OVERLAP
Definition celt.h:40
@ CELT_SPREAD_NORMAL
Definition celt.h:58
#define CELT_MAX_BANDS
Definition celt.h:43
@ CELT_BLOCK_120
Definition celt.h:63
@ CELT_BLOCK_NB
Definition celt.h:68
@ CELT_BLOCK_960
Definition celt.h:66
#define av_clip_uintp2
Definition common.h:124
#define av_clipf
Definition common.h:145
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
static __device__ float sqrtf(float a)
static __device__ float ceilf(float a)
#define cm
Definition dvbsubdec.c:40
#define OPUS_SAMPLES_TO_BLOCK_SIZE(x)
Definition enc.h:41
#define OPUS_BLOCK_SIZE(x)
Definition enc.h:39
static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
Definition enc_psy.c:409
static void step_collect_psy_metrics(OpusPsyContext *s, int index)
Definition enc_psy.c:82
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f)
Definition enc_psy.c:524
static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
Definition enc_psy.c:467
static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f_out)
Definition enc_psy.c:355
av_cold int ff_opus_psy_end(OpusPsyContext *s)
Definition enc_psy.c:642
int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
Definition enc_psy.c:502
int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
Definition enc_psy.c:254
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
Definition enc_psy.c:561
static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
Definition enc_psy.c:444
static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
Definition enc_psy.c:31
static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
Definition enc_psy.c:428
void ff_opus_psy_signal_eof(OpusPsyContext *s)
Definition enc_psy.c:637
void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
Definition enc_psy.c:288
static int flush_silent_frames(OpusPsyContext *s)
Definition enc_psy.c:202
static void psy_output_groups(OpusPsyContext *s)
Definition enc_psy.c:237
static void search_for_change_points(OpusPsyContext *s, float tgt_change, int offset_s, int offset_e, int resolution, int level)
Definition enc_psy.c:182
static float bessel_filter(FFBesselFilter *s, float x)
Definition enc_utils.h:79
static int bessel_init(FFBesselFilter *s, float n, float f0, float fs, int highpass)
Definition enc_utils.h:72
#define X
Definition f_ebur128.c:157
static const uint8_t bits[8]
Definition fastaudio.c:100
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define fail
Definition test.h:479
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
int index
Definition gxfenc.c:90
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
unsigned offset
Definition libaomenc.c:763
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define expf(x)
Definition libm.h:285
#define lrintf(x)
Definition libm_mips.h:72
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
enum AVColorRange range
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
@ OPUS_BANDWIDTH_FULLBAND
Definition opus.h:54
@ OPUS_MODE_HYBRID
Definition opus.h:43
@ OPUS_MODE_CELT
Definition opus.h:44
#define OPUS_MAX_FRAME_SIZE
Definition opus.h:28
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
Definition rc.c:402
#define OPUS_RC_CHECKPOINT_ROLLBACK(rc)
Definition rc.h:124
#define OPUS_RC_CHECKPOINT_SPAWN(rc)
Definition rc.h:117
#define OPUS_RC_CHECKPOINT_BITS(rc)
Definition rc.h:121
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition rc.h:67
const h264_weight_func weight
main external API structure.
Definition avcodec.h:443
int sample_rate
samples per second
Definition avcodec.h:1040
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
int framebits
Definition celt.h:139
int alloc_boost[CELT_MAX_BANDS]
Definition celt.h:120
enum CeltSpread spread
Definition celt.h:130
Definition pvq.h:37
QUANT_FN * quant_band
Definition pvq.h:42
Structure holding the queue.
Definition bufferqueue.h:49
float excitation_dist
Definition enc_psy.h:48
float excitation_init
Definition enc_psy.h:49
float coeffs[OPUS_MAX_CHANNELS][OPUS_BLOCK_SIZE(CELT_BLOCK_960)]
Definition enc_psy.h:43
float tone[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition enc_psy.h:37
int index
Definition enc_psy.h:34
float stereo[CELT_MAX_BANDS]
Definition enc_psy.h:38
float change_amp[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition enc_psy.h:39
int silence
Definition enc_psy.h:35
float * bands[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition enc_psy.h:42
float energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition enc_psy.h:36
float total_change
Definition enc_psy.h:40
uint8_t level
Definition svq3.c:208
const uint8_t ff_celt_freq_range[]
Definition tab.c:836
const uint8_t ff_celt_band_end[]
Definition tab.c:29
const uint8_t ff_celt_freq_bands[]
Definition tab.c:832
const int8_t ff_celt_tf_select[4][2][2][2]
Definition tab.c:846
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
int len
uint8_t base
Definition vp3data.h:128
static double c[64]
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition window_func.h:63
@ WFUNC_SINE
Definition window_func.h:31