00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #define BITSTREAM_READER_LE
00029 #include "libavutil/audioconvert.h"
00030 #include "libavutil/lzo.h"
00031 #include "libavutil/opt.h"
00032 #include "avcodec.h"
00033 #include "internal.h"
00034 #include "get_bits.h"
00035 #include "acelp_vectors.h"
00036 #include "celp_filters.h"
00037 #include "celp_math.h"
00038 #include "g723_1_data.h"
00039
00040 #define CNG_RANDOM_SEED 12345
00041
00042 typedef struct g723_1_context {
00043 AVClass *class;
00044 AVFrame frame;
00045
00046 G723_1_Subframe subframe[4];
00047 enum FrameType cur_frame_type;
00048 enum FrameType past_frame_type;
00049 enum Rate cur_rate;
00050 uint8_t lsp_index[LSP_BANDS];
00051 int pitch_lag[2];
00052 int erased_frames;
00053
00054 int16_t prev_lsp[LPC_ORDER];
00055 int16_t sid_lsp[LPC_ORDER];
00056 int16_t prev_excitation[PITCH_MAX];
00057 int16_t excitation[PITCH_MAX + FRAME_LEN + 4];
00058 int16_t synth_mem[LPC_ORDER];
00059 int16_t fir_mem[LPC_ORDER];
00060 int iir_mem[LPC_ORDER];
00061
00062 int random_seed;
00063 int cng_random_seed;
00064 int interp_index;
00065 int interp_gain;
00066 int sid_gain;
00067 int cur_gain;
00068 int reflection_coef;
00069 int pf_gain;
00070
00071 int postfilter;
00072
00073 int16_t audio[FRAME_LEN + LPC_ORDER + PITCH_MAX + 4];
00074 int16_t prev_data[HALF_FRAME_LEN];
00075 int16_t prev_weight_sig[PITCH_MAX];
00076
00077
00078 int16_t hpf_fir_mem;
00079 int hpf_iir_mem;
00080 int16_t perf_fir_mem[LPC_ORDER];
00081 int16_t perf_iir_mem[LPC_ORDER];
00082
00083 int16_t harmonic_mem[PITCH_MAX];
00084 } G723_1_Context;
00085
00086 static av_cold int g723_1_decode_init(AVCodecContext *avctx)
00087 {
00088 G723_1_Context *p = avctx->priv_data;
00089
00090 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00091 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00092 avctx->channels = 1;
00093 p->pf_gain = 1 << 12;
00094
00095 avcodec_get_frame_defaults(&p->frame);
00096 avctx->coded_frame = &p->frame;
00097
00098 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
00099 memcpy(p->sid_lsp, dc_lsp, LPC_ORDER * sizeof(*p->sid_lsp));
00100
00101 p->cng_random_seed = CNG_RANDOM_SEED;
00102 p->past_frame_type = SID_FRAME;
00103
00104 return 0;
00105 }
00106
00114 static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
00115 int buf_size)
00116 {
00117 GetBitContext gb;
00118 int ad_cb_len;
00119 int temp, info_bits, i;
00120
00121 init_get_bits(&gb, buf, buf_size * 8);
00122
00123
00124 info_bits = get_bits(&gb, 2);
00125
00126 if (info_bits == 3) {
00127 p->cur_frame_type = UNTRANSMITTED_FRAME;
00128 return 0;
00129 }
00130
00131
00132 p->lsp_index[2] = get_bits(&gb, 8);
00133 p->lsp_index[1] = get_bits(&gb, 8);
00134 p->lsp_index[0] = get_bits(&gb, 8);
00135
00136 if (info_bits == 2) {
00137 p->cur_frame_type = SID_FRAME;
00138 p->subframe[0].amp_index = get_bits(&gb, 6);
00139 return 0;
00140 }
00141
00142
00143 p->cur_rate = info_bits ? RATE_5300 : RATE_6300;
00144 p->cur_frame_type = ACTIVE_FRAME;
00145
00146 p->pitch_lag[0] = get_bits(&gb, 7);
00147 if (p->pitch_lag[0] > 123)
00148 return -1;
00149 p->pitch_lag[0] += PITCH_MIN;
00150 p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
00151
00152 p->pitch_lag[1] = get_bits(&gb, 7);
00153 if (p->pitch_lag[1] > 123)
00154 return -1;
00155 p->pitch_lag[1] += PITCH_MIN;
00156 p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
00157 p->subframe[0].ad_cb_lag = 1;
00158 p->subframe[2].ad_cb_lag = 1;
00159
00160 for (i = 0; i < SUBFRAMES; i++) {
00161
00162 temp = get_bits(&gb, 12);
00163 ad_cb_len = 170;
00164 p->subframe[i].dirac_train = 0;
00165 if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
00166 p->subframe[i].dirac_train = temp >> 11;
00167 temp &= 0x7FF;
00168 ad_cb_len = 85;
00169 }
00170 p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
00171 if (p->subframe[i].ad_cb_gain < ad_cb_len) {
00172 p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
00173 GAIN_LEVELS;
00174 } else {
00175 return -1;
00176 }
00177 }
00178
00179 p->subframe[0].grid_index = get_bits1(&gb);
00180 p->subframe[1].grid_index = get_bits1(&gb);
00181 p->subframe[2].grid_index = get_bits1(&gb);
00182 p->subframe[3].grid_index = get_bits1(&gb);
00183
00184 if (p->cur_rate == RATE_6300) {
00185 skip_bits1(&gb);
00186
00187
00188 temp = get_bits(&gb, 13);
00189 p->subframe[0].pulse_pos = temp / 810;
00190
00191 temp -= p->subframe[0].pulse_pos * 810;
00192 p->subframe[1].pulse_pos = FASTDIV(temp, 90);
00193
00194 temp -= p->subframe[1].pulse_pos * 90;
00195 p->subframe[2].pulse_pos = FASTDIV(temp, 9);
00196 p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
00197
00198 p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
00199 get_bits(&gb, 16);
00200 p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
00201 get_bits(&gb, 14);
00202 p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
00203 get_bits(&gb, 16);
00204 p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
00205 get_bits(&gb, 14);
00206
00207 p->subframe[0].pulse_sign = get_bits(&gb, 6);
00208 p->subframe[1].pulse_sign = get_bits(&gb, 5);
00209 p->subframe[2].pulse_sign = get_bits(&gb, 6);
00210 p->subframe[3].pulse_sign = get_bits(&gb, 5);
00211 } else {
00212 p->subframe[0].pulse_pos = get_bits(&gb, 12);
00213 p->subframe[1].pulse_pos = get_bits(&gb, 12);
00214 p->subframe[2].pulse_pos = get_bits(&gb, 12);
00215 p->subframe[3].pulse_pos = get_bits(&gb, 12);
00216
00217 p->subframe[0].pulse_sign = get_bits(&gb, 4);
00218 p->subframe[1].pulse_sign = get_bits(&gb, 4);
00219 p->subframe[2].pulse_sign = get_bits(&gb, 4);
00220 p->subframe[3].pulse_sign = get_bits(&gb, 4);
00221 }
00222
00223 return 0;
00224 }
00225
00229 static int16_t square_root(int val)
00230 {
00231 return (ff_sqrt(val << 1) >> 1) & (~1);
00232 }
00233
00240 static int normalize_bits(int num, int width)
00241 {
00242 return width - av_log2(num) - 1;
00243 }
00244
00245 #define normalize_bits_int16(num) normalize_bits(num, 15)
00246 #define normalize_bits_int32(num) normalize_bits(num, 31)
00247
00251 static int scale_vector(int16_t *dst, const int16_t *vector, int length)
00252 {
00253 int bits, max = 0;
00254 int i;
00255
00256 for (i = 0; i < length; i++)
00257 max |= FFABS(vector[i]);
00258
00259 bits= 14 - av_log2_16bit(max);
00260 bits= FFMAX(bits, 0);
00261
00262 for (i = 0; i < length; i++)
00263 dst[i] = vector[i] << bits >> 3;
00264
00265 return bits - 3;
00266 }
00267
00276 static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
00277 uint8_t *lsp_index, int bad_frame)
00278 {
00279 int min_dist, pred;
00280 int i, j, temp, stable;
00281
00282
00283 if (!bad_frame) {
00284 min_dist = 0x100;
00285 pred = 12288;
00286 } else {
00287 min_dist = 0x200;
00288 pred = 23552;
00289 lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
00290 }
00291
00292
00293 cur_lsp[0] = lsp_band0[lsp_index[0]][0];
00294 cur_lsp[1] = lsp_band0[lsp_index[0]][1];
00295 cur_lsp[2] = lsp_band0[lsp_index[0]][2];
00296 cur_lsp[3] = lsp_band1[lsp_index[1]][0];
00297 cur_lsp[4] = lsp_band1[lsp_index[1]][1];
00298 cur_lsp[5] = lsp_band1[lsp_index[1]][2];
00299 cur_lsp[6] = lsp_band2[lsp_index[2]][0];
00300 cur_lsp[7] = lsp_band2[lsp_index[2]][1];
00301 cur_lsp[8] = lsp_band2[lsp_index[2]][2];
00302 cur_lsp[9] = lsp_band2[lsp_index[2]][3];
00303
00304
00305 for (i = 0; i < LPC_ORDER; i++) {
00306 temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
00307 cur_lsp[i] += dc_lsp[i] + temp;
00308 }
00309
00310 for (i = 0; i < LPC_ORDER; i++) {
00311 cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
00312 cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
00313
00314
00315 for (j = 1; j < LPC_ORDER; j++) {
00316 temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
00317 if (temp > 0) {
00318 temp >>= 1;
00319 cur_lsp[j - 1] -= temp;
00320 cur_lsp[j] += temp;
00321 }
00322 }
00323 stable = 1;
00324 for (j = 1; j < LPC_ORDER; j++) {
00325 temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
00326 if (temp > 0) {
00327 stable = 0;
00328 break;
00329 }
00330 }
00331 if (stable)
00332 break;
00333 }
00334 if (!stable)
00335 memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
00336 }
00337
00344 #define MULL2(a, b) \
00345 MULL(a,b,15)
00346
00352 static void lsp2lpc(int16_t *lpc)
00353 {
00354 int f1[LPC_ORDER / 2 + 1];
00355 int f2[LPC_ORDER / 2 + 1];
00356 int i, j;
00357
00358
00359 for (j = 0; j < LPC_ORDER; j++) {
00360 int index = lpc[j] >> 7;
00361 int offset = lpc[j] & 0x7f;
00362 int temp1 = cos_tab[index] << 16;
00363 int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
00364 ((offset << 8) + 0x80) << 1;
00365
00366 lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
00367 }
00368
00369
00370
00371
00372
00373
00374 f1[0] = 1 << 28;
00375 f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
00376 f1[2] = lpc[0] * lpc[2] + (2 << 28);
00377
00378 f2[0] = 1 << 28;
00379 f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
00380 f2[2] = lpc[1] * lpc[3] + (2 << 28);
00381
00382
00383
00384
00385
00386 for (i = 2; i < LPC_ORDER / 2; i++) {
00387 f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
00388 f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
00389
00390 for (j = i; j >= 2; j--) {
00391 f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
00392 (f1[j] >> 1) + (f1[j - 2] >> 1);
00393 f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
00394 (f2[j] >> 1) + (f2[j - 2] >> 1);
00395 }
00396
00397 f1[0] >>= 1;
00398 f2[0] >>= 1;
00399 f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
00400 f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
00401 }
00402
00403
00404 for (i = 0; i < LPC_ORDER / 2; i++) {
00405 int64_t ff1 = f1[i + 1] + f1[i];
00406 int64_t ff2 = f2[i + 1] - f2[i];
00407
00408 lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
00409 lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
00410 (1 << 15)) >> 16;
00411 }
00412 }
00413
00422 static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
00423 {
00424 int i;
00425 int16_t *lpc_ptr = lpc;
00426
00427
00428 ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
00429 4096, 12288, 1 << 13, 14, LPC_ORDER);
00430 ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
00431 8192, 8192, 1 << 13, 14, LPC_ORDER);
00432 ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
00433 12288, 4096, 1 << 13, 14, LPC_ORDER);
00434 memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
00435
00436 for (i = 0; i < SUBFRAMES; i++) {
00437 lsp2lpc(lpc_ptr);
00438 lpc_ptr += LPC_ORDER;
00439 }
00440 }
00441
00445 static void gen_dirac_train(int16_t *buf, int pitch_lag)
00446 {
00447 int16_t vector[SUBFRAME_LEN];
00448 int i, j;
00449
00450 memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
00451 for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
00452 for (j = 0; j < SUBFRAME_LEN - i; j++)
00453 buf[i + j] += vector[j];
00454 }
00455 }
00456
00466 static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
00467 enum Rate cur_rate, int pitch_lag, int index)
00468 {
00469 int temp, i, j;
00470
00471 memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
00472
00473 if (cur_rate == RATE_6300) {
00474 if (subfrm->pulse_pos >= max_pos[index])
00475 return;
00476
00477
00478 j = PULSE_MAX - pulses[index];
00479 temp = subfrm->pulse_pos;
00480 for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
00481 temp -= combinatorial_table[j][i];
00482 if (temp >= 0)
00483 continue;
00484 temp += combinatorial_table[j++][i];
00485 if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
00486 vector[subfrm->grid_index + GRID_SIZE * i] =
00487 -fixed_cb_gain[subfrm->amp_index];
00488 } else {
00489 vector[subfrm->grid_index + GRID_SIZE * i] =
00490 fixed_cb_gain[subfrm->amp_index];
00491 }
00492 if (j == PULSE_MAX)
00493 break;
00494 }
00495 if (subfrm->dirac_train == 1)
00496 gen_dirac_train(vector, pitch_lag);
00497 } else {
00498 int cb_gain = fixed_cb_gain[subfrm->amp_index];
00499 int cb_shift = subfrm->grid_index;
00500 int cb_sign = subfrm->pulse_sign;
00501 int cb_pos = subfrm->pulse_pos;
00502 int offset, beta, lag;
00503
00504 for (i = 0; i < 8; i += 2) {
00505 offset = ((cb_pos & 7) << 3) + cb_shift + i;
00506 vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
00507 cb_pos >>= 3;
00508 cb_sign >>= 1;
00509 }
00510
00511
00512 lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
00513 subfrm->ad_cb_lag - 1;
00514 beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
00515
00516 if (lag < SUBFRAME_LEN - 2) {
00517 for (i = lag; i < SUBFRAME_LEN; i++)
00518 vector[i] += beta * vector[i - lag] >> 15;
00519 }
00520 }
00521 }
00522
00526 static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
00527 {
00528 int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
00529 int i;
00530
00531 residual[0] = prev_excitation[offset];
00532 residual[1] = prev_excitation[offset + 1];
00533
00534 offset += 2;
00535 for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
00536 residual[i] = prev_excitation[offset + (i - 2) % lag];
00537 }
00538
00539 static int dot_product(const int16_t *a, const int16_t *b, int length)
00540 {
00541 int sum = ff_dot_product(a,b,length);
00542 return av_sat_add32(sum, sum);
00543 }
00544
00548 static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
00549 int pitch_lag, G723_1_Subframe *subfrm,
00550 enum Rate cur_rate)
00551 {
00552 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
00553 const int16_t *cb_ptr;
00554 int lag = pitch_lag + subfrm->ad_cb_lag - 1;
00555
00556 int i;
00557 int sum;
00558
00559 get_residual(residual, prev_excitation, lag);
00560
00561
00562 if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2) {
00563 cb_ptr = adaptive_cb_gain85;
00564 } else
00565 cb_ptr = adaptive_cb_gain170;
00566
00567
00568 cb_ptr += subfrm->ad_cb_gain * 20;
00569 for (i = 0; i < SUBFRAME_LEN; i++) {
00570 sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER);
00571 vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
00572 }
00573 }
00574
00585 static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
00586 int pitch_lag, int length, int dir)
00587 {
00588 int limit, ccr, lag = 0;
00589 int i;
00590
00591 pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
00592 if (dir > 0)
00593 limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
00594 else
00595 limit = pitch_lag + 3;
00596
00597 for (i = pitch_lag - 3; i <= limit; i++) {
00598 ccr = dot_product(buf, buf + dir * i, length);
00599
00600 if (ccr > *ccr_max) {
00601 *ccr_max = ccr;
00602 lag = i;
00603 }
00604 }
00605 return lag;
00606 }
00607
00618 static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
00619 int tgt_eng, int ccr, int res_eng)
00620 {
00621 int pf_residual;
00622 int temp1, temp2;
00623
00624 ppf->index = lag;
00625
00626 temp1 = tgt_eng * res_eng >> 1;
00627 temp2 = ccr * ccr << 1;
00628
00629 if (temp2 > temp1) {
00630 if (ccr >= res_eng) {
00631 ppf->opt_gain = ppf_gain_weight[cur_rate];
00632 } else {
00633 ppf->opt_gain = (ccr << 15) / res_eng *
00634 ppf_gain_weight[cur_rate] >> 15;
00635 }
00636
00637 temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
00638 temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
00639 pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
00640
00641 if (tgt_eng >= pf_residual << 1) {
00642 temp1 = 0x7fff;
00643 } else {
00644 temp1 = (tgt_eng << 14) / pf_residual;
00645 }
00646
00647
00648 ppf->sc_gain = square_root(temp1 << 16);
00649 } else {
00650 ppf->opt_gain = 0;
00651 ppf->sc_gain = 0x7fff;
00652 }
00653
00654 ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
00655 }
00656
00666 static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
00667 PPFParam *ppf, enum Rate cur_rate)
00668 {
00669
00670 int16_t scale;
00671 int i;
00672 int temp1, temp2;
00673
00674
00675
00676
00677
00678
00679
00680
00681 int energy[5] = {0, 0, 0, 0, 0};
00682 int16_t *buf = p->audio + LPC_ORDER + offset;
00683 int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
00684 SUBFRAME_LEN, 1);
00685 int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
00686 SUBFRAME_LEN, -1);
00687
00688 ppf->index = 0;
00689 ppf->opt_gain = 0;
00690 ppf->sc_gain = 0x7fff;
00691
00692
00693 if (!back_lag && !fwd_lag)
00694 return;
00695
00696
00697 energy[0] = dot_product(buf, buf, SUBFRAME_LEN);
00698
00699
00700 if (fwd_lag)
00701 energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag, SUBFRAME_LEN);
00702
00703
00704 if (back_lag)
00705 energy[4] = dot_product(buf - back_lag, buf - back_lag, SUBFRAME_LEN);
00706
00707
00708 temp1 = 0;
00709 for (i = 0; i < 5; i++)
00710 temp1 = FFMAX(energy[i], temp1);
00711
00712 scale = normalize_bits(temp1, 31);
00713 for (i = 0; i < 5; i++)
00714 energy[i] = av_clipl_int32(energy[i] << scale) >> 16;
00715
00716 if (fwd_lag && !back_lag) {
00717 comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
00718 energy[2]);
00719 } else if (!fwd_lag) {
00720 comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
00721 energy[4]);
00722 } else {
00723
00724
00725
00726
00727
00728 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
00729 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
00730 if (temp1 >= temp2) {
00731 comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
00732 energy[2]);
00733 } else {
00734 comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
00735 energy[4]);
00736 }
00737 }
00738 }
00739
00750 static int comp_interp_index(G723_1_Context *p, int pitch_lag,
00751 int *exc_eng, int *scale)
00752 {
00753 int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
00754 int16_t *buf = p->audio + LPC_ORDER;
00755
00756 int index, ccr, tgt_eng, best_eng, temp;
00757
00758 *scale = scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX);
00759 buf += offset;
00760
00761
00762 ccr = 0;
00763 index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
00764 ccr = av_sat_add32(ccr, 1 << 15) >> 16;
00765
00766
00767 tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2);
00768 *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
00769
00770 if (ccr <= 0)
00771 return 0;
00772
00773
00774 best_eng = dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
00775 best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
00776
00777 temp = best_eng * *exc_eng >> 3;
00778
00779 if (temp < ccr * ccr) {
00780 return index;
00781 } else
00782 return 0;
00783 }
00784
00794 static void residual_interp(int16_t *buf, int16_t *out, int lag,
00795 int gain, int *rseed)
00796 {
00797 int i;
00798 if (lag) {
00799 int16_t *vector_ptr = buf + PITCH_MAX;
00800
00801 for (i = 0; i < lag; i++)
00802 out[i] = vector_ptr[i - lag] * 3 >> 2;
00803 av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
00804 (FRAME_LEN - lag) * sizeof(*out));
00805 } else {
00806 for (i = 0; i < FRAME_LEN; i++) {
00807 *rseed = *rseed * 521 + 259;
00808 out[i] = gain * *rseed >> 15;
00809 }
00810 memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
00811 }
00812 }
00813
00823 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
00824 {\
00825 int m, n;\
00826 int res_shift = 16 & ~-(width);\
00827 int in_shift = 16 - res_shift;\
00828 \
00829 for (m = 0; m < SUBFRAME_LEN; m++) {\
00830 int64_t filter = 0;\
00831 for (n = 1; n <= LPC_ORDER; n++) {\
00832 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
00833 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
00834 }\
00835 \
00836 (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
00837 (1 << 15)) >> res_shift;\
00838 }\
00839 }
00840
00848 static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
00849 {
00850 int num, denom, gain, bits1, bits2;
00851 int i;
00852
00853 num = energy;
00854 denom = 0;
00855 for (i = 0; i < SUBFRAME_LEN; i++) {
00856 int temp = buf[i] >> 2;
00857 temp *= temp;
00858 denom = av_sat_dadd32(denom, temp);
00859 }
00860
00861 if (num && denom) {
00862 bits1 = normalize_bits(num, 31);
00863 bits2 = normalize_bits(denom, 31);
00864 num = num << bits1 >> 1;
00865 denom <<= bits2;
00866
00867 bits2 = 5 + bits1 - bits2;
00868 bits2 = FFMAX(0, bits2);
00869
00870 gain = (num >> 1) / (denom >> 16);
00871 gain = square_root(gain << 16 >> bits2);
00872 } else {
00873 gain = 1 << 12;
00874 }
00875
00876 for (i = 0; i < SUBFRAME_LEN; i++) {
00877 p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
00878 buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
00879 (1 << 10)) >> 11);
00880 }
00881 }
00882
00891 static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
00892 int16_t *buf, int16_t *dst)
00893 {
00894 int16_t filter_coef[2][LPC_ORDER];
00895 int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
00896 int i, j, k;
00897
00898 memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
00899 memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
00900
00901 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
00902 for (k = 0; k < LPC_ORDER; k++) {
00903 filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
00904 (1 << 14)) >> 15;
00905 filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
00906 (1 << 14)) >> 15;
00907 }
00908 iir_filter(filter_coef[0], filter_coef[1], buf + i,
00909 filter_signal + i, 1);
00910 lpc += LPC_ORDER;
00911 }
00912
00913 memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
00914 memcpy(p->iir_mem, filter_signal + FRAME_LEN, LPC_ORDER * sizeof(int));
00915
00916 buf += LPC_ORDER;
00917 signal_ptr = filter_signal + LPC_ORDER;
00918 for (i = 0; i < SUBFRAMES; i++) {
00919 int temp;
00920 int auto_corr[2];
00921 int scale, energy;
00922
00923
00924 scale = scale_vector(dst, buf, SUBFRAME_LEN);
00925
00926
00927 auto_corr[0] = dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
00928 auto_corr[1] = dot_product(dst, dst, SUBFRAME_LEN);
00929
00930
00931 temp = auto_corr[1] >> 16;
00932 if (temp) {
00933 temp = (auto_corr[0] >> 2) / temp;
00934 }
00935 p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
00936 temp = -p->reflection_coef >> 1 & ~3;
00937
00938
00939 for (j = 0; j < SUBFRAME_LEN; j++) {
00940 dst[j] = av_sat_dadd32(signal_ptr[j],
00941 (signal_ptr[j - 1] >> 16) * temp) >> 16;
00942 }
00943
00944
00945 temp = 2 * scale + 4;
00946 if (temp < 0) {
00947 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
00948 } else
00949 energy = auto_corr[1] >> temp;
00950
00951 gain_scale(p, dst, energy);
00952
00953 buf += SUBFRAME_LEN;
00954 signal_ptr += SUBFRAME_LEN;
00955 dst += SUBFRAME_LEN;
00956 }
00957 }
00958
00959 static int sid_gain_to_lsp_index(int gain)
00960 {
00961 if (gain < 0x10)
00962 return gain << 6;
00963 else if (gain < 0x20)
00964 return gain - 8 << 7;
00965 else
00966 return gain - 20 << 8;
00967 }
00968
00969 static inline int cng_rand(int *state, int base)
00970 {
00971 *state = (*state * 521 + 259) & 0xFFFF;
00972 return (*state & 0x7FFF) * base >> 15;
00973 }
00974
00975 static int estimate_sid_gain(G723_1_Context *p)
00976 {
00977 int i, shift, seg, seg2, t, val, val_add, x, y;
00978
00979 shift = 16 - p->cur_gain * 2;
00980 if (shift > 0)
00981 t = p->sid_gain << shift;
00982 else
00983 t = p->sid_gain >> -shift;
00984 x = t * cng_filt[0] >> 16;
00985
00986 if (x >= cng_bseg[2])
00987 return 0x3F;
00988
00989 if (x >= cng_bseg[1]) {
00990 shift = 4;
00991 seg = 3;
00992 } else {
00993 shift = 3;
00994 seg = (x >= cng_bseg[0]);
00995 }
00996 seg2 = FFMIN(seg, 3);
00997
00998 val = 1 << shift;
00999 val_add = val >> 1;
01000 for (i = 0; i < shift; i++) {
01001 t = seg * 32 + (val << seg2);
01002 t *= t;
01003 if (x >= t)
01004 val += val_add;
01005 else
01006 val -= val_add;
01007 val_add >>= 1;
01008 }
01009
01010 t = seg * 32 + (val << seg2);
01011 y = t * t - x;
01012 if (y <= 0) {
01013 t = seg * 32 + (val + 1 << seg2);
01014 t = t * t - x;
01015 val = (seg2 - 1 << 4) + val;
01016 if (t >= y)
01017 val++;
01018 } else {
01019 t = seg * 32 + (val - 1 << seg2);
01020 t = t * t - x;
01021 val = (seg2 - 1 << 4) + val;
01022 if (t >= y)
01023 val--;
01024 }
01025
01026 return val;
01027 }
01028
01029 static void generate_noise(G723_1_Context *p)
01030 {
01031 int i, j, idx, t;
01032 int off[SUBFRAMES];
01033 int signs[SUBFRAMES / 2 * 11], pos[SUBFRAMES / 2 * 11];
01034 int tmp[SUBFRAME_LEN * 2];
01035 int16_t *vector_ptr;
01036 int64_t sum;
01037 int b0, c, delta, x, shift;
01038
01039 p->pitch_lag[0] = cng_rand(&p->cng_random_seed, 21) + 123;
01040 p->pitch_lag[1] = cng_rand(&p->cng_random_seed, 19) + 123;
01041
01042 for (i = 0; i < SUBFRAMES; i++) {
01043 p->subframe[i].ad_cb_gain = cng_rand(&p->cng_random_seed, 50) + 1;
01044 p->subframe[i].ad_cb_lag = cng_adaptive_cb_lag[i];
01045 }
01046
01047 for (i = 0; i < SUBFRAMES / 2; i++) {
01048 t = cng_rand(&p->cng_random_seed, 1 << 13);
01049 off[i * 2] = t & 1;
01050 off[i * 2 + 1] = ((t >> 1) & 1) + SUBFRAME_LEN;
01051 t >>= 2;
01052 for (j = 0; j < 11; j++) {
01053 signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
01054 t >>= 1;
01055 }
01056 }
01057
01058 idx = 0;
01059 for (i = 0; i < SUBFRAMES; i++) {
01060 for (j = 0; j < SUBFRAME_LEN / 2; j++)
01061 tmp[j] = j;
01062 t = SUBFRAME_LEN / 2;
01063 for (j = 0; j < pulses[i]; j++, idx++) {
01064 int idx2 = cng_rand(&p->cng_random_seed, t);
01065
01066 pos[idx] = tmp[idx2] * 2 + off[i];
01067 tmp[idx2] = tmp[--t];
01068 }
01069 }
01070
01071 vector_ptr = p->audio + LPC_ORDER;
01072 memcpy(vector_ptr, p->prev_excitation,
01073 PITCH_MAX * sizeof(*p->excitation));
01074 for (i = 0; i < SUBFRAMES; i += 2) {
01075 gen_acb_excitation(vector_ptr, vector_ptr,
01076 p->pitch_lag[i >> 1], &p->subframe[i],
01077 p->cur_rate);
01078 gen_acb_excitation(vector_ptr + SUBFRAME_LEN,
01079 vector_ptr + SUBFRAME_LEN,
01080 p->pitch_lag[i >> 1], &p->subframe[i + 1],
01081 p->cur_rate);
01082
01083 t = 0;
01084 for (j = 0; j < SUBFRAME_LEN * 2; j++)
01085 t |= FFABS(vector_ptr[j]);
01086 t = FFMIN(t, 0x7FFF);
01087 if (!t) {
01088 shift = 0;
01089 } else {
01090 shift = -10 + av_log2(t);
01091 if (shift < -2)
01092 shift = -2;
01093 }
01094 sum = 0;
01095 if (shift < 0) {
01096 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
01097 t = vector_ptr[j] << -shift;
01098 sum += t * t;
01099 tmp[j] = t;
01100 }
01101 } else {
01102 for (j = 0; j < SUBFRAME_LEN * 2; j++) {
01103 t = vector_ptr[j] >> shift;
01104 sum += t * t;
01105 tmp[j] = t;
01106 }
01107 }
01108
01109 b0 = 0;
01110 for (j = 0; j < 11; j++)
01111 b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
01112 b0 = b0 * 2 * 2979LL + (1 << 29) >> 30;
01113
01114 c = p->cur_gain * (p->cur_gain * SUBFRAME_LEN >> 5);
01115 if (shift * 2 + 3 >= 0)
01116 c >>= shift * 2 + 3;
01117 else
01118 c <<= -(shift * 2 + 3);
01119 c = (av_clipl_int32(sum << 1) - c) * 2979LL >> 15;
01120
01121 delta = b0 * b0 * 2 - c;
01122 if (delta <= 0) {
01123 x = -b0;
01124 } else {
01125 delta = square_root(delta);
01126 x = delta - b0;
01127 t = delta + b0;
01128 if (FFABS(t) < FFABS(x))
01129 x = -t;
01130 }
01131 shift++;
01132 if (shift < 0)
01133 x >>= -shift;
01134 else
01135 x <<= shift;
01136 x = av_clip(x, -10000, 10000);
01137
01138 for (j = 0; j < 11; j++) {
01139 idx = (i / 2) * 11 + j;
01140 vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
01141 (x * signs[idx] >> 15));
01142 }
01143
01144
01145 memcpy(vector_ptr + PITCH_MAX, vector_ptr,
01146 sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
01147 vector_ptr += SUBFRAME_LEN * 2;
01148 }
01149
01150 memcpy(p->prev_excitation, p->audio + LPC_ORDER + FRAME_LEN,
01151 PITCH_MAX * sizeof(*p->excitation));
01152 }
01153
01154 static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
01155 int *got_frame_ptr, AVPacket *avpkt)
01156 {
01157 G723_1_Context *p = avctx->priv_data;
01158 const uint8_t *buf = avpkt->data;
01159 int buf_size = avpkt->size;
01160 int dec_mode = buf[0] & 3;
01161
01162 PPFParam ppf[SUBFRAMES];
01163 int16_t cur_lsp[LPC_ORDER];
01164 int16_t lpc[SUBFRAMES * LPC_ORDER];
01165 int16_t acb_vector[SUBFRAME_LEN];
01166 int16_t *out;
01167 int bad_frame = 0, i, j, ret;
01168 int16_t *audio = p->audio;
01169
01170 if (buf_size < frame_size[dec_mode]) {
01171 if (buf_size)
01172 av_log(avctx, AV_LOG_WARNING,
01173 "Expected %d bytes, got %d - skipping packet\n",
01174 frame_size[dec_mode], buf_size);
01175 *got_frame_ptr = 0;
01176 return buf_size;
01177 }
01178
01179 if (unpack_bitstream(p, buf, buf_size) < 0) {
01180 bad_frame = 1;
01181 if (p->past_frame_type == ACTIVE_FRAME)
01182 p->cur_frame_type = ACTIVE_FRAME;
01183 else
01184 p->cur_frame_type = UNTRANSMITTED_FRAME;
01185 }
01186
01187 p->frame.nb_samples = FRAME_LEN;
01188 if ((ret = avctx->get_buffer(avctx, &p->frame)) < 0) {
01189 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01190 return ret;
01191 }
01192
01193 out = (int16_t *)p->frame.data[0];
01194
01195 if (p->cur_frame_type == ACTIVE_FRAME) {
01196 if (!bad_frame)
01197 p->erased_frames = 0;
01198 else if (p->erased_frames != 3)
01199 p->erased_frames++;
01200
01201 inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
01202 lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
01203
01204
01205 memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
01206
01207
01208 memcpy(p->excitation, p->prev_excitation,
01209 PITCH_MAX * sizeof(*p->excitation));
01210 if (!p->erased_frames) {
01211 int16_t *vector_ptr = p->excitation + PITCH_MAX;
01212
01213
01214 p->interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
01215 p->subframe[3].amp_index) >> 1];
01216 for (i = 0; i < SUBFRAMES; i++) {
01217 gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate,
01218 p->pitch_lag[i >> 1], i);
01219 gen_acb_excitation(acb_vector, &p->excitation[SUBFRAME_LEN * i],
01220 p->pitch_lag[i >> 1], &p->subframe[i],
01221 p->cur_rate);
01222
01223 for (j = 0; j < SUBFRAME_LEN; j++) {
01224 int v = av_clip_int16(vector_ptr[j] << 1);
01225 vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
01226 }
01227 vector_ptr += SUBFRAME_LEN;
01228 }
01229
01230 vector_ptr = p->excitation + PITCH_MAX;
01231
01232 p->interp_index = comp_interp_index(p, p->pitch_lag[1],
01233 &p->sid_gain, &p->cur_gain);
01234
01235
01236 if (p->postfilter) {
01237 i = PITCH_MAX;
01238 for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
01239 comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
01240 ppf + j, p->cur_rate);
01241
01242 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
01243 ff_acelp_weighted_vector_sum(p->audio + LPC_ORDER + i,
01244 vector_ptr + i,
01245 vector_ptr + i + ppf[j].index,
01246 ppf[j].sc_gain,
01247 ppf[j].opt_gain,
01248 1 << 14, 15, SUBFRAME_LEN);
01249 } else {
01250 audio = vector_ptr - LPC_ORDER;
01251 }
01252
01253
01254 memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
01255 PITCH_MAX * sizeof(*p->excitation));
01256 } else {
01257 p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
01258 if (p->erased_frames == 3) {
01259
01260 memset(p->excitation, 0,
01261 (FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation));
01262 memset(p->prev_excitation, 0,
01263 PITCH_MAX * sizeof(*p->excitation));
01264 memset(p->frame.data[0], 0,
01265 (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
01266 } else {
01267 int16_t *buf = p->audio + LPC_ORDER;
01268
01269
01270 residual_interp(p->excitation, buf, p->interp_index,
01271 p->interp_gain, &p->random_seed);
01272
01273
01274 memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX),
01275 PITCH_MAX * sizeof(*p->excitation));
01276 }
01277 }
01278 p->cng_random_seed = CNG_RANDOM_SEED;
01279 } else {
01280 if (p->cur_frame_type == SID_FRAME) {
01281 p->sid_gain = sid_gain_to_lsp_index(p->subframe[0].amp_index);
01282 inverse_quant(p->sid_lsp, p->prev_lsp, p->lsp_index, 0);
01283 } else if (p->past_frame_type == ACTIVE_FRAME) {
01284 p->sid_gain = estimate_sid_gain(p);
01285 }
01286
01287 if (p->past_frame_type == ACTIVE_FRAME)
01288 p->cur_gain = p->sid_gain;
01289 else
01290 p->cur_gain = (p->cur_gain * 7 + p->sid_gain) >> 3;
01291 generate_noise(p);
01292 lsp_interpolate(lpc, p->sid_lsp, p->prev_lsp);
01293
01294 memcpy(p->prev_lsp, p->sid_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
01295 }
01296
01297 p->past_frame_type = p->cur_frame_type;
01298
01299 memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio));
01300 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
01301 ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER],
01302 audio + i, SUBFRAME_LEN, LPC_ORDER,
01303 0, 1, 1 << 12);
01304 memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
01305
01306 if (p->postfilter) {
01307 formant_postfilter(p, lpc, p->audio, out);
01308 } else {
01309 for (i = 0; i < FRAME_LEN; i++)
01310 out[i] = av_clip_int16(p->audio[LPC_ORDER + i] << 1);
01311 }
01312
01313 *got_frame_ptr = 1;
01314 *(AVFrame *)data = p->frame;
01315
01316 return frame_size[dec_mode];
01317 }
01318
01319 #define OFFSET(x) offsetof(G723_1_Context, x)
01320 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
01321
01322 static const AVOption options[] = {
01323 { "postfilter", "postfilter on/off", OFFSET(postfilter), AV_OPT_TYPE_INT,
01324 { .i64 = 1 }, 0, 1, AD },
01325 { NULL }
01326 };
01327
01328
01329 static const AVClass g723_1dec_class = {
01330 .class_name = "G.723.1 decoder",
01331 .item_name = av_default_item_name,
01332 .option = options,
01333 .version = LIBAVUTIL_VERSION_INT,
01334 };
01335
01336 AVCodec ff_g723_1_decoder = {
01337 .name = "g723_1",
01338 .type = AVMEDIA_TYPE_AUDIO,
01339 .id = AV_CODEC_ID_G723_1,
01340 .priv_data_size = sizeof(G723_1_Context),
01341 .init = g723_1_decode_init,
01342 .decode = g723_1_decode_frame,
01343 .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
01344 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01345 .priv_class = &g723_1dec_class,
01346 };
01347
01348 #if CONFIG_G723_1_ENCODER
01349 #define BITSTREAM_WRITER_LE
01350 #include "put_bits.h"
01351
01352 static av_cold int g723_1_encode_init(AVCodecContext *avctx)
01353 {
01354 G723_1_Context *p = avctx->priv_data;
01355
01356 if (avctx->sample_rate != 8000) {
01357 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
01358 return -1;
01359 }
01360
01361 if (avctx->channels != 1) {
01362 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
01363 return AVERROR(EINVAL);
01364 }
01365
01366 if (avctx->bit_rate == 6300) {
01367 p->cur_rate = RATE_6300;
01368 } else if (avctx->bit_rate == 5300) {
01369 av_log(avctx, AV_LOG_ERROR, "Bitrate not supported yet, use 6.3k\n");
01370 return AVERROR_PATCHWELCOME;
01371 } else {
01372 av_log(avctx, AV_LOG_ERROR,
01373 "Bitrate not supported, use 6.3k\n");
01374 return AVERROR(EINVAL);
01375 }
01376 avctx->frame_size = 240;
01377 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
01378
01379 return 0;
01380 }
01381
01389 static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
01390 {
01391 int i;
01392 for (i = 0; i < FRAME_LEN; i++) {
01393 *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
01394 *fir = buf[i];
01395 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
01396 }
01397 }
01398
01405 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
01406 {
01407 int i, scale, temp;
01408 int16_t vector[LPC_FRAME];
01409
01410 scale_vector(vector, buf, LPC_FRAME);
01411
01412
01413 for (i = 0; i < LPC_FRAME; i++)
01414 vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
01415
01416
01417 temp = ff_dot_product(vector, vector, LPC_FRAME);
01418
01419
01420 temp += temp >> 10;
01421
01422
01423 scale = normalize_bits_int32(temp);
01424 autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
01425 (1 << 15)) >> 16;
01426
01427
01428 if (!autocorr[0]) {
01429 memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
01430 } else {
01431 for (i = 1; i <= LPC_ORDER; i++) {
01432 temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
01433 temp = MULL2((temp << scale), binomial_window[i - 1]);
01434 autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
01435 }
01436 }
01437 }
01438
01447 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
01448 {
01449 int16_t vector[LPC_ORDER];
01450 int16_t partial_corr;
01451 int i, j, temp;
01452
01453 memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
01454
01455 for (i = 0; i < LPC_ORDER; i++) {
01456
01457 temp = 0;
01458 for (j = 0; j < i; j++)
01459 temp -= lpc[j] * autocorr[i - j - 1];
01460 temp = ((autocorr[i] << 13) + temp) << 3;
01461
01462 if (FFABS(temp) >= (error << 16))
01463 break;
01464
01465 partial_corr = temp / (error << 1);
01466
01467 lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
01468 (1 << 15)) >> 16;
01469
01470
01471 temp = MULL2(temp, partial_corr);
01472 error = av_clipl_int32((int64_t)(error << 16) - temp +
01473 (1 << 15)) >> 16;
01474
01475 memcpy(vector, lpc, i * sizeof(int16_t));
01476 for (j = 0; j < i; j++) {
01477 temp = partial_corr * vector[i - j - 1] << 1;
01478 lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
01479 (1 << 15)) >> 16;
01480 }
01481 }
01482 }
01483
01491 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
01492 {
01493 int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
01494 int16_t *autocorr_ptr = autocorr;
01495 int16_t *lpc_ptr = lpc;
01496 int i, j;
01497
01498 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
01499 comp_autocorr(buf + i, autocorr_ptr);
01500 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
01501
01502 lpc_ptr += LPC_ORDER;
01503 autocorr_ptr += LPC_ORDER + 1;
01504 }
01505 }
01506
01507 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
01508 {
01509 int f[LPC_ORDER + 2];
01510
01511
01512
01513 int max, shift, cur_val, prev_val, count, p;
01514 int i, j;
01515 int64_t temp;
01516
01517
01518 for (i = 0; i < LPC_ORDER; i++)
01519 lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
01520
01521
01522 f[0] = f[1] = 1 << 25;
01523
01524
01525 for (i = 0; i < LPC_ORDER / 2; i++) {
01526
01527 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
01528
01529 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
01530 }
01531
01532
01533 f[LPC_ORDER] >>= 1;
01534 f[LPC_ORDER + 1] >>= 1;
01535
01536
01537 max = FFABS(f[0]);
01538 for (i = 1; i < LPC_ORDER + 2; i++)
01539 max = FFMAX(max, FFABS(f[i]));
01540
01541 shift = normalize_bits_int32(max);
01542
01543 for (i = 0; i < LPC_ORDER + 2; i++)
01544 f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
01545
01550 p = 0;
01551 temp = 0;
01552 for (i = 0; i <= LPC_ORDER / 2; i++)
01553 temp += f[2 * i] * cos_tab[0];
01554 prev_val = av_clipl_int32(temp << 1);
01555 count = 0;
01556 for ( i = 1; i < COS_TBL_SIZE / 2; i++) {
01557
01558 temp = 0;
01559 for (j = 0; j <= LPC_ORDER / 2; j++)
01560 temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
01561 cur_val = av_clipl_int32(temp << 1);
01562
01563
01564 if ((cur_val ^ prev_val) < 0) {
01565 int abs_cur = FFABS(cur_val);
01566 int abs_prev = FFABS(prev_val);
01567 int sum = abs_cur + abs_prev;
01568
01569 shift = normalize_bits_int32(sum);
01570 sum <<= shift;
01571 abs_prev = abs_prev << shift >> 8;
01572 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
01573
01574 if (count == LPC_ORDER)
01575 break;
01576
01577
01578 p ^= 1;
01579
01580
01581 temp = 0;
01582 for (j = 0; j <= LPC_ORDER / 2; j++){
01583 temp += f[LPC_ORDER - 2 * j + p] *
01584 cos_tab[i * j % COS_TBL_SIZE];
01585 }
01586 cur_val = av_clipl_int32(temp<<1);
01587 }
01588 prev_val = cur_val;
01589 }
01590
01591 if (count != LPC_ORDER)
01592 memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
01593 }
01594
01602 #define get_index(num, offset, size) \
01603 {\
01604 int error, max = -1;\
01605 int16_t temp[4];\
01606 int i, j;\
01607 for (i = 0; i < LSP_CB_SIZE; i++) {\
01608 for (j = 0; j < size; j++){\
01609 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
01610 (1 << 14)) >> 15;\
01611 }\
01612 error = dot_product(lsp + (offset), temp, size) << 1;\
01613 error -= dot_product(lsp_band##num[i], temp, size);\
01614 if (error > max) {\
01615 max = error;\
01616 lsp_index[num] = i;\
01617 }\
01618 }\
01619 }
01620
01627 static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
01628 {
01629 int16_t weight[LPC_ORDER];
01630 int16_t min, max;
01631 int shift, i;
01632
01633
01634 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
01635 weight[LPC_ORDER - 1] = (1 << 20) /
01636 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
01637
01638 for (i = 1; i < LPC_ORDER - 1; i++) {
01639 min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
01640 if (min > 0x20)
01641 weight[i] = (1 << 20) / min;
01642 else
01643 weight[i] = INT16_MAX;
01644 }
01645
01646
01647 max = 0;
01648 for (i = 0; i < LPC_ORDER; i++)
01649 max = FFMAX(weight[i], max);
01650
01651 shift = normalize_bits_int16(max);
01652 for (i = 0; i < LPC_ORDER; i++) {
01653 weight[i] <<= shift;
01654 }
01655
01656
01657 for (i = 0; i < LPC_ORDER; i++) {
01658 lsp[i] -= dc_lsp[i] +
01659 (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
01660 }
01661
01662 get_index(0, 0, 3);
01663 get_index(1, 3, 3);
01664 get_index(2, 6, 4);
01665 }
01666
01673 static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
01674 int16_t *unq_lpc, int16_t *buf)
01675 {
01676 int16_t vector[FRAME_LEN + LPC_ORDER];
01677 int i, j, k, l = 0;
01678
01679 memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
01680 memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
01681 memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
01682
01683 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
01684 for (k = 0; k < LPC_ORDER; k++) {
01685 flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
01686 (1 << 14)) >> 15;
01687 flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
01688 percept_flt_tbl[1][k] +
01689 (1 << 14)) >> 15;
01690 }
01691 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
01692 buf + i, 0);
01693 l += LPC_ORDER;
01694 }
01695 memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
01696 memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
01697 }
01698
01705 static int estimate_pitch(int16_t *buf, int start)
01706 {
01707 int max_exp = 32;
01708 int max_ccr = 0x4000;
01709 int max_eng = 0x7fff;
01710 int index = PITCH_MIN;
01711 int offset = start - PITCH_MIN + 1;
01712
01713 int ccr, eng, orig_eng, ccr_eng, exp;
01714 int diff, temp;
01715
01716 int i;
01717
01718 orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
01719
01720 for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
01721 offset--;
01722
01723
01724 orig_eng += buf[offset] * buf[offset] -
01725 buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
01726 ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
01727 if (ccr <= 0)
01728 continue;
01729
01730
01731 exp = normalize_bits_int32(ccr);
01732 ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
01733 exp <<= 1;
01734 ccr *= ccr;
01735 temp = normalize_bits_int32(ccr);
01736 ccr = ccr << temp >> 16;
01737 exp += temp;
01738
01739 temp = normalize_bits_int32(orig_eng);
01740 eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
01741 exp -= temp;
01742
01743 if (ccr >= eng) {
01744 exp--;
01745 ccr >>= 1;
01746 }
01747 if (exp > max_exp)
01748 continue;
01749
01750 if (exp + 1 < max_exp)
01751 goto update;
01752
01753
01754 if (exp + 1 == max_exp)
01755 temp = max_ccr >> 1;
01756 else
01757 temp = max_ccr;
01758 ccr_eng = ccr * max_eng;
01759 diff = ccr_eng - eng * temp;
01760 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
01761 update:
01762 index = i;
01763 max_exp = exp;
01764 max_ccr = ccr;
01765 max_eng = eng;
01766 }
01767 }
01768 return index;
01769 }
01770
01778 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
01779 {
01780 int ccr, eng, max_ccr, max_eng;
01781 int exp, max, diff;
01782 int energy[15];
01783 int i, j;
01784
01785 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
01786
01787 energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
01788
01789 energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
01790 }
01791
01792
01793 energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
01794
01795
01796 max = 0;
01797 for (i = 0; i < 15; i++)
01798 max = FFMAX(max, FFABS(energy[i]));
01799
01800 exp = normalize_bits_int32(max);
01801 for (i = 0; i < 15; i++) {
01802 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
01803 (1 << 15)) >> 16;
01804 }
01805
01806 hf->index = -1;
01807 hf->gain = 0;
01808 max_ccr = 1;
01809 max_eng = 0x7fff;
01810
01811 for (i = 0; i <= 6; i++) {
01812 eng = energy[i << 1];
01813 ccr = energy[(i << 1) + 1];
01814
01815 if (ccr <= 0)
01816 continue;
01817
01818 ccr = (ccr * ccr + (1 << 14)) >> 15;
01819 diff = ccr * max_eng - eng * max_ccr;
01820 if (diff > 0) {
01821 max_ccr = ccr;
01822 max_eng = eng;
01823 hf->index = i;
01824 }
01825 }
01826
01827 if (hf->index == -1) {
01828 hf->index = pitch_lag;
01829 return;
01830 }
01831
01832 eng = energy[14] * max_eng;
01833 eng = (eng >> 2) + (eng >> 3);
01834 ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
01835 if (eng < ccr) {
01836 eng = energy[(hf->index << 1) + 1];
01837
01838 if (eng >= max_eng)
01839 hf->gain = 0x2800;
01840 else
01841 hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
01842 }
01843 hf->index += pitch_lag - 3;
01844 }
01845
01851 static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
01852 {
01853 int i;
01854
01855 for (i = 0; i < SUBFRAME_LEN; i++) {
01856 int64_t temp = hf->gain * src[i - hf->index] << 1;
01857 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
01858 }
01859 }
01860
01861 static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
01862 {
01863 int i;
01864 for (i = 0; i < SUBFRAME_LEN; i++) {
01865 int64_t temp = hf->gain * src[i - hf->index] << 1;
01866 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
01867 (1 << 15)) >> 16;
01868
01869 }
01870 }
01871
01881 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
01882 int16_t *perf_fir, int16_t *perf_iir,
01883 const int16_t *src, int16_t *dest, int scale)
01884 {
01885 int i, j;
01886 int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
01887 int64_t buf[SUBFRAME_LEN];
01888
01889 int16_t *bptr_16 = buf_16 + LPC_ORDER;
01890
01891 memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
01892 memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
01893
01894 for (i = 0; i < SUBFRAME_LEN; i++) {
01895 int64_t temp = 0;
01896 for (j = 1; j <= LPC_ORDER; j++)
01897 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
01898
01899 buf[i] = (src[i] << 15) + (temp << 3);
01900 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
01901 }
01902
01903 for (i = 0; i < SUBFRAME_LEN; i++) {
01904 int64_t fir = 0, iir = 0;
01905 for (j = 1; j <= LPC_ORDER; j++) {
01906 fir -= perf_lpc[j - 1] * bptr_16[i - j];
01907 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
01908 }
01909 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
01910 (1 << 15)) >> 16;
01911 }
01912 memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
01913 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
01914 sizeof(int16_t) * LPC_ORDER);
01915 }
01916
01923 static void acb_search(G723_1_Context *p, int16_t *residual,
01924 int16_t *impulse_resp, const int16_t *buf,
01925 int index)
01926 {
01927
01928 int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
01929
01930 const int16_t *cb_tbl = adaptive_cb_gain85;
01931
01932 int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
01933
01934 int pitch_lag = p->pitch_lag[index >> 1];
01935 int acb_lag = 1;
01936 int acb_gain = 0;
01937 int odd_frame = index & 1;
01938 int iter = 3 + odd_frame;
01939 int count = 0;
01940 int tbl_size = 85;
01941
01942 int i, j, k, l, max;
01943 int64_t temp;
01944
01945 if (!odd_frame) {
01946 if (pitch_lag == PITCH_MIN)
01947 pitch_lag++;
01948 else
01949 pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
01950 }
01951
01952 for (i = 0; i < iter; i++) {
01953 get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
01954
01955 for (j = 0; j < SUBFRAME_LEN; j++) {
01956 temp = 0;
01957 for (k = 0; k <= j; k++)
01958 temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
01959 flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
01960 (1 << 15)) >> 16;
01961 }
01962
01963 for (j = PITCH_ORDER - 2; j >= 0; j--) {
01964 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
01965 for (k = 1; k < SUBFRAME_LEN; k++) {
01966 temp = (flt_buf[j + 1][k - 1] << 15) +
01967 residual[j] * impulse_resp[k];
01968 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
01969 }
01970 }
01971
01972
01973 for (j = 0; j < PITCH_ORDER; j++) {
01974 temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
01975 ccr_buf[count++] = av_clipl_int32(temp << 1);
01976 }
01977
01978
01979 for (j = 0; j < PITCH_ORDER; j++) {
01980 ccr_buf[count++] = dot_product(flt_buf[j], flt_buf[j],
01981 SUBFRAME_LEN);
01982 }
01983
01984 for (j = 1; j < PITCH_ORDER; j++) {
01985 for (k = 0; k < j; k++) {
01986 temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
01987 ccr_buf[count++] = av_clipl_int32(temp<<2);
01988 }
01989 }
01990 }
01991
01992
01993 max = 0;
01994 for (i = 0; i < 20 * iter; i++)
01995 max = FFMAX(max, FFABS(ccr_buf[i]));
01996
01997 temp = normalize_bits_int32(max);
01998
01999 for (i = 0; i < 20 * iter; i++){
02000 ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
02001 (1 << 15)) >> 16;
02002 }
02003
02004 max = 0;
02005 for (i = 0; i < iter; i++) {
02006
02007 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
02008 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
02009 cb_tbl = adaptive_cb_gain170;
02010 tbl_size = 170;
02011 }
02012
02013 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
02014 temp = 0;
02015 for (l = 0; l < 20; l++)
02016 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
02017 temp = av_clipl_int32(temp);
02018
02019 if (temp > max) {
02020 max = temp;
02021 acb_gain = j;
02022 acb_lag = i;
02023 }
02024 }
02025 }
02026
02027 if (!odd_frame) {
02028 pitch_lag += acb_lag - 1;
02029 acb_lag = 1;
02030 }
02031
02032 p->pitch_lag[index >> 1] = pitch_lag;
02033 p->subframe[index].ad_cb_lag = acb_lag;
02034 p->subframe[index].ad_cb_gain = acb_gain;
02035 }
02036
02043 static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
02044 int16_t *buf)
02045 {
02046 int i, j;
02047
02048 for (i = 0; i < SUBFRAME_LEN; i++) {
02049 int64_t temp = buf[i] << 14;
02050 for (j = 0; j <= i; j++)
02051 temp -= residual[j] * impulse_resp[i - j];
02052
02053 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
02054 }
02055 }
02056
02063 static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
02064 int16_t *buf, int pulse_cnt, int pitch_lag)
02065 {
02066 FCBParam param;
02067 int16_t impulse_r[SUBFRAME_LEN];
02068 int16_t temp_corr[SUBFRAME_LEN];
02069 int16_t impulse_corr[SUBFRAME_LEN];
02070
02071 int ccr1[SUBFRAME_LEN];
02072 int ccr2[SUBFRAME_LEN];
02073 int amp, err, max, max_amp_index, min, scale, i, j, k, l;
02074
02075 int64_t temp;
02076
02077
02078 memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
02079 param.dirac_train = 0;
02080 if (pitch_lag < SUBFRAME_LEN - 2) {
02081 param.dirac_train = 1;
02082 gen_dirac_train(impulse_r, pitch_lag);
02083 }
02084
02085 for (i = 0; i < SUBFRAME_LEN; i++)
02086 temp_corr[i] = impulse_r[i] >> 1;
02087
02088
02089 temp = dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
02090
02091 scale = normalize_bits_int32(temp);
02092 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
02093
02094 for (i = 1; i < SUBFRAME_LEN; i++) {
02095 temp = dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i);
02096 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
02097 }
02098
02099
02100 scale -= 4;
02101 for (i = 0; i < SUBFRAME_LEN; i++){
02102 temp = dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
02103 if (scale < 0)
02104 ccr1[i] = temp >> -scale;
02105 else
02106 ccr1[i] = av_clipl_int32(temp << scale);
02107 }
02108
02109
02110 for (i = 0; i < GRID_SIZE; i++) {
02111
02112 max = 0;
02113 for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
02114 temp = FFABS(ccr1[j]);
02115 if (temp >= max) {
02116 max = temp;
02117 param.pulse_pos[0] = j;
02118 }
02119 }
02120
02121
02122 amp = max;
02123 min = 1 << 30;
02124 max_amp_index = GAIN_LEVELS - 2;
02125 for (j = max_amp_index; j >= 2; j--) {
02126 temp = av_clipl_int32((int64_t)fixed_cb_gain[j] *
02127 impulse_corr[0] << 1);
02128 temp = FFABS(temp - amp);
02129 if (temp < min) {
02130 min = temp;
02131 max_amp_index = j;
02132 }
02133 }
02134
02135 max_amp_index--;
02136
02137 for (j = 1; j < 5; j++) {
02138 for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
02139 temp_corr[k] = 0;
02140 ccr2[k] = ccr1[k];
02141 }
02142 param.amp_index = max_amp_index + j - 2;
02143 amp = fixed_cb_gain[param.amp_index];
02144
02145 param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
02146 temp_corr[param.pulse_pos[0]] = 1;
02147
02148 for (k = 1; k < pulse_cnt; k++) {
02149 max = -1 << 30;
02150 for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
02151 if (temp_corr[l])
02152 continue;
02153 temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
02154 temp = av_clipl_int32((int64_t)temp *
02155 param.pulse_sign[k - 1] << 1);
02156 ccr2[l] -= temp;
02157 temp = FFABS(ccr2[l]);
02158 if (temp > max) {
02159 max = temp;
02160 param.pulse_pos[k] = l;
02161 }
02162 }
02163
02164 param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
02165 -amp : amp;
02166 temp_corr[param.pulse_pos[k]] = 1;
02167 }
02168
02169
02170 memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
02171
02172 for (k = 0; k < pulse_cnt; k++)
02173 temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
02174
02175 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
02176 temp = 0;
02177 for (l = 0; l <= k; l++) {
02178 int prod = av_clipl_int32((int64_t)temp_corr[l] *
02179 impulse_r[k - l] << 1);
02180 temp = av_clipl_int32(temp + prod);
02181 }
02182 temp_corr[k] = temp << 2 >> 16;
02183 }
02184
02185
02186 err = 0;
02187 for (k = 0; k < SUBFRAME_LEN; k++) {
02188 int64_t prod;
02189 prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
02190 err = av_clipl_int32(err - prod);
02191 prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
02192 err = av_clipl_int32(err + prod);
02193 }
02194
02195
02196 if (err < optim->min_err) {
02197 optim->min_err = err;
02198 optim->grid_index = i;
02199 optim->amp_index = param.amp_index;
02200 optim->dirac_train = param.dirac_train;
02201
02202 for (k = 0; k < pulse_cnt; k++) {
02203 optim->pulse_sign[k] = param.pulse_sign[k];
02204 optim->pulse_pos[k] = param.pulse_pos[k];
02205 }
02206 }
02207 }
02208 }
02209 }
02210
02217 static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
02218 int16_t *buf, int pulse_cnt)
02219 {
02220 int i, j;
02221
02222 j = PULSE_MAX - pulse_cnt;
02223
02224 subfrm->pulse_sign = 0;
02225 subfrm->pulse_pos = 0;
02226
02227 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
02228 int val = buf[optim->grid_index + (i << 1)];
02229 if (!val) {
02230 subfrm->pulse_pos += combinatorial_table[j][i];
02231 } else {
02232 subfrm->pulse_sign <<= 1;
02233 if (val < 0) subfrm->pulse_sign++;
02234 j++;
02235
02236 if (j == PULSE_MAX) break;
02237 }
02238 }
02239 subfrm->amp_index = optim->amp_index;
02240 subfrm->grid_index = optim->grid_index;
02241 subfrm->dirac_train = optim->dirac_train;
02242 }
02243
02250 static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
02251 int16_t *buf, int index)
02252 {
02253 FCBParam optim;
02254 int pulse_cnt = pulses[index];
02255 int i;
02256
02257 optim.min_err = 1 << 30;
02258 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
02259
02260 if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
02261 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
02262 p->pitch_lag[index >> 1]);
02263 }
02264
02265
02266 memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
02267 for (i = 0; i < pulse_cnt; i++)
02268 buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
02269
02270 pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
02271
02272 if (optim.dirac_train)
02273 gen_dirac_train(buf, p->pitch_lag[index >> 1]);
02274 }
02275
02282 static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size)
02283 {
02284 PutBitContext pb;
02285 int info_bits, i, temp;
02286
02287 init_put_bits(&pb, frame, size);
02288
02289 if (p->cur_rate == RATE_6300) {
02290 info_bits = 0;
02291 put_bits(&pb, 2, info_bits);
02292 }
02293
02294 put_bits(&pb, 8, p->lsp_index[2]);
02295 put_bits(&pb, 8, p->lsp_index[1]);
02296 put_bits(&pb, 8, p->lsp_index[0]);
02297
02298 put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
02299 put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
02300 put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
02301 put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
02302
02303
02304 for (i = 0; i < SUBFRAMES; i++) {
02305 temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
02306 p->subframe[i].amp_index;
02307 if (p->cur_rate == RATE_6300)
02308 temp += p->subframe[i].dirac_train << 11;
02309 put_bits(&pb, 12, temp);
02310 }
02311
02312 put_bits(&pb, 1, p->subframe[0].grid_index);
02313 put_bits(&pb, 1, p->subframe[1].grid_index);
02314 put_bits(&pb, 1, p->subframe[2].grid_index);
02315 put_bits(&pb, 1, p->subframe[3].grid_index);
02316
02317 if (p->cur_rate == RATE_6300) {
02318 skip_put_bits(&pb, 1);
02319
02320
02321 temp = (p->subframe[0].pulse_pos >> 16) * 810 +
02322 (p->subframe[1].pulse_pos >> 14) * 90 +
02323 (p->subframe[2].pulse_pos >> 16) * 9 +
02324 (p->subframe[3].pulse_pos >> 14);
02325 put_bits(&pb, 13, temp);
02326
02327 put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
02328 put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
02329 put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
02330 put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
02331
02332 put_bits(&pb, 6, p->subframe[0].pulse_sign);
02333 put_bits(&pb, 5, p->subframe[1].pulse_sign);
02334 put_bits(&pb, 6, p->subframe[2].pulse_sign);
02335 put_bits(&pb, 5, p->subframe[3].pulse_sign);
02336 }
02337
02338 flush_put_bits(&pb);
02339 return frame_size[info_bits];
02340 }
02341
02342 static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
02343 const AVFrame *frame, int *got_packet_ptr)
02344 {
02345 G723_1_Context *p = avctx->priv_data;
02346 int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
02347 int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
02348 int16_t cur_lsp[LPC_ORDER];
02349 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
02350 int16_t vector[FRAME_LEN + PITCH_MAX];
02351 int offset, ret;
02352 int16_t *in = (const int16_t *)frame->data[0];
02353
02354 HFParam hf[4];
02355 int i, j;
02356
02357 highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
02358
02359 memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
02360 memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
02361
02362 comp_lpc_coeff(vector, unq_lpc);
02363 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
02364 lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
02365
02366
02367 memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
02368 sizeof(int16_t) * SUBFRAME_LEN);
02369 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
02370 sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
02371 memcpy(p->prev_data, in + HALF_FRAME_LEN,
02372 sizeof(int16_t) * HALF_FRAME_LEN);
02373 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
02374
02375 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
02376
02377 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
02378 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
02379 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
02380
02381 scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
02382
02383 p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
02384 p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
02385
02386 for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
02387 comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
02388
02389 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
02390 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
02391 memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
02392
02393 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
02394 harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
02395
02396 inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
02397 lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
02398
02399 memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
02400
02401 offset = 0;
02402 for (i = 0; i < SUBFRAMES; i++) {
02403 int16_t impulse_resp[SUBFRAME_LEN];
02404 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
02405 int16_t flt_in[SUBFRAME_LEN];
02406 int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
02407
02412 memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
02413 memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
02414 memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
02415
02416 flt_in[0] = 1 << 13;
02417 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
02418 zero, zero, flt_in, vector + PITCH_MAX, 1);
02419 harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
02420
02421
02422 flt_in[0] = 0;
02423 memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
02424 memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
02425
02426 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
02427 fir, iir, flt_in, vector + PITCH_MAX, 0);
02428 memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
02429 harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
02430
02431 acb_search(p, residual, impulse_resp, in, i);
02432 gen_acb_excitation(residual, p->prev_excitation,p->pitch_lag[i >> 1],
02433 &p->subframe[i], p->cur_rate);
02434 sub_acb_contrib(residual, impulse_resp, in);
02435
02436 fcb_search(p, impulse_resp, in, i);
02437
02438
02439 gen_acb_excitation(impulse_resp, p->prev_excitation, p->pitch_lag[i >> 1],
02440 &p->subframe[i], RATE_6300);
02441
02442 memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
02443 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
02444 for (j = 0; j < SUBFRAME_LEN; j++)
02445 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
02446 memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
02447 sizeof(int16_t) * SUBFRAME_LEN);
02448
02449
02450 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
02451 p->perf_fir_mem, p->perf_iir_mem,
02452 in, vector + PITCH_MAX, 0);
02453 memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
02454 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
02455 memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
02456 sizeof(int16_t) * SUBFRAME_LEN);
02457
02458 in += SUBFRAME_LEN;
02459 offset += LPC_ORDER;
02460 }
02461
02462 if ((ret = ff_alloc_packet2(avctx, avpkt, 24)))
02463 return ret;
02464
02465 *got_packet_ptr = 1;
02466 avpkt->size = pack_bitstream(p, avpkt->data, avpkt->size);
02467 return 0;
02468 }
02469
02470 AVCodec ff_g723_1_encoder = {
02471 .name = "g723_1",
02472 .type = AVMEDIA_TYPE_AUDIO,
02473 .id = AV_CODEC_ID_G723_1,
02474 .priv_data_size = sizeof(G723_1_Context),
02475 .init = g723_1_encode_init,
02476 .encode2 = g723_1_encode_frame,
02477 .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
02478 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,
02479 AV_SAMPLE_FMT_NONE},
02480 };
02481 #endif