2
0

stereo_find_predictor.c 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main.h"
  31. /* Find least-squares prediction gain for one signal based on another and quantize it */
  32. opus_int32 silk_stereo_find_predictor( /* O Returns predictor in Q13 */
  33. opus_int32 *ratio_Q14, /* O Ratio of residual and mid energies */
  34. const opus_int16 x[], /* I Basis signal */
  35. const opus_int16 y[], /* I Target signal */
  36. opus_int32 mid_res_amp_Q0[], /* I/O Smoothed mid, residual norms */
  37. opus_int length, /* I Number of samples */
  38. opus_int smooth_coef_Q16 /* I Smoothing coefficient */
  39. )
  40. {
  41. opus_int scale, scale1, scale2;
  42. opus_int32 nrgx, nrgy, corr, pred_Q13, pred2_Q10;
  43. /* Find predictor */
  44. silk_sum_sqr_shift( &nrgx, &scale1, x, length );
  45. silk_sum_sqr_shift( &nrgy, &scale2, y, length );
  46. scale = silk_max_int( scale1, scale2 );
  47. scale = scale + ( scale & 1 ); /* make even */
  48. nrgy = silk_RSHIFT32( nrgy, scale - scale2 );
  49. nrgx = silk_RSHIFT32( nrgx, scale - scale1 );
  50. nrgx = silk_max_int( nrgx, 1 );
  51. corr = silk_inner_prod_aligned_scale( x, y, scale, length );
  52. pred_Q13 = silk_DIV32_varQ( corr, nrgx, 13 );
  53. pred_Q13 = silk_LIMIT( pred_Q13, -(1 << 14), 1 << 14 );
  54. pred2_Q10 = silk_SMULWB( pred_Q13, pred_Q13 );
  55. /* Faster update for signals with large prediction parameters */
  56. smooth_coef_Q16 = (opus_int)silk_max_int( smooth_coef_Q16, silk_abs( pred2_Q10 ) );
  57. /* Smoothed mid and residual norms */
  58. silk_assert( smooth_coef_Q16 < 32768 );
  59. scale = silk_RSHIFT( scale, 1 );
  60. mid_res_amp_Q0[ 0 ] = silk_SMLAWB( mid_res_amp_Q0[ 0 ], silk_LSHIFT( silk_SQRT_APPROX( nrgx ), scale ) - mid_res_amp_Q0[ 0 ],
  61. smooth_coef_Q16 );
  62. /* Residual energy = nrgy - 2 * pred * corr + pred^2 * nrgx */
  63. nrgy = silk_SUB_LSHIFT32( nrgy, silk_SMULWB( corr, pred_Q13 ), 3 + 1 );
  64. nrgy = silk_ADD_LSHIFT32( nrgy, silk_SMULWB( nrgx, pred2_Q10 ), 6 );
  65. mid_res_amp_Q0[ 1 ] = silk_SMLAWB( mid_res_amp_Q0[ 1 ], silk_LSHIFT( silk_SQRT_APPROX( nrgy ), scale ) - mid_res_amp_Q0[ 1 ],
  66. smooth_coef_Q16 );
  67. /* Ratio of smoothed residual and mid norms */
  68. *ratio_Q14 = silk_DIV32_varQ( mid_res_amp_Q0[ 1 ], silk_max( mid_res_amp_Q0[ 0 ], 1 ), 14 );
  69. *ratio_Q14 = silk_LIMIT( *ratio_Q14, 0, 32767 );
  70. return pred_Q13;
  71. }