diff options
Diffstat (limited to 'apps/codecs/libspeex/nb_celp.c')
-rw-r--r-- | apps/codecs/libspeex/nb_celp.c | 77 |
1 files changed, 32 insertions, 45 deletions
diff --git a/apps/codecs/libspeex/nb_celp.c b/apps/codecs/libspeex/nb_celp.c index a2da760156..64ecb58689 100644 --- a/apps/codecs/libspeex/nb_celp.c +++ b/apps/codecs/libspeex/nb_celp.c | |||
@@ -209,7 +209,7 @@ void *nb_encoder_init(const SpeexMode *m) | |||
209 | st->highpass_enabled = 1; | 209 | st->highpass_enabled = 1; |
210 | 210 | ||
211 | #ifdef ENABLE_VALGRIND | 211 | #ifdef ENABLE_VALGRIND |
212 | VALGRIND_MAKE_READABLE(st, (st->stack-(char*)st)); | 212 | VALGRIND_MAKE_READABLE(st, NB_ENC_STACK); |
213 | #endif | 213 | #endif |
214 | return st; | 214 | return st; |
215 | } | 215 | } |
@@ -355,7 +355,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
355 | 355 | ||
356 | 356 | ||
357 | /*Open-loop pitch*/ | 357 | /*Open-loop pitch*/ |
358 | if (st->complexity>2 || !st->submodes[st->submodeID] || st->vbr_enabled || st->vad_enabled || SUBMODE(forced_pitch_gain) || | 358 | if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || st->vbr_enabled || st->vad_enabled || SUBMODE(forced_pitch_gain) || |
359 | SUBMODE(lbr_pitch) != -1) | 359 | SUBMODE(lbr_pitch) != -1) |
360 | { | 360 | { |
361 | int nol_pitch[6]; | 361 | int nol_pitch[6]; |
@@ -440,7 +440,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
440 | #endif | 440 | #endif |
441 | { | 441 | { |
442 | spx_word16_t g = compute_rms16(st->exc, st->frameSize); | 442 | spx_word16_t g = compute_rms16(st->exc, st->frameSize); |
443 | if (ol_pitch>0) | 443 | if (st->submodeID!=1 && ol_pitch>0) |
444 | ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14), | 444 | ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14), |
445 | spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16))))); | 445 | spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16))))); |
446 | else | 446 | else |
@@ -787,18 +787,15 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
787 | /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/ | 787 | /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/ |
788 | #endif | 788 | #endif |
789 | 789 | ||
790 | /*FIXME: This will break if we change the window size */ | ||
791 | speex_assert(st->windowSize-st->frameSize == st->subframeSize); | ||
792 | if (sub==0) | ||
790 | { | 793 | { |
791 | /*FIXME: This will break if we change the window size */ | 794 | for (i=0;i<st->subframeSize;i++) |
792 | if (st->windowSize-st->frameSize != st->subframeSize) | 795 | real_exc[i] = sw[i] = st->winBuf[i]; |
793 | speex_error("windowSize-frameSize != subframeSize"); | 796 | } else { |
794 | if (sub==0) | 797 | for (i=0;i<st->subframeSize;i++) |
795 | { | 798 | real_exc[i] = sw[i] = in[i+((sub-1)*st->subframeSize)]; |
796 | for (i=0;i<st->subframeSize;i++) | ||
797 | real_exc[i] = sw[i] = st->winBuf[i]; | ||
798 | } else { | ||
799 | for (i=0;i<st->subframeSize;i++) | ||
800 | real_exc[i] = sw[i] = in[i+((sub-1)*st->subframeSize)]; | ||
801 | } | ||
802 | } | 799 | } |
803 | fir_mem16(real_exc, interp_qlpc, real_exc, st->subframeSize, st->lpcSize, st->mem_exc2, stack); | 800 | fir_mem16(real_exc, interp_qlpc, real_exc, st->subframeSize, st->lpcSize, st->mem_exc2, stack); |
804 | 801 | ||
@@ -845,7 +842,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
845 | exc[i]=0; | 842 | exc[i]=0; |
846 | 843 | ||
847 | /* If we have a long-term predictor (otherwise, something's wrong) */ | 844 | /* If we have a long-term predictor (otherwise, something's wrong) */ |
848 | if (SUBMODE(ltp_quant)) | 845 | speex_assert (SUBMODE(ltp_quant)); |
849 | { | 846 | { |
850 | int pit_min, pit_max; | 847 | int pit_min, pit_max; |
851 | /* Long-term prediction */ | 848 | /* Long-term prediction */ |
@@ -894,10 +891,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
894 | #endif | 891 | #endif |
895 | 892 | ||
896 | st->pitch[sub]=pitch; | 893 | st->pitch[sub]=pitch; |
897 | } else { | ||
898 | speex_error ("No pitch prediction, what's wrong"); | ||
899 | } | 894 | } |
900 | |||
901 | /* Quantization of innovation */ | 895 | /* Quantization of innovation */ |
902 | for (i=0;i<st->subframeSize;i++) | 896 | for (i=0;i<st->subframeSize;i++) |
903 | innov[i]=0; | 897 | innov[i]=0; |
@@ -944,7 +938,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
944 | signal_div(target, target, ener, st->subframeSize); | 938 | signal_div(target, target, ener, st->subframeSize); |
945 | 939 | ||
946 | /* Quantize innovation */ | 940 | /* Quantize innovation */ |
947 | if (SUBMODE(innovation_quant)) | 941 | speex_assert (SUBMODE(innovation_quant)); |
948 | { | 942 | { |
949 | /* Codebook search */ | 943 | /* Codebook search */ |
950 | SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2, | 944 | SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2, |
@@ -980,11 +974,8 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) | |||
980 | { | 974 | { |
981 | st->innov_rms_save[sub] = compute_rms(innov, st->subframeSize); | 975 | st->innov_rms_save[sub] = compute_rms(innov, st->subframeSize); |
982 | } | 976 | } |
983 | } else { | ||
984 | speex_error("No fixed codebook"); | ||
985 | } | 977 | } |
986 | 978 | ||
987 | |||
988 | for (i=0;i<st->subframeSize;i++) | 979 | for (i=0;i<st->subframeSize;i++) |
989 | sw[i] = exc[i]; | 980 | sw[i] = exc[i]; |
990 | /* Final signal synthesis from excitation */ | 981 | /* Final signal synthesis from excitation */ |
@@ -1101,7 +1092,7 @@ void *nb_decoder_init(const SpeexMode *m) | |||
1101 | st->highpass_enabled = 1; | 1092 | st->highpass_enabled = 1; |
1102 | 1093 | ||
1103 | #ifdef ENABLE_VALGRIND | 1094 | #ifdef ENABLE_VALGRIND |
1104 | VALGRIND_MAKE_READABLE(st, (st->stack-(char*)st)); | 1095 | VALGRIND_MAKE_READABLE(st, NB_DEC_STACK); |
1105 | #endif | 1096 | #endif |
1106 | return st; | 1097 | return st; |
1107 | } | 1098 | } |
@@ -1330,10 +1321,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) | |||
1330 | ALLOC(lpc, st->lpcSize, spx_coef_t); | 1321 | ALLOC(lpc, st->lpcSize, spx_coef_t); |
1331 | bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, st->lpcSize); | 1322 | bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, st->lpcSize); |
1332 | { | 1323 | { |
1333 | float innov_gain=0; | 1324 | spx_word16_t innov_gain=0; |
1334 | float pgain=GAIN_SCALING_1*st->last_pitch_gain; | ||
1335 | if (pgain>.6) | ||
1336 | pgain=.6; | ||
1337 | /* FIXME: This was innov, not exc */ | 1325 | /* FIXME: This was innov, not exc */ |
1338 | innov_gain = compute_rms16(st->exc, st->frameSize); | 1326 | innov_gain = compute_rms16(st->exc, st->frameSize); |
1339 | for (i=0;i<st->frameSize;i++) | 1327 | for (i=0;i<st->frameSize;i++) |
@@ -1477,7 +1465,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) | |||
1477 | exc[i]=0; | 1465 | exc[i]=0; |
1478 | 1466 | ||
1479 | /*Adaptive codebook contribution*/ | 1467 | /*Adaptive codebook contribution*/ |
1480 | if (SUBMODE(ltp_unquant)) | 1468 | speex_assert (SUBMODE(ltp_unquant)); |
1481 | { | 1469 | { |
1482 | int pit_min, pit_max; | 1470 | int pit_min, pit_max; |
1483 | /* Handle pitch constraints if any */ | 1471 | /* Handle pitch constraints if any */ |
@@ -1542,8 +1530,6 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) | |||
1542 | if (tmp > best_pitch_gain) | 1530 | if (tmp > best_pitch_gain) |
1543 | best_pitch_gain = tmp; | 1531 | best_pitch_gain = tmp; |
1544 | } | 1532 | } |
1545 | } else { | ||
1546 | speex_error("No pitch prediction, what's wrong"); | ||
1547 | } | 1533 | } |
1548 | 1534 | ||
1549 | /* Unquantize the innovation */ | 1535 | /* Unquantize the innovation */ |
@@ -1567,7 +1553,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) | |||
1567 | ener = ol_gain; | 1553 | ener = ol_gain; |
1568 | } | 1554 | } |
1569 | 1555 | ||
1570 | if (SUBMODE(innovation_unquant)) | 1556 | speex_assert (SUBMODE(innovation_unquant)); |
1571 | { | 1557 | { |
1572 | /*Fixed codebook contribution*/ | 1558 | /*Fixed codebook contribution*/ |
1573 | SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, stack, &st->seed); | 1559 | SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, stack, &st->seed); |
@@ -1599,39 +1585,40 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) | |||
1599 | for (i=0;i<st->subframeSize;i++) | 1585 | for (i=0;i<st->subframeSize;i++) |
1600 | innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT)); | 1586 | innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT)); |
1601 | } | 1587 | } |
1602 | } else { | ||
1603 | speex_error("No fixed codebook"); | ||
1604 | } | 1588 | } |
1605 | 1589 | ||
1606 | /*Vocoder mode*/ | 1590 | /*Vocoder mode*/ |
1607 | if (st->submodeID==1) | 1591 | if (st->submodeID==1) |
1608 | { | 1592 | { |
1609 | float g=ol_pitch_coef*GAIN_SCALING_1; | 1593 | spx_word16_t g=ol_pitch_coef; |
1610 | 1594 | g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6))); | |
1595 | if (g<0) | ||
1596 | g=0; | ||
1597 | if (g>GAIN_SCALING) | ||
1598 | g=GAIN_SCALING; | ||
1611 | 1599 | ||
1612 | for (i=0;i<st->subframeSize;i++) | 1600 | for (i=0;i<st->subframeSize;i++) |
1613 | exc[i]=0; | 1601 | exc[i]=0; |
1614 | while (st->voc_offset<st->subframeSize) | 1602 | while (st->voc_offset<st->subframeSize) |
1615 | { | 1603 | { |
1604 | /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain; | ||
1605 | Not quite sure why we need the factor of two in the sqrt */ | ||
1616 | if (st->voc_offset>=0) | 1606 | if (st->voc_offset>=0) |
1617 | exc[st->voc_offset]=sqrt(1.0*ol_pitch); | 1607 | exc[st->voc_offset]=MULT16_16(spx_sqrt(MULT16_16_16(2,ol_pitch)),EXTRACT16(PSHR32(MULT16_16(g,PSHR32(ol_gain,SIG_SHIFT)),6))); |
1618 | st->voc_offset+=ol_pitch; | 1608 | st->voc_offset+=ol_pitch; |
1619 | } | 1609 | } |
1620 | st->voc_offset -= st->subframeSize; | 1610 | st->voc_offset -= st->subframeSize; |
1621 | 1611 | ||
1622 | g=.5+2*(g-.6); | ||
1623 | if (g<0) | ||
1624 | g=0; | ||
1625 | if (g>1) | ||
1626 | g=1; | ||
1627 | for (i=0;i<st->subframeSize;i++) | 1612 | for (i=0;i<st->subframeSize;i++) |
1628 | { | 1613 | { |
1629 | spx_word16_t exci=exc[i]; | 1614 | spx_word16_t exci=exc[i]; |
1630 | /* FIXME: cleanup the innov[i]/SIG_SCALING */ | 1615 | exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)), |
1631 | exc[i]=.8*g*exc[i]*PSHR32(ol_gain,SIG_SHIFT) + .6*g*st->voc_m1*PSHR32(ol_gain,SIG_SHIFT) + (1-.5*g)*PSHR32(innov[i],SIG_SHIFT) - .5*g*PSHR32(st->voc_m2,SIG_SHIFT); | 1616 | SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))), |
1617 | MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT))) | ||
1618 | )); | ||
1632 | st->voc_m1 = exci; | 1619 | st->voc_m1 = exci; |
1633 | st->voc_m2=innov[i]; | 1620 | st->voc_m2=innov[i]; |
1634 | st->voc_mean = .95*st->voc_mean + .05*exc[i]; | 1621 | st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15)); |
1635 | exc[i]-=st->voc_mean; | 1622 | exc[i]-=st->voc_mean; |
1636 | } | 1623 | } |
1637 | } | 1624 | } |