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.

776 lines
28KB

  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 "audiodsp.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. uint8_t block_size;
  87. } G729FormatDescription;
  88. typedef struct {
  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. } G729ChannelContext;
  125. typedef struct {
  126. AudioDSPContext adsp;
  127. G729ChannelContext *channel_context;
  128. } G729Context;
  129. static const G729FormatDescription format_g729_8k = {
  130. .ac_index_bits = {8,5},
  131. .parity_bit = 1,
  132. .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
  133. .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
  134. .fc_signs_bits = 4,
  135. .fc_indexes_bits = 13,
  136. .block_size = G729_8K_BLOCK_SIZE,
  137. };
  138. static const G729FormatDescription format_g729d_6k4 = {
  139. .ac_index_bits = {8,4},
  140. .parity_bit = 0,
  141. .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
  142. .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
  143. .fc_signs_bits = 2,
  144. .fc_indexes_bits = 9,
  145. .block_size = G729D_6K4_BLOCK_SIZE,
  146. };
  147. /**
  148. * @brief pseudo random number generator
  149. */
  150. static inline uint16_t g729_prng(uint16_t value)
  151. {
  152. return 31821 * value + 13849;
  153. }
  154. /**
  155. * Decodes LSF (Line Spectral Frequencies) from L0-L3 (3.2.4).
  156. * @param[out] lsfq (2.13) quantized LSF coefficients
  157. * @param[in,out] past_quantizer_outputs (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[in,out] lsfq (2.13) quantized LSF coefficients
  194. * @param[in,out] past_quantizer_outputs (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. out[i] = in[i];
  232. out[i] -= (gain_code * fc_cur[i] + 0x2000) >> 14;
  233. out[i] += (gain_code * fc_new[i] + 0x2000) >> 14;
  234. }
  235. }
  236. /**
  237. * Makes decision about onset in current subframe
  238. * @param past_onset decision result of previous subframe
  239. * @param past_gain_code gain code of current and previous subframe
  240. *
  241. * @return onset decision result for current subframe
  242. */
  243. static int g729d_onset_decision(int past_onset, const int16_t* past_gain_code)
  244. {
  245. if ((past_gain_code[0] >> 1) > past_gain_code[1])
  246. return 2;
  247. return FFMAX(past_onset-1, 0);
  248. }
  249. /**
  250. * Makes decision about voice presence in current subframe
  251. * @param onset onset level
  252. * @param prev_voice_decision voice decision result from previous subframe
  253. * @param past_gain_pitch pitch gain of current and previous subframes
  254. *
  255. * @return voice decision result for current subframe
  256. */
  257. static int16_t g729d_voice_decision(int onset, int prev_voice_decision, const int16_t* past_gain_pitch)
  258. {
  259. int i, low_gain_pitch_cnt, voice_decision;
  260. if (past_gain_pitch[0] >= 14745) { // 0.9
  261. voice_decision = DECISION_VOICE;
  262. } else if (past_gain_pitch[0] <= 9830) { // 0.6
  263. voice_decision = DECISION_NOISE;
  264. } else {
  265. voice_decision = DECISION_INTERMEDIATE;
  266. }
  267. for (i = 0, low_gain_pitch_cnt = 0; i < 6; i++)
  268. if (past_gain_pitch[i] < 9830)
  269. low_gain_pitch_cnt++;
  270. if (low_gain_pitch_cnt > 2 && !onset)
  271. voice_decision = DECISION_NOISE;
  272. if (!onset && voice_decision > prev_voice_decision + 1)
  273. voice_decision--;
  274. if (onset && voice_decision < DECISION_VOICE)
  275. voice_decision++;
  276. return voice_decision;
  277. }
  278. static int32_t scalarproduct_int16_c(const int16_t * v1, const int16_t * v2, int order)
  279. {
  280. int64_t res = 0;
  281. while (order--)
  282. res += *v1++ * *v2++;
  283. if (res > INT32_MAX) return INT32_MAX;
  284. else if (res < INT32_MIN) return INT32_MIN;
  285. return res;
  286. }
  287. static av_cold int decoder_init(AVCodecContext * avctx)
  288. {
  289. G729Context *s = avctx->priv_data;
  290. G729ChannelContext *ctx;
  291. int c,i,k;
  292. if (avctx->channels < 1 || avctx->channels > 2) {
  293. av_log(avctx, AV_LOG_ERROR, "Only mono and stereo are supported (requested channels: %d).\n", avctx->channels);
  294. return AVERROR(EINVAL);
  295. }
  296. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  297. /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
  298. avctx->frame_size = SUBFRAME_SIZE << 1;
  299. ctx =
  300. s->channel_context = av_mallocz(sizeof(G729ChannelContext) * avctx->channels);
  301. if (!ctx)
  302. return AVERROR(ENOMEM);
  303. for (c = 0; c < avctx->channels; c++) {
  304. ctx->gain_coeff = 16384; // 1.0 in (1.14)
  305. for (k = 0; k < MA_NP + 1; k++) {
  306. ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
  307. for (i = 1; i < 11; i++)
  308. ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
  309. }
  310. ctx->lsp[0] = ctx->lsp_buf[0];
  311. ctx->lsp[1] = ctx->lsp_buf[1];
  312. memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
  313. ctx->exc = &ctx->exc_base[PITCH_DELAY_MAX+INTERPOL_LEN];
  314. ctx->pitch_delay_int_prev = PITCH_DELAY_MIN;
  315. /* random seed initialization */
  316. ctx->rand_value = 21845;
  317. /* quantized prediction error */
  318. for (i = 0; i < 4; i++)
  319. ctx->quant_energy[i] = -14336; // -14 in (5.10)
  320. ctx++;
  321. }
  322. ff_audiodsp_init(&s->adsp);
  323. s->adsp.scalarproduct_int16 = scalarproduct_int16_c;
  324. return 0;
  325. }
  326. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
  327. AVPacket *avpkt)
  328. {
  329. const uint8_t *buf = avpkt->data;
  330. int buf_size = avpkt->size;
  331. int16_t *out_frame;
  332. GetBitContext gb;
  333. const G729FormatDescription *format;
  334. int c, i;
  335. int16_t *tmp;
  336. G729Formats packet_type;
  337. G729Context *s = avctx->priv_data;
  338. G729ChannelContext *ctx = s->channel_context;
  339. int16_t lp[2][11]; // (3.12)
  340. uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
  341. uint8_t quantizer_1st; ///< first stage vector of quantizer
  342. uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
  343. uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
  344. int pitch_delay_int[2]; // pitch delay, integer part
  345. int pitch_delay_3x; // pitch delay, multiplied by 3
  346. int16_t fc[SUBFRAME_SIZE]; // fixed-codebook vector
  347. int16_t synth[SUBFRAME_SIZE+10]; // fixed-codebook vector
  348. int j, ret;
  349. int gain_before, gain_after;
  350. AVFrame *frame = data;
  351. frame->nb_samples = SUBFRAME_SIZE<<1;
  352. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  353. return ret;
  354. if (buf_size && buf_size % ((G729_8K_BLOCK_SIZE + (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)) * avctx->channels) == 0) {
  355. packet_type = FORMAT_G729_8K;
  356. format = &format_g729_8k;
  357. //Reset voice decision
  358. ctx->onset = 0;
  359. ctx->voice_decision = DECISION_VOICE;
  360. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
  361. } else if (buf_size == G729D_6K4_BLOCK_SIZE * avctx->channels && avctx->codec_id != AV_CODEC_ID_ACELP_KELVIN) {
  362. packet_type = FORMAT_G729D_6K4;
  363. format = &format_g729d_6k4;
  364. av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
  365. } else {
  366. av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
  367. return AVERROR_INVALIDDATA;
  368. }
  369. for (c = 0; c < avctx->channels; c++) {
  370. int frame_erasure = 0; ///< frame erasure detected during decoding
  371. int bad_pitch = 0; ///< parity check failed
  372. int is_periodic = 0; ///< whether one of the subframes is declared as periodic or not
  373. out_frame = (int16_t*)frame->data[c];
  374. if (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN) {
  375. if (*buf != ((avctx->channels - 1 - c) * 0x80 | 2))
  376. avpriv_request_sample(avctx, "First byte value %x for channel %d", *buf, c);
  377. buf++;
  378. }
  379. for (i = 0; i < format->block_size; i++)
  380. frame_erasure |= buf[i];
  381. frame_erasure = !frame_erasure;
  382. init_get_bits8(&gb, buf, format->block_size);
  383. ma_predictor = get_bits(&gb, 1);
  384. quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
  385. quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
  386. quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
  387. if (frame_erasure) {
  388. lsf_restore_from_previous(ctx->lsfq, ctx->past_quantizer_outputs,
  389. ctx->ma_predictor_prev);
  390. } else {
  391. lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
  392. ma_predictor,
  393. quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
  394. ctx->ma_predictor_prev = ma_predictor;
  395. }
  396. tmp = ctx->past_quantizer_outputs[MA_NP];
  397. memmove(ctx->past_quantizer_outputs + 1, ctx->past_quantizer_outputs,
  398. MA_NP * sizeof(int16_t*));
  399. ctx->past_quantizer_outputs[0] = tmp;
  400. ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
  401. ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
  402. FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
  403. for (i = 0; i < 2; i++) {
  404. int gain_corr_factor;
  405. uint8_t ac_index; ///< adaptive codebook index
  406. uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
  407. int fc_indexes; ///< fixed-codebook indexes
  408. uint8_t gc_1st_index; ///< gain codebook (first stage) index
  409. uint8_t gc_2nd_index; ///< gain codebook (second stage) index
  410. ac_index = get_bits(&gb, format->ac_index_bits[i]);
  411. if (!i && format->parity_bit)
  412. bad_pitch = av_parity(ac_index >> 2) == get_bits1(&gb);
  413. fc_indexes = get_bits(&gb, format->fc_indexes_bits);
  414. pulses_signs = get_bits(&gb, format->fc_signs_bits);
  415. gc_1st_index = get_bits(&gb, format->gc_1st_index_bits);
  416. gc_2nd_index = get_bits(&gb, format->gc_2nd_index_bits);
  417. if (frame_erasure) {
  418. pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
  419. } else if (!i) {
  420. if (bad_pitch) {
  421. pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
  422. } else {
  423. pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
  424. }
  425. } else {
  426. int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
  427. PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
  428. if (packet_type == FORMAT_G729D_6K4) {
  429. pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
  430. } else {
  431. pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
  432. }
  433. }
  434. /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */
  435. pitch_delay_int[i] = (pitch_delay_3x + 1) / 3;
  436. if (pitch_delay_int[i] > PITCH_DELAY_MAX) {
  437. av_log(avctx, AV_LOG_WARNING, "pitch_delay_int %d is too large\n", pitch_delay_int[i]);
  438. pitch_delay_int[i] = PITCH_DELAY_MAX;
  439. }
  440. if (frame_erasure) {
  441. ctx->rand_value = g729_prng(ctx->rand_value);
  442. fc_indexes = av_mod_uintp2(ctx->rand_value, format->fc_indexes_bits);
  443. ctx->rand_value = g729_prng(ctx->rand_value);
  444. pulses_signs = ctx->rand_value;
  445. }
  446. memset(fc, 0, sizeof(int16_t) * SUBFRAME_SIZE);
  447. switch (packet_type) {
  448. case FORMAT_G729_8K:
  449. ff_acelp_fc_pulse_per_track(fc, ff_fc_4pulses_8bits_tracks_13,
  450. ff_fc_4pulses_8bits_track_4,
  451. fc_indexes, pulses_signs, 3, 3);
  452. break;
  453. case FORMAT_G729D_6K4:
  454. ff_acelp_fc_pulse_per_track(fc, ff_fc_2pulses_9bits_track1_gray,
  455. ff_fc_2pulses_9bits_track2_gray,
  456. fc_indexes, pulses_signs, 1, 4);
  457. break;
  458. }
  459. /*
  460. This filter enhances harmonic components of the fixed-codebook vector to
  461. improve the quality of the reconstructed speech.
  462. / fc_v[i], i < pitch_delay
  463. fc_v[i] = <
  464. \ fc_v[i] + gain_pitch * fc_v[i-pitch_delay], i >= pitch_delay
  465. */
  466. if (SUBFRAME_SIZE > pitch_delay_int[i])
  467. ff_acelp_weighted_vector_sum(fc + pitch_delay_int[i],
  468. fc + pitch_delay_int[i],
  469. fc, 1 << 14,
  470. av_clip(ctx->past_gain_pitch[0], SHARP_MIN, SHARP_MAX),
  471. 0, 14,
  472. SUBFRAME_SIZE - pitch_delay_int[i]);
  473. memmove(ctx->past_gain_pitch+1, ctx->past_gain_pitch, 5 * sizeof(int16_t));
  474. ctx->past_gain_code[1] = ctx->past_gain_code[0];
  475. if (frame_erasure) {
  476. ctx->past_gain_pitch[0] = (29491 * ctx->past_gain_pitch[0]) >> 15; // 0.90 (0.15)
  477. ctx->past_gain_code[0] = ( 2007 * ctx->past_gain_code[0] ) >> 11; // 0.98 (0.11)
  478. gain_corr_factor = 0;
  479. } else {
  480. if (packet_type == FORMAT_G729D_6K4) {
  481. ctx->past_gain_pitch[0] = cb_gain_1st_6k4[gc_1st_index][0] +
  482. cb_gain_2nd_6k4[gc_2nd_index][0];
  483. gain_corr_factor = cb_gain_1st_6k4[gc_1st_index][1] +
  484. cb_gain_2nd_6k4[gc_2nd_index][1];
  485. /* Without check below overflow can occur in ff_acelp_update_past_gain.
  486. It is not issue for G.729, because gain_corr_factor in it's case is always
  487. greater than 1024, while in G.729D it can be even zero. */
  488. gain_corr_factor = FFMAX(gain_corr_factor, 1024);
  489. #ifndef G729_BITEXACT
  490. gain_corr_factor >>= 1;
  491. #endif
  492. } else {
  493. ctx->past_gain_pitch[0] = cb_gain_1st_8k[gc_1st_index][0] +
  494. cb_gain_2nd_8k[gc_2nd_index][0];
  495. gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
  496. cb_gain_2nd_8k[gc_2nd_index][1];
  497. }
  498. /* Decode the fixed-codebook gain. */
  499. ctx->past_gain_code[0] = ff_acelp_decode_gain_code(&s->adsp, gain_corr_factor,
  500. fc, MR_ENERGY,
  501. ctx->quant_energy,
  502. ma_prediction_coeff,
  503. SUBFRAME_SIZE, 4);
  504. #ifdef G729_BITEXACT
  505. /*
  506. This correction required to get bit-exact result with
  507. reference code, because gain_corr_factor in G.729D is
  508. two times larger than in original G.729.
  509. If bit-exact result is not issue then gain_corr_factor
  510. can be simpler divided by 2 before call to g729_get_gain_code
  511. instead of using correction below.
  512. */
  513. if (packet_type == FORMAT_G729D_6K4) {
  514. gain_corr_factor >>= 1;
  515. ctx->past_gain_code[0] >>= 1;
  516. }
  517. #endif
  518. }
  519. ff_acelp_update_past_gain(ctx->quant_energy, gain_corr_factor, 2, frame_erasure);
  520. /* Routine requires rounding to lowest. */
  521. ff_acelp_interpolate(ctx->exc + i * SUBFRAME_SIZE,
  522. ctx->exc + i * SUBFRAME_SIZE - pitch_delay_3x / 3,
  523. ff_acelp_interp_filter, 6,
  524. (pitch_delay_3x % 3) << 1,
  525. 10, SUBFRAME_SIZE);
  526. ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
  527. ctx->exc + i * SUBFRAME_SIZE, fc,
  528. (!ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_pitch[0],
  529. ( ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_code[0],
  530. 1 << 13, 14, SUBFRAME_SIZE);
  531. memcpy(synth, ctx->syn_filter_data, 10 * sizeof(int16_t));
  532. if (ff_celp_lp_synthesis_filter(
  533. synth+10,
  534. &lp[i][1],
  535. ctx->exc + i * SUBFRAME_SIZE,
  536. SUBFRAME_SIZE,
  537. 10,
  538. 1,
  539. 0,
  540. 0x800))
  541. /* Overflow occurred, downscale excitation signal... */
  542. for (j = 0; j < 2 * SUBFRAME_SIZE + PITCH_DELAY_MAX + INTERPOL_LEN; j++)
  543. ctx->exc_base[j] >>= 2;
  544. /* ... and make synthesis again. */
  545. if (packet_type == FORMAT_G729D_6K4) {
  546. int16_t exc_new[SUBFRAME_SIZE];
  547. ctx->onset = g729d_onset_decision(ctx->onset, ctx->past_gain_code);
  548. ctx->voice_decision = g729d_voice_decision(ctx->onset, ctx->voice_decision, ctx->past_gain_pitch);
  549. g729d_get_new_exc(exc_new, ctx->exc + i * SUBFRAME_SIZE, fc, ctx->voice_decision, ctx->past_gain_code[0], SUBFRAME_SIZE);
  550. ff_celp_lp_synthesis_filter(
  551. synth+10,
  552. &lp[i][1],
  553. exc_new,
  554. SUBFRAME_SIZE,
  555. 10,
  556. 0,
  557. 0,
  558. 0x800);
  559. } else {
  560. ff_celp_lp_synthesis_filter(
  561. synth+10,
  562. &lp[i][1],
  563. ctx->exc + i * SUBFRAME_SIZE,
  564. SUBFRAME_SIZE,
  565. 10,
  566. 0,
  567. 0,
  568. 0x800);
  569. }
  570. /* Save data (without postfilter) for use in next subframe. */
  571. memcpy(ctx->syn_filter_data, synth+SUBFRAME_SIZE, 10 * sizeof(int16_t));
  572. /* Calculate gain of unfiltered signal for use in AGC. */
  573. gain_before = 0;
  574. for (j = 0; j < SUBFRAME_SIZE; j++)
  575. gain_before += FFABS(synth[j+10]);
  576. /* Call postfilter and also update voicing decision for use in next frame. */
  577. ff_g729_postfilter(
  578. &s->adsp,
  579. &ctx->ht_prev_data,
  580. &is_periodic,
  581. &lp[i][0],
  582. pitch_delay_int[0],
  583. ctx->residual,
  584. ctx->res_filter_data,
  585. ctx->pos_filter_data,
  586. synth+10,
  587. SUBFRAME_SIZE);
  588. /* Calculate gain of filtered signal for use in AGC. */
  589. gain_after = 0;
  590. for (j = 0; j < SUBFRAME_SIZE; j++)
  591. gain_after += FFABS(synth[j+10]);
  592. ctx->gain_coeff = ff_g729_adaptive_gain_control(
  593. gain_before,
  594. gain_after,
  595. synth+10,
  596. SUBFRAME_SIZE,
  597. ctx->gain_coeff);
  598. if (frame_erasure) {
  599. ctx->pitch_delay_int_prev = FFMIN(ctx->pitch_delay_int_prev + 1, PITCH_DELAY_MAX);
  600. } else {
  601. ctx->pitch_delay_int_prev = pitch_delay_int[i];
  602. }
  603. memcpy(synth+8, ctx->hpf_z, 2*sizeof(int16_t));
  604. ff_acelp_high_pass_filter(
  605. out_frame + i*SUBFRAME_SIZE,
  606. ctx->hpf_f,
  607. synth+10,
  608. SUBFRAME_SIZE);
  609. memcpy(ctx->hpf_z, synth+8+SUBFRAME_SIZE, 2*sizeof(int16_t));
  610. }
  611. ctx->was_periodic = is_periodic;
  612. /* Save signal for use in next frame. */
  613. memmove(ctx->exc_base, ctx->exc_base + 2 * SUBFRAME_SIZE, (PITCH_DELAY_MAX+INTERPOL_LEN)*sizeof(int16_t));
  614. buf += format->block_size;
  615. ctx++;
  616. }
  617. *got_frame_ptr = 1;
  618. return (format->block_size + (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)) * avctx->channels;
  619. }
  620. static av_cold int decode_close(AVCodecContext *avctx)
  621. {
  622. G729Context *s = avctx->priv_data;
  623. av_freep(&s->channel_context);
  624. return 0;
  625. }
  626. AVCodec ff_g729_decoder = {
  627. .name = "g729",
  628. .long_name = NULL_IF_CONFIG_SMALL("G.729"),
  629. .type = AVMEDIA_TYPE_AUDIO,
  630. .id = AV_CODEC_ID_G729,
  631. .priv_data_size = sizeof(G729Context),
  632. .init = decoder_init,
  633. .decode = decode_frame,
  634. .close = decode_close,
  635. .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
  636. };
  637. AVCodec ff_acelp_kelvin_decoder = {
  638. .name = "acelp.kelvin",
  639. .long_name = NULL_IF_CONFIG_SMALL("Sipro ACELP.KELVIN"),
  640. .type = AVMEDIA_TYPE_AUDIO,
  641. .id = AV_CODEC_ID_ACELP_KELVIN,
  642. .priv_data_size = sizeof(G729Context),
  643. .init = decoder_init,
  644. .decode = decode_frame,
  645. .close = decode_close,
  646. .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
  647. };