opus_celt.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Opus decoder/demuxer common functions
  3. * Copyright (c) 2012 Andrew D'Addesio
  4. * Copyright (c) 2013-2014 Mozilla Corporation
  5. * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #ifndef AVCODEC_OPUS_CELT_H
  24. #define AVCODEC_OPUS_CELT_H
  25. #include <float.h>
  26. #include "opus.h"
  27. #include "opus_pvq.h"
  28. #include "opusdsp.h"
  29. #include "mdct15.h"
  30. #include "libavutil/float_dsp.h"
  31. #include "libavutil/libm.h"
  32. #define CELT_VECTORS 11
  33. #define CELT_ALLOC_STEPS 6
  34. #define CELT_FINE_OFFSET 21
  35. #define CELT_MAX_FINE_BITS 8
  36. #define CELT_NORM_SCALE 16384
  37. #define CELT_QTHETA_OFFSET 4
  38. #define CELT_QTHETA_OFFSET_TWOPHASE 16
  39. #define CELT_POSTFILTER_MINPERIOD 15
  40. #define CELT_ENERGY_SILENCE (-28.0f)
  41. typedef struct CeltPVQ CeltPVQ;
  42. enum CeltSpread {
  43. CELT_SPREAD_NONE,
  44. CELT_SPREAD_LIGHT,
  45. CELT_SPREAD_NORMAL,
  46. CELT_SPREAD_AGGRESSIVE
  47. };
  48. enum CeltBlockSize {
  49. CELT_BLOCK_120,
  50. CELT_BLOCK_240,
  51. CELT_BLOCK_480,
  52. CELT_BLOCK_960,
  53. CELT_BLOCK_NB
  54. };
  55. typedef struct CeltBlock {
  56. float energy[CELT_MAX_BANDS];
  57. float lin_energy[CELT_MAX_BANDS];
  58. float error_energy[CELT_MAX_BANDS];
  59. float prev_energy[2][CELT_MAX_BANDS];
  60. uint8_t collapse_masks[CELT_MAX_BANDS];
  61. /* buffer for mdct output + postfilter */
  62. DECLARE_ALIGNED(32, float, buf)[2048];
  63. DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE];
  64. /* Used by the encoder */
  65. DECLARE_ALIGNED(32, float, overlap)[FFALIGN(CELT_OVERLAP, 16)];
  66. DECLARE_ALIGNED(32, float, samples)[FFALIGN(CELT_MAX_FRAME_SIZE, 16)];
  67. /* postfilter parameters */
  68. int pf_period_new;
  69. float pf_gains_new[3];
  70. int pf_period;
  71. float pf_gains[3];
  72. int pf_period_old;
  73. float pf_gains_old[3];
  74. float emph_coeff;
  75. } CeltBlock;
  76. struct CeltFrame {
  77. // constant values that do not change during context lifetime
  78. AVCodecContext *avctx;
  79. MDCT15Context *imdct[4];
  80. AVFloatDSPContext *dsp;
  81. CeltBlock block[2];
  82. CeltPVQ *pvq;
  83. OpusDSP opusdsp;
  84. int channels;
  85. int output_channels;
  86. int apply_phase_inv;
  87. enum CeltBlockSize size;
  88. int start_band;
  89. int end_band;
  90. int coded_bands;
  91. int transient;
  92. int pfilter;
  93. int skip_band_floor;
  94. int tf_select;
  95. int alloc_trim;
  96. int alloc_boost[CELT_MAX_BANDS];
  97. int blocks; /* number of iMDCT blocks in the frame, depends on transient */
  98. int blocksize; /* size of each block */
  99. int silence; /* Frame is filled with silence */
  100. int anticollapse_needed; /* Whether to expect an anticollapse bit */
  101. int anticollapse; /* Encoded anticollapse bit */
  102. int intensity_stereo;
  103. int dual_stereo;
  104. int flushed;
  105. uint32_t seed;
  106. enum CeltSpread spread;
  107. /* Encoder PF coeffs */
  108. int pf_octave;
  109. int pf_period;
  110. int pf_tapset;
  111. float pf_gain;
  112. /* Bit allocation */
  113. int framebits;
  114. int remaining;
  115. int remaining2;
  116. int caps [CELT_MAX_BANDS];
  117. int fine_bits [CELT_MAX_BANDS];
  118. int fine_priority[CELT_MAX_BANDS];
  119. int pulses [CELT_MAX_BANDS];
  120. int tf_change [CELT_MAX_BANDS];
  121. };
  122. /* LCG for noise generation */
  123. static av_always_inline uint32_t celt_rng(CeltFrame *f)
  124. {
  125. f->seed = 1664525 * f->seed + 1013904223;
  126. return f->seed;
  127. }
  128. static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
  129. {
  130. int i;
  131. float g = 1e-15f;
  132. for (i = 0; i < N; i++)
  133. g += X[i] * X[i];
  134. g = gain / sqrtf(g);
  135. for (i = 0; i < N; i++)
  136. X[i] *= g;
  137. }
  138. int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
  139. int apply_phase_inv);
  140. void ff_celt_free(CeltFrame **f);
  141. void ff_celt_flush(CeltFrame *f);
  142. int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output,
  143. int coded_channels, int frame_size, int startband, int endband);
  144. #endif /* AVCODEC_OPUS_CELT_H */