Commit 3c55d9a8 authored by fotopoulou's avatar fotopoulou
Browse files

first changes to enable bitrate swtiching for MC at encoder side. WIP.

parent c664f705
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -628,6 +628,13 @@ ivas_error ivas_mc_enc_config(
    Encoder_Struct *st_ivas                                     /* i/o: IVAS encoder structure                  */
);

#ifdef MC_BITRATE_SWITCHING
ivas_error ivas_mc_enc_reconfig(
    Encoder_Struct *st_ivas,                                    /* i/o: IVAS encoder structure      */
    const int16_t last_mc_mode                                  /* i: last frame mc mode            */
);
#endif

ivas_error ivas_mc_dec_config(
    Decoder_Struct *st_ivas,                                    /* i/o: IVAS decoder structure                  */
    const int16_t idx                                           /* i  : LS config. index                        */
@@ -3483,7 +3490,17 @@ int16_t ivas_param_mc_getNumTransportChannels(
ivas_error ivas_param_mc_enc_open(
    Encoder_Struct *st_ivas                                     /* i/o: IVAS encoder handle                                 */
);
#ifdef MC_BITRATE_SWITCHING
/*-------------------------------------------------------------------------
 * ivas_param_mc_enc_open()
 *
 * Initialize Parametric MC encoder handle
 *------------------------------------------------------------------------*/

ivas_error ivas_param_mc_reconfig(
    Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
);
#endif
void ivas_param_mc_enc_close(
    PARAM_MC_ENC_HANDLE hParamMC,                               /* i/o: Parametric MC encoder handle                        */
    const int32_t input_Fs                                      /* i  : input sampling rate                                 */
+1 −0
Original line number Diff line number Diff line
@@ -160,6 +160,7 @@


#define CORECODER_BITRATE_SWITCHING                     /* Issue 133: support bitrate switching in core-coder */
#define MC_BITRATE_SWITCHING                            /* Issue 116: support bitrate switching in MC format  */


/* ################## End DEVELOPMENT switches ######################### */
+6 −0
Original line number Diff line number Diff line
@@ -286,6 +286,12 @@ ivas_error ivas_corecoder_enc_reconfig(
                        st_ivas->hCPE[cpe_id]->hCoreCoder[n]->cng_sba_flag = 1;
                    }
                }
#ifdef MC_BITRATE_SWITCHING
                if ( nSCE_old )
                {
                    st_ivas->hCPE[cpe_id]->element_mode = IVAS_CPE_MDCT;
                }
#endif
            }
        }

+123 −0
Original line number Diff line number Diff line
@@ -218,6 +218,129 @@ ivas_error ivas_param_mc_enc_open(
    return error;
}

#ifdef MC_BITRATE_SWITCHING
/*-------------------------------------------------------------------------
 * ivas_param_mc_enc_open()
 *
 * Initialize Parametric MC encoder handle
 *------------------------------------------------------------------------*/

