3 * Copyright (c) 2008 Vladimir Voroshilov
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/avutil.h"
36 #include "celp_math.h"
37 #include "celp_filters.h"
38 #include "acelp_filters.h"
39 #include "acelp_pitch_delay.h"
40 #include "acelp_vectors.h"
44 * minimum quantized LSF value (3.2.4)
50 * maximum quantized LSF value (3.2.4)
53 #define LSFQ_MAX 25681
56 * minimum LSF distance (3.2.4)
59 #define LSFQ_DIFF_MIN 321
61 /// interpolation filter length
62 #define INTERPOL_LEN 11
65 * minimum gain pitch value (3.8, Equation 47)
68 #define SHARP_MIN 3277
71 * maximum gain pitch value (3.8, Equation 47)
72 * (EE) This does not comply with the specification.
73 * Specification says about 0.8, which should be
74 * 13107 in (1.14), but reference C code uses
75 * 13017 (equals to 0.7945) instead of it.
77 #define SHARP_MAX 13017
80 * MR_ENERGY (mean removed energy) = mean_energy + 10 * log10(2^26 * subframe_size) in (7.13)
82 #define MR_ENERGY 1018156
84 #define DECISION_NOISE 0
85 #define DECISION_INTERMEDIATE 1
86 #define DECISION_VOICE 2
95 uint8_t ac_index_bits[2]; ///< adaptive codebook index for second subframe (size in bits)
96 uint8_t parity_bit; ///< parity bit for pitch delay
97 uint8_t gc_1st_index_bits; ///< gain codebook (first stage) index (size in bits)
98 uint8_t gc_2nd_index_bits; ///< gain codebook (second stage) index (size in bits)
99 uint8_t fc_signs_bits; ///< number of pulses in fixed-codebook vector
100 uint8_t fc_indexes_bits; ///< size (in bits) of fixed-codebook index entry
101 } G729FormatDescription;
106 /// past excitation signal buffer
107 int16_t exc_base[2*SUBFRAME_SIZE+PITCH_DELAY_MAX+INTERPOL_LEN];
109 int16_t* exc; ///< start of past excitation data in buffer
110 int pitch_delay_int_prev; ///< integer part of previous subframe's pitch delay (4.1.3)
112 /// (2.13) LSP quantizer outputs
113 int16_t past_quantizer_output_buf[MA_NP + 1][10];
114 int16_t* past_quantizer_outputs[MA_NP + 1];
116 int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame
117 int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
118 int16_t *lsp[2]; ///< pointers to lsp_buf
120 int16_t quant_energy[4]; ///< (5.10) past quantized energy
122 /// previous speech data for LP synthesis filter
123 int16_t syn_filter_data[10];
125 /// (1.14) pitch gain of current and five previous subframes
126 int16_t past_gain_pitch[6];
128 /// (14.1) gain code from current and previous subframe
129 int16_t past_gain_code[2];
131 /// voice decision on previous subframe (0-noise, 1-intermediate, 2-voice), G.729D
132 int16_t voice_decision;
134 int16_t onset; ///< detected onset level (0-2)
135 int16_t was_periodic; ///< whether previous frame was declared as periodic or not (4.4)
136 uint16_t rand_value; ///< random number generator value (4.4.4)
137 int ma_predictor_prev; ///< switched MA predictor of LSP quantizer from last good frame
139 /// (14.14) high-pass filter data (past input)
142 /// high-pass filter data (past output)
146 static const G729FormatDescription format_g729_8k = {
147 .ac_index_bits = {8,5},
149 .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
150 .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
152 .fc_indexes_bits = 13,
155 static const G729FormatDescription format_g729d_6k4 = {
156 .ac_index_bits = {8,4},
158 .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
159 .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
161 .fc_indexes_bits = 9,
165 * @brief pseudo random number generator
167 static inline uint16_t g729_prng(uint16_t value)
169 return 31821 * value + 13849;
173 * Get parity bit of bit 2..7
175 static inline int get_parity(uint8_t value)
177 return (0x6996966996696996ULL >> (value >> 2)) & 1;
181 * Decodes LSF (Line Spectral Frequencies) from L0-L3 (3.2.4).
182 * @param lsfq [out] (2.13) quantized LSF coefficients
183 * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames
184 * @param ma_predictor switched MA predictor of LSP quantizer
185 * @param vq_1st first stage vector of quantizer
186 * @param vq_2nd_low second stage lower vector of LSP quantizer
187 * @param vq_2nd_high second stage higher vector of LSP quantizer
189 static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
190 int16_t ma_predictor,
191 int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
194 static const uint8_t min_distance[2]={10, 5}; //(2.13)
195 int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
197 for (i = 0; i < 5; i++) {
198 quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
199 quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
202 for (j = 0; j < 2; j++) {
203 for (i = 1; i < 10; i++) {
204 int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
206 quantizer_output[i - 1] -= diff;
207 quantizer_output[i ] += diff;
212 for (i = 0; i < 10; i++) {
213 int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
214 for (j = 0; j < MA_NP; j++)
215 sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
220 ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
224 * Restores past LSP quantizer output using LSF from previous frame
225 * @param lsfq [in/out] (2.13) quantized LSF coefficients
226 * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames
227 * @param ma_predictor_prev MA predictor from previous frame
228 * @param lsfq_prev (2.13) quantized LSF coefficients from previous frame
230 static void lsf_restore_from_previous(int16_t* lsfq,
231 int16_t* past_quantizer_outputs[MA_NP + 1],
232 int ma_predictor_prev)
234 int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
237 for (i = 0; i < 10; i++) {
238 int tmp = lsfq[i] << 15;
240 for (k = 0; k < MA_NP; k++)
241 tmp -= past_quantizer_outputs[k][i] * cb_ma_predictor[ma_predictor_prev][k][i];
243 quantizer_output[i] = ((tmp >> 15) * cb_ma_predictor_sum_inv[ma_predictor_prev][i]) >> 12;
248 * Constructs new excitation signal and applies phase filter to it
249 * @param out[out] constructed speech signal
250 * @param in original excitation signal
251 * @param fc_cur (2.13) original fixed-codebook vector
252 * @param gain_code (14.1) gain code
253 * @param subframe_size length of the subframe
255 void g729d_get_new_exc(
258 const int16_t* fc_cur,
264 int16_t fc_new[SUBFRAME_SIZE];
266 ff_celp_convolve_circ(fc_new, fc_cur, phase_filter[dstate], subframe_size);
268 for(i=0; i<subframe_size; i++)
271 out[i] -= (gain_code * fc_cur[i] + 0x2000) >> 14;
272 out[i] += (gain_code * fc_new[i] + 0x2000) >> 14;
277 * Makes decision about onset in current subframe
278 * @param past_onset decision result of previous subframe
279 * @param past_gain_code gain code of current and previous subframe
281 * @return onset decision result for current subframe
283 int g729d_onset_decision(int past_onset, const int16_t* past_gain_code)
285 if((past_gain_code[0] >> 1) > past_gain_code[1])
288 return FFMAX(past_onset-1, 0);
292 * Makes decision about voice presence in current subframe
293 * @param onset onset level
294 * @param prev_voice_decision voice decision result from previous subframe
295 * @param past_gain_pitch pitch gain of current and previous subframes
297 * @return voice decision result for current subframe
299 static int16_t g729d_voice_decision(int onset, int prev_voice_decision, const int16_t* past_gain_pitch)
301 int i, low_gain_pitch_cnt, voice_decision;
303 if(past_gain_pitch[0] >= 14745) // 0.9
304 voice_decision = DECISION_VOICE;
305 else if (past_gain_pitch[0] <= 9830) // 0.6
306 voice_decision = DECISION_NOISE;
308 voice_decision = DECISION_INTERMEDIATE;
310 for(i=0, low_gain_pitch_cnt=0; i<6; i++)
311 if(past_gain_pitch[i] < 9830)
312 low_gain_pitch_cnt++;
314 if(low_gain_pitch_cnt > 2 && !onset)
315 voice_decision = DECISION_NOISE;
317 if(!onset && voice_decision > prev_voice_decision + 1)
320 if(onset && voice_decision < DECISION_VOICE)
323 return voice_decision;
326 static av_cold int decoder_init(AVCodecContext * avctx)
328 G729Context* ctx = avctx->priv_data;
331 if (avctx->channels != 1) {
332 av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
333 return AVERROR(EINVAL);
336 /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
337 avctx->frame_size = SUBFRAME_SIZE << 1;
339 for (k = 0; k < MA_NP + 1; k++) {
340 ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
341 for (i = 1; i < 11; i++)
342 ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
345 ctx->lsp[0] = ctx->lsp_buf[0];
346 ctx->lsp[1] = ctx->lsp_buf[1];
347 memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
349 ctx->exc = &ctx->exc_base[PITCH_DELAY_MAX+INTERPOL_LEN];
351 /* random seed initialization */
352 ctx->rand_value = 21845;
354 /* quantized prediction error */
356 ctx->quant_energy[i] = -14336; // -14 in (5.10)
358 dsputil_init(&ctx->dsp, avctx);
363 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
366 const uint8_t *buf = avpkt->data;
367 int buf_size = avpkt->size;
368 int16_t *out_frame = data;
370 G729FormatDescription format;
371 int frame_erasure = 0; ///< frame erasure detected during decoding
372 int bad_pitch = 0; ///< parity check failed
375 G729Formats packet_type;
376 G729Context *ctx = avctx->priv_data;
377 int16_t lp[2][11]; // (3.12)
378 uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
379 uint8_t quantizer_1st; ///< first stage vector of quantizer
380 uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
381 uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
383 int pitch_delay_int; // pitch delay, integer part
384 int pitch_delay_3x; // pitch delay, multiplied by 3
385 int16_t fc[SUBFRAME_SIZE]; // fixed-codebook vector
386 int16_t synth[SUBFRAME_SIZE+10]; // fixed-codebook vector
388 int is_periodic = 0; // whether one of the subframes is declared as periodic or not
390 if (*data_size < SUBFRAME_SIZE << 2) {
391 av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
395 if (buf_size == 10) {
396 packet_type = FORMAT_G729_8K;
397 format = format_g729_8k;
398 //Reset voice decision
400 ctx->voice_decision = DECISION_VOICE;
401 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
402 } else if (buf_size == 8) {
403 packet_type = FORMAT_G729D_6K4;
404 format = format_g729d_6k4;
405 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
407 av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
408 return AVERROR_INVALIDDATA;
411 for (i=0; i < buf_size; i++)
412 frame_erasure |= buf[i];
413 frame_erasure = !frame_erasure;
415 init_get_bits(&gb, buf, buf_size);
417 ma_predictor = get_bits(&gb, 1);
418 quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
419 quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
420 quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
423 lsf_restore_from_previous(ctx->lsfq, ctx->past_quantizer_outputs,
424 ctx->ma_predictor_prev);
426 lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
428 quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
429 ctx->ma_predictor_prev = ma_predictor;
432 tmp = ctx->past_quantizer_outputs[MA_NP];
433 memmove(ctx->past_quantizer_outputs + 1, ctx->past_quantizer_outputs,
434 MA_NP * sizeof(int16_t*));
435 ctx->past_quantizer_outputs[0] = tmp;
437 ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
439 ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
441 FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
443 for (i = 0; i < 2; i++) {
444 int gain_corr_factor;
446 uint8_t ac_index; ///< adaptive codebook index
447 uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
448 int fc_indexes; ///< fixed-codebook indexes
449 uint8_t gc_1st_index; ///< gain codebook (first stage) index
450 uint8_t gc_2nd_index; ///< gain codebook (second stage) index
452 ac_index = get_bits(&gb, format.ac_index_bits[i]);
453 if(!i && format.parity_bit)
454 bad_pitch = get_parity(ac_index) == get_bits1(&gb);
455 fc_indexes = get_bits(&gb, format.fc_indexes_bits);
456 pulses_signs = get_bits(&gb, format.fc_signs_bits);
457 gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
458 gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
461 pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
464 pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
466 pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
468 int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
469 PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
471 if(packet_type == FORMAT_G729D_6K4)
472 pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
474 pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
477 /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */
478 pitch_delay_int = (pitch_delay_3x + 1) / 3;
481 ctx->rand_value = g729_prng(ctx->rand_value);
482 fc_indexes = ctx->rand_value & ((1 << format.fc_indexes_bits) - 1);
484 ctx->rand_value = g729_prng(ctx->rand_value);
485 pulses_signs = ctx->rand_value;
489 memset(fc, 0, sizeof(int16_t) * SUBFRAME_SIZE);
490 switch (packet_type) {
492 ff_acelp_fc_pulse_per_track(fc, ff_fc_4pulses_8bits_tracks_13,
493 ff_fc_4pulses_8bits_track_4,
494 fc_indexes, pulses_signs, 3, 3);
496 case FORMAT_G729D_6K4:
497 ff_acelp_fc_pulse_per_track(fc, ff_fc_2pulses_9bits_track1_gray,
498 ff_fc_2pulses_9bits_track2_gray,
499 fc_indexes, pulses_signs, 1, 4);
504 This filter enhances harmonic components of the fixed-codebook vector to
505 improve the quality of the reconstructed speech.
507 / fc_v[i], i < pitch_delay
509 \ fc_v[i] + gain_pitch * fc_v[i-pitch_delay], i >= pitch_delay
511 ff_acelp_weighted_vector_sum(fc + pitch_delay_int,
512 fc + pitch_delay_int,
514 av_clip(ctx->past_gain_pitch[0], SHARP_MIN, SHARP_MAX),
516 SUBFRAME_SIZE - pitch_delay_int);
518 memmove(ctx->past_gain_pitch+1, ctx->past_gain_pitch, 5 * sizeof(int16_t));
519 ctx->past_gain_code[1] = ctx->past_gain_code[0];
522 ctx->past_gain_pitch[0] = (29491 * ctx->past_gain_pitch[0]) >> 15; // 0.90 (0.15)
523 ctx->past_gain_code[0] = ( 2007 * ctx->past_gain_code[0] ) >> 11; // 0.98 (0.11)
525 gain_corr_factor = 0;
527 if (packet_type == FORMAT_G729D_6K4) {
528 ctx->past_gain_pitch[0] = cb_gain_1st_6k4[gc_1st_index][0] +
529 cb_gain_2nd_6k4[gc_2nd_index][0];
530 gain_corr_factor = cb_gain_1st_6k4[gc_1st_index][1] +
531 cb_gain_2nd_6k4[gc_2nd_index][1];
533 /* Without check below overflow can occure in ff_acelp_update_past_gain.
534 It is not issue for G.729, because gain_corr_factor in it's case is always
535 greater than 1024, while in G.729D it can be even zero. */
536 gain_corr_factor = FFMAX(gain_corr_factor, 1024);
537 #ifndef G729_BITEXACT
538 gain_corr_factor >>= 1;
541 ctx->past_gain_pitch[0] = cb_gain_1st_8k[gc_1st_index][0] +
542 cb_gain_2nd_8k[gc_2nd_index][0];
543 gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
544 cb_gain_2nd_8k[gc_2nd_index][1];
547 /* Decode the fixed-codebook gain. */
548 ctx->past_gain_code[0] = ff_acelp_decode_gain_code(&ctx->dsp, gain_corr_factor,
555 This correction required to get bit-exact result with
556 reference code, because gain_corr_factor in G.729D is
557 two times larger than in original G.729.
559 If bit-exact result is not issue then gain_corr_factor
560 can be simpler devided by 2 before call to g729_get_gain_code
561 instead of using correction below.
563 if (packet_type == FORMAT_G729D_6K4) {
564 gain_corr_factor >>= 1;
565 ctx->past_gain_code[0] >>= 1;
569 ff_acelp_update_past_gain(ctx->quant_energy, gain_corr_factor, 2, frame_erasure);
571 /* Routine requires rounding to lowest. */
572 ff_acelp_interpolate(ctx->exc + i * SUBFRAME_SIZE,
573 ctx->exc + i * SUBFRAME_SIZE - pitch_delay_3x / 3,
574 ff_acelp_interp_filter, 6,
575 (pitch_delay_3x % 3) << 1,
578 ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
579 ctx->exc + i * SUBFRAME_SIZE, fc,
580 (!ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_pitch[0],
581 ( ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_code[0],
582 1 << 13, 14, SUBFRAME_SIZE);
584 memcpy(synth, ctx->syn_filter_data, 10 * sizeof(int16_t));
586 if (ff_celp_lp_synthesis_filter(
589 ctx->exc + i * SUBFRAME_SIZE,
594 /* Overflow occured, downscale excitation signal... */
595 for (j = 0; j < 2 * SUBFRAME_SIZE + PITCH_DELAY_MAX + INTERPOL_LEN; j++)
596 ctx->exc_base[j] >>= 2;
598 /* ... and make synthesis again. */
599 if (packet_type == FORMAT_G729D_6K4) {
600 int16_t exc_new[SUBFRAME_SIZE];
602 ctx->onset = g729d_onset_decision(ctx->onset, ctx->past_gain_code);
603 ctx->voice_decision = g729d_voice_decision(ctx->onset, ctx->voice_decision, ctx->past_gain_pitch);
605 g729d_get_new_exc(exc_new, ctx->exc + i * SUBFRAME_SIZE, fc, ctx->voice_decision, ctx->past_gain_code[0], SUBFRAME_SIZE);
607 ff_celp_lp_synthesis_filter(
616 ff_celp_lp_synthesis_filter(
619 ctx->exc + i * SUBFRAME_SIZE,
625 /* Save data (without postfilter) for use in next subframe. */
626 memcpy(ctx->syn_filter_data, synth+SUBFRAME_SIZE, 10 * sizeof(int16_t));
629 ctx->pitch_delay_int_prev = FFMIN(ctx->pitch_delay_int_prev + 1, PITCH_DELAY_MAX);
631 ctx->pitch_delay_int_prev = pitch_delay_int;
633 memcpy(synth+8, ctx->hpf_z, 2*sizeof(int16_t));
634 ff_acelp_high_pass_filter(
635 out_frame + i*SUBFRAME_SIZE,
639 memcpy(ctx->hpf_z, synth+8+SUBFRAME_SIZE, 2*sizeof(int16_t));
642 ctx->was_periodic = is_periodic;
644 /* Save signal for use in next frame. */
645 memmove(ctx->exc_base, ctx->exc_base + 2 * SUBFRAME_SIZE, (PITCH_DELAY_MAX+INTERPOL_LEN)*sizeof(int16_t));
647 *data_size = SUBFRAME_SIZE << 2;
651 AVCodec ff_g729_decoder =
661 .long_name = NULL_IF_CONFIG_SMALL("G.729"),