LCOV - code coverage report
Current view: top level - lib_enc - ivas_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 217 260 83.5 %
Date: 2025-09-14 03:13:15 Functions: 1 1 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 <stdint.h>
      34             : #include <math.h>
      35             : #include "options.h"
      36             : #include "cnst.h"
      37             : #include "ivas_cnst.h"
      38             : #include "rom_com.h"
      39             : #include "prot_fx.h"
      40             : #include "ivas_rom_com.h"
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : #include "ivas_prot_fx.h"
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * ivas_enc()
      49             :  *
      50             :  * Principal IVAS encoder routine
      51             :  *-------------------------------------------------------------------*/
      52      424288 : ivas_error ivas_enc_fx(
      53             :     Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure                     */
      54             :     const Word16 *data,      /* i  : input signal                                       Q0*/
      55             :     const Word16 n_samples   /* i  : number of input samples            Q0*/
      56             : )
      57             : {
      58             :     Word16 i, n, input_frame, n_samples_chan, nchan_inp /*, scale*/;
      59             :     Word32 input_Fs;
      60             :     IVAS_FORMAT ivas_format;
      61             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
      62             :     BSTR_ENC_HANDLE hMetaData;
      63             :     Word16 nb_bits_metadata[MAX_SCE + 1];
      64             :     Word32 *data_fx[MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS];
      65             :     Word32 ivas_total_brate;
      66             :     ivas_error error;
      67      424288 :     error = IVAS_ERR_OK;
      68      424288 :     move32();
      69             : 
      70      424288 :     push_wmops( "ivas_enc" );
      71             : 
      72             :     /*------------------------------------------------------------------*
      73             :      * Initialization - general
      74             :      *-----------------------------------------------------------------*/
      75             : 
      76      424288 :     hEncoderConfig = st_ivas->hEncoderConfig;
      77             : 
      78      424288 :     input_Fs = hEncoderConfig->input_Fs; /* Q0 */
      79      424288 :     ivas_format = hEncoderConfig->ivas_format;
      80      424288 :     nchan_inp = hEncoderConfig->nchan_inp;               /* Q0 */
      81      424288 :     ivas_total_brate = hEncoderConfig->ivas_total_brate; /* Q0 */
      82      424288 :     move32();
      83      424288 :     move32();
      84      424288 :     move16();
      85      424288 :     move32();
      86             : 
      87      424288 :     input_frame = extract_l( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) );
      88      424288 :     IF( NE_16( nchan_inp, 1 ) )
      89             :     {
      90      390672 :         n_samples_chan = div_l( L_deposit_l( n_samples ), nchan_inp ); /* Q0 */
      91      390672 :         n_samples_chan = shl( n_samples_chan, 1 );                     /* Q0 */
      92             :     }
      93             :     ELSE
      94             :     {
      95       33616 :         n_samples_chan = n_samples; /* Q0 */
      96       33616 :         move16();
      97             :     }
      98      424288 :     set16_fx( nb_bits_metadata, 0, MAX_SCE + 1 );
      99             : 
     100             :     /*----------------------------------------------------------------*
     101             :      * convert 'Word16' input data to 'Word32' in Q11 format
     102             :      *----------------------------------------------------------------*/
     103             : 
     104     8910048 :     FOR( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
     105             :     {
     106     8485760 :         data_fx[n] = st_ivas->p_data_fx[n];
     107     8485760 :         IF( data_fx[n] )
     108             :         {
     109     2549310 :             Scale_sig32( data_fx[n], input_frame, sub( Q11, st_ivas->q_data_fx ) ); // Q11
     110             :         }
     111             :     }
     112      424288 :     st_ivas->q_data_fx = Q11;
     113      424288 :     move16();
     114      424288 :     n = 0;
     115      424288 :     move16();
     116     2961898 :     WHILE( LT_16( n, nchan_inp ) )
     117             :     {
     118  2082362250 :         FOR( i = 0; i < n_samples_chan; i++ )
     119             :         {
     120  2079824640 :             data_fx[n][i] = L_mult0( data[i * nchan_inp + n], shl( 1, st_ivas->q_data_fx ) );
     121  2079824640 :             move32();
     122             :         }
     123     2537610 :         n = add( n, 1 );
     124             :     }
     125             : 
     126      424288 :     IF( LT_16( n_samples_chan, input_frame ) )
     127             :     {
     128           0 :         FOR( n = 0; n < nchan_inp; n++ )
     129             :         {
     130           0 :             set32_fx( data_fx[n] + n_samples_chan, 0, sub( input_frame, n_samples_chan ) );
     131             :         }
     132             :     }
     133             : 
     134      424288 :     IF( EQ_32( ivas_format, SBA_FORMAT ) )
     135             :     {
     136      122500 :         IF( NE_32( ( error = ivas_sba_enc_reconfigure_fx( st_ivas ) ), IVAS_ERR_OK ) )
     137             :         {
     138           0 :             return error;
     139             :         }
     140             :     }
     141      301788 :     ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
     142             :     {
     143       37000 :         IF( ( error = ivas_osba_enc_reconfig( st_ivas ) ) != IVAS_ERR_OK )
     144             :         {
     145           0 :             return error;
     146             :         }
     147             :     }
     148             : 
     149             :     /*----------------------------------------------------------------*
     150             :      * HP filtering
     151             :      *----------------------------------------------------------------*/
     152             : 
     153      424288 :     n = getNumChanAnalysis_fx( st_ivas );
     154             : 
     155             :     /* bypass EVS coding in float precision, emulating EVS encoder/decoder delay */
     156     2240898 :     FOR( i = 0; i < n; i++ )
     157             :     {
     158     1816610 :         test();
     159     1816610 :         test();
     160     1816610 :         IF( ( EQ_32( ivas_format, SBA_FORMAT ) ) && !( GT_16( st_ivas->sba_analysis_order, 1 ) ) )
     161             :         {
     162      443080 :             hp20_fx_32( data_fx[HOA_keep_ind[st_ivas->hSpar->hMdEnc->HOA_md_ind[i]]], input_frame, st_ivas->mem_hp20_in_fx[i], input_Fs );
     163             :         }
     164     1373530 :         ELSE IF( !( EQ_32( ivas_format, MC_FORMAT ) && EQ_16( i, LFE_CHANNEL ) ) )
     165             :         {
     166     1324680 :             hp20_fx_32( data_fx[i], input_frame, st_ivas->mem_hp20_in_fx[i], input_Fs );
     167             :         }
     168             :     }
     169             : 
     170             :     /*----------------------------------------------------------------*
     171             :      * write IVAS format signaling
     172             :      *----------------------------------------------------------------*/
     173      424288 :     ivas_write_format_fx( st_ivas );
     174             : 
     175             :     /*----------------------------------------------------------------*
     176             :      * Encoding
     177             :      *----------------------------------------------------------------*/
     178      424288 :     test();
     179      424288 :     IF( EQ_32( ivas_format, STEREO_FORMAT ) )
     180             :     {
     181       72486 :         st_ivas->hCPE[0]->element_brate = ivas_total_brate; /* Q0 */
     182       72486 :         move32();
     183       72486 :         IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, 0 /* no metadata */, NULL ) ) != IVAS_ERR_OK )
     184             :         {
     185           0 :             return error;
     186             :         }
     187             :     }
     188      351802 :     ELSE IF( EQ_32( ivas_format, ISM_FORMAT ) )
     189             :     {
     190             :         /* select ISM format mode; reconfigure the ISM format encoder */
     191      103826 :         IF( ( error = ivas_ism_enc_config( st_ivas ) ) != IVAS_ERR_OK )
     192             :         {
     193           0 :             return error;
     194             :         }
     195             : 
     196      103826 :         IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
     197             :         {
     198       20720 :             ivas_param_ism_enc_fx( st_ivas, data_fx, input_frame );
     199             : 
     200             :             /* Stereo DMX generation */
     201       20720 :             ivas_param_ism_stereo_dmx_fx( st_ivas, data_fx, input_frame );
     202             : 
     203             :             /* Core coding of Stereo DMX */
     204       20720 :             IF( ( error = ivas_ism_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata, 0 ) ) != IVAS_ERR_OK )
     205             :             {
     206           0 :                 return error;
     207             :             }
     208             :         }
     209       83106 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_DISC ) )
     210             :         {
     211             :             /* Analysis, decision about bitrates per channel & core coding */
     212       83106 :             IF( ( error = ivas_ism_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata, 0 ) ) != IVAS_ERR_OK )
     213             :             {
     214           0 :                 return error;
     215             :             }
     216             :         }
     217             :     }
     218      247976 :     ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, MASA_FORMAT ) )
     219             :     {
     220             :         /* MASA configuration */
     221      153126 :         IF( EQ_32( ivas_format, MASA_FORMAT ) )
     222             :         {
     223       30626 :             ivas_masa_enc_reconfigure_fx( st_ivas );
     224             :         }
     225             : 
     226             :         /* SBA/MASA metadata encoding and SBA/MASA metadata bitstream writing */
     227             :         /* hMetaData = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData : st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData; */
     228      153126 :         IF( st_ivas->nSCE > 0 )
     229             :         {
     230       51371 :             hMetaData = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData;
     231             :         }
     232             :         ELSE
     233             :         {
     234      101755 :             hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
     235             :         }
     236             : 
     237      153126 :         test();
     238      153126 :         IF( st_ivas->hQMetaData != NULL && EQ_32( ivas_format, MASA_FORMAT ) )
     239             :         {
     240       30626 :             ivas_masa_estimate_energy_fx( st_ivas->hMasa, data_fx, input_frame, st_ivas->nchan_transport, st_ivas->q_data_fx ); /* energy-estimation uses TF-resolution: 4x24 */
     241             : 
     242       30626 :             IF( ( error = ivas_masa_enc_config_fx( st_ivas ) ) != IVAS_ERR_OK )
     243             :             {
     244           0 :                 return error;
     245             :             }
     246             : 
     247       30626 :             IF( EQ_16( st_ivas->nchan_transport, 2 ) )
     248             :             {
     249       16536 :                 IF( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, &nb_bits_metadata[0], st_ivas->nchan_transport, ivas_format, ivas_total_brate, hEncoderConfig->Opt_DTX_ON, st_ivas->hCPE[0]->element_mode,
     250             :                                                    ISM_MODE_NONE, -1, NULL, -1, NULL, 0, 0 ) ) != IVAS_ERR_OK )
     251             :                 {
     252           0 :                     return error;
     253             :                 }
     254             :             }
     255             :             ELSE
     256             :             {
     257       14090 :                 IF( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, &nb_bits_metadata[0], st_ivas->nchan_transport, ivas_format, ivas_total_brate, hEncoderConfig->Opt_DTX_ON, -1,
     258             :                                                    ISM_MODE_NONE, -1, NULL, -1, NULL, 0, 0 ) ) != IVAS_ERR_OK )
     259             :                 {
     260           0 :                     return error;
     261             :                 }
     262             :             }
     263             :         }
     264      122500 :         ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) )
     265             :         {
     266      122500 :             IF( ( error = ivas_spar_enc_fx( st_ivas, data_fx, input_frame, nb_bits_metadata, hMetaData ) ) != IVAS_ERR_OK )
     267             :             {
     268           0 :                 return error;
     269             :             }
     270             :         }
     271             : 
     272      153126 :         IF( EQ_32( ivas_format, SBA_FORMAT ) )
     273             :         {
     274      122500 :             ivas_sba_getTCs_fx( data_fx, st_ivas, input_frame );
     275             :         }
     276             : 
     277             :         /* core-coding of transport channels */
     278      153126 :         IF( EQ_16( st_ivas->nSCE, 1 ) )
     279             :         {
     280       51371 :             IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[0], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     281             :             {
     282           0 :                 return error;
     283             :             }
     284             :         }
     285      101755 :         ELSE IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
     286             :         {
     287       51516 :             IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0], NULL ) ) != IVAS_ERR_OK )
     288             :             {
     289           0 :                 return error;
     290             :             }
     291             :         }
     292       50239 :         ELSE IF( GT_16( st_ivas->nCPE, 1 ) ) /* FOA/HOA format */
     293             :         {
     294       50239 :             IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     295             :             {
     296           0 :                 return error;
     297             :             }
     298             :         }
     299             :     }
     300       94850 :     ELSE IF( EQ_32( ivas_format, MASA_ISM_FORMAT ) )
     301             :     {
     302             :         Word32 *data_separated_object_fx;
     303             :         Word16 q_data_separated_object;
     304             : 
     305             :         Word16 idx_separated_object;
     306             :         Word16 flag_omasa_ener_brate;
     307             : 
     308        9000 :         flag_omasa_ener_brate = 0;
     309        9000 :         move16();
     310             : 
     311             :         /* Stereo transport is used also with monoMASA, duplicate mono if monoMASA */
     312        9000 :         IF( EQ_16( sub( st_ivas->hEncoderConfig->nchan_inp, hEncoderConfig->nchan_ism ), 1 ) )
     313             :         {
     314        2700 :             v_multc_fixed( data_fx[hEncoderConfig->nchan_ism], L_deposit_h( INV_SQRT2_FX_Q15 ) /* Q31 */, data_fx[hEncoderConfig->nchan_ism], input_frame );
     315        2700 :             Copy32( data_fx[hEncoderConfig->nchan_ism], data_fx[hEncoderConfig->nchan_ism + 1], input_frame ); /*  st_ivas->q_data_fx */
     316             :         }
     317             : 
     318             :         /* Estimate TF-tile energy for the input MASA stream */
     319        9000 :         ivas_masa_estimate_energy_fx( st_ivas->hMasa, &( data_fx[hEncoderConfig->nchan_ism] ), input_frame, st_ivas->nchan_transport, st_ivas->q_data_fx );
     320             : 
     321        9000 :         IF( ( error = ivas_omasa_enc_config_fx( st_ivas ) ) != IVAS_ERR_OK )
     322             :         {
     323           0 :             return error;
     324             :         }
     325             : 
     326        9000 :         set16_fx( nb_bits_metadata, 0, MAX_SCE + 1 );
     327        9000 :         idx_separated_object = 0;
     328        9000 :         move16();
     329             : 
     330        9000 :         data_separated_object_fx = data_fx[hEncoderConfig->nchan_ism + CPE_CHANNELS]; // Q(st_ivas->q_data_fx)
     331        9000 :         q_data_separated_object = st_ivas->q_data_fx;
     332        9000 :         move16();
     333             : 
     334             :         /* put audio object data in SCE's */
     335        9000 :         IF( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     336             :         {
     337             :             /* Scaling data_fx */
     338             :             {
     339        5858 :                 Word16 norm_data_in = MAX16B;
     340        5858 :                 move16();
     341       37014 :                 FOR( i = 0; i < hEncoderConfig->nchan_ism + st_ivas->nchan_transport; i++ )
     342             :                 {
     343       31156 :                     norm_data_in = s_min( norm_data_in, L_norm_arr( data_fx[i], input_frame ) );
     344             :                 }
     345        5858 :                 IF( LT_16( norm_data_in, 31 ) )
     346             :                 {
     347        5858 :                     norm_data_in = sub( norm_data_in, 7 );    /*guard bit is 4->to handle overflow in cldfbAnalysis*/
     348        5858 :                     norm_data_in = s_min( norm_data_in, 20 ); // limit Q to 31 (11 + norm)
     349       37014 :                     FOR( i = 0; i < hEncoderConfig->nchan_ism + st_ivas->nchan_transport; i++ )
     350             :                     {
     351       31156 :                         scale_sig32( data_fx[i], input_frame, norm_data_in ); /*  st_ivas->q_data_fx + norm_data_in */
     352             :                     }
     353        5858 :                     st_ivas->q_data_fx = add( st_ivas->q_data_fx, norm_data_in );
     354        5858 :                     move16();
     355             :                 }
     356             :                 ELSE
     357             :                 {
     358           0 :                     st_ivas->q_data_fx = 31;
     359           0 :                     move16();
     360             :                 }
     361             :             }
     362             : 
     363             :             /* Estimate MASA parameters for the objects */
     364        5858 :             ivas_omasa_enc_fx( st_ivas->hOMasa, st_ivas->hMasa, st_ivas->hIsmMetaData, data_fx, st_ivas->q_data_fx, input_frame, st_ivas->nchan_transport, hEncoderConfig->nchan_ism, st_ivas->ism_mode, data_separated_object_fx, &idx_separated_object );
     365             : 
     366        5858 :             q_data_separated_object = st_ivas->q_data_fx;
     367        5858 :             move16();
     368             :         }
     369             : 
     370             :         /* Encode ISMs transport channels */
     371        9000 :         n = 0;
     372        9000 :         move16();
     373        9000 :         IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) )
     374             :         {
     375        1862 :             IF( NE_32( ( error = ivas_sce_enc_fx( st_ivas, 0, data_separated_object_fx, q_data_separated_object, input_frame, nb_bits_metadata[1] ) ), IVAS_ERR_OK ) ) /* there are no metadata bits in SCE in this mode  */
     376             :             {
     377           0 :                 return error;
     378             :             }
     379             :         }
     380        7138 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) )
     381             :         {
     382        2054 :             IF( NE_32( ( error = ivas_ism_enc_fx( st_ivas, &data_separated_object_fx, q_data_separated_object, input_frame, &nb_bits_metadata[1], 0 ) ), IVAS_ERR_OK ) )
     383             :             {
     384           0 :                 return error;
     385             :             }
     386             :         }
     387        5084 :         ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     388             :         {
     389        3142 :             flag_omasa_ener_brate = ivas_omasa_ener_brate_fx( st_ivas->hEncoderConfig->nchan_ism, ivas_total_brate, data_fx, input_frame, sub( 31, st_ivas->q_data_fx ) ); /* Q0 */
     390             : 
     391             :             /* Analysis, decision about bitrates per channel & core coding */
     392        3142 :             IF( NE_32( ( error = ivas_ism_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, &nb_bits_metadata[1], flag_omasa_ener_brate ) ), IVAS_ERR_OK ) )
     393             :             {
     394           0 :                 return error;
     395             :             }
     396        3142 :             n = st_ivas->hEncoderConfig->nchan_ism; /* Q0 */
     397        3142 :             move16();
     398             :         }
     399             : 
     400        9000 :         hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
     401             : 
     402        9000 :         IF( st_ivas->nSCE > 0 )
     403             :         {
     404             :             /* update pointer to the buffer of indices (ISM indices were alredy written) */
     405        7058 :             hMetaData->ind_list = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData->ind_list + st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData->nb_ind_tot;
     406        7058 :             st_ivas->hCPE[0]->hCoreCoder[0]->hBstr->ind_list = st_ivas->hSCE[st_ivas->nSCE - 1]->hCoreCoder[0]->hBstr->ind_list + st_ivas->hSCE[st_ivas->nSCE - 1]->hCoreCoder[0]->hBstr->nb_ind_tot;
     407             :         }
     408             : 
     409             :         /* Encode MASA parameters and write MASA metadata bitstream */
     410             :         Word16 element_mode;
     411        9000 :         if ( st_ivas->nchan_transport == 2 )
     412             :         {
     413        9000 :             element_mode = st_ivas->hCPE[0]->element_mode;
     414        9000 :             move16();
     415             :         }
     416             :         else
     417             :         {
     418           0 :             element_mode = -1;
     419           0 :             move16();
     420             :         }
     421             : 
     422        9000 :         IF( NE_32( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, nb_bits_metadata, st_ivas->nchan_transport, ivas_format, ivas_total_brate, st_ivas->hEncoderConfig->Opt_DTX_ON, element_mode,
     423             :                                                   st_ivas->ism_mode, hEncoderConfig->nchan_ism, st_ivas->hIsmMetaData, idx_separated_object, st_ivas->hOMasa, st_ivas->hIsmMetaData[0]->ism_imp, flag_omasa_ener_brate ) ),
     424             :                    IVAS_ERR_OK ) )
     425             :         {
     426           0 :             return error;
     427             :         }
     428             : 
     429             :         /* Configuration of combined-format bit-budget distribution */
     430        9000 :         ivas_set_surplus_brate_enc( st_ivas
     431             : #ifdef DEBUG_MODE_INFO
     432             :                                     ,
     433             :                                     nb_bits_metadata
     434             : #endif
     435             :         );
     436             : 
     437             :         /* Encode MASA transport channels */
     438        9000 :         IF( ( ivas_cpe_enc_fx( st_ivas, 0, data_fx[n], data_fx[n + 1], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0], NULL ) ) != IVAS_ERR_OK )
     439             :         {
     440           0 :             return error;
     441             :         }
     442             :     }
     443       85850 :     ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
     444             :     {
     445             :         Word16 planar_sba_orig, nchan;
     446       37000 :         planar_sba_orig = hEncoderConfig->sba_planar;
     447       37000 :         move16();
     448             : 
     449             :         /* Analyze objects and determine needed audio signals */
     450       37000 :         ivas_osba_enc_fx( st_ivas->hOSba, st_ivas->hIsmMetaData, data_fx, input_frame, hEncoderConfig->nchan_ism, st_ivas->ism_mode, st_ivas->sba_analysis_order, hEncoderConfig->input_Fs, hEncoderConfig->sba_planar, &st_ivas->q_data_fx );
     451             : 
     452       37000 :         IF( st_ivas->ism_mode == ISM_MODE_NONE )
     453             :         {
     454       14920 :             nchan = imult1616( add( st_ivas->sba_analysis_order, 1 ), add( st_ivas->sba_analysis_order, 1 ) ); /* Q0 */
     455             :         }
     456             :         ELSE
     457             :         {
     458       22080 :             nchan = hEncoderConfig->nchan_ism; /* Q0 */
     459       22080 :             move16();
     460             :         }
     461      166040 :         FOR( n = 0; n < nchan; n++ )
     462             :         {
     463      129040 :             scale_sig32( data_fx[n], input_frame, sub( Q11, st_ivas->q_data_fx ) ); // Q(st_ivas->q_data_fx) -> Q11
     464             :         }
     465       37000 :         st_ivas->q_data_fx = Q11;
     466       37000 :         move16();
     467             : 
     468       37000 :         IF( st_ivas->ism_mode == ISM_MODE_NONE )
     469             :         {
     470             :             /*once SBA and ISM are combined into SBA signal then disable planar flag*/
     471       14920 :             hEncoderConfig->sba_planar = 0;
     472       14920 :             move16();
     473       14920 :             IF( EQ_16( st_ivas->nchan_transport, 1 ) )
     474             :             {
     475        6970 :                 hMetaData = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData;
     476             :             }
     477             :             ELSE
     478             :             {
     479        7950 :                 hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
     480             :             }
     481             : 
     482             :             /* SBA metadata encoding and SBA metadata bitstream writing */
     483       14920 :             IF( ( error = ivas_spar_enc_fx( st_ivas, data_fx, input_frame, nb_bits_metadata, hMetaData ) ) != IVAS_ERR_OK )
     484             :             {
     485           0 :                 return error;
     486             :             }
     487             : 
     488       14920 :             hEncoderConfig->sba_planar = planar_sba_orig; /* Q0 */
     489       14920 :             move16();
     490             :         }
     491             :         ELSE
     492             :         {
     493       22080 :             n = hEncoderConfig->nchan_ism; /* Q0 */
     494       22080 :             move16();
     495       22080 :             hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
     496             : 
     497       22080 :             IF( ( error = ivas_ism_metadata_enc_fx( &st_ivas->hEncoderConfig->ivas_total_brate, n, st_ivas->hEncoderConfig->nchan_ism, st_ivas->hIsmMetaData, NULL, hMetaData, &nb_bits_metadata[1], 0, st_ivas->ism_mode, NULL, st_ivas->hEncoderConfig->ism_extended_metadata_flag, -256 /* -1.0f in Q8 */, 0, NULL, st_ivas->hCPE[0]->hCoreCoder[0]->ini_frame ) ) != IVAS_ERR_OK )
     498             :             {
     499           0 :                 return error;
     500             :             }
     501             : 
     502             :             /* SBA metadata encoding and SBA metadata bitstream writing */
     503       22080 :             IF( ( error = ivas_spar_enc_fx( st_ivas, &data_fx[n], input_frame, nb_bits_metadata, hMetaData ) ) != IVAS_ERR_OK )
     504             :             {
     505           0 :                 return error;
     506             :             }
     507             : 
     508             :             /* get SBA TCs */
     509       22080 :             ivas_sba_getTCs_fx( &data_fx[n], st_ivas, input_frame );
     510             :         }
     511             : 
     512             :         /* core-coding of transport channels */
     513       37000 :         IF( EQ_16( st_ivas->nSCE, 1 ) )
     514             :         {
     515        6970 :             IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[0], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     516             :             {
     517           0 :                 return error;
     518             :             }
     519             :         }
     520       30030 :         ELSE IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
     521             :         {
     522        6360 :             IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0], NULL ) ) != IVAS_ERR_OK )
     523             :             {
     524           0 :                 return error;
     525             :             }
     526             :         }
     527       23670 :         ELSE IF( GT_16( st_ivas->nCPE, 1 ) ) /* FOA/HOA format */
     528             :         {
     529       23670 :             IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
     530             :             {
     531           0 :                 return error;
     532             :             }
     533             :         }
     534             :     }
     535       48850 :     ELSE IF( EQ_32( ivas_format, MC_FORMAT ) )
     536             :     {
     537             :         /* select MC format mode; write MC LS setup; reconfigure the MC format encoder */
     538       48850 :         IF( ( error = ivas_mc_enc_config_fx( st_ivas ) ) != IVAS_ERR_OK )
     539             :         {
     540           0 :             return error;
     541             :         }
     542             : 
     543             :         /* hMetaData = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData : st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData; */
     544       48850 :         IF( st_ivas->nSCE > 0 )
     545             :         {
     546       11260 :             hMetaData = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData;
     547             :         }
     548             :         ELSE
     549             :         {
     550       37590 :             hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
     551             :         }
     552             : 
     553             :         /* LFE low pass filter */
     554       48850 :         ivas_lfe_lpf_enc_apply_fx( st_ivas->hLfeLpf, data_fx[LFE_CHANNEL], input_frame );
     555             : 
     556             :         /* LFE channel encoder */
     557       48850 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
     558             :         {
     559       26130 :             IF( st_ivas->nSCE > 0 )
     560             :             {
     561           0 :                 st_ivas->hLFE->hBstr = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr;
     562             :             }
     563             :             ELSE
     564             :             {
     565       26130 :                 st_ivas->hLFE->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr;
     566             :             }
     567             : 
     568       26130 :             ivas_lfe_enc_fx( st_ivas->hLFE, data_fx[LFE_CHANNEL], st_ivas->q_data_fx, input_frame, st_ivas->hLFE->hBstr );
     569             :         }
     570             : 
     571       48850 :         IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
     572             :         {
     573       26130 :             IF( NE_32( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, 0 ) ), IVAS_ERR_OK ) )
     574             :             {
     575           0 :                 return error;
     576             :             }
     577             :         }
     578       22720 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
     579             :         {
     580             :             /* encode MC ParamUpmix parameters and write bitstream */
     581         790 :             ivas_mc_paramupmix_enc_fx( st_ivas, hMetaData, data_fx, input_frame );
     582             : 
     583             :             /* st_ivas->hLFE->hBstr = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0]->hBstr : st_ivas->hCPE[0]->hCoreCoder[0]->hBstr; */
     584         790 :             IF( st_ivas->nSCE > 0 )
     585             :             {
     586           0 :                 st_ivas->hLFE->hBstr = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr;
     587             :             }
     588             :             ELSE
     589             :             {
     590         790 :                 st_ivas->hLFE->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr;
     591             :             }
     592             : 
     593         790 :             ivas_lfe_enc_fx( st_ivas->hLFE, data_fx[LFE_CHANNEL], st_ivas->q_data_fx, input_frame, st_ivas->hLFE->hBstr );
     594             : 
     595         790 :             IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot ) ) != IVAS_ERR_OK )
     596             :             {
     597           0 :                 return error;
     598             :             }
     599             :         }
     600       21930 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
     601             :         {
     602             :             /* encode Parametric MC parameters and write bitstream */
     603       10250 :             ivas_param_mc_enc_fx( st_ivas, hMetaData, data_fx, input_frame );
     604             : 
     605       10250 :             IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
     606             :             {
     607       10140 :                 IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot, NULL ) ) != IVAS_ERR_OK )
     608             :                 {
     609           0 :                     return error;
     610             :                 }
     611             :             }
     612         110 :             ELSE IF( GT_16( st_ivas->nCPE, 1 ) )
     613             :             {
     614         110 :                 IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot ) ) != IVAS_ERR_OK )
     615             :                 {
     616           0 :                     return error;
     617             :                 }
     618             :             }
     619             :         }
     620       11680 :         ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
     621             :         {
     622       11680 :             if ( st_ivas->hMcMasa->separateChannelEnabled )
     623             :             {
     624         625 :                 hMetaData = st_ivas->hCPE[0]->hMetaData; /* Metadata is always with CPE in the case of separated channel */
     625             :             }
     626             : 
     627       89780 :             FOR( i = 0; i < nchan_inp; i++ )
     628             :             {
     629       78100 :                 scale_sig32( data_fx[i], input_frame, sub( Q8, st_ivas->q_data_fx ) ); // st_ivas->q_data_fx -> Q8
     630             :             }
     631       11680 :             st_ivas->q_data_fx = Q8;
     632       11680 :             move16();
     633             : 
     634       11680 :             ivas_mcmasa_enc_fx( st_ivas->hMcMasa, st_ivas->hQMetaData, st_ivas->hMasa, data_fx, input_frame, st_ivas->nchan_transport, nchan_inp, st_ivas->q_data_fx );
     635             : 
     636       89780 :             FOR( i = 0; i < nchan_inp; i++ )
     637             :             {
     638       78100 :                 scale_sig32( data_fx[i], input_frame, sub( Q11, st_ivas->q_data_fx ) ); // st_ivas->q_data_fx -> Q11
     639             :             }
     640       11680 :             st_ivas->q_data_fx = Q11;
     641       11680 :             move16();
     642             : 
     643             :             /* Scaling down the Q-factor of energy_ratio_fx from Q31 to Q30 */
     644       70080 :             FOR( i = 0; i < st_ivas->hMcMasa->nbands; i++ )
     645             :             {
     646       58400 :                 scale_sig32( st_ivas->hQMetaData->q_direction[0].band_data[i].energy_ratio_fx, MAX_PARAM_SPATIAL_SUBFRAMES, -1 ); // Q31 -> Q30
     647             :             }
     648             : 
     649       11680 :             IF( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, &nb_bits_metadata[0], st_ivas->nchan_transport, ivas_format, ivas_total_brate, 0, -1,
     650             :                                                ISM_MODE_NONE, -1, NULL, -1, NULL, 0, 0 ) ) != IVAS_ERR_OK )
     651             :             {
     652           0 :                 return error;
     653             :             }
     654             : 
     655       11680 :             IF( st_ivas->hMcMasa->separateChannelEnabled )
     656             :             {
     657         625 :                 IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[2], st_ivas->q_data_fx, input_frame, 0 ) ) != IVAS_ERR_OK )
     658             :                 {
     659           0 :                     return error;
     660             :                 }
     661             : 
     662         625 :                 st_ivas->hCPE[0]->hCoreCoder[0]->hBstr->ind_list = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr->ind_list + st_ivas->hSCE[0]->hCoreCoder[0]->hBstr->nb_ind_tot;
     663             : 
     664         625 :                 IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot, NULL ) ) != IVAS_ERR_OK )
     665             :                 {
     666           0 :                     return error;
     667             :                 }
     668             :             }
     669             :             ELSE
     670             :             {
     671       11055 :                 IF( EQ_16( st_ivas->nSCE, 1 ) )
     672             :                 {
     673       10635 :                     IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[0], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot ) ) != IVAS_ERR_OK )
     674             :                     {
     675           0 :                         return error;
     676             :                     }
     677             :                 }
     678         420 :                 ELSE IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
     679             :                 {
     680         420 :                     IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot, NULL ) ) != IVAS_ERR_OK )
     681             :                     {
     682           0 :                         return error;
     683             :                     }
     684             :                 }
     685             :             }
     686             :         }
     687             :     }
     688             : 
     689             :     /*----------------------------------------------------------------*
     690             :      * Common updates
     691             :      *----------------------------------------------------------------*/
     692             : 
     693      424288 :     hEncoderConfig->last_ivas_total_brate = ivas_total_brate;
     694      424288 :     move32();
     695             : 
     696             : #ifdef DEBUG_MODE_INFO
     697             :     {
     698             :         float tmpF = ivas_total_brate / 1000.0f;
     699             :         dbgwrite( &tmpF, sizeof( float ), 1, input_frame, "res/ivas_total_brate" );
     700             :     }
     701             : #endif
     702             : 
     703      424288 :     pop_wmops();
     704      424288 :     return error;
     705             : }

Generated by: LCOV version 1.14