ivas_error ivas_param_mc_reconfig(
    Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
)
{
    int16_t i, k, l;
    PARAM_MC_ENC_HANDLE hParamMC;
    uint16_t config_index;
    MC_LS_SETUP mc_input_setup;
    int16_t max_bwidth;
    int32_t input_Fs, ivas_total_brate;
    ivas_error error;

    error = IVAS_ERR_OK;

    mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
    max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
    input_Fs = st_ivas->hEncoderConfig->input_Fs;
    ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
    hParamMC = st_ivas->hParamMC;

    /* Preparing Config */
    st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );

    /* get configuration index */
    config_index = ivas_param_mc_get_configuration_index( mc_input_setup, ivas_total_brate );

    /* set core coder dependent on the number of transport channels */
    switch ( st_ivas->nchan_transport )
    {
        case 4:
        case 3:
            st_ivas->nCPE = 2;
            st_ivas->nSCE = 0;
            st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
            break;
        case 2:
            st_ivas->nCPE = 1;
            st_ivas->nSCE = 0;
            st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
            break;
#ifdef DEBUGGING
        default:
            assert( 0 && "Number of transport channels not supported by ParamMC!\n" );
#endif
    }

    /* get dmx factors */
    hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;


    /* open/init parameter coding */
    ivas_param_mc_metadata_open( mc_input_setup, hParamMC->lfe_index, ivas_total_brate, &hParamMC->hMetadataPMC );

    /* init icc index states */
    for ( i = 0; i < PARAM_MC_PARAMETER_FRAMES; i++ )
    {
        set_s( hParamMC->icc_map_index[i], -1, PARAM_MC_SZ_ICC_MAP );

        for ( l = 0; l < hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe; l++ )
        {
            for ( k = 0; k < hParamMC->hMetadataPMC.icc_map_size_full; k++ )
            {
                if ( hParamMC->hMetadataPMC.icc_mapping[i][l][0] == hParamMC->hMetadataPMC.icc_map_full[0][k] && hParamMC->hMetadataPMC.icc_mapping[i][l][1] == hParamMC->hMetadataPMC.icc_map_full[1][k] )
                {
                    hParamMC->icc_map_index[i][l] = k;
                }
            }
        }
    }

    /* Band Grouping */
    if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
    {
        mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
    }
    else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
    {
        mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
    }
    else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
    {
        mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
    }
    else
    {
        assert( 0 && "nbands must be 20, 14, or 10!" );
    }

    /* set max parameter band for abs cov */
    i = 0;
    while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
    {
        hParamMC->max_param_band_abs_cov = ( i++ );
    }

    /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
    for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
    {
        hParamMC->band_grouping[i] *= CLDFB_TO_MDFT_FAC;
    }

    /* set correct coded band width */
    hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
    hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
    ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );

    /* initialize offset for transient detection */
    hParamMC->transient_detector_delay = ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1 - (int16_t) ceilf( (float) NS2SA( input_Fs, DELAY_DIRAC_ENC_CMP_NS ) / (float) NS2SA( input_Fs, 2 * DIRAC_SLOT_NS ) );

    /* Init total/dmx ener factors */
    set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );


    return error;
}
#endif

/*-------------------------------------------------------------------------
 * ivas_param_mc_enc_close()
+161 −2
Original line number Diff line number Diff line
@@ -245,8 +245,9 @@ ivas_error create_mct_enc(

    for ( n = 0; n < max_blocks; n++ )
    {
#ifndef MC_BITRATE_SWITCHING
        assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );

#endif
        if ( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) count_malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
@@ -542,9 +543,167 @@ ivas_error ivas_mc_enc_config(
    /* MC format switching */
    if ( st_ivas->hEncoderConfig->last_ivas_total_brate != st_ivas->hEncoderConfig->ivas_total_brate || st_ivas->mc_mode != last_mc_mode )
    {
        /*ivas_mc_enc_reconfigure( st_ivas );*/
#ifdef MC_BITRATE_SWITCHING
        ivas_mc_enc_reconfig( st_ivas, last_mc_mode );
#else
        return IVAS_ERROR( IVAS_ERR_RECONFIGURE_NOT_SUPPORTED, "Error: MC format switching not supported yet!!!\n\n" );
#endif
    }

    return error;
}

#ifdef MC_BITRATE_SWITCHING
/*-------------------------------------------------------------------------
 * ivas_mc_enc_reconfig()
 * - reconfigure the MC format encoder
 *-------------------------------------------------------------------------*/

