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.

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