aacenc_quantization.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * AAC encoder quantizer
  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, float *energy, 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. float qenergy = 0;
  54. const int dim = BT_PAIR ? 2 : 4;
  55. int resbits = 0;
  56. int off;
  57. if (BT_ZERO || BT_NOISE || BT_STEREO) {
  58. for (i = 0; i < size; i++)
  59. cost += in[i]*in[i];
  60. if (bits)
  61. *bits = 0;
  62. if (energy)
  63. *energy = qenergy;
  64. if (out) {
  65. for (i = 0; i < size; i += dim)
  66. for (j = 0; j < dim; j++)
  67. out[i+j] = 0.0f;
  68. }
  69. return cost * lambda;
  70. }
  71. if (!scaled) {
  72. s->abs_pow34(s->scoefs, in, size);
  73. scaled = s->scoefs;
  74. }
  75. s->quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
  76. if (BT_UNSIGNED) {
  77. off = 0;
  78. } else {
  79. off = aac_cb_maxval[cb];
  80. }
  81. for (i = 0; i < size; i += dim) {
  82. const float *vec;
  83. int *quants = s->qcoefs + i;
  84. int curidx = 0;
  85. int curbits;
  86. float quantized, rd = 0.0f;
  87. for (j = 0; j < dim; j++) {
  88. curidx *= aac_cb_range[cb];
  89. curidx += quants[j] + off;
  90. }
  91. curbits = ff_aac_spectral_bits[cb-1][curidx];
  92. vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
  93. if (BT_UNSIGNED) {
  94. for (j = 0; j < dim; j++) {
  95. float t = fabsf(in[i+j]);
  96. float di;
  97. if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
  98. if (t >= CLIPPED_ESCAPE) {
  99. quantized = CLIPPED_ESCAPE;
  100. curbits += 21;
  101. } else {
  102. int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
  103. quantized = c*cbrtf(c)*IQ;
  104. curbits += av_log2(c)*2 - 4 + 1;
  105. }
  106. } else {
  107. quantized = vec[j]*IQ;
  108. }
  109. di = t - quantized;
  110. if (out)
  111. out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
  112. if (vec[j] != 0.0f)
  113. curbits++;
  114. qenergy += quantized*quantized;
  115. rd += di*di;
  116. }
  117. } else {
  118. for (j = 0; j < dim; j++) {
  119. quantized = vec[j]*IQ;
  120. qenergy += quantized*quantized;
  121. if (out)
  122. out[i+j] = quantized;
  123. rd += (in[i+j] - quantized)*(in[i+j] - quantized);
  124. }
  125. }
  126. cost += rd * lambda + curbits;
  127. resbits += curbits;
  128. if (cost >= uplim)
  129. return uplim;
  130. if (pb) {
  131. put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
  132. if (BT_UNSIGNED)
  133. for (j = 0; j < dim; j++)
  134. if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
  135. put_bits(pb, 1, in[i+j] < 0.0f);
  136. if (BT_ESC) {
  137. for (j = 0; j < 2; j++) {
  138. if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
  139. int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
  140. int len = av_log2(coef);
  141. put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
  142. put_sbits(pb, len, coef);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. if (bits)
  149. *bits = resbits;
  150. if (energy)
  151. *energy = qenergy;
  152. return cost;
  153. }
  154. static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
  155. const float *in, float *quant, const float *scaled,
  156. int size, int scale_idx, int cb,
  157. const float lambda, const float uplim,
  158. int *bits, float *energy) {
  159. av_assert0(0);
  160. return 0.0f;
  161. }
  162. #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
  163. static float quantize_and_encode_band_cost_ ## NAME( \
  164. struct AACEncContext *s, \
  165. PutBitContext *pb, const float *in, float *quant, \
  166. const float *scaled, int size, int scale_idx, \
  167. int cb, const float lambda, const float uplim, \
  168. int *bits, float *energy) { \
  169. return quantize_and_encode_band_cost_template( \
  170. s, pb, in, quant, scaled, size, scale_idx, \
  171. BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
  172. BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
  173. ROUNDING); \
  174. }
  175. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
  176. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
  177. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
  178. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
  179. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
  180. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
  181. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
  182. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
  183. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
  184. static float (*const quantize_and_encode_band_cost_arr[])(
  185. struct AACEncContext *s,
  186. PutBitContext *pb, const float *in, float *quant,
  187. const float *scaled, int size, int scale_idx,
  188. int cb, const float lambda, const float uplim,
  189. int *bits, float *energy) = {
  190. quantize_and_encode_band_cost_ZERO,
  191. quantize_and_encode_band_cost_SQUAD,
  192. quantize_and_encode_band_cost_SQUAD,
  193. quantize_and_encode_band_cost_UQUAD,
  194. quantize_and_encode_band_cost_UQUAD,
  195. quantize_and_encode_band_cost_SPAIR,
  196. quantize_and_encode_band_cost_SPAIR,
  197. quantize_and_encode_band_cost_UPAIR,
  198. quantize_and_encode_band_cost_UPAIR,
  199. quantize_and_encode_band_cost_UPAIR,
  200. quantize_and_encode_band_cost_UPAIR,
  201. quantize_and_encode_band_cost_ESC,
  202. quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
  203. quantize_and_encode_band_cost_NOISE,
  204. quantize_and_encode_band_cost_STEREO,
  205. quantize_and_encode_band_cost_STEREO,
  206. };
  207. static float (*const quantize_and_encode_band_cost_rtz_arr[])(
  208. struct AACEncContext *s,
  209. PutBitContext *pb, const float *in, float *quant,
  210. const float *scaled, int size, int scale_idx,
  211. int cb, const float lambda, const float uplim,
  212. int *bits, float *energy) = {
  213. quantize_and_encode_band_cost_ZERO,
  214. quantize_and_encode_band_cost_SQUAD,
  215. quantize_and_encode_band_cost_SQUAD,
  216. quantize_and_encode_band_cost_UQUAD,
  217. quantize_and_encode_band_cost_UQUAD,
  218. quantize_and_encode_band_cost_SPAIR,
  219. quantize_and_encode_band_cost_SPAIR,
  220. quantize_and_encode_band_cost_UPAIR,
  221. quantize_and_encode_band_cost_UPAIR,
  222. quantize_and_encode_band_cost_UPAIR,
  223. quantize_and_encode_band_cost_UPAIR,
  224. quantize_and_encode_band_cost_ESC_RTZ,
  225. quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
  226. quantize_and_encode_band_cost_NOISE,
  227. quantize_and_encode_band_cost_STEREO,
  228. quantize_and_encode_band_cost_STEREO,
  229. };
  230. #define quantize_and_encode_band_cost( \
  231. s, pb, in, quant, scaled, size, scale_idx, cb, \
  232. lambda, uplim, bits, energy, rtz) \
  233. ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
  234. s, pb, in, quant, scaled, size, scale_idx, cb, \
  235. lambda, uplim, bits, energy)
  236. static inline float quantize_band_cost(struct AACEncContext *s, const float *in,
  237. const float *scaled, int size, int scale_idx,
  238. int cb, const float lambda, const float uplim,
  239. int *bits, float *energy, int rtz)
  240. {
  241. return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
  242. cb, lambda, uplim, bits, energy, rtz);
  243. }
  244. static inline int quantize_band_cost_bits(struct AACEncContext *s, const float *in,
  245. const float *scaled, int size, int scale_idx,
  246. int cb, const float lambda, const float uplim,
  247. int *bits, float *energy, int rtz)
  248. {
  249. int auxbits;
  250. quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
  251. cb, 0.0f, uplim, &auxbits, energy, rtz);
  252. if (bits) {
  253. *bits = auxbits;
  254. }
  255. return auxbits;
  256. }
  257. static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
  258. const float *in, float *out, int size, int scale_idx,
  259. int cb, const float lambda, int rtz)
  260. {
  261. quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda,
  262. INFINITY, NULL, NULL, rtz);
  263. }
  264. #include "aacenc_quantization_misc.h"
  265. #endif /* AVCODEC_AACENC_QUANTIZATION_H */