avcodec/aacdec_fixed: fix invalid shift in predict()
[ffmpeg.git] / libavcodec / aacenc_utils.h
index ea3152a..bef4c10 100644 (file)
 #ifndef AVCODEC_AACENC_UTILS_H
 #define AVCODEC_AACENC_UTILS_H
 
+#include "libavutil/ffmath.h"
 #include "aac.h"
-#include "aac_tablegen_decl.h"
 #include "aacenctab.h"
+#include "aactab.h"
 
 #define ROUND_STANDARD 0.4054f
 #define ROUND_TO_ZERO 0.1054f
@@ -45,6 +46,11 @@ static inline void abs_pow34_v(float *out, const float *in, const int size)
     }
 }
 
+static inline float pos_pow34(float a)
+{
+    return sqrtf(a * sqrtf(a));
+}
+
 /**
  * Quantize one coefficient.
  * @return absolute value of the quantized coefficient
@@ -57,17 +63,17 @@ static inline int quant(float coef, const float Q, const float rounding)
 }
 
 static inline void quantize_bands(int *out, const float *in, const float *scaled,
-                                  int size, float Q34, int is_signed, int maxval,
+                                  int size, int is_signed, int maxval, const float Q34,
                                   const float rounding)
 {
     int i;
-    double qc;
     for (i = 0; i < size; i++) {
-        qc = scaled[i] * Q34;
-        out[i] = (int)FFMIN(qc + rounding, (double)maxval);
+        float qc = scaled[i] * Q34;
+        int tmp = (int)FFMIN(qc + rounding, (float)maxval);
         if (is_signed && in[i] < 0.0f) {
-            out[i] = -out[i];
+            tmp = -tmp;
         }
+        out[i] = tmp;
     }
 }
 
@@ -85,8 +91,7 @@ static inline float find_max_val(int group_len, int swb_size, const float *scale
 
 static inline int find_min_book(float maxval, int sf)
 {
-    float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
-    float Q34 = sqrtf(Q * sqrtf(Q));
+    float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
     int qmaxval, cb;
     qmaxval = maxval * Q34 + C_QUANT;
     if (qmaxval >= (FF_ARRAY_ELEMS(aac_maxval_cb)))
@@ -118,7 +123,10 @@ static inline float find_form_factor(int group_len, int swb_size, float thresh,
             if (s >= ethresh) {
                 nzl += 1.0f;
             } else {
-                nzl += powf(s / ethresh, nzslope);
+                if (nzslope == 2.f)
+                    nzl += (s / ethresh) * (s / ethresh);
+                else
+                    nzl += ff_fast_powf(s / ethresh, nzslope);
             }
         }
         if (e2 > thresh) {
@@ -183,9 +191,75 @@ static av_always_inline float bval2bmax(float b)
 }
 
 /*
- * linear congruential pseudorandom number generator, copied from the decoder
+ * Compute a nextband map to be used with SF delta constraint utilities.
+ * The nextband array should contain 128 elements, and positions that don't
+ * map to valid, nonzero bands of the form w*16+g (with w being the initial
+ * window of the window group, only) are left indetermined.
  */
