/*==================================================================================== EVS Codec 3GPP TS26.443 Jun 30, 2015. Version CR 26.443-0006 ====================================================================================*/ #include #include "options.h" #include "cnst.h" #include "rom_enc.h" #include "prot.h" #include "rom_com.h" /*---------------------------------------------------------------------* * Local functions *---------------------------------------------------------------------*/ static short quant_1p_N1( const short pos, const short N ); static short quant_2p_2N1( const short pos1, const short pos2, const short N ); static short quant_3p_3N1( const short pos1, const short pos2, const short pos3, const short N ); static long quant_4p_4N( const short pos[], const short N ); static long quant_5p_5N( const short pos[], const short N ); static long quant_6p_6N_2( const short pos[], const short N ); static int pre_process( const float v[], short pos_vector[], int pos_vector_num[], int *pulse_pos_num); static int fcb_encode_position( short pos_vector[], int n, int pos_num, int flag ); static int fcb_encode_class( int *buffer, int pulse_num, int pos_num ); static int fcb_encode_PI( const float v[], int pulse_num ); /*---------------------------------------------------------------------* * ACELP_4t64() * * 20, 36, 44, 52, 64, 72, 88 bits algebraic codebook. * 4 tracks x 16 positions per track = 64 samples. * * 20 bits --> 4 pulses in a frame of 64 samples. * 36 bits --> 8 pulses in a frame of 64 samples. * 44 bits 13 + 9 + 13 + 9 --> 10 pulses in a frame of 64 samples. * 52 bits 13 + 13 + 13 + 13 --> 12 pulses in a frame of 64 samples. * 64 bits 2 + 2 + 2 + 2 + 14 + 14 + 14 + 14 --> * 16 pulses in a frame of 64 samples. * 72 bits 10 + 2 + 10 + 2 + 10 + 14 + 10 + 14 --> * 18 pulses in a frame of 64 samples. * 88 bits 11 + 11 + 11 + 11 + 11 + 11 + 11 + 11 --> * 24 pulses in a frame of 64 samples. * All pulses can have two (2) possible amplitudes: +1 or -1. * Each pulse can have sixteen (16) possible positions. *---------------------------------------------------------------------*/ short acelp_4t64( Encoder_State *st, /* i/o: encoder state structure */ float dn[], /* i : corr. between target and h[]. */ const float cn[], /* i : residual after long term prediction */ const float H[], /* i : impulse response of weighted synthesis filter */ float R[], /* i : autocorrelation values */ const short acelpautoc, /* i : autocorrealtion flag */ float code[], /* o : algebraic (fixed) codebook excitation */ float y[], /* o : filtered fixed codebook excitation */ short nbbits, /* i : number of bits per codebook */ const short cmpl_flag, /* i : coomplexity reduction flag */ const short Opt_AMR_WB /* i : flag indicating AMR-WB IO mode */ ) { short i, k, index, track; long L_index; short ind[NPMAXPT*NB_TRACK_FCB_4T+32]; short saved_bits = 0; PulseConfig config; int indexing_indices[6], wordcnt, bitcnt; /*-----------------------------------------------------------------* * Configuration *-----------------------------------------------------------------*/ switch (nbbits) { case 20: /* EVS/AMR-WB pulse indexing: 20 bits, 4 pulses, 4 tracks */ config.nbiter = 4; /* 4x12x16=768 loop */ config.alp = 2.0f; config.nb_pulse = 4; config.fixedpulses = 0; config.nbpos[0] = 4; config.nbpos[1] = 8; break; case 28: /* EVS pulse indexing: 28 bits, 6 pulses, 4 tracks */ config.nbiter = 4; /* 4x20x16=1280 loops */ config.alp = 1.0f; /* coeff for sign setting */ config.nb_pulse = 6; config.fixedpulses = 0; config.nbpos[0] = 6; config.nbpos[1] = 6; config.nbpos[2] = 8; break; case 36: /* EVS/AMR-WB pulse indexing: 36 bits, 8 pulses, 4 tracks */ config.nbiter = 4; /* 4x20x16=1280 loops */ config.alp = 1.0f; /* coeff for sign setting */ config.nb_pulse = 8; config.fixedpulses = 2; config.nbpos[0] = 4; config.nbpos[1] = 8; config.nbpos[2] = 8; break; case 43: /* EVS pulse indexing: 43 bits, 10 pulses, 4 tracks */ case 44: /* AMR-WB pulse indexing: 44 bits, 10 pulses, 4 tracks */ config.nbiter = 4; /* 4x26x16=1664 loops */ config.alp = 1.0f; config.nb_pulse = 10; config.fixedpulses = 2; config.nbpos[0] = 4; config.nbpos[1] = 6; config.nbpos[2] = 8; config.nbpos[3] = 8; break; case 50: /* EVS pulse indexing: 50 bits, 12 pulses, 4 tracks */ case 52: /* AMR-WB pulse indexing: 52 bits, 12 pulses, 4 tracks */ config.nbiter = 4; /* 4x26x16=1664 loops */ config.alp = 1.0f; config.nb_pulse = 12; config.fixedpulses = 4; config.nbpos[0] = 4; config.nbpos[1] = 6; config.nbpos[2] = 8; config.nbpos[3] = 8; break; case 62: /* EVS pulse indexing: 62 bits, 16 pulses, 4 tracks */ case 64: /* AMR-WB pulse indexing: 64 bits, 16 pulses, 4 tracks */ config.nbiter = 3; /* 3x36x16=1728 loops */ config.alp = 0.8F; config.nb_pulse = 16; config.fixedpulses = 4; config.nbpos[0] = 4; config.nbpos[1] = 4; config.nbpos[2] = 6; config.nbpos[3] = 6; config.nbpos[4] = 8; config.nbpos[5] = 8; break; case 72: /* AMR-WB pulse indexing: 72 bits, 18 pulses, 4 tracks */ config.nbiter = 3; /* 3x35x16=1680 loops */ config.alp = 0.75F; config.nb_pulse = 18; config.fixedpulses = 4; config.nbpos[0] = 2; config.nbpos[1] = 3; config.nbpos[2] = 4; config.nbpos[3] = 5; config.nbpos[4] = 6; config.nbpos[5] = 7; config.nbpos[6] = 8; break; case 88: /* AMR-WB pulse indexing: 88 bits, 24 pulses, 4 tracks */ config.nbiter = 2; /* 2x53x16=1696 loop */ config.alp = 0.5f; config.nb_pulse = 24; config.fixedpulses = 4; config.nbpos[0] = 2; config.nbpos[1] = 2; config.nbpos[2] = 3; config.nbpos[3] = 4; config.nbpos[4] = 5; config.nbpos[5] = 6; config.nbpos[6] = 7; config.nbpos[7] = 8; config.nbpos[8] = 8; config.nbpos[9] = 8; break; case 87: /* EVS pulse indexing: 87 bits, 26 pulses, 4 tracks */ config.nbiter = 1; config.alp = 0.5F; config.nb_pulse = 26; config.fixedpulses = 4; config.nbpos[0] = 4; config.nbpos[1] = 6; config.nbpos[2] = 6; config.nbpos[3] = 8; config.nbpos[4] = 8; config.nbpos[5] = 8; config.nbpos[6] = 8; config.nbpos[7] = 8; config.nbpos[8] = 8; config.nbpos[9] = 8; config.nbpos[10] = 8; break; } /* reduce the number of iterations as a compromise between the performance and complexity */ if( cmpl_flag > 0 ) { config.nbiter = cmpl_flag; } config.codetrackpos = TRACKPOS_FIXED_FIRST; config.bits = nbbits; /*-----------------------------------------------------------------* * Search *-----------------------------------------------------------------*/ if( acelpautoc ) { E_ACELP_4tsearchx( dn, cn, R, code, &config, ind ); /* Generate weighted code */ set_f( y, 0.0f, L_SUBFR ); for( i=0; i> 4; bitcnt = nbbits & 15; for ( i = 0; i < wordcnt; i++ ) { push_indice( st, IND_ALG_CDBK_4T64, indexing_indices[i], 16 ); } if ( bitcnt ) { push_indice( st, IND_ALG_CDBK_4T64, indexing_indices[i], bitcnt ); } } else { /* AMR-WB pulse indexing */ if (nbbits == 20) { for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; index = quant_1p_N1(ind[k], 4); push_indice( st, IND_ALG_CDBK_4T64, index, 5 ); } } else if (nbbits == 36) { for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; index = quant_2p_2N1(ind[k], ind[k+1], 4); push_indice( st, IND_ALG_CDBK_4T64, index, 9 ); } } else if (nbbits == 44) { for (track = 0; track < (NB_TRACK_FCB_4T - 2); track++) { k = track * NPMAXPT; index = quant_3p_3N1(ind[k], ind[k+1], ind[k+2], 4); push_indice( st, IND_ALG_CDBK_4T64, index, 13 ); } for (track = 2; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; index = quant_2p_2N1(ind[k], ind[k+1], 4); push_indice( st, IND_ALG_CDBK_4T64, index, 9 ); } } else if (nbbits == 52) { for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track*NPMAXPT; index = quant_3p_3N1(ind[k], ind[k+1], ind[k+2], 4); push_indice( st, IND_ALG_CDBK_4T64, index, 13 ); } } else if (nbbits == 64) { for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; L_index = quant_4p_4N(&ind[k], 4); index = ((L_index >> 14) & 3); push_indice( st, IND_ALG_CDBK_4T64_1, index, 2 ); } for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; L_index = quant_4p_4N(&ind[k], 4); index = (L_index & 0x3FFF); push_indice( st, IND_ALG_CDBK_4T64_2, index, 14 ); } } else if (nbbits == 72) { for (track=0; track< (NB_TRACK_FCB_4T - 2); track++) { k = track * NPMAXPT; L_index = quant_5p_5N(&ind[k], 4); index = ((L_index >> 10) & 0x03FF); push_indice( st, IND_ALG_CDBK_4T64_1, index, 10 ); } for (track = 2; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; L_index = quant_4p_4N(&ind[k], 4); index = ((L_index >> 14) & 3); push_indice( st, IND_ALG_CDBK_4T64_1, index, 2 ); } for (track=0; track< (NB_TRACK_FCB_4T - 2); track++) { k = track * NPMAXPT; L_index = quant_5p_5N(&ind[k], 4); index = (L_index & 0x03FF); push_indice( st, IND_ALG_CDBK_4T64_2, index, 10 ); } for (track = 2; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; L_index = quant_4p_4N(&ind[k], 4); index = (L_index & 0x3FFF); push_indice( st, IND_ALG_CDBK_4T64_2, index, 14 ); } } else if (nbbits == 88) { for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; L_index = quant_6p_6N_2(&ind[k], 4); index = ((L_index >> 11) & 0x07FF); push_indice( st, IND_ALG_CDBK_4T64_1, index, 11 ); } for (track = 0; track < NB_TRACK_FCB_4T; track++) { k = track * NPMAXPT; L_index = quant_6p_6N_2(&ind[k], 4); index = (L_index & 0x07FF); push_indice( st, IND_ALG_CDBK_4T64_2, index, 11 ); } } } return saved_bits; } /*---------------------------------------------------------------------* * Quantization of 1 pulse with N+1 bits: * *---------------------------------------------------------------------*/ static short quant_1p_N1( /* o: return N+1 bits */ const short pos, /* i: position of the pulse */ const short N /* i: number of bits for position */ ) { short mask, index; mask = ((1<>2; pos_vector_num[j] = fabs(v[k]); if (v[k]>0) sign = sign << 1; else sign = ( sign << 1 ) + 1; j++; } } *pulse_pos_num = j; return sign; } /*---------------------------------------------------------------------* *encode the position * *---------------------------------------------------------------------*/ static int fcb_encode_position( /* o: return index of the positions which have pulse*/ short pos_vector[], /* i: position of the pulse on a track */ int n, int pos_num, /* i: the number of position which have pulse */ int flag ) { int i; int mmm1; int temp2; mmm1 = PI_select_table[n][pos_num] - 1; temp2 = pos_num; if (flag) /* no decrease */ { for (i=0; i>2; if (code[k]<0) { tmp += 16; } if (fabs(code[k])>1) { ind[j] = tmp; ind[j+1] = tmp; break; } else { ind[j] = tmp; j++; } } } k = track * NPMAXPT; ps[track] = quant_2p_2N1(ind[k], ind[k+1], 4); p[track] = 2; } joint_index = ps[0]*5472 + ps[1]; if (joint_index >= joint_offset) { joint_index += joint_offset; } else { saved_bits += 1; } idxs[0] = ( ( ps[2] << 9 ) + ps[3] ) & 0xffff; idxs[1] = ( ( joint_index << 2 ) + ( ps[2] >> 7 ) ) & 0xffff; idxs[2] = joint_index >> 14; return saved_bits; }