summaryrefslogtreecommitdiff
path: root/apps/codecs/libspeex/nb_celp.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libspeex/nb_celp.c')
-rw-r--r--apps/codecs/libspeex/nb_celp.c171
1 files changed, 9 insertions, 162 deletions
diff --git a/apps/codecs/libspeex/nb_celp.c b/apps/codecs/libspeex/nb_celp.c
index f3810894e1..3de623a482 100644
--- a/apps/codecs/libspeex/nb_celp.c
+++ b/apps/codecs/libspeex/nb_celp.c
@@ -145,9 +145,6 @@ void *nb_encoder_init(const SpeexMode *m)
145 st->bounded_pitch = 1; 145 st->bounded_pitch = 1;
146 146
147 st->encode_submode = 1; 147 st->encode_submode = 1;
148#ifdef EPIC_48K
149 st->lbr_48k=mode->lbr48k;
150#endif
151 148
152#ifdef VORBIS_PSYCHO 149#ifdef VORBIS_PSYCHO
153 st->psy = vorbis_psy_init(8000, 256); 150 st->psy = vorbis_psy_init(8000, 256);
@@ -277,10 +274,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
277 char *stack; 274 char *stack;
278 VARDECL(spx_word16_t *syn_resp); 275 VARDECL(spx_word16_t *syn_resp);
279 VARDECL(spx_word16_t *real_exc); 276 VARDECL(spx_word16_t *real_exc);
280#ifdef EPIC_48K 277
281 int pitch_half[2];
282 int ol_pitch_id=0;
283#endif
284 spx_word32_t ener=0; 278 spx_word32_t ener=0;
285 spx_word16_t fine_gain; 279 spx_word16_t fine_gain;
286 spx_word16_t *in = (spx_word16_t*)vin; 280 spx_word16_t *in = (spx_word16_t*)vin;
@@ -394,19 +388,6 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
394 ol_pitch/=2;*/ 388 ol_pitch/=2;*/
395 /*ol_pitch_coef = sqrt(ol_pitch_coef);*/ 389 /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
396 390
397#ifdef EPIC_48K
398 if (st->lbr_48k)
399 {
400 if (ol_pitch < st->min_pitch+2)
401 ol_pitch = st->min_pitch+2;
402 if (ol_pitch > st->max_pitch-2)
403 ol_pitch = st->max_pitch-2;
404 open_loop_nbest_pitch(st->sw, ol_pitch-2, ol_pitch+2, st->frameSize>>1,
405 &pitch_half[0], nol_pitch_coef, 1, stack);
406 open_loop_nbest_pitch(st->sw+(st->frameSize>>1), pitch_half[0]-1, pitch_half[0]+2, st->frameSize>>1,
407 &pitch_half[1], nol_pitch_coef, 1, stack);
408 }
409#endif
410 } else { 391 } else {
411 ol_pitch=0; 392 ol_pitch=0;
412 ol_pitch_coef=0; 393 ol_pitch_coef=0;
@@ -420,25 +401,6 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
420 fir_mem16(st->exc, interp_lpc, st->exc, st->frameSize, st->lpcSize, st->mem_exc, stack); 401 fir_mem16(st->exc, interp_lpc, st->exc, st->frameSize, st->lpcSize, st->mem_exc, stack);
421 402
422 /* Compute open-loop excitation gain */ 403 /* Compute open-loop excitation gain */
423#ifdef EPIC_48K
424 if (st->lbr_48k)
425 {
426 float ol1=0,ol2=0;
427 float ol_gain2;
428 ol1 = compute_rms16(st->exc, st->frameSize>>1);
429 ol2 = compute_rms16(st->exc+(st->frameSize>>1), st->frameSize>>1);
430 ol1 *= ol1*(st->frameSize>>1);
431 ol2 *= ol2*(st->frameSize>>1);
432
433 ol_gain2=ol1;
434 if (ol2>ol1)
435 ol_gain2=ol2;
436 ol_gain2 = sqrt(2*ol_gain2*(ol1+ol2))*1.3*(1-.5*GAIN_SCALING_1*GAIN_SCALING_1*ol_pitch_coef*ol_pitch_coef);
437
438 ol_gain=SHR32(sqrt(1+ol_gain2/st->frameSize),SIG_SHIFT);
439
440 } else
441#endif
442 { 404 {
443 spx_word16_t g = compute_rms16(st->exc, st->frameSize); 405 spx_word16_t g = compute_rms16(st->exc, st->frameSize);
444 if (st->submodeID!=1 && ol_pitch>0) 406 if (st->submodeID!=1 && ol_pitch>0)
@@ -576,19 +538,12 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
576 538
577 if (st->encode_submode) 539 if (st->encode_submode)
578 { 540 {
579#ifdef EPIC_48K 541 /* First, transmit a zero for narrowband */
580 if (!st->lbr_48k) { 542 speex_bits_pack(bits, 0, 1);
581#endif
582
583 /* First, transmit a zero for narrowband */
584 speex_bits_pack(bits, 0, 1);
585 543
586 /* Transmit the sub-mode we use for this frame */ 544 /* Transmit the sub-mode we use for this frame */
587 speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS); 545 speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
588 546
589#ifdef EPIC_48K
590 }
591#endif
592 } 547 }
593 548
594 /* If null mode (no transmission), just set a couple things to zero*/ 549 /* If null mode (no transmission), just set a couple things to zero*/
@@ -627,35 +582,6 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
627 qlsp[i]=lsp[i]; 582 qlsp[i]=lsp[i];
628#endif 583#endif
629 584
630#ifdef EPIC_48K
631 if (st->lbr_48k) {
632 speex_bits_pack(bits, pitch_half[0]-st->min_pitch, 7);
633 speex_bits_pack(bits, pitch_half[1]-pitch_half[0]+1, 2);
634
635 {
636 int quant = (int)floor(.5+7.4*GAIN_SCALING_1*ol_pitch_coef);
637 if (quant>7)
638 quant=7;
639 if (quant<0)
640 quant=0;
641 ol_pitch_id=quant;
642 speex_bits_pack(bits, quant, 3);
643 ol_pitch_coef=GAIN_SCALING*0.13514*quant;
644
645 }
646 {
647 int qe = (int)(floor(.5+2.1*log(ol_gain*1.0/SIG_SCALING)))-2;
648 if (qe<0)
649 qe=0;
650 if (qe>15)
651 qe=15;
652 ol_gain = exp((qe+2)/2.1)*SIG_SCALING;
653 speex_bits_pack(bits, qe, 4);
654 }
655
656 } else {
657#endif
658
659 /*If we use low bit-rate pitch mode, transmit open-loop pitch*/ 585 /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
660 if (SUBMODE(lbr_pitch)!=-1) 586 if (SUBMODE(lbr_pitch)!=-1)
661 { 587 {
@@ -700,10 +626,6 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
700#endif 626#endif
701 627
702 628
703#ifdef EPIC_48K
704 }
705#endif
706
707 629
708 /* Special case for first frame */ 630 /* Special case for first frame */
709 if (st->first) 631 if (st->first)
@@ -729,15 +651,6 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
729 spx_word16_t *exc; 651 spx_word16_t *exc;
730 int pitch; 652 int pitch;
731 int response_bound = st->subframeSize; 653 int response_bound = st->subframeSize;
732#ifdef EPIC_48K
733 if (st->lbr_48k)
734 {
735 if (sub*2 < st->nbSubframes)
736 ol_pitch = pitch_half[0];
737 else
738 ol_pitch = pitch_half[1];
739 }
740#endif
741 654
742 /* Offset relative to start of frame */ 655 /* Offset relative to start of frame */
743 offset = st->subframeSize*sub; 656 offset = st->subframeSize*sub;
@@ -876,24 +789,11 @@ int nb_encode(void *state, void *vin, SpeexBits *bits)
876 if (st->bounded_pitch && pit_max>offset) 789 if (st->bounded_pitch && pit_max>offset)
877 pit_max=offset; 790 pit_max=offset;
878 791
879#ifdef EPIC_48K
880 if (st->lbr_48k)
881 {
882 pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
883 exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
884 st->lpcSize, st->subframeSize, bits, stack,
885 exc, syn_resp, st->complexity, ol_pitch_id, st->plc_tuning, &st->cumul_gain);
886 } else {
887#endif
888
889 /* Perform pitch search */ 792 /* Perform pitch search */
890 pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2, 793 pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
891 exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef, 794 exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
892 st->lpcSize, st->subframeSize, bits, stack, 795 st->lpcSize, st->subframeSize, bits, stack,
893 exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain); 796 exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
894#ifdef EPIC_48K
895 }
896#endif
897 797
898 st->pitch[sub]=pitch; 798 st->pitch[sub]=pitch;
899 } 799 }
@@ -1054,9 +954,6 @@ void *nb_decoder_init(const SpeexMode *m)
1054 954
1055 955
1056 st->encode_submode = 1; 956 st->encode_submode = 1;
1057#ifdef EPIC_48K
1058 st->lbr_48k=mode->lbr48k;
1059#endif
1060 957
1061 st->first=1; 958 st->first=1;
1062 /* Codec parameters, should eventually have several "modes"*/ 959 /* Codec parameters, should eventually have several "modes"*/
@@ -1222,10 +1119,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1222 VARDECL(spx_coef_t *ak); 1119 VARDECL(spx_coef_t *ak);
1223 VARDECL(spx_lsp_t *qlsp); 1120 VARDECL(spx_lsp_t *qlsp);
1224 spx_word16_t pitch_average=0; 1121 spx_word16_t pitch_average=0;
1225#ifdef EPIC_48K 1122
1226 int pitch_half[2] = {0, 0};
1227 int ol_pitch_id=0;
1228#endif
1229 spx_word16_t *out = (spx_word16_t*)vout; 1123 spx_word16_t *out = (spx_word16_t*)vout;
1230 VARDECL(spx_lsp_t *interp_qlsp); 1124 VARDECL(spx_lsp_t *interp_qlsp);
1231 1125
@@ -1247,9 +1141,6 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1247 1141
1248 if (st->encode_submode) 1142 if (st->encode_submode)
1249 { 1143 {
1250#ifdef EPIC_48K
1251 if (!st->lbr_48k) {
1252#endif
1253 1144
1254 /* Search for next narrowband block (handle requests, skip wideband blocks) */ 1145 /* Search for next narrowband block (handle requests, skip wideband blocks) */
1255 do { 1146 do {
@@ -1322,9 +1213,6 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1322 1213
1323 /* Get the sub-mode that was used */ 1214 /* Get the sub-mode that was used */
1324 st->submodeID = m; 1215 st->submodeID = m;
1325#ifdef EPIC_48K
1326 }
1327#endif
1328 } 1216 }
1329 1217
1330 } 1218 }
@@ -1387,23 +1275,6 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1387 st->old_qlsp[i] = qlsp[i]; 1275 st->old_qlsp[i] = qlsp[i];
1388 } 1276 }
1389 1277
1390#ifdef EPIC_48K
1391 if (st->lbr_48k) {
1392 pitch_half[0] = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);
1393 pitch_half[1] = pitch_half[0]+speex_bits_unpack_unsigned(bits, 2)-1;
1394
1395 ol_pitch_id = speex_bits_unpack_unsigned(bits, 3);
1396 ol_pitch_coef=GAIN_SCALING*0.13514*ol_pitch_id;
1397
1398 {
1399 int qe;
1400 qe = speex_bits_unpack_unsigned(bits, 4);
1401 ol_gain = SIG_SCALING*exp((qe+2)/2.1),SIG_SHIFT;
1402 }
1403
1404 } else {
1405#endif
1406
1407 /* Get open-loop pitch estimation for low bit-rate pitch coding */ 1278 /* Get open-loop pitch estimation for low bit-rate pitch coding */
1408 if (SUBMODE(lbr_pitch)!=-1) 1279 if (SUBMODE(lbr_pitch)!=-1)
1409 { 1280 {
@@ -1428,9 +1299,6 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1428 ol_gain = SIG_SCALING*exp(qe/3.5); 1299 ol_gain = SIG_SCALING*exp(qe/3.5);
1429#endif 1300#endif
1430 } 1301 }
1431#ifdef EPIC_48K
1432 }
1433#endif
1434 1302
1435 ALLOC(ak, st->lpcSize, spx_coef_t); 1303 ALLOC(ak, st->lpcSize, spx_coef_t);
1436 ALLOC(innov, st->subframeSize, spx_sig_t); 1304 ALLOC(innov, st->subframeSize, spx_sig_t);
@@ -1458,16 +1326,6 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1458 spx_word16_t *innov_save = NULL; 1326 spx_word16_t *innov_save = NULL;
1459 spx_word16_t tmp; 1327 spx_word16_t tmp;
1460 1328
1461#ifdef EPIC_48K
1462 if (st->lbr_48k)
1463 {
1464 if (sub*2 < st->nbSubframes)
1465 ol_pitch = pitch_half[0];
1466 else
1467 ol_pitch = pitch_half[1];
1468 }
1469#endif
1470
1471 /* Offset relative to start of frame */ 1329 /* Offset relative to start of frame */
1472 offset = st->subframeSize*sub; 1330 offset = st->subframeSize*sub;
1473 /* Excitation */ 1331 /* Excitation */
@@ -1516,22 +1374,11 @@ int nb_decode(void *state, SpeexBits *bits, void *vout)
1516 } 1374 }
1517 1375
1518 1376
1519#ifdef EPIC_48K
1520 if (st->lbr_48k)
1521 {
1522 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1523 st->subframeSize, &pitch, &pitch_gain[0], bits, stack,
1524 st->count_lost, offset, st->last_pitch_gain, ol_pitch_id);
1525 } else {
1526#endif
1527 1377
1528 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params), 1378 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1529 st->subframeSize, &pitch, &pitch_gain[0], bits, stack, 1379 st->subframeSize, &pitch, &pitch_gain[0], bits, stack,
1530 st->count_lost, offset, st->last_pitch_gain, 0); 1380 st->count_lost, offset, st->last_pitch_gain, 0);
1531 1381
1532#ifdef EPIC_48K
1533 }
1534#endif
1535 /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is 1382 /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is
1536 crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat). 1383 crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat).
1537 We can probably be even more aggressive and limit to 15000 or so. */ 1384 We can probably be even more aggressive and limit to 15000 or so. */