/*
 * adaptive and fixed codebook vector operations for ACELP-based codecs
 *
 * Copyright (c) 2008 Vladimir Voroshilov
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_ACELP_VECTORS_H
#define AVCODEC_ACELP_VECTORS_H

#include <stdint.h>

/** Sparse representation for the algebraic codebook (fixed) vector */
typedef struct {
    int      n;
    int      x[10];
    float    y[10];
    int      no_repeat_mask;
    int      pitch_lag;
    float    pitch_fac;
} AMRFixed;

/**
 * returns the dot product.
 * @param a input data array
 * @param b input data array
 * @param length number of elements
 *
 * @return dot product = sum of elementwise products
 */
float ff_dot_productf(const float* a, const float* b, int length);

/**
 * float implementation of weighted sum of two vectors.
 * @param[out] out result of addition
 * @param in_a first vector
 * @param in_b second vector
 * @param weight_coeff_a first vector weight coefficient
 * @param weight_coeff_a second vector weight coefficient
 * @param length vectors length
 *
 * @note It is safe to pass the same buffer for out and in_a or in_b.
 */
void ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b,
                             float weight_coeff_a, float weight_coeff_b,
                             int length);

/**
 * Set the sum of squares of a signal by scaling
 *
 * @param out output samples
 * @param in input samples
 * @param sum_of_squares new sum of squares
 * @param n number of samples
 *
 * @note If the input is zero (or its energy underflows), the output is zero.
 *       This is the behavior of AGC in the AMR reference decoder. The QCELP
 *       reference decoder seems to have undefined behavior.
 *
 * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
 * 3GPP TS 26.090 6.1 (6)
 */
void ff_scale_vector_to_given_sum_of_squares(float *out, const float *in,
                                             float sum_of_squares, const int n);

/**
 * Calculate fixed gain (part of section 6.1.3 of AMR spec)
 *
 * @param fixed_gain_factor gain correction factor
 * @param prediction_error vector of the quantified predictor errors of
 *        the four previous subframes. It is updated by this function.
 * @param energy_mean desired mean innovation energy
 * @param pred_table table of four moving average coefficients
 */
float ff_amr_set_fixed_gain(float fixed_gain_factor, float *prediction_error,
                            float energy_mean, const float *pred_table);

/**
 * Add fixed vector to an array from a sparse representation
 *
 * @param out fixed vector with pitch sharpening
 * @param in sparse fixed vector
 * @param scale number to multiply the fixed vector by
 * @param size the output vector size
 */
void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size);

/**
 * Clear array values set by set_fixed_vector
 *
 * @param out fixed vector to be cleared
 * @param in sparse fixed vector
 * @param size the output vector size
 */
void ff_clear_fixed_vector(float *out, const AMRFixed *in, int size);

#endif /* AVCODEC_ACELP_VECTORS_H */
