00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00043 #include <string.h>
00044 #include <math.h>
00045
00046 #include "avcodec.h"
00047 #include "dsputil.h"
00048 #include "libavutil/common.h"
00049 #include "libavutil/avassert.h"
00050 #include "celp_math.h"
00051 #include "celp_filters.h"
00052 #include "acelp_filters.h"
00053 #include "acelp_vectors.h"
00054 #include "acelp_pitch_delay.h"
00055 #include "lsp.h"
00056 #include "amr.h"
00057
00058 #include "amrnbdata.h"
00059
00060 #define AMR_BLOCK_SIZE 160
00061 #define AMR_SAMPLE_BOUND 32768.0
00062
00063
00072 #define AMR_SAMPLE_SCALE (2.0 / 32768.0)
00073
00075 #define PRED_FAC_MODE_12k2 0.65
00076
00077 #define LSF_R_FAC (8000.0 / 32768.0)
00078 #define MIN_LSF_SPACING (50.0488 / 8000.0)
00079 #define PITCH_LAG_MIN_MODE_12k2 18
00080
00081
00082 #define MIN_ENERGY -14.0
00083
00089 #define SHARP_MAX 0.79449462890625
00090
00092 #define AMR_TILT_RESPONSE 22
00093
00094 #define AMR_TILT_GAMMA_T 0.8
00095
00096 #define AMR_AGC_ALPHA 0.9
00097
00098 typedef struct AMRContext {
00099 AVFrame avframe;
00100 AMRNBFrame frame;
00101 uint8_t bad_frame_indicator;
00102 enum Mode cur_frame_mode;
00103
00104 int16_t prev_lsf_r[LP_FILTER_ORDER];
00105 double lsp[4][LP_FILTER_ORDER];
00106 double prev_lsp_sub4[LP_FILTER_ORDER];
00107
00108 float lsf_q[4][LP_FILTER_ORDER];
00109 float lsf_avg[LP_FILTER_ORDER];
00110
00111 float lpc[4][LP_FILTER_ORDER];
00112
00113 uint8_t pitch_lag_int;
00114
00115 float excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1 + AMR_SUBFRAME_SIZE];
00116 float *excitation;
00117
00118 float pitch_vector[AMR_SUBFRAME_SIZE];
00119 float fixed_vector[AMR_SUBFRAME_SIZE];
00120
00121 float prediction_error[4];
00122 float pitch_gain[5];
00123 float fixed_gain[5];
00124
00125 float beta;
00126 uint8_t diff_count;
00127 uint8_t hang_count;
00128
00129 float prev_sparse_fixed_gain;
00130 uint8_t prev_ir_filter_nr;
00131 uint8_t ir_filter_onset;
00132
00133 float postfilter_mem[10];
00134 float tilt_mem;
00135 float postfilter_agc;
00136 float high_pass_mem[2];
00137
00138 float samples_in[LP_FILTER_ORDER + AMR_SUBFRAME_SIZE];
00139
00140 ACELPFContext acelpf_ctx;
00141 ACELPVContext acelpv_ctx;
00142 CELPFContext celpf_ctx;
00143 CELPMContext celpm_ctx;
00144
00145 } AMRContext;
00146
00148 static void weighted_vector_sumd(double *out, const double *in_a,
00149 const double *in_b, double weight_coeff_a,
00150 double weight_coeff_b, int length)
00151 {
00152 int i;
00153
00154 for (i = 0; i < length; i++)
00155 out[i] = weight_coeff_a * in_a[i]
00156 + weight_coeff_b * in_b[i];
00157 }
00158
00159 static av_cold int amrnb_decode_init(AVCodecContext *avctx)
00160 {
00161 AMRContext *p = avctx->priv_data;
00162 int i;
00163
00164 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00165
00166
00167 p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1];
00168
00169 for (i = 0; i < LP_FILTER_ORDER; i++) {
00170 p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15);
00171 p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15);
00172 }
00173
00174 for (i = 0; i < 4; i++)
00175 p->prediction_error[i] = MIN_ENERGY;
00176
00177 avcodec_get_frame_defaults(&p->avframe);
00178 avctx->coded_frame = &p->avframe;
00179
00180 ff_acelp_filter_init(&p->acelpf_ctx);
00181 ff_acelp_vectors_init(&p->acelpv_ctx);
00182 ff_celp_filter_init(&p->celpf_ctx);
00183 ff_celp_math_init(&p->celpm_ctx);
00184
00185 return 0;
00186 }
00187
00188
00200 static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf,
00201 int buf_size)
00202 {
00203 enum Mode mode;
00204
00205
00206 mode = buf[0] >> 3 & 0x0F;
00207 p->bad_frame_indicator = (buf[0] & 0x4) != 0x4;
00208
00209 if (mode >= N_MODES || buf_size < frame_sizes_nb[mode] + 1) {
00210 return NO_DATA;
00211 }
00212
00213 if (mode < MODE_DTX)
00214 ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1,
00215 amr_unpacking_bitmaps_per_mode[mode]);
00216
00217 return mode;
00218 }
00219
00220
00223
00232 static void interpolate_lsf(ACELPVContext *ctx, float lsf_q[4][LP_FILTER_ORDER], float *lsf_new)
00233 {
00234 int i;
00235
00236 for (i = 0; i < 4; i++)
00237 ctx->weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new,
00238 0.25 * (3 - i), 0.25 * (i + 1),
00239 LP_FILTER_ORDER);
00240 }
00241
00253 static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER],
00254 const float lsf_no_r[LP_FILTER_ORDER],
00255 const int16_t *lsf_quantizer[5],
00256 const int quantizer_offset,
00257 const int sign, const int update)
00258 {
00259 int16_t lsf_r[LP_FILTER_ORDER];
00260 float lsf_q[LP_FILTER_ORDER];
00261 int i;
00262
00263 for (i = 0; i < LP_FILTER_ORDER >> 1; i++)
00264 memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset],
00265 2 * sizeof(*lsf_r));
00266
00267 if (sign) {
00268 lsf_r[4] *= -1;
00269 lsf_r[5] *= -1;
00270 }
00271
00272 if (update)
00273 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
00274
00275 for (i = 0; i < LP_FILTER_ORDER; i++)
00276 lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0);
00277
00278 ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
00279
00280 if (update)
00281 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
00282
00283 ff_acelp_lsf2lspd(lsp, lsf_q, LP_FILTER_ORDER);
00284 }
00285
00291 static void lsf2lsp_5(AMRContext *p)
00292 {
00293 const uint16_t *lsf_param = p->frame.lsf;
00294 float lsf_no_r[LP_FILTER_ORDER];
00295 const int16_t *lsf_quantizer[5];
00296 int i;
00297
00298 lsf_quantizer[0] = lsf_5_1[lsf_param[0]];
00299 lsf_quantizer[1] = lsf_5_2[lsf_param[1]];
00300 lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1];
00301 lsf_quantizer[3] = lsf_5_4[lsf_param[3]];
00302 lsf_quantizer[4] = lsf_5_5[lsf_param[4]];
00303
00304 for (i = 0; i < LP_FILTER_ORDER; i++)
00305 lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i];
00306
00307 lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0);
00308 lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1);
00309
00310
00311 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER);
00312 weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER);
00313 }
00314
00320 static void lsf2lsp_3(AMRContext *p)
00321 {
00322 const uint16_t *lsf_param = p->frame.lsf;
00323 int16_t lsf_r[LP_FILTER_ORDER];
00324 float lsf_q[LP_FILTER_ORDER];
00325 const int16_t *lsf_quantizer;
00326 int i, j;
00327
00328 lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]];
00329 memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r));
00330
00331 lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)];
00332 memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r));
00333
00334 lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]];
00335 memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r));
00336
00337
00338 for (i = 0; i < LP_FILTER_ORDER; i++)
00339 lsf_q[i] = (lsf_r[i] + p->prev_lsf_r[i] * pred_fac[i]) * (LSF_R_FAC / 8000.0) + lsf_3_mean[i] * (1.0 / 8000.0);
00340
00341 ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
00342
00343
00344 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
00345 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
00346
00347 ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
00348
00349
00350 for (i = 1; i <= 3; i++)
00351 for(j = 0; j < LP_FILTER_ORDER; j++)
00352 p->lsp[i-1][j] = p->prev_lsp_sub4[j] +
00353 (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;
00354 }
00355
00357
00358
00361
00365 static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index,
00366 const int prev_lag_int, const int subframe)
00367 {
00368 if (subframe == 0 || subframe == 2) {
00369 if (pitch_index < 463) {
00370 *lag_int = (pitch_index + 107) * 10923 >> 16;
00371 *lag_frac = pitch_index - *lag_int * 6 + 105;
00372 } else {
00373 *lag_int = pitch_index - 368;
00374 *lag_frac = 0;
00375 }
00376 } else {
00377 *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1;
00378 *lag_frac = pitch_index - *lag_int * 6 - 3;
00379 *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2,
00380 PITCH_DELAY_MAX - 9);
00381 }
00382 }
00383
00384 static void decode_pitch_vector(AMRContext *p,
00385 const AMRNBSubframe *amr_subframe,
00386 const int subframe)
00387 {
00388 int pitch_lag_int, pitch_lag_frac;
00389 enum Mode mode = p->cur_frame_mode;
00390
00391 if (p->cur_frame_mode == MODE_12k2) {
00392 decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac,
00393 amr_subframe->p_lag, p->pitch_lag_int,
00394 subframe);
00395 } else
00396 ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac,
00397 amr_subframe->p_lag,
00398 p->pitch_lag_int, subframe,
00399 mode != MODE_4k75 && mode != MODE_5k15,
00400 mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6));
00401
00402 p->pitch_lag_int = pitch_lag_int;
00403
00404 pitch_lag_frac <<= (p->cur_frame_mode != MODE_12k2);
00405
00406 pitch_lag_int += pitch_lag_frac > 0;
00407
00408
00409
00410 p->acelpf_ctx.acelp_interpolatef(p->excitation,
00411 p->excitation + 1 - pitch_lag_int,
00412 ff_b60_sinc, 6,
00413 pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0),
00414 10, AMR_SUBFRAME_SIZE);
00415
00416 memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float));
00417 }
00418
00420
00421
00424
00428 static void decode_10bit_pulse(int code, int pulse_position[8],
00429 int i1, int i2, int i3)
00430 {
00431
00432
00433 const uint8_t *positions = base_five_table[code >> 3];
00434 pulse_position[i1] = (positions[2] << 1) + ( code & 1);
00435 pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1);
00436 pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1);
00437 }
00438
00446 static void decode_8_pulses_31bits(const int16_t *fixed_index,
00447 AMRFixed *fixed_sparse)
00448 {
00449 int pulse_position[8];
00450 int i, temp;
00451
00452 decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1);
00453 decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5);
00454
00455
00456
00457 temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5;
00458 pulse_position[3] = temp % 5;
00459 pulse_position[7] = temp / 5;
00460 if (pulse_position[7] & 1)
00461 pulse_position[3] = 4 - pulse_position[3];
00462 pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1);
00463 pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1);
00464
00465 fixed_sparse->n = 8;
00466 for (i = 0; i < 4; i++) {
00467 const int pos1 = (pulse_position[i] << 2) + i;
00468 const int pos2 = (pulse_position[i + 4] << 2) + i;
00469 const float sign = fixed_index[i] ? -1.0 : 1.0;
00470 fixed_sparse->x[i ] = pos1;
00471 fixed_sparse->x[i + 4] = pos2;
00472 fixed_sparse->y[i ] = sign;
00473 fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign;
00474 }
00475 }
00476
00492 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const uint16_t *pulses,
00493 const enum Mode mode, const int subframe)
00494 {
00495 av_assert1(MODE_4k75 <= (signed)mode && mode <= MODE_12k2);
00496
00497 if (mode == MODE_12k2) {
00498 ff_decode_10_pulses_35bits(pulses, fixed_sparse, gray_decode, 5, 3);
00499 } else if (mode == MODE_10k2) {
00500 decode_8_pulses_31bits(pulses, fixed_sparse);
00501 } else {
00502 int *pulse_position = fixed_sparse->x;
00503 int i, pulse_subset;
00504 const int fixed_index = pulses[0];
00505
00506 if (mode <= MODE_5k15) {
00507 pulse_subset = ((fixed_index >> 3) & 8) + (subframe << 1);
00508 pulse_position[0] = ( fixed_index & 7) * 5 + track_position[pulse_subset];
00509 pulse_position[1] = ((fixed_index >> 3) & 7) * 5 + track_position[pulse_subset + 1];
00510 fixed_sparse->n = 2;
00511 } else if (mode == MODE_5k9) {
00512 pulse_subset = ((fixed_index & 1) << 1) + 1;
00513 pulse_position[0] = ((fixed_index >> 1) & 7) * 5 + pulse_subset;
00514 pulse_subset = (fixed_index >> 4) & 3;
00515 pulse_position[1] = ((fixed_index >> 6) & 7) * 5 + pulse_subset + (pulse_subset == 3 ? 1 : 0);
00516 fixed_sparse->n = pulse_position[0] == pulse_position[1] ? 1 : 2;
00517 } else if (mode == MODE_6k7) {
00518 pulse_position[0] = (fixed_index & 7) * 5;
00519 pulse_subset = (fixed_index >> 2) & 2;
00520 pulse_position[1] = ((fixed_index >> 4) & 7) * 5 + pulse_subset + 1;
00521 pulse_subset = (fixed_index >> 6) & 2;
00522 pulse_position[2] = ((fixed_index >> 8) & 7) * 5 + pulse_subset + 2;
00523 fixed_sparse->n = 3;
00524 } else {
00525 pulse_position[0] = gray_decode[ fixed_index & 7];
00526 pulse_position[1] = gray_decode[(fixed_index >> 3) & 7] + 1;
00527 pulse_position[2] = gray_decode[(fixed_index >> 6) & 7] + 2;
00528 pulse_subset = (fixed_index >> 9) & 1;
00529 pulse_position[3] = gray_decode[(fixed_index >> 10) & 7] + pulse_subset + 3;
00530 fixed_sparse->n = 4;
00531 }
00532 for (i = 0; i < fixed_sparse->n; i++)
00533 fixed_sparse->y[i] = (pulses[1] >> i) & 1 ? 1.0 : -1.0;
00534 }
00535 }
00536
00545 static void pitch_sharpening(AMRContext *p, int subframe, enum Mode mode,
00546 AMRFixed *fixed_sparse)
00547 {
00548
00549
00550
00551 if (mode == MODE_12k2)
00552 p->beta = FFMIN(p->pitch_gain[4], 1.0);
00553
00554 fixed_sparse->pitch_lag = p->pitch_lag_int;
00555 fixed_sparse->pitch_fac = p->beta;
00556
00557
00558
00559
00560 if (mode != MODE_4k75 || subframe & 1)
00561 p->beta = av_clipf(p->pitch_gain[4], 0.0, SHARP_MAX);
00562 }
00564
00565
00568
00581 static float fixed_gain_smooth(AMRContext *p , const float *lsf,
00582 const float *lsf_avg, const enum Mode mode)
00583 {
00584 float diff = 0.0;
00585 int i;
00586
00587 for (i = 0; i < LP_FILTER_ORDER; i++)
00588 diff += fabs(lsf_avg[i] - lsf[i]) / lsf_avg[i];
00589
00590
00591
00592 p->diff_count++;
00593 if (diff <= 0.65)
00594 p->diff_count = 0;
00595
00596 if (p->diff_count > 10) {
00597 p->hang_count = 0;
00598 p->diff_count--;
00599 }
00600
00601 if (p->hang_count < 40) {
00602 p->hang_count++;
00603 } else if (mode < MODE_7k4 || mode == MODE_10k2) {
00604 const float smoothing_factor = av_clipf(4.0 * diff - 1.6, 0.0, 1.0);
00605 const float fixed_gain_mean = (p->fixed_gain[0] + p->fixed_gain[1] +
00606 p->fixed_gain[2] + p->fixed_gain[3] +
00607 p->fixed_gain[4]) * 0.2;
00608 return smoothing_factor * p->fixed_gain[4] +
00609 (1.0 - smoothing_factor) * fixed_gain_mean;
00610 }
00611 return p->fixed_gain[4];
00612 }
00613
00623 static void decode_gains(AMRContext *p, const AMRNBSubframe *amr_subframe,
00624 const enum Mode mode, const int subframe,
00625 float *fixed_gain_factor)
00626 {
00627 if (mode == MODE_12k2 || mode == MODE_7k95) {
00628 p->pitch_gain[4] = qua_gain_pit [amr_subframe->p_gain ]
00629 * (1.0 / 16384.0);
00630 *fixed_gain_factor = qua_gain_code[amr_subframe->fixed_gain]
00631 * (1.0 / 2048.0);
00632 } else {
00633 const uint16_t *gains;
00634
00635 if (mode >= MODE_6k7) {
00636 gains = gains_high[amr_subframe->p_gain];
00637 } else if (mode >= MODE_5k15) {
00638 gains = gains_low [amr_subframe->p_gain];
00639 } else {
00640
00641 gains = gains_MODE_4k75[(p->frame.subframe[subframe & 2].p_gain << 1) + (subframe & 1)];
00642 }
00643
00644 p->pitch_gain[4] = gains[0] * (1.0 / 16384.0);
00645 *fixed_gain_factor = gains[1] * (1.0 / 4096.0);
00646 }
00647 }
00648
00650
00651
00654
00665 static void apply_ir_filter(float *out, const AMRFixed *in,
00666 const float *filter)
00667 {
00668 float filter1[AMR_SUBFRAME_SIZE],
00669 filter2[AMR_SUBFRAME_SIZE];
00670 int lag = in->pitch_lag;
00671 float fac = in->pitch_fac;
00672 int i;
00673
00674 if (lag < AMR_SUBFRAME_SIZE) {
00675 ff_celp_circ_addf(filter1, filter, filter, lag, fac,
00676 AMR_SUBFRAME_SIZE);
00677
00678 if (lag < AMR_SUBFRAME_SIZE >> 1)
00679 ff_celp_circ_addf(filter2, filter, filter1, lag, fac,
00680 AMR_SUBFRAME_SIZE);
00681 }
00682
00683 memset(out, 0, sizeof(float) * AMR_SUBFRAME_SIZE);
00684 for (i = 0; i < in->n; i++) {
00685 int x = in->x[i];
00686 float y = in->y[i];
00687 const float *filterp;
00688
00689 if (x >= AMR_SUBFRAME_SIZE - lag) {
00690 filterp = filter;
00691 } else if (x >= AMR_SUBFRAME_SIZE - (lag << 1)) {
00692 filterp = filter1;
00693 } else
00694 filterp = filter2;
00695
00696 ff_celp_circ_addf(out, out, filterp, x, y, AMR_SUBFRAME_SIZE);
00697 }
00698 }
00699
00712 static const float *anti_sparseness(AMRContext *p, AMRFixed *fixed_sparse,
00713 const float *fixed_vector,
00714 float fixed_gain, float *out)
00715 {
00716 int ir_filter_nr;
00717
00718 if (p->pitch_gain[4] < 0.6) {
00719 ir_filter_nr = 0;
00720 } else if (p->pitch_gain[4] < 0.9) {
00721 ir_filter_nr = 1;
00722 } else
00723 ir_filter_nr = 2;
00724
00725
00726 if (fixed_gain > 2.0 * p->prev_sparse_fixed_gain) {
00727 p->ir_filter_onset = 2;
00728 } else if (p->ir_filter_onset)
00729 p->ir_filter_onset--;
00730
00731 if (!p->ir_filter_onset) {
00732 int i, count = 0;
00733
00734 for (i = 0; i < 5; i++)
00735 if (p->pitch_gain[i] < 0.6)
00736 count++;
00737 if (count > 2)
00738 ir_filter_nr = 0;
00739
00740 if (ir_filter_nr > p->prev_ir_filter_nr + 1)
00741 ir_filter_nr--;
00742 } else if (ir_filter_nr < 2)
00743 ir_filter_nr++;
00744
00745
00746
00747
00748 if (fixed_gain < 5.0)
00749 ir_filter_nr = 2;
00750
00751 if (p->cur_frame_mode != MODE_7k4 && p->cur_frame_mode < MODE_10k2
00752 && ir_filter_nr < 2) {
00753 apply_ir_filter(out, fixed_sparse,
00754 (p->cur_frame_mode == MODE_7k95 ?
00755 ir_filters_lookup_MODE_7k95 :
00756 ir_filters_lookup)[ir_filter_nr]);
00757 fixed_vector = out;
00758 }
00759
00760
00761 p->prev_ir_filter_nr = ir_filter_nr;
00762 p->prev_sparse_fixed_gain = fixed_gain;
00763
00764 return fixed_vector;
00765 }
00766
00768
00769
00772
00783 static int synthesis(AMRContext *p, float *lpc,
00784 float fixed_gain, const float *fixed_vector,
00785 float *samples, uint8_t overflow)
00786 {
00787 int i;
00788 float excitation[AMR_SUBFRAME_SIZE];
00789
00790
00791
00792 if (overflow)
00793 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00794 p->pitch_vector[i] *= 0.25;
00795
00796 p->acelpv_ctx.weighted_vector_sumf(excitation, p->pitch_vector, fixed_vector,
00797 p->pitch_gain[4], fixed_gain, AMR_SUBFRAME_SIZE);
00798
00799
00800 if (p->pitch_gain[4] > 0.5 && !overflow) {
00801 float energy = p->celpm_ctx.dot_productf(excitation, excitation,
00802 AMR_SUBFRAME_SIZE);
00803 float pitch_factor =
00804 p->pitch_gain[4] *
00805 (p->cur_frame_mode == MODE_12k2 ?
00806 0.25 * FFMIN(p->pitch_gain[4], 1.0) :
00807 0.5 * FFMIN(p->pitch_gain[4], SHARP_MAX));
00808
00809 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00810 excitation[i] += pitch_factor * p->pitch_vector[i];
00811
00812 ff_scale_vector_to_given_sum_of_squares(excitation, excitation, energy,
00813 AMR_SUBFRAME_SIZE);
00814 }
00815
00816 p->celpf_ctx.celp_lp_synthesis_filterf(samples, lpc, excitation,
00817 AMR_SUBFRAME_SIZE,
00818 LP_FILTER_ORDER);
00819
00820
00821 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00822 if (fabsf(samples[i]) > AMR_SAMPLE_BOUND) {
00823 return 1;
00824 }
00825
00826 return 0;
00827 }
00828
00830
00831
00834
00840 static void update_state(AMRContext *p)
00841 {
00842 memcpy(p->prev_lsp_sub4, p->lsp[3], LP_FILTER_ORDER * sizeof(p->lsp[3][0]));
00843
00844 memmove(&p->excitation_buf[0], &p->excitation_buf[AMR_SUBFRAME_SIZE],
00845 (PITCH_DELAY_MAX + LP_FILTER_ORDER + 1) * sizeof(float));
00846
00847 memmove(&p->pitch_gain[0], &p->pitch_gain[1], 4 * sizeof(float));
00848 memmove(&p->fixed_gain[0], &p->fixed_gain[1], 4 * sizeof(float));
00849
00850 memmove(&p->samples_in[0], &p->samples_in[AMR_SUBFRAME_SIZE],
00851 LP_FILTER_ORDER * sizeof(float));
00852 }
00853
00855
00856
00859
00867 static float tilt_factor(AMRContext *p, float *lpc_n, float *lpc_d)
00868 {
00869 float rh0, rh1;
00870
00871
00872 float impulse_buffer[LP_FILTER_ORDER + AMR_TILT_RESPONSE] = { 0 };
00873 float *hf = impulse_buffer + LP_FILTER_ORDER;
00874
00875 hf[0] = 1.0;
00876 memcpy(hf + 1, lpc_n, sizeof(float) * LP_FILTER_ORDER);
00877 p->celpf_ctx.celp_lp_synthesis_filterf(hf, lpc_d, hf,
00878 AMR_TILT_RESPONSE,
00879 LP_FILTER_ORDER);
00880
00881 rh0 = p->celpm_ctx.dot_productf(hf, hf, AMR_TILT_RESPONSE);
00882 rh1 = p->celpm_ctx.dot_productf(hf, hf + 1, AMR_TILT_RESPONSE - 1);
00883
00884
00885
00886 return rh1 >= 0.0 ? rh1 / rh0 * AMR_TILT_GAMMA_T : 0.0;
00887 }
00888
00897 static void postfilter(AMRContext *p, float *lpc, float *buf_out)
00898 {
00899 int i;
00900 float *samples = p->samples_in + LP_FILTER_ORDER;
00901
00902 float speech_gain = p->celpm_ctx.dot_productf(samples, samples,
00903 AMR_SUBFRAME_SIZE);
00904
00905 float pole_out[AMR_SUBFRAME_SIZE + LP_FILTER_ORDER];
00906 const float *gamma_n, *gamma_d;
00907 float lpc_n[LP_FILTER_ORDER], lpc_d[LP_FILTER_ORDER];
00908
00909 if (p->cur_frame_mode == MODE_12k2 || p->cur_frame_mode == MODE_10k2) {
00910 gamma_n = ff_pow_0_7;
00911 gamma_d = ff_pow_0_75;
00912 } else {
00913 gamma_n = ff_pow_0_55;
00914 gamma_d = ff_pow_0_7;
00915 }
00916
00917 for (i = 0; i < LP_FILTER_ORDER; i++) {
00918 lpc_n[i] = lpc[i] * gamma_n[i];
00919 lpc_d[i] = lpc[i] * gamma_d[i];
00920 }
00921
00922 memcpy(pole_out, p->postfilter_mem, sizeof(float) * LP_FILTER_ORDER);
00923 p->celpf_ctx.celp_lp_synthesis_filterf(pole_out + LP_FILTER_ORDER, lpc_d, samples,
00924 AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
00925 memcpy(p->postfilter_mem, pole_out + AMR_SUBFRAME_SIZE,
00926 sizeof(float) * LP_FILTER_ORDER);
00927
00928 p->celpf_ctx.celp_lp_zero_synthesis_filterf(buf_out, lpc_n,
00929 pole_out + LP_FILTER_ORDER,
00930 AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
00931
00932 ff_tilt_compensation(&p->tilt_mem, tilt_factor(p, lpc_n, lpc_d), buf_out,
00933 AMR_SUBFRAME_SIZE);
00934
00935 ff_adaptive_gain_control(buf_out, buf_out, speech_gain, AMR_SUBFRAME_SIZE,
00936 AMR_AGC_ALPHA, &p->postfilter_agc);
00937 }
00938
00940
00941 static int amrnb_decode_frame(AVCodecContext *avctx, void *data,
00942 int *got_frame_ptr, AVPacket *avpkt)
00943 {
00944
00945 AMRContext *p = avctx->priv_data;
00946 const uint8_t *buf = avpkt->data;
00947 int buf_size = avpkt->size;
00948 float *buf_out;
00949 int i, subframe, ret;
00950 float fixed_gain_factor;
00951 AMRFixed fixed_sparse = {0};
00952 float spare_vector[AMR_SUBFRAME_SIZE];
00953 float synth_fixed_gain;
00954 const float *synth_fixed_vector;
00955
00956
00957 p->avframe.nb_samples = AMR_BLOCK_SIZE;
00958 if ((ret = avctx->get_buffer(avctx, &p->avframe)) < 0) {
00959 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00960 return ret;
00961 }
00962 buf_out = (float *)p->avframe.data[0];
00963
00964 p->cur_frame_mode = unpack_bitstream(p, buf, buf_size);
00965 if (p->cur_frame_mode == NO_DATA) {
00966 av_log(avctx, AV_LOG_ERROR, "Corrupt bitstream\n");
00967 return AVERROR_INVALIDDATA;
00968 }
00969 if (p->cur_frame_mode == MODE_DTX) {
00970 av_log_missing_feature(avctx, "dtx mode", 0);
00971 av_log(avctx, AV_LOG_INFO, "Note: libopencore_amrnb supports dtx\n");
00972 return -1;
00973 }
00974
00975 if (p->cur_frame_mode == MODE_12k2) {
00976 lsf2lsp_5(p);
00977 } else
00978 lsf2lsp_3(p);
00979
00980 for (i = 0; i < 4; i++)
00981 ff_acelp_lspd2lpc(p->lsp[i], p->lpc[i], 5);
00982
00983 for (subframe = 0; subframe < 4; subframe++) {
00984 const AMRNBSubframe *amr_subframe = &p->frame.subframe[subframe];
00985
00986 decode_pitch_vector(p, amr_subframe, subframe);
00987
00988 decode_fixed_sparse(&fixed_sparse, amr_subframe->pulses,
00989 p->cur_frame_mode, subframe);
00990
00991
00992
00993
00994
00995 decode_gains(p, amr_subframe, p->cur_frame_mode, subframe,
00996 &fixed_gain_factor);
00997
00998 pitch_sharpening(p, subframe, p->cur_frame_mode, &fixed_sparse);
00999
01000 if (fixed_sparse.pitch_lag == 0) {
01001 av_log(avctx, AV_LOG_ERROR, "The file is corrupted, pitch_lag = 0 is not allowed\n");
01002 return AVERROR_INVALIDDATA;
01003 }
01004 ff_set_fixed_vector(p->fixed_vector, &fixed_sparse, 1.0,
01005 AMR_SUBFRAME_SIZE);
01006
01007 p->fixed_gain[4] =
01008 ff_amr_set_fixed_gain(fixed_gain_factor,
01009 p->celpm_ctx.dot_productf(p->fixed_vector,
01010 p->fixed_vector,
01011 AMR_SUBFRAME_SIZE) /
01012 AMR_SUBFRAME_SIZE,
01013 p->prediction_error,
01014 energy_mean[p->cur_frame_mode], energy_pred_fac);
01015
01016
01017
01018 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
01019 p->excitation[i] *= p->pitch_gain[4];
01020 ff_set_fixed_vector(p->excitation, &fixed_sparse, p->fixed_gain[4],
01021 AMR_SUBFRAME_SIZE);
01022
01023
01024
01025
01026
01027
01028 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
01029 p->excitation[i] = truncf(p->excitation[i]);
01030
01031
01032
01033
01034 synth_fixed_gain = fixed_gain_smooth(p, p->lsf_q[subframe],
01035 p->lsf_avg, p->cur_frame_mode);
01036
01037 synth_fixed_vector = anti_sparseness(p, &fixed_sparse, p->fixed_vector,
01038 synth_fixed_gain, spare_vector);
01039
01040 if (synthesis(p, p->lpc[subframe], synth_fixed_gain,
01041 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 0))
01042
01043
01044
01045 synthesis(p, p->lpc[subframe], synth_fixed_gain,
01046 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 1);
01047
01048 postfilter(p, p->lpc[subframe], buf_out + subframe * AMR_SUBFRAME_SIZE);
01049
01050
01051 ff_clear_fixed_vector(p->fixed_vector, &fixed_sparse, AMR_SUBFRAME_SIZE);
01052 update_state(p);
01053 }
01054
01055 p->acelpf_ctx.acelp_apply_order_2_transfer_function(buf_out,
01056 buf_out, highpass_zeros,
01057 highpass_poles,
01058 highpass_gain * AMR_SAMPLE_SCALE,
01059 p->high_pass_mem, AMR_BLOCK_SIZE);
01060
01061
01062
01063
01064
01065
01066
01067 p->acelpv_ctx.weighted_vector_sumf(p->lsf_avg, p->lsf_avg, p->lsf_q[3],
01068 0.84, 0.16, LP_FILTER_ORDER);
01069
01070 *got_frame_ptr = 1;
01071 *(AVFrame *)data = p->avframe;
01072
01073
01074 return frame_sizes_nb[p->cur_frame_mode] + 1;
01075 }
01076
01077
01078 AVCodec ff_amrnb_decoder = {
01079 .name = "amrnb",
01080 .type = AVMEDIA_TYPE_AUDIO,
01081 .id = AV_CODEC_ID_AMR_NB,
01082 .priv_data_size = sizeof(AMRContext),
01083 .init = amrnb_decode_init,
01084 .decode = amrnb_decode_frame,
01085 .capabilities = CODEC_CAP_DR1,
01086 .long_name = NULL_IF_CONFIG_SMALL("AMR-NB (Adaptive Multi-Rate NarrowBand)"),
01087 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
01088 AV_SAMPLE_FMT_NONE },
01089 };