00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <math.h>
00025 #include <stdint.h>
00026
00027 #include "libavutil/mathematics.h"
00028 #include "avcodec.h"
00029 #define ALT_BITSTREAM_READER_LE
00030 #include "get_bits.h"
00031 #include "dsputil.h"
00032
00033 #include "lsp.h"
00034 #include "celp_math.h"
00035 #include "acelp_vectors.h"
00036 #include "acelp_pitch_delay.h"
00037 #include "acelp_filters.h"
00038 #include "celp_filters.h"
00039
00040 #define MAX_SUBFRAME_COUNT 5
00041
00042 #include "sipr.h"
00043 #include "siprdata.h"
00044
00045 typedef struct {
00046 const char *mode_name;
00047 uint16_t bits_per_frame;
00048 uint8_t subframe_count;
00049 uint8_t frames_per_packet;
00050 float pitch_sharp_factor;
00051
00052
00053 uint8_t number_of_fc_indexes;
00054 uint8_t ma_predictor_bits;
00055
00057 uint8_t vq_indexes_bits[5];
00058
00060 uint8_t pitch_delay_bits[5];
00061
00062 uint8_t gp_index_bits;
00063 uint8_t fc_index_bits[10];
00064 uint8_t gc_index_bits;
00065 } SiprModeParam;
00066
00067 static const SiprModeParam modes[MODE_COUNT] = {
00068 [MODE_16k] = {
00069 .mode_name = "16k",
00070 .bits_per_frame = 160,
00071 .subframe_count = SUBFRAME_COUNT_16k,
00072 .frames_per_packet = 1,
00073 .pitch_sharp_factor = 0.00,
00074
00075 .number_of_fc_indexes = 10,
00076 .ma_predictor_bits = 1,
00077 .vq_indexes_bits = {7, 8, 7, 7, 7},
00078 .pitch_delay_bits = {9, 6},
00079 .gp_index_bits = 4,
00080 .fc_index_bits = {4, 5, 4, 5, 4, 5, 4, 5, 4, 5},
00081 .gc_index_bits = 5
00082 },
00083
00084 [MODE_8k5] = {
00085 .mode_name = "8k5",
00086 .bits_per_frame = 152,
00087 .subframe_count = 3,
00088 .frames_per_packet = 1,
00089 .pitch_sharp_factor = 0.8,
00090
00091 .number_of_fc_indexes = 3,
00092 .ma_predictor_bits = 0,
00093 .vq_indexes_bits = {6, 7, 7, 7, 5},
00094 .pitch_delay_bits = {8, 5, 5},
00095 .gp_index_bits = 0,
00096 .fc_index_bits = {9, 9, 9},
00097 .gc_index_bits = 7
00098 },
00099
00100 [MODE_6k5] = {
00101 .mode_name = "6k5",
00102 .bits_per_frame = 232,
00103 .subframe_count = 3,
00104 .frames_per_packet = 2,
00105 .pitch_sharp_factor = 0.8,
00106
00107 .number_of_fc_indexes = 3,
00108 .ma_predictor_bits = 0,
00109 .vq_indexes_bits = {6, 7, 7, 7, 5},
00110 .pitch_delay_bits = {8, 5, 5},
00111 .gp_index_bits = 0,
00112 .fc_index_bits = {5, 5, 5},
00113 .gc_index_bits = 7
00114 },
00115
00116 [MODE_5k0] = {
00117 .mode_name = "5k0",
00118 .bits_per_frame = 296,
00119 .subframe_count = 5,
00120 .frames_per_packet = 2,
00121 .pitch_sharp_factor = 0.85,
00122
00123 .number_of_fc_indexes = 1,
00124 .ma_predictor_bits = 0,
00125 .vq_indexes_bits = {6, 7, 7, 7, 5},
00126 .pitch_delay_bits = {8, 5, 8, 5, 5},
00127 .gp_index_bits = 0,
00128 .fc_index_bits = {10},
00129 .gc_index_bits = 7
00130 }
00131 };
00132
00133 const float ff_pow_0_5[] = {
00134 1.0/(1 << 1), 1.0/(1 << 2), 1.0/(1 << 3), 1.0/(1 << 4),
00135 1.0/(1 << 5), 1.0/(1 << 6), 1.0/(1 << 7), 1.0/(1 << 8),
00136 1.0/(1 << 9), 1.0/(1 << 10), 1.0/(1 << 11), 1.0/(1 << 12),
00137 1.0/(1 << 13), 1.0/(1 << 14), 1.0/(1 << 15), 1.0/(1 << 16)
00138 };
00139
00140 static void dequant(float *out, const int *idx, const float *cbs[])
00141 {
00142 int i;
00143 int stride = 2;
00144 int num_vec = 5;
00145
00146 for (i = 0; i < num_vec; i++)
00147 memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float));
00148
00149 }
00150
00151 static void lsf_decode_fp(float *lsfnew, float *lsf_history,
00152 const SiprParameters *parm)
00153 {
00154 int i;
00155 float lsf_tmp[LP_FILTER_ORDER];
00156
00157 dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks);
00158
00159 for (i = 0; i < LP_FILTER_ORDER; i++)
00160 lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i];
00161
00162 ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1);
00163
00164
00165
00166 ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1);
00167 lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI);
00168
00169 memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history));
00170
00171 for (i = 0; i < LP_FILTER_ORDER - 1; i++)
00172 lsfnew[i] = cos(lsfnew[i]);
00173 lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI;
00174 }
00175
00177 static void pitch_sharpening(int pitch_lag_int, float beta,
00178 float *fixed_vector)
00179 {
00180 int i;
00181
00182 for (i = pitch_lag_int; i < SUBFR_SIZE; i++)
00183 fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int];
00184 }
00185
00191 static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
00192 const SiprModeParam *p)
00193 {
00194 int i, j;
00195
00196 parms->ma_pred_switch = get_bits(pgb, p->ma_predictor_bits);
00197
00198 for (i = 0; i < 5; i++)
00199 parms->vq_indexes[i] = get_bits(pgb, p->vq_indexes_bits[i]);
00200
00201 for (i = 0; i < p->subframe_count; i++) {
00202 parms->pitch_delay[i] = get_bits(pgb, p->pitch_delay_bits[i]);
00203 parms->gp_index[i] = get_bits(pgb, p->gp_index_bits);
00204
00205 for (j = 0; j < p->number_of_fc_indexes; j++)
00206 parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]);
00207
00208 parms->gc_index[i] = get_bits(pgb, p->gc_index_bits);
00209 }
00210 }
00211
00212 static void lsp2lpc_sipr(const double *lsp, float *Az)
00213 {
00214 int lp_half_order = LP_FILTER_ORDER >> 1;
00215 double buf[(LP_FILTER_ORDER >> 1) + 1];
00216 double pa[(LP_FILTER_ORDER >> 1) + 1];
00217 double *qa = buf + 1;
00218 int i,j;
00219
00220 qa[-1] = 0.0;
00221
00222 ff_lsp2polyf(lsp , pa, lp_half_order );
00223 ff_lsp2polyf(lsp + 1, qa, lp_half_order - 1);
00224
00225 for (i = 1, j = LP_FILTER_ORDER - 1; i < lp_half_order; i++, j--) {
00226 double paf = pa[i] * (1 + lsp[LP_FILTER_ORDER - 1]);
00227 double qaf = (qa[i] - qa[i-2]) * (1 - lsp[LP_FILTER_ORDER - 1]);
00228 Az[i-1] = (paf + qaf) * 0.5;
00229 Az[j-1] = (paf - qaf) * 0.5;
00230 }
00231
00232 Az[lp_half_order - 1] = (1.0 + lsp[LP_FILTER_ORDER - 1]) *
00233 pa[lp_half_order] * 0.5;
00234
00235 Az[LP_FILTER_ORDER - 1] = lsp[LP_FILTER_ORDER - 1];
00236 }
00237
00238 static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az,
00239 int num_subfr)
00240 {
00241 double lsfint[LP_FILTER_ORDER];
00242 int i,j;
00243 float t, t0 = 1.0 / num_subfr;
00244
00245 t = t0 * 0.5;
00246 for (i = 0; i < num_subfr; i++) {
00247 for (j = 0; j < LP_FILTER_ORDER; j++)
00248 lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
00249
00250 lsp2lpc_sipr(lsfint, Az);
00251 Az += LP_FILTER_ORDER;
00252 t += t0;
00253 }
00254 }
00255
00259 static void eval_ir(const float *Az, int pitch_lag, float *freq,
00260 float pitch_sharp_factor)
00261 {
00262 float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1];
00263 int i;
00264
00265 tmp1[0] = 1.;
00266 for (i = 0; i < LP_FILTER_ORDER; i++) {
00267 tmp1[i+1] = Az[i] * ff_pow_0_55[i];
00268 tmp2[i ] = Az[i] * ff_pow_0_7 [i];
00269 }
00270 memset(tmp1 + 11, 0, 37 * sizeof(float));
00271
00272 ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE,
00273 LP_FILTER_ORDER);
00274
00275 pitch_sharpening(pitch_lag, pitch_sharp_factor, freq);
00276 }
00277
00281 static void convolute_with_sparse(float *out, const AMRFixed *pulses,
00282 const float *shape, int length)
00283 {
00284 int i, j;
00285
00286 memset(out, 0, length*sizeof(float));
00287 for (i = 0; i < pulses->n; i++)
00288 for (j = pulses->x[i]; j < length; j++)
00289 out[j] += pulses->y[i] * shape[j - pulses->x[i]];
00290 }
00291
00295 static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples)
00296 {
00297 float buf[SUBFR_SIZE + LP_FILTER_ORDER];
00298 float *pole_out = buf + LP_FILTER_ORDER;
00299 float lpc_n[LP_FILTER_ORDER];
00300 float lpc_d[LP_FILTER_ORDER];
00301 int i;
00302
00303 for (i = 0; i < LP_FILTER_ORDER; i++) {
00304 lpc_d[i] = lpc[i] * ff_pow_0_75[i];
00305 lpc_n[i] = lpc[i] * ff_pow_0_5 [i];
00306 };
00307
00308 memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem,
00309 LP_FILTER_ORDER*sizeof(float));
00310
00311 ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE,
00312 LP_FILTER_ORDER);
00313
00314 memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
00315 LP_FILTER_ORDER*sizeof(float));
00316
00317 ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE);
00318
00319 memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0,
00320 LP_FILTER_ORDER*sizeof(*pole_out));
00321
00322 memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
00323 LP_FILTER_ORDER*sizeof(*pole_out));
00324
00325 ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE,
00326 LP_FILTER_ORDER);
00327
00328 }
00329
00330 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses,
00331 SiprMode mode, int low_gain)
00332 {
00333 int i;
00334
00335 switch (mode) {
00336 case MODE_6k5:
00337 for (i = 0; i < 3; i++) {
00338 fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i;
00339 fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1;
00340 }
00341 fixed_sparse->n = 3;
00342 break;
00343 case MODE_8k5:
00344 for (i = 0; i < 3; i++) {
00345 fixed_sparse->x[2*i ] = 3 * ((pulses[i] >> 4) & 0xf) + i;
00346 fixed_sparse->x[2*i + 1] = 3 * ( pulses[i] & 0xf) + i;
00347
00348 fixed_sparse->y[2*i ] = (pulses[i] & 0x100) ? -1.0: 1.0;
00349
00350 fixed_sparse->y[2*i + 1] =
00351 (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ?
00352 -fixed_sparse->y[2*i ] : fixed_sparse->y[2*i];
00353 }
00354
00355 fixed_sparse->n = 6;
00356 break;
00357 case MODE_5k0:
00358 default:
00359 if (low_gain) {
00360 int offset = (pulses[0] & 0x200) ? 2 : 0;
00361 int val = pulses[0];
00362
00363 for (i = 0; i < 3; i++) {
00364 int index = (val & 0x7) * 6 + 4 - i*2;
00365
00366 fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1;
00367 fixed_sparse->x[i] = index;
00368
00369 val >>= 3;
00370 }
00371 fixed_sparse->n = 3;
00372 } else {
00373 int pulse_subset = (pulses[0] >> 8) & 1;
00374
00375 fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset;
00376 fixed_sparse->x[1] = ( pulses[0] & 15) * 3 + pulse_subset + 1;
00377
00378 fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1;
00379 fixed_sparse->y[1] = -fixed_sparse->y[0];
00380 fixed_sparse->n = 2;
00381 }
00382 break;
00383 }
00384 }
00385
00386 static void decode_frame(SiprContext *ctx, SiprParameters *params,
00387 float *out_data)
00388 {
00389 int i, j;
00390 int subframe_count = modes[ctx->mode].subframe_count;
00391 int frame_size = subframe_count * SUBFR_SIZE;
00392 float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT];
00393 float *excitation;
00394 float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER];
00395 float lsf_new[LP_FILTER_ORDER];
00396 float *impulse_response = ir_buf + LP_FILTER_ORDER;
00397 float *synth = ctx->synth_buf + 16;
00398
00399 int t0_first = 0;
00400 AMRFixed fixed_cb;
00401
00402 memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
00403 lsf_decode_fp(lsf_new, ctx->lsf_history, params);
00404
00405 sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count);
00406
00407 memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
00408
00409 excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
00410
00411 for (i = 0; i < subframe_count; i++) {
00412 float *pAz = Az + i*LP_FILTER_ORDER;
00413 float fixed_vector[SUBFR_SIZE];
00414 int T0,T0_frac;
00415 float pitch_gain, gain_code, avg_energy;
00416
00417 ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i,
00418 ctx->mode == MODE_5k0, 6);
00419
00420 if (i == 0 || (i == 2 && ctx->mode == MODE_5k0))
00421 t0_first = T0;
00422
00423 ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0),
00424 ff_b60_sinc, 6,
00425 2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER,
00426 SUBFR_SIZE);
00427
00428 decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode,
00429 ctx->past_pitch_gain < 0.8);
00430
00431 eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor);
00432
00433 convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
00434 SUBFR_SIZE);
00435
00436 avg_energy =
00437 (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/
00438 SUBFR_SIZE;
00439
00440 ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0];
00441
00442 gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1],
00443 avg_energy, ctx->energy_history,
00444 34 - 15.0/(0.05*M_LN10/M_LN2),
00445 pred);
00446
00447 ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
00448 pitch_gain, gain_code, SUBFR_SIZE);
00449
00450 pitch_gain *= 0.5 * pitch_gain;
00451 pitch_gain = FFMIN(pitch_gain, 0.4);
00452
00453 ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain;
00454 ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain);
00455 gain_code *= ctx->gain_mem;
00456
00457 for (j = 0; j < SUBFR_SIZE; j++)
00458 fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
00459
00460 if (ctx->mode == MODE_5k0) {
00461 postfilter_5k0(ctx, pAz, fixed_vector);
00462
00463 ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
00464 pAz, excitation, SUBFR_SIZE,
00465 LP_FILTER_ORDER);
00466 }
00467
00468 ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector,
00469 SUBFR_SIZE, LP_FILTER_ORDER);
00470
00471 excitation += SUBFR_SIZE;
00472 }
00473
00474 memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
00475 LP_FILTER_ORDER * sizeof(float));
00476
00477 if (ctx->mode == MODE_5k0) {
00478 for (i = 0; i < subframe_count; i++) {
00479 float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
00480 ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
00481 SUBFR_SIZE);
00482 ff_adaptive_gain_control(&synth[i * SUBFR_SIZE],
00483 &synth[i * SUBFR_SIZE], energy,
00484 SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
00485 }
00486
00487 memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size,
00488 LP_FILTER_ORDER*sizeof(float));
00489 }
00490 memcpy(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL,
00491 (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float));
00492
00493 ff_acelp_apply_order_2_transfer_function(out_data, synth,
00494 (const float[2]) {-1.99997 , 1.000000000},
00495 (const float[2]) {-1.93307352, 0.935891986},
00496 0.939805806,
00497 ctx->highpass_filt_mem,
00498 frame_size);
00499 }
00500
00501 static av_cold int sipr_decoder_init(AVCodecContext * avctx)
00502 {
00503 SiprContext *ctx = avctx->priv_data;
00504 int i;
00505
00506 if (avctx->bit_rate > 12200) ctx->mode = MODE_16k;
00507 else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5;
00508 else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5;
00509 else ctx->mode = MODE_5k0;
00510
00511 av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name);
00512
00513 if (ctx->mode == MODE_16k)
00514 ff_sipr_init_16k(ctx);
00515
00516 for (i = 0; i < LP_FILTER_ORDER; i++)
00517 ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1));
00518
00519 for (i = 0; i < 4; i++)
00520 ctx->energy_history[i] = -14;
00521
00522 avctx->sample_fmt = SAMPLE_FMT_FLT;
00523
00524 dsputil_init(&ctx->dsp, avctx);
00525
00526 return 0;
00527 }
00528
00529 static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
00530 int *data_size, AVPacket *avpkt)
00531 {
00532 SiprContext *ctx = avctx->priv_data;
00533 const uint8_t *buf=avpkt->data;
00534 SiprParameters parm;
00535 const SiprModeParam *mode_par = &modes[ctx->mode];
00536 GetBitContext gb;
00537 float *data = datap;
00538 int subframe_size = ctx->mode == MODE_16k ? L_SUBFR_16k : SUBFR_SIZE;
00539 int i;
00540
00541 ctx->avctx = avctx;
00542 if (avpkt->size < (mode_par->bits_per_frame >> 3)) {
00543 av_log(avctx, AV_LOG_ERROR,
00544 "Error processing packet: packet size (%d) too small\n",
00545 avpkt->size);
00546
00547 *data_size = 0;
00548 return -1;
00549 }
00550 if (*data_size < subframe_size * mode_par->subframe_count * sizeof(float)) {
00551 av_log(avctx, AV_LOG_ERROR,
00552 "Error processing packet: output buffer (%d) too small\n",
00553 *data_size);
00554
00555 *data_size = 0;
00556 return -1;
00557 }
00558
00559 init_get_bits(&gb, buf, mode_par->bits_per_frame);
00560
00561 for (i = 0; i < mode_par->frames_per_packet; i++) {
00562 decode_parameters(&parm, &gb, mode_par);
00563
00564 if (ctx->mode == MODE_16k)
00565 ff_sipr_decode_frame_16k(ctx, &parm, data);
00566 else
00567 decode_frame(ctx, &parm, data);
00568
00569 data += subframe_size * mode_par->subframe_count;
00570 }
00571
00572 *data_size = mode_par->frames_per_packet * subframe_size *
00573 mode_par->subframe_count * sizeof(float);
00574
00575 return mode_par->bits_per_frame >> 3;
00576 };
00577
00578 AVCodec sipr_decoder = {
00579 "sipr",
00580 AVMEDIA_TYPE_AUDIO,
00581 CODEC_ID_SIPR,
00582 sizeof(SiprContext),
00583 sipr_decoder_init,
00584 NULL,
00585 NULL,
00586 sipr_decode_frame,
00587 .long_name = NULL_IF_CONFIG_SMALL("RealAudio SIPR / ACELP.NET"),
00588 };