ivas_error ivas_mc_enc_reconfig(
    Encoder_Struct *st_ivas,   /* i/o: IVAS encoder structure      */
    const int16_t last_mc_mode /* i: last frame mc mode            */
)
{
    int16_t nchan_transport_old, nSCE_old, nCPE_old;
    ivas_error error;

    error = IVAS_ERR_OK;

    nchan_transport_old = st_ivas->nchan_transport;
    nSCE_old = st_ivas->nSCE;
    nCPE_old = st_ivas->nCPE;

    if ( st_ivas->mc_mode == MC_MODE_MCT )
    {
        st_ivas->nSCE = 0;
        st_ivas->nCPE = st_ivas->hEncoderConfig->nchan_inp / 2;
#if 0
        for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
        {
            if ( ( error = create_cpe_enc( st_ivas, cpe_id, ( ivas_total_brate / ( hEncoderConfig->nchan_inp - 1 ) * CPE_CHANNELS ) ) ) != IVAS_ERR_OK )
            {
                return error;
            }

            for ( n = 0; n < CPE_CHANNELS; n++ )
            {
                if ( cpe_id * CPE_CHANNELS + n == LFE_CHANNEL )
                {
                    st_ivas->hCPE[cpe_id]->hCoreCoder[n]->hBstr->nb_bits_tot = 0;
                    continue;
                }
                st_ivas->hCPE[cpe_id]->hCoreCoder[n]->hBstr->ind_list = ind_list[cpe_id * CPE_CHANNELS + n];
                reset_indices_enc( st_ivas->hCPE[cpe_id]->hCoreCoder[n]->hBstr, MAX_NUM_INDICES );
            }
        }

        if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
        {
            return error;
        }
#endif
        st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( st_ivas->hEncoderConfig->mc_input_setup );

        if ( last_mc_mode != MC_MODE_MCT )
        {
#if 0
            /*-----------------------------------------------------------------*
             * Allocate MCT handle
             *-----------------------------------------------------------------*/

            if ( ( st_ivas->hMCT = (MCT_ENC_HANDLE) count_malloc( sizeof( MCT_ENC_DATA ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT\n" ) );
            }
#endif

            /*De-allocate handles for other MC modes*/
            if ( st_ivas->hParamMC != NULL )
            {
                ivas_param_mc_enc_close( st_ivas->hParamMC, st_ivas->hEncoderConfig->input_Fs );
                st_ivas->hParamMC = NULL;
            }

            if ( st_ivas->hMcMasa != NULL )
            {
                ivas_mcmasa_enc_close( st_ivas->hMcMasa, st_ivas->hEncoderConfig->input_Fs );
                st_ivas->hMcMasa = NULL;
            }
        }
    }
    else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
    {
        if ( last_mc_mode != MC_MODE_PARAMMC )
        {
            if ( ( error = ivas_param_mc_enc_open( st_ivas ) ) != IVAS_ERR_OK )
            {
                return error;
            }
        }
        else
        {
        }
#if 0
        for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
        {
            if ( ( error = create_cpe_enc( st_ivas, cpe_id, ivas_total_brate / ( st_ivas->nCPE + st_ivas->nSCE ) ) ) != IVAS_ERR_OK )
            {
                return error;
            }

            /* prepare bitstream buffers */
            for ( n = 0; n < CPE_CHANNELS; n++ )
            {
                st_ivas->hCPE[cpe_id]->hCoreCoder[n]->hBstr->ind_list = ind_list[cpe_id * CPE_CHANNELS + n];
                reset_indices_enc( st_ivas->hCPE[cpe_id]->hCoreCoder[n]->hBstr, MAX_NUM_INDICES );
            }

            /* Metadata only initialized for the last CPE index*/
            if ( cpe_id == st_ivas->nCPE - 1 )
            {
                st_ivas->hCPE[cpe_id]->hMetaData->ind_list = ind_list_metadata[st_ivas->nSCE];
                reset_indices_enc( st_ivas->hCPE[cpe_id]->hMetaData, MAX_BITS_METADATA );
            }
        }

        if ( st_ivas->nCPE > 1 )
        {
            if ( ( error = create_mct_enc( st_ivas ) ) != IVAS_ERR_OK )
            {
                return error;
            }
        }
    }
#endif
    }
    else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
    {
        ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->hEncoderConfig->element_mode_init ), st_ivas->hEncoderConfig->ivas_total_brate );
#if 0
        if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        if ( ( error = ivas_mcmasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
        {
            return error;
        }
#endif
    }

    /* re-configure core coder*/
    if ( ( error = ivas_corecoder_enc_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old ) ) != IVAS_ERR_OK )
    {
        return error;
    }


    return error;
}
#endif
 No newline at end of file