-static inline int lcg_random(unsigned previous_val)
+static inline void ff_init_nextband_map(const SingleChannelElement *sce, uint8_t *nextband)
+{
+    unsigned char prevband = 0;
+    int w, g;
+    /** Just a safe default */
+    for (g = 0; g < 128; g++)
+        nextband[g] = g;
+
+    /** Now really navigate the nonzero band chain */
+    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+        for (g = 0; g < sce->ics.num_swb; g++) {
+            if (!sce->zeroes[w*16+g] && sce->band_type[w*16+g] < RESERVED_BT)
+                prevband = nextband[prevband] = w*16+g;
+        }
+    }
+    nextband[prevband] = prevband; /* terminate */
+}
+
+/*
+ * Updates nextband to reflect a removed band (equivalent to
+ * calling ff_init_nextband_map after marking a band as zero)
+ */
+static inline void ff_nextband_remove(uint8_t *nextband, int prevband, int band)
+{
+    nextband[prevband] = nextband[band];
+}
+
+/*
+ * Checks whether the specified band could be removed without inducing
+ * scalefactor delta that violates SF delta encoding constraints.
+ * prev_sf has to be the scalefactor of the previous nonzero, nonspecial
+ * band, in encoding order, or negative if there was no such band.
+ */
+static inline int ff_sfdelta_can_remove_band(const SingleChannelElement *sce,
+    const uint8_t *nextband, int prev_sf, int band)
+{
+    return prev_sf >= 0
+        && sce->sf_idx[nextband[band]] >= (prev_sf - SCALE_MAX_DIFF)
+        && sce->sf_idx[nextband[band]] <= (prev_sf + SCALE_MAX_DIFF);
+}
+
+/*
+ * Checks whether the specified band's scalefactor could be replaced
+ * with another one without violating SF delta encoding constraints.
+ * prev_sf has to be the scalefactor of the previous nonzero, nonsepcial
+ * band, in encoding order, or negative if there was no such band.
+ */
+static inline int ff_sfdelta_can_replace(const SingleChannelElement *sce,
+    const uint8_t *nextband, int prev_sf, int new_sf, int band)
+{
+    return new_sf >= (prev_sf - SCALE_MAX_DIFF)
+        && new_sf <= (prev_sf + SCALE_MAX_DIFF)
+        && sce->sf_idx[nextband[band]] >= (new_sf - SCALE_MAX_DIFF)
+        && sce->sf_idx[nextband[band]] <= (new_sf + SCALE_MAX_DIFF);
+}
+
+/**
+ * linear congruential pseudorandom number generator
+ *
+ * @param   previous_val    pointer to the current state of the generator
+ *
+ * @return  Returns a 32-bit pseudorandom integer
+ */
+static av_always_inline int lcg_random(unsigned previous_val)
 {
     union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
     return v.s;
@@ -202,43 +276,4 @@ static inline int lcg_random(unsigned previous_val)
         av_log(avctx, AV_LOG_WARNING, __VA_ARGS__); \
     }
 
-#define AAC_OPT_SET(e_opt, p_opt, bypass, name)                                \
-    ERROR_IF ((e_opt)->name == 1 && (p_opt)->name == OPT_BANNED,               \
-              "Profile %i does not allow %s\n", avctx->profile, #name);        \
-    ERROR_IF ((e_opt)->name == 0 && (p_opt)->name == OPT_REQUIRED,             \
-             "Option %s is a requirement for this profile (%i)\n",             \
-              #name, avctx->profile);                                          \
-    if ((e_opt)->name == 1 && (p_opt)->name == OPT_NEEDS_MAIN &&               \
-        avctx->profile == FF_PROFILE_AAC_LOW) {                                \
-        WARN_IF(1, "Profile %i does not allow for %s, setting profile to "     \
-                "\"aac_main\"(%i)\n", avctx->profile, #name,                   \
-                FF_PROFILE_AAC_MAIN);                                          \
-        avctx->profile = FF_PROFILE_AAC_MAIN;                                  \
-        p_opt = &aacenc_profiles[FF_PROFILE_AAC_MAIN].opts;                    \
-    }                                                                          \
-    if ((e_opt)->name == 1 && (p_opt)->name == OPT_NEEDS_LTP &&                \
-        avctx->profile == FF_PROFILE_AAC_LOW) {                                \
-        WARN_IF(1, "Profile %i does not allow for %s, setting profile to "     \
-                "\"aac_ltp\"(%i)\n", avctx->profile, #name,                    \
-                FF_PROFILE_AAC_LTP);                                           \
-        avctx->profile = FF_PROFILE_AAC_LTP;                                   \
-        p_opt = &aacenc_profiles[FF_PROFILE_AAC_LTP].opts;                     \
-    }                                                                          \
-    if ((e_opt)->name == OPT_AUTO) {                                           \
-        if ((p_opt)->name == OPT_BANNED) {                                     \
-            (e_opt)->name = 0;                                                 \
-        } else if ((p_opt)->name == OPT_NEEDS_LTP) {                           \
-            (e_opt)->name = 0;                                                 \
-        } else if ((p_opt)->name == OPT_NEEDS_MAIN) {                          \
-            (e_opt)->name = 0;                                                 \
-        } else if ((p_opt)->name == OPT_REQUIRED) {                            \
-            (e_opt)->name = 1;                                                 \
-        } else if (bypass) {                                                   \
-            (e_opt)->name = (e_opt)->name;                                     \
-        } else {                                                               \
-            (e_opt)->name = (p_opt)->name;                                     \
-        }                                                                      \
-    }                                                                          \
-    av_log(avctx, AV_LOG_VERBOSE, "Option %s set to %i\n", #name, (e_opt)->name);
-
 #endif /* AVCODEC_AACENC_UTILS_H */