FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
opusenc_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 "opusenc_psy.h"
23 #include "opus_pvq.h"
24 #include "opustab.h"
25 #include "mdct15.h"
26 #include "libavutil/qsort.h"
27 
28 /* Populate metrics without taking into consideration neighbouring steps */
30 {
31  int silence = 0, ch, i, j;
32  OpusPsyStep *st = s->steps[index];
33 
34  st->index = index;
35 
36  for (ch = 0; ch < s->avctx->channels; ch++) {
37  const int lap_size = (1 << s->bsize_analysis);
38  for (i = 1; i <= FFMIN(lap_size, index); i++) {
39  const int offset = i*120;
40  AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index - i);
41  memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
42  }
43  for (i = 0; i < lap_size; i++) {
44  const int offset = i*120 + lap_size;
45  AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index + i);
46  memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
47  }
48 
50  (OPUS_BLOCK_SIZE(s->bsize_analysis) << 1));
51 
52  s->mdct[s->bsize_analysis]->mdct(s->mdct[s->bsize_analysis], st->coeffs[ch], s->scratch, 1);
53 
54  for (i = 0; i < CELT_MAX_BANDS; i++)
55  st->bands[ch][i] = &st->coeffs[ch][ff_celt_freq_bands[i] << s->bsize_analysis];
56  }
57 
58  for (ch = 0; ch < s->avctx->channels; ch++) {
59  for (i = 0; i < CELT_MAX_BANDS; i++) {
60  float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
61  const int range = ff_celt_freq_range[i] << s->bsize_analysis;
62  const float *coeffs = st->bands[ch][i];
63  for (j = 0; j < range; j++)
64  energy += coeffs[j]*coeffs[j];
65 
66  st->energy[ch][i] += sqrtf(energy);
67  silence |= !!st->energy[ch][i];
68  avg_c_s = energy / range;
69 
70  for (j = 0; j < range; j++) {
71  const float c_s = coeffs[j]*coeffs[j];
72  dist_dev = (avg_c_s - c_s)*(avg_c_s - c_s);
73  }
74 
75  st->tone[ch][i] += sqrtf(dist_dev);
76  }
77  }
78 
79  st->silence = !silence;
80 
81  if (s->avctx->channels > 1) {
82  for (i = 0; i < CELT_MAX_BANDS; i++) {
83  float incompat = 0.0f;
84  const float *coeffs1 = st->bands[0][i];
85  const float *coeffs2 = st->bands[1][i];
86  const int range = ff_celt_freq_range[i] << s->bsize_analysis;
87  for (j = 0; j < range; j++)
88  incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
89  st->stereo[i] = sqrtf(incompat);
90  }
91  }
92 
93  for (ch = 0; ch < s->avctx->channels; ch++) {
94  for (i = 0; i < CELT_MAX_BANDS; i++) {
95  OpusBandExcitation *ex = &s->ex[ch][i];
96  float bp_e = bessel_filter(&s->bfilter_lo[ch][i], st->energy[ch][i]);
97  bp_e = bessel_filter(&s->bfilter_hi[ch][i], bp_e);
98  bp_e *= bp_e;
99  if (bp_e > ex->excitation) {
100  st->change_amp[ch][i] = bp_e - ex->excitation;
101  st->total_change += st->change_amp[ch][i];
102  ex->excitation = ex->excitation_init = bp_e;
103  ex->excitation_dist = 0.0f;
104  }
105  if (ex->excitation > 0.0f) {
106  ex->excitation -= av_clipf((1/expf(ex->excitation_dist)), ex->excitation_init/20, ex->excitation_init/1.09);
107  ex->excitation = FFMAX(ex->excitation, 0.0f);
108  ex->excitation_dist += 1.0f;
109  }
110  }
111  }
112 }
113 
114 static void search_for_change_points(OpusPsyContext *s, float tgt_change,
115  int offset_s, int offset_e, int resolution,
116  int level)
117 {
118  int i;
119  float c_change = 0.0f;
120  if ((offset_e - offset_s) <= resolution)
121  return;
122  for (i = offset_s; i < offset_e; i++) {
123  c_change += s->steps[i]->total_change;
124  if (c_change > tgt_change)
125  break;
126  }
127  if (i == offset_e)
128  return;
129  search_for_change_points(s, tgt_change / 2.0f, offset_s, i + 0, resolution, level + 1);
131  search_for_change_points(s, tgt_change / 2.0f, i + 1, offset_e, resolution, level + 1);
132 }
133 
135 {
136  int fsize, silent_frames;
137 
138  for (silent_frames = 0; silent_frames < s->buffered_steps; silent_frames++)
139  if (!s->steps[silent_frames]->silence)
140  break;
141  if (--silent_frames < 0)
142  return 0;
143 
144  for (fsize = CELT_BLOCK_960; fsize > CELT_BLOCK_120; fsize--) {
145  if ((1 << fsize) > silent_frames)
146  continue;
147  s->p.frames = FFMIN(silent_frames / (1 << fsize), 48 >> fsize);
148  s->p.framesize = fsize;
149  return 1;
150  }
151 
152  return 0;
153 }
154 
155 /* Main function which decides frame size and frames per current packet */
157 {
158  int max_delay_samples = (s->options->max_delay_ms*s->avctx->sample_rate)/1000;
159  int max_bsize = FFMIN(OPUS_SAMPLES_TO_BLOCK_SIZE(max_delay_samples), CELT_BLOCK_960);
160 
161  /* These don't change for now */
162  s->p.mode = OPUS_MODE_CELT;
164 
165  /* Flush silent frames ASAP */
166  if (s->steps[0]->silence && flush_silent_frames(s))
167  return;
168 
169  s->p.framesize = FFMIN(max_bsize, CELT_BLOCK_960);
170  s->p.frames = 1;
171 }
172 
174 {
175  int i;
176  float total_energy_change = 0.0f;
177 
178  if (s->buffered_steps < s->max_steps && !s->eof) {
179  const int awin = (1 << s->bsize_analysis);
180  if (++s->steps_to_process >= awin) {
181  step_collect_psy_metrics(s, s->buffered_steps - awin + 1);
182  s->steps_to_process = 0;
183  }
184  if ((++s->buffered_steps) < s->max_steps)
185  return 1;
186  }
187 
188  for (i = 0; i < s->buffered_steps; i++)
189  total_energy_change += s->steps[i]->total_change;
190 
191  search_for_change_points(s, total_energy_change / 2.0f, 0,
192  s->buffered_steps, 1, 0);
193 
195 
196  p->frames = s->p.frames;
197  p->framesize = s->p.framesize;
198  p->mode = s->p.mode;
199  p->bandwidth = s->p.bandwidth;
200 
201  return 0;
202 }
203 
205 {
206  int i, neighbouring_points = 0, start_offset = 0;
207  int radius = (1 << s->p.framesize), step_offset = radius*index;
208  int silence = 1;
209 
210  f->start_band = (s->p.mode == OPUS_MODE_HYBRID) ? 17 : 0;
212  f->channels = s->avctx->channels;
213  f->size = s->p.framesize;
214 
215  for (i = 0; i < (1 << f->size); i++)
216  silence &= s->steps[index*(1 << f->size) + i]->silence;
217 
218  f->silence = silence;
219  if (f->silence) {
220  f->framebits = 0; /* Otherwise the silence flag eats up 16(!) bits */
221  return;
222  }
223 
224  for (i = 0; i < s->inflection_points_count; i++) {
225  if (s->inflection_points[i] >= step_offset) {
226  start_offset = i;
227  break;
228  }
229  }
230 
231  for (i = start_offset; i < FFMIN(radius, s->inflection_points_count - start_offset); i++) {
232  if (s->inflection_points[i] < (step_offset + radius)) {
233  neighbouring_points++;
234  }
235  }
236 
237  /* Transient flagging */
238  f->transient = neighbouring_points > 0;
240 
241  /* Some sane defaults */
242  f->pfilter = 0;
243  f->pf_gain = 0.5f;
244  f->pf_octave = 2;
245  f->pf_period = 1;
246  f->pf_tapset = 2;
247 
248  /* More sane defaults */
249  f->tf_select = 0;
250  f->anticollapse = 1;
251  f->alloc_trim = 5;
252  f->skip_band_floor = f->end_band;
253  f->intensity_stereo = f->end_band;
254  f->dual_stereo = 0;
256  memset(f->tf_change, 0, sizeof(int)*CELT_MAX_BANDS);
257  memset(f->alloc_boost, 0, sizeof(int)*CELT_MAX_BANDS);
258 }
259 
261  CeltFrame *f_out)
262 {
263  int i, f, ch;
265  float rate, frame_bits = 0;
266 
267  /* Used for the global ROTATE flag */
268  float tonal = 0.0f;
269 
270  /* Pseudo-weights */
271  float band_score[CELT_MAX_BANDS] = { 0 };
272  float max_score = 1.0f;
273 
274  /* Pass one - one loop around each band, computing unquant stuff */
275  for (i = 0; i < CELT_MAX_BANDS; i++) {
276  float weight = 0.0f;
277  float tonal_contrib = 0.0f;
278  for (f = 0; f < (1 << s->p.framesize); f++) {
279  weight = start[f]->stereo[i];
280  for (ch = 0; ch < s->avctx->channels; ch++) {
281  weight += start[f]->change_amp[ch][i] + start[f]->tone[ch][i] + start[f]->energy[ch][i];
282  tonal_contrib += start[f]->tone[ch][i];
283  }
284  }
285  tonal += tonal_contrib;
286  band_score[i] = weight;
287  }
288 
289  tonal /= (float)CELT_MAX_BANDS;
290 
291  for (i = 0; i < CELT_MAX_BANDS; i++) {
292  if (band_score[i] > max_score)
293  max_score = band_score[i];
294  }
295 
296  for (i = 0; i < CELT_MAX_BANDS; i++) {
297  f_out->alloc_boost[i] = (int)((band_score[i]/max_score)*3.0f);
298  frame_bits += band_score[i]*8.0f;
299  }
300 
301  tonal /= 1333136.0f;
302  f_out->spread = av_clip_uintp2(lrintf(tonal), 2);
303 
304  rate = ((float)s->avctx->bit_rate) + frame_bits*frame_size*16;
305  rate *= s->lambda;
306  rate /= s->avctx->sample_rate/frame_size;
307 
308  f_out->framebits = lrintf(rate);
309  f_out->framebits = FFMIN(f_out->framebits, OPUS_MAX_PACKET_SIZE*8);
310  f_out->framebits = FFALIGN(f_out->framebits, 8);
311 }
312 
313 static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
314 {
315  int i, tdist = 0.0f;
316  OpusRangeCoder dump;
317 
318  ff_opus_rc_enc_init(&dump);
319  ff_celt_enc_bitalloc(&dump, f);
320 
321  for (i = 0; i < CELT_MAX_BANDS; i++) {
322  float bits = 0.0f;
323  float dist = f->pvq->band_cost(f->pvq, f, &dump, i, &bits, s->lambda);
324  tdist += dist;
325  }
326 
327  *total_dist = tdist;
328 
329  return 0;
330 }
331 
333 {
334  float td1, td2;
335  f->dual_stereo = 0;
336  bands_dist(s, f, &td1);
337  f->dual_stereo = 1;
338  bands_dist(s, f, &td2);
339 
340  f->dual_stereo = td2 < td1;
341  s->dual_stereo_used += td2 < td1;
342 }
343 
345 {
346  int i, best_band = CELT_MAX_BANDS - 1;
347  float dist, best_dist = FLT_MAX;
348 
349  /* TODO: fix, make some heuristic up here using the lambda value */
350  float end_band = 0;
351 
352  for (i = f->end_band; i >= end_band; i--) {
353  f->intensity_stereo = i;
354  bands_dist(s, f, &dist);
355  if (best_dist > dist) {
356  best_dist = dist;
357  best_band = i;
358  }
359  }
360 
361  f->intensity_stereo = best_band;
362  s->avg_is_band = (s->avg_is_band + f->intensity_stereo)/2.0f;
363 }
364 
366 {
367  int i, j, k, cway, config[2][CELT_MAX_BANDS] = { { 0 } };
368  float score[2] = { 0 };
369 
370  for (cway = 0; cway < 2; cway++) {
371  int mag[2];
372  int base = f->transient ? 120 : 960;
373  int i;
374 
375  for (i = 0; i < 2; i++) {
376  int c = ff_celt_tf_select[f->size][f->transient][cway][i];
377  mag[i] = c < 0 ? base >> FFABS(c) : base << FFABS(c);
378  }
379 
380  for (i = 0; i < CELT_MAX_BANDS; i++) {
381  float iscore0 = 0.0f;
382  float iscore1 = 0.0f;
383  for (j = 0; j < (1 << f->size); j++) {
384  for (k = 0; k < s->avctx->channels; k++) {
385  iscore0 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[0];
386  iscore1 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[1];
387  }
388  }
389  config[cway][i] = FFABS(iscore0 - 1.0f) < FFABS(iscore1 - 1.0f);
390  score[cway] += config[cway][i] ? iscore1 : iscore0;
391  }
392  }
393 
394  f->tf_select = score[0] < score[1];
395  memcpy(f->tf_change, config[f->tf_select], sizeof(int)*CELT_MAX_BANDS);
396 
397  return 0;
398 }
399 
401 {
402  int start_transient_flag = f->transient;
403  OpusPsyStep **start = &s->steps[index * (1 << s->p.framesize)];
404 
405  if (f->silence)
406  return 0;
407 
408  celt_gauge_psy_weight(s, start, f);
411  celt_search_for_tf(s, start, f);
412 
413  if (f->transient != start_transient_flag) {
415  s->redo_analysis = 1;
416  return 1;
417  }
418 
419  s->redo_analysis = 0;
420 
421  return 0;
422 }
423 
425 {
426  int i, frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
427  int steps_out = s->p.frames*(frame_size/120);
428  void *tmp[FF_BUFQUEUE_SIZE];
429  float ideal_fbits;
430 
431  for (i = 0; i < steps_out; i++)
432  memset(s->steps[i], 0, sizeof(OpusPsyStep));
433 
434  for (i = 0; i < s->max_steps; i++)
435  tmp[i] = s->steps[i];
436 
437  for (i = 0; i < s->max_steps; i++) {
438  const int i_new = i - steps_out;
439  s->steps[i_new < 0 ? s->max_steps + i_new : i_new] = tmp[i];
440  }
441 
442  for (i = steps_out; i < s->buffered_steps; i++)
443  s->steps[i]->index -= steps_out;
444 
445  ideal_fbits = s->avctx->bit_rate/(s->avctx->sample_rate/frame_size);
446 
447  for (i = 0; i < s->p.frames; i++) {
448  s->avg_is_band += f[i].intensity_stereo;
449  s->lambda *= ideal_fbits / f[i].framebits;
450  }
451 
452  s->avg_is_band /= (s->p.frames + 1);
453 
454  s->cs_num = 0;
455  s->steps_to_process = 0;
456  s->buffered_steps -= steps_out;
457  s->total_packets_out += s->p.frames;
459 }
460 
462  struct FFBufQueue *bufqueue, OpusEncOptions *options)
463 {
464  int i, ch, ret;
465 
466  s->redo_analysis = 0;
467  s->lambda = 1.0f;
468  s->options = options;
469  s->avctx = avctx;
470  s->bufqueue = bufqueue;
471  s->max_steps = ceilf(s->options->max_delay_ms/2.5f);
473  s->avg_is_band = CELT_MAX_BANDS - 1;
475 
477  if (!s->inflection_points) {
478  ret = AVERROR(ENOMEM);
479  goto fail;
480  }
481 
483  if (!s->dsp) {
484  ret = AVERROR(ENOMEM);
485  goto fail;
486  }
487 
488  for (ch = 0; ch < s->avctx->channels; ch++) {
489  for (i = 0; i < CELT_MAX_BANDS; i++) {
490  bessel_init(&s->bfilter_hi[ch][i], 1.0f, 19.0f, 100.0f, 1);
491  bessel_init(&s->bfilter_lo[ch][i], 1.0f, 20.0f, 100.0f, 0);
492  }
493  }
494 
495  for (i = 0; i < s->max_steps; i++) {
496  s->steps[i] = av_mallocz(sizeof(OpusPsyStep));
497  if (!s->steps[i]) {
498  ret = AVERROR(ENOMEM);
499  goto fail;
500  }
501  }
502 
503  for (i = 0; i < CELT_BLOCK_NB; i++) {
504  float tmp;
505  const int len = OPUS_BLOCK_SIZE(i);
506  s->window[i] = av_malloc(2*len*sizeof(float));
507  if (!s->window[i]) {
508  ret = AVERROR(ENOMEM);
509  goto fail;
510  }
511  generate_window_func(s->window[i], 2*len, WFUNC_SINE, &tmp);
512  if ((ret = ff_mdct15_init(&s->mdct[i], 0, i + 3, 68 << (CELT_BLOCK_NB - 1 - i))))
513  goto fail;
514  }
515 
516  return 0;
517 
518 fail:
520  av_freep(&s->dsp);
521 
522  for (i = 0; i < CELT_BLOCK_NB; i++) {
523  ff_mdct15_uninit(&s->mdct[i]);
524  av_freep(&s->window[i]);
525  }
526 
527  for (i = 0; i < s->max_steps; i++)
528  av_freep(&s->steps[i]);
529 
530  return ret;
531 }
532 
534 {
535  s->eof = 1;
536 }
537 
539 {
540  int i;
541 
543  av_freep(&s->dsp);
544 
545  for (i = 0; i < CELT_BLOCK_NB; i++) {
546  ff_mdct15_uninit(&s->mdct[i]);
547  av_freep(&s->window[i]);
548  }
549 
550  for (i = 0; i < s->max_steps; i++)
551  av_freep(&s->steps[i]);
552 
553  av_log(s->avctx, AV_LOG_INFO, "Average Intensity Stereo band: %0.1f\n", s->avg_is_band);
554  av_log(s->avctx, AV_LOG_INFO, "Dual Stereo used: %0.2f%%\n", ((float)s->dual_stereo_used/s->total_packets_out)*100.0f);
555 
556  return 0;
557 }
int channels
Definition: opus_celt.h:99
float max_delay_ms
Definition: opusenc.h:44
MDCT15Context * mdct[CELT_BLOCK_NB]
Definition: opusenc_psy.h:71
void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
Definition: opusenc_psy.c:204
static int flush_silent_frames(OpusPsyContext *s)
Definition: opusenc_psy.c:134
#define OPUS_SAMPLES_TO_BLOCK_SIZE(x)
Definition: opusenc.h:41
AVCodecContext * avctx
Definition: opusenc_psy.h:55
int64_t total_packets_out
Definition: opusenc_psy.h:80
int anticollapse
Definition: opus_celt.h:116
enum OpusBandwidth bandwidth
Definition: opusenc.h:49
const char * s
Definition: avisynth_c.h:768
struct FFBufQueue * bufqueue
Definition: opusenc_psy.h:57
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
int framebits
Definition: opus_celt.h:130
float * window[CELT_BLOCK_NB]
Definition: opusenc_psy.h:70
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
Definition: opus_rc.c:402
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1826
static FFServerConfig config
Definition: ffserver.c:193
OpusPsyStep * steps[FF_BUFQUEUE_SIZE+1]
Definition: opusenc_psy.h:67
FFBesselFilter bfilter_hi[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:65
FFBesselFilter bfilter_lo[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:64
const uint8_t ff_celt_freq_bands[]
Definition: opustab.c:763
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:35
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
#define OPUS_MAX_PACKET_SIZE
Definition: opus_rc.h:29
int pf_period
Definition: opus_celt.h:125
void ff_celt_enc_bitalloc(OpusRangeCoder *rc, CeltFrame *f)
Definition: opusenc.c:285
Structure holding the queue.
Definition: bufferqueue.h:49
const uint8_t ff_celt_band_end[]
Definition: opustab.c:27
float coeffs[OPUS_MAX_CHANNELS][OPUS_BLOCK_SIZE(CELT_BLOCK_960)]
Definition: opusenc_psy.h:40
OpusEncOptions * options
Definition: opusenc_psy.h:58
static float bessel_filter(FFBesselFilter *s, float x)
Definition: opusenc_utils.h:76
uint8_t bits
Definition: crc.c:296
#define av_cold
Definition: attributes.h:82
#define CELT_OVERLAP
Definition: opus.h:42
#define av_malloc(s)
int silence
Definition: opus_celt.h:114
int * inflection_points
Definition: opusenc_psy.h:90
float stereo[CELT_MAX_BANDS]
Definition: opusenc_psy.h:35
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
Definition: opusenc_psy.c:173
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
Definition: mdct15.c:247
int dual_stereo
Definition: opus_celt.h:118
int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
Definition: opusenc_psy.c:400
enum OpusMode mode
Definition: opusenc.h:48
#define lrintf(x)
Definition: libm_mips.h:70
int skip_band_floor
Definition: opus_celt.h:108
const OptionDef options[]
Definition: ffserver.c:3948
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int end_band
Definition: opus_celt.h:104
#define expf(x)
Definition: libm.h:283
int alloc_boost[CELT_MAX_BANDS]
Definition: opus_celt.h:111
static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
Definition: opusenc_psy.c:313
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:127
AVFloatDSPContext * dsp
Definition: opusenc_psy.h:56
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
#define OPUS_BLOCK_SIZE(x)
Definition: opusenc.h:39
#define AVERROR(e)
Definition: error.h:43
int start_band
Definition: opus_celt.h:103
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1856
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
Definition: opusenc_psy.c:461
#define FF_BUFQUEUE_SIZE
int tf_change[CELT_MAX_BANDS]
Definition: opus_celt.h:137
float total_change
Definition: opusenc_psy.h:37
int pfilter
Definition: opus_celt.h:107
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:109
float pf_gain
Definition: opus_celt.h:127
static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
Definition: opusenc_psy.c:332
float * bands[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:39
const int8_t ff_celt_tf_select[4][2][2][2]
Definition: opustab.c:777
static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f_out)
Definition: opusenc_psy.c:260
static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
Definition: opusenc_psy.c:365
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:929
#define FFMIN(a, b)
Definition: common.h:96
float tone[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:34
float change_amp[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:36
static void psy_output_groups(OpusPsyContext *s)
Definition: opusenc_psy.c:156
float avg_is_band
Definition: opusenc_psy.h:78
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int blocks
Definition: opus_celt.h:112
int inflection_points_count
Definition: opusenc_psy.h:91
int transient
Definition: opus_celt.h:106
const uint8_t ff_celt_freq_range[]
Definition: opustab.c:767
float(* band_cost)(struct CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
Definition: opus_pvq.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int frame_size
Definition: mxfenc.c:1896
#define CELT_MAX_BANDS
Definition: opus.h:45
int sample_rate
samples per second
Definition: avcodec.h:2523
int pf_tapset
Definition: opus_celt.h:126
main external API structure.
Definition: avcodec.h:1761
static int bessel_init(FFBesselFilter *s, float n, float f0, float fs, int highpass)
Definition: opusenc_utils.h:69
void ff_opus_psy_signal_eof(OpusPsyContext *s)
Definition: opusenc_psy.c:533
float scratch[2048]
Definition: opusenc_psy.h:74
int index
Definition: gxfenc.c:89
static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
Definition: opusenc_psy.c:344
int64_t dual_stereo_used
Definition: opusenc_psy.h:79
OpusPacketInfo p
Definition: opusenc_psy.h:84
av_cold int ff_opus_psy_end(OpusPsyContext *s)
Definition: opusenc_psy.c:538
CeltPVQ * pvq
Definition: opus_celt.h:98
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc)
Definition: opusenc_psy.c:424
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1522
int framesize
Definition: opusenc.h:50
uint8_t level
Definition: svq3.c:207
int
static double c[64]
enum CeltSpread spread
Definition: opus_celt.h:121
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
Definition: mdct15.c:43
int tf_select
Definition: opus_celt.h:109
static const int16_t coeffs[]
int len
static void step_collect_psy_metrics(OpusPsyContext *s, int index)
Definition: opusenc_psy.c:29
int channels
number of audio channels
Definition: avcodec.h:2524
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
float energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:33
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
static void search_for_change_points(OpusPsyContext *s, float tgt_change, int offset_s, int offset_e, int resolution, int level)
Definition: opusenc_psy.c:114
enum CeltBlockSize size
Definition: opus_celt.h:102
int alloc_trim
Definition: opus_celt.h:110
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:248
void(* mdct)(struct MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
Definition: mdct15.h:49
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:267
for(j=16;j >0;--j)
int pf_octave
Definition: opus_celt.h:124
int intensity_stereo
Definition: opus_celt.h:117
OpusBandExcitation ex[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
Definition: opusenc_psy.h:63
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition: bufferqueue.h:87
static uint8_t tmp[11]
Definition: aes_ctr.c:26
int steps_to_process
Definition: opusenc_psy.h:87