FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
opus_celt.c
Go to the documentation of this file.
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) */
35 {
36  int i, j;
37  float prev[2] = { 0 };
38  float alpha = ff_celt_alpha_coef[f->size];
39  float beta = ff_celt_beta_coef[f->size];
40  const uint8_t *model = ff_celt_coarse_energy_dist[f->size][0];
41 
42  /* intra frame */
43  if (opus_rc_tell(rc) + 3 <= f->framebits && ff_opus_rc_dec_log(rc, 3)) {
44  alpha = 0.0f;
45  beta = 1.0f - (4915.0f/32768.0f);
46  model = ff_celt_coarse_energy_dist[f->size][1];
47  }
48 
49  for (i = 0; i < CELT_MAX_BANDS; i++) {
50  for (j = 0; j < f->channels; j++) {
51  CeltBlock *block = &f->block[j];
52  float value;
53  int available;
54 
55  if (i < f->start_band || i >= f->end_band) {
56  block->energy[i] = 0.0;
57  continue;
58  }
59 
60  available = f->framebits - opus_rc_tell(rc);
61  if (available >= 15) {
62  /* decode using a Laplace distribution */
63  int k = FFMIN(i, 20) << 1;
64  value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6);
65  } else if (available >= 2) {
67  value = (x>>1) ^ -(x&1);
68  } else if (available >= 1) {
69  value = -(float)ff_opus_rc_dec_log(rc, 1);
70  } else value = -1;
71 
72  block->energy[i] = FFMAX(-9.0f, block->energy[i]) * alpha + prev[j] + value;
73  prev[j] += beta * value;
74  }
75  }
76 }
77 
79 {
80  int i;
81  for (i = f->start_band; i < f->end_band; i++) {
82  int j;
83  if (!f->fine_bits[i])
84  continue;
85 
86  for (j = 0; j < f->channels; j++) {
87  CeltBlock *block = &f->block[j];
88  int q2;
89  float offset;
90  q2 = ff_opus_rc_get_raw(rc, f->fine_bits[i]);
91  offset = (q2 + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f - 0.5f;
92  block->energy[i] += offset;
93  }
94  }
95 }
96 
98 {
99  int priority, i, j;
100  int bits_left = f->framebits - opus_rc_tell(rc);
101 
102  for (priority = 0; priority < 2; priority++) {
103  for (i = f->start_band; i < f->end_band && bits_left >= f->channels; i++) {
104  if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS)
105  continue;
106 
107  for (j = 0; j < f->channels; j++) {
108  int q2;
109  float offset;
110  q2 = ff_opus_rc_get_raw(rc, 1);
111  offset = (q2 - 0.5f) * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f;
112  f->block[j].energy[i] += offset;
113  bits_left--;
114  }
115  }
116  }
117 }
118 
120 {
121  int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit;
122  int consumed, bits = f->transient ? 2 : 4;
123 
124  consumed = opus_rc_tell(rc);
125  tf_select_bit = (f->size != 0 && consumed+bits+1 <= f->framebits);
126 
127  for (i = f->start_band; i < f->end_band; i++) {
128  if (consumed+bits+tf_select_bit <= f->framebits) {
129  diff ^= ff_opus_rc_dec_log(rc, bits);
130  consumed = opus_rc_tell(rc);
131  tf_changed |= diff;
132  }
133  f->tf_change[i] = diff;
134  bits = f->transient ? 4 : 5;
135  }
136 
137  if (tf_select_bit && ff_celt_tf_select[f->size][f->transient][0][tf_changed] !=
138  ff_celt_tf_select[f->size][f->transient][1][tf_changed])
139  tf_select = ff_opus_rc_dec_log(rc, 1);
140 
141  for (i = f->start_band; i < f->end_band; i++) {
142  f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]];
143  }
144 }
145 
147 {
148  // approx. maximum bit allocation for each band before boost/trim
149  int cap[CELT_MAX_BANDS];
150  int boost[CELT_MAX_BANDS];
151  int threshold[CELT_MAX_BANDS];
152  int bits1[CELT_MAX_BANDS];
153  int bits2[CELT_MAX_BANDS];
154  int trim_offset[CELT_MAX_BANDS];
155 
156  int skip_start_band = f->start_band;
157  int dynalloc = 6;
158  int alloctrim = 5;
159  int extrabits = 0;
160 
161  int skip_bit = 0;
162  int intensity_stereo_bit = 0;
163  int dual_stereo_bit = 0;
164 
165  int remaining, bandbits;
166  int low, high, total, done;
167  int totalbits;
168  int consumed;
169  int i, j;
170 
171  consumed = opus_rc_tell(rc);
172 
173  /* obtain spread flag */
175  if (consumed + 4 <= f->framebits)
177 
178  /* generate static allocation caps */
179  for (i = 0; i < CELT_MAX_BANDS; i++) {
180  cap[i] = (ff_celt_static_caps[f->size][f->channels - 1][i] + 64)
181  * ff_celt_freq_range[i] << (f->channels - 1) << f->size >> 2;
182  }
183 
184  /* obtain band boost */
185  totalbits = f->framebits << 3; // convert to 1/8 bits
186  consumed = opus_rc_tell_frac(rc);
187  for (i = f->start_band; i < f->end_band; i++) {
188  int quanta, band_dynalloc;
189 
190  boost[i] = 0;
191 
192  quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
193  quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
194  band_dynalloc = dynalloc;
195  while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {
196  int add = ff_opus_rc_dec_log(rc, band_dynalloc);
197  consumed = opus_rc_tell_frac(rc);
198  if (!add)
199  break;
200 
201  boost[i] += quanta;
202  totalbits -= quanta;
203  band_dynalloc = 1;
204  }
205  /* dynalloc is more likely to occur if it's already been used for earlier bands */
206  if (boost[i])
207  dynalloc = FFMAX(2, dynalloc - 1);
208  }
209 
210  /* obtain allocation trim */
211  if (consumed + (6 << 3) <= totalbits)
213 
214  /* anti-collapse bit reservation */
215  totalbits = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
216  f->anticollapse_needed = 0;
217  if (f->blocks > 1 && f->size >= 2 &&
218  totalbits >= ((f->size + 2) << 3))
219  f->anticollapse_needed = 1 << 3;
220  totalbits -= f->anticollapse_needed;
221 
222  /* band skip bit reservation */
223  if (totalbits >= 1 << 3)
224  skip_bit = 1 << 3;
225  totalbits -= skip_bit;
226 
227  /* intensity/dual stereo bit reservation */
228  if (f->channels == 2) {
229  intensity_stereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
230  if (intensity_stereo_bit <= totalbits) {
231  totalbits -= intensity_stereo_bit;
232  if (totalbits >= 1 << 3) {
233  dual_stereo_bit = 1 << 3;
234  totalbits -= 1 << 3;
235  }
236  } else
237  intensity_stereo_bit = 0;
238  }
239 
240  for (i = f->start_band; i < f->end_band; i++) {
241  int trim = alloctrim - 5 - f->size;
242  int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
243  int duration = f->size + 3;
244  int scale = duration + f->channels - 1;
245 
246  /* PVQ minimum allocation threshold, below this value the band is
247  * skipped */
248  threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
249  f->channels << 3);
250 
251  trim_offset[i] = trim * (band << scale) >> 6;
252 
253  if (ff_celt_freq_range[i] << f->size == 1)
254  trim_offset[i] -= f->channels << 3;
255  }
256 
257  /* bisection */
258  low = 1;
259  high = CELT_VECTORS - 1;
260  while (low <= high) {
261  int center = (low + high) >> 1;
262  done = total = 0;
263 
264  for (i = f->end_band - 1; i >= f->start_band; i--) {
265  bandbits = ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]
266  << (f->channels - 1) << f->size >> 2;
267 
268  if (bandbits)
269  bandbits = FFMAX(0, bandbits + trim_offset[i]);
270  bandbits += boost[i];
271 
272  if (bandbits >= threshold[i] || done) {
273  done = 1;
274  total += FFMIN(bandbits, cap[i]);
275  } else if (bandbits >= f->channels << 3)
276  total += f->channels << 3;
277  }
278 
279  if (total > totalbits)
280  high = center - 1;
281  else
282  low = center + 1;
283  }
284  high = low--;
285 
286  for (i = f->start_band; i < f->end_band; i++) {
287  bits1[i] = ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]
288  << (f->channels - 1) << f->size >> 2;
289  bits2[i] = high >= CELT_VECTORS ? cap[i] :
291  << (f->channels - 1) << f->size >> 2;
292 
293  if (bits1[i])
294  bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);
295  if (bits2[i])
296  bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);
297  if (low)
298  bits1[i] += boost[i];
299  bits2[i] += boost[i];
300 
301  if (boost[i])
302  skip_start_band = i;
303  bits2[i] = FFMAX(0, bits2[i] - bits1[i]);
304  }
305 
306  /* bisection */
307  low = 0;
308  high = 1 << CELT_ALLOC_STEPS;
309  for (i = 0; i < CELT_ALLOC_STEPS; i++) {
310  int center = (low + high) >> 1;
311  done = total = 0;
312 
313  for (j = f->end_band - 1; j >= f->start_band; j--) {
314  bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
315 
316  if (bandbits >= threshold[j] || done) {
317  done = 1;
318  total += FFMIN(bandbits, cap[j]);
319  } else if (bandbits >= f->channels << 3)
320  total += f->channels << 3;
321  }
322  if (total > totalbits)
323  high = center;
324  else
325  low = center;
326  }
327 
328  done = total = 0;
329  for (i = f->end_band - 1; i >= f->start_band; i--) {
330  bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
331 
332  if (bandbits >= threshold[i] || done)
333  done = 1;
334  else
335  bandbits = (bandbits >= f->channels << 3) ?
336  f->channels << 3 : 0;
337 
338  bandbits = FFMIN(bandbits, cap[i]);
339  f->pulses[i] = bandbits;
340  total += bandbits;
341  }
342 
343  /* band skipping */
344  for (f->coded_bands = f->end_band; ; f->coded_bands--) {
345  int allocation;
346  j = f->coded_bands - 1;
347 
348  if (j == skip_start_band) {
349  /* all remaining bands are not skipped */
350  totalbits += skip_bit;
351  break;
352  }
353 
354  /* determine the number of bits available for coding "do not skip" markers */
355  remaining = totalbits - total;
356  bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
357  remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
358  allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j]
359  + FFMAX(0, remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]));
360 
361  /* a "do not skip" marker is only coded if the allocation is
362  above the chosen threshold */
363  if (allocation >= FFMAX(threshold[j], (f->channels + 1) <<3 )) {
364  if (ff_opus_rc_dec_log(rc, 1))
365  break;
366 
367  total += 1 << 3;
368  allocation -= 1 << 3;
369  }
370 
371  /* the band is skipped, so reclaim its bits */
372  total -= f->pulses[j];
373  if (intensity_stereo_bit) {
374  total -= intensity_stereo_bit;
375  intensity_stereo_bit = ff_celt_log2_frac[j - f->start_band];
376  total += intensity_stereo_bit;
377  }
378 
379  total += f->pulses[j] = (allocation >= f->channels << 3) ?
380  f->channels << 3 : 0;
381  }
382 
383  /* obtain stereo flags */
384  f->intensity_stereo = 0;
385  f->dual_stereo = 0;
386  if (intensity_stereo_bit)
387  f->intensity_stereo = f->start_band +
388  ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
389  if (f->intensity_stereo <= f->start_band)
390  totalbits += dual_stereo_bit; /* no intensity stereo means no dual stereo */
391  else if (dual_stereo_bit)
392  f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
393 
394  /* supply the remaining bits in this frame to lower bands */
395  remaining = totalbits - total;
396  bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
397  remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
398  for (i = f->start_band; i < f->coded_bands; i++) {
399  int bits = FFMIN(remaining, ff_celt_freq_range[i]);
400 
401  f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
402  remaining -= bits;
403  }
404 
405  for (i = f->start_band; i < f->coded_bands; i++) {
406  int N = ff_celt_freq_range[i] << f->size;
407  int prev_extra = extrabits;
408  f->pulses[i] += extrabits;
409 
410  if (N > 1) {
411  int dof; // degrees of freedom
412  int temp; // dof * channels * log(dof)
413  int offset; // fine energy quantization offset, i.e.
414  // extra bits assigned over the standard
415  // totalbits/dof
416  int fine_bits, max_bits;
417 
418  extrabits = FFMAX(0, f->pulses[i] - cap[i]);
419  f->pulses[i] -= extrabits;
420 
421  /* intensity stereo makes use of an extra degree of freedom */
422  dof = N * f->channels
423  + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
424  temp = dof * (ff_celt_log_freq_range[i] + (f->size<<3));
425  offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
426  if (N == 2) /* dof=2 is the only case that doesn't fit the model */
427  offset += dof<<1;
428 
429  /* grant an additional bias for the first and second pulses */
430  if (f->pulses[i] + offset < 2 * (dof << 3))
431  offset += temp >> 2;
432  else if (f->pulses[i] + offset < 3 * (dof << 3))
433  offset += temp >> 3;
434 
435  fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
436  max_bits = FFMIN((f->pulses[i]>>3) >> (f->channels - 1),
438 
439  max_bits = FFMAX(max_bits, 0);
440 
441  f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
442 
443  /* if fine_bits was rounded down or capped,
444  give priority for the final fine energy pass */
445  f->fine_priority[i] = (f->fine_bits[i] * (dof<<3) >= f->pulses[i] + offset);
446 
447  /* the remaining bits are assigned to PVQ */
448  f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
449  } else {
450  /* all bits go to fine energy except for the sign bit */
451  extrabits = FFMAX(0, f->pulses[i] - (f->channels << 3));
452  f->pulses[i] -= extrabits;
453  f->fine_bits[i] = 0;
454  f->fine_priority[i] = 1;
455  }
456 
457  /* hand back a limited number of extra fine energy bits to this band */
458  if (extrabits > 0) {
459  int fineextra = FFMIN(extrabits >> (f->channels + 2),
460  CELT_MAX_FINE_BITS - f->fine_bits[i]);
461  f->fine_bits[i] += fineextra;
462 
463  fineextra <<= f->channels + 2;
464  f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
465  extrabits -= fineextra;
466  }
467  }
468  f->remaining = extrabits;
469 
470  /* skipped bands dedicate all of their bits for fine energy */
471  for (; i < f->end_band; i++) {
472  f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
473  f->pulses[i] = 0;
474  f->fine_priority[i] = f->fine_bits[i] < 1;
475  }
476 }
477 
478 static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
479 {
480  int i, j;
481 
482  for (i = f->start_band; i < f->end_band; i++) {
483  float *dst = data + (ff_celt_freq_bands[i] << f->size);
484  float log_norm = block->energy[i] + ff_celt_mean_energy[i];
485  float norm = exp2f(FFMIN(log_norm, 32.0f));
486 
487  for (j = 0; j < ff_celt_freq_range[i] << f->size; j++)
488  dst[j] *= norm;
489  }
490 }
491 
493 {
494  const int T0 = block->pf_period_old;
495  const int T1 = block->pf_period;
496 
497  float g00, g01, g02;
498  float g10, g11, g12;
499 
500  float x0, x1, x2, x3, x4;
501 
502  int i;
503 
504  if (block->pf_gains[0] == 0.0 &&
505  block->pf_gains_old[0] == 0.0)
506  return;
507 
508  g00 = block->pf_gains_old[0];
509  g01 = block->pf_gains_old[1];
510  g02 = block->pf_gains_old[2];
511  g10 = block->pf_gains[0];
512  g11 = block->pf_gains[1];
513  g12 = block->pf_gains[2];
514 
515  x1 = data[-T1 + 1];
516  x2 = data[-T1];
517  x3 = data[-T1 - 1];
518  x4 = data[-T1 - 2];
519 
520  for (i = 0; i < CELT_OVERLAP; i++) {
521  float w = ff_celt_window2[i];
522  x0 = data[i - T1 + 2];
523 
524  data[i] += (1.0 - w) * g00 * data[i - T0] +
525  (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
526  (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
527  w * g10 * x2 +
528  w * g11 * (x1 + x3) +
529  w * g12 * (x0 + x4);
530  x4 = x3;
531  x3 = x2;
532  x2 = x1;
533  x1 = x0;
534  }
535 }
536 
537 static void celt_postfilter_apply(CeltBlock *block, float *data, int len)
538 {
539  const int T = block->pf_period;
540  float g0, g1, g2;
541  float x0, x1, x2, x3, x4;
542  int i;
543 
544  if (block->pf_gains[0] == 0.0 || len <= 0)
545  return;
546 
547  g0 = block->pf_gains[0];
548  g1 = block->pf_gains[1];
549  g2 = block->pf_gains[2];
550 
551  x4 = data[-T - 2];
552  x3 = data[-T - 1];
553  x2 = data[-T];
554  x1 = data[-T + 1];
555 
556  for (i = 0; i < len; i++) {
557  x0 = data[i - T + 2];
558  data[i] += g0 * x2 +
559  g1 * (x1 + x3) +
560  g2 * (x0 + x4);
561  x4 = x3;
562  x3 = x2;
563  x2 = x1;
564  x1 = x0;
565  }
566 }
567 
569 {
570  int len = f->blocksize * f->blocks;
571 
572  celt_postfilter_apply_transition(block, block->buf + 1024);
573 
574  block->pf_period_old = block->pf_period;
575  memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
576 
577  block->pf_period = block->pf_period_new;
578  memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains));
579 
580  if (len > CELT_OVERLAP) {
581  celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP);
582  celt_postfilter_apply(block, block->buf + 1024 + 2 * CELT_OVERLAP,
583  len - 2 * CELT_OVERLAP);
584 
585  block->pf_period_old = block->pf_period;
586  memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
587  }
588 
589  memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
590 }
591 
592 static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
593 {
594  int i;
595 
596  memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new));
597  memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new));
598 
599  if (f->start_band == 0 && consumed + 16 <= f->framebits) {
600  int has_postfilter = ff_opus_rc_dec_log(rc, 1);
601  if (has_postfilter) {
602  float gain;
603  int tapset, octave, period;
604 
605  octave = ff_opus_rc_dec_uint(rc, 6);
606  period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
607  gain = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
608  tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ?
610 
611  for (i = 0; i < 2; i++) {
612  CeltBlock *block = &f->block[i];
613 
615  block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0];
616  block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1];
617  block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2];
618  }
619  }
620 
621  consumed = opus_rc_tell(rc);
622  }
623 
624  return consumed;
625 }
626 
627 static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
628 {
629  int i, j, k;
630 
631  for (i = f->start_band; i < f->end_band; i++) {
632  int renormalize = 0;
633  float *xptr;
634  float prev[2];
635  float Ediff, r;
636  float thresh, sqrt_1;
637  int depth;
638 
639  /* depth in 1/8 bits */
640  depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size);
641  thresh = exp2f(-1.0 - 0.125f * depth);
642  sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size);
643 
644  xptr = X + (ff_celt_freq_bands[i] << f->size);
645 
646  prev[0] = block->prev_energy[0][i];
647  prev[1] = block->prev_energy[1][i];
648  if (f->channels == 1) {
649  CeltBlock *block1 = &f->block[1];
650 
651  prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]);
652  prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]);
653  }
654  Ediff = block->energy[i] - FFMIN(prev[0], prev[1]);
655  Ediff = FFMAX(0, Ediff);
656 
657  /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
658  short blocks don't have the same energy as long */
659  r = exp2f(1 - Ediff);
660  if (f->size == 3)
661  r *= M_SQRT2;
662  r = FFMIN(thresh, r) * sqrt_1;
663  for (k = 0; k < 1 << f->size; k++) {
664  /* Detect collapse */
665  if (!(block->collapse_masks[i] & 1 << k)) {
666  /* Fill with noise */
667  for (j = 0; j < ff_celt_freq_range[i]; j++)
668  xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r;
669  renormalize = 1;
670  }
671  }
672 
673  /* We just added some energy, so we need to renormalize */
674  if (renormalize)
675  celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f);
676  }
677 }
678 
680  float **output, int channels, int frame_size,
681  int start_band, int end_band)
682 {
683  int i, j, downmix = 0;
684  int consumed; // bits of entropy consumed thus far for this frame
685  MDCT15Context *imdct;
686 
687  if (channels != 1 && channels != 2) {
688  av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
689  channels);
690  return AVERROR_INVALIDDATA;
691  }
692  if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) {
693  av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
694  start_band, end_band);
695  return AVERROR_INVALIDDATA;
696  }
697 
698  f->silence = 0;
699  f->transient = 0;
700  f->anticollapse = 0;
701  f->flushed = 0;
702  f->channels = channels;
703  f->start_band = start_band;
704  f->end_band = end_band;
705  f->framebits = rc->rb.bytes * 8;
706 
707  f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
708  if (f->size > CELT_MAX_LOG_BLOCKS ||
709  frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) {
710  av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
711  frame_size);
712  return AVERROR_INVALIDDATA;
713  }
714 
715  if (!f->output_channels)
717 
718  for (i = 0; i < f->channels; i++) {
719  memset(f->block[i].coeffs, 0, sizeof(f->block[i].coeffs));
720  memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks));
721  }
722 
723  consumed = opus_rc_tell(rc);
724 
725  /* obtain silence flag */
726  if (consumed >= f->framebits)
727  f->silence = 1;
728  else if (consumed == 1)
729  f->silence = ff_opus_rc_dec_log(rc, 15);
730 
731 
732  if (f->silence) {
733  consumed = f->framebits;
734  rc->total_bits += f->framebits - opus_rc_tell(rc);
735  }
736 
737  /* obtain post-filter options */
738  consumed = parse_postfilter(f, rc, consumed);
739 
740  /* obtain transient flag */
741  if (f->size != 0 && consumed+3 <= f->framebits)
742  f->transient = ff_opus_rc_dec_log(rc, 3);
743 
744  f->blocks = f->transient ? 1 << f->size : 1;
745  f->blocksize = frame_size / f->blocks;
746 
747  imdct = f->imdct[f->transient ? 0 : f->size];
748 
749  if (channels == 1) {
750  for (i = 0; i < CELT_MAX_BANDS; i++)
751  f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]);
752  }
753 
755  celt_decode_tf_changes (f, rc);
756  celt_decode_allocation (f, rc);
757  celt_decode_fine_energy (f, rc);
758  ff_celt_quant_bands (f, rc);
759 
760  if (f->anticollapse_needed)
761  f->anticollapse = ff_opus_rc_get_raw(rc, 1);
762 
764 
765  /* apply anti-collapse processing and denormalization to
766  * each coded channel */
767  for (i = 0; i < f->channels; i++) {
768  CeltBlock *block = &f->block[i];
769 
770  if (f->anticollapse)
771  process_anticollapse(f, block, f->block[i].coeffs);
772 
773  celt_denormalize(f, block, f->block[i].coeffs);
774  }
775 
776  /* stereo -> mono downmix */
777  if (f->output_channels < f->channels) {
778  f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16));
779  downmix = 1;
780  } else if (f->output_channels > f->channels)
781  memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float));
782 
783  if (f->silence) {
784  for (i = 0; i < 2; i++) {
785  CeltBlock *block = &f->block[i];
786 
787  for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++)
788  block->energy[j] = CELT_ENERGY_SILENCE;
789  }
790  memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs));
791  memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs));
792  }
793 
794  /* transform and output for each output channel */
795  for (i = 0; i < f->output_channels; i++) {
796  CeltBlock *block = &f->block[i];
797  float m = block->emph_coeff;
798 
799  /* iMDCT and overlap-add */
800  for (j = 0; j < f->blocks; j++) {
801  float *dst = block->buf + 1024 + j * f->blocksize;
802 
803  imdct->imdct_half(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j,
804  f->blocks);
805  f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
807  }
808 
809  if (downmix)
810  f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size);
811 
812  /* postfilter */
813  celt_postfilter(f, block);
814 
815  /* deemphasis and output scaling */
816  for (j = 0; j < frame_size; j++) {
817  const float tmp = block->buf[1024 - frame_size + j] + m;
818  m = tmp * CELT_EMPH_COEFF;
819  output[i][j] = tmp;
820  }
821 
822  block->emph_coeff = m;
823  }
824 
825  if (channels == 1)
826  memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy));
827 
828  for (i = 0; i < 2; i++ ) {
829  CeltBlock *block = &f->block[i];
830 
831  if (!f->transient) {
832  memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0]));
833  memcpy(block->prev_energy[0], block->energy, sizeof(block->prev_energy[0]));
834  } else {
835  for (j = 0; j < CELT_MAX_BANDS; j++)
836  block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]);
837  }
838 
839  for (j = 0; j < f->start_band; j++) {
840  block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
841  block->energy[j] = 0.0;
842  }
843  for (j = f->end_band; j < CELT_MAX_BANDS; j++) {
844  block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
845  block->energy[j] = 0.0;
846  }
847  }
848 
849  f->seed = rc->range;
850 
851  return 0;
852 }
853 
855 {
856  int i, j;
857 
858  if (f->flushed)
859  return;
860 
861  for (i = 0; i < 2; i++) {
862  CeltBlock *block = &f->block[i];
863 
864  for (j = 0; j < CELT_MAX_BANDS; j++)
865  block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE;
866 
867  memset(block->energy, 0, sizeof(block->energy));
868  memset(block->buf, 0, sizeof(block->buf));
869 
870  memset(block->pf_gains, 0, sizeof(block->pf_gains));
871  memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
872  memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));
873 
874  block->emph_coeff = 0.0;
875  }
876  f->seed = 0;
877 
878  f->flushed = 1;
879 }
880 
882 {
883  CeltFrame *frm = *f;
884  int i;
885 
886  if (!frm)
887  return;
888 
889  for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
890  ff_mdct15_uninit(&frm->imdct[i]);
891 
892  ff_celt_pvq_uninit(&frm->pvq);
893 
894  av_freep(&frm->dsp);
895  av_freep(f);
896 }
897 
898 int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
899  int apply_phase_inv)
900 {
901  CeltFrame *frm;
902  int i, ret;
903 
904  if (output_channels != 1 && output_channels != 2) {
905  av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
906  output_channels);
907  return AVERROR(EINVAL);
908  }
909 
910  frm = av_mallocz(sizeof(*frm));
911  if (!frm)
912  return AVERROR(ENOMEM);
913 
914  frm->avctx = avctx;
915  frm->output_channels = output_channels;
916  frm->apply_phase_inv = apply_phase_inv;
917 
918  for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
919  if ((ret = ff_mdct15_init(&frm->imdct[i], 1, i + 3, -1.0f/32768)) < 0)
920  goto fail;
921 
922  if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0)
923  goto fail;
924 
926  if (!frm->dsp) {
927  ret = AVERROR(ENOMEM);
928  goto fail;
929  }
930 
931  ff_celt_flush(frm);
932 
933  *f = frm;
934 
935  return 0;
936 fail:
937  ff_celt_free(&frm);
938  return ret;
939 }
int channels
Definition: opus_celt.h:99
int anticollapse
Definition: opus_celt.h:117
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, int channels, int frame_size, int start_band, int end_band)
Definition: opus_celt.c:679
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static float alpha(float a)
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int framebits
Definition: opus_celt.h:131
static void celt_postfilter(CeltFrame *f, CeltBlock *block)
Definition: opus_celt.c:568
const uint8_t ff_celt_coarse_energy_dist[4][2][42]
Definition: opustab.c:803
const uint8_t ff_celt_log_freq_range[]
Definition: opustab.c:771
float coeffs[CELT_MAX_FRAME_SIZE]
Definition: opus_celt.h:75
else temp
Definition: vf_mcdeint.c:256
int output_channels
Definition: opus_celt.h:100
channels
Definition: aptx.c:30
const uint8_t ff_celt_freq_bands[]
Definition: opustab.c:763
int av_log2(unsigned v)
Definition: intmath.c:26
float pf_gains_new[3]
Definition: opus_celt.h:83
RawBitsContext rb
Definition: opus_rc.h:42
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition: opus_rc.c:114
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode)
Definition: opus_pvq.c:897
const float ff_celt_postfilter_taps[3][3]
Definition: opustab.c:1093
static void celt_postfilter_apply(CeltBlock *block, float *data, int len)
Definition: opus_celt.c:537
#define N
Definition: af_mcompand.c:54
int pf_period_new
Definition: opus_celt.h:82
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
Definition: float_dsp.h:54
static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
Definition: opus_celt.c:478
static int16_t block[64]
Definition: dct.c:115
int fine_priority[CELT_MAX_BANDS]
Definition: opus_celt.h:136
CeltBlock block[2]
Definition: opus_celt.h:97
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:119
uint32_t total_bits
Definition: opus_rc.h:45
int flushed
Definition: opus_celt.h:120
uint8_t
#define CELT_OVERLAP
Definition: opus.h:42
const float * ff_celt_window
Definition: opustab.c:1130
int silence
Definition: opus_celt.h:115
#define CELT_VECTORS
Definition: opus_celt.h:36
static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
Definition: opus_celt.c:627
int64_t duration
Definition: movenc.c:63
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
Definition: mdct15.c:247
#define CELT_POSTFILTER_MINPERIOD
Definition: opus_celt.h:44
#define CELT_MAX_LOG_BLOCKS
Definition: opus.h:43
int dual_stereo
Definition: opus_celt.h:119
static const uint8_t bits2[81]
Definition: aactab.c:140
int coded_bands
Definition: opus_celt.h:106
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int end_band
Definition: opus_celt.h:105
uint32_t range
Definition: opus_rc.h:43
float pf_gains[3]
Definition: opus_celt.h:85
const uint8_t ff_celt_log2_frac[]
Definition: opustab.c:925
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* imdct_half)(struct MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
Definition: mdct15.h:52
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:127
#define AVERROR(e)
Definition: error.h:43
int start_band
Definition: opus_celt.h:104
#define CELT_EMPH_COEFF
Definition: opus_celt.h:43
const char * r
Definition: vf_curves.c:111
void ff_celt_flush(CeltFrame *f)
Definition: opus_celt.c:854
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1568
static void celt_decode_allocation(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus_celt.c:146
static void celt_decode_tf_changes(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus_celt.c:119
int tf_change[CELT_MAX_BANDS]
Definition: opus_celt.h:138
float emph_coeff
Definition: opus_celt.h:89
int pulses[CELT_MAX_BANDS]
Definition: opus_celt.h:137
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int apply_phase_inv
Definition: opus_celt.h:101
#define FFMAX(a, b)
Definition: common.h:94
int anticollapse_needed
Definition: opus_celt.h:116
#define fail()
Definition: checkasm.h:112
int fine_bits[CELT_MAX_BANDS]
Definition: opus_celt.h:135
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
Definition: opus_rc.c:90
AVCodecContext * avctx
Definition: opus_celt.h:94
uint32_t seed
Definition: opus_celt.h:121
const int8_t ff_celt_tf_select[4][2][2][2]
Definition: opustab.c:777
#define T(x)
Definition: vp56_arith.h:29
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:883
#define FFMIN(a, b)
Definition: common.h:96
#define CELT_SHORT_BLOCKSIZE
Definition: opus.h:41
uint32_t bytes
Definition: opus_rc.h:35
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
void ff_celt_free(CeltFrame **f)
Definition: opus_celt.c:881
int blocks
Definition: opus_celt.h:113
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:85
int transient
Definition: opus_celt.h:107
#define CELT_FINE_OFFSET
Definition: opus_celt.h:38
int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
Definition: opus_rc.c:275
const uint8_t ff_celt_freq_range[]
Definition: opustab.c:767
static void celt_decode_coarse_energy(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus_celt.c:34
static void celt_postfilter_apply_transition(CeltBlock *block, float *data)
Definition: opus_celt.c:492
#define FF_ARRAY_ELEMS(a)
const uint8_t ff_celt_static_caps[4][2][21]
Definition: opustab.c:861
#define exp2f(x)
Definition: libm.h:293
int frame_size
Definition: mxfenc.c:1947
static void celt_decode_fine_energy(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus_celt.c:78
#define CELT_MAX_BANDS
Definition: opus.h:45
const uint16_t ff_celt_model_spread[]
Definition: opustab.c:755
main external API structure.
Definition: avcodec.h:1488
const float ff_celt_window2[120]
Definition: opustab.c:1133
int pf_period_old
Definition: opus_celt.h:86
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame...
Definition: opus_rc.h:61
#define CELT_MAX_FINE_BITS
Definition: opus_celt.h:39
AVFloatDSPContext * dsp
Definition: opus_celt.h:96
#define CELT_ENERGY_SILENCE
Definition: opus_celt.h:45
const uint8_t ff_celt_static_alloc[11][21]
Definition: opustab.c:847
uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count)
CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise.
Definition: opus_rc.c:140
CeltPVQ * pvq
Definition: opus_celt.h:98
static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
Definition: opus_celt.h:148
float pf_gains_old[3]
Definition: opus_celt.h:87
const uint16_t ff_celt_model_energy_small[]
Definition: opustab.c:761
const uint16_t ff_celt_model_alloc_trim[]
Definition: opustab.c:757
#define M_SQRT2
Definition: mathematics.h:61
int remaining
Definition: opus_celt.h:132
float energy[CELT_MAX_BANDS]
Definition: opus_celt.h:66
const float ff_celt_beta_coef[]
Definition: opustab.c:799
if(ret< 0)
Definition: vf_mcdeint.c:279
static av_always_inline uint32_t celt_rng(CeltFrame *f)
Definition: opus_celt.h:142
#define CELT_ALLOC_STEPS
Definition: opus_celt.h:37
enum CeltSpread spread
Definition: opus_celt.h:122
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
Definition: mdct15.c:43
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int blocksize
Definition: opus_celt.h:114
int len
static void celt_decode_final_energy(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus_celt.c:97
const float ff_celt_alpha_coef[]
Definition: opustab.c:795
static int16_t block1[64]
Definition: dct.c:116
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition: opus_rc.c:182
const float ff_celt_mean_energy[]
Definition: opustab.c:787
#define av_freep(p)
static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
Definition: opus_celt.c:592
enum CeltBlockSize size
Definition: opus_celt.h:103
MDCT15Context * imdct[4]
Definition: opus_celt.h:95
int pf_period
Definition: opus_celt.h:84
float prev_energy[2][CELT_MAX_BANDS]
Definition: opus_celt.h:69
uint8_t collapse_masks[CELT_MAX_BANDS]
Definition: opus_celt.h:71
const uint16_t ff_celt_model_tapset[]
Definition: opustab.c:753
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition: opus_celt.c:898
void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
Definition: opus_pvq.c:914
int intensity_stereo
Definition: opus_celt.h:118
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus.c:443
static const uint8_t bits1[81]
Definition: aactab.c:117
float buf[2048]
Definition: opus_celt.h:74
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition: opus_rc.h:66
static uint8_t tmp[11]
Definition: aes_ctr.c:26