LCOV - code coverage report
Current view: top level - lib_enc - ivas_sba_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 74 82 90.2 %
Date: 2025-05-23 08:37:30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include <math.h>
      36             : #include "options.h"
      37             : #include "cnst.h"
      38             : #include "ivas_cnst.h"
      39             : #include "rom_com.h"
      40             : #include "prot.h"
      41             : #include "ivas_prot.h"
      42             : #include "ivas_rom_com.h"
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * ivas_sba_getTCs()
      51             :  *
      52             :  * Get TCs from Ambisonics signal in ACN
      53             :  *-------------------------------------------------------------------*/
      54             : 
      55      144580 : void ivas_sba_getTCs(
      56             :     float *sba_data[],        /* i  : SBA signals                  */
      57             :     Encoder_Struct *st_ivas,  /* i/o: Encoder struct               */
      58             :     const int16_t input_frame /* i  : frame length                 */
      59             : )
      60             : {
      61      144580 :     if ( st_ivas->hEncoderConfig->sba_planar )
      62             :     {
      63       10250 :         ivas_sba_zero_vert_comp( sba_data, st_ivas->sba_analysis_order, st_ivas->hEncoderConfig->sba_planar, input_frame );
      64             :     }
      65             : 
      66      144580 :     st_ivas->nchan_transport = ivas_get_sba_num_TCs( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->sba_analysis_order );
      67             : 
      68      144580 :     if ( st_ivas->nchan_transport >= 3 )
      69             :     {
      70             :         /*convert WYZX downmix to WYXZ*/
      71       72319 :         int16_t i = 0;
      72             :         float temp;
      73    59223359 :         for ( i = 0; i < input_frame; i++ )
      74             :         {
      75    59151040 :             temp = sba_data[2][i];
      76    59151040 :             sba_data[2][i] = sba_data[3][i];
      77    59151040 :             sba_data[3][i] = temp;
      78             :         }
      79             :     }
      80             : 
      81             : #ifdef DEBUG_MODE_DIRAC
      82             :     for ( int16_t n = 0; n < st_ivas->nchan_transport; n++ )
      83             :     {
      84             :         int16_t tmp[L_FRAME48k];
      85             :         char file_name[50] = { 0 };
      86             : 
      87             :         sprintf( file_name, "./res/ivas_dirac_enc_%d.%d.pcm", n, (int16_t) ( input_frame * 0.05 ) );
      88             :         for ( int16_t i = 0; i < input_frame; i++ )
      89             :         {
      90             :             tmp[i] = (int16_t) ( sba_data[n][i] + 0.5f );
      91             :         }
      92             :         dbgwrite( tmp, sizeof( int16_t ), input_frame, 1, file_name );
      93             :     }
      94             : #endif
      95             : 
      96      144580 :     return;
      97             : }
      98             : 
      99             : /*-------------------------------------------------------------------*
     100             :  * ivas_sba_enc_reconfigure()
     101             :  *
     102             :  * Reconfigure IVAS SBA encoder
     103             :  *-------------------------------------------------------------------*/
     104             : 
     105      122500 : ivas_error ivas_sba_enc_reconfigure(
     106             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder structure      */
     107             : )
     108             : {
     109             :     int16_t n, nSCE_old, nCPE_old, nchan_transport_old;
     110             :     int32_t ivas_total_brate;
     111             :     ivas_error error;
     112             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     113             : 
     114      122500 :     error = IVAS_ERR_OK;
     115      122500 :     hEncoderConfig = st_ivas->hEncoderConfig;
     116      122500 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     117             : 
     118      122500 :     if ( ivas_total_brate != hEncoderConfig->last_ivas_total_brate )
     119             :     {
     120        1020 :         DIRAC_ENC_HANDLE hDirAC = st_ivas->hDirAC;
     121             :         SPAR_ENC_HANDLE hSpar;
     122             :         int16_t analysis_order_old;
     123             :         int16_t spar_reconfig_flag;
     124             :         int16_t nbands_old;
     125             :         int16_t ndir_old;
     126             : 
     127        1020 :         spar_reconfig_flag = 0;
     128        1020 :         nchan_transport_old = st_ivas->nchan_transport;
     129        1020 :         nCPE_old = st_ivas->nCPE;
     130        1020 :         nSCE_old = st_ivas->nSCE;
     131        1020 :         st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, hEncoderConfig->sba_order );
     132        1020 :         analysis_order_old = ivas_sba_get_analysis_order( hEncoderConfig->last_ivas_total_brate, hEncoderConfig->sba_order );
     133        1020 :         nbands_old = st_ivas->hQMetaData->q_direction->cfg.nbands;
     134        1020 :         ndir_old = st_ivas->hQMetaData->no_directions;
     135             : 
     136        1020 :         if ( analysis_order_old != st_ivas->sba_analysis_order )
     137             :         {
     138             :             int16_t i, n_old;
     139             :             float **old_mem_hp20_in;
     140             : 
     141         241 :             n_old = ( analysis_order_old + 1 ) * ( analysis_order_old + 1 );
     142         241 :             n = ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
     143             : 
     144         241 :             if ( n > n_old )
     145             :             {
     146             :                 /* save old mem_hp_20 pointer */
     147         124 :                 old_mem_hp20_in = st_ivas->mem_hp20_in;
     148         124 :                 st_ivas->mem_hp20_in = NULL;
     149             : 
     150         124 :                 if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
     151             :                 {
     152           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
     153             :                 }
     154             : 
     155         620 :                 for ( i = 0; i < n_old; i++ )
     156             :                 {
     157         496 :                     st_ivas->mem_hp20_in[i] = old_mem_hp20_in[i];
     158         496 :                     old_mem_hp20_in[i] = NULL;
     159             :                 }
     160             :                 /* create additional hp20 memories */
     161        1479 :                 for ( ; i < n; i++ )
     162             :                 {
     163        1355 :                     if ( ( st_ivas->mem_hp20_in[i] = (float *) malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
     164             :                     {
     165           0 :                         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
     166             :                     }
     167             : 
     168        1355 :                     set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
     169             :                 }
     170             : 
     171         124 :                 free( old_mem_hp20_in );
     172         124 :                 old_mem_hp20_in = NULL;
     173             :             }
     174         117 :             else if ( n < n_old )
     175             :             {
     176             :                 /* save old mem_hp_20 pointer */
     177         117 :                 old_mem_hp20_in = st_ivas->mem_hp20_in;
     178         117 :                 st_ivas->mem_hp20_in = NULL;
     179             : 
     180         117 :                 if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
     181             :                 {
     182           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
     183             :                 }
     184             : 
     185         585 :                 for ( i = 0; i < n; i++ )
     186             :                 {
     187         468 :                     st_ivas->mem_hp20_in[i] = old_mem_hp20_in[i];
     188         468 :                     old_mem_hp20_in[i] = NULL;
     189             :                 }
     190             :                 /* remove superfluous hp20 memories */
     191        1395 :                 for ( ; i < n_old; i++ )
     192             :                 {
     193        1278 :                     free( old_mem_hp20_in[i] );
     194        1278 :                     old_mem_hp20_in[i] = NULL;
     195             :                 }
     196             : 
     197         117 :                 free( old_mem_hp20_in );
     198         117 :                 old_mem_hp20_in = NULL;
     199             :             }
     200             :         }
     201             : 
     202        1020 :         ivas_spar_config( ivas_total_brate, min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ), &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &st_ivas->hSpar->core_nominal_brate, -1 );
     203             : 
     204        1020 :         hSpar = st_ivas->hSpar;
     205             : 
     206        1020 :         if ( st_ivas->nchan_transport == 1 )
     207             :         {
     208         372 :             hEncoderConfig->element_mode_init = IVAS_SCE;
     209             :         }
     210             :         else
     211             :         {
     212         648 :             hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     213             :         }
     214             : 
     215        1020 :         if ( nchan_transport_old != st_ivas->nchan_transport || ( ivas_total_brate < IVAS_512k && hEncoderConfig->last_ivas_total_brate >= IVAS_512k ) || ( ivas_total_brate >= IVAS_512k && hEncoderConfig->last_ivas_total_brate < IVAS_512k ) )
     216             :         {
     217             :             /* FB mixer handle */
     218         815 :             if ( hDirAC->hFbMixer != NULL )
     219             :             {
     220           0 :                 ivas_FB_mixer_close( &( hDirAC->hFbMixer ), hEncoderConfig->input_Fs, 0 );
     221           0 :                 hDirAC->hFbMixer = NULL;
     222             :             }
     223         815 :             spar_reconfig_flag = 1;
     224         815 :             ivas_spar_enc_close( &( st_ivas->hSpar ), hEncoderConfig->input_Fs, hEncoderConfig->nchan_inp, spar_reconfig_flag );
     225             : 
     226         815 :             if ( ( error = ivas_spar_enc_open( st_ivas, spar_reconfig_flag ) ) != IVAS_ERR_OK )
     227             :             {
     228           0 :                 return error;
     229             :             }
     230             :         }
     231             : 
     232        1020 :         st_ivas->hSpar->spar_reconfig_flag = spar_reconfig_flag;
     233             : 
     234        1020 :         if ( ( error = ivas_dirac_enc_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
     235             :         {
     236           0 :             return error;
     237             :         }
     238             : 
     239        1020 :         if ( st_ivas->hQMetaData->q_direction->cfg.nbands != nbands_old || st_ivas->hQMetaData->no_directions != ndir_old )
     240             :         {
     241             :             int16_t dir, j, i;
     242         522 :             IVAS_QDIRECTION *q_direction = st_ivas->hQMetaData->q_direction;
     243        1130 :             for ( dir = 0; dir < st_ivas->hQMetaData->no_directions; dir++ )
     244             :             {
     245        3602 :                 for ( j = 0; j < q_direction[dir].cfg.nbands; j++ )
     246             :                 {
     247       14970 :                     for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
     248             :                     {
     249       11976 :                         q_direction[dir].band_data[j].energy_ratio_index[i] = 0;
     250       11976 :                         q_direction[dir].band_data[j].energy_ratio_index_mod[i] = 0;
     251             :                     }
     252             :                 }
     253             :             }
     254             :         }
     255        1020 :         hSpar->enc_param_start_band = hDirAC->hConfig->enc_param_start_band;
     256             : 
     257             :         /*-----------------------------------------------------------------*
     258             :          * Allocate, initialize, and configure SCE/CPE/MCT handles
     259             :          *-----------------------------------------------------------------*/
     260             : 
     261        1020 :         if ( ( error = ivas_corecoder_enc_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, ivas_total_brate / st_ivas->nchan_transport, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS, MC_MODE_NONE ) ) != IVAS_ERR_OK )
     262             :         {
     263           0 :             return error;
     264             :         }
     265             :     }
     266             : 
     267      122500 :     return error;
     268             : }

Generated by: LCOV version 1.14