/* * AMR narrowband decoder * Copyright (c) 2006-2007 Robert Swain * Copyright (c) 2009 Colin McQuillan * * 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 */ /** * @file * AMR narrowband decoder * * This decoder uses floats for simplicity and so is not bit-exact. One * difference is that differences in phase can accumulate. The test sequences * in 3GPP TS 26.074 can still be useful. * * - Comparing this file's output to the output of the ref decoder gives a * PSNR of 30 to 80. Plotting the output samples shows a difference in * phase in some areas. * * - Comparing both decoders against their input, this decoder gives a similar * PSNR. If the test sequence homing frames are removed (this decoder does * not detect them), the PSNR is at least as good as the reference on 140 * out of 169 tests. */ #include <string.h>#include <math.h> #include "libavutil/channel_layout.h"#include "avcodec.h"#include "libavutil/common.h"#include "libavutil/avassert.h"#include "celp_math.h"#include "celp_filters.h"#include "acelp_filters.h"#include "acelp_vectors.h"#include "acelp_pitch_delay.h"#include "lsp.h"#include "amr.h"#include "codec_internal.h"#include "decode.h" #include "amrnbdata.h" #define AMR_BLOCK_SIZE 160 ///< samples per frame#define AMR_SAMPLE_BOUND 32768.0 ///< threshold for synthesis overflow /** * Scale from constructed speech to [-1,1] * * AMR is designed to produce 16-bit PCM samples (3GPP TS 26.090 4.2) but * upscales by two (section 6.2.2). * * Fundamentally, this scale is determined by energy_mean through * the fixed vector contribution to the excitation vector. */#define AMR_SAMPLE_SCALE (2.0 / 32768.0) /** Prediction factor for 12.2kbit/s mode */#define PRED_FAC_MODE_12k2 0.65 #define LSF_R_FAC (8000.0 / 32768.0) ///< LSF residual tables to Hertz#define MIN_LSF_SPACING (50.0488 / 8000.0) ///< Ensures stability of LPC filter#define PITCH_LAG_MIN_MODE_12k2 18 ///< Lower bound on decoded lag search in 12.2kbit/s mode /** Initial energy in dB. Also used for bad frames (unimplemented). */#define MIN_ENERGY -14.0 /** Maximum sharpening factor * * The specification says 0.8, which should be 13107, but the reference C code * uses 13017 instead. (Amusingly the same applies to SHARP_MAX in g729dec.c.) */#define SHARP_MAX 0.79449462890625 /** Number of impulse response coefficients used for tilt factor */#define AMR_TILT_RESPONSE 22/** Tilt factor = 1st reflection coefficient * gamma_t */#define AMR_TILT_GAMMA_T 0.8/** Adaptive gain control factor used in post-filter */#define AMR_AGC_ALPHA 0.9 typedef struct AMRContext { AMRNBFrame frame; ///< decoded AMR parameters (lsf coefficients, codebook indexes, etc) uint8_t bad_frame_indicator; ///< bad frame ? 1 : 0 enum Mode cur_frame_mode; int16_t prev_lsf_r[LP_FILTER_ORDER]; ///< residual LSF vector from previous subframe double lsp[4][LP_FILTER_ORDER]; ///< lsp vectors from current frame double prev_lsp_sub4[LP_FILTER_ORDER]; ///< lsp vector for the 4th subframe of the previous frame float lsf_q[4][LP_FILTER_ORDER]; ///< Interpolated LSF vector for fixed gain smoothing float lsf_avg[LP_FILTER_ORDER]; ///< vector of averaged lsf vector float lpc[4][LP_FILTER_ORDER]; ///< lpc coefficient vectors for 4 subframes uint8_t pitch_lag_int; ///< integer part of pitch lag from current subframe float excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1 + AMR_SUBFRAME_SIZE]; ///< current excitation and all necessary excitation history float *excitation; ///< pointer to the current excitation vector in excitation_buf float pitch_vector[AMR_SUBFRAME_SIZE]; ///< adaptive code book (pitch) vector float fixed_vector[AMR_SUBFRAME_SIZE]; ///< algebraic codebook (fixed) vector (must be kept zero between frames) float prediction_error[4]; ///< quantified prediction errors {20log10(^gamma_gc)} for previous four subframes float pitch_gain[5]; ///< quantified pitch gains for the current and previous four subframes float fixed_gain[5]; ///< quantified fixed gains for the current and previous four subframes float beta; ///< previous pitch_gain, bounded by [0.0,SHARP_MAX] uint8_t diff_count; ///< the number of subframes for which diff has been above 0.65 uint8_t hang_count; ///< the number of subframes since a hangover period started float prev_sparse_fixed_gain; ///< previous fixed gain; used by anti-sparseness processing to determine "onset" uint8_t prev_ir_filter_nr; ///< previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none uint8_t ir_filter_onset; ///< flag for impulse response filter strength float postfilter_mem[10]; ///< previous intermediate values in the formant filter float tilt_mem; ///< previous input to tilt compensation filter float postfilter_agc; ///< previous factor used for adaptive gain control float high_pass_mem[2]; ///< previous intermediate values in the high-pass filter float samples_in[LP_FILTER_ORDER + AMR_SUBFRAME_SIZE]; ///< floating point samples ACELPFContext acelpf_ctx; ///< context for filters for ACELP-based codecs ACELPVContext acelpv_ctx; ///< context for vector operations for ACELP-based codecs CELPFContext celpf_ctx; ///< context for filters for CELP-based codecs CELPMContext celpm_ctx; ///< context for fixed point math operations } AMRContext; typedef struct AMRChannelsContext { AMRContext ch[2];} AMRChannelsContext; /** Double version of ff_weighted_vector_sumf() */static void weighted_vector_sumd(double *out, const double *in_a, const double *in_b, double weight_coeff_a, double weight_coeff_b, int length){ int i; for (i = 0; i < length; i++) out[i] = weight_coeff_a * in_a[i] + weight_coeff_b * in_b[i];} static av_cold int amrnb_decode_init(AVCodecContext *avctx){ AMRChannelsContext *s = avctx->priv_data; int i; if (avctx->ch_layout.nb_channels > 2) { avpriv_report_missing_feature(avctx, ">2 channel AMR"); return AVERROR_PATCHWELCOME; } if (!avctx->ch_layout.nb_channels) { av_channel_layout_uninit(&avctx->ch_layout); avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; } if (!avctx->sample_rate) avctx->sample_rate = 8000; avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { AMRContext *p = &s->ch[ch]; // p->excitation always points to the same position in p->excitation_buf p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1]; for (i = 0; i < LP_FILTER_ORDER; i++) { p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15); p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15); } for (i = 0; i < 4; i++) p->prediction_error[i] = MIN_ENERGY; ff_acelp_filter_init(&p->acelpf_ctx); ff_acelp_vectors_init(&p->acelpv_ctx); ff_celp_filter_init(&p->celpf_ctx); ff_celp_math_init(&p->celpm_ctx); } return 0;} /** * Unpack an RFC4867 speech frame into the AMR frame mode and parameters. * * The order of speech bits is specified by 3GPP TS 26.101. * * @param p the context * @param buf pointer to the input buffer * @param buf_size size of the input buffer * * @return the frame mode */static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf, int buf_size){ enum Mode mode; // Decode the first octet. mode = buf[0] >> 3 & 0x0F; // frame type p->bad_frame_indicator = (buf[0] & 0x4) != 0x4; // quality bit if (mode >= N_MODES || buf_size < frame_sizes_nb[mode] + 1) { return NO_DATA; } if (mode < MODE_DTX) ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1, amr_unpacking_bitmaps_per_mode[mode]); return mode;} /// @name AMR pitch LPC coefficient decoding functions/// @{ /** * Interpolate the LSF vector (used for fixed gain smoothing). * The interpolation is done over all four subframes even in MODE_12k2. * * @param[in] ctx The Context * @param[in,out] lsf_q LSFs in [0,1] for each subframe * @param[in] lsf_new New LSFs in [0,1] for subframe 4 */static void interpolate_lsf(ACELPVContext *ctx, float lsf_q[4][LP_FILTER_ORDER], float *lsf_new){ int i; for (i = 0; i < 4; i++) ctx->weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new, 0.25 * (3 - i), 0.25 * (i + 1), LP_FILTER_ORDER);} /** * Decode a set of 5 split-matrix quantized lsf indexes into an lsp vector. * * @param p the context * @param lsp output LSP vector * @param lsf_no_r LSF vector without the residual vector added * @param lsf_quantizer pointers to LSF dictionary tables * @param quantizer_offset offset in tables * @param sign for the 3 dictionary table * @param update store data for computing the next frame's LSFs */static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER], const float lsf_no_r[LP_FILTER_ORDER], const int16_t *lsf_quantizer[5], const int quantizer_offset, const int sign, const int update){ int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector int i; for (i = 0; i < LP_FILTER_ORDER >> 1; i++) memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset], 2 * sizeof(*lsf_r)); if (sign) { lsf_r[4] *= -1; lsf_r[5] *= -1; } if (update) memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r)); for (i = 0; i < LP_FILTER_ORDER; i++) lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0); ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER); if (update) interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q); ff_acelp_lsf2lspd(lsp, lsf_q, LP_FILTER_ORDER);} /** * Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors. * * @param p pointer to the AMRContext */static void lsf2lsp_5(AMRContext *p){ const uint16_t *lsf_param = p->frame.lsf; float lsf_no_r[LP_FILTER_ORDER]; // LSFs without the residual vector const int16_t *lsf_quantizer[5]; int i; lsf_quantizer[0] = lsf_5_1[lsf_param[0]]; lsf_quantizer[1] = lsf_5_2[lsf_param[1]]; lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1]; lsf_quantizer[3] = lsf_5_4[lsf_param[3]]; lsf_quantizer[4] = lsf_5_5[lsf_param[4]]; for (i = 0; i < LP_FILTER_ORDER; i++) lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i]; lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0); lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1); // interpolate LSP vectors at subframes 1 and 3 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER); weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER);} /** * Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector. * * @param p pointer to the AMRContext */static void lsf2lsp_3(AMRContext *p){ const uint16_t *lsf_param = p->frame.lsf; int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector const int16_t *lsf_quantizer; int i, j; lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]]; memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r)); lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)]; memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r)); lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]]; memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r)); // calculate mean-removed LSF vector and add mean for (i = 0; i < LP_FILTER_ORDER; i++) lsf_q[i] = (lsf_r[i] + p->prev_lsf_r[i] * pred_fac[i]) * (LSF_R_FAC / 8000.0) + lsf_3_mean[i] * (1.0 / 8000.0); ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER); // store data for computing the next frame's LSFs interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q); memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r)); ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER); // interpolate LSP vectors at subframes 1, 2 and 3 for (i = 1; i <= 3; i++) for(j = 0; j < LP_FILTER_ORDER; j++) p->lsp[i-1][j] = p->prev_lsp_sub4[j] + (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;} /// @} /// @name AMR pitch vector decoding functions/// @{ /** * Like ff_decode_pitch_lag(), but with 1/6 resolution */static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index, const int prev_lag_int, const int subframe){ if (subframe == 0 || subframe == 2) { if (pitch_index < 463) { *lag_int = (pitch_index + 107) * 10923 >> 16; *lag_frac = pitch_index - *lag_int * 6 + 105; } else { *lag_int = pitch_index - 368; *lag_frac = 0; } } else { *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1; *lag_frac = pitch_index - *lag_int * 6 - 3; *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2, PITCH_DELAY_MAX - 9); }} static void decode_pitch_vector(AMRContext *p, const AMRNBSubframe *amr_subframe, const int subframe){ int pitch_lag_int, pitch_lag_frac; enum Mode mode = p->cur_frame_mode; if (p->cur_frame_mode == MODE_12k2) { decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac, amr_subframe->p_lag, p->pitch_lag_int, subframe); } else { ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac, amr_subframe->p_lag, p->pitch_lag_int, subframe, mode != MODE_4k75 && mode != MODE_5k15, mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6)); pitch_lag_frac *= 2; } p->pitch_lag_int = pitch_lag_int; // store previous lag in a uint8_t pitch_lag_int += pitch_lag_frac > 0; /* Calculate the pitch vector by interpolating the past excitation at the pitch lag using a b60 hamming windowed sinc function. */ p->acelpf_ctx.acelp_interpolatef(p->excitation, p->excitation + 1 - pitch_lag_int, ff_b60_sinc, 6, pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0), 10, AMR_SUBFRAME_SIZE); memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float));} /// @} /// @name AMR algebraic code book (fixed) vector decoding functions/// @{ /** * Decode a 10-bit algebraic codebook index from a 10.2 kbit/s frame. */static void decode_10bit_pulse(int code, int pulse_position[8], int i1, int i2, int i3){ // coded using 7+3 bits with the 3 LSBs being, individually, the LSB of 1 of // the 3 pulses and the upper 7 bits being coded in base 5 const uint8_t *positions = base_five_table[code >> 3]; pulse_position[i1] = (positions[2] << 1) + ( code & 1); pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1); pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1);} /** * Decode the algebraic codebook index to pulse positions and signs and * construct the algebraic codebook vector for MODE_10k2. * * @param fixed_index positions of the eight pulses * @param fixed_sparse pointer to the algebraic codebook vector */static void decode_8_pulses_31bits(const int16_t *fixed_index, AMRFixed *fixed_sparse){ int pulse_position[8]; int i, temp; decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1); decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5); // coded using 5+2 bits with the 2 LSBs being, individually, the LSB of 1 of // the 2 pulses and the upper 5 bits being coded in base 5 temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5; pulse_position[3] = temp % 5; pulse_position[7] = temp / 5; if (pulse_position[7] & 1) pulse_position[3] = 4 - pulse_position[3]; pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1); pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1); fixed_sparse->n = 8; for (i = 0; i < 4; i++) { const int pos1 = (pulse_position[i] << 2) + i; const int pos2 = (pulse_position[i + 4] << 2) + i; const float sign = fixed_index[i] ? -1.0 : 1.0; fixed_sparse->x[i ] = pos1; fixed_sparse->x[i + 4] = pos2; fixed_sparse->y[i ] = sign; fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign; }} /** * Decode the algebraic codebook index to pulse positions and signs, * then construct the algebraic codebook vector. * * nb of pulses | bits encoding pulses * For MODE_4k75 or MODE_5k15, 2 | 1-3, 4-6, 7 * MODE_5k9, 2 | 1, 2-4, 5-6, 7-9 * MODE_6k7, 3 | 1-3, 4, 5-7, 8, 9-11Lines 1–500 of 1109. Load more
/* * AMR narrowband decoder * Copyright (c) 2006-2007 Robert Swain * Copyright (c) 2009 Colin McQuillan * * 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 */ /** * @file * AMR narrowband decoder * * This decoder uses floats for simplicity and so is not bit-exact. One * difference is that differences in phase can accumulate. The test sequences * in 3GPP TS 26.074 can still be useful. * * - Comparing this file's output to the output of the ref decoder gives a * PSNR of 30 to 80. Plotting the output samples shows a difference in * phase in some areas. * * - Comparing both decoders against their input, this decoder gives a similar * PSNR. If the test sequence homing frames are removed (this decoder does * not detect them), the PSNR is at least as good as the reference on 140 * out of 169 tests. */ #include <string.h> #include <math.h> #include "libavutil/channel_layout.h" #include "avcodec.h" #include "libavutil/common.h" #include "libavutil/avassert.h" #include "celp_math.h" #include "celp_filters.h" #include "acelp_filters.h" #include "acelp_vectors.h" #include "acelp_pitch_delay.h" #include "lsp.h" #include "amr.h" #include "codec_internal.h" #include "decode.h" #include "amrnbdata.h" #define AMR_BLOCK_SIZE 160 ///< samples per frame #define AMR_SAMPLE_BOUND 32768.0 ///< threshold for synthesis overflow /** * Scale from constructed speech to [-1,1] * * AMR is designed to produce 16-bit PCM samples (3GPP TS 26.090 4.2) but * upscales by two (section 6.2.2). * * Fundamentally, this scale is determined by energy_mean through * the fixed vector contribution to the excitation vector. */ #define AMR_SAMPLE_SCALE (2.0 / 32768.0) /** Prediction factor for 12.2kbit/s mode */ #define PRED_FAC_MODE_12k2 0.65 #define LSF_R_FAC (8000.0 / 32768.0) ///< LSF residual tables to Hertz #define MIN_LSF_SPACING (50.0488 / 8000.0) ///< Ensures stability of LPC filter #define PITCH_LAG_MIN_MODE_12k2 18 ///< Lower bound on decoded lag search in 12.2kbit/s mode /** Initial energy in dB. Also used for bad frames (unimplemented). */ #define MIN_ENERGY -14.0 /** Maximum sharpening factor * * The specification says 0.8, which should be 13107, but the reference C code * uses 13017 instead. (Amusingly the same applies to SHARP_MAX in g729dec.c.) */ #define SHARP_MAX 0.79449462890625 /** Number of impulse response coefficients used for tilt factor */ #define AMR_TILT_RESPONSE 22 /** Tilt factor = 1st reflection coefficient * gamma_t */ #define AMR_TILT_GAMMA_T 0.8 /** Adaptive gain control factor used in post-filter */ #define AMR_AGC_ALPHA 0.9 typedef struct AMRContext { AMRNBFrame frame; ///< decoded AMR parameters (lsf coefficients, codebook indexes, etc) uint8_t bad_frame_indicator; ///< bad frame ? 1 : 0 enum Mode cur_frame_mode; int16_t prev_lsf_r[LP_FILTER_ORDER]; ///< residual LSF vector from previous subframe double lsp[4][LP_FILTER_ORDER]; ///< lsp vectors from current frame double prev_lsp_sub4[LP_FILTER_ORDER]; ///< lsp vector for the 4th subframe of the previous frame float lsf_q[4][LP_FILTER_ORDER]; ///< Interpolated LSF vector for fixed gain smoothing float lsf_avg[LP_FILTER_ORDER]; ///< vector of averaged lsf vector float lpc[4][LP_FILTER_ORDER]; ///< lpc coefficient vectors for 4 subframes uint8_t pitch_lag_int; ///< integer part of pitch lag from current subframe float excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1 + AMR_SUBFRAME_SIZE]; ///< current excitation and all necessary excitation history float *excitation; ///< pointer to the current excitation vector in excitation_buf float pitch_vector[AMR_SUBFRAME_SIZE]; ///< adaptive code book (pitch) vector float fixed_vector[AMR_SUBFRAME_SIZE]; ///< algebraic codebook (fixed) vector (must be kept zero between frames) float prediction_error[4]; ///< quantified prediction errors {20log10(^gamma_gc)} for previous four subframes float pitch_gain[5]; ///< quantified pitch gains for the current and previous four subframes float fixed_gain[5]; ///< quantified fixed gains for the current and previous four subframes float beta; ///< previous pitch_gain, bounded by [0.0,SHARP_MAX] uint8_t diff_count; ///< the number of subframes for which diff has been above 0.65 uint8_t hang_count; ///< the number of subframes since a hangover period started float prev_sparse_fixed_gain; ///< previous fixed gain; used by anti-sparseness processing to determine "onset" uint8_t prev_ir_filter_nr; ///< previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none uint8_t ir_filter_onset; ///< flag for impulse response filter strength float postfilter_mem[10]; ///< previous intermediate values in the formant filter float tilt_mem; ///< previous input to tilt compensation filter float postfilter_agc; ///< previous factor used for adaptive gain control float high_pass_mem[2]; ///< previous intermediate values in the high-pass filter float samples_in[LP_FILTER_ORDER + AMR_SUBFRAME_SIZE]; ///< floating point samples ACELPFContext acelpf_ctx; ///< context for filters for ACELP-based codecs ACELPVContext acelpv_ctx; ///< context for vector operations for ACELP-based codecs CELPFContext celpf_ctx; ///< context for filters for CELP-based codecs CELPMContext celpm_ctx; ///< context for fixed point math operations } AMRContext; typedef struct AMRChannelsContext { AMRContext ch[2]; } AMRChannelsContext; /** Double version of ff_weighted_vector_sumf() */ static void weighted_vector_sumd(double *out, const double *in_a, const double *in_b, double weight_coeff_a, double weight_coeff_b, int length) { int i; for (i = 0; i < length; i++) out[i] = weight_coeff_a * in_a[i] + weight_coeff_b * in_b[i]; } static av_cold int amrnb_decode_init(AVCodecContext *avctx) { AMRChannelsContext *s = avctx->priv_data; int i; if (avctx->ch_layout.nb_channels > 2) { avpriv_report_missing_feature(avctx, ">2 channel AMR"); return AVERROR_PATCHWELCOME; } if (!avctx->ch_layout.nb_channels) { av_channel_layout_uninit(&avctx->ch_layout); avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; } if (!avctx->sample_rate) avctx->sample_rate = 8000; avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { AMRContext *p = &s->ch[ch]; // p->excitation always points to the same position in p->excitation_buf p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1]; for (i = 0; i < LP_FILTER_ORDER; i++) { p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15); p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15); } for (i = 0; i < 4; i++) p->prediction_error[i] = MIN_ENERGY; ff_acelp_filter_init(&p->acelpf_ctx); ff_acelp_vectors_init(&p->acelpv_ctx); ff_celp_filter_init(&p->celpf_ctx); ff_celp_math_init(&p->celpm_ctx); } return 0; } /** * Unpack an RFC4867 speech frame into the AMR frame mode and parameters. * * The order of speech bits is specified by 3GPP TS 26.101. * * @param p the context * @param buf pointer to the input buffer * @param buf_size size of the input buffer * * @return the frame mode */ static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf, int buf_size) { enum Mode mode; // Decode the first octet. mode = buf[0] >> 3 & 0x0F; // frame type p->bad_frame_indicator = (buf[0] & 0x4) != 0x4; // quality bit if (mode >= N_MODES || buf_size < frame_sizes_nb[mode] + 1) { return NO_DATA; } if (mode < MODE_DTX) ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1, amr_unpacking_bitmaps_per_mode[mode]); return mode; } /// @name AMR pitch LPC coefficient decoding functions /// @{ /** * Interpolate the LSF vector (used for fixed gain smoothing). * The interpolation is done over all four subframes even in MODE_12k2. * * @param[in] ctx The Context * @param[in,out] lsf_q LSFs in [0,1] for each subframe * @param[in] lsf_new New LSFs in [0,1] for subframe 4 */ static void interpolate_lsf(ACELPVContext *ctx, float lsf_q[4][LP_FILTER_ORDER], float *lsf_new) { int i; for (i = 0; i < 4; i++) ctx->weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new, 0.25 * (3 - i), 0.25 * (i + 1), LP_FILTER_ORDER); } /** * Decode a set of 5 split-matrix quantized lsf indexes into an lsp vector. * * @param p the context * @param lsp output LSP vector * @param lsf_no_r LSF vector without the residual vector added * @param lsf_quantizer pointers to LSF dictionary tables * @param quantizer_offset offset in tables * @param sign for the 3 dictionary table * @param update store data for computing the next frame's LSFs */ static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER], const float lsf_no_r[LP_FILTER_ORDER], const int16_t *lsf_quantizer[5], const int quantizer_offset, const int sign, const int update) { int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector int i; for (i = 0; i < LP_FILTER_ORDER >> 1; i++) memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset], 2 * sizeof(*lsf_r)); if (sign) { lsf_r[4] *= -1; lsf_r[5] *= -1; } if (update) memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r)); for (i = 0; i < LP_FILTER_ORDER; i++) lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0); ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER); if (update) interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q); ff_acelp_lsf2lspd(lsp, lsf_q, LP_FILTER_ORDER); } /** * Decode a set of 5 split-matrix quantized lsf indexes into 2 lsp vectors. * * @param p pointer to the AMRContext */ static void lsf2lsp_5(AMRContext *p) { const uint16_t *lsf_param = p->frame.lsf; float lsf_no_r[LP_FILTER_ORDER]; // LSFs without the residual vector const int16_t *lsf_quantizer[5]; int i; lsf_quantizer[0] = lsf_5_1[lsf_param[0]]; lsf_quantizer[1] = lsf_5_2[lsf_param[1]]; lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1]; lsf_quantizer[3] = lsf_5_4[lsf_param[3]]; lsf_quantizer[4] = lsf_5_5[lsf_param[4]]; for (i = 0; i < LP_FILTER_ORDER; i++) lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i]; lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0); lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1); // interpolate LSP vectors at subframes 1 and 3 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER); weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER); } /** * Decode a set of 3 split-matrix quantized lsf indexes into an lsp vector. * * @param p pointer to the AMRContext */ static void lsf2lsp_3(AMRContext *p) { const uint16_t *lsf_param = p->frame.lsf; int16_t lsf_r[LP_FILTER_ORDER]; // residual LSF vector float lsf_q[LP_FILTER_ORDER]; // quantified LSF vector const int16_t *lsf_quantizer; int i, j; lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]]; memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r)); lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)]; memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r)); lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]]; memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r)); // calculate mean-removed LSF vector and add mean for (i = 0; i < LP_FILTER_ORDER; i++) lsf_q[i] = (lsf_r[i] + p->prev_lsf_r[i] * pred_fac[i]) * (LSF_R_FAC / 8000.0) + lsf_3_mean[i] * (1.0 / 8000.0); ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER); // store data for computing the next frame's LSFs interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q); memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r)); ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER); // interpolate LSP vectors at subframes 1, 2 and 3 for (i = 1; i <= 3; i++) for(j = 0; j < LP_FILTER_ORDER; j++) p->lsp[i-1][j] = p->prev_lsp_sub4[j] + (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i; } /// @} /// @name AMR pitch vector decoding functions /// @{ /** * Like ff_decode_pitch_lag(), but with 1/6 resolution */ static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index, const int prev_lag_int, const int subframe) { if (subframe == 0 || subframe == 2) { if (pitch_index < 463) { *lag_int = (pitch_index + 107) * 10923 >> 16; *lag_frac = pitch_index - *lag_int * 6 + 105; } else { *lag_int = pitch_index - 368; *lag_frac = 0; } } else { *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1; *lag_frac = pitch_index - *lag_int * 6 - 3; *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2, PITCH_DELAY_MAX - 9); } } static void decode_pitch_vector(AMRContext *p, const AMRNBSubframe *amr_subframe, const int subframe) { int pitch_lag_int, pitch_lag_frac; enum Mode mode = p->cur_frame_mode; if (p->cur_frame_mode == MODE_12k2) { decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac, amr_subframe->p_lag, p->pitch_lag_int, subframe); } else { ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac, amr_subframe->p_lag, p->pitch_lag_int, subframe, mode != MODE_4k75 && mode != MODE_5k15, mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6)); pitch_lag_frac *= 2; } p->pitch_lag_int = pitch_lag_int; // store previous lag in a uint8_t pitch_lag_int += pitch_lag_frac > 0; /* Calculate the pitch vector by interpolating the past excitation at the pitch lag using a b60 hamming windowed sinc function. */ p->acelpf_ctx.acelp_interpolatef(p->excitation, p->excitation + 1 - pitch_lag_int, ff_b60_sinc, 6, pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0), 10, AMR_SUBFRAME_SIZE); memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float)); } /// @} /// @name AMR algebraic code book (fixed) vector decoding functions /// @{ /** * Decode a 10-bit algebraic codebook index from a 10.2 kbit/s frame. */ static void decode_10bit_pulse(int code, int pulse_position[8], int i1, int i2, int i3) { // coded using 7+3 bits with the 3 LSBs being, individually, the LSB of 1 of // the 3 pulses and the upper 7 bits being coded in base 5 const uint8_t *positions = base_five_table[code >> 3]; pulse_position[i1] = (positions[2] << 1) + ( code & 1); pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1); pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1); } /** * Decode the algebraic codebook index to pulse positions and signs and * construct the algebraic codebook vector for MODE_10k2. * * @param fixed_index positions of the eight pulses * @param fixed_sparse pointer to the algebraic codebook vector */ static void decode_8_pulses_31bits(const int16_t *fixed_index, AMRFixed *fixed_sparse) { int pulse_position[8]; int i, temp; decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1); decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5); // coded using 5+2 bits with the 2 LSBs being, individually, the LSB of 1 of // the 2 pulses and the upper 5 bits being coded in base 5 temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5; pulse_position[3] = temp % 5; pulse_position[7] = temp / 5; if (pulse_position[7] & 1) pulse_position[3] = 4 - pulse_position[3]; pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1); pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1); fixed_sparse->n = 8; for (i = 0; i < 4; i++) { const int pos1 = (pulse_position[i] << 2) + i; const int pos2 = (pulse_position[i + 4] << 2) + i; const float sign = fixed_index[i] ? -1.0 : 1.0; fixed_sparse->x[i ] = pos1; fixed_sparse->x[i + 4] = pos2; fixed_sparse->y[i ] = sign; fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign; } } /** * Decode the algebraic codebook index to pulse positions and signs, * then construct the algebraic codebook vector. * * nb of pulses | bits encoding pulses * For MODE_4k75 or MODE_5k15, 2 | 1-3, 4-6, 7 * MODE_5k9, 2 | 1, 2-4, 5-6, 7-9 * MODE_6k7, 3 | 1-3, 4, 5-7, 8, 9-11