opus_celt.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "mdct15.h"
  29. #include "libavutil/float_dsp.h"
  30. #include "libavutil/libm.h"
  31. #define CELT_VECTORS 11
  32. #define CELT_ALLOC_STEPS 6
  33. #define CELT_FINE_OFFSET 21
  34. #define CELT_MAX_FINE_BITS 8
  35. #define CELT_NORM_SCALE 16384
  36. #define CELT_QTHETA_OFFSET 4
  37. #define CELT_QTHETA_OFFSET_TWOPHASE 16
  38. #define CELT_EMPH_COEFF 0.85000610f
  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. int channels;
  84. int output_channels;
  85. int apply_phase_inv;
  86. enum CeltBlockSize size;
  87. int start_band;
  88. int end_band;
  89. int coded_bands;
  90. int transient;
  91. int pfilter;
  92. int skip_band_floor;
  93. int tf_select;
  94. int alloc_trim;
  95. int alloc_boost[CELT_MAX_BANDS];
  96. int blocks; /* number of iMDCT blocks in the frame, depends on transient */
  97. int blocksize; /* size of each block */
  98. int silence; /* Frame is filled with silence */
  99. int anticollapse_needed; /* Whether to expect an anticollapse bit */
  100. int anticollapse; /* Encoded anticollapse bit */
  101. int intensity_stereo;
  102. int dual_stereo;
  103. int flushed;
  104. uint32_t seed;
  105. enum CeltSpread spread;
  106. /* Encoder PF coeffs */
  107. int pf_octave;
  108. int pf_period;
  109. int pf_tapset;
  110. float pf_gain;
  111. /* Bit allocation */
  112. int framebits;
  113. int remaining;
  114. int remaining2;
  115. int caps [CELT_MAX_BANDS];
  116. int fine_bits [CELT_MAX_BANDS];
  117. int fine_priority[CELT_MAX_BANDS];
  118. int pulses [CELT_MAX_BANDS];
  119. int tf_change [CELT_MAX_BANDS];
  120. };
  121. /* LCG for noise generation */
  122. static av_always_inline uint32_t celt_rng(CeltFrame *f)
  123. {
  124. f->seed = 1664525 * f->seed + 1013904223;
  125. return f->seed;
  126. }
  127. static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
  128. {
  129. int i;
  130. float g = 1e-15f;
  131. for (i = 0; i < N; i++)
  132. g += X[i] * X[i];
  133. g = gain / sqrtf(g);
  134. for (i = 0; i < N; i++)
  135. X[i] *= g;
  136. }
  137. int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
  138. int apply_phase_inv);
  139. void ff_celt_free(CeltFrame **f);
  140. void ff_celt_flush(CeltFrame *f);
  141. int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output,
  142. int coded_channels, int frame_size, int startband, int endband);
  143. #endif /* AVCODEC_OPUS_CELT_H */