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.

733 lines
26KB

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