00001 /* 00002 * gain code, gain pitch and pitch delay decoding 00003 * 00004 * Copyright (c) 2008 Vladimir Voroshilov 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #ifndef FFMPEG_ACELP_PITCH_DELAY_H 00024 #define FFMPEG_ACELP_PITCH_DELAY_H 00025 00026 #include <stdint.h> 00027 00028 #define PITCH_DELAY_MIN 20 00029 #define PITCH_DELAY_MAX 143 00030 00031 /** 00032 * \brief Decode pitch delay of the first subframe encoded by 8 bits with 1/3 00033 * resolution. 00034 * \param ac_index adaptive codebook index (8 bits) 00035 * 00036 * \return pitch delay in 1/3 units 00037 * 00038 * Pitch delay is coded: 00039 * with 1/3 resolution, 19 < pitch_delay < 85 00040 * integers only, 85 <= pitch_delay <= 143 00041 */ 00042 int ff_acelp_decode_8bit_to_1st_delay3(int ac_index); 00043 00044 /** 00045 * \brief Decode pitch delay of the second subframe encoded by 5 or 6 bits 00046 * with 1/3 precision. 00047 * \param ac_index adaptive codebook index (5 or 6 bits) 00048 * \param pitch_delay_min lower bound (integer) of pitch delay interval 00049 * for second subframe 00050 * 00051 * \return pitch delay in 1/3 units 00052 * 00053 * Pitch delay is coded: 00054 * with 1/3 resolution, -6 < pitch_delay - int(prev_pitch_delay) < 5 00055 * 00056 * \remark The routine is used in G.729 @8k, AMR @10.2k, AMR @7.95k, 00057 * AMR @7.4k for the second subframe. 00058 */ 00059 int ff_acelp_decode_5_6_bit_to_2nd_delay3( 00060 int ac_index, 00061 int pitch_delay_min); 00062 00063 /** 00064 * \brief Decode pitch delay with 1/3 precision. 00065 * \param ac_index adaptive codebook index (4 bits) 00066 * \param pitch_delay_min lower bound (integer) of pitch delay interval for 00067 * second subframe 00068 * 00069 * \return pitch delay in 1/3 units 00070 * 00071 * Pitch delay is coded: 00072 * integers only, -6 < pitch_delay - int(prev_pitch_delay) <= -2 00073 * with 1/3 resolution, -2 < pitch_delay - int(prev_pitch_delay) < 1 00074 * integers only, 1 <= pitch_delay - int(prev_pitch_delay) < 5 00075 * 00076 * \remark The routine is used in G.729 @6.4k, AMR @6.7k, AMR @5.9k, 00077 * AMR @5.15k, AMR @4.75k for the second subframe. 00078 */ 00079 int ff_acelp_decode_4bit_to_2nd_delay3( 00080 int ac_index, 00081 int pitch_delay_min); 00082 00083 /** 00084 * \brief Decode pitch delay of the first subframe encoded by 9 bits 00085 * with 1/6 precision. 00086 * \param ac_index adaptive codebook index (9 bits) 00087 * \param pitch_delay_min lower bound (integer) of pitch delay interval for 00088 * second subframe 00089 * 00090 * \return pitch delay in 1/6 units 00091 * 00092 * Pitch delay is coded: 00093 * with 1/6 resolution, 17 < pitch_delay < 95 00094 * integers only, 95 <= pitch_delay <= 143 00095 * 00096 * \remark The routine is used in AMR @12.2k for the first and third subframes. 00097 */ 00098 int ff_acelp_decode_9bit_to_1st_delay6(int ac_index); 00099 00100 /** 00101 * \brief Decode pitch delay of the second subframe encoded by 6 bits 00102 * with 1/6 precision. 00103 * \param ac_index adaptive codebook index (6 bits) 00104 * \param pitch_delay_min lower bound (integer) of pitch delay interval for 00105 * second subframe 00106 * 00107 * \return pitch delay in 1/6 units 00108 * 00109 * Pitch delay is coded: 00110 * with 1/6 resolution, -6 < pitch_delay - int(prev_pitch_delay) < 5 00111 * 00112 * \remark The routine is used in AMR @12.2k for the second and fourth subframes. 00113 */ 00114 int ff_acelp_decode_6bit_to_2nd_delay6( 00115 int ac_index, 00116 int pitch_delay_min); 00117 00118 /** 00119 * \brief Update past quantized energies 00120 * \param quant_energy [in/out] past quantized energies (5.10) 00121 * \param gain_corr_factor gain correction factor 00122 * \param log2_ma_pred_order log2() of MA prediction order 00123 * \param erasure frame erasure flag 00124 * 00125 * If frame erasure flag is not equal to zero, memory is updated with 00126 * averaged energy, attenuated by 4dB: 00127 * max(avg(quant_energy[i])-4, -14), i=0,ma_pred_order 00128 * 00129 * In normal mode memory is updated with 00130 * Er - Ep = 20 * log10(gain_corr_factor) 00131 * 00132 * \remark The routine is used in G.729 and AMR (all modes). 00133 */ 00134 void ff_acelp_update_past_gain( 00135 int16_t* quant_energy, 00136 int gain_corr_factor, 00137 int log2_ma_pred_order, 00138 int erasure); 00139 00140 /** 00141 * \brief Decode the adaptive codebook gain and add 00142 * correction (4.1.5 and 3.9.1 of G.729). 00143 * \param gain_corr_factor gain correction factor (2.13) 00144 * \param fc_v fixed-codebook vector (2.13) 00145 * \param mr_energy mean innovation energy and fixed-point correction (7.13) 00146 * \param quant_energy [in/out] past quantized energies (5.10) 00147 * \param subframe_size length of subframe 00148 * \param ma_pred_order MA prediction order 00149 * 00150 * \return quantized fixed-codebook gain (14.1) 00151 * 00152 * The routine implements equations 69, 66 and 71 of the G.729 specification (3.9.1) 00153 * 00154 * Em - mean innovation energy (dB, constant, depends on decoding algorithm) 00155 * Ep - mean-removed predicted energy (dB) 00156 * Er - mean-removed innovation energy (dB) 00157 * Ei - mean energy of the fixed-codebook contribution (dB) 00158 * N - subframe_size 00159 * M - MA (Moving Average) prediction order 00160 * gc - fixed-codebook gain 00161 * gc_p - predicted fixed-codebook gain 00162 * 00163 * Fixed codebook gain is computed using predicted gain gc_p and 00164 * correction factor gain_corr_factor as shown below: 00165 * 00166 * gc = gc_p * gain_corr_factor 00167 * 00168 * The predicted fixed codebook gain gc_p is found by predicting 00169 * the energy of the fixed-codebook contribution from the energy 00170 * of previous fixed-codebook contributions. 00171 * 00172 * mean = 1/N * sum(i,0,N){ fc_v[i] * fc_v[i] } 00173 * 00174 * Ei = 10log(mean) 00175 * 00176 * Er = 10log(1/N * gc^2 * mean) - Em = 20log(gc) + Ei - Em 00177 * 00178 * Replacing Er with Ep and gc with gc_p we will receive: 00179 * 00180 * Ep = 10log(1/N * gc_p^2 * mean) - Em = 20log(gc_p) + Ei - Em 00181 * 00182 * and from above: 00183 * 00184 * gc_p = 10^((Ep - Ei + Em) / 20) 00185 * 00186 * Ep is predicted using past energies and prediction coefficients: 00187 * 00188 * Ep = sum(i,0,M){ ma_prediction_coeff[i] * quant_energy[i] } 00189 * 00190 * gc_p in fixed-point arithmetic is calculated as following: 00191 * 00192 * mean = 1/N * sum(i,0,N){ (fc_v[i] / 2^13) * (fc_v[i] / 2^13) } = 00193 * = 1/N * sum(i,0,N) { fc_v[i] * fc_v[i] } / 2^26 00194 * 00195 * Ei = 10log(mean) = -10log(N) - 10log(2^26) + 00196 * + 10log(sum(i,0,N) { fc_v[i] * fc_v[i] }) 00197 * 00198 * Ep - Ei + Em = Ep + Em + 10log(N) + 10log(2^26) - 00199 * - 10log(sum(i,0,N) { fc_v[i] * fc_v[i] }) = 00200 * = Ep + mr_energy - 10log(sum(i,0,N) { fc_v[i] * fc_v[i] }) 00201 * 00202 * gc_p = 10 ^ ((Ep - Ei + Em) / 20) = 00203 * = 2 ^ (3.3219 * (Ep - Ei + Em) / 20) = 2 ^ (0.166 * (Ep - Ei + Em)) 00204 * 00205 * where 00206 * 00207 * mr_energy = Em + 10log(N) + 10log(2^26) 00208 * 00209 * \remark The routine is used in G.729 and AMR (all modes). 00210 */ 00211 int16_t ff_acelp_decode_gain_code( 00212 int gain_corr_factor, 00213 const int16_t* fc_v, 00214 int mr_energy, 00215 const int16_t* quant_energy, 00216 const int16_t* ma_prediction_coeff, 00217 int subframe_size, 00218 int max_pred_order); 00219 00220 #endif /* FFMPEG_ACELP_PITCH_DELAY_H */
1.5.1