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 "libavutil/channel_layout.h"
00047 #include "avcodec.h"
00048 #include "dsputil.h"
00049 #include "libavutil/common.h"
00050 #include "libavutil/avassert.h"
00051 #include "celp_math.h"
00052 #include "celp_filters.h"
00053 #include "acelp_filters.h"
00054 #include "acelp_vectors.h"
00055 #include "acelp_pitch_delay.h"
00056 #include "lsp.h"
00057 #include "amr.h"
00058 #include "internal.h"
00059
00060 #include "amrnbdata.h"
00061
00062 #define AMR_BLOCK_SIZE 160
00063 #define AMR_SAMPLE_BOUND 32768.0
00064
00065
00074 #define AMR_SAMPLE_SCALE (2.0 / 32768.0)
00075
00077 #define PRED_FAC_MODE_12k2 0.65
00078
00079 #define LSF_R_FAC (8000.0 / 32768.0)
00080 #define MIN_LSF_SPACING (50.0488 / 8000.0)
00081 #define PITCH_LAG_MIN_MODE_12k2 18
00082
00083
00084 #define MIN_ENERGY -14.0
00085
00091 #define SHARP_MAX 0.79449462890625
00092
00094 #define AMR_TILT_RESPONSE 22
00095
00096 #define AMR_TILT_GAMMA_T 0.8
00097
00098 #define AMR_AGC_ALPHA 0.9
00099
00100 typedef struct AMRContext {
00101 AVFrame avframe;
00102 AMRNBFrame frame;
00103 uint8_t bad_frame_indicator;
00104 enum Mode cur_frame_mode;
00105
00106 int16_t prev_lsf_r[LP_FILTER_ORDER];
00107 double lsp[4][LP_FILTER_ORDER];
00108 double prev_lsp_sub4[LP_FILTER_ORDER];
00109
00110 float lsf_q[4][LP_FILTER_ORDER];
00111 float lsf_avg[LP_FILTER_ORDER];
00112
00113 float lpc[4][LP_FILTER_ORDER];
00114
00115 uint8_t pitch_lag_int;
00116
00117 float excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1 + AMR_SUBFRAME_SIZE];
00118 float *excitation;
00119
00120 float pitch_vector[AMR_SUBFRAME_SIZE];
00121 float fixed_vector[AMR_SUBFRAME_SIZE];
00122
00123 float prediction_error[4];
00124 float pitch_gain[5];
00125 float fixed_gain[5];
00126
00127 float beta;
00128 uint8_t diff_count;
00129 uint8_t hang_count;
00130
00131 float prev_sparse_fixed_gain;
00132 uint8_t prev_ir_filter_nr;
00133 uint8_t ir_filter_onset;
00134
00135 float postfilter_mem[10];
00136 float tilt_mem;
00137 float postfilter_agc;
00138 float high_pass_mem[2];
00139
00140 float samples_in[LP_FILTER_ORDER + AMR_SUBFRAME_SIZE];
00141
00142 ACELPFContext acelpf_ctx;
00143 ACELPVContext acelpv_ctx;
00144 CELPFContext celpf_ctx;
00145 CELPMContext celpm_ctx;
00146
00147 } AMRContext;
00148
00150 static void weighted_vector_sumd(double *out, const double *in_a,
00151 const double *in_b, double weight_coeff_a,
00152 double weight_coeff_b, int length)
00153 {
00154 int i;
00155
00156 for (i = 0; i < length; i++)
00157 out[i] = weight_coeff_a * in_a[i]
00158 + weight_coeff_b * in_b[i];
00159 }
00160
00161 static av_cold int amrnb_decode_init(AVCodecContext *avctx)
00162 {
00163 AMRContext *p = avctx->priv_data;
00164 int i;
00165
00166 if (avctx->channels > 1) {
00167 av_log_missing_feature(avctx, "multi-channel AMR", 0);
00168 return AVERROR_PATCHWELCOME;
00169 }
00170
00171 avctx->channels = 1;
00172 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00173 if (!avctx->sample_rate)
00174 avctx->sample_rate = 8000;
00175 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00176
00177
00178 p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1];
00179
00180 for (i = 0; i < LP_FILTER_ORDER; i++) {
00181 p->prev_lsp_sub4[i] = lsp_sub4_init[i] * 1000 / (float)(1 << 15);
00182 p->lsf_avg[i] = p->lsf_q[3][i] = lsp_avg_init[i] / (float)(1 << 15);
00183 }
00184
00185 for (i = 0; i < 4; i++)
00186 p->prediction_error[i] = MIN_ENERGY;
00187
00188 avcodec_get_frame_defaults(&p->avframe);
00189 avctx->coded_frame = &p->avframe;
00190
00191 ff_acelp_filter_init(&p->acelpf_ctx);
00192 ff_acelp_vectors_init(&p->acelpv_ctx);
00193 ff_celp_filter_init(&p->celpf_ctx);
00194 ff_celp_math_init(&p->celpm_ctx);
00195
00196 return 0;
00197 }
00198
00199
00211 static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf,
00212 int buf_size)
00213 {
00214 enum Mode mode;
00215
00216
00217 mode = buf[0] >> 3 & 0x0F;
00218 p->bad_frame_indicator = (buf[0] & 0x4) != 0x4;
00219
00220 if (mode >= N_MODES || buf_size < frame_sizes_nb[mode] + 1) {
00221 return NO_DATA;
00222 }
00223
00224 if (mode < MODE_DTX)
00225 ff_amr_bit_reorder((uint16_t *) &p->frame, sizeof(AMRNBFrame), buf + 1,
00226 amr_unpacking_bitmaps_per_mode[mode]);
00227
00228 return mode;
00229 }
00230
00231
00234
00243 static void interpolate_lsf(ACELPVContext *ctx, float lsf_q[4][LP_FILTER_ORDER], float *lsf_new)
00244 {
00245 int i;
00246
00247 for (i = 0; i < 4; i++)
00248 ctx->weighted_vector_sumf(lsf_q[i], lsf_q[3], lsf_new,
00249 0.25 * (3 - i), 0.25 * (i + 1),
00250 LP_FILTER_ORDER);
00251 }
00252
00264 static void lsf2lsp_for_mode12k2(AMRContext *p, double lsp[LP_FILTER_ORDER],
00265 const float lsf_no_r[LP_FILTER_ORDER],
00266 const int16_t *lsf_quantizer[5],
00267 const int quantizer_offset,
00268 const int sign, const int update)
00269 {
00270 int16_t lsf_r[LP_FILTER_ORDER];
00271 float lsf_q[LP_FILTER_ORDER];
00272 int i;
00273
00274 for (i = 0; i < LP_FILTER_ORDER >> 1; i++)
00275 memcpy(&lsf_r[i << 1], &lsf_quantizer[i][quantizer_offset],
00276 2 * sizeof(*lsf_r));
00277
00278 if (sign) {
00279 lsf_r[4] *= -1;
00280 lsf_r[5] *= -1;
00281 }
00282
00283 if (update)
00284 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
00285
00286 for (i = 0; i < LP_FILTER_ORDER; i++)
00287 lsf_q[i] = lsf_r[i] * (LSF_R_FAC / 8000.0) + lsf_no_r[i] * (1.0 / 8000.0);
00288
00289 ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
00290
00291 if (update)
00292 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
00293
00294 ff_acelp_lsf2lspd(lsp, lsf_q, LP_FILTER_ORDER);
00295 }
00296
00302 static void lsf2lsp_5(AMRContext *p)
00303 {
00304 const uint16_t *lsf_param = p->frame.lsf;
00305 float lsf_no_r[LP_FILTER_ORDER];
00306 const int16_t *lsf_quantizer[5];
00307 int i;
00308
00309 lsf_quantizer[0] = lsf_5_1[lsf_param[0]];
00310 lsf_quantizer[1] = lsf_5_2[lsf_param[1]];
00311 lsf_quantizer[2] = lsf_5_3[lsf_param[2] >> 1];
00312 lsf_quantizer[3] = lsf_5_4[lsf_param[3]];
00313 lsf_quantizer[4] = lsf_5_5[lsf_param[4]];
00314
00315 for (i = 0; i < LP_FILTER_ORDER; i++)
00316 lsf_no_r[i] = p->prev_lsf_r[i] * LSF_R_FAC * PRED_FAC_MODE_12k2 + lsf_5_mean[i];
00317
00318 lsf2lsp_for_mode12k2(p, p->lsp[1], lsf_no_r, lsf_quantizer, 0, lsf_param[2] & 1, 0);
00319 lsf2lsp_for_mode12k2(p, p->lsp[3], lsf_no_r, lsf_quantizer, 2, lsf_param[2] & 1, 1);
00320
00321
00322 weighted_vector_sumd(p->lsp[0], p->prev_lsp_sub4, p->lsp[1], 0.5, 0.5, LP_FILTER_ORDER);
00323 weighted_vector_sumd(p->lsp[2], p->lsp[1] , p->lsp[3], 0.5, 0.5, LP_FILTER_ORDER);
00324 }
00325
00331 static void lsf2lsp_3(AMRContext *p)
00332 {
00333 const uint16_t *lsf_param = p->frame.lsf;
00334 int16_t lsf_r[LP_FILTER_ORDER];
00335 float lsf_q[LP_FILTER_ORDER];
00336 const int16_t *lsf_quantizer;
00337 int i, j;
00338
00339 lsf_quantizer = (p->cur_frame_mode == MODE_7k95 ? lsf_3_1_MODE_7k95 : lsf_3_1)[lsf_param[0]];
00340 memcpy(lsf_r, lsf_quantizer, 3 * sizeof(*lsf_r));
00341
00342 lsf_quantizer = lsf_3_2[lsf_param[1] << (p->cur_frame_mode <= MODE_5k15)];
00343 memcpy(lsf_r + 3, lsf_quantizer, 3 * sizeof(*lsf_r));
00344
00345 lsf_quantizer = (p->cur_frame_mode <= MODE_5k15 ? lsf_3_3_MODE_5k15 : lsf_3_3)[lsf_param[2]];
00346 memcpy(lsf_r + 6, lsf_quantizer, 4 * sizeof(*lsf_r));
00347
00348
00349 for (i = 0; i < LP_FILTER_ORDER; i++)
00350 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);
00351
00352 ff_set_min_dist_lsf(lsf_q, MIN_LSF_SPACING, LP_FILTER_ORDER);
00353
00354
00355 interpolate_lsf(&p->acelpv_ctx, p->lsf_q, lsf_q);
00356 memcpy(p->prev_lsf_r, lsf_r, LP_FILTER_ORDER * sizeof(*lsf_r));
00357
00358 ff_acelp_lsf2lspd(p->lsp[3], lsf_q, LP_FILTER_ORDER);
00359
00360
00361 for (i = 1; i <= 3; i++)
00362 for(j = 0; j < LP_FILTER_ORDER; j++)
00363 p->lsp[i-1][j] = p->prev_lsp_sub4[j] +
00364 (p->lsp[3][j] - p->prev_lsp_sub4[j]) * 0.25 * i;
00365 }
00366
00368
00369
00372
00376 static void decode_pitch_lag_1_6(int *lag_int, int *lag_frac, int pitch_index,
00377 const int prev_lag_int, const int subframe)
00378 {
00379 if (subframe == 0 || subframe == 2) {
00380 if (pitch_index < 463) {
00381 *lag_int = (pitch_index + 107) * 10923 >> 16;
00382 *lag_frac = pitch_index - *lag_int * 6 + 105;
00383 } else {
00384 *lag_int = pitch_index - 368;
00385 *lag_frac = 0;
00386 }
00387 } else {
00388 *lag_int = ((pitch_index + 5) * 10923 >> 16) - 1;
00389 *lag_frac = pitch_index - *lag_int * 6 - 3;
00390 *lag_int += av_clip(prev_lag_int - 5, PITCH_LAG_MIN_MODE_12k2,
00391 PITCH_DELAY_MAX - 9);
00392 }
00393 }
00394
00395 static void decode_pitch_vector(AMRContext *p,
00396 const AMRNBSubframe *amr_subframe,
00397 const int subframe)
00398 {
00399 int pitch_lag_int, pitch_lag_frac;
00400 enum Mode mode = p->cur_frame_mode;
00401
00402 if (p->cur_frame_mode == MODE_12k2) {
00403 decode_pitch_lag_1_6(&pitch_lag_int, &pitch_lag_frac,
00404 amr_subframe->p_lag, p->pitch_lag_int,
00405 subframe);
00406 } else
00407 ff_decode_pitch_lag(&pitch_lag_int, &pitch_lag_frac,
00408 amr_subframe->p_lag,
00409 p->pitch_lag_int, subframe,
00410 mode != MODE_4k75 && mode != MODE_5k15,
00411 mode <= MODE_6k7 ? 4 : (mode == MODE_7k95 ? 5 : 6));
00412
00413 p->pitch_lag_int = pitch_lag_int;
00414
00415 pitch_lag_frac <<= (p->cur_frame_mode != MODE_12k2);
00416
00417 pitch_lag_int += pitch_lag_frac > 0;
00418
00419
00420
00421 p->acelpf_ctx.acelp_interpolatef(p->excitation,
00422 p->excitation + 1 - pitch_lag_int,
00423 ff_b60_sinc, 6,
00424 pitch_lag_frac + 6 - 6*(pitch_lag_frac > 0),
00425 10, AMR_SUBFRAME_SIZE);
00426
00427 memcpy(p->pitch_vector, p->excitation, AMR_SUBFRAME_SIZE * sizeof(float));
00428 }
00429
00431
00432
00435
00439 static void decode_10bit_pulse(int code, int pulse_position[8],
00440 int i1, int i2, int i3)
00441 {
00442
00443
00444 const uint8_t *positions = base_five_table[code >> 3];
00445 pulse_position[i1] = (positions[2] << 1) + ( code & 1);
00446 pulse_position[i2] = (positions[1] << 1) + ((code >> 1) & 1);
00447 pulse_position[i3] = (positions[0] << 1) + ((code >> 2) & 1);
00448 }
00449
00457 static void decode_8_pulses_31bits(const int16_t *fixed_index,
00458 AMRFixed *fixed_sparse)
00459 {
00460 int pulse_position[8];
00461 int i, temp;
00462
00463 decode_10bit_pulse(fixed_index[4], pulse_position, 0, 4, 1);
00464 decode_10bit_pulse(fixed_index[5], pulse_position, 2, 6, 5);
00465
00466
00467
00468 temp = ((fixed_index[6] >> 2) * 25 + 12) >> 5;
00469 pulse_position[3] = temp % 5;
00470 pulse_position[7] = temp / 5;
00471 if (pulse_position[7] & 1)
00472 pulse_position[3] = 4 - pulse_position[3];
00473 pulse_position[3] = (pulse_position[3] << 1) + ( fixed_index[6] & 1);
00474 pulse_position[7] = (pulse_position[7] << 1) + ((fixed_index[6] >> 1) & 1);
00475
00476 fixed_sparse->n = 8;
00477 for (i = 0; i < 4; i++) {
00478 const int pos1 = (pulse_position[i] << 2) + i;
00479 const int pos2 = (pulse_position[i + 4] << 2) + i;
00480 const float sign = fixed_index[i] ? -1.0 : 1.0;
00481 fixed_sparse->x[i ] = pos1;
00482 fixed_sparse->x[i + 4] = pos2;
00483 fixed_sparse->y[i ] = sign;
00484 fixed_sparse->y[i + 4] = pos2 < pos1 ? -sign : sign;
00485 }
00486 }
00487
00503 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const uint16_t *pulses,
00504 const enum Mode mode, const int subframe)
00505 {
00506 av_assert1(MODE_4k75 <= (signed)mode && mode <= MODE_12k2);
00507
00508 if (mode == MODE_12k2) {
00509 ff_decode_10_pulses_35bits(pulses, fixed_sparse, gray_decode, 5, 3);
00510 } else if (mode == MODE_10k2) {
00511 decode_8_pulses_31bits(pulses, fixed_sparse);
00512 } else {
00513 int *pulse_position = fixed_sparse->x;
00514 int i, pulse_subset;
00515 const int fixed_index = pulses[0];
00516
00517 if (mode <= MODE_5k15) {
00518 pulse_subset = ((fixed_index >> 3) & 8) + (subframe << 1);
00519 pulse_position[0] = ( fixed_index & 7) * 5 + track_position[pulse_subset];
00520 pulse_position[1] = ((fixed_index >> 3) & 7) * 5 + track_position[pulse_subset + 1];
00521 fixed_sparse->n = 2;
00522 } else if (mode == MODE_5k9) {
00523 pulse_subset = ((fixed_index & 1) << 1) + 1;
00524 pulse_position[0] = ((fixed_index >> 1) & 7) * 5 + pulse_subset;
00525 pulse_subset = (fixed_index >> 4) & 3;
00526 pulse_position[1] = ((fixed_index >> 6) & 7) * 5 + pulse_subset + (pulse_subset == 3 ? 1 : 0);
00527 fixed_sparse->n = pulse_position[0] == pulse_position[1] ? 1 : 2;
00528 } else if (mode == MODE_6k7) {
00529 pulse_position[0] = (fixed_index & 7) * 5;
00530 pulse_subset = (fixed_index >> 2) & 2;
00531 pulse_position[1] = ((fixed_index >> 4) & 7) * 5 + pulse_subset + 1;
00532 pulse_subset = (fixed_index >> 6) & 2;
00533 pulse_position[2] = ((fixed_index >> 8) & 7) * 5 + pulse_subset + 2;
00534 fixed_sparse->n = 3;
00535 } else {
00536 pulse_position[0] = gray_decode[ fixed_index & 7];
00537 pulse_position[1] = gray_decode[(fixed_index >> 3) & 7] + 1;
00538 pulse_position[2] = gray_decode[(fixed_index >> 6) & 7] + 2;
00539 pulse_subset = (fixed_index >> 9) & 1;
00540 pulse_position[3] = gray_decode[(fixed_index >> 10) & 7] + pulse_subset + 3;
00541 fixed_sparse->n = 4;
00542 }
00543 for (i = 0; i < fixed_sparse->n; i++)
00544 fixed_sparse->y[i] = (pulses[1] >> i) & 1 ? 1.0 : -1.0;
00545 }
00546 }
00547
00556 static void pitch_sharpening(AMRContext *p, int subframe, enum Mode mode,
00557 AMRFixed *fixed_sparse)
00558 {
00559
00560
00561
00562 if (mode == MODE_12k2)
00563 p->beta = FFMIN(p->pitch_gain[4], 1.0);
00564
00565 fixed_sparse->pitch_lag = p->pitch_lag_int;
00566 fixed_sparse->pitch_fac = p->beta;
00567
00568
00569
00570
00571 if (mode != MODE_4k75 || subframe & 1)
00572 p->beta = av_clipf(p->pitch_gain[4], 0.0, SHARP_MAX);
00573 }
00575
00576
00579
00592 static float fixed_gain_smooth(AMRContext *p , const float *lsf,
00593 const float *lsf_avg, const enum Mode mode)
00594 {
00595 float diff = 0.0;
00596 int i;
00597
00598 for (i = 0; i < LP_FILTER_ORDER; i++)
00599 diff += fabs(lsf_avg[i] - lsf[i]) / lsf_avg[i];
00600
00601
00602
00603 p->diff_count++;
00604 if (diff <= 0.65)
00605 p->diff_count = 0;
00606
00607 if (p->diff_count > 10) {
00608 p->hang_count = 0;
00609 p->diff_count--;
00610 }
00611
00612 if (p->hang_count < 40) {
00613 p->hang_count++;
00614 } else if (mode < MODE_7k4 || mode == MODE_10k2) {
00615 const float smoothing_factor = av_clipf(4.0 * diff - 1.6, 0.0, 1.0);
00616 const float fixed_gain_mean = (p->fixed_gain[0] + p->fixed_gain[1] +
00617 p->fixed_gain[2] + p->fixed_gain[3] +
00618 p->fixed_gain[4]) * 0.2;
00619 return smoothing_factor * p->fixed_gain[4] +
00620 (1.0 - smoothing_factor) * fixed_gain_mean;
00621 }
00622 return p->fixed_gain[4];
00623 }
00624
00634 static void decode_gains(AMRContext *p, const AMRNBSubframe *amr_subframe,
00635 const enum Mode mode, const int subframe,
00636 float *fixed_gain_factor)
00637 {
00638 if (mode == MODE_12k2 || mode == MODE_7k95) {
00639 p->pitch_gain[4] = qua_gain_pit [amr_subframe->p_gain ]
00640 * (1.0 / 16384.0);
00641 *fixed_gain_factor = qua_gain_code[amr_subframe->fixed_gain]
00642 * (1.0 / 2048.0);
00643 } else {
00644 const uint16_t *gains;
00645
00646 if (mode >= MODE_6k7) {
00647 gains = gains_high[amr_subframe->p_gain];
00648 } else if (mode >= MODE_5k15) {
00649 gains = gains_low [amr_subframe->p_gain];
00650 } else {
00651
00652 gains = gains_MODE_4k75[(p->frame.subframe[subframe & 2].p_gain << 1) + (subframe & 1)];
00653 }
00654
00655 p->pitch_gain[4] = gains[0] * (1.0 / 16384.0);
00656 *fixed_gain_factor = gains[1] * (1.0 / 4096.0);
00657 }
00658 }
00659
00661
00662
00665
00676 static void apply_ir_filter(float *out, const AMRFixed *in,
00677 const float *filter)
00678 {
00679 float filter1[AMR_SUBFRAME_SIZE],
00680 filter2[AMR_SUBFRAME_SIZE];
00681 int lag = in->pitch_lag;
00682 float fac = in->pitch_fac;
00683 int i;
00684
00685 if (lag < AMR_SUBFRAME_SIZE) {
00686 ff_celp_circ_addf(filter1, filter, filter, lag, fac,
00687 AMR_SUBFRAME_SIZE);
00688
00689 if (lag < AMR_SUBFRAME_SIZE >> 1)
00690 ff_celp_circ_addf(filter2, filter, filter1, lag, fac,
00691 AMR_SUBFRAME_SIZE);
00692 }
00693
00694 memset(out, 0, sizeof(float) * AMR_SUBFRAME_SIZE);
00695 for (i = 0; i < in->n; i++) {
00696 int x = in->x[i];
00697 float y = in->y[i];
00698 const float *filterp;
00699
00700 if (x >= AMR_SUBFRAME_SIZE - lag) {
00701 filterp = filter;
00702 } else if (x >= AMR_SUBFRAME_SIZE - (lag << 1)) {
00703 filterp = filter1;
00704 } else
00705 filterp = filter2;
00706
00707 ff_celp_circ_addf(out, out, filterp, x, y, AMR_SUBFRAME_SIZE);
00708 }
00709 }
00710
00723 static const float *anti_sparseness(AMRContext *p, AMRFixed *fixed_sparse,
00724 const float *fixed_vector,
00725 float fixed_gain, float *out)
00726 {
00727 int ir_filter_nr;
00728
00729 if (p->pitch_gain[4] < 0.6) {
00730 ir_filter_nr = 0;
00731 } else if (p->pitch_gain[4] < 0.9) {
00732 ir_filter_nr = 1;
00733 } else
00734 ir_filter_nr = 2;
00735
00736
00737 if (fixed_gain > 2.0 * p->prev_sparse_fixed_gain) {
00738 p->ir_filter_onset = 2;
00739 } else if (p->ir_filter_onset)
00740 p->ir_filter_onset--;
00741
00742 if (!p->ir_filter_onset) {
00743 int i, count = 0;
00744
00745 for (i = 0; i < 5; i++)
00746 if (p->pitch_gain[i] < 0.6)
00747 count++;
00748 if (count > 2)
00749 ir_filter_nr = 0;
00750
00751 if (ir_filter_nr > p->prev_ir_filter_nr + 1)
00752 ir_filter_nr--;
00753 } else if (ir_filter_nr < 2)
00754 ir_filter_nr++;
00755
00756
00757
00758
00759 if (fixed_gain < 5.0)
00760 ir_filter_nr = 2;
00761
00762 if (p->cur_frame_mode != MODE_7k4 && p->cur_frame_mode < MODE_10k2
00763 && ir_filter_nr < 2) {
00764 apply_ir_filter(out, fixed_sparse,
00765 (p->cur_frame_mode == MODE_7k95 ?
00766 ir_filters_lookup_MODE_7k95 :
00767 ir_filters_lookup)[ir_filter_nr]);
00768 fixed_vector = out;
00769 }
00770
00771
00772 p->prev_ir_filter_nr = ir_filter_nr;
00773 p->prev_sparse_fixed_gain = fixed_gain;
00774
00775 return fixed_vector;
00776 }
00777
00779
00780
00783
00794 static int synthesis(AMRContext *p, float *lpc,
00795 float fixed_gain, const float *fixed_vector,
00796 float *samples, uint8_t overflow)
00797 {
00798 int i;
00799 float excitation[AMR_SUBFRAME_SIZE];
00800
00801
00802
00803 if (overflow)
00804 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00805 p->pitch_vector[i] *= 0.25;
00806
00807 p->acelpv_ctx.weighted_vector_sumf(excitation, p->pitch_vector, fixed_vector,
00808 p->pitch_gain[4], fixed_gain, AMR_SUBFRAME_SIZE);
00809
00810
00811 if (p->pitch_gain[4] > 0.5 && !overflow) {
00812 float energy = p->celpm_ctx.dot_productf(excitation, excitation,
00813 AMR_SUBFRAME_SIZE);
00814 float pitch_factor =
00815 p->pitch_gain[4] *
00816 (p->cur_frame_mode == MODE_12k2 ?
00817 0.25 * FFMIN(p->pitch_gain[4], 1.0) :
00818 0.5 * FFMIN(p->pitch_gain[4], SHARP_MAX));
00819
00820 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00821 excitation[i] += pitch_factor * p->pitch_vector[i];
00822
00823 ff_scale_vector_to_given_sum_of_squares(excitation, excitation, energy,
00824 AMR_SUBFRAME_SIZE);
00825 }
00826
00827 p->celpf_ctx.celp_lp_synthesis_filterf(samples, lpc, excitation,
00828 AMR_SUBFRAME_SIZE,
00829 LP_FILTER_ORDER);
00830
00831
00832 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
00833 if (fabsf(samples[i]) > AMR_SAMPLE_BOUND) {
00834 return 1;
00835 }
00836
00837 return 0;
00838 }
00839
00841
00842
00845
00851 static void update_state(AMRContext *p)
00852 {
00853 memcpy(p->prev_lsp_sub4, p->lsp[3], LP_FILTER_ORDER * sizeof(p->lsp[3][0]));
00854
00855 memmove(&p->excitation_buf[0], &p->excitation_buf[AMR_SUBFRAME_SIZE],
00856 (PITCH_DELAY_MAX + LP_FILTER_ORDER + 1) * sizeof(float));
00857
00858 memmove(&p->pitch_gain[0], &p->pitch_gain[1], 4 * sizeof(float));
00859 memmove(&p->fixed_gain[0], &p->fixed_gain[1], 4 * sizeof(float));
00860
00861 memmove(&p->samples_in[0], &p->samples_in[AMR_SUBFRAME_SIZE],
00862 LP_FILTER_ORDER * sizeof(float));
00863 }
00864
00866
00867
00870
00878 static float tilt_factor(AMRContext *p, float *lpc_n, float *lpc_d)
00879 {
00880 float rh0, rh1;
00881
00882
00883 float impulse_buffer[LP_FILTER_ORDER + AMR_TILT_RESPONSE] = { 0 };
00884 float *hf = impulse_buffer + LP_FILTER_ORDER;
00885
00886 hf[0] = 1.0;
00887 memcpy(hf + 1, lpc_n, sizeof(float) * LP_FILTER_ORDER);
00888 p->celpf_ctx.celp_lp_synthesis_filterf(hf, lpc_d, hf,
00889 AMR_TILT_RESPONSE,
00890 LP_FILTER_ORDER);
00891
00892 rh0 = p->celpm_ctx.dot_productf(hf, hf, AMR_TILT_RESPONSE);
00893 rh1 = p->celpm_ctx.dot_productf(hf, hf + 1, AMR_TILT_RESPONSE - 1);
00894
00895
00896
00897 return rh1 >= 0.0 ? rh1 / rh0 * AMR_TILT_GAMMA_T : 0.0;
00898 }
00899
00908 static void postfilter(AMRContext *p, float *lpc, float *buf_out)
00909 {
00910 int i;
00911 float *samples = p->samples_in + LP_FILTER_ORDER;
00912
00913 float speech_gain = p->celpm_ctx.dot_productf(samples, samples,
00914 AMR_SUBFRAME_SIZE);
00915
00916 float pole_out[AMR_SUBFRAME_SIZE + LP_FILTER_ORDER];
00917 const float *gamma_n, *gamma_d;
00918 float lpc_n[LP_FILTER_ORDER], lpc_d[LP_FILTER_ORDER];
00919
00920 if (p->cur_frame_mode == MODE_12k2 || p->cur_frame_mode == MODE_10k2) {
00921 gamma_n = ff_pow_0_7;
00922 gamma_d = ff_pow_0_75;
00923 } else {
00924 gamma_n = ff_pow_0_55;
00925 gamma_d = ff_pow_0_7;
00926 }
00927
00928 for (i = 0; i < LP_FILTER_ORDER; i++) {
00929 lpc_n[i] = lpc[i] * gamma_n[i];
00930 lpc_d[i] = lpc[i] * gamma_d[i];
00931 }
00932
00933 memcpy(pole_out, p->postfilter_mem, sizeof(float) * LP_FILTER_ORDER);
00934 p->celpf_ctx.celp_lp_synthesis_filterf(pole_out + LP_FILTER_ORDER, lpc_d, samples,
00935 AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
00936 memcpy(p->postfilter_mem, pole_out + AMR_SUBFRAME_SIZE,
00937 sizeof(float) * LP_FILTER_ORDER);
00938
00939 p->celpf_ctx.celp_lp_zero_synthesis_filterf(buf_out, lpc_n,
00940 pole_out + LP_FILTER_ORDER,
00941 AMR_SUBFRAME_SIZE, LP_FILTER_ORDER);
00942
00943 ff_tilt_compensation(&p->tilt_mem, tilt_factor(p, lpc_n, lpc_d), buf_out,
00944 AMR_SUBFRAME_SIZE);
00945
00946 ff_adaptive_gain_control(buf_out, buf_out, speech_gain, AMR_SUBFRAME_SIZE,
00947 AMR_AGC_ALPHA, &p->postfilter_agc);
00948 }
00949
00951
00952 static int amrnb_decode_frame(AVCodecContext *avctx, void *data,
00953 int *got_frame_ptr, AVPacket *avpkt)
00954 {
00955
00956 AMRContext *p = avctx->priv_data;
00957 const uint8_t *buf = avpkt->data;
00958 int buf_size = avpkt->size;
00959 float *buf_out;
00960 int i, subframe, ret;
00961 float fixed_gain_factor;
00962 AMRFixed fixed_sparse = {0};
00963 float spare_vector[AMR_SUBFRAME_SIZE];
00964 float synth_fixed_gain;
00965 const float *synth_fixed_vector;
00966
00967
00968 p->avframe.nb_samples = AMR_BLOCK_SIZE;
00969 if ((ret = ff_get_buffer(avctx, &p->avframe)) < 0) {
00970 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00971 return ret;
00972 }
00973 buf_out = (float *)p->avframe.data[0];
00974
00975 p->cur_frame_mode = unpack_bitstream(p, buf, buf_size);
00976 if (p->cur_frame_mode == NO_DATA) {
00977 av_log(avctx, AV_LOG_ERROR, "Corrupt bitstream\n");
00978 return AVERROR_INVALIDDATA;
00979 }
00980 if (p->cur_frame_mode == MODE_DTX) {
00981 av_log_missing_feature(avctx, "dtx mode", 0);
00982 av_log(avctx, AV_LOG_INFO, "Note: libopencore_amrnb supports dtx\n");
00983 return AVERROR_PATCHWELCOME;
00984 }
00985
00986 if (p->cur_frame_mode == MODE_12k2) {
00987 lsf2lsp_5(p);
00988 } else
00989 lsf2lsp_3(p);
00990
00991 for (i = 0; i < 4; i++)
00992 ff_acelp_lspd2lpc(p->lsp[i], p->lpc[i], 5);
00993
00994 for (subframe = 0; subframe < 4; subframe++) {
00995 const AMRNBSubframe *amr_subframe = &p->frame.subframe[subframe];
00996
00997 decode_pitch_vector(p, amr_subframe, subframe);
00998
00999 decode_fixed_sparse(&fixed_sparse, amr_subframe->pulses,
01000 p->cur_frame_mode, subframe);
01001
01002
01003
01004
01005
01006 decode_gains(p, amr_subframe, p->cur_frame_mode, subframe,
01007 &fixed_gain_factor);
01008
01009 pitch_sharpening(p, subframe, p->cur_frame_mode, &fixed_sparse);
01010
01011 if (fixed_sparse.pitch_lag == 0) {
01012 av_log(avctx, AV_LOG_ERROR, "The file is corrupted, pitch_lag = 0 is not allowed\n");
01013 return AVERROR_INVALIDDATA;
01014 }
01015 ff_set_fixed_vector(p->fixed_vector, &fixed_sparse, 1.0,
01016 AMR_SUBFRAME_SIZE);
01017
01018 p->fixed_gain[4] =
01019 ff_amr_set_fixed_gain(fixed_gain_factor,
01020 p->celpm_ctx.dot_productf(p->fixed_vector,
01021 p->fixed_vector,
01022 AMR_SUBFRAME_SIZE) /
01023 AMR_SUBFRAME_SIZE,
01024 p->prediction_error,
01025 energy_mean[p->cur_frame_mode], energy_pred_fac);
01026
01027
01028
01029 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
01030 p->excitation[i] *= p->pitch_gain[4];
01031 ff_set_fixed_vector(p->excitation, &fixed_sparse, p->fixed_gain[4],
01032 AMR_SUBFRAME_SIZE);
01033
01034
01035
01036
01037
01038
01039 for (i = 0; i < AMR_SUBFRAME_SIZE; i++)
01040 p->excitation[i] = truncf(p->excitation[i]);
01041
01042
01043
01044
01045 synth_fixed_gain = fixed_gain_smooth(p, p->lsf_q[subframe],
01046 p->lsf_avg, p->cur_frame_mode);
01047
01048 synth_fixed_vector = anti_sparseness(p, &fixed_sparse, p->fixed_vector,
01049 synth_fixed_gain, spare_vector);
01050
01051 if (synthesis(p, p->lpc[subframe], synth_fixed_gain,
01052 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 0))
01053
01054
01055
01056 synthesis(p, p->lpc[subframe], synth_fixed_gain,
01057 synth_fixed_vector, &p->samples_in[LP_FILTER_ORDER], 1);
01058
01059 postfilter(p, p->lpc[subframe], buf_out + subframe * AMR_SUBFRAME_SIZE);
01060
01061
01062 ff_clear_fixed_vector(p->fixed_vector, &fixed_sparse, AMR_SUBFRAME_SIZE);
01063 update_state(p);
01064 }
01065
01066 p->acelpf_ctx.acelp_apply_order_2_transfer_function(buf_out,
01067 buf_out, highpass_zeros,
01068 highpass_poles,
01069 highpass_gain * AMR_SAMPLE_SCALE,
01070 p->high_pass_mem, AMR_BLOCK_SIZE);
01071
01072
01073
01074
01075
01076
01077
01078 p->acelpv_ctx.weighted_vector_sumf(p->lsf_avg, p->lsf_avg, p->lsf_q[3],
01079 0.84, 0.16, LP_FILTER_ORDER);
01080
01081 *got_frame_ptr = 1;
01082 *(AVFrame *)data = p->avframe;
01083
01084
01085 return frame_sizes_nb[p->cur_frame_mode] + 1;
01086 }
01087
01088
01089 AVCodec ff_amrnb_decoder = {
01090 .name = "amrnb",
01091 .type = AVMEDIA_TYPE_AUDIO,
01092 .id = AV_CODEC_ID_AMR_NB,
01093 .priv_data_size = sizeof(AMRContext),
01094 .init = amrnb_decode_init,
01095 .decode = amrnb_decode_frame,
01096 .capabilities = CODEC_CAP_DR1,
01097 .long_name = NULL_IF_CONFIG_SMALL("AMR-NB (Adaptive Multi-Rate NarrowBand)"),
01098 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
01099 AV_SAMPLE_FMT_NONE },
01100 };