2 * G.723.1 compatible decoder
3 * Copyright (c) 2006 Benjamin Larsson
4 * Copyright (c) 2010 Mohamed Naufal Basheer
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * G.723.1 compatible decoder
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
32 #define BITSTREAM_READER_LE
33 #include "acelp_vectors.h"
35 #include "bitstream.h"
36 #include "celp_filters.h"
40 #define CNG_RANDOM_SEED 12345
42 static av_cold int g723_1_decode_init(AVCodecContext *avctx)
44 G723_1_Context *p = avctx->priv_data;
46 avctx->channel_layout = AV_CH_LAYOUT_MONO;
47 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
49 avctx->sample_rate = 8000;
52 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
53 memcpy(p->sid_lsp, dc_lsp, LPC_ORDER * sizeof(*p->sid_lsp));
55 p->cng_random_seed = CNG_RANDOM_SEED;
56 p->past_frame_type = SID_FRAME;
62 * Unpack the frame into parameters.
64 * @param p the context
65 * @param buf pointer to the input buffer
66 * @param buf_size size of the input buffer
68 static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
73 int temp, info_bits, i;
75 bitstream_init(&bc, buf, buf_size * 8);
77 /* Extract frame type and rate info */
78 info_bits = bitstream_read(&bc, 2);
81 p->cur_frame_type = UNTRANSMITTED_FRAME;
85 /* Extract 24 bit lsp indices, 8 bit for each band */
86 p->lsp_index[2] = bitstream_read(&bc, 8);
87 p->lsp_index[1] = bitstream_read(&bc, 8);
88 p->lsp_index[0] = bitstream_read(&bc, 8);
91 p->cur_frame_type = SID_FRAME;
92 p->subframe[0].amp_index = bitstream_read(&bc, 6);
96 /* Extract the info common to both rates */
97 p->cur_rate = info_bits ? RATE_5300 : RATE_6300;
98 p->cur_frame_type = ACTIVE_FRAME;
100 p->pitch_lag[0] = bitstream_read(&bc, 7);
101 if (p->pitch_lag[0] > 123) /* test if forbidden code */
103 p->pitch_lag[0] += PITCH_MIN;
104 p->subframe[1].ad_cb_lag = bitstream_read(&bc, 2);
106 p->pitch_lag[1] = bitstream_read(&bc, 7);
107 if (p->pitch_lag[1] > 123)
109 p->pitch_lag[1] += PITCH_MIN;
110 p->subframe[3].ad_cb_lag = bitstream_read(&bc, 2);
111 p->subframe[0].ad_cb_lag = 1;
112 p->subframe[2].ad_cb_lag = 1;
114 for (i = 0; i < SUBFRAMES; i++) {
115 /* Extract combined gain */
116 temp = bitstream_read(&bc, 12);
118 p->subframe[i].dirac_train = 0;
119 if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
120 p->subframe[i].dirac_train = temp >> 11;
124 p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
125 if (p->subframe[i].ad_cb_gain < ad_cb_len) {
126 p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
133 p->subframe[0].grid_index = bitstream_read(&bc, 1);
134 p->subframe[1].grid_index = bitstream_read(&bc, 1);
135 p->subframe[2].grid_index = bitstream_read(&bc, 1);
136 p->subframe[3].grid_index = bitstream_read(&bc, 1);
138 if (p->cur_rate == RATE_6300) {
139 bitstream_skip(&bc, 1); /* skip reserved bit */
141 /* Compute pulse_pos index using the 13-bit combined position index */
142 temp = bitstream_read(&bc, 13);
143 p->subframe[0].pulse_pos = temp / 810;
145 temp -= p->subframe[0].pulse_pos * 810;
146 p->subframe[1].pulse_pos = FASTDIV(temp, 90);
148 temp -= p->subframe[1].pulse_pos * 90;
149 p->subframe[2].pulse_pos = FASTDIV(temp, 9);
150 p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
152 p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
153 bitstream_read(&bc, 16);
154 p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
155 bitstream_read(&bc, 14);
156 p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
157 bitstream_read(&bc, 16);
158 p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
159 bitstream_read(&bc, 14);
161 p->subframe[0].pulse_sign = bitstream_read(&bc, 6);
162 p->subframe[1].pulse_sign = bitstream_read(&bc, 5);
163 p->subframe[2].pulse_sign = bitstream_read(&bc, 6);
164 p->subframe[3].pulse_sign = bitstream_read(&bc, 5);
165 } else { /* 5300 bps */
166 p->subframe[0].pulse_pos = bitstream_read(&bc, 12);
167 p->subframe[1].pulse_pos = bitstream_read(&bc, 12);
168 p->subframe[2].pulse_pos = bitstream_read(&bc, 12);
169 p->subframe[3].pulse_pos = bitstream_read(&bc, 12);
171 p->subframe[0].pulse_sign = bitstream_read(&bc, 4);
172 p->subframe[1].pulse_sign = bitstream_read(&bc, 4);
173 p->subframe[2].pulse_sign = bitstream_read(&bc, 4);
174 p->subframe[3].pulse_sign = bitstream_read(&bc, 4);
181 * Bitexact implementation of sqrt(val/2).
183 static int16_t square_root(int val)
186 int16_t exp = 0x4000;
189 for (i = 0; i < 14; i ++) {
190 int res_exp = res + exp;
191 if (val >= res_exp * res_exp << 1)
199 * Bitexact implementation of 2ab scaled by 1/2^16.
201 * @param a 32 bit multiplicand
202 * @param b 16 bit multiplier
204 #define MULL2(a, b) \
205 ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))
208 * Generate fixed codebook excitation vector.
210 * @param vector decoded excitation vector
211 * @param subfrm current subframe
212 * @param cur_rate current bitrate
213 * @param pitch_lag closed loop pitch lag
214 * @param index current subframe index
216 static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
217 enum Rate cur_rate, int pitch_lag, int index)
221 memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
223 if (cur_rate == RATE_6300) {
224 if (subfrm->pulse_pos >= max_pos[index])
227 /* Decode amplitudes and positions */
228 j = PULSE_MAX - pulses[index];
229 temp = subfrm->pulse_pos;
230 for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
231 temp -= combinatorial_table[j][i];
234 temp += combinatorial_table[j++][i];
235 if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
236 vector[subfrm->grid_index + GRID_SIZE * i] =
237 -fixed_cb_gain[subfrm->amp_index];
239 vector[subfrm->grid_index + GRID_SIZE * i] =
240 fixed_cb_gain[subfrm->amp_index];
245 if (subfrm->dirac_train == 1)
246 ff_g723_1_gen_dirac_train(vector, pitch_lag);
247 } else { /* 5300 bps */
248 int cb_gain = fixed_cb_gain[subfrm->amp_index];
249 int cb_shift = subfrm->grid_index;
250 int cb_sign = subfrm->pulse_sign;
251 int cb_pos = subfrm->pulse_pos;
252 int offset, beta, lag;
254 for (i = 0; i < 8; i += 2) {
255 offset = ((cb_pos & 7) << 3) + cb_shift + i;
256 vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
261 /* Enhance harmonic components */
262 lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
263 subfrm->ad_cb_lag - 1;
264 beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
266 if (lag < SUBFRAME_LEN - 2) {
267 for (i = lag; i < SUBFRAME_LEN; i++)
268 vector[i] += beta * vector[i - lag] >> 15;
274 * Estimate maximum auto-correlation around pitch lag.
276 * @param buf buffer with offset applied
277 * @param offset offset of the excitation vector
278 * @param ccr_max pointer to the maximum auto-correlation
279 * @param pitch_lag decoded pitch lag
280 * @param length length of autocorrelation
281 * @param dir forward lag(1) / backward lag(-1)
283 static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
284 int pitch_lag, int length, int dir)
286 int limit, ccr, lag = 0;
289 pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
291 limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
293 limit = pitch_lag + 3;
295 for (i = pitch_lag - 3; i <= limit; i++) {
296 ccr = ff_g723_1_dot_product(buf, buf + dir * i, length);
298 if (ccr > *ccr_max) {
307 * Calculate pitch postfilter optimal and scaling gains.
309 * @param lag pitch postfilter forward/backward lag
310 * @param ppf pitch postfilter parameters
311 * @param cur_rate current bitrate
312 * @param tgt_eng target energy
313 * @param ccr cross-correlation
314 * @param res_eng residual energy
316 static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
317 int tgt_eng, int ccr, int res_eng)
319 int pf_residual; /* square of postfiltered residual */
324 temp1 = tgt_eng * res_eng >> 1;
325 temp2 = ccr * ccr << 1;
328 if (ccr >= res_eng) {
329 ppf->opt_gain = ppf_gain_weight[cur_rate];
331 ppf->opt_gain = (ccr << 15) / res_eng *
332 ppf_gain_weight[cur_rate] >> 15;
334 /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
335 temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
336 temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
337 pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
339 if (tgt_eng >= pf_residual << 1) {
342 temp1 = (tgt_eng << 14) / pf_residual;
345 /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
346 ppf->sc_gain = square_root(temp1 << 16);
349 ppf->sc_gain = 0x7fff;
352 ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
356 * Calculate pitch postfilter parameters.
358 * @param p the context
359 * @param offset offset of the excitation vector
360 * @param pitch_lag decoded pitch lag
361 * @param ppf pitch postfilter parameters
362 * @param cur_rate current bitrate
364 static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
365 PPFParam *ppf, enum Rate cur_rate)
374 * 1 - forward cross-correlation
375 * 2 - forward residual energy
376 * 3 - backward cross-correlation
377 * 4 - backward residual energy
379 int energy[5] = {0, 0, 0, 0, 0};
380 int16_t *buf = p->audio + LPC_ORDER + offset;
381 int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
383 int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
388 ppf->sc_gain = 0x7fff;
390 /* Case 0, Section 3.6 */
391 if (!back_lag && !fwd_lag)
394 /* Compute target energy */
395 energy[0] = ff_g723_1_dot_product(buf, buf, SUBFRAME_LEN);
397 /* Compute forward residual energy */
399 energy[2] = ff_g723_1_dot_product(buf + fwd_lag, buf + fwd_lag,
402 /* Compute backward residual energy */
404 energy[4] = ff_g723_1_dot_product(buf - back_lag, buf - back_lag,
407 /* Normalize and shorten */
409 for (i = 0; i < 5; i++)
410 temp1 = FFMAX(energy[i], temp1);
412 scale = ff_g723_1_normalize_bits(temp1, 31);
413 for (i = 0; i < 5; i++)
414 energy[i] = (energy[i] << scale) >> 16;
416 if (fwd_lag && !back_lag) { /* Case 1 */
417 comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
419 } else if (!fwd_lag) { /* Case 2 */
420 comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
422 } else { /* Case 3 */
425 * Select the largest of energy[1]^2/energy[2]
426 * and energy[3]^2/energy[4]
428 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
429 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
430 if (temp1 >= temp2) {
431 comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
434 comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
441 * Classify frames as voiced/unvoiced.
443 * @param p the context
444 * @param pitch_lag decoded pitch_lag
445 * @param exc_eng excitation energy estimation
446 * @param scale scaling factor of exc_eng
448 * @return residual interpolation index if voiced, 0 otherwise
450 static int comp_interp_index(G723_1_Context *p, int pitch_lag,
451 int *exc_eng, int *scale)
453 int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
454 int16_t *buf = p->audio + LPC_ORDER;
456 int index, ccr, tgt_eng, best_eng, temp;
458 *scale = ff_g723_1_scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX);
461 /* Compute maximum backward cross-correlation */
463 index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
464 ccr = av_sat_add32(ccr, 1 << 15) >> 16;
466 /* Compute target energy */
467 tgt_eng = ff_g723_1_dot_product(buf, buf, SUBFRAME_LEN * 2);
468 *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
473 /* Compute best energy */
474 best_eng = ff_g723_1_dot_product(buf - index, buf - index,
476 best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
478 temp = best_eng * *exc_eng >> 3;
480 if (temp < ccr * ccr)
487 * Perform residual interpolation based on frame classification.
489 * @param buf decoded excitation vector
490 * @param out output vector
491 * @param lag decoded pitch lag
492 * @param gain interpolated gain
493 * @param rseed seed for random number generator
495 static void residual_interp(int16_t *buf, int16_t *out, int lag,
496 int gain, int *rseed)
499 if (lag) { /* Voiced */
500 int16_t *vector_ptr = buf + PITCH_MAX;
502 for (i = 0; i < lag; i++)
503 out[i] = vector_ptr[i - lag] * 3 >> 2;
504 av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
505 (FRAME_LEN - lag) * sizeof(*out));
506 } else { /* Unvoiced */
507 for (i = 0; i < FRAME_LEN; i++) {
508 *rseed = *rseed * 521 + 259;
509 out[i] = gain * *rseed >> 15;
511 memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
516 * Perform IIR filtering.
518 * @param fir_coef FIR coefficients
519 * @param iir_coef IIR coefficients
520 * @param src source vector
521 * @param dest destination vector
523 static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
524 int16_t *src, int *dest)
528 for (m = 0; m < SUBFRAME_LEN; m++) {
530 for (n = 1; n <= LPC_ORDER; n++) {
531 filter -= fir_coef[n - 1] * src[m - n] -
532 iir_coef[n - 1] * (dest[m - n] >> 16);
535 dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) + (1 << 15));
540 * Adjust gain of postfiltered signal.
542 * @param p the context
543 * @param buf postfiltered output vector
544 * @param energy input energy coefficient
546 static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
548 int num, denom, gain, bits1, bits2;
553 for (i = 0; i < SUBFRAME_LEN; i++) {
554 int temp = buf[i] >> 2;
556 denom = av_sat_dadd32(denom, temp);
560 bits1 = ff_g723_1_normalize_bits(num, 31);
561 bits2 = ff_g723_1_normalize_bits(denom, 31);
562 num = num << bits1 >> 1;
565 bits2 = 5 + bits1 - bits2;
566 bits2 = FFMAX(0, bits2);
568 gain = (num >> 1) / (denom >> 16);
569 gain = square_root(gain << 16 >> bits2);
574 for (i = 0; i < SUBFRAME_LEN; i++) {
575 p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
576 buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
582 * Perform formant filtering.
584 * @param p the context
585 * @param lpc quantized lpc coefficients
586 * @param buf input buffer
587 * @param dst output buffer
589 static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
590 int16_t *buf, int16_t *dst)
592 int16_t filter_coef[2][LPC_ORDER];
593 int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
596 memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
597 memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
599 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
600 for (k = 0; k < LPC_ORDER; k++) {
601 filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
603 filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
606 iir_filter(filter_coef[0], filter_coef[1], buf + i, filter_signal + i);
610 memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(*p->fir_mem));
611 memcpy(p->iir_mem, filter_signal + FRAME_LEN,
612 LPC_ORDER * sizeof(*p->iir_mem));
615 signal_ptr = filter_signal + LPC_ORDER;
616 for (i = 0; i < SUBFRAMES; i++) {
622 scale = ff_g723_1_scale_vector(dst, buf, SUBFRAME_LEN);
624 /* Compute auto correlation coefficients */
625 auto_corr[0] = ff_g723_1_dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
626 auto_corr[1] = ff_g723_1_dot_product(dst, dst, SUBFRAME_LEN);
628 /* Compute reflection coefficient */
629 temp = auto_corr[1] >> 16;
631 temp = (auto_corr[0] >> 2) / temp;
633 p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
634 temp = -p->reflection_coef >> 1 & ~3;
636 /* Compensation filter */
637 for (j = 0; j < SUBFRAME_LEN; j++) {
638 dst[j] = av_sat_dadd32(signal_ptr[j],
639 (signal_ptr[j - 1] >> 16) * temp) >> 16;
642 /* Compute normalized signal energy */
643 temp = 2 * scale + 4;
645 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
647 energy = auto_corr[1] >> temp;
649 gain_scale(p, dst, energy);
652 signal_ptr += SUBFRAME_LEN;
657 static int sid_gain_to_lsp_index(int gain)
661 else if (gain < 0x20)
662 return gain - 8 << 7;
664 return gain - 20 << 8;
667 static inline int cng_rand(int *state, int base)
669 *state = (*state * 521 + 259) & 0xFFFF;
670 return (*state & 0x7FFF) * base >> 15;
673 static int estimate_sid_gain(G723_1_Context *p)
675 int i, shift, seg, seg2, t, val, val_add, x, y;
677 shift = 16 - p->cur_gain * 2;
679 t = p->sid_gain << shift;
681 t = p->sid_gain >> -shift;
682 x = t * cng_filt[0] >> 16;
684 if (x >= cng_bseg[2])
687 if (x >= cng_bseg[1]) {
692 seg = (x >= cng_bseg[0]);
694 seg2 = FFMIN(seg, 3);
698 for (i = 0; i < shift; i++) {
699 t = seg * 32 + (val << seg2);
708 t = seg * 32 + (val << seg2);
711 t = seg * 32 + (val + 1 << seg2);
713 val = (seg2 - 1 << 4) + val;
717 t = seg * 32 + (val - 1 << seg2);
719 val = (seg2 - 1 << 4) + val;
727 static void generate_noise(G723_1_Context *p)
731 int signs[SUBFRAMES / 2 * 11], pos[SUBFRAMES / 2 * 11];
732 int tmp[SUBFRAME_LEN * 2];
735 int b0, c, delta, x, shift;
737 p->pitch_lag[0] = cng_rand(&p->cng_random_seed, 21) + 123;
738 p->pitch_lag[1] = cng_rand(&p->cng_random_seed, 19) + 123;
740 for (i = 0; i < SUBFRAMES; i++) {
741 p->subframe[i].ad_cb_gain = cng_rand(&p->cng_random_seed, 50) + 1;
742 p->subframe[i].ad_cb_lag = cng_adaptive_cb_lag[i];
745 for (i = 0; i < SUBFRAMES / 2; i++) {
746 t = cng_rand(&p->cng_random_seed, 1 << 13);
748 off[i * 2 + 1] = ((t >> 1) & 1) + SUBFRAME_LEN;
750 for (j = 0; j < 11; j++) {
751 signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
757 for (i = 0; i < SUBFRAMES; i++) {
758 for (j = 0; j < SUBFRAME_LEN / 2; j++)
760 t = SUBFRAME_LEN / 2;
761 for (j = 0; j < pulses[i]; j++, idx++) {
762 int idx2 = cng_rand(&p->cng_random_seed, t);
764 pos[idx] = tmp[idx2] * 2 + off[i];
765 tmp[idx2] = tmp[--t];
769 vector_ptr = p->audio + LPC_ORDER;
770 memcpy(vector_ptr, p->prev_excitation,
771 PITCH_MAX * sizeof(*p->excitation));
772 for (i = 0; i < SUBFRAMES; i += 2) {
773 ff_g723_1_gen_acb_excitation(vector_ptr, vector_ptr,
774 p->pitch_lag[i >> 1], &p->subframe[i],
776 ff_g723_1_gen_acb_excitation(vector_ptr + SUBFRAME_LEN,
777 vector_ptr + SUBFRAME_LEN,
778 p->pitch_lag[i >> 1], &p->subframe[i + 1],
782 for (j = 0; j < SUBFRAME_LEN * 2; j++)
783 t |= FFABS(vector_ptr[j]);
784 t = FFMIN(t, 0x7FFF);
788 shift = -10 + av_log2(t);
794 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
795 t = vector_ptr[j] << -shift;
800 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
801 t = vector_ptr[j] >> shift;
808 for (j = 0; j < 11; j++)
809 b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
810 b0 = b0 * 2 * 2979LL + (1 << 29) >> 30; // approximated division by 11
812 c = p->cur_gain * (p->cur_gain * SUBFRAME_LEN >> 5);
813 if (shift * 2 + 3 >= 0)
816 c <<= -(shift * 2 + 3);
817 c = (av_clipl_int32(sum << 1) - c) * 2979LL >> 15;
819 delta = b0 * b0 * 2 - c;
823 delta = square_root(delta);
826 if (FFABS(t) < FFABS(x))
834 x = av_clip(x, -10000, 10000);
836 for (j = 0; j < 11; j++) {
837 idx = (i / 2) * 11 + j;
838 vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
839 (x * signs[idx] >> 15));
842 /* copy decoded data to serve as a history for the next decoded subframes */
843 memcpy(vector_ptr + PITCH_MAX, vector_ptr,
844 sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
845 vector_ptr += SUBFRAME_LEN * 2;
847 /* Save the excitation for the next frame */
848 memcpy(p->prev_excitation, p->audio + LPC_ORDER + FRAME_LEN,
849 PITCH_MAX * sizeof(*p->excitation));
852 static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
853 int *got_frame_ptr, AVPacket *avpkt)
855 G723_1_Context *p = avctx->priv_data;
856 AVFrame *frame = data;
857 const uint8_t *buf = avpkt->data;
858 int buf_size = avpkt->size;
859 int dec_mode = buf[0] & 3;
861 PPFParam ppf[SUBFRAMES];
862 int16_t cur_lsp[LPC_ORDER];
863 int16_t lpc[SUBFRAMES * LPC_ORDER];
864 int16_t acb_vector[SUBFRAME_LEN];
866 int bad_frame = 0, i, j, ret;
867 int16_t *audio = p->audio;
869 if (buf_size < frame_size[dec_mode]) {
871 av_log(avctx, AV_LOG_WARNING,
872 "Expected %d bytes, got %d - skipping packet\n",
873 frame_size[dec_mode], buf_size);
878 if (unpack_bitstream(p, buf, buf_size) < 0) {
880 if (p->past_frame_type == ACTIVE_FRAME)
881 p->cur_frame_type = ACTIVE_FRAME;
883 p->cur_frame_type = UNTRANSMITTED_FRAME;
886 frame->nb_samples = FRAME_LEN;
887 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
888 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
892 out = (int16_t *)frame->data[0];
894 if (p->cur_frame_type == ACTIVE_FRAME) {
896 p->erased_frames = 0;
897 else if (p->erased_frames != 3)
900 ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
901 ff_g723_1_lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
903 /* Save the lsp_vector for the next frame */
904 memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
906 /* Generate the excitation for the frame */
907 memcpy(p->excitation, p->prev_excitation,
908 PITCH_MAX * sizeof(*p->excitation));
909 if (!p->erased_frames) {
910 int16_t *vector_ptr = p->excitation + PITCH_MAX;
912 /* Update interpolation gain memory */
913 p->interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
914 p->subframe[3].amp_index) >> 1];
915 for (i = 0; i < SUBFRAMES; i++) {
916 gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate,
917 p->pitch_lag[i >> 1], i);
918 ff_g723_1_gen_acb_excitation(acb_vector,
919 &p->excitation[SUBFRAME_LEN * i],
920 p->pitch_lag[i >> 1],
921 &p->subframe[i], p->cur_rate);
922 /* Get the total excitation */
923 for (j = 0; j < SUBFRAME_LEN; j++) {
924 int v = av_clip_int16(vector_ptr[j] << 1);
925 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
927 vector_ptr += SUBFRAME_LEN;
930 vector_ptr = p->excitation + PITCH_MAX;
932 p->interp_index = comp_interp_index(p, p->pitch_lag[1],
933 &p->sid_gain, &p->cur_gain);
935 /* Perform pitch postfiltering */
938 for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
939 comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
940 ppf + j, p->cur_rate);
942 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
943 ff_acelp_weighted_vector_sum(p->audio + LPC_ORDER + i,
945 vector_ptr + i + ppf[j].index,
948 1 << 14, 15, SUBFRAME_LEN);
950 audio = vector_ptr - LPC_ORDER;
953 /* Save the excitation for the next frame */
954 memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
955 PITCH_MAX * sizeof(*p->excitation));
957 p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
958 if (p->erased_frames == 3) {
960 memset(p->excitation, 0,
961 (FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation));
962 memset(p->prev_excitation, 0,
963 PITCH_MAX * sizeof(*p->excitation));
964 memset(frame->data[0], 0,
965 (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
967 int16_t *buf = p->audio + LPC_ORDER;
969 /* Regenerate frame */
970 residual_interp(p->excitation, buf, p->interp_index,
971 p->interp_gain, &p->random_seed);
973 /* Save the excitation for the next frame */
974 memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX),
975 PITCH_MAX * sizeof(*p->excitation));
978 p->cng_random_seed = CNG_RANDOM_SEED;
980 if (p->cur_frame_type == SID_FRAME) {
981 p->sid_gain = sid_gain_to_lsp_index(p->subframe[0].amp_index);
982 ff_g723_1_inverse_quant(p->sid_lsp, p->prev_lsp, p->lsp_index, 0);
983 } else if (p->past_frame_type == ACTIVE_FRAME) {
984 p->sid_gain = estimate_sid_gain(p);
987 if (p->past_frame_type == ACTIVE_FRAME)
988 p->cur_gain = p->sid_gain;
990 p->cur_gain = (p->cur_gain * 7 + p->sid_gain) >> 3;
992 ff_g723_1_lsp_interpolate(lpc, p->sid_lsp, p->prev_lsp);
993 /* Save the lsp_vector for the next frame */
994 memcpy(p->prev_lsp, p->sid_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
997 p->past_frame_type = p->cur_frame_type;
999 memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio));
1000 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1001 ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER],
1002 audio + i, SUBFRAME_LEN, LPC_ORDER,
1004 memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
1006 if (p->postfilter) {
1007 formant_postfilter(p, lpc, p->audio, out);
1008 } else { // if output is not postfiltered it should be scaled by 2
1009 for (i = 0; i < FRAME_LEN; i++)
1010 out[i] = av_clip_int16(p->audio[LPC_ORDER + i] << 1);
1015 return frame_size[dec_mode];
1018 #define OFFSET(x) offsetof(G723_1_Context, x)
1019 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1021 static const AVOption options[] = {
1022 { "postfilter", "postfilter on/off", OFFSET(postfilter), AV_OPT_TYPE_INT,
1023 { .i64 = 1 }, 0, 1, AD },
1028 static const AVClass g723_1dec_class = {
1029 .class_name = "G.723.1 decoder",
1030 .item_name = av_default_item_name,
1032 .version = LIBAVUTIL_VERSION_INT,
1035 AVCodec ff_g723_1_decoder = {
1037 .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
1038 .type = AVMEDIA_TYPE_AUDIO,
1039 .id = AV_CODEC_ID_G723_1,
1040 .priv_data_size = sizeof(G723_1_Context),
1041 .init = g723_1_decode_init,
1042 .decode = g723_1_decode_frame,
1043 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1044 .priv_class = &g723_1dec_class,