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.

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