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 "get_bits.h"
00048 #include "libavutil/common.h"
00049 #include "celp_math.h"
00050 #include "celp_filters.h"
00051 #include "acelp_filters.h"
00052 #include "acelp_vectors.h"
00053 #include "acelp_pitch_delay.h"
00054 #include "lsp.h"
00055 #include "amr.h"
00056
00057 #include "amrnbdata.h"
00058
00059 #define AMR_BLOCK_SIZE 160
00060 #define AMR_SAMPLE_BOUND 32768.0
00061
00062
00071 #define AMR_SAMPLE_SCALE (2.0 / 32768.0)
00072
00074 #define PRED_FAC_MODE_12k2 0.65
00075
00076 #define LSF_R_FAC (8000.0 / 32768.0)
00077 #define MIN_LSF_SPACING (50.0488 / 8000.0)
00078 #define PITCH_LAG_MIN_MODE_12k2 18
00079
00080
00081 #define MIN_ENERGY -14.0
00082
00088 #define SHARP_MAX 0.79449462890625
00089
00091 #define AMR_TILT_RESPONSE 22
00092
00093 #define AMR_TILT_GAMMA_T 0.8
00094
00095 #define AMR_AGC_ALPHA 0.9
00096
00097 typedef struct AMRContext {
00098 AVFrame avframe;
00099 AMRNBFrame frame;
00100 uint8_t bad_frame_indicator;
00101 enum Mode cur_frame_mode;
00102
00103 int16_t prev_lsf_r[LP_FILTER_ORDER];
00104 double lsp[4][LP_FILTER_ORDER];
00105 double prev_lsp_sub4[LP_FILTER_ORDER];
00106
00107 float lsf_q[4][LP_FILTER_ORDER];
00108 float lsf_avg[LP_FILTER_ORDER];
00109
00110 float lpc[4][LP_FILTER_ORDER];
00111
00112 uint8_t pitch_lag_int;
00113
00114 float excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1 + AMR_SUBFRAME_SIZE];
00115 float *excitation;
00116
00117 float pitch_vector[AMR_SUBFRAME_SIZE];
00118 float fixed_vector[AMR_SUBFRAME_SIZE];
00119
00120 float prediction_error[4];
00121 float pitch_gain[5];
00122 float fixed_gain[5];
00123
00124 float beta;
00125 uint8_t diff_count;
00126 uint8_t hang_count;
00127
00128 float prev_sparse_fixed_gain;
00129 uint8_t prev_ir_filter_nr;
00130 uint8_t ir_filter_onset;
00131
00132 float postfilter_mem[10];
00133 float tilt_mem;
00134 float postfilter_agc;
00135 float high_pass_mem[2];
00136
00137 float samples_in[LP_FILTER_ORDER + AMR_SUBFRAME_SIZE];
00138
00139 } AMRContext;
00140
00142 static void weighted_vector_sumd(double *out, const double *in_a,
00143 const double *in_b, double weight_coeff_a,
00144 double weight_coeff_b, int length)
00145 {
00146 int i;
00147
00148 for (i = 0; i < length; i++)
00149 out[i] = weight_coeff_a * in_a[i]
00150 + weight_coeff_b * in_b[i];
00151 }
00152
00153 static av_cold int amrnb_decode_init(AVCodecContext *avctx)
00154 {
00155 AMRContext *p = avctx->priv_data;
00156 int i;
00157
00158 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00159
00160
00161 p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1];
00162
00163 for (i = 0; i < LP_FILTER_ORDER; i++) {
00164 p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15);
00165 p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15);
00166 }
00167
00168 for (i = 0; i < 4; i++)
00169 p->prediction_error[i] = MIN_ENERGY;
00170
00171 avcodec_get_frame_defaults(&p->avframe);
00172 avctx->coded_frame = &p->avframe;
00173
00174 return 0;
00175 }
00176
00177
00189 static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf,
00190 int buf_size)
00191 {
00192 GetBitContext gb;
00193 enum Mode mode;
00194
00195 init_get_bits(&gb, buf, buf_size * 8);
00196
00197
00198 skip_bits(&gb, 1);
00199 mode = get_bits(&gb, 4);
00200 p->bad_frame_indicator = !get_bits1(&gb);
00201 skip_bits(&gb, 2);
00202
00203 if (mode < MODE_DTX)
00204 ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1,
00205 amr_unpacking_bitmaps_per_mode[mode]);
00206
00207 return mode;
00208 }
00209
00210
00213
00221 static void interpolate_lsf(float lsf_q[4][LP_FILTER_ORDER], float *lsf_new)
00222 {
00223 int i;
00224
00225 for (i = 0; i < 4; i++)
00226 ff_weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new,
00227 0.25 * (3 - i), 0.25 * (i + 1),
00228 LP_FILTER_ORDER);
00229 }
00230
00242 static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER],
00243 const float lsf_no_r[LP_FILTER_ORDER],
00244 const int16_t *lsf_quantizer[5],
00245 const int quantizer_offset,
00246 const int sign, const int update)
00247 {
00248 int16_t lsf_r[LP_FILTER_ORDER];
00249 float lsf_q[LP_FILTER_ORDER];
00250 int i;
00251
00252 for (i = 0; i < LP_FILTER_ORDER >> 1; i++)
00253 memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset],
00254 2 * sizeof(*lsf_r));
00255
00256 if (sign) {
00257 lsf_r[4] *= -1;
00258 lsf_r[5] *= -1;
00259 }
00260
00261 if (update)
00262 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
00263
00264 for (i = 0; i < LP_FILTER_ORDER; i++)
00265 lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0);
00266
00267 ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
00268
00269 if (update)
00270 interpolate_lsf(p->lsf_q, lsf_q);
00271
00272 ff_acelp_lsf2lspd(lsp, lsf_q, LP_FILTER_ORDER);
00273 }
00274
00280 static void lsf2lsp_5(AMRContext *p)
00281 {
00282 const uint16_t *lsf_param = p->frame.lsf;
00283 float lsf_no_r[LP_FILTER_ORDER];
00284 const int16_t *lsf_quantizer[5];
00285 int i;
00286
00287 lsf_quantizer[0] = lsf_5_1[lsf_param[0]];
00288 lsf_quantizer[1] = lsf_5_2[lsf_param[1]];
00289 lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1];
00290 lsf_quantizer[3] = lsf_5_4[lsf_param[3]];
00291 lsf_quantizer[4] = lsf_5_5[lsf_param[4]];
00292
00293 for (i = 0; i < LP_FILTER_ORDER; i++)
00294 lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i];
00295
00296 lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0);
00297 lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1);
00298
00299
00300 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER);
00301 weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER);
00302 }
00303
00309 static void lsf2lsp_3(AMRContext *p)
00310 {
00311 const uint16_t *lsf_param = p->frame.lsf;
00312 int16_t lsf_r[LP_FILTER_ORDER];
00313 float lsf_q[LP_FILTER_ORDER];
00314 const int16_t *lsf_quantizer;
00315 int i, j;
00316
00317 lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]];
00318 memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r));
00319
00320 lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)];
00321 memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r));
00322
00323 lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]];
00324 memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r));
00325
00326
00327 for (i = 0; i < LP_FILTER_ORDER; i++)
00328 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);
00329
00330 ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
00331
00332
00333 interpolate_lsf(p->lsf_q, lsf_q);
00334 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
00335
00336 ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
00337
00338
00339 for (i = 1; i <= 3; i++)
00340 for(j = 0; j < LP_FILTER_ORDER; j++)
00341 p->lsp[i-1][j] = p->prev_lsp_sub4[j] +
00342 (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;
00343 }
00344
00346
00347
00350
00354 static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index,
00355 const int prev_lag_int, const int subframe)
00356 {
00357 if (subframe == 0 || subframe == 2) {
00358 if (pitch_index < 463) {
00359 *lag_int = (pitch_index + 107) * 10923 >> 16;
00360 *lag_frac = pitch_index - *lag_int * 6 + 105;
00361 } else {
00362 *lag_int = pitch_index - 368;
00363 *lag_frac = 0;
00364 }
00365 } else {
00366 *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1;
00367 *lag_frac = pitch_index - *lag_int * 6 - 3;
00368 *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2,
00369 PITCH_DELAY_MAX - 9);
00370 }
00371 }
00372
00373 static void decode_pitch_vector(AMRContext *p,
00374 const AMRNBSubframe *amr_subframe,
00375 const int subframe)
00376 {
00377 int pitch_lag_int, pitch_lag_frac;
00378 enum Mode mode = p->cur_frame_mode;
00379
00380 if (p->cur_frame_mode == MODE_12k2) {
00381 decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac,
00382 amr_subframe->p_lag, p->pitch_lag_int,
00383 subframe);
00384 } else
00385 ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac,
00386 amr_subframe->p_lag,
00387 p->pitch_lag_int, subframe,
00388 mode != MODE_4k75 && mode != MODE_5k15,
00389 mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6));
00390
00391 p->pitch_lag_int = pitch_lag_int;
00392
00393 pitch_lag_frac <<= (p->cur_frame_mode != MODE_12k2);
00394
00395 pitch_lag_int += pitch_lag_frac > 0;
00396
00397
00398
00399 ff_acelp_interpolatef(p->excitation, p->excitation + 1 - pitch_lag_int,
00400 ff_b60_sinc, 6,
00401 pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0),
00402 10, AMR_SUBFRAME_SIZE);
00403
00404 memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float));
00405 }
00406
00408
00409
00412
00416 static void decode_10bit_pulse(int code, int pulse_position[8],
00417 int i1, int i2, int i3)
00418 {
00419
00420
00421 const uint8_t *positions = base_five_table[code >> 3];
00422 pulse_position[i1] = (positions[2] << 1) + ( code & 1);
00423 pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1);
00424 pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1);
00425 }
00426
00434 static void decode_8_pulses_31bits(const int16_t *fixed_index,
00435 AMRFixed *fixed_sparse)
00436 {
00437 int pulse_position[8];
00438 int i, temp;
00439
00440 decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1);
00441 decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5);
00442
00443
00444
00445 temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5;
00446 pulse_position[3] = temp % 5;
00447 pulse_position[7] = temp / 5;
00448 if (pulse_position[7] & 1)
00449 pulse_position[3] = 4 - pulse_position[3];
00450 pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1);
00451 pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1);
00452
00453 fixed_sparse->n = 8;
00454 for (i = 0; i < 4; i++) {
00455 const int pos1 = (pulse_position[i] << 2) + i;
00456 const int pos2 = (pulse_position[i + 4] << 2) + i;
00457 const float sign = fixed_index[i] ? -1.0 : 1.0;
00458 fixed_sparse->x[i ] = pos1;
00459 fixed_sparse->x[i + 4] = pos2;
00460 fixed_sparse->y[i ] = sign;
00461 fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign;
00462 }
00463 }
00464
00480 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const uint16_t *pulses,
00481 const enum Mode mode, const int subframe)
00482 {
00483 assert(MODE_4k75 <= mode && mode <= MODE_12k2);
00484
00485 if (mode == MODE_12k2) {
00486 ff_decode_10_pulses_35bits(pulses, fixed_sparse, gray_decode, 5, 3);
00487 } else if (mode == MODE_10k2) {
00488 decode_8_pulses_31bits(pulses, fixed_sparse);
00489 } else {
00490 int *pulse_position = fixed_sparse->x;
00491 int i, pulse_subset;
00492 const int fixed_index = pulses[0];
00493
00494 if (mode <= MODE_5k15) {
00495 pulse_subset = ((fixed_index >> 3) & 8) + (subframe << 1);
00496 pulse_position[0] = ( fixed_index & 7) * 5 + track_position[pulse_subset];
00497 pulse_position[1] = ((fixed_index >> 3) & 7) * 5 + track_position[pulse_subset + 1];
00498 fixed_sparse->n = 2;
00499 } else if (mode == MODE_5k9) {
00500 pulse_subset = ((fixed_index & 1) << 1) + 1;
00501 pulse_position[0] = ((fixed_index >> 1) & 7) * 5 + pulse_subset;
00502 pulse_subset = (fixed_index >> 4) & 3;
00503 pulse_position[1] = ((fixed_index >> 6) & 7) * 5 + pulse_subset + (pulse_subset == 3 ? 1 : 0);
00504 fixed_sparse->n = pulse_position[0] == pulse_position[1] ? 1 : 2;
00505 } else if (mode == MODE_6k7) {
00506 pulse_position[0] = (fixed_index & 7) * 5;
00507 pulse_subset = (fixed_index >> 2) & 2;
00508 pulse_position[1] = ((fixed_index >> 4) & 7) * 5 + pulse_subset + 1;
00509 pulse_subset = (fixed_index >> 6) & 2;
00510 pulse_position[2] = ((fixed_index >> 8) & 7) * 5 + pulse_subset + 2;
00511 fixed_sparse->n = 3;
00512 } else {
00513 pulse_position[0] = gray_decode[ fixed_index & 7];
00514 pulse_position[1] = gray_decode[(fixed_index >> 3) & 7] + 1;
00515 pulse_position[2] = gray_decode[(fixed_index >> 6) & 7] + 2;
00516 pulse_subset = (fixed_index >> 9) & 1;
00517 pulse_position[3] = gray_decode[(fixed_index >> 10) & 7] + pulse_subset + 3;
00518 fixed_sparse->n = 4;
00519 }
00520 for (i = 0; i < fixed_sparse->n; i++)
00521 fixed_sparse->y[i] = (pulses[1] >> i) & 1 ? 1.0 : -1.0;
00522 }
00523 }
00524
00533 static void pitch_sharpening(AMRContext *p, int subframe, enum Mode mode,
00534 AMRFixed *fixed_sparse)
00535 {
00536
00537
00538
00539 if (mode == MODE_12k2)
00540 p->beta = FFMIN(p->pitch_gain[4], 1.0);
00541
00542 fixed_sparse->pitch_lag = p->pitch_lag_int;
00543 fixed_sparse->pitch_fac = p->beta;
00544
00545
00546
00547
00548 if (mode != MODE_4k75 || subframe & 1)
00549 p->beta = av_clipf(p->pitch_gain[4], 0.0, SHARP_MAX);
00550 }
00552
00553
00556
00569 static float fixed_gain_smooth(AMRContext *p , const float *lsf,
00570 const float *lsf_avg, const enum Mode mode)
00571 {
00572 float diff = 0.0;
00573 int i;
00574
00575 for (i = 0; i < LP_FILTER_ORDER; i++)
00576 diff += fabs(lsf_avg[i] - lsf[i]) / lsf_avg[i];
00577
00578
00579
00580 p->diff_count++;
00581 if (diff <= 0.65)
00582 p->diff_count = 0;
00583
00584 if (p->diff_count > 10) {
00585 p->hang_count = 0;
00586 p->diff_count--;
00587 }
00588
00589 if (p->hang_count < 40) {
00590 p->hang_count++;
00591 } else if (mode < MODE_7k4 || mode == MODE_10k2) {
00592 const float smoothing_factor = av_clipf(4.0 * diff - 1.6, 0.0, 1.0);
00593 const float fixed_gain_mean = (p->fixed_gain[0] + p->fixed_gain[1] +
00594 p->fixed_gain[2] + p->fixed_gain[3] +
00595 p->fixed_gain[4]) * 0.2;
00596 return smoothing_factor * p->fixed_gain[4] +
00597 (1.0 - smoothing_factor) * fixed_gain_mean;
00598 }
00599 return p->fixed_gain[4];
00600 }
00601
00611 static void decode_gains(AMRContext *p, const AMRNBSubframe *amr_subframe,
00612 const enum Mode mode, const int subframe,
00613 float *fixed_gain_factor)
00614 {
00615 if (mode == MODE_12k2 || mode == MODE_7k95) {
00616 p->pitch_gain[4] = qua_gain_pit [amr_subframe->p_gain ]
00617 * (1.0 / 16384.0);
00618 *fixed_gain_factor = qua_gain_code[amr_subframe->fixed_gain]
00619 * (1.0 / 2048.0);
00620 } else {
00621 const uint16_t *gains;
00622
00623 if (mode >= MODE_6k7) {
00624 gains = gains_high[amr_subframe->p_gain];
00625 } else if (mode >= MODE_5k15) {
00626 gains = gains_low [amr_subframe->p_gain];
00627 } else {
00628
00629 gains = gains_MODE_4k75[(p->frame.subframe[subframe & 2].p_gain << 1) + (subframe & 1)];
00630 }
00631
00632 p->pitch_gain[4] = gains[0] * (1.0 / 16384.0);
00633 *fixed_gain_factor = gains[1] * (1.0 / 4096.0);
00634 }
00635 }
00636
00638
00639
00642
00653 static void apply_ir_filter(float *out, const AMRFixed *in,
00654 const float *filter)
00655 {
00656 float filter1[AMR_SUBFRAME_SIZE],
00657 filter2[AMR_SUBFRAME_SIZE];
00658 int lag = in->pitch_lag;
00659 float fac = in->pitch_fac;
00660 int i;
00661
00662 if (lag < AMR_SUBFRAME_SIZE) {
00663 ff_celp_circ_addf(filter1, filter, filter, lag, fac,
00664 AMR_SUBFRAME_SIZE);
00665
00666 if (lag < AMR_SUBFRAME_SIZE >> 1)
00667 ff_celp_circ_addf(filter2, filter, filter1, lag, fac,
00668 AMR_SUBFRAME_SIZE);
00669 }
00670
00671 memset(out, 0, sizeof(float) * AMR_SUBFRAME_SIZE);
00672 for (i = 0; i < in->n; i++) {
00673 int x = in->x[i];
00674 float y = in->y[i];
00675 const float *filterp;
00676
00677 if (x >= AMR_SUBFRAME_SIZE - lag) {
00678 filterp = filter;
00679 } else if (x >= AMR_SUBFRAME_SIZE - (lag << 1)) {
00680 filterp = filter1;
00681 } else
00682 filterp = filter2;
00683
00684 ff_celp_circ_addf(out, out, filterp, x, y, AMR_SUBFRAME_SIZE);
00685 }
00686 }
00687
00700 static const float *anti_sparseness(AMRContext *p, AMRFixed *fixed_sparse,
00701 const float *fixed_vector,
00702 float fixed_gain, float *out)
00703 {
00704 int ir_filter_nr;
00705
00706 if (p->pitch_gain[4] < 0.6) {
00707 ir_filter_nr = 0;
00708 } else if (p->pitch_gain[4] < 0.9) {
00709 ir_filter_nr = 1;
00710 } else
00711 ir_filter_nr = 2;
00712
00713
00714 if (fixed_gain > 2.0 * p->prev_sparse_fixed_gain) {
00715 p->ir_filter_onset = 2;
00716 } else if (p->ir_filter_onset)
00717 p->ir_filter_onset--;
00718
00719 if (!p->ir_filter_onset) {
00720 int i, count = 0;
00721
00722 for (i = 0; i < 5; i++)
00723 if (p->pitch_gain[i] < 0.6)
00724 count++;
00725 if (count > 2)
00726 ir_filter_nr = 0;
00727
00728 if (ir_filter_nr > p->prev_ir_filter_nr + 1)
00729 ir_filter_nr--;
00730 } else if (ir_filter_nr < 2)
00731 ir_filter_nr++;
00732
00733
00734
00735
00736 if (fixed_gain < 5.0)
00737 ir_filter_nr = 2;
00738
00739 if (p->cur_frame_mode != MODE_7k4 && p->cur_frame_mode < MODE_10k2
00740 && ir_filter_nr < 2) {
00741 apply_ir_filter(out, fixed_sparse,
00742 (p->cur_frame_mode == MODE_7k95 ?
00743 ir_filters_lookup_MODE_7k95 :
00744 ir_filters_lookup)[ir_filter_nr]);
00745 fixed_vector = out;
00746 }
00747
00748
00749 p->prev_ir_filter_nr = ir_filter_nr;
00750 p->prev_sparse_fixed_gain = fixed_gain;
00751
00752 return fixed_vector;
00753 }
00754
00756
00757
00760
00771 static int synthesis(AMRContext *p, float *lpc,
00772 float fixed_gain, const float *fixed_vector,
00773 float *samples, uint8_t overflow)
00774 {
00775 int i;
00776 float excitation[AMR_SUBFRAME_SIZE];
00777
00778
00779
00780 if (overflow)
00781 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00782 p->pitch_vector[i] *= 0.25;
00783
00784 ff_weighted_vector_sumf(excitation, p->pitch_vector, fixed_vector,
00785 p->pitch_gain[4], fixed_gain, AMR_SUBFRAME_SIZE);
00786
00787
00788 if (p->pitch_gain[4] > 0.5 && !overflow) {
00789 float energy = ff_dot_productf(excitation, excitation,
00790 AMR_SUBFRAME_SIZE);
00791 float pitch_factor =
00792 p->pitch_gain[4] *
00793 (p->cur_frame_mode == MODE_12k2 ?
00794 0.25 * FFMIN(p->pitch_gain[4], 1.0) :
00795 0.5 * FFMIN(p->pitch_gain[4], SHARP_MAX));
00796
00797 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00798 excitation[i] += pitch_factor * p->pitch_vector[i];
00799
00800 ff_scale_vector_to_given_sum_of_squares(excitation, excitation, energy,
00801 AMR_SUBFRAME_SIZE);
00802 }
00803
00804 ff_celp_lp_synthesis_filterf(samples, lpc, excitation, AMR_SUBFRAME_SIZE,
00805 LP_FILTER_ORDER);
00806
00807
00808 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00809 if (fabsf(samples[i]) > AMR_SAMPLE_BOUND) {
00810 return 1;
00811 }
00812
00813 return 0;
00814 }
00815
00817
00818
00821
00827 static void update_state(AMRContext *p)
00828 {
00829 memcpy(p->prev_lsp_sub4, p->lsp[3], LP_FILTER_ORDER * sizeof(p->lsp[3][0]));
00830
00831 memmove(&p->excitation_buf[0], &p->excitation_buf[AMR_SUBFRAME_SIZE],
00832 (PITCH_DELAY_MAX + LP_FILTER_ORDER + 1) * sizeof(float));
00833
00834 memmove(&p->pitch_gain[0], &p->pitch_gain[1], 4 * sizeof(float));
00835 memmove(&p->fixed_gain[0], &p->fixed_gain[1], 4 * sizeof(float));
00836
00837 memmove(&p->samples_in[0], &p->samples_in[AMR_SUBFRAME_SIZE],
00838 LP_FILTER_ORDER * sizeof(float));
00839 }
00840
00842
00843
00846
00853 static float tilt_factor(float *lpc_n, float *lpc_d)
00854 {
00855 float rh0, rh1;
00856
00857
00858 float impulse_buffer[LP_FILTER_ORDER + AMR_TILT_RESPONSE] = { 0 };
00859 float *hf = impulse_buffer + LP_FILTER_ORDER;
00860
00861 hf[0] = 1.0;
00862 memcpy(hf + 1, lpc_n, sizeof(float) * LP_FILTER_ORDER);
00863 ff_celp_lp_synthesis_filterf(hf, lpc_d, hf, AMR_TILT_RESPONSE,
00864 LP_FILTER_ORDER);
00865
00866 rh0 = ff_dot_productf(hf, hf, AMR_TILT_RESPONSE);
00867 rh1 = ff_dot_productf(hf, hf + 1, AMR_TILT_RESPONSE - 1);
00868
00869
00870
00871 return rh1 >= 0.0 ? rh1 / rh0 * AMR_TILT_GAMMA_T : 0.0;
00872 }
00873
00882 static void postfilter(AMRContext *p, float *lpc, float *buf_out)
00883 {
00884 int i;
00885 float *samples = p->samples_in + LP_FILTER_ORDER;
00886
00887 float speech_gain = ff_dot_productf(samples, samples,
00888 AMR_SUBFRAME_SIZE);
00889
00890 float pole_out[AMR_SUBFRAME_SIZE + LP_FILTER_ORDER];
00891 const float *gamma_n, *gamma_d;
00892 float lpc_n[LP_FILTER_ORDER], lpc_d[LP_FILTER_ORDER];
00893
00894 if (p->cur_frame_mode == MODE_12k2 || p->cur_frame_mode == MODE_10k2) {
00895 gamma_n = ff_pow_0_7;
00896 gamma_d = ff_pow_0_75;
00897 } else {
00898 gamma_n = ff_pow_0_55;
00899 gamma_d = ff_pow_0_7;
00900 }
00901
00902 for (i = 0; i < LP_FILTER_ORDER; i++) {
00903 lpc_n[i] = lpc[i] * gamma_n[i];
00904 lpc_d[i] = lpc[i] * gamma_d[i];
00905 }
00906
00907 memcpy(pole_out, p->postfilter_mem, sizeof(float) * LP_FILTER_ORDER);
00908 ff_celp_lp_synthesis_filterf(pole_out + LP_FILTER_ORDER, lpc_d, samples,
00909 AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
00910 memcpy(p->postfilter_mem, pole_out + AMR_SUBFRAME_SIZE,
00911 sizeof(float) * LP_FILTER_ORDER);
00912
00913 ff_celp_lp_zero_synthesis_filterf(buf_out, lpc_n,
00914 pole_out + LP_FILTER_ORDER,
00915 AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
00916
00917 ff_tilt_compensation(&p->tilt_mem, tilt_factor(lpc_n, lpc_d), buf_out,
00918 AMR_SUBFRAME_SIZE);
00919
00920 ff_adaptive_gain_control(buf_out, buf_out, speech_gain, AMR_SUBFRAME_SIZE,
00921 AMR_AGC_ALPHA, &p->postfilter_agc);
00922 }
00923
00925
00926 static int amrnb_decode_frame(AVCodecContext *avctx, void *data,
00927 int *got_frame_ptr, AVPacket *avpkt)
00928 {
00929
00930 AMRContext *p = avctx->priv_data;
00931 const uint8_t *buf = avpkt->data;
00932 int buf_size = avpkt->size;
00933 float *buf_out;
00934 int i, subframe, ret;
00935 float fixed_gain_factor;
00936 AMRFixed fixed_sparse = {0};
00937 float spare_vector[AMR_SUBFRAME_SIZE];
00938 float synth_fixed_gain;
00939 const float *synth_fixed_vector;
00940
00941
00942 p->avframe.nb_samples = AMR_BLOCK_SIZE;
00943 if ((ret = avctx->get_buffer(avctx, &p->avframe)) < 0) {
00944 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00945 return ret;
00946 }
00947 buf_out = (float *)p->avframe.data[0];
00948
00949 p->cur_frame_mode = unpack_bitstream(p, buf, buf_size);
00950 if (p->cur_frame_mode == MODE_DTX) {
00951 av_log_missing_feature(avctx, "dtx mode", 0);
00952 av_log(avctx, AV_LOG_INFO, "Note: libopencore_amrnb supports dtx\n");
00953 return -1;
00954 }
00955
00956 if (p->cur_frame_mode == MODE_12k2) {
00957 lsf2lsp_5(p);
00958 } else
00959 lsf2lsp_3(p);
00960
00961 for (i = 0; i < 4; i++)
00962 ff_acelp_lspd2lpc(p->lsp[i], p->lpc[i], 5);
00963
00964 for (subframe = 0; subframe < 4; subframe++) {
00965 const AMRNBSubframe *amr_subframe = &p->frame.subframe[subframe];
00966
00967 decode_pitch_vector(p, amr_subframe, subframe);
00968
00969 decode_fixed_sparse(&fixed_sparse, amr_subframe->pulses,
00970 p->cur_frame_mode, subframe);
00971
00972
00973
00974
00975
00976 decode_gains(p, amr_subframe, p->cur_frame_mode, subframe,
00977 &fixed_gain_factor);
00978
00979 pitch_sharpening(p, subframe, p->cur_frame_mode, &fixed_sparse);
00980
00981 ff_set_fixed_vector(p->fixed_vector, &fixed_sparse, 1.0,
00982 AMR_SUBFRAME_SIZE);
00983
00984 p->fixed_gain[4] =
00985 ff_amr_set_fixed_gain(fixed_gain_factor,
00986 ff_dot_productf(p->fixed_vector, p->fixed_vector,
00987 AMR_SUBFRAME_SIZE)/AMR_SUBFRAME_SIZE,
00988 p->prediction_error,
00989 energy_mean[p->cur_frame_mode], energy_pred_fac);
00990
00991
00992
00993 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00994 p->excitation[i] *= p->pitch_gain[4];
00995 ff_set_fixed_vector(p->excitation, &fixed_sparse, p->fixed_gain[4],
00996 AMR_SUBFRAME_SIZE);
00997
00998
00999
01000
01001
01002
01003 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
01004 p->excitation[i] = truncf(p->excitation[i]);
01005
01006
01007
01008
01009 synth_fixed_gain = fixed_gain_smooth(p, p->lsf_q[subframe],
01010 p->lsf_avg, p->cur_frame_mode);
01011
01012 synth_fixed_vector = anti_sparseness(p, &fixed_sparse, p->fixed_vector,
01013 synth_fixed_gain, spare_vector);
01014
01015 if (synthesis(p, p->lpc[subframe], synth_fixed_gain,
01016 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 0))
01017
01018
01019
01020 synthesis(p, p->lpc[subframe], synth_fixed_gain,
01021 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 1);
01022
01023 postfilter(p, p->lpc[subframe], buf_out + subframe * AMR_SUBFRAME_SIZE);
01024
01025
01026 ff_clear_fixed_vector(p->fixed_vector, &fixed_sparse, AMR_SUBFRAME_SIZE);
01027 update_state(p);
01028 }
01029
01030 ff_acelp_apply_order_2_transfer_function(buf_out, buf_out, highpass_zeros,
01031 highpass_poles,
01032 highpass_gain * AMR_SAMPLE_SCALE,
01033 p->high_pass_mem, AMR_BLOCK_SIZE);
01034
01035
01036
01037
01038
01039
01040
01041 ff_weighted_vector_sumf(p->lsf_avg, p->lsf_avg, p->lsf_q[3],
01042 0.84, 0.16, LP_FILTER_ORDER);
01043
01044 *got_frame_ptr = 1;
01045 *(AVFrame *)data = p->avframe;
01046
01047
01048 return frame_sizes_nb[p->cur_frame_mode] + 1;
01049 }
01050
01051
01052 AVCodec ff_amrnb_decoder = {
01053 .name = "amrnb",
01054 .type = AVMEDIA_TYPE_AUDIO,
01055 .id = CODEC_ID_AMR_NB,
01056 .priv_data_size = sizeof(AMRContext),
01057 .init = amrnb_decode_init,
01058 .decode = amrnb_decode_frame,
01059 .capabilities = CODEC_CAP_DR1,
01060 .long_name = NULL_IF_CONFIG_SMALL("Adaptive Multi-Rate NarrowBand"),
01061 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
01062 };