Coverage Report

Created: 2025-07-23 07:59

/src/opus/silk/stereo_find_predictor.c
Line
Count
Source
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
28
#ifdef HAVE_CONFIG_H
29
#include "config.h"
30
#endif
31
32
#include "main.h"
33
34
/* Find least-squares prediction gain for one signal based on another and quantize it */
35
opus_int32 silk_stereo_find_predictor(                          /* O    Returns predictor in Q13                    */
36
    opus_int32                  *ratio_Q14,                     /* O    Ratio of residual and mid energies          */
37
    const opus_int16            x[],                            /* I    Basis signal                                */
38
    const opus_int16            y[],                            /* I    Target signal                               */
39
    opus_int32                  mid_res_amp_Q0[],               /* I/O  Smoothed mid, residual norms                */
40
    opus_int                    length,                         /* I    Number of samples                           */
41
    opus_int                    smooth_coef_Q16                 /* I    Smoothing coefficient                       */
42
)
43
274k
{
44
274k
    opus_int   scale, scale1, scale2;
45
274k
    opus_int32 nrgx, nrgy, corr, pred_Q13, pred2_Q10;
46
47
    /* Find  predictor */
48
274k
    silk_sum_sqr_shift( &nrgx, &scale1, x, length );
49
274k
    silk_sum_sqr_shift( &nrgy, &scale2, y, length );
50
274k
    scale = silk_max_int( scale1, scale2 );
51
274k
    scale = scale + ( scale & 1 );          /* make even */
52
274k
    nrgy = silk_RSHIFT32( nrgy, scale - scale2 );
53
274k
    nrgx = silk_RSHIFT32( nrgx, scale - scale1 );
54
274k
    nrgx = silk_max_int( nrgx, 1 );
55
274k
    corr = silk_inner_prod_aligned_scale( x, y, scale, length );
56
274k
    pred_Q13 = silk_DIV32_varQ( corr, nrgx, 13 );
57
274k
    pred_Q13 = silk_LIMIT( pred_Q13, -(1 << 14), 1 << 14 );
58
274k
    pred2_Q10 = silk_SMULWB( pred_Q13, pred_Q13 );
59
60
    /* Faster update for signals with large prediction parameters */
61
274k
    smooth_coef_Q16 = (opus_int)silk_max_int( smooth_coef_Q16, silk_abs( pred2_Q10 ) );
62
63
    /* Smoothed mid and residual norms */
64
274k
    silk_assert( smooth_coef_Q16 < 32768 );
65
274k
    scale = silk_RSHIFT( scale, 1 );
66
274k
    mid_res_amp_Q0[ 0 ] = silk_SMLAWB( mid_res_amp_Q0[ 0 ], silk_LSHIFT( silk_SQRT_APPROX( nrgx ), scale ) - mid_res_amp_Q0[ 0 ],
67
274k
        smooth_coef_Q16 );
68
    /* Residual energy = nrgy - 2 * pred * corr + pred^2 * nrgx */
69
274k
    nrgy = silk_SUB_LSHIFT32( nrgy, silk_SMULWB( corr, pred_Q13 ), 3 + 1 );
70
274k
    nrgy = silk_ADD_LSHIFT32( nrgy, silk_SMULWB( nrgx, pred2_Q10 ), 6 );
71
274k
    mid_res_amp_Q0[ 1 ] = silk_SMLAWB( mid_res_amp_Q0[ 1 ], silk_LSHIFT( silk_SQRT_APPROX( nrgy ), scale ) - mid_res_amp_Q0[ 1 ],
72
274k
        smooth_coef_Q16 );
73
74
    /* Ratio of smoothed residual and mid norms */
75
274k
    *ratio_Q14 = silk_DIV32_varQ( mid_res_amp_Q0[ 1 ], silk_max( mid_res_amp_Q0[ 0 ], 1 ), 14 );
76
274k
    *ratio_Q14 = silk_LIMIT( *ratio_Q14, 0, 32767 );
77
78
274k
    return pred_Q13;
79
274k
}