$OpenBSD: patch-libavcodec_aacenc_h,v 1.9 2016/12/05 09:02:29 ajacoutot Exp $

aacenc: copy PRNG from the decoder

AAC encoder: tweak rate-distortion logic

AAC encoder: Extensive improvements

AAC encoder: memoize quantize_band_cost

aacenc: add support for changing options based on a profile

aacenc: increase size of s->planar_samples[] from 6 to 8

aacenc: shorten name of ff_aac_adjust_common_prediction

aacenc: add support for encoding files using Long Term Prediction

aacenc: switch to using the RNG from libavutil

acenc: remove deprecated avctx->frame_bits use

aacenc: remove FAAC-like coder

aacenc: use generational cache instead of resetting.

aacenc: use the decoder's lcg PRNG

aacenc: quit when the audio queue reaches 0 rather than keeping track of empty frames

--- libavcodec/aacenc.h.orig	Sat Aug 27 22:51:19 2016
+++ libavcodec/aacenc.h	Thu Nov 10 19:21:34 2016
@@ -33,8 +33,7 @@
 #include "lpc.h"
 
 typedef enum AACCoder {
-    AAC_CODER_FAAC = 0,
-    AAC_CODER_ANMR,
+    AAC_CODER_ANMR = 0,
     AAC_CODER_TWOLOOP,
     AAC_CODER_FAST,
 
@@ -42,11 +41,12 @@ typedef enum AACCoder {
 }AACCoder;
 
 typedef struct AACEncOptions {
-    int stereo_mode;
-    int aac_coder;
+    int coder;
     int pns;
     int tns;
+    int ltp;
     int pred;
+    int mid_side;
     int intensity_stereo;
 } AACEncOptions;
 
@@ -60,13 +60,19 @@ typedef struct AACCoefficientsEncoder {
     void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
                                      int scale_idx, int cb, const float lambda, int rtz);
     void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
+    void (*encode_ltp_info)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
     void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
-    void (*adjust_common_prediction)(struct AACEncContext *s, ChannelElement *cpe);
+    void (*adjust_common_pred)(struct AACEncContext *s, ChannelElement *cpe);
+    void (*adjust_common_ltp)(struct AACEncContext *s, ChannelElement *cpe);
     void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
     void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
+    void (*update_ltp)(struct AACEncContext *s, SingleChannelElement *sce);
+    void (*ltp_insert_new_frame)(struct AACEncContext *s);
     void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
     void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
+    void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
     void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
+    void (*search_for_ltp)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
     void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
     void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
     void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
@@ -74,6 +80,15 @@ typedef struct AACCoefficientsEncoder {
 
 extern AACCoefficientsEncoder ff_aac_coders[];
 
+typedef struct AACQuantizeBandCostCacheEntry {
+    float rd;
+    float energy;
+    int bits;
+    char cb;
+    char rtz;
+    uint16_t generation;
+} AACQuantizeBandCostCacheEntry;
+
 /**
  * AAC encoder context
  */
@@ -84,7 +99,7 @@ typedef struct AACEncContext {
     FFTContext mdct1024;                         ///< long (1024 samples) frame transform context
     FFTContext mdct128;                          ///< short (128 samples) frame transform context
     AVFloatDSPContext *fdsp;
-    float *planar_samples[6];                    ///< saved preprocessed input
+    float *planar_samples[8];                    ///< saved preprocessed input
 
     int profile;                                 ///< copied from avctx
     LPCContext lpc;                              ///< used by TNS
@@ -96,18 +111,28 @@ typedef struct AACEncContext {
     FFPsyContext psy;
     struct FFPsyPreprocessContext* psypp;
     AACCoefficientsEncoder *coder;
-    int cur_channel;
-    int last_frame;
+    int cur_channel;                             ///< current channel for coder context
+    int random_state;
     float lambda;
+    int last_frame_pb_count;                     ///< number of bits for the previous frame
+    float lambda_sum;                            ///< sum(lambda), for Qvg reporting
+    int lambda_count;                            ///< count(lambda), for Qvg reporting
+    enum RawDataBlockType cur_type;              ///< channel group type cur_channel belongs to
+
     AudioFrameQueue afq;
     DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
     DECLARE_ALIGNED(32, float, scoefs)[1024];    ///< scaled coefficients
 
+    uint16_t quantize_band_cost_cache_generation;
+    AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
+
     struct {
         float *samples;
     } buffer;
 } AACEncContext;
 
 void ff_aac_coder_init_mips(AACEncContext *c);
+void ff_quantize_band_cost_cache_init(struct AACEncContext *s);
+
 
 #endif /* AVCODEC_AACENC_H */
