summaryrefslogtreecommitdiff
path: root/libavcodec/aacenc_utils.h
diff options
context:
space:
mode:
authorClaudio Freire <klaussfreire@gmail.com>2015-12-01 03:28:36 -0300
committerClaudio Freire <klaussfreire@gmail.com>2015-12-02 07:47:37 -0300
commitca203e9985cd2dcf42a0c0853940850d3a8edf3a (patch)
tree4dd1ad824283d75afdd1191c70be982c03c1b683 /libavcodec/aacenc_utils.h
parentec83efd4d3c5fe1e4bc5723d0b91abf85b722f41 (diff)
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it woudln't be possible to commit them separately without causing either quality regressions or assertion failures. Fate comparison targets don't all reflect improvements in quality, yet listening tests show substantially improved quality and stability. 1. Increase SF range utilization. The spec requires SF delta values to be constrained within the range -60..60. The previous code was applying that range to the whole SF array and not only the deltas of consecutive values, because doing so requires smarter code: zeroing or otherwise skipping a band may invalidate lots of SF choices. This patch implements that logic to allow the coders to utilize the full dynamic range of scalefactors, increasing quality quite considerably, and fixing delta-SF-related assertion failures, since now the limitation is enforced rather than asserted. 2. PNS tweaks The previous modification makes big improvements in twoloop's efficiency, and every time that happens PNS logic needs to be tweaked accordingly to avoid it from stepping all over twoloop's decisions. This patch includes modifications of the sort. 3. Account for lowpass cutoff during PSY analysis The closer PSY's allocation is to final allocation the better the quality is, and given these modifications, twoloop is now very efficient at avoiding holes. Thus, to compute accurate thresholds, PSY needs to account for the lowpass applied implicitly during twoloop (by zeroing high bands). This patch makes twoloop set the cutoff in psymodel's context the first time it runs, and makes PSY account for it during threshold computation, making PE and threshold computations closer to the final allocation and thus achieving better subjective quality. 4. Tweaks to RC lambda tracking loop in relation to PNS Without this tweak some corner cases cause quality regressions. Basically, lambda needs to react faster to overall bitrate efficiency changes since now PNS can be quite successful in enforcing maximum bitrates, when PSY allocates too many bits to the lower bands, suppressing the signals RC logic uses to lower lambda in those cases and causing aggressive PNS. This tweak makes PNS much less aggressive, though it can still use some further tweaks. Also update MIPS specializations and adjust fuzz Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
Diffstat (limited to 'libavcodec/aacenc_utils.h')
-rw-r--r--libavcodec/aacenc_utils.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/libavcodec/aacenc_utils.h b/libavcodec/aacenc_utils.h
index 40e1746639..5b308f2efe 100644
--- a/libavcodec/aacenc_utils.h
+++ b/libavcodec/aacenc_utils.h
@@ -191,6 +191,69 @@ static inline int lcg_random(unsigned previous_val)
return v.s;
}
+
+/*
+ * 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 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);
+}
+
#define ERROR_IF(cond, ...) \
if (cond) { \
av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \