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 #define BITSTREAM_READER_LE
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
34 #include "acelp_vectors.h"
35 #include "celp_filters.h"
39 #define CNG_RANDOM_SEED 12345
41 static av_cold
int g723_1_decode_init(AVCodecContext
*avctx
)
43 G723_1_Context
*p
= avctx
->priv_data
;
45 avctx
->channel_layout
= AV_CH_LAYOUT_MONO
;
46 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16
;
48 avctx
->sample_rate
= 8000;
51 memcpy(p
->prev_lsp
, dc_lsp
, LPC_ORDER
* sizeof(*p
->prev_lsp
));
52 memcpy(p
->sid_lsp
, dc_lsp
, LPC_ORDER
* sizeof(*p
->sid_lsp
));
54 p
->cng_random_seed
= CNG_RANDOM_SEED
;
55 p
->past_frame_type
= SID_FRAME
;
61 * Unpack the frame into parameters.
63 * @param p the context
64 * @param buf pointer to the input buffer
65 * @param buf_size size of the input buffer
67 static int unpack_bitstream(G723_1_Context
*p
, const uint8_t *buf
,
72 int temp
, info_bits
, i
;
74 init_get_bits(&gb
, buf
, buf_size
* 8);
76 /* Extract frame type and rate info */
77 info_bits
= get_bits(&gb
, 2);
80 p
->cur_frame_type
= UNTRANSMITTED_FRAME
;
84 /* Extract 24 bit lsp indices, 8 bit for each band */
85 p
->lsp_index
[2] = get_bits(&gb
, 8);
86 p
->lsp_index
[1] = get_bits(&gb
, 8);
87 p
->lsp_index
[0] = get_bits(&gb
, 8);
90 p
->cur_frame_type
= SID_FRAME
;
91 p
->subframe
[0].amp_index
= get_bits(&gb
, 6);
95 /* Extract the info common to both rates */
96 p
->cur_rate
= info_bits ? RATE_5300
: RATE_6300
;
97 p
->cur_frame_type
= ACTIVE_FRAME
;
99 p
->pitch_lag
[0] = get_bits(&gb
, 7);
100 if (p
->pitch_lag
[0] > 123) /* test if forbidden code */
102 p
->pitch_lag
[0] += PITCH_MIN
;
103 p
->subframe
[1].ad_cb_lag
= get_bits(&gb
, 2);
105 p
->pitch_lag
[1] = get_bits(&gb
, 7);
106 if (p
->pitch_lag
[1] > 123)
108 p
->pitch_lag
[1] += PITCH_MIN
;
109 p
->subframe
[3].ad_cb_lag
= get_bits(&gb
, 2);
110 p
->subframe
[0].ad_cb_lag
= 1;
111 p
->subframe
[2].ad_cb_lag
= 1;
113 for (i
= 0; i
< SUBFRAMES
; i
++) {
114 /* Extract combined gain */
115 temp
= get_bits(&gb
, 12);
117 p
->subframe
[i
].dirac_train
= 0;
118 if (p
->cur_rate
== RATE_6300
&& p
->pitch_lag
[i
>> 1] < SUBFRAME_LEN
- 2) {
119 p
->subframe
[i
].dirac_train
= temp
>> 11;
123 p
->subframe
[i
].ad_cb_gain
= FASTDIV(temp
, GAIN_LEVELS
);
124 if (p
->subframe
[i
].ad_cb_gain
< ad_cb_len
) {
125 p
->subframe
[i
].amp_index
= temp
- p
->subframe
[i
].ad_cb_gain
*
132 p
->subframe
[0].grid_index
= get_bits(&gb
, 1);
133 p
->subframe
[1].grid_index
= get_bits(&gb
, 1);
134 p
->subframe
[2].grid_index
= get_bits(&gb
, 1);
135 p
->subframe
[3].grid_index
= get_bits(&gb
, 1);
137 if (p
->cur_rate
== RATE_6300
) {
138 skip_bits(&gb
, 1); /* skip reserved bit */
140 /* Compute pulse_pos index using the 13-bit combined position index */
141 temp
= get_bits(&gb
, 13);
142 p
->subframe
[0].pulse_pos
= temp
/ 810;
144 temp
-= p
->subframe
[0].pulse_pos
* 810;
145 p
->subframe
[1].pulse_pos
= FASTDIV(temp
, 90);
147 temp
-= p
->subframe
[1].pulse_pos
* 90;
148 p
->subframe
[2].pulse_pos
= FASTDIV(temp
, 9);
149 p
->subframe
[3].pulse_pos
= temp
- p
->subframe
[2].pulse_pos
* 9;
151 p
->subframe
[0].pulse_pos
= (p
->subframe
[0].pulse_pos
<< 16) +
153 p
->subframe
[1].pulse_pos
= (p
->subframe
[1].pulse_pos
<< 14) +
155 p
->subframe
[2].pulse_pos
= (p
->subframe
[2].pulse_pos
<< 16) +
157 p
->subframe
[3].pulse_pos
= (p
->subframe
[3].pulse_pos
<< 14) +
160 p
->subframe
[0].pulse_sign
= get_bits(&gb
, 6);
161 p
->subframe
[1].pulse_sign
= get_bits(&gb
, 5);
162 p
->subframe
[2].pulse_sign
= get_bits(&gb
, 6);
163 p
->subframe
[3].pulse_sign
= get_bits(&gb
, 5);
164 } else { /* 5300 bps */
165 p
->subframe
[0].pulse_pos
= get_bits(&gb
, 12);
166 p
->subframe
[1].pulse_pos
= get_bits(&gb
, 12);
167 p
->subframe
[2].pulse_pos
= get_bits(&gb
, 12);
168 p
->subframe
[3].pulse_pos
= get_bits(&gb
, 12);
170 p
->subframe
[0].pulse_sign
= get_bits(&gb
, 4);
171 p
->subframe
[1].pulse_sign
= get_bits(&gb
, 4);
172 p
->subframe
[2].pulse_sign
= get_bits(&gb
, 4);
173 p
->subframe
[3].pulse_sign
= get_bits(&gb
, 4);
180 * Bitexact implementation of sqrt(val/2).
182 static int16_t square_root(int val
)
185 int16_t exp
= 0x4000;
188 for (i
= 0; i
< 14; i
++) {
189 int res_exp
= res
+ exp
;
190 if (val
>= res_exp
* res_exp
<< 1)
198 * Bitexact implementation of 2ab scaled by 1/2^16.
200 * @param a 32 bit multiplicand
201 * @param b 16 bit multiplier
203 #define MULL2(a, b) \
204 ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))
207 * Generate fixed codebook excitation vector.
209 * @param vector decoded excitation vector
210 * @param subfrm current subframe
211 * @param cur_rate current bitrate
212 * @param pitch_lag closed loop pitch lag
213 * @param index current subframe index
215 static void gen_fcb_excitation(int16_t *vector
, G723_1_Subframe
*subfrm
,
216 enum Rate cur_rate
, int pitch_lag
, int index
)
220 memset(vector
, 0, SUBFRAME_LEN
* sizeof(*vector
));
222 if (cur_rate
== RATE_6300
) {
223 if (subfrm
->pulse_pos
>= max_pos
[index
])
226 /* Decode amplitudes and positions */
227 j
= PULSE_MAX
- pulses
[index
];
228 temp
= subfrm
->pulse_pos
;
229 for (i
= 0; i
< SUBFRAME_LEN
/ GRID_SIZE
; i
++) {
230 temp
-= combinatorial_table
[j
][i
];
233 temp
+= combinatorial_table
[j
++][i
];
234 if (subfrm
->pulse_sign
& (1 << (PULSE_MAX
- j
))) {
235 vector
[subfrm
->grid_index
+ GRID_SIZE
* i
] =
236 -fixed_cb_gain
[subfrm
->amp_index
];
238 vector
[subfrm
->grid_index
+ GRID_SIZE
* i
] =
239 fixed_cb_gain
[subfrm
->amp_index
];
244 if (subfrm
->dirac_train
== 1)
245 ff_g723_1_gen_dirac_train(vector
, pitch_lag
);
246 } else { /* 5300 bps */
247 int cb_gain
= fixed_cb_gain
[subfrm
->amp_index
];
248 int cb_shift
= subfrm
->grid_index
;
249 int cb_sign
= subfrm
->pulse_sign
;
250 int cb_pos
= subfrm
->pulse_pos
;
251 int offset
, beta
, lag
;
253 for (i
= 0; i
< 8; i
+= 2) {
254 offset
= ((cb_pos
& 7) << 3) + cb_shift
+ i
;
255 vector
[offset
] = (cb_sign
& 1) ? cb_gain
: -cb_gain
;
260 /* Enhance harmonic components */
261 lag
= pitch_contrib
[subfrm
->ad_cb_gain
<< 1] + pitch_lag
+
262 subfrm
->ad_cb_lag
- 1;
263 beta
= pitch_contrib
[(subfrm
->ad_cb_gain
<< 1) + 1];
265 if (lag
< SUBFRAME_LEN
- 2) {
266 for (i
= lag
; i
< SUBFRAME_LEN
; i
++)
267 vector
[i
] += beta
* vector
[i
- lag
] >> 15;
273 * Estimate maximum auto-correlation around pitch lag.
275 * @param buf buffer with offset applied
276 * @param offset offset of the excitation vector
277 * @param ccr_max pointer to the maximum auto-correlation
278 * @param pitch_lag decoded pitch lag
279 * @param length length of autocorrelation
280 * @param dir forward lag(1) / backward lag(-1)
282 static int autocorr_max(const int16_t *buf
, int offset
, int *ccr_max
,
283 int pitch_lag
, int length
, int dir
)
285 int limit
, ccr
, lag
= 0;
288 pitch_lag
= FFMIN(PITCH_MAX
- 3, pitch_lag
);
290 limit
= FFMIN(FRAME_LEN
+ PITCH_MAX
- offset
- length
, pitch_lag
+ 3);
292 limit
= pitch_lag
+ 3;
294 for (i
= pitch_lag
- 3; i
<= limit
; i
++) {
295 ccr
= ff_g723_1_dot_product(buf
, buf
+ dir
* i
, length
);
297 if (ccr
> *ccr_max
) {
306 * Calculate pitch postfilter optimal and scaling gains.
308 * @param lag pitch postfilter forward/backward lag
309 * @param ppf pitch postfilter parameters
310 * @param cur_rate current bitrate
311 * @param tgt_eng target energy
312 * @param ccr cross-correlation
313 * @param res_eng residual energy
315 static void comp_ppf_gains(int lag
, PPFParam
*ppf
, enum Rate cur_rate
,
316 int tgt_eng
, int ccr
, int res_eng
)
318 int pf_residual
; /* square of postfiltered residual */
323 temp1
= tgt_eng
* res_eng
>> 1;
324 temp2
= ccr
* ccr
<< 1;
327 if (ccr
>= res_eng
) {
328 ppf
->opt_gain
= ppf_gain_weight
[cur_rate
];
330 ppf
->opt_gain
= (ccr
<< 15) / res_eng
*
331 ppf_gain_weight
[cur_rate
] >> 15;
333 /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
334 temp1
= (tgt_eng
<< 15) + (ccr
* ppf
->opt_gain
<< 1);
335 temp2
= (ppf
->opt_gain
* ppf
->opt_gain
>> 15) * res_eng
;
336 pf_residual
= av_sat_add32(temp1
, temp2
+ (1 << 15)) >> 16;
338 if (tgt_eng
>= pf_residual
<< 1) {
341 temp1
= (tgt_eng
<< 14) / pf_residual
;
344 /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
345 ppf
->sc_gain
= square_root(temp1
<< 16);
348 ppf
->sc_gain
= 0x7fff;
351 ppf
->opt_gain
= av_clip_int16(ppf
->opt_gain
* ppf
->sc_gain
>> 15);
355 * Calculate pitch postfilter parameters.
357 * @param p the context
358 * @param offset offset of the excitation vector
359 * @param pitch_lag decoded pitch lag
360 * @param ppf pitch postfilter parameters
361 * @param cur_rate current bitrate
363 static void comp_ppf_coeff(G723_1_Context
*p
, int offset
, int pitch_lag
,
364 PPFParam
*ppf
, enum Rate cur_rate
)
373 * 1 - forward cross-correlation
374 * 2 - forward residual energy
375 * 3 - backward cross-correlation
376 * 4 - backward residual energy
378 int energy
[5] = {0, 0, 0, 0, 0};
379 int16_t *buf
= p
->audio
+ LPC_ORDER
+ offset
;
380 int fwd_lag
= autocorr_max(buf
, offset
, &energy
[1], pitch_lag
,
382 int back_lag
= autocorr_max(buf
, offset
, &energy
[3], pitch_lag
,
387 ppf
->sc_gain
= 0x7fff;
389 /* Case 0, Section 3.6 */
390 if (!back_lag
&& !fwd_lag
)
393 /* Compute target energy */
394 energy
[0] = ff_g723_1_dot_product(buf
, buf
, SUBFRAME_LEN
);
396 /* Compute forward residual energy */
398 energy
[2] = ff_g723_1_dot_product(buf
+ fwd_lag
, buf
+ fwd_lag
,
401 /* Compute backward residual energy */
403 energy
[4] = ff_g723_1_dot_product(buf
- back_lag
, buf
- back_lag
,
406 /* Normalize and shorten */
408 for (i
= 0; i
< 5; i
++)
409 temp1
= FFMAX(energy
[i
], temp1
);
411 scale
= ff_g723_1_normalize_bits(temp1
, 31);
412 for (i
= 0; i
< 5; i
++)
413 energy
[i
] = (energy
[i
] << scale
) >> 16;
415 if (fwd_lag
&& !back_lag
) { /* Case 1 */
416 comp_ppf_gains(fwd_lag
, ppf
, cur_rate
, energy
[0], energy
[1],
418 } else if (!fwd_lag
) { /* Case 2 */
419 comp_ppf_gains(-back_lag
, ppf
, cur_rate
, energy
[0], energy
[3],
421 } else { /* Case 3 */
424 * Select the largest of energy[1]^2/energy[2]
425 * and energy[3]^2/energy[4]
427 temp1
= energy
[4] * ((energy
[1] * energy
[1] + (1 << 14)) >> 15);
428 temp2
= energy
[2] * ((energy
[3] * energy
[3] + (1 << 14)) >> 15);
429 if (temp1
>= temp2
) {
430 comp_ppf_gains(fwd_lag
, ppf
, cur_rate
, energy
[0], energy
[1],
433 comp_ppf_gains(-back_lag
, ppf
, cur_rate
, energy
[0], energy
[3],
440 * Classify frames as voiced/unvoiced.
442 * @param p the context
443 * @param pitch_lag decoded pitch_lag
444 * @param exc_eng excitation energy estimation
445 * @param scale scaling factor of exc_eng
447 * @return residual interpolation index if voiced, 0 otherwise
449 static int comp_interp_index(G723_1_Context
*p
, int pitch_lag
,
450 int *exc_eng
, int *scale
)
452 int offset
= PITCH_MAX
+ 2 * SUBFRAME_LEN
;
453 int16_t *buf
= p
->audio
+ LPC_ORDER
;
455 int index
, ccr
, tgt_eng
, best_eng
, temp
;
457 *scale
= ff_g723_1_scale_vector(buf
, p
->excitation
, FRAME_LEN
+ PITCH_MAX
);
460 /* Compute maximum backward cross-correlation */
462 index
= autocorr_max(buf
, offset
, &ccr
, pitch_lag
, SUBFRAME_LEN
* 2, -1);
463 ccr
= av_sat_add32(ccr
, 1 << 15) >> 16;
465 /* Compute target energy */
466 tgt_eng
= ff_g723_1_dot_product(buf
, buf
, SUBFRAME_LEN
* 2);
467 *exc_eng
= av_sat_add32(tgt_eng
, 1 << 15) >> 16;
472 /* Compute best energy */
473 best_eng
= ff_g723_1_dot_product(buf
- index
, buf
- index
,
475 best_eng
= av_sat_add32(best_eng
, 1 << 15) >> 16;
477 temp
= best_eng
* *exc_eng
>> 3;
479 if (temp
< ccr
* ccr
)
486 * Peform residual interpolation based on frame classification.
488 * @param buf decoded excitation vector
489 * @param out output vector
490 * @param lag decoded pitch lag
491 * @param gain interpolated gain
492 * @param rseed seed for random number generator
494 static void residual_interp(int16_t *buf
, int16_t *out
, int lag
,
495 int gain
, int *rseed
)
498 if (lag
) { /* Voiced */
499 int16_t *vector_ptr
= buf
+ PITCH_MAX
;
501 for (i
= 0; i
< lag
; i
++)
502 out
[i
] = vector_ptr
[i
- lag
] * 3 >> 2;
503 av_memcpy_backptr((uint8_t*)(out
+ lag
), lag
* sizeof(*out
),
504 (FRAME_LEN
- lag
) * sizeof(*out
));
505 } else { /* Unvoiced */
506 for (i
= 0; i
< FRAME_LEN
; i
++) {
507 *rseed
= *rseed
* 521 + 259;
508 out
[i
] = gain
* *rseed
>> 15;
510 memset(buf
, 0, (FRAME_LEN
+ PITCH_MAX
) * sizeof(*buf
));
515 * Perform IIR filtering.
517 * @param fir_coef FIR coefficients
518 * @param iir_coef IIR coefficients
519 * @param src source vector
520 * @param dest destination vector
522 static void iir_filter(int16_t *fir_coef
, int16_t *iir_coef
,
523 int16_t *src
, int *dest
)
527 for (m
= 0; m
< SUBFRAME_LEN
; m
++) {
529 for (n
= 1; n
<= LPC_ORDER
; n
++) {
530 filter
-= fir_coef
[n
- 1] * src
[m
- n
] -
531 iir_coef
[n
- 1] * (dest
[m
- n
] >> 16);
534 dest
[m
] = av_clipl_int32((src
[m
] << 16) + (filter
<< 3) + (1 << 15));
539 * Adjust gain of postfiltered signal.
541 * @param p the context
542 * @param buf postfiltered output vector
543 * @param energy input energy coefficient
545 static void gain_scale(G723_1_Context
*p
, int16_t * buf
, int energy
)
547 int num
, denom
, gain
, bits1
, bits2
;
552 for (i
= 0; i
< SUBFRAME_LEN
; i
++) {
553 int temp
= buf
[i
] >> 2;
555 denom
= av_sat_dadd32(denom
, temp
);
559 bits1
= ff_g723_1_normalize_bits(num
, 31);
560 bits2
= ff_g723_1_normalize_bits(denom
, 31);
561 num
= num
<< bits1
>> 1;
564 bits2
= 5 + bits1
- bits2
;
565 bits2
= FFMAX(0, bits2
);
567 gain
= (num
>> 1) / (denom
>> 16);
568 gain
= square_root(gain
<< 16 >> bits2
);
573 for (i
= 0; i
< SUBFRAME_LEN
; i
++) {
574 p
->pf_gain
= (15 * p
->pf_gain
+ gain
+ (1 << 3)) >> 4;
575 buf
[i
] = av_clip_int16((buf
[i
] * (p
->pf_gain
+ (p
->pf_gain
>> 4)) +
581 * Perform formant filtering.
583 * @param p the context
584 * @param lpc quantized lpc coefficients
585 * @param buf input buffer
586 * @param dst output buffer
588 static void formant_postfilter(G723_1_Context
*p
, int16_t *lpc
,
589 int16_t *buf
, int16_t *dst
)
591 int16_t filter_coef
[2][LPC_ORDER
];
592 int filter_signal
[LPC_ORDER
+ FRAME_LEN
], *signal_ptr
;
595 memcpy(buf
, p
->fir_mem
, LPC_ORDER
* sizeof(*buf
));
596 memcpy(filter_signal
, p
->iir_mem
, LPC_ORDER
* sizeof(*filter_signal
));
598 for (i
= LPC_ORDER
, j
= 0; j
< SUBFRAMES
; i
+= SUBFRAME_LEN
, j
++) {
599 for (k
= 0; k
< LPC_ORDER
; k
++) {
600 filter_coef
[0][k
] = (-lpc
[k
] * postfilter_tbl
[0][k
] +
602 filter_coef
[1][k
] = (-lpc
[k
] * postfilter_tbl
[1][k
] +
605 iir_filter(filter_coef
[0], filter_coef
[1], buf
+ i
, filter_signal
+ i
);
609 memcpy(p
->fir_mem
, buf
+ FRAME_LEN
, LPC_ORDER
* sizeof(*p
->fir_mem
));
610 memcpy(p
->iir_mem
, filter_signal
+ FRAME_LEN
,
611 LPC_ORDER
* sizeof(*p
->iir_mem
));
614 signal_ptr
= filter_signal
+ LPC_ORDER
;
615 for (i
= 0; i
< SUBFRAMES
; i
++) {
621 scale
= ff_g723_1_scale_vector(dst
, buf
, SUBFRAME_LEN
);
623 /* Compute auto correlation coefficients */
624 auto_corr
[0] = ff_g723_1_dot_product(dst
, dst
+ 1, SUBFRAME_LEN
- 1);
625 auto_corr
[1] = ff_g723_1_dot_product(dst
, dst
, SUBFRAME_LEN
);
627 /* Compute reflection coefficient */
628 temp
= auto_corr
[1] >> 16;
630 temp
= (auto_corr
[0] >> 2) / temp
;
632 p
->reflection_coef
= (3 * p
->reflection_coef
+ temp
+ 2) >> 2;
633 temp
= -p
->reflection_coef
>> 1 & ~3;
635 /* Compensation filter */
636 for (j
= 0; j
< SUBFRAME_LEN
; j
++) {
637 dst
[j
] = av_sat_dadd32(signal_ptr
[j
],
638 (signal_ptr
[j
- 1] >> 16) * temp
) >> 16;
641 /* Compute normalized signal energy */
642 temp
= 2 * scale
+ 4;
644 energy
= av_clipl_int32((int64_t)auto_corr
[1] << -temp
);
646 energy
= auto_corr
[1] >> temp
;
648 gain_scale(p
, dst
, energy
);
651 signal_ptr
+= SUBFRAME_LEN
;
656 static int sid_gain_to_lsp_index(int gain
)
660 else if (gain
< 0x20)
661 return gain
- 8 << 7;
663 return gain
- 20 << 8;
666 static inline int cng_rand(int *state
, int base
)
668 *state
= (*state
* 521 + 259) & 0xFFFF;
669 return (*state
& 0x7FFF) * base
>> 15;
672 static int estimate_sid_gain(G723_1_Context
*p
)
674 int i
, shift
, seg
, seg2
, t
, val
, val_add
, x
, y
;
676 shift
= 16 - p
->cur_gain
* 2;
678 t
= p
->sid_gain
<< shift
;
680 t
= p
->sid_gain
>> -shift
;
681 x
= t
* cng_filt
[0] >> 16;
683 if (x
>= cng_bseg
[2])
686 if (x
>= cng_bseg
[1]) {
691 seg
= (x
>= cng_bseg
[0]);
693 seg2
= FFMIN(seg
, 3);
697 for (i
= 0; i
< shift
; i
++) {
698 t
= seg
* 32 + (val
<< seg2
);
707 t
= seg
* 32 + (val
<< seg2
);
710 t
= seg
* 32 + (val
+ 1 << seg2
);
712 val
= (seg2
- 1 << 4) + val
;
716 t
= seg
* 32 + (val
- 1 << seg2
);
718 val
= (seg2
- 1 << 4) + val
;
726 static void generate_noise(G723_1_Context
*p
)
730 int signs
[SUBFRAMES
/ 2 * 11], pos
[SUBFRAMES
/ 2 * 11];
731 int tmp
[SUBFRAME_LEN
* 2];
734 int b0
, c
, delta
, x
, shift
;
736 p
->pitch_lag
[0] = cng_rand(&p
->cng_random_seed
, 21) + 123;
737 p
->pitch_lag
[1] = cng_rand(&p
->cng_random_seed
, 19) + 123;
739 for (i
= 0; i
< SUBFRAMES
; i
++) {
740 p
->subframe
[i
].ad_cb_gain
= cng_rand(&p
->cng_random_seed
, 50) + 1;
741 p
->subframe
[i
].ad_cb_lag
= cng_adaptive_cb_lag
[i
];
744 for (i
= 0; i
< SUBFRAMES
/ 2; i
++) {
745 t
= cng_rand(&p
->cng_random_seed
, 1 << 13);
747 off
[i
* 2 + 1] = ((t
>> 1) & 1) + SUBFRAME_LEN
;
749 for (j
= 0; j
< 11; j
++) {
750 signs
[i
* 11 + j
] = (t
& 1) * 2 - 1 << 14;
756 for (i
= 0; i
< SUBFRAMES
; i
++) {
757 for (j
= 0; j
< SUBFRAME_LEN
/ 2; j
++)
759 t
= SUBFRAME_LEN
/ 2;
760 for (j
= 0; j
< pulses
[i
]; j
++, idx
++) {
761 int idx2
= cng_rand(&p
->cng_random_seed
, t
);
763 pos
[idx
] = tmp
[idx2
] * 2 + off
[i
];
764 tmp
[idx2
] = tmp
[--t
];
768 vector_ptr
= p
->audio
+ LPC_ORDER
;
769 memcpy(vector_ptr
, p
->prev_excitation
,
770 PITCH_MAX
* sizeof(*p
->excitation
));
771 for (i
= 0; i
< SUBFRAMES
; i
+= 2) {
772 ff_g723_1_gen_acb_excitation(vector_ptr
, vector_ptr
,
773 p
->pitch_lag
[i
>> 1], &p
->subframe
[i
],
775 ff_g723_1_gen_acb_excitation(vector_ptr
+ SUBFRAME_LEN
,
776 vector_ptr
+ SUBFRAME_LEN
,
777 p
->pitch_lag
[i
>> 1], &p
->subframe
[i
+ 1],
781 for (j
= 0; j
< SUBFRAME_LEN
* 2; j
++)
782 t
|= FFABS(vector_ptr
[j
]);
783 t
= FFMIN(t
, 0x7FFF);
787 shift
= -10 + av_log2(t
);
793 for (j
= 0; j
< SUBFRAME_LEN
* 2; j
++) {
794 t
= vector_ptr
[j
] << -shift
;
799 for (j
= 0; j
< SUBFRAME_LEN
* 2; j
++) {
800 t
= vector_ptr
[j
] >> shift
;
807 for (j
= 0; j
< 11; j
++)
808 b0
+= tmp
[pos
[(i
/ 2) * 11 + j
]] * signs
[(i
/ 2) * 11 + j
];
809 b0
= b0
* 2 * 2979LL + (1 << 29) >> 30; // approximated division by 11
811 c
= p
->cur_gain
* (p
->cur_gain
* SUBFRAME_LEN
>> 5);
812 if (shift
* 2 + 3 >= 0)
815 c
<<= -(shift
* 2 + 3);
816 c
= (av_clipl_int32(sum
<< 1) - c
) * 2979LL >> 15;
818 delta
= b0
* b0
* 2 - c
;
822 delta
= square_root(delta
);
825 if (FFABS(t
) < FFABS(x
))
833 x
= av_clip(x
, -10000, 10000);
835 for (j
= 0; j
< 11; j
++) {
836 idx
= (i
/ 2) * 11 + j
;
837 vector_ptr
[pos
[idx
]] = av_clip_int16(vector_ptr
[pos
[idx
]] +
838 (x
* signs
[idx
] >> 15));
841 /* copy decoded data to serve as a history for the next decoded subframes */
842 memcpy(vector_ptr
+ PITCH_MAX
, vector_ptr
,
843 sizeof(*vector_ptr
) * SUBFRAME_LEN
* 2);
844 vector_ptr
+= SUBFRAME_LEN
* 2;
846 /* Save the excitation for the next frame */
847 memcpy(p
->prev_excitation
, p
->audio
+ LPC_ORDER
+ FRAME_LEN
,
848 PITCH_MAX
* sizeof(*p
->excitation
));
851 static int g723_1_decode_frame(AVCodecContext
*avctx
, void *data
,
852 int *got_frame_ptr
, AVPacket
*avpkt
)
854 G723_1_Context
*p
= avctx
->priv_data
;
855 AVFrame
*frame
= data
;
856 const uint8_t *buf
= avpkt
->data
;
857 int buf_size
= avpkt
->size
;
858 int dec_mode
= buf
[0] & 3;
860 PPFParam ppf
[SUBFRAMES
];
861 int16_t cur_lsp
[LPC_ORDER
];
862 int16_t lpc
[SUBFRAMES
* LPC_ORDER
];
863 int16_t acb_vector
[SUBFRAME_LEN
];
865 int bad_frame
= 0, i
, j
, ret
;
866 int16_t *audio
= p
->audio
;
868 if (buf_size
< frame_size
[dec_mode
]) {
870 av_log(avctx
, AV_LOG_WARNING
,
871 "Expected %d bytes, got %d - skipping packet\n",
872 frame_size
[dec_mode
], buf_size
);
877 if (unpack_bitstream(p
, buf
, buf_size
) < 0) {
879 if (p
->past_frame_type
== ACTIVE_FRAME
)
880 p
->cur_frame_type
= ACTIVE_FRAME
;
882 p
->cur_frame_type
= UNTRANSMITTED_FRAME
;
885 frame
->nb_samples
= FRAME_LEN
;
886 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0) {
887 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
891 out
= (int16_t *)frame
->data
[0];
893 if (p
->cur_frame_type
== ACTIVE_FRAME
) {
895 p
->erased_frames
= 0;
896 else if (p
->erased_frames
!= 3)
899 ff_g723_1_inverse_quant(cur_lsp
, p
->prev_lsp
, p
->lsp_index
, bad_frame
);
900 ff_g723_1_lsp_interpolate(lpc
, cur_lsp
, p
->prev_lsp
);
902 /* Save the lsp_vector for the next frame */
903 memcpy(p
->prev_lsp
, cur_lsp
, LPC_ORDER
* sizeof(*p
->prev_lsp
));
905 /* Generate the excitation for the frame */
906 memcpy(p
->excitation
, p
->prev_excitation
,
907 PITCH_MAX
* sizeof(*p
->excitation
));
908 if (!p
->erased_frames
) {
909 int16_t *vector_ptr
= p
->excitation
+ PITCH_MAX
;
911 /* Update interpolation gain memory */
912 p
->interp_gain
= fixed_cb_gain
[(p
->subframe
[2].amp_index
+
913 p
->subframe
[3].amp_index
) >> 1];
914 for (i
= 0; i
< SUBFRAMES
; i
++) {
915 gen_fcb_excitation(vector_ptr
, &p
->subframe
[i
], p
->cur_rate
,
916 p
->pitch_lag
[i
>> 1], i
);
917 ff_g723_1_gen_acb_excitation(acb_vector
,
918 &p
->excitation
[SUBFRAME_LEN
* i
],
919 p
->pitch_lag
[i
>> 1],
920 &p
->subframe
[i
], p
->cur_rate
);
921 /* Get the total excitation */
922 for (j
= 0; j
< SUBFRAME_LEN
; j
++) {
923 int v
= av_clip_int16(vector_ptr
[j
] << 1);
924 vector_ptr
[j
] = av_clip_int16(v
+ acb_vector
[j
]);
926 vector_ptr
+= SUBFRAME_LEN
;
929 vector_ptr
= p
->excitation
+ PITCH_MAX
;
931 p
->interp_index
= comp_interp_index(p
, p
->pitch_lag
[1],
932 &p
->sid_gain
, &p
->cur_gain
);
934 /* Peform pitch postfiltering */
937 for (j
= 0; j
< SUBFRAMES
; i
+= SUBFRAME_LEN
, j
++)
938 comp_ppf_coeff(p
, i
, p
->pitch_lag
[j
>> 1],
939 ppf
+ j
, p
->cur_rate
);
941 for (i
= 0, j
= 0; j
< SUBFRAMES
; i
+= SUBFRAME_LEN
, j
++)
942 ff_acelp_weighted_vector_sum(p
->audio
+ LPC_ORDER
+ i
,
944 vector_ptr
+ i
+ ppf
[j
].index
,
947 1 << 14, 15, SUBFRAME_LEN
);
949 audio
= vector_ptr
- LPC_ORDER
;
952 /* Save the excitation for the next frame */
953 memcpy(p
->prev_excitation
, p
->excitation
+ FRAME_LEN
,
954 PITCH_MAX
* sizeof(*p
->excitation
));
956 p
->interp_gain
= (p
->interp_gain
* 3 + 2) >> 2;
957 if (p
->erased_frames
== 3) {
959 memset(p
->excitation
, 0,
960 (FRAME_LEN
+ PITCH_MAX
) * sizeof(*p
->excitation
));
961 memset(p
->prev_excitation
, 0,
962 PITCH_MAX
* sizeof(*p
->excitation
));
963 memset(frame
->data
[0], 0,
964 (FRAME_LEN
+ LPC_ORDER
) * sizeof(int16_t));
966 int16_t *buf
= p
->audio
+ LPC_ORDER
;
968 /* Regenerate frame */
969 residual_interp(p
->excitation
, buf
, p
->interp_index
,
970 p
->interp_gain
, &p
->random_seed
);
972 /* Save the excitation for the next frame */
973 memcpy(p
->prev_excitation
, buf
+ (FRAME_LEN
- PITCH_MAX
),
974 PITCH_MAX
* sizeof(*p
->excitation
));
977 p
->cng_random_seed
= CNG_RANDOM_SEED
;
979 if (p
->cur_frame_type
== SID_FRAME
) {
980 p
->sid_gain
= sid_gain_to_lsp_index(p
->subframe
[0].amp_index
);
981 ff_g723_1_inverse_quant(p
->sid_lsp
, p
->prev_lsp
, p
->lsp_index
, 0);
982 } else if (p
->past_frame_type
== ACTIVE_FRAME
) {
983 p
->sid_gain
= estimate_sid_gain(p
);
986 if (p
->past_frame_type
== ACTIVE_FRAME
)
987 p
->cur_gain
= p
->sid_gain
;
989 p
->cur_gain
= (p
->cur_gain
* 7 + p
->sid_gain
) >> 3;
991 ff_g723_1_lsp_interpolate(lpc
, p
->sid_lsp
, p
->prev_lsp
);
992 /* Save the lsp_vector for the next frame */
993 memcpy(p
->prev_lsp
, p
->sid_lsp
, LPC_ORDER
* sizeof(*p
->prev_lsp
));
996 p
->past_frame_type
= p
->cur_frame_type
;
998 memcpy(p
->audio
, p
->synth_mem
, LPC_ORDER
* sizeof(*p
->audio
));
999 for (i
= LPC_ORDER
, j
= 0; j
< SUBFRAMES
; i
+= SUBFRAME_LEN
, j
++)
1000 ff_celp_lp_synthesis_filter(p
->audio
+ i
, &lpc
[j
* LPC_ORDER
],
1001 audio
+ i
, SUBFRAME_LEN
, LPC_ORDER
,
1003 memcpy(p
->synth_mem
, p
->audio
+ FRAME_LEN
, LPC_ORDER
* sizeof(*p
->audio
));
1005 if (p
->postfilter
) {
1006 formant_postfilter(p
, lpc
, p
->audio
, out
);
1007 } else { // if output is not postfiltered it should be scaled by 2
1008 for (i
= 0; i
< FRAME_LEN
; i
++)
1009 out
[i
] = av_clip_int16(p
->audio
[LPC_ORDER
+ i
] << 1);
1014 return frame_size
[dec_mode
];
1017 #define OFFSET(x) offsetof(G723_1_Context, x)
1018 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1020 static const AVOption options
[] = {
1021 { "postfilter", "postfilter on/off", OFFSET(postfilter
), AV_OPT_TYPE_INT
,
1022 { .i64
= 1 }, 0, 1, AD
},
1027 static const AVClass g723_1dec_class
= {
1028 .class_name
= "G.723.1 decoder",
1029 .item_name
= av_default_item_name
,
1031 .version
= LIBAVUTIL_VERSION_INT
,
1034 AVCodec ff_g723_1_decoder
= {
1036 .long_name
= NULL_IF_CONFIG_SMALL("G.723.1"),
1037 .type
= AVMEDIA_TYPE_AUDIO
,
1038 .id
= AV_CODEC_ID_G723_1
,
1039 .priv_data_size
= sizeof(G723_1_Context
),
1040 .init
= g723_1_decode_init
,
1041 .decode
= g723_1_decode_frame
,
1042 .capabilities
= AV_CODEC_CAP_SUBFRAMES
| AV_CODEC_CAP_DR1
,
1043 .priv_class
= &g723_1dec_class
,