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.

261 lines
11KB

  1. /*
  2. * AAC encoder intensity stereo
  3. * Copyright (C) 2015 Rostislav Pehlivanov
  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. * @file
  23. * AAC encoder quantizer
  24. * @author Rostislav Pehlivanov ( atomnuker gmail com )
  25. */
  26. #ifndef AVCODEC_AACENC_QUANTIZATION_H
  27. #define AVCODEC_AACENC_QUANTIZATION_H
  28. #include "aactab.h"
  29. #include "aacenc.h"
  30. #include "aacenctab.h"
  31. #include "aacenc_utils.h"
  32. /**
  33. * Calculate rate distortion cost for quantizing with given codebook
  34. *
  35. * @return quantization distortion
  36. */
  37. static av_always_inline float quantize_and_encode_band_cost_template(
  38. struct AACEncContext *s,
  39. PutBitContext *pb, const float *in, float *out,
  40. const float *scaled, int size, int scale_idx,
  41. int cb, const float lambda, const float uplim,
  42. int *bits, int BT_ZERO, int BT_UNSIGNED,
  43. int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
  44. const float ROUNDING)
  45. {
  46. const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
  47. const float Q = ff_aac_pow2sf_tab [q_idx];
  48. const float Q34 = ff_aac_pow34sf_tab[q_idx];
  49. const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
  50. const float CLIPPED_ESCAPE = 165140.0f*IQ;
  51. int i, j;
  52. float cost = 0;
  53. const int dim = BT_PAIR ? 2 : 4;
  54. int resbits = 0;
  55. int off;
  56. if (BT_ZERO || BT_NOISE || BT_STEREO) {
  57. for (i = 0; i < size; i++)
  58. cost += in[i]*in[i];
  59. if (bits)
  60. *bits = 0;
  61. if (out) {
  62. for (i = 0; i < size; i += dim)
  63. for (j = 0; j < dim; j++)
  64. out[i+j] = 0.0f;
  65. }
  66. return cost * lambda;
  67. }
  68. if (!scaled) {
  69. abs_pow34_v(s->scoefs, in, size);
  70. scaled = s->scoefs;
  71. }
  72. quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb], ROUNDING);
  73. if (BT_UNSIGNED) {
  74. off = 0;
  75. } else {
  76. off = aac_cb_maxval[cb];
  77. }
  78. for (i = 0; i < size; i += dim) {
  79. const float *vec;
  80. int *quants = s->qcoefs + i;
  81. int curidx = 0;
  82. int curbits;
  83. float quantized, rd = 0.0f;
  84. for (j = 0; j < dim; j++) {
  85. curidx *= aac_cb_range[cb];
  86. curidx += quants[j] + off;
  87. }
  88. curbits = ff_aac_spectral_bits[cb-1][curidx];
  89. vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
  90. if (BT_UNSIGNED) {
  91. for (j = 0; j < dim; j++) {
  92. float t = fabsf(in[i+j]);
  93. float di;
  94. if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
  95. if (t >= CLIPPED_ESCAPE) {
  96. quantized = CLIPPED_ESCAPE;
  97. curbits += 21;
  98. } else {
  99. int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
  100. quantized = c*cbrtf(c)*IQ;
  101. curbits += av_log2(c)*2 - 4 + 1;
  102. }
  103. } else {
  104. quantized = vec[j]*IQ;
  105. }
  106. di = t - quantized;
  107. if (out)
  108. out[i+j] = copysignf(quantized, in[i+j]);
  109. if (vec[j] != 0.0f)
  110. curbits++;
  111. rd += di*di;
  112. }
  113. } else {
  114. for (j = 0; j < dim; j++) {
  115. quantized = vec[j]*IQ;
  116. if (out)
  117. out[i+j] = quantized;
  118. rd += (in[i+j] - quantized)*(in[i+j] - quantized);
  119. }
  120. }
  121. cost += rd * lambda + curbits;
  122. resbits += curbits;
  123. if (cost >= uplim)
  124. return uplim;
  125. if (pb) {
  126. put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
  127. if (BT_UNSIGNED)
  128. for (j = 0; j < dim; j++)
  129. if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
  130. put_bits(pb, 1, in[i+j] < 0.0f);
  131. if (BT_ESC) {
  132. for (j = 0; j < 2; j++) {
  133. if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
  134. int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
  135. int len = av_log2(coef);
  136. put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
  137. put_sbits(pb, len, coef);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. if (bits)
  144. *bits = resbits;
  145. return cost;
  146. }
  147. static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
  148. const float *in, float *quant, const float *scaled,
  149. int size, int scale_idx, int cb,
  150. const float lambda, const float uplim,
  151. int *bits) {
  152. av_assert0(0);
  153. return 0.0f;
  154. }
  155. #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
  156. static float quantize_and_encode_band_cost_ ## NAME( \
  157. struct AACEncContext *s, \
  158. PutBitContext *pb, const float *in, float *quant, \
  159. const float *scaled, int size, int scale_idx, \
  160. int cb, const float lambda, const float uplim, \
  161. int *bits) { \
  162. return quantize_and_encode_band_cost_template( \
  163. s, pb, in, quant, scaled, size, scale_idx, \
  164. BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
  165. BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
  166. ROUNDING); \
  167. }
  168. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
  169. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
  170. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
  171. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
  172. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
  173. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
  174. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
  175. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
  176. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
  177. static float (*const quantize_and_encode_band_cost_arr[])(
  178. struct AACEncContext *s,
  179. PutBitContext *pb, const float *in, float *quant,
  180. const float *scaled, int size, int scale_idx,
  181. int cb, const float lambda, const float uplim,
  182. int *bits) = {
  183. quantize_and_encode_band_cost_ZERO,
  184. quantize_and_encode_band_cost_SQUAD,
  185. quantize_and_encode_band_cost_SQUAD,
  186. quantize_and_encode_band_cost_UQUAD,
  187. quantize_and_encode_band_cost_UQUAD,
  188. quantize_and_encode_band_cost_SPAIR,
  189. quantize_and_encode_band_cost_SPAIR,
  190. quantize_and_encode_band_cost_UPAIR,
  191. quantize_and_encode_band_cost_UPAIR,
  192. quantize_and_encode_band_cost_UPAIR,
  193. quantize_and_encode_band_cost_UPAIR,
  194. quantize_and_encode_band_cost_ESC,
  195. quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
  196. quantize_and_encode_band_cost_NOISE,
  197. quantize_and_encode_band_cost_STEREO,
  198. quantize_and_encode_band_cost_STEREO,
  199. };
  200. static float (*const quantize_and_encode_band_cost_rtz_arr[])(
  201. struct AACEncContext *s,
  202. PutBitContext *pb, const float *in, float *quant,
  203. const float *scaled, int size, int scale_idx,
  204. int cb, const float lambda, const float uplim,
  205. int *bits) = {
  206. quantize_and_encode_band_cost_ZERO,
  207. quantize_and_encode_band_cost_SQUAD,
  208. quantize_and_encode_band_cost_SQUAD,
  209. quantize_and_encode_band_cost_UQUAD,
  210. quantize_and_encode_band_cost_UQUAD,
  211. quantize_and_encode_band_cost_SPAIR,
  212. quantize_and_encode_band_cost_SPAIR,
  213. quantize_and_encode_band_cost_UPAIR,
  214. quantize_and_encode_band_cost_UPAIR,
  215. quantize_and_encode_band_cost_UPAIR,
  216. quantize_and_encode_band_cost_UPAIR,
  217. quantize_and_encode_band_cost_ESC_RTZ,
  218. quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
  219. quantize_and_encode_band_cost_NOISE,
  220. quantize_and_encode_band_cost_STEREO,
  221. quantize_and_encode_band_cost_STEREO,
  222. };
  223. #define quantize_and_encode_band_cost( \
  224. s, pb, in, quant, scaled, size, scale_idx, cb, \
  225. lambda, uplim, bits, rtz) \
  226. ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
  227. s, pb, in, quant, scaled, size, scale_idx, cb, \
  228. lambda, uplim, bits)
  229. static inline float quantize_band_cost(struct AACEncContext *s, const float *in,
  230. const float *scaled, int size, int scale_idx,
  231. int cb, const float lambda, const float uplim,
  232. int *bits, int rtz)
  233. {
  234. return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
  235. cb, lambda, uplim, bits, rtz);
  236. }
  237. static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
  238. const float *in, float *out, int size, int scale_idx,
  239. int cb, const float lambda, int rtz)
  240. {
  241. quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda,
  242. INFINITY, NULL, rtz);
  243. }
  244. #endif /* AVCODEC_AACENC_QUANTIZATION_H */