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