patch-libavcodec_aacenc_utils_h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. $OpenBSD: patch-libavcodec_aacenc_utils_h,v 1.11 2016/10/17 07:51:00 ajacoutot Exp $
  2. aacenc: copy PRNG from the decoder
  3. AAC encoder: simplify and speed up find_min_book
  4. AAC encoder: Extensive improvements
  5. aacenc_utils: add 'inline' flag to find_form_factor, silence warning
  6. aacenc: add support for changing options based on a profile
  7. aacenc_utils: fit find_form_factor() below 80 chars per line
  8. aacenc: partially revert previous commits to set options via a profile
  9. avcodec/aac_tablegen: get rid of hardcoded tables entirely
  10. AAC encoder: improve SF range utilization
  11. aacenc: switch to using the RNG from libavutil
  12. avcodec/aacenc_is: replace pow(x, 0.75) by x/sqrtf(sqrtf(x))
  13. lavc/aacenc_utils: replace sqrtf(Q*sqrtf(Q)) by precomputed value
  14. aacenc: avoid double in quantize_bands.
  15. aacenc_utils: Use temporary variable.
  16. lavc/aacenc_utils: replace powf(x,y) by expf(logf(x), y)
  17. aacenc: use the decoder's lcg PRNG
  18. --- libavcodec/aacenc_utils.h.orig Sat Aug 27 22:51:19 2016
  19. +++ libavcodec/aacenc_utils.h Sat Oct 15 18:51:24 2016
  20. @@ -28,9 +28,10 @@
  21. #ifndef AVCODEC_AACENC_UTILS_H
  22. #define AVCODEC_AACENC_UTILS_H
  23. +#include "libavutil/internal.h"
  24. #include "aac.h"
  25. -#include "aac_tablegen_decl.h"
  26. #include "aacenctab.h"
  27. +#include "aactab.h"
  28. #define ROUND_STANDARD 0.4054f
  29. #define ROUND_TO_ZERO 0.1054f
  30. @@ -45,6 +46,11 @@ static inline void abs_pow34_v(float *out, const float
  31. }
  32. }
  33. +static inline float pos_pow34(float a)
  34. +{
  35. + return sqrtf(a * sqrtf(a));
  36. +}
  37. +
  38. /**
  39. * Quantize one coefficient.
  40. * @return absolute value of the quantized coefficient
  41. @@ -61,13 +67,13 @@ static inline void quantize_bands(int *out, const floa
  42. const float rounding)
  43. {
  44. int i;
  45. - double qc;
  46. for (i = 0; i < size; i++) {
  47. - qc = scaled[i] * Q34;
  48. - out[i] = (int)FFMIN(qc + rounding, (double)maxval);
  49. + float qc = scaled[i] * Q34;
  50. + int tmp = (int)FFMIN(qc + rounding, (float)maxval);
  51. if (is_signed && in[i] < 0.0f) {
  52. - out[i] = -out[i];
  53. + tmp = -tmp;
  54. }
  55. + out[i] = tmp;
  56. }
  57. }
  58. @@ -85,20 +91,68 @@ static inline float find_max_val(int group_len, int sw
  59. static inline int find_min_book(float maxval, int sf)
  60. {
  61. - float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
  62. - float Q34 = sqrtf(Q * sqrtf(Q));
  63. + float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
  64. int qmaxval, cb;
  65. qmaxval = maxval * Q34 + C_QUANT;
  66. - if (qmaxval == 0) cb = 0;
  67. - else if (qmaxval == 1) cb = 1;
  68. - else if (qmaxval == 2) cb = 3;
  69. - else if (qmaxval <= 4) cb = 5;
  70. - else if (qmaxval <= 7) cb = 7;
  71. - else if (qmaxval <= 12) cb = 9;
  72. - else cb = 11;
  73. + if (qmaxval >= (FF_ARRAY_ELEMS(aac_maxval_cb)))
  74. + cb = 11;
  75. + else
  76. + cb = aac_maxval_cb[qmaxval];
  77. return cb;
  78. }
  79. +static inline float find_form_factor(int group_len, int swb_size, float thresh,
  80. + const float *scaled, float nzslope) {
  81. + const float iswb_size = 1.0f / swb_size;
  82. + const float iswb_sizem1 = 1.0f / (swb_size - 1);
  83. + const float ethresh = thresh;
  84. + float form = 0.0f, weight = 0.0f;
  85. + int w2, i;
  86. + for (w2 = 0; w2 < group_len; w2++) {
  87. + float e = 0.0f, e2 = 0.0f, var = 0.0f, maxval = 0.0f;
  88. + float nzl = 0;
  89. + for (i = 0; i < swb_size; i++) {
  90. + float s = fabsf(scaled[w2*128+i]);
  91. + maxval = FFMAX(maxval, s);
  92. + e += s;
  93. + e2 += s *= s;
  94. + /* We really don't want a hard non-zero-line count, since
  95. + * even below-threshold lines do add up towards band spectral power.
  96. + * So, fall steeply towards zero, but smoothly
  97. + */
  98. + if (s >= ethresh) {
  99. + nzl += 1.0f;
  100. + } else {
  101. + if (nzslope == 2.f)
  102. + nzl += (s / ethresh) * (s / ethresh);
  103. + else
  104. + nzl += ff_fast_powf(s / ethresh, nzslope);
  105. + }
  106. + }
  107. + if (e2 > thresh) {
  108. + float frm;
  109. + e *= iswb_size;
  110. +
  111. + /** compute variance */
  112. + for (i = 0; i < swb_size; i++) {
  113. + float d = fabsf(scaled[w2*128+i]) - e;
  114. + var += d*d;
  115. + }
  116. + var = sqrtf(var * iswb_sizem1);
  117. +
  118. + e2 *= iswb_size;
  119. + frm = e / FFMIN(e+4*var,maxval);
  120. + form += e2 * sqrtf(frm) / FFMAX(0.5f,nzl);
  121. + weight += e2;
  122. + }
  123. + }
  124. + if (weight > 0) {
  125. + return form / weight;
  126. + } else {
  127. + return 1.0f;
  128. + }
  129. +}
  130. +
  131. /** Return the minimum scalefactor where the quantized coef does not clip. */
  132. static inline uint8_t coef2minsf(float coef)
  133. {
  134. @@ -128,6 +182,89 @@ static inline int quant_array_idx(const float val, con
  135. return index;
  136. }
  137. +/**
  138. + * approximates exp10f(-3.0f*(0.5f + 0.5f * cosf(FFMIN(b,15.5f) / 15.5f)))
  139. + */
  140. +static av_always_inline float bval2bmax(float b)
  141. +{
  142. + return 0.001f + 0.0035f * (b*b*b) / (15.5f*15.5f*15.5f);
  143. +}
  144. +
  145. +/*
  146. + * Compute a nextband map to be used with SF delta constraint utilities.
  147. + * The nextband array should contain 128 elements, and positions that don't
  148. + * map to valid, nonzero bands of the form w*16+g (with w being the initial
  149. + * window of the window group, only) are left indetermined.
  150. + */
  151. +static inline void ff_init_nextband_map(const SingleChannelElement *sce, uint8_t *nextband)
  152. +{
  153. + unsigned char prevband = 0;
  154. + int w, g;
  155. + /** Just a safe default */
  156. + for (g = 0; g < 128; g++)
  157. + nextband[g] = g;
  158. +
  159. + /** Now really navigate the nonzero band chain */
  160. + for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  161. + for (g = 0; g < sce->ics.num_swb; g++) {
  162. + if (!sce->zeroes[w*16+g] && sce->band_type[w*16+g] < RESERVED_BT)
  163. + prevband = nextband[prevband] = w*16+g;
  164. + }
  165. + }
  166. + nextband[prevband] = prevband; /* terminate */
  167. +}
  168. +
  169. +/*
  170. + * Updates nextband to reflect a removed band (equivalent to
  171. + * calling ff_init_nextband_map after marking a band as zero)
  172. + */
  173. +static inline void ff_nextband_remove(uint8_t *nextband, int prevband, int band)
  174. +{
  175. + nextband[prevband] = nextband[band];
  176. +}
  177. +
  178. +/*
  179. + * Checks whether the specified band could be removed without inducing
  180. + * scalefactor delta that violates SF delta encoding constraints.
  181. + * prev_sf has to be the scalefactor of the previous nonzero, nonspecial
  182. + * band, in encoding order, or negative if there was no such band.
  183. + */
  184. +static inline int ff_sfdelta_can_remove_band(const SingleChannelElement *sce,
  185. + const uint8_t *nextband, int prev_sf, int band)
  186. +{
  187. + return prev_sf >= 0
  188. + && sce->sf_idx[nextband[band]] >= (prev_sf - SCALE_MAX_DIFF)
  189. + && sce->sf_idx[nextband[band]] <= (prev_sf + SCALE_MAX_DIFF);
  190. +}
  191. +
  192. +/*
  193. + * Checks whether the specified band's scalefactor could be replaced
  194. + * with another one without violating SF delta encoding constraints.
  195. + * prev_sf has to be the scalefactor of the previous nonzero, nonsepcial
  196. + * band, in encoding order, or negative if there was no such band.
  197. + */
  198. +static inline int ff_sfdelta_can_replace(const SingleChannelElement *sce,
  199. + const uint8_t *nextband, int prev_sf, int new_sf, int band)
  200. +{
  201. + return new_sf >= (prev_sf - SCALE_MAX_DIFF)
  202. + && new_sf <= (prev_sf + SCALE_MAX_DIFF)
  203. + && sce->sf_idx[nextband[band]] >= (new_sf - SCALE_MAX_DIFF)
  204. + && sce->sf_idx[nextband[band]] <= (new_sf + SCALE_MAX_DIFF);
  205. +}
  206. +
  207. +/**
  208. + * linear congruential pseudorandom number generator
  209. + *
  210. + * @param previous_val pointer to the current state of the generator
  211. + *
  212. + * @return Returns a 32-bit pseudorandom integer
  213. + */
  214. +static av_always_inline int lcg_random(unsigned previous_val)
  215. +{
  216. + union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
  217. + return v.s;
  218. +}
  219. +
  220. #define ERROR_IF(cond, ...) \
  221. if (cond) { \
  222. av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
  223. @@ -138,6 +275,5 @@ static inline int quant_array_idx(const float val, con
  224. if (cond) { \
  225. av_log(avctx, AV_LOG_WARNING, __VA_ARGS__); \
  226. }
  227. -
  228. #endif /* AVCODEC_AACENC_UTILS_H */