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