| OLD | NEW |
| 1 /* Copyright (c) 2007-2008 CSIRO | 1 /* Copyright (c) 2007-2008 CSIRO |
| 2 Copyright (c) 2007-2010 Xiph.Org Foundation | 2 Copyright (c) 2007-2010 Xiph.Org Foundation |
| 3 Copyright (c) 2008 Gregory Maxwell | 3 Copyright (c) 2008 Gregory Maxwell |
| 4 Written by Jean-Marc Valin and Gregory Maxwell */ | 4 Written by Jean-Marc Valin and Gregory Maxwell */ |
| 5 /* | 5 /* |
| 6 Redistribution and use in source and binary forms, with or without | 6 Redistribution and use in source and binary forms, with or without |
| 7 modification, are permitted provided that the following conditions | 7 modification, are permitted provided that the following conditions |
| 8 are met: | 8 are met: |
| 9 | 9 |
| 10 - Redistributions of source code must retain the above copyright | 10 - Redistributions of source code must retain the above copyright |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 #include <stdarg.h> | 50 #include <stdarg.h> |
| 51 #include "celt_lpc.h" | 51 #include "celt_lpc.h" |
| 52 #include "vq.h" | 52 #include "vq.h" |
| 53 | 53 |
| 54 | 54 |
| 55 /** Encoder state | 55 /** Encoder state |
| 56 @brief Encoder state | 56 @brief Encoder state |
| 57 */ | 57 */ |
| 58 struct OpusCustomEncoder { | 58 struct OpusCustomEncoder { |
| 59 const OpusCustomMode *mode; /**< Mode used by the encoder */ | 59 const OpusCustomMode *mode; /**< Mode used by the encoder */ |
| 60 int overlap; | |
| 61 int channels; | 60 int channels; |
| 62 int stream_channels; | 61 int stream_channels; |
| 63 | 62 |
| 64 int force_intra; | 63 int force_intra; |
| 65 int clip; | 64 int clip; |
| 66 int disable_pf; | 65 int disable_pf; |
| 67 int complexity; | 66 int complexity; |
| 68 int upsample; | 67 int upsample; |
| 69 int start, end; | 68 int start, end; |
| 70 | 69 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 { | 165 { |
| 167 if (channels < 0 || channels > 2) | 166 if (channels < 0 || channels > 2) |
| 168 return OPUS_BAD_ARG; | 167 return OPUS_BAD_ARG; |
| 169 | 168 |
| 170 if (st==NULL || mode==NULL) | 169 if (st==NULL || mode==NULL) |
| 171 return OPUS_ALLOC_FAIL; | 170 return OPUS_ALLOC_FAIL; |
| 172 | 171 |
| 173 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels)); | 172 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels)); |
| 174 | 173 |
| 175 st->mode = mode; | 174 st->mode = mode; |
| 176 st->overlap = mode->overlap; | |
| 177 st->stream_channels = st->channels = channels; | 175 st->stream_channels = st->channels = channels; |
| 178 | 176 |
| 179 st->upsample = 1; | 177 st->upsample = 1; |
| 180 st->start = 0; | 178 st->start = 0; |
| 181 st->end = st->mode->effEBands; | 179 st->end = st->mode->effEBands; |
| 182 st->signalling = 1; | 180 st->signalling = 1; |
| 183 | 181 |
| 184 st->arch = arch; | 182 st->arch = arch; |
| 185 | 183 |
| 186 st->constrained_vbr = 1; | 184 st->constrained_vbr = 1; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 mem1 = x - SHR32(y,1); | 267 mem1 = x - SHR32(y,1); |
| 270 #else | 268 #else |
| 271 mem0 = mem1 + y - 2*x; | 269 mem0 = mem1 + y - 2*x; |
| 272 mem1 = x - .5f*y; | 270 mem1 = x - .5f*y; |
| 273 #endif | 271 #endif |
| 274 tmp[i] = EXTRACT16(SHR32(y,2)); | 272 tmp[i] = EXTRACT16(SHR32(y,2)); |
| 275 /*printf("%f ", tmp[i]);*/ | 273 /*printf("%f ", tmp[i]);*/ |
| 276 } | 274 } |
| 277 /*printf("\n");*/ | 275 /*printf("\n");*/ |
| 278 /* First few samples are bad because we don't propagate the memory */ | 276 /* First few samples are bad because we don't propagate the memory */ |
| 279 for (i=0;i<12;i++) | 277 OPUS_CLEAR(tmp, 12); |
| 280 tmp[i] = 0; | |
| 281 | 278 |
| 282 #ifdef FIXED_POINT | 279 #ifdef FIXED_POINT |
| 283 /* Normalize tmp to max range */ | 280 /* Normalize tmp to max range */ |
| 284 { | 281 { |
| 285 int shift=0; | 282 int shift=0; |
| 286 shift = 14-celt_ilog2(1+celt_maxabs16(tmp, len)); | 283 shift = 14-celt_ilog2(1+celt_maxabs16(tmp, len)); |
| 287 if (shift!=0) | 284 if (shift!=0) |
| 288 { | 285 { |
| 289 for (i=0;i<len;i++) | 286 for (i=0;i<len;i++) |
| 290 tmp[i] = SHL16(tmp[i], shift); | 287 tmp[i] = SHL16(tmp[i], shift); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 { | 356 { |
| 360 *tf_chan = c; | 357 *tf_chan = c; |
| 361 mask_metric = unmask; | 358 mask_metric = unmask; |
| 362 } | 359 } |
| 363 } | 360 } |
| 364 is_transient = mask_metric>200; | 361 is_transient = mask_metric>200; |
| 365 | 362 |
| 366 /* Arbitrary metric for VBR boost */ | 363 /* Arbitrary metric for VBR boost */ |
| 367 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42); | 364 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42); |
| 368 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */ | 365 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */ |
| 369 *tf_estimate = celt_sqrt(MAX16(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(1
63,tf_max)),14)-QCONST32(0.139,28))); | 366 *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(1
63,tf_max)),14)-QCONST32(0.139,28))); |
| 370 /*printf("%d %f\n", tf_max, mask_metric);*/ | 367 /*printf("%d %f\n", tf_max, mask_metric);*/ |
| 371 RESTORE_STACK; | 368 RESTORE_STACK; |
| 372 #ifdef FUZZING | 369 #ifdef FUZZING |
| 373 is_transient = rand()&0x1; | 370 is_transient = rand()&0x1; |
| 374 #endif | 371 #endif |
| 375 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/ | 372 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/ |
| 376 return is_transient; | 373 return is_transient; |
| 377 } | 374 } |
| 378 | 375 |
| 379 /* Looks for sudden increases of energy to decide whether we need to patch | 376 /* Looks for sudden increases of energy to decide whether we need to patch |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 mean_diff = DIV32(mean_diff, C*(end-3)); | 409 mean_diff = DIV32(mean_diff, C*(end-3)); |
| 413 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/ | 410 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/ |
| 414 return mean_diff > QCONST16(1.f, DB_SHIFT); | 411 return mean_diff > QCONST16(1.f, DB_SHIFT); |
| 415 } | 412 } |
| 416 | 413 |
| 417 /** Apply window and compute the MDCT for all sub-frames and | 414 /** Apply window and compute the MDCT for all sub-frames and |
| 418 all channels in a frame */ | 415 all channels in a frame */ |
| 419 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS
_RESTRICT in, | 416 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS
_RESTRICT in, |
| 420 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, i
nt upsample) | 417 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, i
nt upsample) |
| 421 { | 418 { |
| 422 const int overlap = OVERLAP(mode); | 419 const int overlap = mode->overlap; |
| 423 int N; | 420 int N; |
| 424 int B; | 421 int B; |
| 425 int shift; | 422 int shift; |
| 426 int i, b, c; | 423 int i, b, c; |
| 427 if (shortBlocks) | 424 if (shortBlocks) |
| 428 { | 425 { |
| 429 B = shortBlocks; | 426 B = shortBlocks; |
| 430 N = mode->shortMdctSize; | 427 N = mode->shortMdctSize; |
| 431 shift = mode->maxLM; | 428 shift = mode->maxLM; |
| 432 } else { | 429 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 446 for (i=0;i<B*N;i++) | 443 for (i=0;i<B*N;i++) |
| 447 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i])); | 444 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i])); |
| 448 } | 445 } |
| 449 if (upsample != 1) | 446 if (upsample != 1) |
| 450 { | 447 { |
| 451 c=0; do | 448 c=0; do |
| 452 { | 449 { |
| 453 int bound = B*N/upsample; | 450 int bound = B*N/upsample; |
| 454 for (i=0;i<bound;i++) | 451 for (i=0;i<bound;i++) |
| 455 out[c*B*N+i] *= upsample; | 452 out[c*B*N+i] *= upsample; |
| 456 for (;i<B*N;i++) | 453 OPUS_CLEAR(&out[c*B*N+bound], B*N-bound); |
| 457 out[c*B*N+i] = 0; | |
| 458 } while (++c<C); | 454 } while (++c<C); |
| 459 } | 455 } |
| 460 } | 456 } |
| 461 | 457 |
| 462 | 458 |
| 463 void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RES
TRICT inp, | 459 void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RES
TRICT inp, |
| 464 int N, int CC, int upsample, const opus_val16 *coef, cel
t_sig *mem, int clip) | 460 int N, int CC, int upsample, const opus_val16 *coef, cel
t_sig *mem, int clip) |
| 465 { | 461 { |
| 466 int i; | 462 int i; |
| 467 opus_val16 coef0; | 463 opus_val16 coef0; |
| 468 celt_sig m; | 464 celt_sig m; |
| 469 int Nu; | 465 int Nu; |
| 470 | 466 |
| 471 coef0 = coef[0]; | 467 coef0 = coef[0]; |
| 468 m = *mem; |
| 472 | 469 |
| 470 /* Fast path for the normal 48kHz case and no clipping */ |
| 471 if (coef[1] == 0 && upsample == 1 && !clip) |
| 472 { |
| 473 for (i=0;i<N;i++) |
| 474 { |
| 475 opus_val16 x; |
| 476 x = SCALEIN(pcmp[CC*i]); |
| 477 /* Apply pre-emphasis */ |
| 478 inp[i] = SHL32(x, SIG_SHIFT) - m; |
| 479 m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT); |
| 480 } |
| 481 *mem = m; |
| 482 return; |
| 483 } |
| 473 | 484 |
| 474 Nu = N/upsample; | 485 Nu = N/upsample; |
| 475 if (upsample!=1) | 486 if (upsample!=1) |
| 476 { | 487 { |
| 477 for (i=0;i<N;i++) | 488 OPUS_CLEAR(inp, N); |
| 478 inp[i] = 0; | |
| 479 } | 489 } |
| 480 for (i=0;i<Nu;i++) | 490 for (i=0;i<Nu;i++) |
| 481 { | 491 inp[i*upsample] = SCALEIN(pcmp[CC*i]); |
| 482 celt_sig x; | |
| 483 | |
| 484 x = SCALEIN(pcmp[CC*i]); | |
| 485 #ifndef FIXED_POINT | |
| 486 /* Replace NaNs with zeros */ | |
| 487 if (!(x==x)) | |
| 488 x = 0; | |
| 489 #endif | |
| 490 inp[i*upsample] = x; | |
| 491 } | |
| 492 | 492 |
| 493 #ifndef FIXED_POINT | 493 #ifndef FIXED_POINT |
| 494 if (clip) | 494 if (clip) |
| 495 { | 495 { |
| 496 /* Clip input to avoid encoding non-portable files */ | 496 /* Clip input to avoid encoding non-portable files */ |
| 497 for (i=0;i<Nu;i++) | 497 for (i=0;i<Nu;i++) |
| 498 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample])); | 498 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample])); |
| 499 } | 499 } |
| 500 #else | 500 #else |
| 501 (void)clip; /* Avoids a warning about clip being unused. */ | 501 (void)clip; /* Avoids a warning about clip being unused. */ |
| 502 #endif | 502 #endif |
| 503 m = *mem; | |
| 504 #ifdef CUSTOM_MODES | 503 #ifdef CUSTOM_MODES |
| 505 if (coef[1] != 0) | 504 if (coef[1] != 0) |
| 506 { | 505 { |
| 507 opus_val16 coef1 = coef[1]; | 506 opus_val16 coef1 = coef[1]; |
| 508 opus_val16 coef2 = coef[2]; | 507 opus_val16 coef2 = coef[2]; |
| 509 for (i=0;i<N;i++) | 508 for (i=0;i<N;i++) |
| 510 { | 509 { |
| 511 celt_sig x, tmp; | 510 celt_sig x, tmp; |
| 512 x = inp[i]; | 511 x = inp[i]; |
| 513 /* Apply pre-emphasis */ | 512 /* Apply pre-emphasis */ |
| 514 tmp = MULT16_16(coef2, x); | 513 tmp = MULT16_16(coef2, x); |
| 515 inp[i] = tmp + m; | 514 inp[i] = tmp + m; |
| 516 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp); | 515 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp); |
| 517 } | 516 } |
| 518 } else | 517 } else |
| 519 #endif | 518 #endif |
| 520 { | 519 { |
| 521 for (i=0;i<N;i++) | 520 for (i=0;i<N;i++) |
| 522 { | 521 { |
| 523 celt_sig x; | 522 opus_val16 x; |
| 524 x = SHL32(inp[i], SIG_SHIFT); | 523 x = inp[i]; |
| 525 /* Apply pre-emphasis */ | 524 /* Apply pre-emphasis */ |
| 526 inp[i] = x + m; | 525 inp[i] = SHL32(x, SIG_SHIFT) - m; |
| 527 m = - MULT16_32_Q15(coef0, x); | 526 m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT); |
| 528 } | 527 } |
| 529 } | 528 } |
| 530 *mem = m; | 529 *mem = m; |
| 531 } | 530 } |
| 532 | 531 |
| 533 | 532 |
| 534 | 533 |
| 535 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias
) | 534 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias
) |
| 536 { | 535 { |
| 537 int i; | 536 int i; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 568 | 567 |
| 569 ALLOC(metric, len, int); | 568 ALLOC(metric, len, int); |
| 570 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); | 569 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); |
| 571 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); | 570 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); |
| 572 ALLOC(path0, len, int); | 571 ALLOC(path0, len, int); |
| 573 ALLOC(path1, len, int); | 572 ALLOC(path1, len, int); |
| 574 | 573 |
| 575 *tf_sum = 0; | 574 *tf_sum = 0; |
| 576 for (i=0;i<len;i++) | 575 for (i=0;i<len;i++) |
| 577 { | 576 { |
| 578 int j, k, N; | 577 int k, N; |
| 579 int narrow; | 578 int narrow; |
| 580 opus_val32 L1, best_L1; | 579 opus_val32 L1, best_L1; |
| 581 int best_level=0; | 580 int best_level=0; |
| 582 N = (m->eBands[i+1]-m->eBands[i])<<LM; | 581 N = (m->eBands[i+1]-m->eBands[i])<<LM; |
| 583 /* band is too narrow to be split down to LM=-1 */ | 582 /* band is too narrow to be split down to LM=-1 */ |
| 584 narrow = (m->eBands[i+1]-m->eBands[i])==1; | 583 narrow = (m->eBands[i+1]-m->eBands[i])==1; |
| 585 for (j=0;j<N;j++) | 584 OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N); |
| 586 tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)]; | |
| 587 /* Just add the right channel if we're in stereo */ | 585 /* Just add the right channel if we're in stereo */ |
| 588 /*if (C==2) | 586 /*if (C==2) |
| 589 for (j=0;j<N;j++) | 587 for (j=0;j<N;j++) |
| 590 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1)
);*/ | 588 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1)
);*/ |
| 591 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias); | 589 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias); |
| 592 best_L1 = L1; | 590 best_L1 = L1; |
| 593 /* Check the -1 case for transients */ | 591 /* Check the -1 case for transients */ |
| 594 if (isTransient && !narrow) | 592 if (isTransient && !narrow) |
| 595 { | 593 { |
| 596 for (j=0;j<N;j++) | 594 OPUS_COPY(tmp_1, tmp, N); |
| 597 tmp_1[j] = tmp[j]; | |
| 598 haar1(tmp_1, N>>LM, 1<<LM); | 595 haar1(tmp_1, N>>LM, 1<<LM); |
| 599 L1 = l1_metric(tmp_1, N, LM+1, bias); | 596 L1 = l1_metric(tmp_1, N, LM+1, bias); |
| 600 if (L1<best_L1) | 597 if (L1<best_L1) |
| 601 { | 598 { |
| 602 best_L1 = L1; | 599 best_L1 = L1; |
| 603 best_level = -1; | 600 best_level = -1; |
| 604 } | 601 } |
| 605 } | 602 } |
| 606 /*printf ("%f ", L1);*/ | 603 /*printf ("%f ", L1);*/ |
| 607 for (k=0;k<LM+!(isTransient||narrow);k++) | 604 for (k=0;k<LM+!(isTransient||narrow);k++) |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 tf_select = 0; | 744 tf_select = 0; |
| 748 for (i=start;i<end;i++) | 745 for (i=start;i<end;i++) |
| 749 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; | 746 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
| 750 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);pri
ntf("\n");*/ | 747 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);pri
ntf("\n");*/ |
| 751 } | 748 } |
| 752 | 749 |
| 753 | 750 |
| 754 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, | 751 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, |
| 755 const opus_val16 *bandLogE, int end, int LM, int C, int N0, | 752 const opus_val16 *bandLogE, int end, int LM, int C, int N0, |
| 756 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate, | 753 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate, |
| 757 int intensity, opus_val16 surround_trim) | 754 int intensity, opus_val16 surround_trim, int arch) |
| 758 { | 755 { |
| 759 int i; | 756 int i; |
| 760 opus_val32 diff=0; | 757 opus_val32 diff=0; |
| 761 int c; | 758 int c; |
| 762 int trim_index = 5; | 759 int trim_index; |
| 763 opus_val16 trim = QCONST16(5.f, 8); | 760 opus_val16 trim = QCONST16(5.f, 8); |
| 764 opus_val16 logXC, logXC2; | 761 opus_val16 logXC, logXC2; |
| 765 if (C==2) | 762 if (C==2) |
| 766 { | 763 { |
| 767 opus_val16 sum = 0; /* Q10 */ | 764 opus_val16 sum = 0; /* Q10 */ |
| 768 opus_val16 minXC; /* Q10 */ | 765 opus_val16 minXC; /* Q10 */ |
| 769 /* Compute inter-channel correlation for low frequencies */ | 766 /* Compute inter-channel correlation for low frequencies */ |
| 770 for (i=0;i<8;i++) | 767 for (i=0;i<8;i++) |
| 771 { | 768 { |
| 772 int j; | 769 opus_val32 partial; |
| 773 opus_val32 partial = 0; | 770 partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM
)], |
| 774 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) | 771 (m->eBands[i+1]-m->eBands[i])<<LM, arch); |
| 775 partial = MAC16_16(partial, X[j], X[N0+j]); | |
| 776 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); | 772 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); |
| 777 } | 773 } |
| 778 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); | 774 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); |
| 779 sum = MIN16(QCONST16(1.f, 10), ABS16(sum)); | 775 sum = MIN16(QCONST16(1.f, 10), ABS16(sum)); |
| 780 minXC = sum; | 776 minXC = sum; |
| 781 for (i=8;i<intensity;i++) | 777 for (i=8;i<intensity;i++) |
| 782 { | 778 { |
| 783 int j; | 779 opus_val32 partial; |
| 784 opus_val32 partial = 0; | 780 partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM
)], |
| 785 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) | 781 (m->eBands[i+1]-m->eBands[i])<<LM, arch); |
| 786 partial = MAC16_16(partial, X[j], X[N0+j]); | |
| 787 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18)))); | 782 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18)))); |
| 788 } | 783 } |
| 789 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC)); | 784 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC)); |
| 790 /*printf ("%f\n", sum);*/ | 785 /*printf ("%f\n", sum);*/ |
| 791 if (sum > QCONST16(.995f,10)) | |
| 792 trim_index-=4; | |
| 793 else if (sum > QCONST16(.92f,10)) | |
| 794 trim_index-=3; | |
| 795 else if (sum > QCONST16(.85f,10)) | |
| 796 trim_index-=2; | |
| 797 else if (sum > QCONST16(.8f,10)) | |
| 798 trim_index-=1; | |
| 799 /* mid-side savings estimations based on the LF average*/ | 786 /* mid-side savings estimations based on the LF average*/ |
| 800 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum)); | 787 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum)); |
| 801 /* mid-side savings estimations based on min correlation */ | 788 /* mid-side savings estimations based on min correlation */ |
| 802 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(min
XC, minXC))); | 789 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(min
XC, minXC))); |
| 803 #ifdef FIXED_POINT | 790 #ifdef FIXED_POINT |
| 804 /* Compensate for Q20 vs Q14 input and convert output to Q8 */ | 791 /* Compensate for Q20 vs Q14 input and convert output to Q8 */ |
| 805 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); | 792 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); |
| 806 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); | 793 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); |
| 807 #endif | 794 #endif |
| 808 | 795 |
| 809 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC)); | 796 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC)); |
| 810 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2
)); | 797 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2
)); |
| 811 } | 798 } |
| 812 | 799 |
| 813 /* Estimate spectral tilt */ | 800 /* Estimate spectral tilt */ |
| 814 c=0; do { | 801 c=0; do { |
| 815 for (i=0;i<end-1;i++) | 802 for (i=0;i<end-1;i++) |
| 816 { | 803 { |
| 817 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end); | 804 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end); |
| 818 } | 805 } |
| 819 } while (++c<C); | 806 } while (++c<C); |
| 820 diff /= C*(end-1); | 807 diff /= C*(end-1); |
| 821 /*printf("%f\n", diff);*/ | 808 /*printf("%f\n", diff);*/ |
| 822 if (diff > QCONST16(2.f, DB_SHIFT)) | |
| 823 trim_index--; | |
| 824 if (diff > QCONST16(8.f, DB_SHIFT)) | |
| 825 trim_index--; | |
| 826 if (diff < -QCONST16(4.f, DB_SHIFT)) | |
| 827 trim_index++; | |
| 828 if (diff < -QCONST16(10.f, DB_SHIFT)) | |
| 829 trim_index++; | |
| 830 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(
1.f, DB_SHIFT),DB_SHIFT-8)/6 )); | 809 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(
1.f, DB_SHIFT),DB_SHIFT-8)/6 )); |
| 831 trim -= SHR16(surround_trim, DB_SHIFT-8); | 810 trim -= SHR16(surround_trim, DB_SHIFT-8); |
| 832 trim -= 2*SHR16(tf_estimate, 14-8); | 811 trim -= 2*SHR16(tf_estimate, 14-8); |
| 833 #ifndef DISABLE_FLOAT_API | 812 #ifndef DISABLE_FLOAT_API |
| 834 if (analysis->valid) | 813 if (analysis->valid) |
| 835 { | 814 { |
| 836 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), | 815 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), |
| 837 (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f)))); | 816 (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f)))); |
| 838 } | 817 } |
| 818 #else |
| 819 (void)analysis; |
| 839 #endif | 820 #endif |
| 840 | 821 |
| 841 #ifdef FIXED_POINT | 822 #ifdef FIXED_POINT |
| 842 trim_index = PSHR32(trim, 8); | 823 trim_index = PSHR32(trim, 8); |
| 843 #else | 824 #else |
| 844 trim_index = (int)floor(.5f+trim); | 825 trim_index = (int)floor(.5f+trim); |
| 845 #endif | 826 #endif |
| 846 if (trim_index<0) | 827 trim_index = IMAX(0, IMIN(10, trim_index)); |
| 847 trim_index = 0; | |
| 848 if (trim_index>10) | |
| 849 trim_index = 10; | |
| 850 /*printf("%d\n", trim_index);*/ | 828 /*printf("%d\n", trim_index);*/ |
| 851 #ifdef FUZZING | 829 #ifdef FUZZING |
| 852 trim_index = rand()%11; | 830 trim_index = rand()%11; |
| 853 #endif | 831 #endif |
| 854 return trim_index; | 832 return trim_index; |
| 855 } | 833 } |
| 856 | 834 |
| 857 static int stereo_analysis(const CELTMode *m, const celt_norm *X, | 835 static int stereo_analysis(const CELTMode *m, const celt_norm *X, |
| 858 int LM, int N0) | 836 int LM, int N0) |
| 859 { | 837 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 879 } | 857 } |
| 880 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); | 858 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); |
| 881 thetas = 13; | 859 thetas = 13; |
| 882 /* We don't need thetas for lower bands with LM<=1 */ | 860 /* We don't need thetas for lower bands with LM<=1 */ |
| 883 if (LM<=1) | 861 if (LM<=1) |
| 884 thetas -= 8; | 862 thetas -= 8; |
| 885 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) | 863 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) |
| 886 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); | 864 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); |
| 887 } | 865 } |
| 888 | 866 |
| 867 #define MSWAP(a,b) do {opus_val16 tmp = a;a=b;b=tmp;} while(0) |
| 868 static opus_val16 median_of_5(const opus_val16 *x) |
| 869 { |
| 870 opus_val16 t0, t1, t2, t3, t4; |
| 871 t2 = x[2]; |
| 872 if (x[0] > x[1]) |
| 873 { |
| 874 t0 = x[1]; |
| 875 t1 = x[0]; |
| 876 } else { |
| 877 t0 = x[0]; |
| 878 t1 = x[1]; |
| 879 } |
| 880 if (x[3] > x[4]) |
| 881 { |
| 882 t3 = x[4]; |
| 883 t4 = x[3]; |
| 884 } else { |
| 885 t3 = x[3]; |
| 886 t4 = x[4]; |
| 887 } |
| 888 if (t0 > t3) |
| 889 { |
| 890 MSWAP(t0, t3); |
| 891 MSWAP(t1, t4); |
| 892 } |
| 893 if (t2 > t1) |
| 894 { |
| 895 if (t1 < t3) |
| 896 return MIN16(t2, t3); |
| 897 else |
| 898 return MIN16(t4, t1); |
| 899 } else { |
| 900 if (t2 < t3) |
| 901 return MIN16(t1, t3); |
| 902 else |
| 903 return MIN16(t2, t4); |
| 904 } |
| 905 } |
| 906 |
| 907 static opus_val16 median_of_3(const opus_val16 *x) |
| 908 { |
| 909 opus_val16 t0, t1, t2; |
| 910 if (x[0] > x[1]) |
| 911 { |
| 912 t0 = x[1]; |
| 913 t1 = x[0]; |
| 914 } else { |
| 915 t0 = x[0]; |
| 916 t1 = x[1]; |
| 917 } |
| 918 t2 = x[2]; |
| 919 if (t1 < t2) |
| 920 return t1; |
| 921 else if (t0 < t2) |
| 922 return t2; |
| 923 else |
| 924 return t0; |
| 925 } |
| 926 |
| 889 static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16
*bandLogE2, | 927 static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16
*bandLogE2, |
| 890 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, cons
t opus_int16 *logN, | 928 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, cons
t opus_int16 *logN, |
| 891 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, i
nt LM, | 929 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, i
nt LM, |
| 892 int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_
dynalloc) | 930 int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_
dynalloc) |
| 893 { | 931 { |
| 894 int i, c; | 932 int i, c; |
| 895 opus_int32 tot_boost=0; | 933 opus_int32 tot_boost=0; |
| 896 opus_val16 maxDepth; | 934 opus_val16 maxDepth; |
| 897 VARDECL(opus_val16, follower); | 935 VARDECL(opus_val16, follower); |
| 898 VARDECL(opus_val16, noise_floor); | 936 VARDECL(opus_val16, noise_floor); |
| 899 SAVE_STACK; | 937 SAVE_STACK; |
| 900 ALLOC(follower, C*nbEBands, opus_val16); | 938 ALLOC(follower, C*nbEBands, opus_val16); |
| 901 ALLOC(noise_floor, C*nbEBands, opus_val16); | 939 ALLOC(noise_floor, C*nbEBands, opus_val16); |
| 902 for (i=0;i<nbEBands;i++) | 940 OPUS_CLEAR(offsets, nbEBands); |
| 903 offsets[i] = 0; | |
| 904 /* Dynamic allocation code */ | 941 /* Dynamic allocation code */ |
| 905 maxDepth=-QCONST16(31.9f, DB_SHIFT); | 942 maxDepth=-QCONST16(31.9f, DB_SHIFT); |
| 906 for (i=0;i<end;i++) | 943 for (i=0;i<end;i++) |
| 907 { | 944 { |
| 908 /* Noise floor must take into account eMeans, the depth, the width of the
bands | 945 /* Noise floor must take into account eMeans, the depth, the width of the
bands |
| 909 and the preemphasis filter (approx. square of bark band ID) */ | 946 and the preemphasis filter (approx. square of bark band ID) */ |
| 910 noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i]) | 947 noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i]) |
| 911 +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],
6) | 948 +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],
6) |
| 912 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5)); | 949 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5)); |
| 913 } | 950 } |
| 914 c=0;do | 951 c=0;do |
| 915 { | 952 { |
| 916 for (i=0;i<end;i++) | 953 for (i=0;i<end;i++) |
| 917 maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]); | 954 maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]); |
| 918 } while (++c<C); | 955 } while (++c<C); |
| 919 /* Make sure that dynamic allocation can't make us bust the budget */ | 956 /* Make sure that dynamic allocation can't make us bust the budget */ |
| 920 if (effectiveBytes > 50 && LM>=1 && !lfe) | 957 if (effectiveBytes > 50 && LM>=1 && !lfe) |
| 921 { | 958 { |
| 922 int last=0; | 959 int last=0; |
| 923 c=0;do | 960 c=0;do |
| 924 { | 961 { |
| 925 follower[c*nbEBands] = bandLogE2[c*nbEBands]; | 962 opus_val16 offset; |
| 963 opus_val16 tmp; |
| 964 opus_val16 *f; |
| 965 f = &follower[c*nbEBands]; |
| 966 f[0] = bandLogE2[c*nbEBands]; |
| 926 for (i=1;i<end;i++) | 967 for (i=1;i<end;i++) |
| 927 { | 968 { |
| 928 /* The last band to be at least 3 dB higher than the previous one | 969 /* The last band to be at least 3 dB higher than the previous one |
| 929 is the last we'll consider. Otherwise, we run into problems on | 970 is the last we'll consider. Otherwise, we run into problems on |
| 930 bandlimited signals. */ | 971 bandlimited signals. */ |
| 931 if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f
,DB_SHIFT)) | 972 if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f
,DB_SHIFT)) |
| 932 last=i; | 973 last=i; |
| 933 follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i-1]+QCONST16(1.5
f,DB_SHIFT), bandLogE2[c*nbEBands+i]); | 974 f[i] = MIN16(f[i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]
); |
| 934 } | 975 } |
| 935 for (i=last-1;i>=0;i--) | 976 for (i=last-1;i>=0;i--) |
| 936 follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i], MIN16(followe
r[c*nbEBands+i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*nbEBands+i])); | 977 f[i] = MIN16(f[i], MIN16(f[i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*
nbEBands+i])); |
| 978 |
| 979 /* Combine with a median filter to avoid dynalloc triggering unnecessar
ily. |
| 980 The "offset" value controls how conservative we are -- a higher offs
et |
| 981 reduces the impact of the median filter and makes dynalloc use more
bits. */ |
| 982 offset = QCONST16(1.f, DB_SHIFT); |
| 983 for (i=2;i<end-2;i++) |
| 984 f[i] = MAX16(f[i], median_of_5(&bandLogE2[c*nbEBands+i-2])-offset); |
| 985 tmp = median_of_3(&bandLogE2[c*nbEBands])-offset; |
| 986 f[0] = MAX16(f[0], tmp); |
| 987 f[1] = MAX16(f[1], tmp); |
| 988 tmp = median_of_3(&bandLogE2[c*nbEBands+end-3])-offset; |
| 989 f[end-2] = MAX16(f[end-2], tmp); |
| 990 f[end-1] = MAX16(f[end-1], tmp); |
| 991 |
| 937 for (i=0;i<end;i++) | 992 for (i=0;i<end;i++) |
| 938 follower[c*nbEBands+i] = MAX16(follower[c*nbEBands+i], noise_floor[i
]); | 993 f[i] = MAX16(f[i], noise_floor[i]); |
| 939 } while (++c<C); | 994 } while (++c<C); |
| 940 if (C==2) | 995 if (C==2) |
| 941 { | 996 { |
| 942 for (i=start;i<end;i++) | 997 for (i=start;i<end;i++) |
| 943 { | 998 { |
| 944 /* Consider 24 dB "cross-talk" */ | 999 /* Consider 24 dB "cross-talk" */ |
| 945 follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[
i]-QCONST16(4.f,DB_SHIFT)); | 1000 follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[
i]-QCONST16(4.f,DB_SHIFT)); |
| 946 follower[ i] = MAX16(follower[ i], follower[nbEBands
+i]-QCONST16(4.f,DB_SHIFT)); | 1001 follower[ i] = MAX16(follower[ i], follower[nbEBands
+i]-QCONST16(4.f,DB_SHIFT)); |
| 947 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, ba
ndLogE[nbEBands+i]-follower[nbEBands+i])); | 1002 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, ba
ndLogE[nbEBands+i]-follower[nbEBands+i])); |
| 948 } | 1003 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1009 { | 1064 { |
| 1010 int c; | 1065 int c; |
| 1011 VARDECL(celt_sig, _pre); | 1066 VARDECL(celt_sig, _pre); |
| 1012 celt_sig *pre[2]; | 1067 celt_sig *pre[2]; |
| 1013 const CELTMode *mode; | 1068 const CELTMode *mode; |
| 1014 int pitch_index; | 1069 int pitch_index; |
| 1015 opus_val16 gain1; | 1070 opus_val16 gain1; |
| 1016 opus_val16 pf_threshold; | 1071 opus_val16 pf_threshold; |
| 1017 int pf_on; | 1072 int pf_on; |
| 1018 int qg; | 1073 int qg; |
| 1074 int overlap; |
| 1019 SAVE_STACK; | 1075 SAVE_STACK; |
| 1020 | 1076 |
| 1021 mode = st->mode; | 1077 mode = st->mode; |
| 1078 overlap = mode->overlap; |
| 1022 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig); | 1079 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig); |
| 1023 | 1080 |
| 1024 pre[0] = _pre; | 1081 pre[0] = _pre; |
| 1025 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); | 1082 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); |
| 1026 | 1083 |
| 1027 | 1084 |
| 1028 c=0; do { | 1085 c=0; do { |
| 1029 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERI
OD); | 1086 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERI
OD); |
| 1030 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N
); | 1087 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+overlap)+overlap, N); |
| 1031 } while (++c<CC); | 1088 } while (++c<CC); |
| 1032 | 1089 |
| 1033 if (enabled) | 1090 if (enabled) |
| 1034 { | 1091 { |
| 1035 VARDECL(opus_val16, pitch_buf); | 1092 VARDECL(opus_val16, pitch_buf); |
| 1036 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); | 1093 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); |
| 1037 | 1094 |
| 1038 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch); | 1095 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch); |
| 1039 /* Don't search for the fir last 1.5 octave of the range because | 1096 /* Don't search for the fir last 1.5 octave of the range because |
| 1040 there's too many false-positives due to short-term correlation */ | 1097 there's too many false-positives due to short-term correlation */ |
| 1041 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, | 1098 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, |
| 1042 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index, | 1099 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index, |
| 1043 st->arch); | 1100 st->arch); |
| 1044 pitch_index = COMBFILTER_MAXPERIOD-pitch_index; | 1101 pitch_index = COMBFILTER_MAXPERIOD-pitch_index; |
| 1045 | 1102 |
| 1046 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPER
IOD, | 1103 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPER
IOD, |
| 1047 N, &pitch_index, st->prefilter_period, st->prefilter_gain); | 1104 N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch)
; |
| 1048 if (pitch_index > COMBFILTER_MAXPERIOD-2) | 1105 if (pitch_index > COMBFILTER_MAXPERIOD-2) |
| 1049 pitch_index = COMBFILTER_MAXPERIOD-2; | 1106 pitch_index = COMBFILTER_MAXPERIOD-2; |
| 1050 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); | 1107 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); |
| 1051 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.t
onality);*/ | 1108 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.t
onality);*/ |
| 1052 if (st->loss_rate>2) | 1109 if (st->loss_rate>2) |
| 1053 gain1 = HALF32(gain1); | 1110 gain1 = HALF32(gain1); |
| 1054 if (st->loss_rate>4) | 1111 if (st->loss_rate>4) |
| 1055 gain1 = HALF32(gain1); | 1112 gain1 = HALF32(gain1); |
| 1056 if (st->loss_rate>8) | 1113 if (st->loss_rate>8) |
| 1057 gain1 = 0; | 1114 gain1 = 0; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1093 #else | 1150 #else |
| 1094 qg = (int)floor(.5f+gain1*32/3)-1; | 1151 qg = (int)floor(.5f+gain1*32/3)-1; |
| 1095 #endif | 1152 #endif |
| 1096 qg = IMAX(0, IMIN(7, qg)); | 1153 qg = IMAX(0, IMIN(7, qg)); |
| 1097 gain1 = QCONST16(0.09375f,15)*(qg+1); | 1154 gain1 = QCONST16(0.09375f,15)*(qg+1); |
| 1098 pf_on = 1; | 1155 pf_on = 1; |
| 1099 } | 1156 } |
| 1100 /*printf("%d %f\n", pitch_index, gain1);*/ | 1157 /*printf("%d %f\n", pitch_index, gain1);*/ |
| 1101 | 1158 |
| 1102 c=0; do { | 1159 c=0; do { |
| 1103 int offset = mode->shortMdctSize-st->overlap; | 1160 int offset = mode->shortMdctSize-overlap; |
| 1104 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); | 1161 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
| 1105 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap); | 1162 OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap); |
| 1106 if (offset) | 1163 if (offset) |
| 1107 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERI
OD, | 1164 comb_filter(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD, |
| 1108 st->prefilter_period, st->prefilter_period, offset, -st->prefilte
r_gain, -st->prefilter_gain, | 1165 st->prefilter_period, st->prefilter_period, offset, -st->prefilte
r_gain, -st->prefilter_gain, |
| 1109 st->prefilter_tapset, st->prefilter_tapset, NULL, 0); | 1166 st->prefilter_tapset, st->prefilter_tapset, NULL, 0); |
| 1110 | 1167 |
| 1111 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAX
PERIOD+offset, | 1168 comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+o
ffset, |
| 1112 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -g
ain1, | 1169 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -g
ain1, |
| 1113 st->prefilter_tapset, prefilter_tapset, mode->window, st->overlap); | 1170 st->prefilter_tapset, prefilter_tapset, mode->window, overlap); |
| 1114 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap)
; | 1171 OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap); |
| 1115 | 1172 |
| 1116 if (N>COMBFILTER_MAXPERIOD) | 1173 if (N>COMBFILTER_MAXPERIOD) |
| 1117 { | 1174 { |
| 1118 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MA
XPERIOD); | 1175 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MA
XPERIOD); |
| 1119 } else { | 1176 } else { |
| 1120 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFIL
TER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); | 1177 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFIL
TER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); |
| 1121 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N,
pre[c]+COMBFILTER_MAXPERIOD, N); | 1178 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N,
pre[c]+COMBFILTER_MAXPERIOD, N); |
| 1122 } | 1179 } |
| 1123 } while (++c<CC); | 1180 } while (++c<CC); |
| 1124 | 1181 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1189 float tonal; | 1246 float tonal; |
| 1190 | 1247 |
| 1191 /* Tonality boost (compensating for the average). */ | 1248 /* Tonality boost (compensating for the average). */ |
| 1192 tonal = MAX16(0.f,analysis->tonality-.15f)-0.09f; | 1249 tonal = MAX16(0.f,analysis->tonality-.15f)-0.09f; |
| 1193 tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal); | 1250 tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal); |
| 1194 if (pitch_change) | 1251 if (pitch_change) |
| 1195 tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f); | 1252 tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f); |
| 1196 /*printf("%f %f ", analysis->tonality, tonal);*/ | 1253 /*printf("%f %f ", analysis->tonality, tonal);*/ |
| 1197 target = tonal_target; | 1254 target = tonal_target; |
| 1198 } | 1255 } |
| 1256 #else |
| 1257 (void)analysis; |
| 1258 (void)pitch_change; |
| 1199 #endif | 1259 #endif |
| 1200 | 1260 |
| 1201 if (has_surround_mask&&!lfe) | 1261 if (has_surround_mask&&!lfe) |
| 1202 { | 1262 { |
| 1203 opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(surround
_masking,coded_bins<<BITRES), DB_SHIFT); | 1263 opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(surround
_masking,coded_bins<<BITRES), DB_SHIFT); |
| 1204 /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, s
t->intensity, surround_target, target, st->bitrate);*/ | 1264 /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, s
t->intensity, surround_target, target, st->bitrate);*/ |
| 1205 target = IMAX(target/4, surround_target); | 1265 target = IMAX(target/4, surround_target); |
| 1206 } | 1266 } |
| 1207 | 1267 |
| 1208 { | 1268 { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1266 VARDECL(unsigned char, collapse_masks); | 1326 VARDECL(unsigned char, collapse_masks); |
| 1267 celt_sig *prefilter_mem; | 1327 celt_sig *prefilter_mem; |
| 1268 opus_val16 *oldBandE, *oldLogE, *oldLogE2; | 1328 opus_val16 *oldBandE, *oldLogE, *oldLogE2; |
| 1269 int shortBlocks=0; | 1329 int shortBlocks=0; |
| 1270 int isTransient=0; | 1330 int isTransient=0; |
| 1271 const int CC = st->channels; | 1331 const int CC = st->channels; |
| 1272 const int C = st->stream_channels; | 1332 const int C = st->stream_channels; |
| 1273 int LM, M; | 1333 int LM, M; |
| 1274 int tf_select; | 1334 int tf_select; |
| 1275 int nbFilledBytes, nbAvailableBytes; | 1335 int nbFilledBytes, nbAvailableBytes; |
| 1336 int start; |
| 1337 int end; |
| 1276 int effEnd; | 1338 int effEnd; |
| 1277 int codedBands; | 1339 int codedBands; |
| 1278 int tf_sum; | 1340 int tf_sum; |
| 1279 int alloc_trim; | 1341 int alloc_trim; |
| 1280 int pitch_index=COMBFILTER_MINPERIOD; | 1342 int pitch_index=COMBFILTER_MINPERIOD; |
| 1281 opus_val16 gain1 = 0; | 1343 opus_val16 gain1 = 0; |
| 1282 int dual_stereo=0; | 1344 int dual_stereo=0; |
| 1283 int effectiveBytes; | 1345 int effectiveBytes; |
| 1284 int dynalloc_logp; | 1346 int dynalloc_logp; |
| 1285 opus_int32 vbr_rate; | 1347 opus_int32 vbr_rate; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1309 opus_val16 temporal_vbr=0; | 1371 opus_val16 temporal_vbr=0; |
| 1310 opus_val16 surround_trim = 0; | 1372 opus_val16 surround_trim = 0; |
| 1311 opus_int32 equiv_rate = 510000; | 1373 opus_int32 equiv_rate = 510000; |
| 1312 VARDECL(opus_val16, surround_dynalloc); | 1374 VARDECL(opus_val16, surround_dynalloc); |
| 1313 ALLOC_STACK; | 1375 ALLOC_STACK; |
| 1314 | 1376 |
| 1315 mode = st->mode; | 1377 mode = st->mode; |
| 1316 nbEBands = mode->nbEBands; | 1378 nbEBands = mode->nbEBands; |
| 1317 overlap = mode->overlap; | 1379 overlap = mode->overlap; |
| 1318 eBands = mode->eBands; | 1380 eBands = mode->eBands; |
| 1381 start = st->start; |
| 1382 end = st->end; |
| 1319 tf_estimate = 0; | 1383 tf_estimate = 0; |
| 1320 if (nbCompressedBytes<2 || pcm==NULL) | 1384 if (nbCompressedBytes<2 || pcm==NULL) |
| 1321 { | 1385 { |
| 1322 RESTORE_STACK; | 1386 RESTORE_STACK; |
| 1323 return OPUS_BAD_ARG; | 1387 return OPUS_BAD_ARG; |
| 1324 } | 1388 } |
| 1325 | 1389 |
| 1326 frame_size *= st->upsample; | 1390 frame_size *= st->upsample; |
| 1327 for (LM=0;LM<=mode->maxLM;LM++) | 1391 for (LM=0;LM<=mode->maxLM;LM++) |
| 1328 if (mode->shortMdctSize<<LM==frame_size) | 1392 if (mode->shortMdctSize<<LM==frame_size) |
| 1329 break; | 1393 break; |
| 1330 if (LM>mode->maxLM) | 1394 if (LM>mode->maxLM) |
| 1331 { | 1395 { |
| 1332 RESTORE_STACK; | 1396 RESTORE_STACK; |
| 1333 return OPUS_BAD_ARG; | 1397 return OPUS_BAD_ARG; |
| 1334 } | 1398 } |
| 1335 M=1<<LM; | 1399 M=1<<LM; |
| 1336 N = M*mode->shortMdctSize; | 1400 N = M*mode->shortMdctSize; |
| 1337 | 1401 |
| 1338 prefilter_mem = st->in_mem+CC*(st->overlap); | 1402 prefilter_mem = st->in_mem+CC*(overlap); |
| 1339 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD)); | 1403 oldBandE = (opus_val16*)(st->in_mem+CC*(overlap+COMBFILTER_MAXPERIOD)); |
| 1340 oldLogE = oldBandE + CC*nbEBands; | 1404 oldLogE = oldBandE + CC*nbEBands; |
| 1341 oldLogE2 = oldLogE + CC*nbEBands; | 1405 oldLogE2 = oldLogE + CC*nbEBands; |
| 1342 | 1406 |
| 1343 if (enc==NULL) | 1407 if (enc==NULL) |
| 1344 { | 1408 { |
| 1345 tell=1; | 1409 tell=1; |
| 1346 nbFilledBytes=0; | 1410 nbFilledBytes=0; |
| 1347 } else { | 1411 } else { |
| 1348 tell=ec_tell(enc); | 1412 tell=ec_tell(enc); |
| 1349 nbFilledBytes=(tell+4)>>3; | 1413 nbFilledBytes=(tell+4)>>3; |
| 1350 } | 1414 } |
| 1351 | 1415 |
| 1352 #ifdef CUSTOM_MODES | 1416 #ifdef CUSTOM_MODES |
| 1353 if (st->signalling && enc==NULL) | 1417 if (st->signalling && enc==NULL) |
| 1354 { | 1418 { |
| 1355 int tmp = (mode->effEBands-st->end)>>1; | 1419 int tmp = (mode->effEBands-end)>>1; |
| 1356 st->end = IMAX(1, mode->effEBands-tmp); | 1420 end = st->end = IMAX(1, mode->effEBands-tmp); |
| 1357 compressed[0] = tmp<<5; | 1421 compressed[0] = tmp<<5; |
| 1358 compressed[0] |= LM<<3; | 1422 compressed[0] |= LM<<3; |
| 1359 compressed[0] |= (C==2)<<2; | 1423 compressed[0] |= (C==2)<<2; |
| 1360 /* Convert "standard mode" to Opus header */ | 1424 /* Convert "standard mode" to Opus header */ |
| 1361 if (mode->Fs==48000 && mode->shortMdctSize==120) | 1425 if (mode->Fs==48000 && mode->shortMdctSize==120) |
| 1362 { | 1426 { |
| 1363 int c0 = toOpus(compressed[0]); | 1427 int c0 = toOpus(compressed[0]); |
| 1364 if (c0<0) | 1428 if (c0<0) |
| 1365 { | 1429 { |
| 1366 RESTORE_STACK; | 1430 RESTORE_STACK; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1429 if(max_allowed < nbAvailableBytes) | 1493 if(max_allowed < nbAvailableBytes) |
| 1430 { | 1494 { |
| 1431 nbCompressedBytes = nbFilledBytes+max_allowed; | 1495 nbCompressedBytes = nbFilledBytes+max_allowed; |
| 1432 nbAvailableBytes = max_allowed; | 1496 nbAvailableBytes = max_allowed; |
| 1433 ec_enc_shrink(enc, nbCompressedBytes); | 1497 ec_enc_shrink(enc, nbCompressedBytes); |
| 1434 } | 1498 } |
| 1435 } | 1499 } |
| 1436 } | 1500 } |
| 1437 total_bits = nbCompressedBytes*8; | 1501 total_bits = nbCompressedBytes*8; |
| 1438 | 1502 |
| 1439 effEnd = st->end; | 1503 effEnd = end; |
| 1440 if (effEnd > mode->effEBands) | 1504 if (effEnd > mode->effEBands) |
| 1441 effEnd = mode->effEBands; | 1505 effEnd = mode->effEBands; |
| 1442 | 1506 |
| 1443 ALLOC(in, CC*(N+st->overlap), celt_sig); | 1507 ALLOC(in, CC*(N+overlap), celt_sig); |
| 1444 | 1508 |
| 1445 sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsamp
le)); | 1509 sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsamp
le)); |
| 1446 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->u
psample); | 1510 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->u
psample); |
| 1447 sample_max=MAX32(sample_max, st->overlap_max); | 1511 sample_max=MAX32(sample_max, st->overlap_max); |
| 1448 #ifdef FIXED_POINT | 1512 #ifdef FIXED_POINT |
| 1449 silence = (sample_max==0); | 1513 silence = (sample_max==0); |
| 1450 #else | 1514 #else |
| 1451 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth)); | 1515 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth)); |
| 1452 #endif | 1516 #endif |
| 1453 #ifdef FUZZING | 1517 #ifdef FUZZING |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1467 total_bits=nbCompressedBytes*8; | 1531 total_bits=nbCompressedBytes*8; |
| 1468 nbAvailableBytes=2; | 1532 nbAvailableBytes=2; |
| 1469 ec_enc_shrink(enc, nbCompressedBytes); | 1533 ec_enc_shrink(enc, nbCompressedBytes); |
| 1470 } | 1534 } |
| 1471 /* Pretend we've filled all the remaining bits with zeros | 1535 /* Pretend we've filled all the remaining bits with zeros |
| 1472 (that's what the initialiser did anyway) */ | 1536 (that's what the initialiser did anyway) */ |
| 1473 tell = nbCompressedBytes*8; | 1537 tell = nbCompressedBytes*8; |
| 1474 enc->nbits_total+=tell-ec_tell(enc); | 1538 enc->nbits_total+=tell-ec_tell(enc); |
| 1475 } | 1539 } |
| 1476 c=0; do { | 1540 c=0; do { |
| 1477 celt_preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsam
ple, | 1541 int need_clip=0; |
| 1478 mode->preemph, st->preemph_memE+c, st->clip); | 1542 #ifndef FIXED_POINT |
| 1543 need_clip = st->clip && sample_max>65536.f; |
| 1544 #endif |
| 1545 celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample, |
| 1546 mode->preemph, st->preemph_memE+c, need_clip); |
| 1479 } while (++c<CC); | 1547 } while (++c<CC); |
| 1480 | 1548 |
| 1481 | 1549 |
| 1482 | 1550 |
| 1483 /* Find pitch period and gain */ | 1551 /* Find pitch period and gain */ |
| 1484 { | 1552 { |
| 1485 int enabled; | 1553 int enabled; |
| 1486 int qg; | 1554 int qg; |
| 1487 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && st->
start==0 && !silence && !st->disable_pf | 1555 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && star
t==0 && !silence && !st->disable_pf |
| 1488 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->var
iable_duration==OPUS_FRAMESIZE_VARIABLE); | 1556 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->var
iable_duration==OPUS_FRAMESIZE_VARIABLE); |
| 1489 | 1557 |
| 1490 prefilter_tapset = st->tapset_decision; | 1558 prefilter_tapset = st->tapset_decision; |
| 1491 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pit
ch_index, &gain1, &qg, enabled, nbAvailableBytes); | 1559 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pit
ch_index, &gain1, &qg, enabled, nbAvailableBytes); |
| 1492 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) &&
(!st->analysis.valid || st->analysis.tonality > .3) | 1560 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) &&
(!st->analysis.valid || st->analysis.tonality > .3) |
| 1493 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st-
>prefilter_period)) | 1561 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st-
>prefilter_period)) |
| 1494 pitch_change = 1; | 1562 pitch_change = 1; |
| 1495 if (pf_on==0) | 1563 if (pf_on==0) |
| 1496 { | 1564 { |
| 1497 if(st->start==0 && tell+16<=total_bits) | 1565 if(start==0 && tell+16<=total_bits) |
| 1498 ec_enc_bit_logp(enc, 0, 1); | 1566 ec_enc_bit_logp(enc, 0, 1); |
| 1499 } else { | 1567 } else { |
| 1500 /*This block is not gated by a total bits check only because | 1568 /*This block is not gated by a total bits check only because |
| 1501 of the nbAvailableBytes check above.*/ | 1569 of the nbAvailableBytes check above.*/ |
| 1502 int octave; | 1570 int octave; |
| 1503 ec_enc_bit_logp(enc, 1, 1); | 1571 ec_enc_bit_logp(enc, 1, 1); |
| 1504 pitch_index += 1; | 1572 pitch_index += 1; |
| 1505 octave = EC_ILOG(pitch_index)-5; | 1573 octave = EC_ILOG(pitch_index)-5; |
| 1506 ec_enc_uint(enc, octave, 6); | 1574 ec_enc_uint(enc, octave, 6); |
| 1507 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); | 1575 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); |
| 1508 pitch_index -= 1; | 1576 pitch_index -= 1; |
| 1509 ec_enc_bits(enc, qg, 3); | 1577 ec_enc_bits(enc, qg, 3); |
| 1510 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2); | 1578 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2); |
| 1511 } | 1579 } |
| 1512 } | 1580 } |
| 1513 | 1581 |
| 1514 isTransient = 0; | 1582 isTransient = 0; |
| 1515 shortBlocks = 0; | 1583 shortBlocks = 0; |
| 1516 if (st->complexity >= 1 && !st->lfe) | 1584 if (st->complexity >= 1 && !st->lfe) |
| 1517 { | 1585 { |
| 1518 isTransient = transient_analysis(in, N+st->overlap, CC, | 1586 isTransient = transient_analysis(in, N+overlap, CC, |
| 1519 &tf_estimate, &tf_chan); | 1587 &tf_estimate, &tf_chan); |
| 1520 } | 1588 } |
| 1521 if (LM>0 && ec_tell(enc)+3<=total_bits) | 1589 if (LM>0 && ec_tell(enc)+3<=total_bits) |
| 1522 { | 1590 { |
| 1523 if (isTransient) | 1591 if (isTransient) |
| 1524 shortBlocks = M; | 1592 shortBlocks = M; |
| 1525 } else { | 1593 } else { |
| 1526 isTransient = 0; | 1594 isTransient = 0; |
| 1527 transient_got_disabled=1; | 1595 transient_got_disabled=1; |
| 1528 } | 1596 } |
| 1529 | 1597 |
| 1530 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */ | 1598 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 1531 ALLOC(bandE,nbEBands*CC, celt_ener); | 1599 ALLOC(bandE,nbEBands*CC, celt_ener); |
| 1532 ALLOC(bandLogE,nbEBands*CC, opus_val16); | 1600 ALLOC(bandLogE,nbEBands*CC, opus_val16); |
| 1533 | 1601 |
| 1534 secondMdct = shortBlocks && st->complexity>=8; | 1602 secondMdct = shortBlocks && st->complexity>=8; |
| 1535 ALLOC(bandLogE2, C*nbEBands, opus_val16); | 1603 ALLOC(bandLogE2, C*nbEBands, opus_val16); |
| 1536 if (secondMdct) | 1604 if (secondMdct) |
| 1537 { | 1605 { |
| 1538 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample); | 1606 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample); |
| 1539 compute_band_energies(mode, freq, bandE, effEnd, C, M); | 1607 compute_band_energies(mode, freq, bandE, effEnd, C, LM); |
| 1540 amp2Log2(mode, effEnd, st->end, bandE, bandLogE2, C); | 1608 amp2Log2(mode, effEnd, end, bandE, bandLogE2, C); |
| 1541 for (i=0;i<C*nbEBands;i++) | 1609 for (i=0;i<C*nbEBands;i++) |
| 1542 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT)); | 1610 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT)); |
| 1543 } | 1611 } |
| 1544 | 1612 |
| 1545 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample); | 1613 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample); |
| 1546 if (CC==2&&C==1) | 1614 if (CC==2&&C==1) |
| 1547 tf_chan = 0; | 1615 tf_chan = 0; |
| 1548 compute_band_energies(mode, freq, bandE, effEnd, C, M); | 1616 compute_band_energies(mode, freq, bandE, effEnd, C, LM); |
| 1549 | 1617 |
| 1550 if (st->lfe) | 1618 if (st->lfe) |
| 1551 { | 1619 { |
| 1552 for (i=2;i<st->end;i++) | 1620 for (i=2;i<end;i++) |
| 1553 { | 1621 { |
| 1554 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0])); | 1622 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0])); |
| 1555 bandE[i] = MAX32(bandE[i], EPSILON); | 1623 bandE[i] = MAX32(bandE[i], EPSILON); |
| 1556 } | 1624 } |
| 1557 } | 1625 } |
| 1558 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C); | 1626 amp2Log2(mode, effEnd, end, bandE, bandLogE, C); |
| 1559 | 1627 |
| 1560 ALLOC(surround_dynalloc, C*nbEBands, opus_val16); | 1628 ALLOC(surround_dynalloc, C*nbEBands, opus_val16); |
| 1561 for(i=0;i<st->end;i++) | 1629 OPUS_CLEAR(surround_dynalloc, end); |
| 1562 surround_dynalloc[i] = 0; | |
| 1563 /* This computes how much masking takes place between surround channels */ | 1630 /* This computes how much masking takes place between surround channels */ |
| 1564 if (st->start==0&&st->energy_mask&&!st->lfe) | 1631 if (start==0&&st->energy_mask&&!st->lfe) |
| 1565 { | 1632 { |
| 1566 int mask_end; | 1633 int mask_end; |
| 1567 int midband; | 1634 int midband; |
| 1568 int count_dynalloc; | 1635 int count_dynalloc; |
| 1569 opus_val32 mask_avg=0; | 1636 opus_val32 mask_avg=0; |
| 1570 opus_val32 diff=0; | 1637 opus_val32 diff=0; |
| 1571 int count=0; | 1638 int count=0; |
| 1572 mask_end = IMAX(2,st->lastCodedBands); | 1639 mask_end = IMAX(2,st->lastCodedBands); |
| 1573 for (c=0;c<C;c++) | 1640 for (c=0;c<C;c++) |
| 1574 { | 1641 { |
| 1575 for(i=0;i<mask_end;i++) | 1642 for(i=0;i<mask_end;i++) |
| 1576 { | 1643 { |
| 1577 opus_val16 mask; | 1644 opus_val16 mask; |
| 1578 mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i], | 1645 mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i], |
| 1579 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); | 1646 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); |
| 1580 if (mask > 0) | 1647 if (mask > 0) |
| 1581 mask = HALF16(mask); | 1648 mask = HALF16(mask); |
| 1582 mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]); | 1649 mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]); |
| 1583 count += eBands[i+1]-eBands[i]; | 1650 count += eBands[i+1]-eBands[i]; |
| 1584 diff += MULT16_16(mask, 1+2*i-mask_end); | 1651 diff += MULT16_16(mask, 1+2*i-mask_end); |
| 1585 } | 1652 } |
| 1586 } | 1653 } |
| 1654 celt_assert(count>0); |
| 1587 mask_avg = DIV32_16(mask_avg,count); | 1655 mask_avg = DIV32_16(mask_avg,count); |
| 1588 mask_avg += QCONST16(.2f, DB_SHIFT); | 1656 mask_avg += QCONST16(.2f, DB_SHIFT); |
| 1589 diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end); | 1657 diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end); |
| 1590 /* Again, being conservative */ | 1658 /* Again, being conservative */ |
| 1591 diff = HALF32(diff); | 1659 diff = HALF32(diff); |
| 1592 diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_S
HIFT)); | 1660 diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_S
HIFT)); |
| 1593 /* Find the band that's in the middle of the coded spectrum */ | 1661 /* Find the band that's in the middle of the coded spectrum */ |
| 1594 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++); | 1662 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++); |
| 1595 count_dynalloc=0; | 1663 count_dynalloc=0; |
| 1596 for(i=0;i<mask_end;i++) | 1664 for(i=0;i<mask_end;i++) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1614 { | 1682 { |
| 1615 /* If we need dynalloc in many bands, it's probably because our | 1683 /* If we need dynalloc in many bands, it's probably because our |
| 1616 initial masking rate was too low. */ | 1684 initial masking rate was too low. */ |
| 1617 mask_avg += QCONST16(.25f, DB_SHIFT); | 1685 mask_avg += QCONST16(.25f, DB_SHIFT); |
| 1618 if (mask_avg>0) | 1686 if (mask_avg>0) |
| 1619 { | 1687 { |
| 1620 /* Something went really wrong in the original calculations, | 1688 /* Something went really wrong in the original calculations, |
| 1621 disabling masking. */ | 1689 disabling masking. */ |
| 1622 mask_avg = 0; | 1690 mask_avg = 0; |
| 1623 diff = 0; | 1691 diff = 0; |
| 1624 for(i=0;i<mask_end;i++) | 1692 OPUS_CLEAR(surround_dynalloc, mask_end); |
| 1625 surround_dynalloc[i] = 0; | |
| 1626 } else { | 1693 } else { |
| 1627 for(i=0;i<mask_end;i++) | 1694 for(i=0;i<mask_end;i++) |
| 1628 surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25
f, DB_SHIFT)); | 1695 surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25
f, DB_SHIFT)); |
| 1629 } | 1696 } |
| 1630 } | 1697 } |
| 1631 mask_avg += QCONST16(.2f, DB_SHIFT); | 1698 mask_avg += QCONST16(.2f, DB_SHIFT); |
| 1632 /* Convert to 1/64th units used for the trim */ | 1699 /* Convert to 1/64th units used for the trim */ |
| 1633 surround_trim = 64*diff; | 1700 surround_trim = 64*diff; |
| 1634 /*printf("%d %d ", mask_avg, surround_trim);*/ | 1701 /*printf("%d %d ", mask_avg, surround_trim);*/ |
| 1635 surround_masking = mask_avg; | 1702 surround_masking = mask_avg; |
| 1636 } | 1703 } |
| 1637 /* Temporal VBR (but not for LFE) */ | 1704 /* Temporal VBR (but not for LFE) */ |
| 1638 if (!st->lfe) | 1705 if (!st->lfe) |
| 1639 { | 1706 { |
| 1640 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT); | 1707 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT); |
| 1641 opus_val32 frame_avg=0; | 1708 opus_val32 frame_avg=0; |
| 1642 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0; | 1709 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0; |
| 1643 for(i=st->start;i<st->end;i++) | 1710 for(i=start;i<end;i++) |
| 1644 { | 1711 { |
| 1645 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset); | 1712 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset); |
| 1646 if (C==2) | 1713 if (C==2) |
| 1647 follow = MAX16(follow, bandLogE[i+nbEBands]-offset); | 1714 follow = MAX16(follow, bandLogE[i+nbEBands]-offset); |
| 1648 frame_avg += follow; | 1715 frame_avg += follow; |
| 1649 } | 1716 } |
| 1650 frame_avg /= (st->end-st->start); | 1717 frame_avg /= (end-start); |
| 1651 temporal_vbr = SUB16(frame_avg,st->spec_avg); | 1718 temporal_vbr = SUB16(frame_avg,st->spec_avg); |
| 1652 temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHI
FT), temporal_vbr)); | 1719 temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHI
FT), temporal_vbr)); |
| 1653 st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr); | 1720 st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr); |
| 1654 } | 1721 } |
| 1655 /*for (i=0;i<21;i++) | 1722 /*for (i=0;i<21;i++) |
| 1656 printf("%f ", bandLogE[i]); | 1723 printf("%f ", bandLogE[i]); |
| 1657 printf("\n");*/ | 1724 printf("\n");*/ |
| 1658 | 1725 |
| 1659 if (!secondMdct) | 1726 if (!secondMdct) |
| 1660 { | 1727 { |
| 1661 for (i=0;i<C*nbEBands;i++) | 1728 OPUS_COPY(bandLogE2, bandLogE, C*nbEBands); |
| 1662 bandLogE2[i] = bandLogE[i]; | |
| 1663 } | 1729 } |
| 1664 | 1730 |
| 1665 /* Last chance to catch any transient we might have missed in the | 1731 /* Last chance to catch any transient we might have missed in the |
| 1666 time-domain analysis */ | 1732 time-domain analysis */ |
| 1667 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 &
& !st->lfe) | 1733 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 &
& !st->lfe) |
| 1668 { | 1734 { |
| 1669 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, st->end, C)) | 1735 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, end, C)) |
| 1670 { | 1736 { |
| 1671 isTransient = 1; | 1737 isTransient = 1; |
| 1672 shortBlocks = M; | 1738 shortBlocks = M; |
| 1673 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample); | 1739 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample); |
| 1674 compute_band_energies(mode, freq, bandE, effEnd, C, M); | 1740 compute_band_energies(mode, freq, bandE, effEnd, C, LM); |
| 1675 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C); | 1741 amp2Log2(mode, effEnd, end, bandE, bandLogE, C); |
| 1676 /* Compensate for the scaling of short vs long mdcts */ | 1742 /* Compensate for the scaling of short vs long mdcts */ |
| 1677 for (i=0;i<C*nbEBands;i++) | 1743 for (i=0;i<C*nbEBands;i++) |
| 1678 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT)); | 1744 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT)); |
| 1679 tf_estimate = QCONST16(.2f,14); | 1745 tf_estimate = QCONST16(.2f,14); |
| 1680 } | 1746 } |
| 1681 } | 1747 } |
| 1682 | 1748 |
| 1683 if (LM>0 && ec_tell(enc)+3<=total_bits) | 1749 if (LM>0 && ec_tell(enc)+3<=total_bits) |
| 1684 ec_enc_bit_logp(enc, isTransient, 3); | 1750 ec_enc_bit_logp(enc, isTransient, 3); |
| 1685 | 1751 |
| 1686 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | 1752 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 1687 | 1753 |
| 1688 /* Band normalisation */ | 1754 /* Band normalisation */ |
| 1689 normalise_bands(mode, freq, X, bandE, effEnd, C, M); | 1755 normalise_bands(mode, freq, X, bandE, effEnd, C, M); |
| 1690 | 1756 |
| 1691 ALLOC(tf_res, nbEBands, int); | 1757 ALLOC(tf_res, nbEBands, int); |
| 1692 /* Disable variable tf resolution for hybrid and at very low bitrate */ | 1758 /* Disable variable tf resolution for hybrid and at very low bitrate */ |
| 1693 if (effectiveBytes>=15*C && st->start==0 && st->complexity>=2 && !st->lfe) | 1759 if (effectiveBytes>=15*C && start==0 && st->complexity>=2 && !st->lfe) |
| 1694 { | 1760 { |
| 1695 int lambda; | 1761 int lambda; |
| 1696 if (effectiveBytes<40) | 1762 if (effectiveBytes<40) |
| 1697 lambda = 12; | 1763 lambda = 12; |
| 1698 else if (effectiveBytes<60) | 1764 else if (effectiveBytes<60) |
| 1699 lambda = 6; | 1765 lambda = 6; |
| 1700 else if (effectiveBytes<100) | 1766 else if (effectiveBytes<100) |
| 1701 lambda = 4; | 1767 lambda = 4; |
| 1702 else | 1768 else |
| 1703 lambda = 3; | 1769 lambda = 3; |
| 1704 lambda*=2; | 1770 lambda*=2; |
| 1705 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, L
M, &tf_sum, tf_estimate, tf_chan); | 1771 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, L
M, &tf_sum, tf_estimate, tf_chan); |
| 1706 for (i=effEnd;i<st->end;i++) | 1772 for (i=effEnd;i<end;i++) |
| 1707 tf_res[i] = tf_res[effEnd-1]; | 1773 tf_res[i] = tf_res[effEnd-1]; |
| 1708 } else { | 1774 } else { |
| 1709 tf_sum = 0; | 1775 tf_sum = 0; |
| 1710 for (i=0;i<st->end;i++) | 1776 for (i=0;i<end;i++) |
| 1711 tf_res[i] = isTransient; | 1777 tf_res[i] = isTransient; |
| 1712 tf_select=0; | 1778 tf_select=0; |
| 1713 } | 1779 } |
| 1714 | 1780 |
| 1715 ALLOC(error, C*nbEBands, opus_val16); | 1781 ALLOC(error, C*nbEBands, opus_val16); |
| 1716 quant_coarse_energy(mode, st->start, st->end, effEnd, bandLogE, | 1782 quant_coarse_energy(mode, start, end, effEnd, bandLogE, |
| 1717 oldBandE, total_bits, error, enc, | 1783 oldBandE, total_bits, error, enc, |
| 1718 C, LM, nbAvailableBytes, st->force_intra, | 1784 C, LM, nbAvailableBytes, st->force_intra, |
| 1719 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe); | 1785 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe); |
| 1720 | 1786 |
| 1721 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc); | 1787 tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc); |
| 1722 | 1788 |
| 1723 if (ec_tell(enc)+4<=total_bits) | 1789 if (ec_tell(enc)+4<=total_bits) |
| 1724 { | 1790 { |
| 1725 if (st->lfe) | 1791 if (st->lfe) |
| 1726 { | 1792 { |
| 1727 st->tapset_decision = 0; | 1793 st->tapset_decision = 0; |
| 1728 st->spread_decision = SPREAD_NORMAL; | 1794 st->spread_decision = SPREAD_NORMAL; |
| 1729 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C ||
st->start != 0) | 1795 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C ||
start != 0) |
| 1730 { | 1796 { |
| 1731 if (st->complexity == 0) | 1797 if (st->complexity == 0) |
| 1732 st->spread_decision = SPREAD_NONE; | 1798 st->spread_decision = SPREAD_NONE; |
| 1733 else | 1799 else |
| 1734 st->spread_decision = SPREAD_NORMAL; | 1800 st->spread_decision = SPREAD_NORMAL; |
| 1735 } else { | 1801 } else { |
| 1736 /* Disable new spreading+tapset estimator until we can show it works | 1802 /* Disable new spreading+tapset estimator until we can show it works |
| 1737 better than the old one. So far it seems like spreading_decision() | 1803 better than the old one. So far it seems like spreading_decision() |
| 1738 works best. */ | 1804 works best. */ |
| 1739 #if 0 | 1805 #if 0 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1753 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); | 1819 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); |
| 1754 } | 1820 } |
| 1755 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/ | 1821 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/ |
| 1756 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision,
st->analysis.tonality_slope, st->tapset_decision);*/ | 1822 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision,
st->analysis.tonality_slope, st->tapset_decision);*/ |
| 1757 } | 1823 } |
| 1758 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); | 1824 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); |
| 1759 } | 1825 } |
| 1760 | 1826 |
| 1761 ALLOC(offsets, nbEBands, int); | 1827 ALLOC(offsets, nbEBands, int); |
| 1762 | 1828 |
| 1763 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, st->start, st->en
d, C, offsets, | 1829 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, start, end, C, of
fsets, |
| 1764 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr, | 1830 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr, |
| 1765 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc); | 1831 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc); |
| 1766 /* For LFE, everything interesting is in the first band */ | 1832 /* For LFE, everything interesting is in the first band */ |
| 1767 if (st->lfe) | 1833 if (st->lfe) |
| 1768 offsets[0] = IMIN(8, effectiveBytes/3); | 1834 offsets[0] = IMIN(8, effectiveBytes/3); |
| 1769 ALLOC(cap, nbEBands, int); | 1835 ALLOC(cap, nbEBands, int); |
| 1770 init_caps(mode,cap,LM,C); | 1836 init_caps(mode,cap,LM,C); |
| 1771 | 1837 |
| 1772 dynalloc_logp = 6; | 1838 dynalloc_logp = 6; |
| 1773 total_bits<<=BITRES; | 1839 total_bits<<=BITRES; |
| 1774 total_boost = 0; | 1840 total_boost = 0; |
| 1775 tell = ec_tell_frac(enc); | 1841 tell = ec_tell_frac(enc); |
| 1776 for (i=st->start;i<st->end;i++) | 1842 for (i=start;i<end;i++) |
| 1777 { | 1843 { |
| 1778 int width, quanta; | 1844 int width, quanta; |
| 1779 int dynalloc_loop_logp; | 1845 int dynalloc_loop_logp; |
| 1780 int boost; | 1846 int boost; |
| 1781 int j; | 1847 int j; |
| 1782 width = C*(eBands[i+1]-eBands[i])<<LM; | 1848 width = C*(eBands[i+1]-eBands[i])<<LM; |
| 1783 /* quanta is 6 bits, but no more than 1 bit/sample | 1849 /* quanta is 6 bits, but no more than 1 bit/sample |
| 1784 and no less than 1/8 bit/sample */ | 1850 and no less than 1/8 bit/sample */ |
| 1785 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | 1851 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
| 1786 dynalloc_loop_logp = dynalloc_logp; | 1852 dynalloc_loop_logp = dynalloc_logp; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1811 { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134}; | 1877 { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134}; |
| 1812 static const opus_val16 intensity_histeresis[21]= | 1878 static const opus_val16 intensity_histeresis[21]= |
| 1813 { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8}; | 1879 { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8}; |
| 1814 | 1880 |
| 1815 /* Always use MS for 2.5 ms frames until we can do a better analysis */ | 1881 /* Always use MS for 2.5 ms frames until we can do a better analysis */ |
| 1816 if (LM!=0) | 1882 if (LM!=0) |
| 1817 dual_stereo = stereo_analysis(mode, X, LM, N); | 1883 dual_stereo = stereo_analysis(mode, X, LM, N); |
| 1818 | 1884 |
| 1819 st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000), | 1885 st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000), |
| 1820 intensity_thresholds, intensity_histeresis, 21, st->intensity); | 1886 intensity_thresholds, intensity_histeresis, 21, st->intensity); |
| 1821 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity)); | 1887 st->intensity = IMIN(end,IMAX(start, st->intensity)); |
| 1822 } | 1888 } |
| 1823 | 1889 |
| 1824 alloc_trim = 5; | 1890 alloc_trim = 5; |
| 1825 if (tell+(6<<BITRES) <= total_bits - total_boost) | 1891 if (tell+(6<<BITRES) <= total_bits - total_boost) |
| 1826 { | 1892 { |
| 1827 if (st->lfe) | 1893 if (st->lfe) |
| 1828 alloc_trim = 5; | 1894 alloc_trim = 5; |
| 1829 else | 1895 else |
| 1830 alloc_trim = alloc_trim_analysis(mode, X, bandLogE, | 1896 alloc_trim = alloc_trim_analysis(mode, X, bandLogE, |
| 1831 st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, s
t->intensity, surround_trim); | 1897 end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, |
| 1898 st->intensity, surround_trim, st->arch); |
| 1832 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7); | 1899 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7); |
| 1833 tell = ec_tell_frac(enc); | 1900 tell = ec_tell_frac(enc); |
| 1834 } | 1901 } |
| 1835 | 1902 |
| 1836 /* Variable bitrate */ | 1903 /* Variable bitrate */ |
| 1837 if (vbr_rate>0) | 1904 if (vbr_rate>0) |
| 1838 { | 1905 { |
| 1839 opus_val16 alpha; | 1906 opus_val16 alpha; |
| 1840 opus_int32 delta; | 1907 opus_int32 delta; |
| 1841 /* The target rate in 8th bits per frame */ | 1908 /* The target rate in 8th bits per frame */ |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1923 | 1990 |
| 1924 /* Bit allocation */ | 1991 /* Bit allocation */ |
| 1925 ALLOC(fine_quant, nbEBands, int); | 1992 ALLOC(fine_quant, nbEBands, int); |
| 1926 ALLOC(pulses, nbEBands, int); | 1993 ALLOC(pulses, nbEBands, int); |
| 1927 ALLOC(fine_priority, nbEBands, int); | 1994 ALLOC(fine_priority, nbEBands, int); |
| 1928 | 1995 |
| 1929 /* bits = packet size - where we are - safety*/ | 1996 /* bits = packet size - where we are - safety*/ |
| 1930 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; | 1997 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; |
| 1931 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES)
: 0; | 1998 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES)
: 0; |
| 1932 bits -= anti_collapse_rsv; | 1999 bits -= anti_collapse_rsv; |
| 1933 signalBandwidth = st->end-1; | 2000 signalBandwidth = end-1; |
| 1934 #ifndef DISABLE_FLOAT_API | 2001 #ifndef DISABLE_FLOAT_API |
| 1935 if (st->analysis.valid) | 2002 if (st->analysis.valid) |
| 1936 { | 2003 { |
| 1937 int min_bandwidth; | 2004 int min_bandwidth; |
| 1938 if (equiv_rate < (opus_int32)32000*C) | 2005 if (equiv_rate < (opus_int32)32000*C) |
| 1939 min_bandwidth = 13; | 2006 min_bandwidth = 13; |
| 1940 else if (equiv_rate < (opus_int32)48000*C) | 2007 else if (equiv_rate < (opus_int32)48000*C) |
| 1941 min_bandwidth = 16; | 2008 min_bandwidth = 16; |
| 1942 else if (equiv_rate < (opus_int32)60000*C) | 2009 else if (equiv_rate < (opus_int32)60000*C) |
| 1943 min_bandwidth = 18; | 2010 min_bandwidth = 18; |
| 1944 else if (equiv_rate < (opus_int32)80000*C) | 2011 else if (equiv_rate < (opus_int32)80000*C) |
| 1945 min_bandwidth = 19; | 2012 min_bandwidth = 19; |
| 1946 else | 2013 else |
| 1947 min_bandwidth = 20; | 2014 min_bandwidth = 20; |
| 1948 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth); | 2015 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth); |
| 1949 } | 2016 } |
| 1950 #endif | 2017 #endif |
| 1951 if (st->lfe) | 2018 if (st->lfe) |
| 1952 signalBandwidth = 1; | 2019 signalBandwidth = 1; |
| 1953 codedBands = compute_allocation(mode, st->start, st->end, offsets, cap, | 2020 codedBands = compute_allocation(mode, start, end, offsets, cap, |
| 1954 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses, | 2021 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses, |
| 1955 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBan
dwidth); | 2022 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBan
dwidth); |
| 1956 if (st->lastCodedBands) | 2023 if (st->lastCodedBands) |
| 1957 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,c
odedBands)); | 2024 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,c
odedBands)); |
| 1958 else | 2025 else |
| 1959 st->lastCodedBands = codedBands; | 2026 st->lastCodedBands = codedBands; |
| 1960 | 2027 |
| 1961 quant_fine_energy(mode, st->start, st->end, oldBandE, error, fine_quant, enc,
C); | 2028 quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, enc, C); |
| 1962 | 2029 |
| 1963 /* Residual quantisation */ | 2030 /* Residual quantisation */ |
| 1964 ALLOC(collapse_masks, C*nbEBands, unsigned char); | 2031 ALLOC(collapse_masks, C*nbEBands, unsigned char); |
| 1965 quant_all_bands(1, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_m
asks, | 2032 quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks, |
| 1966 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, st->inten
sity, tf_res, | 2033 bandE, pulses, shortBlocks, st->spread_decision, |
| 1967 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, code
dBands, &st->rng); | 2034 dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_
collapse_rsv, |
| 2035 balance, enc, LM, codedBands, &st->rng, st->arch); |
| 1968 | 2036 |
| 1969 if (anti_collapse_rsv > 0) | 2037 if (anti_collapse_rsv > 0) |
| 1970 { | 2038 { |
| 1971 anti_collapse_on = st->consec_transient<2; | 2039 anti_collapse_on = st->consec_transient<2; |
| 1972 #ifdef FUZZING | 2040 #ifdef FUZZING |
| 1973 anti_collapse_on = rand()&0x1; | 2041 anti_collapse_on = rand()&0x1; |
| 1974 #endif | 2042 #endif |
| 1975 ec_enc_bits(enc, anti_collapse_on, 1); | 2043 ec_enc_bits(enc, anti_collapse_on, 1); |
| 1976 } | 2044 } |
| 1977 quant_energy_finalise(mode, st->start, st->end, oldBandE, error, fine_quant,
fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C); | 2045 quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_pri
ority, nbCompressedBytes*8-ec_tell(enc), enc, C); |
| 1978 | 2046 |
| 1979 if (silence) | 2047 if (silence) |
| 1980 { | 2048 { |
| 1981 for (i=0;i<C*nbEBands;i++) | 2049 for (i=0;i<C*nbEBands;i++) |
| 1982 oldBandE[i] = -QCONST16(28.f,DB_SHIFT); | 2050 oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
| 1983 } | 2051 } |
| 1984 | 2052 |
| 1985 #ifdef RESYNTH | 2053 #ifdef RESYNTH |
| 1986 /* Re-synthesis of the coded audio if required */ | 2054 /* Re-synthesis of the coded audio if required */ |
| 1987 { | 2055 { |
| 1988 celt_sig *out_mem[2]; | 2056 celt_sig *out_mem[2]; |
| 1989 | 2057 |
| 1990 if (anti_collapse_on) | 2058 if (anti_collapse_on) |
| 1991 { | 2059 { |
| 1992 anti_collapse(mode, X, collapse_masks, LM, C, N, | 2060 anti_collapse(mode, X, collapse_masks, LM, C, N, |
| 1993 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng)
; | 2061 start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
| 1994 } | |
| 1995 | |
| 1996 if (silence) | |
| 1997 { | |
| 1998 for (i=0;i<C*N;i++) | |
| 1999 freq[i] = 0; | |
| 2000 } else { | |
| 2001 /* Synthesis */ | |
| 2002 denormalise_bands(mode, X, freq, oldBandE, st->start, effEnd, C, M); | |
| 2003 } | 2062 } |
| 2004 | 2063 |
| 2005 c=0; do { | 2064 c=0; do { |
| 2006 OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2); | 2065 OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2); |
| 2007 } while (++c<CC); | 2066 } while (++c<CC); |
| 2008 | 2067 |
| 2009 if (CC==2&&C==1) | |
| 2010 { | |
| 2011 for (i=0;i<N;i++) | |
| 2012 freq[N+i] = freq[i]; | |
| 2013 } | |
| 2014 | |
| 2015 c=0; do { | 2068 c=0; do { |
| 2016 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N; | 2069 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N; |
| 2017 } while (++c<CC); | 2070 } while (++c<CC); |
| 2018 | 2071 |
| 2019 compute_inv_mdcts(mode, shortBlocks, freq, out_mem, CC, LM); | 2072 celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd, C, CC, isTransie
nt, LM, st->upsample, silence); |
| 2020 | 2073 |
| 2021 c=0; do { | 2074 c=0; do { |
| 2022 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); | 2075 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
| 2023 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINP
ERIOD); | 2076 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINP
ERIOD); |
| 2024 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefi
lter_period, mode->shortMdctSize, | 2077 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefi
lter_period, mode->shortMdctSize, |
| 2025 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_
old, st->prefilter_tapset, | 2078 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_
old, st->prefilter_tapset, |
| 2026 mode->window, st->overlap); | 2079 mode->window, overlap); |
| 2027 if (LM!=0) | 2080 if (LM!=0) |
| 2028 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMd
ctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize, | 2081 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMd
ctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize, |
| 2029 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tap
set, | 2082 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tap
set, |
| 2030 mode->window, overlap); | 2083 mode->window, overlap); |
| 2031 } while (++c<CC); | 2084 } while (++c<CC); |
| 2032 | 2085 |
| 2033 /* We reuse freq[] as scratch space for the de-emphasis */ | 2086 /* We reuse freq[] as scratch space for the de-emphasis */ |
| 2034 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph,
st->preemph_memD, freq); | 2087 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph,
st->preemph_memD); |
| 2035 st->prefilter_period_old = st->prefilter_period; | 2088 st->prefilter_period_old = st->prefilter_period; |
| 2036 st->prefilter_gain_old = st->prefilter_gain; | 2089 st->prefilter_gain_old = st->prefilter_gain; |
| 2037 st->prefilter_tapset_old = st->prefilter_tapset; | 2090 st->prefilter_tapset_old = st->prefilter_tapset; |
| 2038 } | 2091 } |
| 2039 #endif | 2092 #endif |
| 2040 | 2093 |
| 2041 st->prefilter_period = pitch_index; | 2094 st->prefilter_period = pitch_index; |
| 2042 st->prefilter_gain = gain1; | 2095 st->prefilter_gain = gain1; |
| 2043 st->prefilter_tapset = prefilter_tapset; | 2096 st->prefilter_tapset = prefilter_tapset; |
| 2044 #ifdef RESYNTH | 2097 #ifdef RESYNTH |
| 2045 if (LM!=0) | 2098 if (LM!=0) |
| 2046 { | 2099 { |
| 2047 st->prefilter_period_old = st->prefilter_period; | 2100 st->prefilter_period_old = st->prefilter_period; |
| 2048 st->prefilter_gain_old = st->prefilter_gain; | 2101 st->prefilter_gain_old = st->prefilter_gain; |
| 2049 st->prefilter_tapset_old = st->prefilter_tapset; | 2102 st->prefilter_tapset_old = st->prefilter_tapset; |
| 2050 } | 2103 } |
| 2051 #endif | 2104 #endif |
| 2052 | 2105 |
| 2053 if (CC==2&&C==1) { | 2106 if (CC==2&&C==1) { |
| 2054 for (i=0;i<nbEBands;i++) | 2107 OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands); |
| 2055 oldBandE[nbEBands+i]=oldBandE[i]; | |
| 2056 } | 2108 } |
| 2057 | 2109 |
| 2058 if (!isTransient) | 2110 if (!isTransient) |
| 2059 { | 2111 { |
| 2060 for (i=0;i<CC*nbEBands;i++) | 2112 OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands); |
| 2061 oldLogE2[i] = oldLogE[i]; | 2113 OPUS_COPY(oldLogE, oldBandE, CC*nbEBands); |
| 2062 for (i=0;i<CC*nbEBands;i++) | |
| 2063 oldLogE[i] = oldBandE[i]; | |
| 2064 } else { | 2114 } else { |
| 2065 for (i=0;i<CC*nbEBands;i++) | 2115 for (i=0;i<CC*nbEBands;i++) |
| 2066 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); | 2116 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
| 2067 } | 2117 } |
| 2068 /* In case start or end were to change */ | 2118 /* In case start or end were to change */ |
| 2069 c=0; do | 2119 c=0; do |
| 2070 { | 2120 { |
| 2071 for (i=0;i<st->start;i++) | 2121 for (i=0;i<start;i++) |
| 2072 { | 2122 { |
| 2073 oldBandE[c*nbEBands+i]=0; | 2123 oldBandE[c*nbEBands+i]=0; |
| 2074 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | 2124 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); |
| 2075 } | 2125 } |
| 2076 for (i=st->end;i<nbEBands;i++) | 2126 for (i=end;i<nbEBands;i++) |
| 2077 { | 2127 { |
| 2078 oldBandE[c*nbEBands+i]=0; | 2128 oldBandE[c*nbEBands+i]=0; |
| 2079 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | 2129 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); |
| 2080 } | 2130 } |
| 2081 } while (++c<CC); | 2131 } while (++c<CC); |
| 2082 | 2132 |
| 2083 if (isTransient || transient_got_disabled) | 2133 if (isTransient || transient_got_disabled) |
| 2084 st->consec_transient++; | 2134 st->consec_transient++; |
| 2085 else | 2135 else |
| 2086 st->consec_transient=0; | 2136 st->consec_transient=0; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2267 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: | 2317 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
| 2268 { | 2318 { |
| 2269 opus_int32 value = va_arg(ap, opus_int32); | 2319 opus_int32 value = va_arg(ap, opus_int32); |
| 2270 st->variable_duration = value; | 2320 st->variable_duration = value; |
| 2271 } | 2321 } |
| 2272 break; | 2322 break; |
| 2273 case OPUS_RESET_STATE: | 2323 case OPUS_RESET_STATE: |
| 2274 { | 2324 { |
| 2275 int i; | 2325 int i; |
| 2276 opus_val16 *oldBandE, *oldLogE, *oldLogE2; | 2326 opus_val16 *oldBandE, *oldLogE, *oldLogE2; |
| 2277 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTE
R_MAXPERIOD)); | 2327 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->mode->overlap+COM
BFILTER_MAXPERIOD)); |
| 2278 oldLogE = oldBandE + st->channels*st->mode->nbEBands; | 2328 oldLogE = oldBandE + st->channels*st->mode->nbEBands; |
| 2279 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands; | 2329 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands; |
| 2280 OPUS_CLEAR((char*)&st->ENCODER_RESET_START, | 2330 OPUS_CLEAR((char*)&st->ENCODER_RESET_START, |
| 2281 opus_custom_encoder_get_size(st->mode, st->channels)- | 2331 opus_custom_encoder_get_size(st->mode, st->channels)- |
| 2282 ((char*)&st->ENCODER_RESET_START - (char*)st)); | 2332 ((char*)&st->ENCODER_RESET_START - (char*)st)); |
| 2283 for (i=0;i<st->channels*st->mode->nbEBands;i++) | 2333 for (i=0;i<st->channels*st->mode->nbEBands;i++) |
| 2284 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); | 2334 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); |
| 2285 st->vbr_offset = 0; | 2335 st->vbr_offset = 0; |
| 2286 st->delayedIntra = 1; | 2336 st->delayedIntra = 1; |
| 2287 st->spread_decision = SPREAD_NORMAL; | 2337 st->spread_decision = SPREAD_NORMAL; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2344 } | 2394 } |
| 2345 va_end(ap); | 2395 va_end(ap); |
| 2346 return OPUS_OK; | 2396 return OPUS_OK; |
| 2347 bad_arg: | 2397 bad_arg: |
| 2348 va_end(ap); | 2398 va_end(ap); |
| 2349 return OPUS_BAD_ARG; | 2399 return OPUS_BAD_ARG; |
| 2350 bad_request: | 2400 bad_request: |
| 2351 va_end(ap); | 2401 va_end(ap); |
| 2352 return OPUS_UNIMPLEMENTED; | 2402 return OPUS_UNIMPLEMENTED; |
| 2353 } | 2403 } |
| OLD | NEW |