00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "avcodec.h"
00029 #define BITSTREAM_READER_LE
00030 #include "get_bits.h"
00031 #include "acelp_vectors.h"
00032 #include "celp_filters.h"
00033 #include "celp_math.h"
00034 #include "lsp.h"
00035 #include "libavutil/lzo.h"
00036 #include "g723_1_data.h"
00037
00038 typedef struct g723_1_context {
00039 AVFrame frame;
00040 G723_1_Subframe subframe[4];
00041 FrameType cur_frame_type;
00042 FrameType past_frame_type;
00043 Rate cur_rate;
00044 uint8_t lsp_index[LSP_BANDS];
00045 int pitch_lag[2];
00046 int erased_frames;
00047
00048 int16_t prev_lsp[LPC_ORDER];
00049 int16_t prev_excitation[PITCH_MAX];
00050 int16_t excitation[PITCH_MAX + FRAME_LEN];
00051 int16_t synth_mem[LPC_ORDER];
00052 int16_t fir_mem[LPC_ORDER];
00053 int iir_mem[LPC_ORDER];
00054
00055 int random_seed;
00056 int interp_index;
00057 int interp_gain;
00058 int sid_gain;
00059 int cur_gain;
00060 int reflection_coef;
00061 int pf_gain;
00062
00063
00064 int16_t prev_data[HALF_FRAME_LEN];
00065 int16_t prev_weight_sig[PITCH_MAX];
00066
00067
00068 int16_t hpf_fir_mem;
00069 int hpf_iir_mem;
00070 int16_t perf_fir_mem[LPC_ORDER];
00071 int16_t perf_iir_mem[LPC_ORDER];
00072
00073 int16_t harmonic_mem[PITCH_MAX];
00074 } G723_1_Context;
00075
00076 static av_cold int g723_1_decode_init(AVCodecContext *avctx)
00077 {
00078 G723_1_Context *p = avctx->priv_data;
00079
00080 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00081 p->pf_gain = 1 << 12;
00082 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
00083
00084 avcodec_get_frame_defaults(&p->frame);
00085 avctx->coded_frame = &p->frame;
00086
00087 return 0;
00088 }
00089
00097 static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
00098 int buf_size)
00099 {
00100 GetBitContext gb;
00101 int ad_cb_len;
00102 int temp, info_bits, i;
00103
00104 init_get_bits(&gb, buf, buf_size * 8);
00105
00106
00107 info_bits = get_bits(&gb, 2);
00108
00109 if (info_bits == 3) {
00110 p->cur_frame_type = UntransmittedFrame;
00111 return 0;
00112 }
00113
00114
00115 p->lsp_index[2] = get_bits(&gb, 8);
00116 p->lsp_index[1] = get_bits(&gb, 8);
00117 p->lsp_index[0] = get_bits(&gb, 8);
00118
00119 if (info_bits == 2) {
00120 p->cur_frame_type = SIDFrame;
00121 p->subframe[0].amp_index = get_bits(&gb, 6);
00122 return 0;
00123 }
00124
00125
00126 p->cur_rate = info_bits ? Rate5k3 : Rate6k3;
00127 p->cur_frame_type = ActiveFrame;
00128
00129 p->pitch_lag[0] = get_bits(&gb, 7);
00130 if (p->pitch_lag[0] > 123)
00131 return -1;
00132 p->pitch_lag[0] += PITCH_MIN;
00133 p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
00134
00135 p->pitch_lag[1] = get_bits(&gb, 7);
00136 if (p->pitch_lag[1] > 123)
00137 return -1;
00138 p->pitch_lag[1] += PITCH_MIN;
00139 p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
00140 p->subframe[0].ad_cb_lag = 1;
00141 p->subframe[2].ad_cb_lag = 1;
00142
00143 for (i = 0; i < SUBFRAMES; i++) {
00144
00145 temp = get_bits(&gb, 12);
00146 ad_cb_len = 170;
00147 p->subframe[i].dirac_train = 0;
00148 if (p->cur_rate == Rate6k3 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
00149 p->subframe[i].dirac_train = temp >> 11;
00150 temp &= 0x7ff;
00151 ad_cb_len = 85;
00152 }
00153 p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
00154 if (p->subframe[i].ad_cb_gain < ad_cb_len) {
00155 p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
00156 GAIN_LEVELS;
00157 } else {
00158 return -1;
00159 }
00160 }
00161
00162 p->subframe[0].grid_index = get_bits1(&gb);
00163 p->subframe[1].grid_index = get_bits1(&gb);
00164 p->subframe[2].grid_index = get_bits1(&gb);
00165 p->subframe[3].grid_index = get_bits1(&gb);
00166
00167 if (p->cur_rate == Rate6k3) {
00168 skip_bits1(&gb);
00169
00170
00171 temp = get_bits(&gb, 13);
00172 p->subframe[0].pulse_pos = temp / 810;
00173
00174 temp -= p->subframe[0].pulse_pos * 810;
00175 p->subframe[1].pulse_pos = FASTDIV(temp, 90);
00176
00177 temp -= p->subframe[1].pulse_pos * 90;
00178 p->subframe[2].pulse_pos = FASTDIV(temp, 9);
00179 p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
00180
00181 p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
00182 get_bits(&gb, 16);
00183 p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
00184 get_bits(&gb, 14);
00185 p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
00186 get_bits(&gb, 16);
00187 p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
00188 get_bits(&gb, 14);
00189
00190 p->subframe[0].pulse_sign = get_bits(&gb, 6);
00191 p->subframe[1].pulse_sign = get_bits(&gb, 5);
00192 p->subframe[2].pulse_sign = get_bits(&gb, 6);
00193 p->subframe[3].pulse_sign = get_bits(&gb, 5);
00194 } else {
00195 p->subframe[0].pulse_pos = get_bits(&gb, 12);
00196 p->subframe[1].pulse_pos = get_bits(&gb, 12);
00197 p->subframe[2].pulse_pos = get_bits(&gb, 12);
00198 p->subframe[3].pulse_pos = get_bits(&gb, 12);
00199
00200 p->subframe[0].pulse_sign = get_bits(&gb, 4);
00201 p->subframe[1].pulse_sign = get_bits(&gb, 4);
00202 p->subframe[2].pulse_sign = get_bits(&gb, 4);
00203 p->subframe[3].pulse_sign = get_bits(&gb, 4);
00204 }
00205
00206 return 0;
00207 }
00208
00212 static int16_t square_root(int val)
00213 {
00214 return (ff_sqrt(val << 1) >> 1) & (~1);
00215 }
00216
00223 static int normalize_bits(int num, int width)
00224 {
00225 int i = 0;
00226 int bits = (width) ? 31 : 15;
00227
00228 if (num) {
00229 if (num == -1)
00230 return bits;
00231 if (num < 0)
00232 num = ~num;
00233 i= bits - av_log2(num) - 1;
00234 i= FFMAX(i, 0);
00235 }
00236 return i;
00237 }
00238
00239 #define normalize_bits_int16(num) normalize_bits(num, 0)
00240 #define normalize_bits_int32(num) normalize_bits(num, 1)
00241 #define dot_product(a,b,c,d) (ff_dot_product(a,b,c)<<(d))
00242
00246 static int scale_vector(int16_t *vector, int length)
00247 {
00248 int bits, scale, max = 0;
00249 int i;
00250
00251 const int16_t shift_table[16] = {
00252 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
00253 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x7fff
00254 };
00255
00256 for (i = 0; i < length; i++)
00257 max = FFMAX(max, FFABS(vector[i]));
00258
00259 bits = normalize_bits(max, 0);
00260 scale = shift_table[bits];
00261
00262 for (i = 0; i < length; i++)
00263 vector[i] = (vector[i] * scale) >> 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(int16_t));
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 int64_t 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_clipl_int32(((temp1 + temp2) << 1) + (1 << 15)) >> 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(int16_t));
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(int16_t));
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 Rate cur_rate, int pitch_lag, int index)
00468 {
00469 int temp, i, j;
00470
00471 memset(vector, 0, SUBFRAME_LEN * sizeof(int16_t));
00472
00473 if (cur_rate == Rate6k3) {
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
00542 static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
00543 int pitch_lag, G723_1_Subframe subfrm,
00544 Rate cur_rate)
00545 {
00546 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
00547 const int16_t *cb_ptr;
00548 int lag = pitch_lag + subfrm.ad_cb_lag - 1;
00549
00550 int i;
00551 int64_t sum;
00552
00553 get_residual(residual, prev_excitation, lag);
00554
00555
00556 if (cur_rate == Rate6k3 && pitch_lag < SUBFRAME_LEN - 2) {
00557 cb_ptr = adaptive_cb_gain85;
00558 } else
00559 cb_ptr = adaptive_cb_gain170;
00560
00561
00562 cb_ptr += subfrm.ad_cb_gain * 20;
00563 for (i = 0; i < SUBFRAME_LEN; i++) {
00564 sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER);
00565 vector[i] = av_clipl_int32((sum << 2) + (1 << 15)) >> 16;
00566 }
00567 }
00568
00579 static int autocorr_max(G723_1_Context *p, int offset, int *ccr_max,
00580 int pitch_lag, int length, int dir)
00581 {
00582 int limit, ccr, lag = 0;
00583 int16_t *buf = p->excitation + offset;
00584 int i;
00585
00586 pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
00587 limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
00588
00589 for (i = pitch_lag - 3; i <= limit; i++) {
00590 ccr = ff_dot_product(buf, buf + dir * i, length)<<1;
00591
00592 if (ccr > *ccr_max) {
00593 *ccr_max = ccr;
00594 lag = i;
00595 }
00596 }
00597 return lag;
00598 }
00599
00610 static void comp_ppf_gains(int lag, PPFParam *ppf, Rate cur_rate,
00611 int tgt_eng, int ccr, int res_eng)
00612 {
00613 int pf_residual;
00614 int64_t temp1, temp2;
00615
00616 ppf->index = lag;
00617
00618 temp1 = tgt_eng * res_eng >> 1;
00619 temp2 = ccr * ccr << 1;
00620
00621 if (temp2 > temp1) {
00622 if (ccr >= res_eng) {
00623 ppf->opt_gain = ppf_gain_weight[cur_rate];
00624 } else {
00625 ppf->opt_gain = (ccr << 15) / res_eng *
00626 ppf_gain_weight[cur_rate] >> 15;
00627 }
00628
00629 temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
00630 temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
00631 pf_residual = av_clipl_int32(temp1 + temp2 + (1 << 15)) >> 16;
00632
00633 if (tgt_eng >= pf_residual << 1) {
00634 temp1 = 0x7fff;
00635 } else {
00636 temp1 = (tgt_eng << 14) / pf_residual;
00637 }
00638
00639
00640 ppf->sc_gain = square_root(temp1 << 16);
00641 } else {
00642 ppf->opt_gain = 0;
00643 ppf->sc_gain = 0x7fff;
00644 }
00645
00646 ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
00647 }
00648
00658 static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
00659 PPFParam *ppf, Rate cur_rate)
00660 {
00661
00662 int16_t scale;
00663 int i;
00664 int64_t temp1, temp2;
00665
00666
00667
00668
00669
00670
00671
00672
00673 int energy[5] = {0, 0, 0, 0, 0};
00674 int16_t *buf = p->excitation + offset;
00675 int fwd_lag = autocorr_max(p, offset, &energy[1], pitch_lag,
00676 SUBFRAME_LEN, 1);
00677 int back_lag = autocorr_max(p, offset, &energy[3], pitch_lag,
00678 SUBFRAME_LEN, -1);
00679
00680 ppf->index = 0;
00681 ppf->opt_gain = 0;
00682 ppf->sc_gain = 0x7fff;
00683
00684
00685 if (!back_lag && !fwd_lag)
00686 return;
00687
00688
00689 energy[0] = ff_dot_product(buf, buf, SUBFRAME_LEN)<<1;
00690
00691
00692 if (fwd_lag)
00693 energy[2] = ff_dot_product(buf + fwd_lag, buf + fwd_lag,
00694 SUBFRAME_LEN)<<1;
00695
00696
00697 if (back_lag)
00698 energy[4] = ff_dot_product(buf - back_lag, buf - back_lag,
00699 SUBFRAME_LEN)<<1;
00700
00701
00702 temp1 = 0;
00703 for (i = 0; i < 5; i++)
00704 temp1 = FFMAX(energy[i], temp1);
00705
00706 scale = normalize_bits(temp1, 1);
00707 for (i = 0; i < 5; i++)
00708 energy[i] = av_clipl_int32(energy[i] << scale) >> 16;
00709
00710 if (fwd_lag && !back_lag) {
00711 comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
00712 energy[2]);
00713 } else if (!fwd_lag) {
00714 comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
00715 energy[4]);
00716 } else {
00717
00718
00719
00720
00721
00722 temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
00723 temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
00724 if (temp1 >= temp2) {
00725 comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
00726 energy[2]);
00727 } else {
00728 comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
00729 energy[4]);
00730 }
00731 }
00732 }
00733
00744 static int comp_interp_index(G723_1_Context *p, int pitch_lag,
00745 int *exc_eng, int *scale)
00746 {
00747 int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
00748 int16_t *buf = p->excitation + offset;
00749
00750 int index, ccr, tgt_eng, best_eng, temp;
00751
00752 *scale = scale_vector(p->excitation, FRAME_LEN + PITCH_MAX);
00753
00754
00755 ccr = 0;
00756 index = autocorr_max(p, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
00757 ccr = av_clipl_int32((int64_t)ccr + (1 << 15)) >> 16;
00758
00759
00760 tgt_eng = ff_dot_product(buf, buf, SUBFRAME_LEN * 2)<<1;
00761 *exc_eng = av_clipl_int32(tgt_eng + (1 << 15)) >> 16;
00762
00763 if (ccr <= 0)
00764 return 0;
00765
00766
00767 best_eng = ff_dot_product(buf - index, buf - index,
00768 SUBFRAME_LEN * 2)<<1;
00769 best_eng = av_clipl_int32((int64_t)best_eng + (1 << 15)) >> 16;
00770
00771 temp = best_eng * *exc_eng >> 3;
00772
00773 if (temp < ccr * ccr) {
00774 return index;
00775 } else
00776 return 0;
00777 }
00778
00788 static void residual_interp(int16_t *buf, int16_t *out, int lag,
00789 int gain, int *rseed)
00790 {
00791 int i;
00792 if (lag) {
00793 int16_t *vector_ptr = buf + PITCH_MAX;
00794
00795 for (i = 0; i < lag; i++)
00796 vector_ptr[i - lag] = vector_ptr[i - lag] * 3 >> 2;
00797 av_memcpy_backptr((uint8_t*)vector_ptr, lag * sizeof(int16_t),
00798 FRAME_LEN * sizeof(int16_t));
00799 memcpy(out, vector_ptr, FRAME_LEN * sizeof(int16_t));
00800 } else {
00801 for (i = 0; i < FRAME_LEN; i++) {
00802 *rseed = *rseed * 521 + 259;
00803 out[i] = gain * *rseed >> 15;
00804 }
00805 memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(int16_t));
00806 }
00807 }
00808
00818 #define iir_filter(fir_coef, iir_coef, src, dest, width)\
00819 {\
00820 int m, n;\
00821 int res_shift = 16 & ~-(width);\
00822 int in_shift = 16 - res_shift;\
00823 \
00824 for (m = 0; m < SUBFRAME_LEN; m++) {\
00825 int64_t filter = 0;\
00826 for (n = 1; n <= LPC_ORDER; n++) {\
00827 filter -= (fir_coef)[n - 1] * (src)[m - n] -\
00828 (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
00829 }\
00830 \
00831 (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
00832 (1 << 15)) >> res_shift;\
00833 }\
00834 }
00835
00843 static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
00844 {
00845 int num, denom, gain, bits1, bits2;
00846 int i;
00847
00848 num = energy;
00849 denom = 0;
00850 for (i = 0; i < SUBFRAME_LEN; i++) {
00851 int64_t temp = buf[i] >> 2;
00852 temp = av_clipl_int32(MUL64(temp, temp) << 1);
00853 denom = av_clipl_int32(denom + temp);
00854 }
00855
00856 if (num && denom) {
00857 bits1 = normalize_bits(num, 1);
00858 bits2 = normalize_bits(denom, 1);
00859 num = num << bits1 >> 1;
00860 denom <<= bits2;
00861
00862 bits2 = 5 + bits1 - bits2;
00863 bits2 = FFMAX(0, bits2);
00864
00865 gain = (num >> 1) / (denom >> 16);
00866 gain = square_root(gain << 16 >> bits2);
00867 } else {
00868 gain = 1 << 12;
00869 }
00870
00871 for (i = 0; i < SUBFRAME_LEN; i++) {
00872 p->pf_gain = ((p->pf_gain << 4) - p->pf_gain + gain + (1 << 3)) >> 4;
00873 buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
00874 (1 << 10)) >> 11);
00875 }
00876 }
00877
00885 static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf)
00886 {
00887 int16_t filter_coef[2][LPC_ORDER], *buf_ptr;
00888 int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
00889 int i, j, k;
00890
00891 memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(int16_t));
00892 memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(int));
00893
00894 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
00895 for (k = 0; k < LPC_ORDER; k++) {
00896 filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
00897 (1 << 14)) >> 15;
00898 filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
00899 (1 << 14)) >> 15;
00900 }
00901 iir_filter(filter_coef[0], filter_coef[1], buf + i,
00902 filter_signal + i, 1);
00903 }
00904
00905 memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
00906 memcpy(p->iir_mem, filter_signal + FRAME_LEN, LPC_ORDER * sizeof(int));
00907
00908 buf_ptr = buf + LPC_ORDER;
00909 signal_ptr = filter_signal + LPC_ORDER;
00910 for (i = 0; i < SUBFRAMES; i++) {
00911 int16_t temp_vector[SUBFRAME_LEN];
00912 int16_t temp;
00913 int auto_corr[2];
00914 int scale, energy;
00915
00916
00917 memcpy(temp_vector, buf_ptr, SUBFRAME_LEN * sizeof(int16_t));
00918 scale = scale_vector(temp_vector, SUBFRAME_LEN);
00919
00920
00921 auto_corr[0] = ff_dot_product(temp_vector, temp_vector + 1,
00922 SUBFRAME_LEN - 1)<<1;
00923 auto_corr[1] = ff_dot_product(temp_vector, temp_vector,
00924 SUBFRAME_LEN)<<1;
00925
00926
00927 temp = auto_corr[1] >> 16;
00928 if (temp) {
00929 temp = (auto_corr[0] >> 2) / temp;
00930 }
00931 p->reflection_coef = ((p->reflection_coef << 2) - p->reflection_coef +
00932 temp + 2) >> 2;
00933 temp = (p->reflection_coef * 0xffffc >> 3) & 0xfffc;
00934
00935
00936 for (j = 0; j < SUBFRAME_LEN; j++) {
00937 buf_ptr[j] = av_clipl_int32(signal_ptr[j] +
00938 ((signal_ptr[j - 1] >> 16) *
00939 temp << 1)) >> 16;
00940 }
00941
00942
00943 temp = 2 * scale + 4;
00944 if (temp < 0) {
00945 energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
00946 } else
00947 energy = auto_corr[1] >> temp;
00948
00949 gain_scale(p, buf_ptr, energy);
00950
00951 buf_ptr += SUBFRAME_LEN;
00952 signal_ptr += SUBFRAME_LEN;
00953 }
00954 }
00955
00956 static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
00957 int *got_frame_ptr, AVPacket *avpkt)
00958 {
00959 G723_1_Context *p = avctx->priv_data;
00960 const uint8_t *buf = avpkt->data;
00961 int buf_size = avpkt->size;
00962 int16_t *out;
00963 int dec_mode = buf[0] & 3;
00964
00965 PPFParam ppf[SUBFRAMES];
00966 int16_t cur_lsp[LPC_ORDER];
00967 int16_t lpc[SUBFRAMES * LPC_ORDER];
00968 int16_t acb_vector[SUBFRAME_LEN];
00969 int16_t *vector_ptr;
00970 int bad_frame = 0, i, j, ret;
00971
00972 if (!buf_size || buf_size < frame_size[dec_mode]) {
00973 *got_frame_ptr = 0;
00974 return buf_size;
00975 }
00976
00977 if (unpack_bitstream(p, buf, buf_size) < 0) {
00978 bad_frame = 1;
00979 p->cur_frame_type = p->past_frame_type == ActiveFrame ?
00980 ActiveFrame : UntransmittedFrame;
00981 }
00982
00983 p->frame.nb_samples = FRAME_LEN + LPC_ORDER;
00984 if ((ret = avctx->get_buffer(avctx, &p->frame)) < 0) {
00985 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00986 return ret;
00987 }
00988 out= (int16_t*)p->frame.data[0];
00989
00990
00991 if(p->cur_frame_type == ActiveFrame) {
00992 if (!bad_frame) {
00993 p->erased_frames = 0;
00994 } else if(p->erased_frames != 3)
00995 p->erased_frames++;
00996
00997 inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
00998 lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
00999
01000
01001 memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(int16_t));
01002
01003
01004 memcpy(p->excitation, p->prev_excitation, PITCH_MAX * sizeof(int16_t));
01005 vector_ptr = p->excitation + PITCH_MAX;
01006 if (!p->erased_frames) {
01007
01008 p->interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
01009 p->subframe[3].amp_index) >> 1];
01010 for (i = 0; i < SUBFRAMES; i++) {
01011 gen_fcb_excitation(vector_ptr, p->subframe[i], p->cur_rate,
01012 p->pitch_lag[i >> 1], i);
01013 gen_acb_excitation(acb_vector, &p->excitation[SUBFRAME_LEN * i],
01014 p->pitch_lag[i >> 1], p->subframe[i],
01015 p->cur_rate);
01016
01017 for (j = 0; j < SUBFRAME_LEN; j++) {
01018 vector_ptr[j] = av_clip_int16(vector_ptr[j] << 1);
01019 vector_ptr[j] = av_clip_int16(vector_ptr[j] +
01020 acb_vector[j]);
01021 }
01022 vector_ptr += SUBFRAME_LEN;
01023 }
01024
01025 vector_ptr = p->excitation + PITCH_MAX;
01026
01027
01028 memcpy(out, vector_ptr, FRAME_LEN * sizeof(int16_t));
01029
01030 p->interp_index = comp_interp_index(p, p->pitch_lag[1],
01031 &p->sid_gain, &p->cur_gain);
01032
01033 for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
01034 comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
01035 ppf + j, p->cur_rate);
01036
01037
01038 memcpy(p->excitation, p->prev_excitation,
01039 PITCH_MAX * sizeof(int16_t));
01040 memcpy(vector_ptr, out, FRAME_LEN * sizeof(int16_t));
01041
01042
01043 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
01044 ff_acelp_weighted_vector_sum(out + LPC_ORDER + i, vector_ptr + i,
01045 vector_ptr + i + ppf[j].index,
01046 ppf[j].sc_gain, ppf[j].opt_gain,
01047 1 << 14, 15, SUBFRAME_LEN);
01048 } else {
01049 p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
01050 if (p->erased_frames == 3) {
01051
01052 memset(p->excitation, 0,
01053 (FRAME_LEN + PITCH_MAX) * sizeof(int16_t));
01054 memset(out, 0, (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
01055 } else {
01056
01057 residual_interp(p->excitation, out + LPC_ORDER, p->interp_index,
01058 p->interp_gain, &p->random_seed);
01059 }
01060 }
01061
01062 memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
01063 PITCH_MAX * sizeof(int16_t));
01064 } else {
01065 memset(out, 0, sizeof(int16_t)*FRAME_LEN);
01066 av_log(avctx, AV_LOG_WARNING,
01067 "G.723.1: Comfort noise generation not supported yet\n");
01068 return frame_size[dec_mode];
01069 }
01070
01071 p->past_frame_type = p->cur_frame_type;
01072
01073 memcpy(out, p->synth_mem, LPC_ORDER * sizeof(int16_t));
01074 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
01075 ff_celp_lp_synthesis_filter(out + i, &lpc[j * LPC_ORDER],
01076 out + i, SUBFRAME_LEN, LPC_ORDER,
01077 0, 1, 1 << 12);
01078 memcpy(p->synth_mem, out + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
01079
01080 formant_postfilter(p, lpc, out);
01081
01082 memmove(out, out + LPC_ORDER, sizeof(int16_t)*FRAME_LEN);
01083 p->frame.nb_samples = FRAME_LEN;
01084 *(AVFrame*)data = p->frame;
01085 *got_frame_ptr = 1;
01086
01087 return frame_size[dec_mode];
01088 }
01089
01090 AVCodec ff_g723_1_decoder = {
01091 .name = "g723_1",
01092 .type = AVMEDIA_TYPE_AUDIO,
01093 .id = CODEC_ID_G723_1,
01094 .priv_data_size = sizeof(G723_1_Context),
01095 .init = g723_1_decode_init,
01096 .decode = g723_1_decode_frame,
01097 .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
01098 .capabilities = CODEC_CAP_SUBFRAMES,
01099 };
01100
01101 #if CONFIG_G723_1_ENCODER
01102 #define BITSTREAM_WRITER_LE
01103 #include "put_bits.h"
01104
01105 static av_cold int g723_1_encode_init(AVCodecContext *avctx)
01106 {
01107 G723_1_Context *p = avctx->priv_data;
01108
01109 if (avctx->sample_rate != 8000) {
01110 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
01111 return -1;
01112 }
01113
01114 if (avctx->channels != 1) {
01115 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
01116 return AVERROR(EINVAL);
01117 }
01118
01119 if (avctx->bit_rate == 6300) {
01120 p->cur_rate = Rate6k3;
01121 } else if (avctx->bit_rate == 5300) {
01122 av_log(avctx, AV_LOG_ERROR, "Bitrate not supported yet, use 6.3k\n");
01123 return AVERROR_PATCHWELCOME;
01124 } else {
01125 av_log(avctx, AV_LOG_ERROR,
01126 "Bitrate not supported, use 6.3k\n");
01127 return AVERROR(EINVAL);
01128 }
01129 avctx->frame_size = 240;
01130 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
01131
01132 return 0;
01133 }
01134
01142 static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
01143 {
01144 int i;
01145 for (i = 0; i < FRAME_LEN; i++) {
01146 *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
01147 *fir = buf[i];
01148 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
01149 }
01150 }
01151
01158 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
01159 {
01160 int i, scale, temp;
01161 int16_t vector[LPC_FRAME];
01162
01163 memcpy(vector, buf, LPC_FRAME * sizeof(int16_t));
01164 scale_vector(vector, LPC_FRAME);
01165
01166
01167 for (i = 0; i < LPC_FRAME; i++)
01168 vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
01169
01170
01171 temp = dot_product(vector, vector, LPC_FRAME, 0);
01172
01173
01174 temp += temp >> 10;
01175
01176
01177 scale = normalize_bits_int32(temp);
01178 autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
01179 (1 << 15)) >> 16;
01180
01181
01182 if (!autocorr[0]) {
01183 memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
01184 } else {
01185 for (i = 1; i <= LPC_ORDER; i++) {
01186 temp = dot_product(vector, vector + i, LPC_FRAME - i, 0);
01187 temp = MULL2((temp << scale), binomial_window[i - 1]);
01188 autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
01189 }
01190 }
01191 }
01192
01201 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
01202 {
01203 int16_t vector[LPC_ORDER];
01204 int16_t partial_corr;
01205 int i, j, temp;
01206
01207 memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
01208
01209 for (i = 0; i < LPC_ORDER; i++) {
01210
01211 temp = 0;
01212 for (j = 0; j < i; j++)
01213 temp -= lpc[j] * autocorr[i - j - 1];
01214 temp = ((autocorr[i] << 13) + temp) << 3;
01215
01216 if (FFABS(temp) >= (error << 16))
01217 break;
01218
01219 partial_corr = temp / (error << 1);
01220
01221 lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
01222 (1 << 15)) >> 16;
01223
01224
01225 temp = MULL2(temp, partial_corr);
01226 error = av_clipl_int32((int64_t)(error << 16) - temp +
01227 (1 << 15)) >> 16;
01228
01229 memcpy(vector, lpc, i * sizeof(int16_t));
01230 for (j = 0; j < i; j++) {
01231 temp = partial_corr * vector[i - j - 1] << 1;
01232 lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
01233 (1 << 15)) >> 16;
01234 }
01235 }
01236 }
01237
01245 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
01246 {
01247 int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
01248 int16_t *autocorr_ptr = autocorr;
01249 int16_t *lpc_ptr = lpc;
01250 int i, j;
01251
01252 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
01253 comp_autocorr(buf + i, autocorr_ptr);
01254 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
01255
01256 lpc_ptr += LPC_ORDER;
01257 autocorr_ptr += LPC_ORDER + 1;
01258 }
01259 }
01260
01261 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
01262 {
01263 int f[LPC_ORDER + 2];
01264
01265
01266
01267 int max, shift, cur_val, prev_val, count, p;
01268 int i, j;
01269 int64_t temp;
01270
01271
01272 for (i = 0; i < LPC_ORDER; i++)
01273 lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
01274
01275
01276 f[0] = f[1] = 1 << 25;
01277
01278
01279 for (i = 0; i < LPC_ORDER / 2; i++) {
01280
01281 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
01282
01283 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
01284 }
01285
01286
01287 f[LPC_ORDER] >>= 1;
01288 f[LPC_ORDER + 1] >>= 1;
01289
01290
01291 max = FFABS(f[0]);
01292 for (i = 1; i < LPC_ORDER + 2; i++)
01293 max = FFMAX(max, FFABS(f[i]));
01294
01295 shift = normalize_bits_int32(max);
01296
01297 for (i = 0; i < LPC_ORDER + 2; i++)
01298 f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
01299
01304 p = 0;
01305 temp = 0;
01306 for (i = 0; i <= LPC_ORDER / 2; i++)
01307 temp += f[2 * i] * cos_tab[0];
01308 prev_val = av_clipl_int32(temp << 1);
01309 count = 0;
01310 for ( i = 1; i < COS_TBL_SIZE / 2; i++) {
01311
01312 temp = 0;
01313 for (j = 0; j <= LPC_ORDER / 2; j++)
01314 temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
01315 cur_val = av_clipl_int32(temp << 1);
01316
01317
01318 if ((cur_val ^ prev_val) < 0) {
01319 int abs_cur = FFABS(cur_val);
01320 int abs_prev = FFABS(prev_val);
01321 int sum = abs_cur + abs_prev;
01322
01323 shift = normalize_bits_int32(sum);
01324 sum <<= shift;
01325 abs_prev = abs_prev << shift >> 8;
01326 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
01327
01328 if (count == LPC_ORDER)
01329 break;
01330
01331
01332 p ^= 1;
01333
01334
01335 temp = 0;
01336 for (j = 0; j <= LPC_ORDER / 2; j++){
01337 temp += f[LPC_ORDER - 2 * j + p] *
01338 cos_tab[i * j % COS_TBL_SIZE];
01339 }
01340 cur_val = av_clipl_int32(temp<<1);
01341 }
01342 prev_val = cur_val;
01343 }
01344
01345 if (count != LPC_ORDER)
01346 memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
01347 }
01348
01356 #define get_index(num, offset, size) \
01357 {\
01358 int error, max = -1;\
01359 int16_t temp[4];\
01360 int i, j;\
01361 for (i = 0; i < LSP_CB_SIZE; i++) {\
01362 for (j = 0; j < size; j++){\
01363 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
01364 (1 << 14)) >> 15;\
01365 }\
01366 error = dot_product(lsp + (offset), temp, size, 1) << 1;\
01367 error -= dot_product(lsp_band##num[i], temp, size, 1);\
01368 if (error > max) {\
01369 max = error;\
01370 lsp_index[num] = i;\
01371 }\
01372 }\
01373 }
01374
01381 static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
01382 {
01383 int16_t weight[LPC_ORDER];
01384 int16_t min, max;
01385 int shift, i;
01386
01387
01388 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
01389 weight[LPC_ORDER - 1] = (1 << 20) /
01390 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
01391
01392 for (i = 1; i < LPC_ORDER - 1; i++) {
01393 min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
01394 if (min > 0x20)
01395 weight[i] = (1 << 20) / min;
01396 else
01397 weight[i] = INT16_MAX;
01398 }
01399
01400
01401 max = 0;
01402 for (i = 0; i < LPC_ORDER; i++)
01403 max = FFMAX(weight[i], max);
01404
01405 shift = normalize_bits_int16(max);
01406 for (i = 0; i < LPC_ORDER; i++) {
01407 weight[i] <<= shift;
01408 }
01409
01410
01411 for (i = 0; i < LPC_ORDER; i++) {
01412 lsp[i] -= dc_lsp[i] +
01413 (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
01414 }
01415
01416 get_index(0, 0, 3);
01417 get_index(1, 3, 3);
01418 get_index(2, 6, 4);
01419 }
01420
01427 static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
01428 int16_t *unq_lpc, int16_t *buf)
01429 {
01430 int16_t vector[FRAME_LEN + LPC_ORDER];
01431 int i, j, k, l = 0;
01432
01433 memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
01434 memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
01435 memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
01436
01437 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
01438 for (k = 0; k < LPC_ORDER; k++) {
01439 flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
01440 (1 << 14)) >> 15;
01441 flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
01442 percept_flt_tbl[1][k] +
01443 (1 << 14)) >> 15;
01444 }
01445 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
01446 buf + i, 0);
01447 l += LPC_ORDER;
01448 }
01449 memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
01450 memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
01451 }
01452
01459 static int estimate_pitch(int16_t *buf, int start)
01460 {
01461 int max_exp = 32;
01462 int max_ccr = 0x4000;
01463 int max_eng = 0x7fff;
01464 int index = PITCH_MIN;
01465 int offset = start - PITCH_MIN + 1;
01466
01467 int ccr, eng, orig_eng, ccr_eng, exp;
01468 int diff, temp;
01469
01470 int i;
01471
01472 orig_eng = dot_product(buf + offset, buf + offset, HALF_FRAME_LEN, 0);
01473
01474 for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
01475 offset--;
01476
01477
01478 orig_eng += buf[offset] * buf[offset] -
01479 buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
01480 ccr = dot_product(buf + start, buf + offset, HALF_FRAME_LEN, 0);
01481 if (ccr <= 0)
01482 continue;
01483
01484
01485 exp = normalize_bits_int32(ccr);
01486 ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
01487 exp <<= 1;
01488 ccr *= ccr;
01489 temp = normalize_bits_int32(ccr);
01490 ccr = ccr << temp >> 16;
01491 exp += temp;
01492
01493 temp = normalize_bits_int32(orig_eng);
01494 eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
01495 exp -= temp;
01496
01497 if (ccr >= eng) {
01498 exp--;
01499 ccr >>= 1;
01500 }
01501 if (exp > max_exp)
01502 continue;
01503
01504 if (exp + 1 < max_exp)
01505 goto update;
01506
01507
01508 if (exp + 1 == max_exp)
01509 temp = max_ccr >> 1;
01510 else
01511 temp = max_ccr;
01512 ccr_eng = ccr * max_eng;
01513 diff = ccr_eng - eng * temp;
01514 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
01515 update:
01516 index = i;
01517 max_exp = exp;
01518 max_ccr = ccr;
01519 max_eng = eng;
01520 }
01521 }
01522 return index;
01523 }
01524
01532 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
01533 {
01534 int ccr, eng, max_ccr, max_eng;
01535 int exp, max, diff;
01536 int energy[15];
01537 int i, j;
01538
01539 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
01540
01541 energy[i << 1] = dot_product(buf - j, buf - j, SUBFRAME_LEN, 0);
01542
01543 energy[(i << 1) + 1] = dot_product(buf, buf - j, SUBFRAME_LEN, 0);
01544 }
01545
01546
01547 energy[14] = dot_product(buf, buf, SUBFRAME_LEN, 0);
01548
01549
01550 max = 0;
01551 for (i = 0; i < 15; i++)
01552 max = FFMAX(max, FFABS(energy[i]));
01553
01554 exp = normalize_bits_int32(max);
01555 for (i = 0; i < 15; i++) {
01556 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
01557 (1 << 15)) >> 16;
01558 }
01559
01560 hf->index = -1;
01561 hf->gain = 0;
01562 max_ccr = 1;
01563 max_eng = 0x7fff;
01564
01565 for (i = 0; i <= 6; i++) {
01566 eng = energy[i << 1];
01567 ccr = energy[(i << 1) + 1];
01568
01569 if (ccr <= 0)
01570 continue;
01571
01572 ccr = (ccr * ccr + (1 << 14)) >> 15;
01573 diff = ccr * max_eng - eng * max_ccr;
01574 if (diff > 0) {
01575 max_ccr = ccr;
01576 max_eng = eng;
01577 hf->index = i;
01578 }
01579 }
01580
01581 if (hf->index == -1) {
01582 hf->index = pitch_lag;
01583 return;
01584 }
01585
01586 eng = energy[14] * max_eng;
01587 eng = (eng >> 2) + (eng >> 3);
01588 ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
01589 if (eng < ccr) {
01590 eng = energy[(hf->index << 1) + 1];
01591
01592 if (eng >= max_eng)
01593 hf->gain = 0x2800;
01594 else
01595 hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
01596 }
01597 hf->index += pitch_lag - 3;
01598 }
01599
01605 static void harmonic_filter(HFParam *hf, int16_t *src, int16_t *dest)
01606 {
01607 int i;
01608
01609 for (i = 0; i < SUBFRAME_LEN; i++) {
01610 int64_t temp = hf->gain * src[i - hf->index] << 1;
01611 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
01612 }
01613 }
01614
01615 static void harmonic_noise_sub(HFParam *hf, int16_t *src, int16_t *dest)
01616 {
01617 int i;
01618 for (i = 0; i < SUBFRAME_LEN; i++) {
01619 int64_t temp = hf->gain * src[i - hf->index] << 1;
01620 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
01621 (1 << 15)) >> 16;
01622
01623 }
01624 }
01625
01635 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
01636 int16_t *perf_fir, int16_t *perf_iir,
01637 int16_t *src, int16_t *dest, int scale)
01638 {
01639 int i, j;
01640 int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
01641 int64_t buf[SUBFRAME_LEN];
01642
01643 int16_t *bptr_16 = buf_16 + LPC_ORDER;
01644
01645 memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
01646 memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
01647
01648 for (i = 0; i < SUBFRAME_LEN; i++) {
01649 int64_t temp = 0;
01650 for (j = 1; j <= LPC_ORDER; j++)
01651 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
01652
01653 buf[i] = (src[i] << 15) + (temp << 3);
01654 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
01655 }
01656
01657 for (i = 0; i < SUBFRAME_LEN; i++) {
01658 int64_t fir = 0, iir = 0;
01659 for (j = 1; j <= LPC_ORDER; j++) {
01660 fir -= perf_lpc[j - 1] * bptr_16[i - j];
01661 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
01662 }
01663 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
01664 (1 << 15)) >> 16;
01665 }
01666 memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
01667 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
01668 sizeof(int16_t) * LPC_ORDER);
01669 }
01670
01677 static void acb_search(G723_1_Context *p, int16_t *residual,
01678 int16_t *impulse_resp, int16_t *buf,
01679 int index)
01680 {
01681
01682 int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
01683
01684 const int16_t *cb_tbl = adaptive_cb_gain85;
01685
01686 int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
01687
01688 int pitch_lag = p->pitch_lag[index >> 1];
01689 int acb_lag = 1;
01690 int acb_gain = 0;
01691 int odd_frame = index & 1;
01692 int iter = 3 + odd_frame;
01693 int count = 0;
01694 int tbl_size = 85;
01695
01696 int i, j, k, l, max;
01697 int64_t temp;
01698
01699 if (!odd_frame) {
01700 if (pitch_lag == PITCH_MIN)
01701 pitch_lag++;
01702 else
01703 pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
01704 }
01705
01706 for (i = 0; i < iter; i++) {
01707 get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
01708
01709 for (j = 0; j < SUBFRAME_LEN; j++) {
01710 temp = 0;
01711 for (k = 0; k <= j; k++)
01712 temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
01713 flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
01714 (1 << 15)) >> 16;
01715 }
01716
01717 for (j = PITCH_ORDER - 2; j >= 0; j--) {
01718 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
01719 for (k = 1; k < SUBFRAME_LEN; k++) {
01720 temp = (flt_buf[j + 1][k - 1] << 15) +
01721 residual[j] * impulse_resp[k];
01722 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
01723 }
01724 }
01725
01726
01727 for (j = 0; j < PITCH_ORDER; j++) {
01728 temp = dot_product(buf, flt_buf[j], SUBFRAME_LEN, 0);
01729 ccr_buf[count++] = av_clipl_int32(temp << 1);
01730 }
01731
01732
01733 for (j = 0; j < PITCH_ORDER; j++) {
01734 ccr_buf[count++] = dot_product(flt_buf[j], flt_buf[j],
01735 SUBFRAME_LEN, 1);
01736 }
01737
01738 for (j = 1; j < PITCH_ORDER; j++) {
01739 for (k = 0; k < j; k++) {
01740 temp = dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN, 0);
01741 ccr_buf[count++] = av_clipl_int32(temp<<2);
01742 }
01743 }
01744 }
01745
01746
01747 max = 0;
01748 for (i = 0; i < 20 * iter; i++)
01749 max = FFMAX(max, FFABS(ccr_buf[i]));
01750
01751 temp = normalize_bits_int32(max);
01752
01753 for (i = 0; i < 20 * iter; i++){
01754 ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
01755 (1 << 15)) >> 16;
01756 }
01757
01758 max = 0;
01759 for (i = 0; i < iter; i++) {
01760
01761 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
01762 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
01763 cb_tbl = adaptive_cb_gain170;
01764 tbl_size = 170;
01765 }
01766
01767 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
01768 temp = 0;
01769 for (l = 0; l < 20; l++)
01770 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
01771 temp = av_clipl_int32(temp);
01772
01773 if (temp > max) {
01774 max = temp;
01775 acb_gain = j;
01776 acb_lag = i;
01777 }
01778 }
01779 }
01780
01781 if (!odd_frame) {
01782 pitch_lag += acb_lag - 1;
01783 acb_lag = 1;
01784 }
01785
01786 p->pitch_lag[index >> 1] = pitch_lag;
01787 p->subframe[index].ad_cb_lag = acb_lag;
01788 p->subframe[index].ad_cb_gain = acb_gain;
01789 }
01790
01797 static void sub_acb_contrib(int16_t *residual, int16_t *impulse_resp,
01798 int16_t *buf)
01799 {
01800 int i, j;
01801
01802 for (i = 0; i < SUBFRAME_LEN; i++) {
01803 int64_t temp = buf[i] << 14;
01804 for (j = 0; j <= i; j++)
01805 temp -= residual[j] * impulse_resp[i - j];
01806
01807 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
01808 }
01809 }
01810
01817 static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
01818 int16_t *buf, int pulse_cnt, int pitch_lag)
01819 {
01820 FCBParam param;
01821 int16_t impulse_r[SUBFRAME_LEN];
01822 int16_t temp_corr[SUBFRAME_LEN];
01823 int16_t impulse_corr[SUBFRAME_LEN];
01824
01825 int ccr1[SUBFRAME_LEN];
01826 int ccr2[SUBFRAME_LEN];
01827 int amp, err, max, max_amp_index, min, scale, i, j, k, l;
01828
01829 int64_t temp;
01830
01831
01832 memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
01833 param.dirac_train = 0;
01834 if (pitch_lag < SUBFRAME_LEN - 2) {
01835 param.dirac_train = 1;
01836 gen_dirac_train(impulse_r, pitch_lag);
01837 }
01838
01839 for (i = 0; i < SUBFRAME_LEN; i++)
01840 temp_corr[i] = impulse_r[i] >> 1;
01841
01842
01843 temp = dot_product(temp_corr, temp_corr, SUBFRAME_LEN, 1);
01844
01845 scale = normalize_bits_int32(temp);
01846 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
01847
01848 for (i = 1; i < SUBFRAME_LEN; i++) {
01849 temp = dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i, 1);
01850 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
01851 }
01852
01853
01854 scale -= 4;
01855 for (i = 0; i < SUBFRAME_LEN; i++){
01856 temp = dot_product(buf + i, impulse_r, SUBFRAME_LEN - i, 1);
01857 if (scale < 0)
01858 ccr1[i] = temp >> -scale;
01859 else
01860 ccr1[i] = av_clipl_int32(temp << scale);
01861 }
01862
01863
01864 for (i = 0; i < GRID_SIZE; i++) {
01865
01866 max = 0;
01867 for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
01868 temp = FFABS(ccr1[j]);
01869 if (temp >= max) {
01870 max = temp;
01871 param.pulse_pos[0] = j;
01872 }
01873 }
01874
01875
01876 amp = max;
01877 min = 1 << 30;
01878 max_amp_index = GAIN_LEVELS - 2;
01879 for (j = max_amp_index; j >= 2; j--) {
01880 temp = av_clipl_int32((int64_t)fixed_cb_gain[j] *
01881 impulse_corr[0] << 1);
01882 temp = FFABS(temp - amp);
01883 if (temp < min) {
01884 min = temp;
01885 max_amp_index = j;
01886 }
01887 }
01888
01889 max_amp_index--;
01890
01891 for (j = 1; j < 5; j++) {
01892 for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
01893 temp_corr[k] = 0;
01894 ccr2[k] = ccr1[k];
01895 }
01896 param.amp_index = max_amp_index + j - 2;
01897 amp = fixed_cb_gain[param.amp_index];
01898
01899 param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
01900 temp_corr[param.pulse_pos[0]] = 1;
01901
01902 for (k = 1; k < pulse_cnt; k++) {
01903 max = -1 << 30;
01904 for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
01905 if (temp_corr[l])
01906 continue;
01907 temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
01908 temp = av_clipl_int32((int64_t)temp *
01909 param.pulse_sign[k - 1] << 1);
01910 ccr2[l] -= temp;
01911 temp = FFABS(ccr2[l]);
01912 if (temp > max) {
01913 max = temp;
01914 param.pulse_pos[k] = l;
01915 }
01916 }
01917
01918 param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
01919 -amp : amp;
01920 temp_corr[param.pulse_pos[k]] = 1;
01921 }
01922
01923
01924 memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
01925
01926 for (k = 0; k < pulse_cnt; k++)
01927 temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
01928
01929 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
01930 temp = 0;
01931 for (l = 0; l <= k; l++) {
01932 int prod = av_clipl_int32((int64_t)temp_corr[l] *
01933 impulse_r[k - l] << 1);
01934 temp = av_clipl_int32(temp + prod);
01935 }
01936 temp_corr[k] = temp << 2 >> 16;
01937 }
01938
01939
01940 err = 0;
01941 for (k = 0; k < SUBFRAME_LEN; k++) {
01942 int64_t prod;
01943 prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
01944 err = av_clipl_int32(err - prod);
01945 prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
01946 err = av_clipl_int32(err + prod);
01947 }
01948
01949
01950 if (err < optim->min_err) {
01951 optim->min_err = err;
01952 optim->grid_index = i;
01953 optim->amp_index = param.amp_index;
01954 optim->dirac_train = param.dirac_train;
01955
01956 for (k = 0; k < pulse_cnt; k++) {
01957 optim->pulse_sign[k] = param.pulse_sign[k];
01958 optim->pulse_pos[k] = param.pulse_pos[k];
01959 }
01960 }
01961 }
01962 }
01963 }
01964
01971 static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
01972 int16_t *buf, int pulse_cnt)
01973 {
01974 int i, j;
01975
01976 j = PULSE_MAX - pulse_cnt;
01977
01978 subfrm->pulse_sign = 0;
01979 subfrm->pulse_pos = 0;
01980
01981 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
01982 int val = buf[optim->grid_index + (i << 1)];
01983 if (!val) {
01984 subfrm->pulse_pos += combinatorial_table[j][i];
01985 } else {
01986 subfrm->pulse_sign <<= 1;
01987 if (val < 0) subfrm->pulse_sign++;
01988 j++;
01989
01990 if (j == PULSE_MAX) break;
01991 }
01992 }
01993 subfrm->amp_index = optim->amp_index;
01994 subfrm->grid_index = optim->grid_index;
01995 subfrm->dirac_train = optim->dirac_train;
01996 }
01997
02004 static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
02005 int16_t *buf, int index)
02006 {
02007 FCBParam optim;
02008 int pulse_cnt = pulses[index];
02009 int i;
02010
02011 optim.min_err = 1 << 30;
02012 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
02013
02014 if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
02015 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
02016 p->pitch_lag[index >> 1]);
02017 }
02018
02019
02020 memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
02021 for (i = 0; i < pulse_cnt; i++)
02022 buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
02023
02024 pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
02025
02026 if (optim.dirac_train)
02027 gen_dirac_train(buf, p->pitch_lag[index >> 1]);
02028 }
02029
02036 static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size)
02037 {
02038 PutBitContext pb;
02039 int info_bits, i, temp;
02040
02041 init_put_bits(&pb, frame, size);
02042
02043 if (p->cur_rate == Rate6k3) {
02044 info_bits = 0;
02045 put_bits(&pb, 2, info_bits);
02046 }
02047
02048 put_bits(&pb, 8, p->lsp_index[2]);
02049 put_bits(&pb, 8, p->lsp_index[1]);
02050 put_bits(&pb, 8, p->lsp_index[0]);
02051
02052 put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
02053 put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
02054 put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
02055 put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
02056
02057
02058 for (i = 0; i < SUBFRAMES; i++) {
02059 temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
02060 p->subframe[i].amp_index;
02061 if (p->cur_rate == Rate6k3)
02062 temp += p->subframe[i].dirac_train << 11;
02063 put_bits(&pb, 12, temp);
02064 }
02065
02066 put_bits(&pb, 1, p->subframe[0].grid_index);
02067 put_bits(&pb, 1, p->subframe[1].grid_index);
02068 put_bits(&pb, 1, p->subframe[2].grid_index);
02069 put_bits(&pb, 1, p->subframe[3].grid_index);
02070
02071 if (p->cur_rate == Rate6k3) {
02072 skip_put_bits(&pb, 1);
02073
02074
02075 temp = (p->subframe[0].pulse_pos >> 16) * 810 +
02076 (p->subframe[1].pulse_pos >> 14) * 90 +
02077 (p->subframe[2].pulse_pos >> 16) * 9 +
02078 (p->subframe[3].pulse_pos >> 14);
02079 put_bits(&pb, 13, temp);
02080
02081 put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
02082 put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
02083 put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
02084 put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
02085
02086 put_bits(&pb, 6, p->subframe[0].pulse_sign);
02087 put_bits(&pb, 5, p->subframe[1].pulse_sign);
02088 put_bits(&pb, 6, p->subframe[2].pulse_sign);
02089 put_bits(&pb, 5, p->subframe[3].pulse_sign);
02090 }
02091
02092 flush_put_bits(&pb);
02093 return frame_size[info_bits];
02094 }
02095
02096 static int g723_1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
02097 int buf_size, void *data)
02098 {
02099 G723_1_Context *p = avctx->priv_data;
02100 int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
02101 int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
02102 int16_t cur_lsp[LPC_ORDER];
02103 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
02104 int16_t vector[FRAME_LEN + PITCH_MAX];
02105 int offset;
02106 int16_t *in = data;
02107
02108 HFParam hf[4];
02109 int i, j;
02110
02111 highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
02112
02113 memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
02114 memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
02115
02116 comp_lpc_coeff(vector, unq_lpc);
02117 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
02118 lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
02119
02120
02121 memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
02122 sizeof(int16_t) * SUBFRAME_LEN);
02123 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
02124 sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
02125 memcpy(p->prev_data, in + HALF_FRAME_LEN,
02126 sizeof(int16_t) * HALF_FRAME_LEN);
02127 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
02128
02129 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
02130
02131 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
02132 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
02133 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
02134
02135 scale_vector(vector, FRAME_LEN + PITCH_MAX);
02136
02137 p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
02138 p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
02139
02140 for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
02141 comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
02142
02143 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
02144 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
02145 memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
02146
02147 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
02148 harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
02149
02150 inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
02151 lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
02152
02153 memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
02154
02155 offset = 0;
02156 for (i = 0; i < SUBFRAMES; i++) {
02157 int16_t impulse_resp[SUBFRAME_LEN];
02158 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
02159 int16_t flt_in[SUBFRAME_LEN];
02160 int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
02161
02166 memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
02167 memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
02168 memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
02169
02170 flt_in[0] = 1 << 13;
02171 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
02172 zero, zero, flt_in, vector + PITCH_MAX, 1);
02173 harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
02174
02175
02176 flt_in[0] = 0;
02177 memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
02178 memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
02179
02180 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
02181 fir, iir, flt_in, vector + PITCH_MAX, 0);
02182 memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
02183 harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
02184
02185 acb_search(p, residual, impulse_resp, in, i);
02186 gen_acb_excitation(residual, p->prev_excitation,p->pitch_lag[i >> 1],
02187 p->subframe[i], p->cur_rate);
02188 sub_acb_contrib(residual, impulse_resp, in);
02189
02190 fcb_search(p, impulse_resp, in, i);
02191
02192
02193 gen_acb_excitation(impulse_resp, p->prev_excitation, p->pitch_lag[i >> 1],
02194 p->subframe[i], Rate6k3);
02195
02196 memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
02197 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
02198 for (j = 0; j < SUBFRAME_LEN; j++)
02199 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
02200 memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
02201 sizeof(int16_t) * SUBFRAME_LEN);
02202
02203
02204 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
02205 p->perf_fir_mem, p->perf_iir_mem,
02206 in, vector + PITCH_MAX, 0);
02207 memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
02208 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
02209 memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
02210 sizeof(int16_t) * SUBFRAME_LEN);
02211
02212 in += SUBFRAME_LEN;
02213 offset += LPC_ORDER;
02214 }
02215
02216 return pack_bitstream(p, buf, buf_size);
02217 }
02218
02219 AVCodec ff_g723_1_encoder = {
02220 .name = "g723_1",
02221 .type = AVMEDIA_TYPE_AUDIO,
02222 .id = CODEC_ID_G723_1,
02223 .priv_data_size = sizeof(G723_1_Context),
02224 .init = g723_1_encode_init,
02225 .encode = g723_1_encode_frame,
02226 .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
02227 .sample_fmts = (const enum SampleFormat[]){AV_SAMPLE_FMT_S16,
02228 AV_SAMPLE_FMT_NONE},
02229 };
02230 #endif