/* * AMR wideband decoder * Copyright (c) 2010 Marcelo Galvao Povoa * * 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 wideband decoder */ #include "config.h" #include "libavutil/avassert.h"#include "libavutil/channel_layout.h"#include "libavutil/common.h"#include "libavutil/lfg.h" #include "avcodec.h"#include "lsp.h"#include "celp_filters.h"#include "celp_math.h"#include "acelp_filters.h"#include "acelp_vectors.h"#include "acelp_pitch_delay.h"#include "codec_internal.h"#include "decode.h" #define AMR_USE_16BIT_TABLES#include "amr.h" #include "amrwbdata.h"#if ARCH_MIPS#include "mips/amrwbdec_mips.h"#endif /* ARCH_MIPS */ typedef struct AMRWBContext { AMRWBFrame frame; ///< AMRWB parameters decoded from bitstream enum Mode fr_cur_mode; ///< mode index of current frame uint8_t fr_quality; ///< frame quality index (FQI) float isf_cur[LP_ORDER]; ///< working ISF vector from current frame float isf_q_past[LP_ORDER]; ///< quantized ISF vector of the previous frame float isf_past_final[LP_ORDER]; ///< final processed ISF vector of the previous frame double isp[4][LP_ORDER]; ///< ISP vectors from current frame double isp_sub4_past[LP_ORDER]; ///< ISP vector for the 4th subframe of the previous frame float lp_coef[4][LP_ORDER]; ///< Linear Prediction Coefficients from ISP vector uint8_t base_pitch_lag; ///< integer part of pitch lag for the next relative subframe uint8_t pitch_lag_int; ///< integer part of pitch lag of the previous subframe float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE]; ///< current excitation and all necessary excitation history float *excitation; ///< points to current excitation in excitation_buf[] float pitch_vector[AMRWB_SFR_SIZE]; ///< adaptive codebook (pitch) vector for current subframe float fixed_vector[AMRWB_SFR_SIZE]; ///< algebraic codebook (fixed) vector for current subframe float prediction_error[4]; ///< quantified prediction errors {20log10(^gamma_gc)} for previous four subframes float pitch_gain[6]; ///< quantified pitch gains for the current and previous five subframes float fixed_gain[2]; ///< quantified fixed gains for the current and previous subframes float tilt_coef; ///< {beta_1} related to the voicing of the previous subframe float prev_sparse_fixed_gain; ///< previous fixed gain; used by anti-sparseness to determine "onset" uint8_t prev_ir_filter_nr; ///< previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none float prev_tr_gain; ///< previous initial gain used by noise enhancer for threshold float samples_az[LP_ORDER + AMRWB_SFR_SIZE]; ///< low-band samples and memory from synthesis at 12.8kHz float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE]; ///< low-band samples and memory processed for upsampling float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k]; ///< high-band samples and memory from synthesis at 16kHz float hpf_31_mem[2], hpf_400_mem[2]; ///< previous values in the high pass filters float demph_mem[1]; ///< previous value in the de-emphasis filter float bpf_6_7_mem[HB_FIR_SIZE]; ///< previous values in the high-band band pass filter float lpf_7_mem[HB_FIR_SIZE]; ///< previous values in the high-band low pass filter AVLFG prng; ///< random number generator for white noise excitation uint8_t first_frame; ///< flag active during decoding of the first frame 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 } AMRWBContext; typedef struct AMRWBChannelsContext { AMRWBContext ch[2];} AMRWBChannelsContext; static av_cold int amrwb_decode_init(AVCodecContext *avctx){ AMRWBChannelsContext *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 = 16000; avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { AMRWBContext *ctx = &s->ch[ch]; av_lfg_init(&ctx->prng, 1); ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1]; ctx->first_frame = 1; for (i = 0; i < LP_ORDER; i++) ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15)); for (i = 0; i < 4; i++) ctx->prediction_error[i] = MIN_ENERGY; ff_acelp_filter_init(&ctx->acelpf_ctx); ff_acelp_vectors_init(&ctx->acelpv_ctx); ff_celp_filter_init(&ctx->celpf_ctx); ff_celp_math_init(&ctx->celpm_ctx); } return 0;} /** * Decode the frame header in the "MIME/storage" format. This format * is simpler and does not carry the auxiliary frame information. * * @param[in] ctx The Context * @param[in] buf Pointer to the input buffer * * @return The decoded header length in bytes */static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf){ /* Decode frame header (1st octet) */ ctx->fr_cur_mode = buf[0] >> 3 & 0x0F; ctx->fr_quality = (buf[0] & 0x4) == 0x4; return 1;} /** * Decode quantized ISF vectors using 36-bit indexes (6K60 mode only). * * @param[in] ind Array of 5 indexes * @param[out] isf_q Buffer for isf_q[LP_ORDER] */static void decode_isf_indices_36b(uint16_t *ind, float *isf_q){ int i; for (i = 0; i < 9; i++) isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15)); for (i = 0; i < 7; i++) isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15)); for (i = 0; i < 5; i++) isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15)); for (i = 0; i < 4; i++) isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15)); for (i = 0; i < 7; i++) isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15));} /** * Decode quantized ISF vectors using 46-bit indexes (except 6K60 mode). * * @param[in] ind Array of 7 indexes * @param[out] isf_q Buffer for isf_q[LP_ORDER] */static void decode_isf_indices_46b(uint16_t *ind, float *isf_q){ int i; for (i = 0; i < 9; i++) isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15)); for (i = 0; i < 7; i++) isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15)); for (i = 0; i < 4; i++) isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15));} /** * Apply mean and past ISF values using the prediction factor. * Updates past ISF vector. * * @param[in,out] isf_q Current quantized ISF * @param[in,out] isf_past Past quantized ISF */static void isf_add_mean_and_past(float *isf_q, float *isf_past){ int i; float tmp; for (i = 0; i < LP_ORDER; i++) { tmp = isf_q[i]; isf_q[i] += isf_mean[i] * (1.0f / (1 << 15)); isf_q[i] += PRED_FACTOR * isf_past[i]; isf_past[i] = tmp; }} /** * Interpolate the fourth ISP vector from current and past frames * to obtain an ISP vector for each subframe. * * @param[in,out] isp_q ISPs for each subframe * @param[in] isp4_past Past ISP for subframe 4 */static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past){ int i, k; for (k = 0; k < 3; k++) { float c = isfp_inter[k]; for (i = 0; i < LP_ORDER; i++) isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i]; }} /** * Decode an adaptive codebook index into pitch lag (except 6k60, 8k85 modes). * Calculate integer lag and fractional lag always using 1/4 resolution. * In 1st and 3rd subframes the index is relative to last subframe integer lag. * * @param[out] lag_int Decoded integer pitch lag * @param[out] lag_frac Decoded fractional pitch lag * @param[in] pitch_index Adaptive codebook pitch index * @param[in,out] base_lag_int Base integer lag used in relative subframes * @param[in] subframe Current subframe index (0 to 3) */static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index, uint8_t *base_lag_int, int subframe){ if (subframe == 0 || subframe == 2) { if (pitch_index < 376) { *lag_int = (pitch_index + 137) >> 2; *lag_frac = pitch_index - (*lag_int << 2) + 136; } else if (pitch_index < 440) { *lag_int = (pitch_index + 257 - 376) >> 1; *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) * 2; /* the actual resolution is 1/2 but expressed as 1/4 */ } else { *lag_int = pitch_index - 280; *lag_frac = 0; } /* minimum lag for next subframe */ *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0), AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15); // XXX: the spec states clearly that *base_lag_int should be // the nearest integer to *lag_int (minus 8), but the ref code // actually always uses its floor, I'm following the latter } else { *lag_int = (pitch_index + 1) >> 2; *lag_frac = pitch_index - (*lag_int << 2); *lag_int += *base_lag_int; }} /** * Decode an adaptive codebook index into pitch lag for 8k85 and 6k60 modes. * The description is analogous to decode_pitch_lag_high, but in 6k60 the * relative index is used for all subframes except the first. */static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index, uint8_t *base_lag_int, int subframe, enum Mode mode){ if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) { if (pitch_index < 116) { *lag_int = (pitch_index + 69) >> 1; *lag_frac = (pitch_index - (*lag_int << 1) + 68) * 2; } else { *lag_int = pitch_index - 24; *lag_frac = 0; } // XXX: same problem as before *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0), AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15); } else { *lag_int = (pitch_index + 1) >> 1; *lag_frac = (pitch_index - (*lag_int << 1)) * 2; *lag_int += *base_lag_int; }} /** * Find the pitch vector by interpolating the past excitation at the * pitch delay, which is obtained in this function. * * @param[in,out] ctx The context * @param[in] amr_subframe Current subframe data * @param[in] subframe Current subframe index (0 to 3) */static void decode_pitch_vector(AMRWBContext *ctx, const AMRWBSubFrame *amr_subframe, const int subframe){ int pitch_lag_int, pitch_lag_frac; int i; float *exc = ctx->excitation; enum Mode mode = ctx->fr_cur_mode; if (mode <= MODE_8k85) { decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap, &ctx->base_pitch_lag, subframe, mode); } else decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap, &ctx->base_pitch_lag, subframe); ctx->pitch_lag_int = pitch_lag_int; pitch_lag_int += pitch_lag_frac > 0; /* Calculate the pitch vector by interpolating the past excitation at the pitch lag using a hamming windowed sinc function */ ctx->acelpf_ctx.acelp_interpolatef(exc, exc + 1 - pitch_lag_int, ac_inter, 4, pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4), LP_ORDER, AMRWB_SFR_SIZE + 1); /* Check which pitch signal path should be used * 6k60 and 8k85 modes have the ltp flag set to 0 */ if (amr_subframe->ltp) { memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float)); } else { for (i = 0; i < AMRWB_SFR_SIZE; i++) ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] + 0.18 * exc[i + 1]; memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float)); }} /** Get x bits in the index interval [lsb,lsb+len-1] inclusive */#define BIT_STR(x,lsb,len) av_zero_extend((x) >> (lsb), (len)) /** Get the bit at specified position */#define BIT_POS(x, p) (((x) >> (p)) & 1) /** * The next six functions decode_[i]p_track decode exactly i pulses * positions and amplitudes (-1 or 1) in a subframe track using * an encoded pulse indexing (TS 26.190 section 5.8.2). * * The results are given in out[], in which a negative number means * amplitude -1 and vice versa (i.e., ampl(x) = x / abs(x) ). * * @param[out] out Output buffer (writes i elements) * @param[in] code Pulse index (no. of bits varies, see below) * @param[in] m (log2) Number of potential positions * @param[in] off Offset for decoded positions */static inline void decode_1p_track(int *out, int code, int m, int off){ int pos = BIT_STR(code, 0, m) + off; ///code: m+1 bits out[0] = BIT_POS(code, m) ? -pos : pos;} static inline void decode_2p_track(int *out, int code, int m, int off) ///code: 2m+1 bits{ int pos0 = BIT_STR(code, m, m) + off; int pos1 = BIT_STR(code, 0, m) + off; out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0; out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1; out[1] = pos0 > pos1 ? -out[1] : out[1];} static void decode_3p_track(int *out, int code, int m, int off) ///code: 3m+1 bits{ int half_2p = BIT_POS(code, 2*m - 1) << (m - 1); decode_2p_track(out, BIT_STR(code, 0, 2*m - 1), m - 1, off + half_2p); decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off);} static void decode_4p_track(int *out, int code, int m, int off) ///code: 4m bits{ int half_4p, subhalf_2p; int b_offset = 1 << (m - 1); switch (BIT_STR(code, 4*m - 2, 2)) { /* case ID (2 bits) */ case 0: /* 0 pulses in A, 4 pulses in B or vice versa */ half_4p = BIT_POS(code, 4*m - 3) << (m - 1); // which has 4 pulses subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2); decode_2p_track(out, BIT_STR(code, 0, 2*m - 3), m - 2, off + half_4p + subhalf_2p); decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1), m - 1, off + half_4p); break; case 1: /* 1 pulse in A, 3 pulses in B */ decode_1p_track(out, BIT_STR(code, 3*m - 2, m), m - 1, off); decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2), m - 1, off + b_offset); break; case 2: /* 2 pulses in each half */ decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1), m - 1, off); decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1), m - 1, off + b_offset); break; case 3: /* 3 pulses in A, 1 pulse in B */ decode_3p_track(out, BIT_STR(code, m, 3*m - 2), m - 1, off); decode_1p_track(out + 3, BIT_STR(code, 0, m), m - 1, off + b_offset); break; }} static void decode_5p_track(int *out, int code, int m, int off) ///code: 5m bits{ int half_3p = BIT_POS(code, 5*m - 1) << (m - 1); decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2), m - 1, off + half_3p); decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off);} static void decode_6p_track(int *out, int code, int m, int off) ///code: 6m-2 bits{ int b_offset = 1 << (m - 1); /* which half has more pulses in cases 0 to 2 */ int half_more = BIT_POS(code, 6*m - 5) << (m - 1); int half_other = b_offset - half_more; switch (BIT_STR(code, 6*m - 4, 2)) { /* case ID (2 bits) */ case 0: /* 0 pulses in A, 6 pulses in B or vice versa */ decode_1p_track(out, BIT_STR(code, 0, m), m - 1, off + half_more); decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5), m - 1, off + half_more); break; case 1: /* 1 pulse in A, 5 pulses in B or vice versa */ decode_1p_track(out, BIT_STR(code, 0, m), m - 1, off + half_other); decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5), m - 1, off + half_more); break; case 2: /* 2 pulses in A, 4 pulses in B or vice versa */ decode_2p_track(out, BIT_STR(code, 0, 2*m - 1), m - 1, off + half_other); decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4), m - 1, off + half_more); break; case 3: /* 3 pulses in A, 3 pulses in B */ decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2), m - 1, off); decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2), m - 1, off + b_offset); break; }} /** * Decode the algebraic codebook index to pulse positions and signs, * then construct the algebraic codebook vector.Lines 1–500 of 1312. Load more
/* * AMR wideband decoder * Copyright (c) 2010 Marcelo Galvao Povoa * * 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 wideband decoder */ #include "config.h" #include "libavutil/avassert.h" #include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/lfg.h" #include "avcodec.h" #include "lsp.h" #include "celp_filters.h" #include "celp_math.h" #include "acelp_filters.h" #include "acelp_vectors.h" #include "acelp_pitch_delay.h" #include "codec_internal.h" #include "decode.h" #define AMR_USE_16BIT_TABLES #include "amr.h" #include "amrwbdata.h" #if ARCH_MIPS #include "mips/amrwbdec_mips.h" #endif /* ARCH_MIPS */ typedef struct AMRWBContext { AMRWBFrame frame; ///< AMRWB parameters decoded from bitstream enum Mode fr_cur_mode; ///< mode index of current frame uint8_t fr_quality; ///< frame quality index (FQI) float isf_cur[LP_ORDER]; ///< working ISF vector from current frame float isf_q_past[LP_ORDER]; ///< quantized ISF vector of the previous frame float isf_past_final[LP_ORDER]; ///< final processed ISF vector of the previous frame double isp[4][LP_ORDER]; ///< ISP vectors from current frame double isp_sub4_past[LP_ORDER]; ///< ISP vector for the 4th subframe of the previous frame float lp_coef[4][LP_ORDER]; ///< Linear Prediction Coefficients from ISP vector uint8_t base_pitch_lag; ///< integer part of pitch lag for the next relative subframe uint8_t pitch_lag_int; ///< integer part of pitch lag of the previous subframe float excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 2 + AMRWB_SFR_SIZE]; ///< current excitation and all necessary excitation history float *excitation; ///< points to current excitation in excitation_buf[] float pitch_vector[AMRWB_SFR_SIZE]; ///< adaptive codebook (pitch) vector for current subframe float fixed_vector[AMRWB_SFR_SIZE]; ///< algebraic codebook (fixed) vector for current subframe float prediction_error[4]; ///< quantified prediction errors {20log10(^gamma_gc)} for previous four subframes float pitch_gain[6]; ///< quantified pitch gains for the current and previous five subframes float fixed_gain[2]; ///< quantified fixed gains for the current and previous subframes float tilt_coef; ///< {beta_1} related to the voicing of the previous subframe float prev_sparse_fixed_gain; ///< previous fixed gain; used by anti-sparseness to determine "onset" uint8_t prev_ir_filter_nr; ///< previous impulse response filter "impNr": 0 - strong, 1 - medium, 2 - none float prev_tr_gain; ///< previous initial gain used by noise enhancer for threshold float samples_az[LP_ORDER + AMRWB_SFR_SIZE]; ///< low-band samples and memory from synthesis at 12.8kHz float samples_up[UPS_MEM_SIZE + AMRWB_SFR_SIZE]; ///< low-band samples and memory processed for upsampling float samples_hb[LP_ORDER_16k + AMRWB_SFR_SIZE_16k]; ///< high-band samples and memory from synthesis at 16kHz float hpf_31_mem[2], hpf_400_mem[2]; ///< previous values in the high pass filters float demph_mem[1]; ///< previous value in the de-emphasis filter float bpf_6_7_mem[HB_FIR_SIZE]; ///< previous values in the high-band band pass filter float lpf_7_mem[HB_FIR_SIZE]; ///< previous values in the high-band low pass filter AVLFG prng; ///< random number generator for white noise excitation uint8_t first_frame; ///< flag active during decoding of the first frame 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 } AMRWBContext; typedef struct AMRWBChannelsContext { AMRWBContext ch[2]; } AMRWBChannelsContext; static av_cold int amrwb_decode_init(AVCodecContext *avctx) { AMRWBChannelsContext *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 = 16000; avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { AMRWBContext *ctx = &s->ch[ch]; av_lfg_init(&ctx->prng, 1); ctx->excitation = &ctx->excitation_buf[AMRWB_P_DELAY_MAX + LP_ORDER + 1]; ctx->first_frame = 1; for (i = 0; i < LP_ORDER; i++) ctx->isf_past_final[i] = isf_init[i] * (1.0f / (1 << 15)); for (i = 0; i < 4; i++) ctx->prediction_error[i] = MIN_ENERGY; ff_acelp_filter_init(&ctx->acelpf_ctx); ff_acelp_vectors_init(&ctx->acelpv_ctx); ff_celp_filter_init(&ctx->celpf_ctx); ff_celp_math_init(&ctx->celpm_ctx); } return 0; } /** * Decode the frame header in the "MIME/storage" format. This format * is simpler and does not carry the auxiliary frame information. * * @param[in] ctx The Context * @param[in] buf Pointer to the input buffer * * @return The decoded header length in bytes */ static int decode_mime_header(AMRWBContext *ctx, const uint8_t *buf) { /* Decode frame header (1st octet) */ ctx->fr_cur_mode = buf[0] >> 3 & 0x0F; ctx->fr_quality = (buf[0] & 0x4) == 0x4; return 1; } /** * Decode quantized ISF vectors using 36-bit indexes (6K60 mode only). * * @param[in] ind Array of 5 indexes * @param[out] isf_q Buffer for isf_q[LP_ORDER] */ static void decode_isf_indices_36b(uint16_t *ind, float *isf_q) { int i; for (i = 0; i < 9; i++) isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15)); for (i = 0; i < 7; i++) isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15)); for (i = 0; i < 5; i++) isf_q[i] += dico21_isf_36b[ind[2]][i] * (1.0f / (1 << 15)); for (i = 0; i < 4; i++) isf_q[i + 5] += dico22_isf_36b[ind[3]][i] * (1.0f / (1 << 15)); for (i = 0; i < 7; i++) isf_q[i + 9] += dico23_isf_36b[ind[4]][i] * (1.0f / (1 << 15)); } /** * Decode quantized ISF vectors using 46-bit indexes (except 6K60 mode). * * @param[in] ind Array of 7 indexes * @param[out] isf_q Buffer for isf_q[LP_ORDER] */ static void decode_isf_indices_46b(uint16_t *ind, float *isf_q) { int i; for (i = 0; i < 9; i++) isf_q[i] = dico1_isf[ind[0]][i] * (1.0f / (1 << 15)); for (i = 0; i < 7; i++) isf_q[i + 9] = dico2_isf[ind[1]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i] += dico21_isf[ind[2]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i + 3] += dico22_isf[ind[3]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i + 6] += dico23_isf[ind[4]][i] * (1.0f / (1 << 15)); for (i = 0; i < 3; i++) isf_q[i + 9] += dico24_isf[ind[5]][i] * (1.0f / (1 << 15)); for (i = 0; i < 4; i++) isf_q[i + 12] += dico25_isf[ind[6]][i] * (1.0f / (1 << 15)); } /** * Apply mean and past ISF values using the prediction factor. * Updates past ISF vector. * * @param[in,out] isf_q Current quantized ISF * @param[in,out] isf_past Past quantized ISF */ static void isf_add_mean_and_past(float *isf_q, float *isf_past) { int i; float tmp; for (i = 0; i < LP_ORDER; i++) { tmp = isf_q[i]; isf_q[i] += isf_mean[i] * (1.0f / (1 << 15)); isf_q[i] += PRED_FACTOR * isf_past[i]; isf_past[i] = tmp; } } /** * Interpolate the fourth ISP vector from current and past frames * to obtain an ISP vector for each subframe. * * @param[in,out] isp_q ISPs for each subframe * @param[in] isp4_past Past ISP for subframe 4 */ static void interpolate_isp(double isp_q[4][LP_ORDER], const double *isp4_past) { int i, k; for (k = 0; k < 3; k++) { float c = isfp_inter[k]; for (i = 0; i < LP_ORDER; i++) isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i]; } } /** * Decode an adaptive codebook index into pitch lag (except 6k60, 8k85 modes). * Calculate integer lag and fractional lag always using 1/4 resolution. * In 1st and 3rd subframes the index is relative to last subframe integer lag. * * @param[out] lag_int Decoded integer pitch lag * @param[out] lag_frac Decoded fractional pitch lag * @param[in] pitch_index Adaptive codebook pitch index * @param[in,out] base_lag_int Base integer lag used in relative subframes * @param[in] subframe Current subframe index (0 to 3) */ static void decode_pitch_lag_high(int *lag_int, int *lag_frac, int pitch_index, uint8_t *base_lag_int, int subframe) { if (subframe == 0 || subframe == 2) { if (pitch_index < 376) { *lag_int = (pitch_index + 137) >> 2; *lag_frac = pitch_index - (*lag_int << 2) + 136; } else if (pitch_index < 440) { *lag_int = (pitch_index + 257 - 376) >> 1; *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) * 2; /* the actual resolution is 1/2 but expressed as 1/4 */ } else { *lag_int = pitch_index - 280; *lag_frac = 0; } /* minimum lag for next subframe */ *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0), AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15); // XXX: the spec states clearly that *base_lag_int should be // the nearest integer to *lag_int (minus 8), but the ref code // actually always uses its floor, I'm following the latter } else { *lag_int = (pitch_index + 1) >> 2; *lag_frac = pitch_index - (*lag_int << 2); *lag_int += *base_lag_int; } } /** * Decode an adaptive codebook index into pitch lag for 8k85 and 6k60 modes. * The description is analogous to decode_pitch_lag_high, but in 6k60 the * relative index is used for all subframes except the first. */ static void decode_pitch_lag_low(int *lag_int, int *lag_frac, int pitch_index, uint8_t *base_lag_int, int subframe, enum Mode mode) { if (subframe == 0 || (subframe == 2 && mode != MODE_6k60)) { if (pitch_index < 116) { *lag_int = (pitch_index + 69) >> 1; *lag_frac = (pitch_index - (*lag_int << 1) + 68) * 2; } else { *lag_int = pitch_index - 24; *lag_frac = 0; } // XXX: same problem as before *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0), AMRWB_P_DELAY_MIN, AMRWB_P_DELAY_MAX - 15); } else { *lag_int = (pitch_index + 1) >> 1; *lag_frac = (pitch_index - (*lag_int << 1)) * 2; *lag_int += *base_lag_int; } } /** * Find the pitch vector by interpolating the past excitation at the * pitch delay, which is obtained in this function. * * @param[in,out] ctx The context * @param[in] amr_subframe Current subframe data * @param[in] subframe Current subframe index (0 to 3) */ static void decode_pitch_vector(AMRWBContext *ctx, const AMRWBSubFrame *amr_subframe, const int subframe) { int pitch_lag_int, pitch_lag_frac; int i; float *exc = ctx->excitation; enum Mode mode = ctx->fr_cur_mode; if (mode <= MODE_8k85) { decode_pitch_lag_low(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap, &ctx->base_pitch_lag, subframe, mode); } else decode_pitch_lag_high(&pitch_lag_int, &pitch_lag_frac, amr_subframe->adap, &ctx->base_pitch_lag, subframe); ctx->pitch_lag_int = pitch_lag_int; pitch_lag_int += pitch_lag_frac > 0; /* Calculate the pitch vector by interpolating the past excitation at the pitch lag using a hamming windowed sinc function */ ctx->acelpf_ctx.acelp_interpolatef(exc, exc + 1 - pitch_lag_int, ac_inter, 4, pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4), LP_ORDER, AMRWB_SFR_SIZE + 1); /* Check which pitch signal path should be used * 6k60 and 8k85 modes have the ltp flag set to 0 */ if (amr_subframe->ltp) { memcpy(ctx->pitch_vector, exc, AMRWB_SFR_SIZE * sizeof(float)); } else { for (i = 0; i < AMRWB_SFR_SIZE; i++) ctx->pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] + 0.18 * exc[i + 1]; memcpy(exc, ctx->pitch_vector, AMRWB_SFR_SIZE * sizeof(float)); } } /** Get x bits in the index interval [lsb,lsb+len-1] inclusive */ #define BIT_STR(x,lsb,len) av_zero_extend((x) >> (lsb), (len)) /** Get the bit at specified position */ #define BIT_POS(x, p) (((x) >> (p)) & 1) /** * The next six functions decode_[i]p_track decode exactly i pulses * positions and amplitudes (-1 or 1) in a subframe track using * an encoded pulse indexing (TS 26.190 section 5.8.2). * * The results are given in out[], in which a negative number means * amplitude -1 and vice versa (i.e., ampl(x) = x / abs(x) ). * * @param[out] out Output buffer (writes i elements) * @param[in] code Pulse index (no. of bits varies, see below) * @param[in] m (log2) Number of potential positions * @param[in] off Offset for decoded positions */ static inline void decode_1p_track(int *out, int code, int m, int off) { int pos = BIT_STR(code, 0, m) + off; ///code: m+1 bits out[0] = BIT_POS(code, m) ? -pos : pos; } static inline void decode_2p_track(int *out, int code, int m, int off) ///code: 2m+1 bits { int pos0 = BIT_STR(code, m, m) + off; int pos1 = BIT_STR(code, 0, m) + off; out[0] = BIT_POS(code, 2*m) ? -pos0 : pos0; out[1] = BIT_POS(code, 2*m) ? -pos1 : pos1; out[1] = pos0 > pos1 ? -out[1] : out[1]; } static void decode_3p_track(int *out, int code, int m, int off) ///code: 3m+1 bits { int half_2p = BIT_POS(code, 2*m - 1) << (m - 1); decode_2p_track(out, BIT_STR(code, 0, 2*m - 1), m - 1, off + half_2p); decode_1p_track(out + 2, BIT_STR(code, 2*m, m + 1), m, off); } static void decode_4p_track(int *out, int code, int m, int off) ///code: 4m bits { int half_4p, subhalf_2p; int b_offset = 1 << (m - 1); switch (BIT_STR(code, 4*m - 2, 2)) { /* case ID (2 bits) */ case 0: /* 0 pulses in A, 4 pulses in B or vice versa */ half_4p = BIT_POS(code, 4*m - 3) << (m - 1); // which has 4 pulses subhalf_2p = BIT_POS(code, 2*m - 3) << (m - 2); decode_2p_track(out, BIT_STR(code, 0, 2*m - 3), m - 2, off + half_4p + subhalf_2p); decode_2p_track(out + 2, BIT_STR(code, 2*m - 2, 2*m - 1), m - 1, off + half_4p); break; case 1: /* 1 pulse in A, 3 pulses in B */ decode_1p_track(out, BIT_STR(code, 3*m - 2, m), m - 1, off); decode_3p_track(out + 1, BIT_STR(code, 0, 3*m - 2), m - 1, off + b_offset); break; case 2: /* 2 pulses in each half */ decode_2p_track(out, BIT_STR(code, 2*m - 1, 2*m - 1), m - 1, off); decode_2p_track(out + 2, BIT_STR(code, 0, 2*m - 1), m - 1, off + b_offset); break; case 3: /* 3 pulses in A, 1 pulse in B */ decode_3p_track(out, BIT_STR(code, m, 3*m - 2), m - 1, off); decode_1p_track(out + 3, BIT_STR(code, 0, m), m - 1, off + b_offset); break; } } static void decode_5p_track(int *out, int code, int m, int off) ///code: 5m bits { int half_3p = BIT_POS(code, 5*m - 1) << (m - 1); decode_3p_track(out, BIT_STR(code, 2*m + 1, 3*m - 2), m - 1, off + half_3p); decode_2p_track(out + 3, BIT_STR(code, 0, 2*m + 1), m, off); } static void decode_6p_track(int *out, int code, int m, int off) ///code: 6m-2 bits { int b_offset = 1 << (m - 1); /* which half has more pulses in cases 0 to 2 */ int half_more = BIT_POS(code, 6*m - 5) << (m - 1); int half_other = b_offset - half_more; switch (BIT_STR(code, 6*m - 4, 2)) { /* case ID (2 bits) */ case 0: /* 0 pulses in A, 6 pulses in B or vice versa */ decode_1p_track(out, BIT_STR(code, 0, m), m - 1, off + half_more); decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5), m - 1, off + half_more); break; case 1: /* 1 pulse in A, 5 pulses in B or vice versa */ decode_1p_track(out, BIT_STR(code, 0, m), m - 1, off + half_other); decode_5p_track(out + 1, BIT_STR(code, m, 5*m - 5), m - 1, off + half_more); break; case 2: /* 2 pulses in A, 4 pulses in B or vice versa */ decode_2p_track(out, BIT_STR(code, 0, 2*m - 1), m - 1, off + half_other); decode_4p_track(out + 2, BIT_STR(code, 2*m - 1, 4*m - 4), m - 1, off + half_more); break; case 3: /* 3 pulses in A, 3 pulses in B */ decode_3p_track(out, BIT_STR(code, 3*m - 2, 3*m - 2), m - 1, off); decode_3p_track(out + 3, BIT_STR(code, 0, 3*m - 2), m - 1, off + b_offset); break; } } /** * Decode the algebraic codebook index to pulse positions and signs, * then construct the algebraic codebook vector.