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