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