Use tables symetry to reduce their size by half.
[ffmpeg-lucabe.git] / libavcodec / acelp_pitch_delay.c
blob0746588a22fd7efa23f5c06519a7505cc4dc7c8d
1 /*
2 * gain code, gain pitch and pitch delay decoding
4 * Copyright (c) 2008 Vladimir Voroshilov
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "avcodec.h"
24 #include "acelp_pitch_delay.h"
25 #include "acelp_math.h"
27 int ff_acelp_decode_8bit_to_1st_delay3(int ac_index)
29 ac_index += 58;
30 if(ac_index > 254)
31 ac_index = 3 * ac_index - 510;
32 return ac_index;
35 int ff_acelp_decode_4bit_to_2nd_delay3(
36 int ac_index,
37 int pitch_delay_min)
39 if(ac_index < 4)
40 return 3 * (ac_index + pitch_delay_min);
41 else if(ac_index < 12)
42 return 3 * pitch_delay_min + ac_index + 6;
43 else
44 return 3 * (ac_index + pitch_delay_min) - 18;
47 int ff_acelp_decode_5_6_bit_to_2nd_delay3(
48 int ac_index,
49 int pitch_delay_min)
51 return 3 * pitch_delay_min + ac_index - 2;
54 int ff_acelp_decode_9bit_to_1st_delay6(int ac_index)
56 if(ac_index < 463)
57 return ac_index + 105;
58 else
59 return 6 * (ac_index - 368);
61 int ff_acelp_decode_6bit_to_2nd_delay6(
62 int ac_index,
63 int pitch_delay_min)
65 return 6 * pitch_delay_min + ac_index - 3;
68 void ff_acelp_update_past_gain(
69 int16_t* quant_energy,
70 int gain_corr_factor,
71 int log2_ma_pred_order,
72 int erasure)
74 int i;
75 int avg_gain=quant_energy[(1 << log2_ma_pred_order) - 1]; // (5.10)
77 for(i=(1 << log2_ma_pred_order) - 1; i>0; i--)
79 avg_gain += quant_energy[i-1];
80 quant_energy[i] = quant_energy[i-1];
83 if(erasure)
84 quant_energy[0] = FFMAX(avg_gain >> log2_ma_pred_order, -10240) - 4096; // -10 and -4 in (5.10)
85 else
86 quant_energy[0] = (6165 * ((ff_log2(gain_corr_factor) >> 2) - (13 << 13))) >> 13;
89 int16_t ff_acelp_decode_gain_code(
90 int gain_corr_factor,
91 const int16_t* fc_v,
92 int mr_energy,
93 const int16_t* quant_energy,
94 const int16_t* ma_prediction_coeff,
95 int subframe_size,
96 int ma_pred_order)
98 int i;
100 mr_energy <<= 10;
102 for(i=0; i<ma_pred_order; i++)
103 mr_energy += quant_energy[i] * ma_prediction_coeff[i];
105 #ifdef G729_BITEXACT
106 mr_energy += (((-6165LL * ff_log2(dot_product(fc_v, fc_v, subframe_size, 0))) >> 3) & ~0x3ff);
108 mr_energy = (5439 * (mr_energy >> 15)) >> 8; // (0.15) = (0.15) * (7.23)
110 return bidir_sal(
111 ((ff_exp2(mr_energy & 0x7fff) + 16) >> 5) * (gain_corr_factor >> 1),
112 (mr_energy >> 15) - 25
114 #else
115 mr_energy = gain_corr_factor * exp(M_LN10 / (20 << 23) * mr_energy) /
116 sqrt(dot_product(fc_v, fc_v, subframe_size, 0));
117 return mr_energy >> 12;
118 #endif