You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.5KB

  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 "mdct15.h"
  28. #include "libavutil/float_dsp.h"
  29. #include "libavutil/libm.h"
  30. #define CELT_VECTORS 11
  31. #define CELT_ALLOC_STEPS 6
  32. #define CELT_FINE_OFFSET 21
  33. #define CELT_MAX_FINE_BITS 8
  34. #define CELT_NORM_SCALE 16384
  35. #define CELT_QTHETA_OFFSET 4
  36. #define CELT_QTHETA_OFFSET_TWOPHASE 16
  37. #define CELT_EMPH_COEFF 0.85000610f
  38. #define CELT_POSTFILTER_MINPERIOD 15
  39. #define CELT_ENERGY_SILENCE (-28.0f)
  40. enum CeltSpread {
  41. CELT_SPREAD_NONE,
  42. CELT_SPREAD_LIGHT,
  43. CELT_SPREAD_NORMAL,
  44. CELT_SPREAD_AGGRESSIVE
  45. };
  46. enum CeltBlockSize {
  47. CELT_BLOCK_120,
  48. CELT_BLOCK_240,
  49. CELT_BLOCK_480,
  50. CELT_BLOCK_960,
  51. CELT_BLOCK_NB
  52. };
  53. typedef struct CeltBlock {
  54. float energy[CELT_MAX_BANDS];
  55. float lin_energy[CELT_MAX_BANDS];
  56. float error_energy[CELT_MAX_BANDS];
  57. float prev_energy[2][CELT_MAX_BANDS];
  58. uint8_t collapse_masks[CELT_MAX_BANDS];
  59. /* buffer for mdct output + postfilter */
  60. DECLARE_ALIGNED(32, float, buf)[2048];
  61. DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE];
  62. /* Used by the encoder */
  63. DECLARE_ALIGNED(32, float, overlap)[120];
  64. DECLARE_ALIGNED(32, float, samples)[CELT_MAX_FRAME_SIZE];
  65. /* postfilter parameters */
  66. int pf_period_new;
  67. float pf_gains_new[3];
  68. int pf_period;
  69. float pf_gains[3];
  70. int pf_period_old;
  71. float pf_gains_old[3];
  72. float emph_coeff;
  73. } CeltBlock;
  74. struct CeltFrame {
  75. // constant values that do not change during context lifetime
  76. AVCodecContext *avctx;
  77. MDCT15Context *imdct[4];
  78. AVFloatDSPContext *dsp;
  79. CeltBlock block[2];
  80. int channels;
  81. int output_channels;
  82. enum CeltBlockSize size;
  83. int start_band;
  84. int end_band;
  85. int coded_bands;
  86. int transient;
  87. int pfilter;
  88. int skip_band_floor;
  89. int tf_select;
  90. int alloc_trim;
  91. int alloc_boost[CELT_MAX_BANDS];
  92. int blocks; /* number of iMDCT blocks in the frame, depends on transient */
  93. int blocksize; /* size of each block */
  94. int silence; /* Frame is filled with silence */
  95. int anticollapse_needed; /* Whether to expect an anticollapse bit */
  96. int anticollapse; /* Encoded anticollapse bit */
  97. int intensity_stereo;
  98. int dual_stereo;
  99. int flushed;
  100. uint32_t seed;
  101. enum CeltSpread spread;
  102. /* Bit allocation */
  103. int framebits;
  104. int remaining;
  105. int remaining2;
  106. int caps [CELT_MAX_BANDS];
  107. int fine_bits [CELT_MAX_BANDS];
  108. int fine_priority[CELT_MAX_BANDS];
  109. int pulses [CELT_MAX_BANDS];
  110. int tf_change [CELT_MAX_BANDS];
  111. DECLARE_ALIGNED(32, float, scratch)[22 * 8]; // MAX(ff_celt_freq_range) * 1<<CELT_MAX_LOG_BLOCKS
  112. };
  113. /* LCG for noise generation */
  114. static av_always_inline uint32_t celt_rng(CeltFrame *f)
  115. {
  116. f->seed = 1664525 * f->seed + 1013904223;
  117. return f->seed;
  118. }
  119. static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
  120. {
  121. int i;
  122. float g = 1e-15f;
  123. for (i = 0; i < N; i++)
  124. g += X[i] * X[i];
  125. g = gain / sqrtf(g);
  126. for (i = 0; i < N; i++)
  127. X[i] *= g;
  128. }
  129. int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels);
  130. void ff_celt_free(CeltFrame **f);
  131. void ff_celt_flush(CeltFrame *f);
  132. int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output,
  133. int coded_channels, int frame_size, int startband, int endband);
  134. #endif /* AVCODEC_OPUS_CELT_H */