]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/sipr.c
Rename aac.c to aacdec.c.
[ffmpeg] / libavcodec / sipr.c
index 7bfa3ab9f132058a4ae7b3cebe61962242be2264..b76e89100ffe9323d115922c4fbdde37ba042ef5 100644 (file)
@@ -24,6 +24,7 @@
 #include <math.h>
 #include <stdint.h>
 
+#include "libavutil/mathematics.h"
 #include "avcodec.h"
 #define ALT_BITSTREAM_READER_LE
 #include "get_bits.h"
 #include "acelp_filters.h"
 #include "celp_filters.h"
 
-#define LSFQ_DIFF_MIN        (0.0125 * M_PI)
-
-#define LP_FILTER_ORDER      10
-
-/** Number of past samples needed for excitation interpolation */
-#define L_INTERPOL           (LP_FILTER_ORDER + 1)
-
-/**  Subframe size for all modes except 16k */
-#define SUBFR_SIZE           48
+#define MAX_SUBFRAME_COUNT   5
 
+#include "sipr.h"
 #include "siprdata.h"
 
-typedef enum {
-    MODE_16k,
-    MODE_8k5,
-    MODE_6k5,
-    MODE_5k0,
-    MODE_COUNT
-} SiprMode;
-
 typedef struct {
     const char *mode_name;
     uint16_t bits_per_frame;
@@ -65,6 +51,7 @@ typedef struct {
 
     /* bitstream parameters */
     uint8_t number_of_fc_indexes;
+    uint8_t ma_predictor_bits;  ///< size in bits of the switched MA predictor
 
     /** size in bits of the i-th stage vector of quantizer */
     uint8_t vq_indexes_bits[5];
@@ -78,6 +65,22 @@ typedef struct {
 } SiprModeParam;
 
 static const SiprModeParam modes[MODE_COUNT] = {
+    [MODE_16k] = {
+        .mode_name          = "16k",
+        .bits_per_frame     = 160,
+        .subframe_count     = SUBFRAME_COUNT_16k,
+        .frames_per_packet  = 1,
+        .pitch_sharp_factor = 0.00,
+
+        .number_of_fc_indexes = 10,
+        .ma_predictor_bits    = 1,
+        .vq_indexes_bits      = {7, 8, 7, 7, 7},
+        .pitch_delay_bits     = {9, 6},
+        .gp_index_bits        = 4,
+        .fc_index_bits        = {4, 5, 4, 5, 4, 5, 4, 5, 4, 5},
+        .gc_index_bits        = 5
+    },
+
     [MODE_8k5] = {
         .mode_name          = "8k5",
         .bits_per_frame     = 152,
@@ -86,6 +89,7 @@ static const SiprModeParam modes[MODE_COUNT] = {
         .pitch_sharp_factor = 0.8,
 
         .number_of_fc_indexes = 3,
+        .ma_predictor_bits    = 0,
         .vq_indexes_bits      = {6, 7, 7, 7, 5},
         .pitch_delay_bits     = {8, 5, 5},
         .gp_index_bits        = 0,
@@ -101,6 +105,7 @@ static const SiprModeParam modes[MODE_COUNT] = {
         .pitch_sharp_factor = 0.8,
 
         .number_of_fc_indexes = 3,
+        .ma_predictor_bits    = 0,
         .vq_indexes_bits      = {6, 7, 7, 7, 5},
         .pitch_delay_bits     = {8, 5, 5},
         .gp_index_bits        = 0,
@@ -116,6 +121,7 @@ static const SiprModeParam modes[MODE_COUNT] = {
         .pitch_sharp_factor = 0.85,
 
         .number_of_fc_indexes = 1,
+        .ma_predictor_bits    = 0,
         .vq_indexes_bits      = {6, 7, 7, 7, 5},
         .pitch_delay_bits     = {8, 5, 8, 5, 5},
         .gp_index_bits        = 0,
@@ -124,41 +130,12 @@ static const SiprModeParam modes[MODE_COUNT] = {
     }
 };
 
-typedef struct {
-    AVCodecContext *avctx;
-    DSPContext dsp;
-
-    SiprModeParam m;
-    SiprMode mode;
-
-    float past_pitch_gain;
-    float lsf_history[LP_FILTER_ORDER];
-
-    float excitation[L_INTERPOL + PITCH_DELAY_MAX + 5*SUBFR_SIZE];
-
-    DECLARE_ALIGNED_16(float, synth_buf[LP_FILTER_ORDER + 5*SUBFR_SIZE + 6]);
-
-    float lsp_history[LP_FILTER_ORDER];
-    float gain_mem;
-    float energy_history[4];
-    float highpass_filt_mem[2];
-    float postfilter_mem[PITCH_DELAY_MAX + LP_FILTER_ORDER];
-
-    /* 5k0 */
-    float tilt_mem;
-    float postfilter_agc;
-    float postfilter_mem5k0[PITCH_DELAY_MAX + LP_FILTER_ORDER];
-    float postfilter_syn5k0[LP_FILTER_ORDER + SUBFR_SIZE*5];
-} SiprContext;
-
-typedef struct {
-    int vq_indexes[5];
-    int pitch_delay[5];        ///< pitch delay
-    int gp_index[5];           ///< adaptive-codebook gain indexes
-    int16_t fc_indexes[5][10]; ///< fixed-codebook indexes
-    int gc_index[5];           ///< fixed-codebook gain indexes
-} SiprParameters;
-
+const float ff_pow_0_5[] = {
+    1.0/(1 <<  1), 1.0/(1 <<  2), 1.0/(1 <<  3), 1.0/(1 <<  4),
+    1.0/(1 <<  5), 1.0/(1 <<  6), 1.0/(1 <<  7), 1.0/(1 <<  8),
+    1.0/(1 <<  9), 1.0/(1 << 10), 1.0/(1 << 11), 1.0/(1 << 12),
+    1.0/(1 << 13), 1.0/(1 << 14), 1.0/(1 << 15), 1.0/(1 << 16)
+};
 
 static void dequant(float *out, const int *idx, const float *cbs[])
 {
@@ -216,6 +193,8 @@ static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
 {
     int i, j;
 
+    parms->ma_pred_switch           = get_bits(pgb, p->ma_predictor_bits);
+
     for (i = 0; i < 5; i++)
         parms->vq_indexes[i]        = get_bits(pgb, p->vq_indexes_bits[i]);
 
@@ -233,8 +212,8 @@ static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
 static void lsp2lpc_sipr(const double *lsp, float *Az)
 {
     int lp_half_order = LP_FILTER_ORDER >> 1;
-    double buf[lp_half_order + 1];
-    double pa[lp_half_order + 1];
+    double buf[(LP_FILTER_ORDER >> 1) + 1];
+    double pa[(LP_FILTER_ORDER >> 1) + 1];
     double *qa = buf + 1;
     int i,j;
 
@@ -275,7 +254,7 @@ static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az,
 }
 
 /**
- * Evaluates the adaptative impulse response.
+ * Evaluates the adaptive impulse response.
  */
 static void eval_ir(const float *Az, int pitch_lag, float *freq,
                     float pitch_sharp_factor)
@@ -323,7 +302,7 @@ static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples)
 
     for (i = 0; i < LP_FILTER_ORDER; i++) {
         lpc_d[i] = lpc[i] * ff_pow_0_75[i];
-        lpc_n[i] = lpc[i] *    pow_0_5 [i];
+        lpc_n[i] = lpc[i] * ff_pow_0_5 [i];
     };
 
     memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem,
@@ -408,8 +387,9 @@ static void decode_frame(SiprContext *ctx, SiprParameters *params,
                          float *out_data)
 {
     int i, j;
-    int frame_size = ctx->m.subframe_count * SUBFR_SIZE;
-    float Az[LP_FILTER_ORDER * ctx->m.subframe_count];
+    int subframe_count = modes[ctx->mode].subframe_count;
+    int frame_size = subframe_count * SUBFR_SIZE;
+    float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT];
     float *excitation;
     float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER];
     float lsf_new[LP_FILTER_ORDER];
@@ -422,13 +402,13 @@ static void decode_frame(SiprContext *ctx, SiprParameters *params,
     memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
     lsf_decode_fp(lsf_new, ctx->lsf_history, params);
 
-    sipr_decode_lp(lsf_new, ctx->lsp_history, Az, ctx->m.subframe_count);
+    sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count);
 
     memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
 
     excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
 
-    for (i = 0; i < ctx->m.subframe_count; i++) {
+    for (i = 0; i < subframe_count; i++) {
         float *pAz = Az + i*LP_FILTER_ORDER;
         float fixed_vector[SUBFR_SIZE];
         int T0,T0_frac;
@@ -448,7 +428,7 @@ static void decode_frame(SiprContext *ctx, SiprParameters *params,
         decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode,
                             ctx->past_pitch_gain < 0.8);
 
-        eval_ir(pAz, T0, impulse_response, ctx->m.pitch_sharp_factor);
+        eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor);
 
         convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
                               SUBFR_SIZE);
@@ -461,7 +441,7 @@ static void decode_frame(SiprContext *ctx, SiprParameters *params,
 
         gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1],
                                           avg_energy, ctx->energy_history,
-                                          34 - 15.0/(log2f(10.0) * 0.05),
+                                          34 - 15.0/(0.05*M_LN10/M_LN2),
                                           pred);
 
         ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
@@ -495,12 +475,13 @@ static void decode_frame(SiprContext *ctx, SiprParameters *params,
            LP_FILTER_ORDER * sizeof(float));
 
     if (ctx->mode == MODE_5k0) {
-        for (i = 0; i < ctx->m.subframe_count; i++) {
+        for (i = 0; i < subframe_count; i++) {
             float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
                                            ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
                                            SUBFR_SIZE);
-            ff_adaptative_gain_control(&synth[i * SUBFR_SIZE], energy,
-                                       SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
+            ff_adaptive_gain_control(&synth[i * SUBFR_SIZE],
+                                     &synth[i * SUBFR_SIZE], energy,
+                                     SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
         }
 
         memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size,
@@ -509,15 +490,12 @@ static void decode_frame(SiprContext *ctx, SiprParameters *params,
     memcpy(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL,
            (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float));
 
-    ff_acelp_apply_order_2_transfer_function(synth,
+    ff_acelp_apply_order_2_transfer_function(out_data, synth,
                                              (const float[2]) {-1.99997   , 1.000000000},
                                              (const float[2]) {-1.93307352, 0.935891986},
                                              0.939805806,
                                              ctx->highpass_filt_mem,
                                              frame_size);
-
-    ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size);
-
 }
 
 static av_cold int sipr_decoder_init(AVCodecContext * avctx)
@@ -530,8 +508,10 @@ static av_cold int sipr_decoder_init(AVCodecContext * avctx)
     else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5;
     else                              ctx->mode = MODE_5k0;
 
-    ctx->m = modes[ctx->mode];
-    av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", ctx->m.mode_name);
+    av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name);
+
+    if (ctx->mode == MODE_16k)
+        ff_sipr_init_16k(ctx);
 
     for (i = 0; i < LP_FILTER_ORDER; i++)
         ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1));
@@ -541,12 +521,6 @@ static av_cold int sipr_decoder_init(AVCodecContext * avctx)
 
     avctx->sample_fmt = SAMPLE_FMT_FLT;
 
-    if (ctx->mode == MODE_16k) {
-        av_log(avctx, AV_LOG_ERROR, "decoding 16kbps SIPR files is not "
-                                    "supported yet.\n");
-        return -1;
-    }
-
     dsputil_init(&ctx->dsp, avctx);
 
     return 0;
@@ -558,12 +532,14 @@ static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
     SiprContext *ctx = avctx->priv_data;
     const uint8_t *buf=avpkt->data;
     SiprParameters parm;
+    const SiprModeParam *mode_par = &modes[ctx->mode];
     GetBitContext gb;
     float *data = datap;
+    int subframe_size = ctx->mode == MODE_16k ? L_SUBFR_16k : SUBFR_SIZE;
     int i;
 
     ctx->avctx = avctx;
-    if (avpkt->size < (ctx->m.bits_per_frame >> 3)) {
+    if (avpkt->size < (mode_par->bits_per_frame >> 3)) {
         av_log(avctx, AV_LOG_ERROR,
                "Error processing packet: packet size (%d) too small\n",
                avpkt->size);
@@ -571,7 +547,7 @@ static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
         *data_size = 0;
         return -1;
     }
-    if (*data_size < SUBFR_SIZE * ctx->m.subframe_count * sizeof(float)) {
+    if (*data_size < subframe_size * mode_par->subframe_count * sizeof(float)) {
         av_log(avctx, AV_LOG_ERROR,
                "Error processing packet: output buffer (%d) too small\n",
                *data_size);
@@ -580,24 +556,28 @@ static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
         return -1;
     }
 
-    init_get_bits(&gb, buf, ctx->m.bits_per_frame);
+    init_get_bits(&gb, buf, mode_par->bits_per_frame);
+
+    for (i = 0; i < mode_par->frames_per_packet; i++) {
+        decode_parameters(&parm, &gb, mode_par);
 
-    for (i = 0; i < ctx->m.frames_per_packet; i++) {
-        decode_parameters(&parm, &gb, &ctx->m);
-        decode_frame(ctx, &parm, data);
+        if (ctx->mode == MODE_16k)
+            ff_sipr_decode_frame_16k(ctx, &parm, data);
+        else
+            decode_frame(ctx, &parm, data);
 
-        data += SUBFR_SIZE * ctx->m.subframe_count;
+        data += subframe_size * mode_par->subframe_count;
     }
 
-    *data_size = ctx->m.frames_per_packet * SUBFR_SIZE *
-        ctx->m.subframe_count * sizeof(float);
+    *data_size = mode_par->frames_per_packet * subframe_size *
+        mode_par->subframe_count * sizeof(float);
 
-    return ctx->m.bits_per_frame >> 3;
+    return mode_par->bits_per_frame >> 3;
 };
 
 AVCodec sipr_decoder = {
     "sipr",
-    CODEC_TYPE_AUDIO,
+    AVMEDIA_TYPE_AUDIO,
     CODEC_ID_SIPR,
     sizeof(SiprContext),
     sipr_decoder_init,