You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

720 lines
25KB

  1. /*
  2. * G.729, G729 Annex D decoders
  3. * Copyright (c) 2008 Vladimir Voroshilov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <inttypes.h>
  22. #include <string.h>
  23. #include "avcodec.h"
  24. #include "libavutil/avutil.h"
  25. #include "get_bits.h"
  26. #include "dsputil.h"
  27. #include "g729.h"
  28. #include "lsp.h"
  29. #include "celp_math.h"
  30. #include "celp_filters.h"
  31. #include "acelp_filters.h"
  32. #include "acelp_pitch_delay.h"
  33. #include "acelp_vectors.h"
  34. #include "g729data.h"
  35. #include "g729postfilter.h"
  36. /**
  37. * minimum quantized LSF value (3.2.4)
  38. * 0.005 in Q13
  39. */
  40. #define LSFQ_MIN 40
  41. /**
  42. * maximum quantized LSF value (3.2.4)
  43. * 3.135 in Q13
  44. */
  45. #define LSFQ_MAX 25681
  46. /**
  47. * minimum LSF distance (3.2.4)
  48. * 0.0391 in Q13
  49. */
  50. #define LSFQ_DIFF_MIN 321
  51. /// interpolation filter length
  52. #define INTERPOL_LEN 11
  53. /**
  54. * minimum gain pitch value (3.8, Equation 47)
  55. * 0.2 in (1.14)
  56. */
  57. #define SHARP_MIN 3277
  58. /**
  59. * maximum gain pitch value (3.8, Equation 47)
  60. * (EE) This does not comply with the specification.
  61. * Specification says about 0.8, which should be
  62. * 13107 in (1.14), but reference C code uses
  63. * 13017 (equals to 0.7945) instead of it.
  64. */
  65. #define SHARP_MAX 13017
  66. /**
  67. * MR_ENERGY (mean removed energy) = mean_energy + 10 * log10(2^26 * subframe_size) in (7.13)
  68. */
  69. #define MR_ENERGY 1018156
  70. #define DECISION_NOISE 0
  71. #define DECISION_INTERMEDIATE 1
  72. #define DECISION_VOICE 2
  73. typedef enum {
  74. FORMAT_G729_8K = 0,
  75. FORMAT_G729D_6K4,
  76. FORMAT_COUNT,
  77. } G729Formats;
  78. typedef struct {
  79. uint8_t ac_index_bits[2]; ///< adaptive codebook index for second subframe (size in bits)
  80. uint8_t parity_bit; ///< parity bit for pitch delay
  81. uint8_t gc_1st_index_bits; ///< gain codebook (first stage) index (size in bits)
  82. uint8_t gc_2nd_index_bits; ///< gain codebook (second stage) index (size in bits)
  83. uint8_t fc_signs_bits; ///< number of pulses in fixed-codebook vector
  84. uint8_t fc_indexes_bits; ///< size (in bits) of fixed-codebook index entry
  85. } G729FormatDescription;
  86. typedef struct {
  87. DSPContext dsp;
  88. /// past excitation signal buffer
  89. int16_t exc_base[2*SUBFRAME_SIZE+PITCH_DELAY_MAX+INTERPOL_LEN];
  90. int16_t* exc; ///< start of past excitation data in buffer
  91. int pitch_delay_int_prev; ///< integer part of previous subframe's pitch delay (4.1.3)
  92. /// (2.13) LSP quantizer outputs
  93. int16_t past_quantizer_output_buf[MA_NP + 1][10];
  94. int16_t* past_quantizer_outputs[MA_NP + 1];
  95. int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame
  96. int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
  97. int16_t *lsp[2]; ///< pointers to lsp_buf
  98. int16_t quant_energy[4]; ///< (5.10) past quantized energy
  99. /// previous speech data for LP synthesis filter
  100. int16_t syn_filter_data[10];
  101. /// residual signal buffer (used in long-term postfilter)
  102. int16_t residual[SUBFRAME_SIZE + RES_PREV_DATA_SIZE];
  103. /// previous speech data for residual calculation filter
  104. int16_t res_filter_data[SUBFRAME_SIZE+10];
  105. /// previous speech data for short-term postfilter
  106. int16_t pos_filter_data[SUBFRAME_SIZE+10];
  107. /// (1.14) pitch gain of current and five previous subframes
  108. int16_t past_gain_pitch[6];
  109. /// (14.1) gain code from current and previous subframe
  110. int16_t past_gain_code[2];
  111. /// voice decision on previous subframe (0-noise, 1-intermediate, 2-voice), G.729D
  112. int16_t voice_decision;
  113. int16_t onset; ///< detected onset level (0-2)
  114. int16_t was_periodic; ///< whether previous frame was declared as periodic or not (4.4)
  115. int16_t ht_prev_data; ///< previous data for 4.2.3, equation 86
  116. int gain_coeff; ///< (1.14) gain coefficient (4.2.4)
  117. uint16_t rand_value; ///< random number generator value (4.4.4)
  118. int ma_predictor_prev; ///< switched MA predictor of LSP quantizer from last good frame
  119. /// (14.14) high-pass filter data (past input)
  120. int hpf_f[2];
  121. /// high-pass filter data (past output)
  122. int16_t hpf_z[2];
  123. } G729Context;
  124. static const G729FormatDescription format_g729_8k = {
  125. .ac_index_bits = {8,5},
  126. .parity_bit = 1,
  127. .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
  128. .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
  129. .fc_signs_bits = 4,
  130. .fc_indexes_bits = 13,
  131. };
  132. static const G729FormatDescription format_g729d_6k4 = {
  133. .ac_index_bits = {8,4},
  134. .parity_bit = 0,
  135. .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
  136. .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
  137. .fc_signs_bits = 2,
  138. .fc_indexes_bits = 9,
  139. };
  140. /**
  141. * @brief pseudo random number generator
  142. */
  143. static inline uint16_t g729_prng(uint16_t value)
  144. {
  145. return 31821 * value + 13849;
  146. }
  147. /**
  148. * Get parity bit of bit 2..7
  149. */
  150. static inline int get_parity(uint8_t value)
  151. {
  152. return (0x6996966996696996ULL >> (value >> 2)) & 1;
  153. }
  154. /*
  155. * Decodes LSF (Line Spectral Frequencies) from L0-L3 (3.2.4).
  156. * @param lsfq [out] (2.13) quantized LSF coefficients
  157. * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames
  158. * @param ma_predictor switched MA predictor of LSP quantizer
  159. * @param vq_1st first stage vector of quantizer
  160. * @param vq_2nd_low second stage lower vector of LSP quantizer
  161. * @param vq_2nd_high second stage higher vector of LSP quantizer
  162. */
  163. static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
  164. int16_t ma_predictor,
  165. int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
  166. {
  167. int i,j;
  168. static const uint8_t min_distance[2]={10, 5}; //(2.13)
  169. int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
  170. for (i = 0; i < 5; i++) {
  171. quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
  172. quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
  173. }
  174. for (j = 0; j < 2; j++) {
  175. for (i = 1; i < 10; i++) {
  176. int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
  177. if (diff > 0) {
  178. quantizer_output[i - 1] -= diff;
  179. quantizer_output[i ] += diff;
  180. }
  181. }
  182. }
  183. for (i = 0; i < 10; i++) {
  184. int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
  185. for (j = 0; j < MA_NP; j++)
  186. sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
  187. lsfq[i] = sum >> 15;
  188. }
  189. ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
  190. }
  191. /**
  192. * Restores past LSP quantizer output using LSF from previous frame
  193. * @param lsfq [in/out] (2.13) quantized LSF coefficients
  194. * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames
  195. * @param ma_predictor_prev MA predictor from previous frame
  196. * @param lsfq_prev (2.13) quantized LSF coefficients from previous frame
  197. */
  198. static void lsf_restore_from_previous(int16_t* lsfq,
  199. int16_t* past_quantizer_outputs[MA_NP + 1],
  200. int ma_predictor_prev)
  201. {
  202. int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
  203. int i,k;
  204. for (i = 0; i < 10; i++) {
  205. int tmp = lsfq[i] << 15;
  206. for (k = 0; k < MA_NP; k++)
  207. tmp -= past_quantizer_outputs[k][i] * cb_ma_predictor[ma_predictor_prev][k][i];
  208. quantizer_output[i] = ((tmp >> 15) * cb_ma_predictor_sum_inv[ma_predictor_prev][i]) >> 12;
  209. }
  210. }
  211. /**
  212. * Constructs new excitation signal and applies phase filter to it
  213. * @param out[out] constructed speech signal
  214. * @param in original excitation signal
  215. * @param fc_cur (2.13) original fixed-codebook vector
  216. * @param gain_code (14.1) gain code
  217. * @param subframe_size length of the subframe
  218. */
  219. static void g729d_get_new_exc(
  220. int16_t* out,
  221. const int16_t* in,
  222. const int16_t* fc_cur,
  223. int dstate,
  224. int gain_code,
  225. int subframe_size)
  226. {
  227. int i;
  228. int16_t fc_new[SUBFRAME_SIZE];
  229. ff_celp_convolve_circ(fc_new, fc_cur, phase_filter[dstate], subframe_size);
  230. for(i=0; i<subframe_size; i++)
  231. {
  232. out[i] = in[i];
  233. out[i] -= (gain_code * fc_cur[i] + 0x2000) >> 14;
  234. out[i] += (gain_code * fc_new[i] + 0x2000) >> 14;
  235. }
  236. }
  237. /**
  238. * Makes decision about onset in current subframe
  239. * @param past_onset decision result of previous subframe
  240. * @param past_gain_code gain code of current and previous subframe
  241. *
  242. * @return onset decision result for current subframe
  243. */
  244. static int g729d_onset_decision(int past_onset, const int16_t* past_gain_code)
  245. {
  246. if((past_gain_code[0] >> 1) > past_gain_code[1])
  247. return 2;
  248. else
  249. return FFMAX(past_onset-1, 0);
  250. }
  251. /**
  252. * Makes decision about voice presence in current subframe
  253. * @param onset onset level
  254. * @param prev_voice_decision voice decision result from previous subframe
  255. * @param past_gain_pitch pitch gain of current and previous subframes
  256. *
  257. * @return voice decision result for current subframe
  258. */
  259. static int16_t g729d_voice_decision(int onset, int prev_voice_decision, const int16_t* past_gain_pitch)
  260. {
  261. int i, low_gain_pitch_cnt, voice_decision;
  262. if(past_gain_pitch[0] >= 14745) // 0.9
  263. voice_decision = DECISION_VOICE;
  264. else if (past_gain_pitch[0] <= 9830) // 0.6
  265. voice_decision = DECISION_NOISE;
  266. else
  267. voice_decision = DECISION_INTERMEDIATE;
  268. for(i=0, low_gain_pitch_cnt=0; i<6; i++)
  269. if(past_gain_pitch[i] < 9830)
  270. low_gain_pitch_cnt++;
  271. if(low_gain_pitch_cnt > 2 && !onset)
  272. voice_decision = DECISION_NOISE;
  273. if(!onset && voice_decision > prev_voice_decision + 1)
  274. voice_decision--;
  275. if(onset && voice_decision < DECISION_VOICE)
  276. voice_decision++;
  277. return voice_decision;
  278. }
  279. static int32_t scalarproduct_int16_c(const int16_t * v1, const int16_t * v2, int order, int shift)
  280. {
  281. int res = 0;
  282. while (order--)
  283. res += (*v1++ * *v2++) >> shift;
  284. return res;
  285. }
  286. static av_cold int decoder_init(AVCodecContext * avctx)
  287. {
  288. G729Context* ctx = avctx->priv_data;
  289. int i,k;
  290. if (avctx->channels != 1) {
  291. av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
  292. return AVERROR(EINVAL);
  293. }
  294. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  295. /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
  296. avctx->frame_size = SUBFRAME_SIZE << 1;
  297. ctx->gain_coeff = 16384; // 1.0 in (1.14)
  298. for (k = 0; k < MA_NP + 1; k++) {
  299. ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
  300. for (i = 1; i < 11; i++)
  301. ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
  302. }
  303. ctx->lsp[0] = ctx->lsp_buf[0];
  304. ctx->lsp[1] = ctx->lsp_buf[1];
  305. memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
  306. ctx->exc = &ctx->exc_base[PITCH_DELAY_MAX+INTERPOL_LEN];
  307. /* random seed initialization */
  308. ctx->rand_value = 21845;
  309. /* quantized prediction error */
  310. for(i=0; i<4; i++)
  311. ctx->quant_energy[i] = -14336; // -14 in (5.10)
  312. dsputil_init(&ctx->dsp, avctx);
  313. ctx->dsp.scalarproduct_int16 = scalarproduct_int16_c;
  314. return 0;
  315. }
  316. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  317. AVPacket *avpkt)
  318. {
  319. const uint8_t *buf = avpkt->data;
  320. int buf_size = avpkt->size;
  321. int16_t *out_frame = data;
  322. GetBitContext gb;
  323. const G729FormatDescription *format;
  324. int frame_erasure = 0; ///< frame erasure detected during decoding
  325. int bad_pitch = 0; ///< parity check failed
  326. int i;
  327. int16_t *tmp;
  328. G729Formats packet_type;
  329. G729Context *ctx = avctx->priv_data;
  330. int16_t lp[2][11]; // (3.12)
  331. uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
  332. uint8_t quantizer_1st; ///< first stage vector of quantizer
  333. uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
  334. uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
  335. int pitch_delay_int[2]; // pitch delay, integer part
  336. int pitch_delay_3x; // pitch delay, multiplied by 3
  337. int16_t fc[SUBFRAME_SIZE]; // fixed-codebook vector
  338. int16_t synth[SUBFRAME_SIZE+10]; // fixed-codebook vector
  339. int j;
  340. int gain_before, gain_after;
  341. int is_periodic = 0; // whether one of the subframes is declared as periodic or not
  342. if (*data_size < SUBFRAME_SIZE << 2) {
  343. av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
  344. return AVERROR(EIO);
  345. }
  346. if (buf_size == 10) {
  347. packet_type = FORMAT_G729_8K;
  348. format = &format_g729_8k;
  349. //Reset voice decision
  350. ctx->onset = 0;
  351. ctx->voice_decision = DECISION_VOICE;
  352. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
  353. } else if (buf_size == 8) {
  354. packet_type = FORMAT_G729D_6K4;
  355. format = &format_g729d_6k4;
  356. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
  357. } else {
  358. av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
  359. return AVERROR_INVALIDDATA;
  360. }
  361. for (i=0; i < buf_size; i++)
  362. frame_erasure |= buf[i];
  363. frame_erasure = !frame_erasure;
  364. init_get_bits(&gb, buf, 8*buf_size);
  365. ma_predictor = get_bits(&gb, 1);
  366. quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
  367. quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
  368. quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
  369. if(frame_erasure)
  370. lsf_restore_from_previous(ctx->lsfq, ctx->past_quantizer_outputs,
  371. ctx->ma_predictor_prev);
  372. else {
  373. lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
  374. ma_predictor,
  375. quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
  376. ctx->ma_predictor_prev = ma_predictor;
  377. }
  378. tmp = ctx->past_quantizer_outputs[MA_NP];
  379. memmove(ctx->past_quantizer_outputs + 1, ctx->past_quantizer_outputs,
  380. MA_NP * sizeof(int16_t*));
  381. ctx->past_quantizer_outputs[0] = tmp;
  382. ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
  383. ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
  384. FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
  385. for (i = 0; i < 2; i++) {
  386. int gain_corr_factor;
  387. uint8_t ac_index; ///< adaptive codebook index
  388. uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
  389. int fc_indexes; ///< fixed-codebook indexes
  390. uint8_t gc_1st_index; ///< gain codebook (first stage) index
  391. uint8_t gc_2nd_index; ///< gain codebook (second stage) index
  392. ac_index = get_bits(&gb, format->ac_index_bits[i]);
  393. if(!i && format->parity_bit)
  394. bad_pitch = get_parity(ac_index) == get_bits1(&gb);
  395. fc_indexes = get_bits(&gb, format->fc_indexes_bits);
  396. pulses_signs = get_bits(&gb, format->fc_signs_bits);
  397. gc_1st_index = get_bits(&gb, format->gc_1st_index_bits);
  398. gc_2nd_index = get_bits(&gb, format->gc_2nd_index_bits);
  399. if (frame_erasure)
  400. pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
  401. else if(!i) {
  402. if (bad_pitch)
  403. pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
  404. else
  405. pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
  406. } else {
  407. int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
  408. PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
  409. if(packet_type == FORMAT_G729D_6K4)
  410. pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
  411. else
  412. pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
  413. }
  414. /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */
  415. pitch_delay_int[i] = (pitch_delay_3x + 1) / 3;
  416. if (frame_erasure) {
  417. ctx->rand_value = g729_prng(ctx->rand_value);
  418. fc_indexes = ctx->rand_value & ((1 << format->fc_indexes_bits) - 1);
  419. ctx->rand_value = g729_prng(ctx->rand_value);
  420. pulses_signs = ctx->rand_value;
  421. }
  422. memset(fc, 0, sizeof(int16_t) * SUBFRAME_SIZE);
  423. switch (packet_type) {
  424. case FORMAT_G729_8K:
  425. ff_acelp_fc_pulse_per_track(fc, ff_fc_4pulses_8bits_tracks_13,
  426. ff_fc_4pulses_8bits_track_4,
  427. fc_indexes, pulses_signs, 3, 3);
  428. break;
  429. case FORMAT_G729D_6K4:
  430. ff_acelp_fc_pulse_per_track(fc, ff_fc_2pulses_9bits_track1_gray,
  431. ff_fc_2pulses_9bits_track2_gray,
  432. fc_indexes, pulses_signs, 1, 4);
  433. break;
  434. }
  435. /*
  436. This filter enhances harmonic components of the fixed-codebook vector to
  437. improve the quality of the reconstructed speech.
  438. / fc_v[i], i < pitch_delay
  439. fc_v[i] = <
  440. \ fc_v[i] + gain_pitch * fc_v[i-pitch_delay], i >= pitch_delay
  441. */
  442. ff_acelp_weighted_vector_sum(fc + pitch_delay_int[i],
  443. fc + pitch_delay_int[i],
  444. fc, 1 << 14,
  445. av_clip(ctx->past_gain_pitch[0], SHARP_MIN, SHARP_MAX),
  446. 0, 14,
  447. SUBFRAME_SIZE - pitch_delay_int[i]);
  448. memmove(ctx->past_gain_pitch+1, ctx->past_gain_pitch, 5 * sizeof(int16_t));
  449. ctx->past_gain_code[1] = ctx->past_gain_code[0];
  450. if (frame_erasure) {
  451. ctx->past_gain_pitch[0] = (29491 * ctx->past_gain_pitch[0]) >> 15; // 0.90 (0.15)
  452. ctx->past_gain_code[0] = ( 2007 * ctx->past_gain_code[0] ) >> 11; // 0.98 (0.11)
  453. gain_corr_factor = 0;
  454. } else {
  455. if (packet_type == FORMAT_G729D_6K4) {
  456. ctx->past_gain_pitch[0] = cb_gain_1st_6k4[gc_1st_index][0] +
  457. cb_gain_2nd_6k4[gc_2nd_index][0];
  458. gain_corr_factor = cb_gain_1st_6k4[gc_1st_index][1] +
  459. cb_gain_2nd_6k4[gc_2nd_index][1];
  460. /* Without check below overflow can occure in ff_acelp_update_past_gain.
  461. It is not issue for G.729, because gain_corr_factor in it's case is always
  462. greater than 1024, while in G.729D it can be even zero. */
  463. gain_corr_factor = FFMAX(gain_corr_factor, 1024);
  464. #ifndef G729_BITEXACT
  465. gain_corr_factor >>= 1;
  466. #endif
  467. } else {
  468. ctx->past_gain_pitch[0] = cb_gain_1st_8k[gc_1st_index][0] +
  469. cb_gain_2nd_8k[gc_2nd_index][0];
  470. gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
  471. cb_gain_2nd_8k[gc_2nd_index][1];
  472. }
  473. /* Decode the fixed-codebook gain. */
  474. ctx->past_gain_code[0] = ff_acelp_decode_gain_code(&ctx->dsp, gain_corr_factor,
  475. fc, MR_ENERGY,
  476. ctx->quant_energy,
  477. ma_prediction_coeff,
  478. SUBFRAME_SIZE, 4);
  479. #ifdef G729_BITEXACT
  480. /*
  481. This correction required to get bit-exact result with
  482. reference code, because gain_corr_factor in G.729D is
  483. two times larger than in original G.729.
  484. If bit-exact result is not issue then gain_corr_factor
  485. can be simpler devided by 2 before call to g729_get_gain_code
  486. instead of using correction below.
  487. */
  488. if (packet_type == FORMAT_G729D_6K4) {
  489. gain_corr_factor >>= 1;
  490. ctx->past_gain_code[0] >>= 1;
  491. }
  492. #endif
  493. }
  494. ff_acelp_update_past_gain(ctx->quant_energy, gain_corr_factor, 2, frame_erasure);
  495. /* Routine requires rounding to lowest. */
  496. ff_acelp_interpolate(ctx->exc + i * SUBFRAME_SIZE,
  497. ctx->exc + i * SUBFRAME_SIZE - pitch_delay_3x / 3,
  498. ff_acelp_interp_filter, 6,
  499. (pitch_delay_3x % 3) << 1,
  500. 10, SUBFRAME_SIZE);
  501. ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
  502. ctx->exc + i * SUBFRAME_SIZE, fc,
  503. (!ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_pitch[0],
  504. ( ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_code[0],
  505. 1 << 13, 14, SUBFRAME_SIZE);
  506. memcpy(synth, ctx->syn_filter_data, 10 * sizeof(int16_t));
  507. if (ff_celp_lp_synthesis_filter(
  508. synth+10,
  509. &lp[i][1],
  510. ctx->exc + i * SUBFRAME_SIZE,
  511. SUBFRAME_SIZE,
  512. 10,
  513. 1,
  514. 0,
  515. 0x800))
  516. /* Overflow occured, downscale excitation signal... */
  517. for (j = 0; j < 2 * SUBFRAME_SIZE + PITCH_DELAY_MAX + INTERPOL_LEN; j++)
  518. ctx->exc_base[j] >>= 2;
  519. /* ... and make synthesis again. */
  520. if (packet_type == FORMAT_G729D_6K4) {
  521. int16_t exc_new[SUBFRAME_SIZE];
  522. ctx->onset = g729d_onset_decision(ctx->onset, ctx->past_gain_code);
  523. ctx->voice_decision = g729d_voice_decision(ctx->onset, ctx->voice_decision, ctx->past_gain_pitch);
  524. g729d_get_new_exc(exc_new, ctx->exc + i * SUBFRAME_SIZE, fc, ctx->voice_decision, ctx->past_gain_code[0], SUBFRAME_SIZE);
  525. ff_celp_lp_synthesis_filter(
  526. synth+10,
  527. &lp[i][1],
  528. exc_new,
  529. SUBFRAME_SIZE,
  530. 10,
  531. 0,
  532. 0,
  533. 0x800);
  534. } else {
  535. ff_celp_lp_synthesis_filter(
  536. synth+10,
  537. &lp[i][1],
  538. ctx->exc + i * SUBFRAME_SIZE,
  539. SUBFRAME_SIZE,
  540. 10,
  541. 0,
  542. 0,
  543. 0x800);
  544. }
  545. /* Save data (without postfilter) for use in next subframe. */
  546. memcpy(ctx->syn_filter_data, synth+SUBFRAME_SIZE, 10 * sizeof(int16_t));
  547. /* Calculate gain of unfiltered signal for use in AGC. */
  548. gain_before = 0;
  549. for (j = 0; j < SUBFRAME_SIZE; j++)
  550. gain_before += FFABS(synth[j+10]);
  551. /* Call postfilter and also update voicing decision for use in next frame. */
  552. ff_g729_postfilter(
  553. &ctx->dsp,
  554. &ctx->ht_prev_data,
  555. &is_periodic,
  556. &lp[i][0],
  557. pitch_delay_int[0],
  558. ctx->residual,
  559. ctx->res_filter_data,
  560. ctx->pos_filter_data,
  561. synth+10,
  562. SUBFRAME_SIZE);
  563. /* Calculate gain of filtered signal for use in AGC. */
  564. gain_after = 0;
  565. for(j=0; j<SUBFRAME_SIZE; j++)
  566. gain_after += FFABS(synth[j+10]);
  567. ctx->gain_coeff = ff_g729_adaptive_gain_control(
  568. gain_before,
  569. gain_after,
  570. synth+10,
  571. SUBFRAME_SIZE,
  572. ctx->gain_coeff);
  573. if (frame_erasure)
  574. ctx->pitch_delay_int_prev = FFMIN(ctx->pitch_delay_int_prev + 1, PITCH_DELAY_MAX);
  575. else
  576. ctx->pitch_delay_int_prev = pitch_delay_int[i];
  577. memcpy(synth+8, ctx->hpf_z, 2*sizeof(int16_t));
  578. ff_acelp_high_pass_filter(
  579. out_frame + i*SUBFRAME_SIZE,
  580. ctx->hpf_f,
  581. synth+10,
  582. SUBFRAME_SIZE);
  583. memcpy(ctx->hpf_z, synth+8+SUBFRAME_SIZE, 2*sizeof(int16_t));
  584. }
  585. ctx->was_periodic = is_periodic;
  586. /* Save signal for use in next frame. */
  587. memmove(ctx->exc_base, ctx->exc_base + 2 * SUBFRAME_SIZE, (PITCH_DELAY_MAX+INTERPOL_LEN)*sizeof(int16_t));
  588. *data_size = SUBFRAME_SIZE << 2;
  589. return buf_size;
  590. }
  591. AVCodec ff_g729_decoder =
  592. {
  593. "g729",
  594. AVMEDIA_TYPE_AUDIO,
  595. CODEC_ID_G729,
  596. sizeof(G729Context),
  597. decoder_init,
  598. NULL,
  599. NULL,
  600. decode_frame,
  601. .long_name = NULL_IF_CONFIG_SMALL("G.729"),
  602. };