LCOV - code coverage report
Current view: top level - libavcodec - sbrdsp_fixed.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 160 169 94.7 %
Date: 2017-12-13 10:57:33 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :  * AAC Spectral Band Replication decoding functions
       3             :  * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
       4             :  * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  *
      22             :  * Note: Rounding-to-nearest used unless otherwise stated
      23             :  *
      24             :  */
      25             : 
      26             : #define USE_FIXED 1
      27             : 
      28             : #include "aac.h"
      29             : #include "config.h"
      30             : #include "libavutil/attributes.h"
      31             : #include "libavutil/intfloat.h"
      32             : #include "sbrdsp.h"
      33             : 
      34      127837 : static SoftFloat sbr_sum_square_c(int (*x)[2], int n)
      35             : {
      36             :     SoftFloat ret;
      37             :     uint64_t accu, round;
      38      127837 :     uint64_t accu0 = 0, accu1 = 0, accu2 = 0, accu3 = 0;
      39             :     int i, nz, nz0;
      40             :     unsigned u;
      41             : 
      42     1483629 :     for (i = 0; i < n; i += 2) {
      43             :         // Larger values are inavlid and could cause overflows of accu.
      44             :         av_assert2(FFABS(x[i + 0][0]) >> 30 == 0);
      45     1355792 :         accu0 += (int64_t)x[i + 0][0] * x[i + 0][0];
      46             :         av_assert2(FFABS(x[i + 0][1]) >> 30 == 0);
      47     1355792 :         accu1 += (int64_t)x[i + 0][1] * x[i + 0][1];
      48             :         av_assert2(FFABS(x[i + 1][0]) >> 30 == 0);
      49     1355792 :         accu2 += (int64_t)x[i + 1][0] * x[i + 1][0];
      50             :         av_assert2(FFABS(x[i + 1][1]) >> 30 == 0);
      51     1355792 :         accu3 += (int64_t)x[i + 1][1] * x[i + 1][1];
      52             :     }
      53             : 
      54      127837 :     nz0 = 15;
      55      255674 :     while ((accu0|accu1|accu2|accu3) >> 62) {
      56           0 :         accu0 >>= 1;
      57           0 :         accu1 >>= 1;
      58           0 :         accu2 >>= 1;
      59           0 :         accu3 >>= 1;
      60           0 :         nz0 --;
      61             :     }
      62      127837 :     accu = accu0 + accu1 + accu2 + accu3;
      63             : 
      64      127837 :     u = accu >> 32;
      65      127837 :     if (u) {
      66      120661 :         nz = 33;
      67     2727796 :         while (u < 0x80000000U) {
      68     2486474 :             u <<= 1;
      69     2486474 :             nz--;
      70             :         }
      71             :     } else
      72        7176 :         nz = 1;
      73             : 
      74      127837 :     round = 1ULL << (nz-1);
      75      127837 :     u = ((accu + round) >> nz);
      76      127837 :     u >>= 1;
      77      127837 :     ret = av_int2sf(u, nz0 - nz);
      78             : 
      79      127837 :     return ret;
      80             : }
      81             : 
      82      193024 : static void sbr_neg_odd_64_c(int *x)
      83             : {
      84             :     int i;
      85     6369792 :     for (i = 1; i < 64; i += 2)
      86     6176768 :         x[i] = -x[i];
      87      193024 : }
      88             : 
      89      266496 : static void sbr_qmf_pre_shuffle_c(int *z)
      90             : {
      91             :     int k;
      92      266496 :     z[64] = z[0];
      93      266496 :     z[65] = z[1];
      94     8527872 :     for (k = 1; k < 32; k++) {
      95     8261376 :         z[64+2*k  ] = -z[64 - k];
      96     8261376 :         z[64+2*k+1] =  z[ k + 1];
      97             :     }
      98      266496 : }
      99             : 
     100      266496 : static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
     101             : {
     102             :     int k;
     103     8794368 :     for (k = 0; k < 32; k++) {
     104     8527872 :         W[k][0] = -z[63-k];
     105     8527872 :         W[k][1] = z[k];
     106             :     }
     107      266496 : }
     108             : 
     109       73472 : static void sbr_qmf_deint_neg_c(int *v, const int *src)
     110             : {
     111             :     int i;
     112     2424576 :     for (i = 0; i < 32; i++) {
     113     2351104 :         v[     i] = ( src[63 - 2*i    ] + 0x10) >> 5;
     114     2351104 :         v[63 - i] = (-src[63 - 2*i - 1] + 0x10) >> 5;
     115             :     }
     116       73472 : }
     117             : 
     118      544784 : static av_always_inline SoftFloat autocorr_calc(int64_t accu)
     119             : {
     120             :         int nz, mant, expo;
     121             :         unsigned round;
     122      544784 :         int i = (int)(accu >> 32);
     123      544784 :         if (i == 0) {
     124       29514 :             nz = 1;
     125             :         } else {
     126      515270 :             nz = 0;
     127    10410370 :             while (FFABS(i) < 0x40000000) {
     128     9379830 :                 i *= 2;
     129     9379830 :                 nz++;
     130             :             }
     131      515270 :             nz = 32-nz;
     132             :         }
     133             : 
     134      544784 :         round = 1U << (nz-1);
     135      544784 :         mant = (int)((accu + round) >> nz);
     136      544784 :         mant = (mant + 0x40LL)>>7;
     137      544784 :         mant *= 64;
     138      544784 :         expo = nz + 15;
     139      544784 :         return av_int2sf(mant, 30 - expo);
     140             : }
     141             : 
     142      204294 : static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
     143             : {
     144             :     int i;
     145             :     int64_t real_sum, imag_sum;
     146      204294 :     int64_t accu_re = 0, accu_im = 0;
     147             : 
     148      204294 :     if (lag) {
     149     5175448 :         for (i = 1; i < 38; i++) {
     150     5039252 :             accu_re += (uint64_t)x[i][0] * x[i+lag][0];
     151     5039252 :             accu_re += (uint64_t)x[i][1] * x[i+lag][1];
     152     5039252 :             accu_im += (uint64_t)x[i][0] * x[i+lag][1];
     153     5039252 :             accu_im -= (uint64_t)x[i][1] * x[i+lag][0];
     154             :         }
     155             : 
     156      136196 :         real_sum = accu_re;
     157      136196 :         imag_sum = accu_im;
     158             : 
     159      136196 :         accu_re += (uint64_t)x[ 0][0] * x[lag][0];
     160      136196 :         accu_re += (uint64_t)x[ 0][1] * x[lag][1];
     161      136196 :         accu_im += (uint64_t)x[ 0][0] * x[lag][1];
     162      136196 :         accu_im -= (uint64_t)x[ 0][1] * x[lag][0];
     163             : 
     164      136196 :         phi[2-lag][1][0] = autocorr_calc(accu_re);
     165      136196 :         phi[2-lag][1][1] = autocorr_calc(accu_im);
     166             : 
     167      136196 :         if (lag == 1) {
     168       68098 :             accu_re = real_sum;
     169       68098 :             accu_im = imag_sum;
     170       68098 :             accu_re += (uint64_t)x[38][0] * x[39][0];
     171       68098 :             accu_re += (uint64_t)x[38][1] * x[39][1];
     172       68098 :             accu_im += (uint64_t)x[38][0] * x[39][1];
     173       68098 :             accu_im -= (uint64_t)x[38][1] * x[39][0];
     174             : 
     175       68098 :             phi[0][0][0] = autocorr_calc(accu_re);
     176       68098 :             phi[0][0][1] = autocorr_calc(accu_im);
     177             :         }
     178             :     } else {
     179     2587724 :         for (i = 1; i < 38; i++) {
     180     2519626 :             accu_re += (uint64_t)x[i][0] * x[i][0];
     181     2519626 :             accu_re += (uint64_t)x[i][1] * x[i][1];
     182             :         }
     183       68098 :         real_sum = accu_re;
     184       68098 :         accu_re += (uint64_t)x[ 0][0] * x[ 0][0];
     185       68098 :         accu_re += (uint64_t)x[ 0][1] * x[ 0][1];
     186             : 
     187       68098 :         phi[2][1][0] = autocorr_calc(accu_re);
     188             : 
     189       68098 :         accu_re = real_sum;
     190       68098 :         accu_re += (uint64_t)x[38][0] * x[38][0];
     191       68098 :         accu_re += (uint64_t)x[38][1] * x[38][1];
     192             : 
     193       68098 :         phi[1][0][0] = autocorr_calc(accu_re);
     194             :     }
     195      204294 : }
     196             : 
     197       68098 : static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
     198             : {
     199       68098 :     autocorrelate(x, phi, 0);
     200       68098 :     autocorrelate(x, phi, 1);
     201       68098 :     autocorrelate(x, phi, 2);
     202       68098 : }
     203             : 
     204       84737 : static void sbr_hf_gen_c(int (*X_high)[2], const int (*X_low)[2],
     205             :                        const int alpha0[2], const int alpha1[2],
     206             :                        int bw, int start, int end)
     207             : {
     208             :     int alpha[4];
     209             :     int i;
     210             :     int64_t accu;
     211             : 
     212       84737 :     accu = (int64_t)alpha0[0] * bw;
     213       84737 :     alpha[2] = (int)((accu + 0x40000000) >> 31);
     214       84737 :     accu = (int64_t)alpha0[1] * bw;
     215       84737 :     alpha[3] = (int)((accu + 0x40000000) >> 31);
     216       84737 :     accu = (int64_t)bw * bw;
     217       84737 :     bw = (int)((accu + 0x40000000) >> 31);
     218       84737 :     accu = (int64_t)alpha1[0] * bw;
     219       84737 :     alpha[0] = (int)((accu + 0x40000000) >> 31);
     220       84737 :     accu = (int64_t)alpha1[1] * bw;
     221       84737 :     alpha[1] = (int)((accu + 0x40000000) >> 31);
     222             : 
     223     2796321 :     for (i = start; i < end; i++) {
     224     2711584 :         accu  = (int64_t)X_low[i][0] * 0x20000000;
     225     2711584 :         accu += (int64_t)X_low[i - 2][0] * alpha[0];
     226     2711584 :         accu -= (int64_t)X_low[i - 2][1] * alpha[1];
     227     2711584 :         accu += (int64_t)X_low[i - 1][0] * alpha[2];
     228     2711584 :         accu -= (int64_t)X_low[i - 1][1] * alpha[3];
     229     2711584 :         X_high[i][0] = (int)((accu + 0x10000000) >> 29);
     230             : 
     231     2711584 :         accu  = (int64_t)X_low[i][1] * 0x20000000;
     232     2711584 :         accu += (int64_t)X_low[i - 2][1] * alpha[0];
     233     2711584 :         accu += (int64_t)X_low[i - 2][0] * alpha[1];
     234     2711584 :         accu += (int64_t)X_low[i - 1][1] * alpha[2];
     235     2711584 :         accu += (int64_t)X_low[i - 1][0] * alpha[3];
     236     2711584 :         X_high[i][1] = (int)((accu + 0x10000000) >> 29);
     237             :     }
     238       84737 : }
     239             : 
     240      121184 : static void sbr_hf_g_filt_c(int (*Y)[2], const int (*X_high)[40][2],
     241             :                           const SoftFloat *g_filt, int m_max, intptr_t ixh)
     242             : {
     243             :     int m;
     244             :     int64_t accu;
     245             : 
     246     2832768 :     for (m = 0; m < m_max; m++) {
     247     2711584 :         if (22 - g_filt[m].exp < 61) {
     248     2711584 :             int64_t r = 1LL << (22-g_filt[m].exp);
     249     2711584 :             accu = (int64_t)X_high[m][ixh][0] * ((g_filt[m].mant + 0x40)>>7);
     250     2711584 :             Y[m][0] = (int)((accu + r) >> (23-g_filt[m].exp));
     251             : 
     252     2711584 :             accu = (int64_t)X_high[m][ixh][1] * ((g_filt[m].mant + 0x40)>>7);
     253     2711584 :             Y[m][1] = (int)((accu + r) >> (23-g_filt[m].exp));
     254             :         }
     255             :     }
     256      121184 : }
     257             : 
     258      119686 : static av_always_inline int sbr_hf_apply_noise(int (*Y)[2],
     259             :                                                 const SoftFloat *s_m,
     260             :                                                 const SoftFloat *q_filt,
     261             :                                                 int noise,
     262             :                                                 int phi_sign0,
     263             :                                                 int phi_sign1,
     264             :                                                 int m_max)
     265             : {
     266             :     int m;
     267             : 
     268     2795466 :     for (m = 0; m < m_max; m++) {
     269     2675780 :         unsigned y0 = Y[m][0];
     270     2675780 :         unsigned y1 = Y[m][1];
     271     2675780 :         noise = (noise + 1) & 0x1ff;
     272     2675780 :         if (s_m[m].mant) {
     273             :             int shift, round;
     274             : 
     275       25746 :             shift = 22 - s_m[m].exp;
     276       25746 :             if (shift < 1) {
     277           0 :                 av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
     278           0 :                 return AVERROR(ERANGE);
     279       25746 :             } else if (shift < 30) {
     280       25746 :                 round = 1 << (shift-1);
     281       25746 :                 y0 += (s_m[m].mant * phi_sign0 + round) >> shift;
     282       25746 :                 y1 += (s_m[m].mant * phi_sign1 + round) >> shift;
     283             :             }
     284             :         } else {
     285             :             int shift, round, tmp;
     286             :             int64_t accu;
     287             : 
     288     2650034 :             shift = 22 - q_filt[m].exp;
     289     2650034 :             if (shift < 1) {
     290           0 :                 av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
     291           0 :                 return AVERROR(ERANGE);
     292     2650034 :             } else if (shift < 30) {
     293     2650034 :                 round = 1 << (shift-1);
     294             : 
     295     2650034 :                 accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][0];
     296     2650034 :                 tmp = (int)((accu + 0x40000000) >> 31);
     297     2650034 :                 y0 += (tmp + round) >> shift;
     298             : 
     299     2650034 :                 accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][1];
     300     2650034 :                 tmp = (int)((accu + 0x40000000) >> 31);
     301     2650034 :                 y1 += (tmp + round) >> shift;
     302             :             }
     303             :         }
     304     2675780 :         Y[m][0] = y0;
     305     2675780 :         Y[m][1] = y1;
     306     2675780 :         phi_sign1 = -phi_sign1;
     307             :     }
     308      119686 :     return 0;
     309             : }
     310             : 
     311             : #include "sbrdsp_template.c"

Generated by: LCOV version 1.13