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"
35 #include "celp_math.h"
36 #include "acelp_filters.h"
37 #include "acelp_pitch_delay.h"
38 #include "acelp_vectors.h"
42 * minimum quantized LSF value (3.2.4)
48 * maximum quantized LSF value (3.2.4)
51 #define LSFQ_MAX 25681
54 * minimum LSF distance (3.2.4)
57 #define LSFQ_DIFF_MIN 321
60 * minimum gain pitch value (3.8, Equation 47)
63 #define SHARP_MIN 3277
66 * maximum gain pitch value (3.8, Equation 47)
67 * (EE) This does not comply with the specification.
68 * Specification says about 0.8, which should be
69 * 13107 in (1.14), but reference C code uses
70 * 13017 (equals to 0.7945) instead of it.
72 #define SHARP_MAX 13017
75 uint8_t ac_index_bits[2]; ///< adaptive codebook index for second subframe (size in bits)
76 uint8_t parity_bit; ///< parity bit for pitch delay
77 uint8_t gc_1st_index_bits; ///< gain codebook (first stage) index (size in bits)
78 uint8_t gc_2nd_index_bits; ///< gain codebook (second stage) index (size in bits)
79 uint8_t fc_signs_bits; ///< number of pulses in fixed-codebook vector
80 uint8_t fc_indexes_bits; ///< size (in bits) of fixed-codebook index entry
81 } G729FormatDescription;
84 /// (2.13) LSP quantizer outputs
85 int16_t past_quantizer_output_buf[MA_NP + 1][10];
86 int16_t* past_quantizer_outputs[MA_NP + 1];
88 int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame
89 int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
90 int16_t *lsp[2]; ///< pointers to lsp_buf
93 static const G729FormatDescription format_g729_8k = {
94 .ac_index_bits = {8,5},
96 .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
97 .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
99 .fc_indexes_bits = 13,
102 static const G729FormatDescription format_g729d_6k4 = {
103 .ac_index_bits = {8,4},
105 .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
106 .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
108 .fc_indexes_bits = 9,
112 * \brief pseudo random number generator
114 static inline uint16_t g729_prng(uint16_t value)
116 return 31821 * value + 13849;
120 * Get parity bit of bit 2..7
122 static inline int get_parity(uint8_t value)
124 return (0x6996966996696996ULL >> (value >> 2)) & 1;
127 static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
128 int16_t ma_predictor,
129 int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
132 static const uint8_t min_distance[2]={10, 5}; //(2.13)
133 int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
135 for (i = 0; i < 5; i++) {
136 quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
137 quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
140 for (j = 0; j < 2; j++) {
141 for (i = 1; i < 10; i++) {
142 int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
144 quantizer_output[i - 1] -= diff;
145 quantizer_output[i ] += diff;
150 for (i = 0; i < 10; i++) {
151 int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
152 for (j = 0; j < MA_NP; j++)
153 sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
158 /* Rotate past_quantizer_outputs. */
159 memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*));
160 past_quantizer_outputs[0] = quantizer_output;
162 ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
165 static av_cold int decoder_init(AVCodecContext * avctx)
167 G729Context* ctx = avctx->priv_data;
170 if (avctx->channels != 1) {
171 av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
172 return AVERROR_NOFMT;
175 /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
176 avctx->frame_size = SUBFRAME_SIZE << 1;
178 for (k = 0; k < MA_NP + 1; k++) {
179 ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
180 for (i = 1; i < 11; i++)
181 ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
184 ctx->lsp[0] = ctx->lsp_buf[0];
185 ctx->lsp[1] = ctx->lsp_buf[1];
186 memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
191 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
194 const uint8_t *buf = avpkt->data;
195 int buf_size = avpkt->size;
196 int16_t *out_frame = data;
198 G729FormatDescription format;
199 int frame_erasure = 0; ///< frame erasure detected during decoding
200 int bad_pitch = 0; ///< parity check failed
202 G729Context *ctx = avctx->priv_data;
203 int16_t lp[2][11]; // (3.12)
204 uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer
205 uint8_t quantizer_1st; ///< first stage vector of quantizer
206 uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
207 uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
209 if (*data_size < SUBFRAME_SIZE << 2) {
210 av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
214 if (buf_size == 10) {
215 format = format_g729_8k;
216 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
217 } else if (buf_size == 8) {
218 format = format_g729d_6k4;
219 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
221 av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
222 return (AVERROR_NOFMT);
225 for (i=0; i < buf_size; i++)
226 frame_erasure |= buf[i];
227 frame_erasure = !frame_erasure;
229 init_get_bits(&gb, buf, buf_size);
231 ma_predictor = get_bits(&gb, 1);
232 quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
233 quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
234 quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
236 lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
238 quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
240 ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
242 ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
244 FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
246 for (i = 0; i < 2; i++) {
247 uint8_t ac_index; ///< adaptive codebook index
248 uint8_t pulses_signs; ///< fixed-codebook vector pulse signs
249 int fc_indexes; ///< fixed-codebook indexes
250 uint8_t gc_1st_index; ///< gain codebook (first stage) index
251 uint8_t gc_2nd_index; ///< gain codebook (second stage) index
253 ac_index = get_bits(&gb, format.ac_index_bits[i]);
254 if(!i && format.parity_bit)
255 bad_pitch = get_parity(ac_index) == get_bits1(&gb);
256 fc_indexes = get_bits(&gb, format.fc_indexes_bits);
257 pulses_signs = get_bits(&gb, format.fc_signs_bits);
258 gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
259 gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
261 ff_acelp_weighted_vector_sum(fc + pitch_delay_int,
262 fc + pitch_delay_int,
264 av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX),
266 SUBFRAME_SIZE - pitch_delay_int);
269 ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15; // 0.90 (0.15)
270 ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11; // 0.98 (0.11)
272 gain_corr_factor = 0;
274 ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] +
275 cb_gain_2nd_8k[gc_2nd_index][0];
276 gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
277 cb_gain_2nd_8k[gc_2nd_index][1];
279 ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
280 ctx->exc + i * SUBFRAME_SIZE, fc,
281 (!voicing && frame_erasure) ? 0 : ctx->gain_pitch,
282 ( voicing && frame_erasure) ? 0 : ctx->gain_code,
283 1 << 13, 14, SUBFRAME_SIZE);
286 *data_size = SUBFRAME_SIZE << 2;
290 AVCodec g729_decoder =
300 .long_name = NULL_IF_CONFIG_SMALL("G.729"),