Commit 6518ebf4 authored by vaillancour's avatar vaillancour
Browse files

comment tdm_configure_enc

parent 3291069a
Loading
Loading
Loading
Loading
+145 −17
Original line number Diff line number Diff line
@@ -337,8 +337,20 @@ void tdm_configure_enc(
    sts[0]->hSpMusClas->tdm_lt_Etot = 0.1f * Etot_last[0] + 0.9f * sts[0]->hSpMusClas->tdm_lt_Etot;
    sts[1]->hSpMusClas->tdm_lt_Etot = 0.1f * Etot_last[1] + 0.9f * sts[1]->hSpMusClas->tdm_lt_Etot;
    
    if ( hCPE->hStereoClassif->lrtd_mode == 0 && ( ( sts[1]->hSpMusClas->tdm_lt_Etot < 0 && hCPE->hCoreCoder[1]->vad_flag == 0 ) /* very clean signal */
                                                   || ( hCPE->hCoreCoder[1]->vad_flag == 0 || ( Etot_last[1] < 30.0f && ( sts[0]->hSpMusClas->tdm_lt_Etot - sts[1]->hSpMusClas->tdm_lt_Etot ) > 26.0f ) ) ) )
    /* ( In case of normal td content and 
         the signal of the secondary channel doesn't contain signal activity and has its long term energy is low  
      or secondary channel doesn't contain signal activity or its last energy was low and the difference between the long term energy of primary and secondary channel is above 26dB 
      then : 
      - the secondary channel coder type is set to INACTIVE. 
        if the lp reusing flag is 0 and the content of the primary channel contains active signal 
            then :
            - the flag indicating the use of an average lp filter is set to one
      - the lp reusing flag is set to 1  
      */
    if ( hCPE->hStereoClassif->lrtd_mode == 0 &&  
        ( ( sts[1]->hSpMusClas->tdm_lt_Etot < 0 && hCPE->hCoreCoder[1]->vad_flag == 0 ) /* very clean signal */
                                                   || ( hCPE->hCoreCoder[1]->vad_flag == 0 
                                                       || ( Etot_last[1] < 30.0f && ( sts[0]->hSpMusClas->tdm_lt_Etot - sts[1]->hSpMusClas->tdm_lt_Etot ) > 26.0f ) ) ) )
    {
        sts[1]->coder_type = INACTIVE;

@@ -348,7 +360,22 @@ void tdm_configure_enc(
        }
        hStereoTD->tdm_lp_reuse_flag = 1;
    }
    else if ( ( ( hCPE->hCoreCoder[1]->vad_flag == 0 ) || ( hCPE->hCoreCoder[0]->vad_flag == 0 && Etot_last[1] < 30.0f && ( sts[0]->hSpMusClas->tdm_lt_Etot - sts[1]->hSpMusClas->tdm_lt_Etot ) > 26.0f ) ) && ( hCPE->hStereoClassif->lrtd_mode == 1 ) /* && NO_DTX */ ) /* boths channels are inactive but not DTX used*/
    /* 
    Else, in case of over-talk or uncorrelated content (LRTD) and 
    the signal of the secondary channel doesn't contain voice activity or the signal of the primary channel doesn't contain voice activity 
    and its last energy was below 30 dB and the long term energy of the secondary channel is 26dB below the long term energy of the primary channel 
    then : 
      - the secondary channel coder type is set to INACTIVE. 
        if tdm_ratio_idx is between 1 and 29 
            if the lp reusing flag is 0 and the content of the primary channel contains active signal 
                then :
                - the flag indicating the use of an average lp filter is set to one
            - the lp reusing flag is set to 1  
    */
    else if ( ( ( hCPE->hCoreCoder[1]->vad_flag == 0 ) 
                    || ( hCPE->hCoreCoder[0]->vad_flag == 0 && Etot_last[1] < 30.0f 
                                && ( sts[0]->hSpMusClas->tdm_lt_Etot - sts[1]->hSpMusClas->tdm_lt_Etot ) > 26.0f ) ) 
                                        && ( hCPE->hStereoClassif->lrtd_mode == 1 ) /* && NO_DTX */ ) /* boths channels are inactive but not DTX used*/
    {
        sts[1]->coder_type = INACTIVE;
        if ( tdm_ratio_idx > 1 && tdm_ratio_idx < 29 )
@@ -360,7 +387,23 @@ void tdm_configure_enc(
            hStereoTD->tdm_lp_reuse_flag = 1;
        }
    }
    else if ( !( sts[1]->sp_aud_decision0 ) && sts[1]->tc_cnt <= 0 && ( sts[1]->coder_type_raw == UNVOICED || ( hStereoTD->tdm_LRTD_flag == 1 && hStereoTD->tdm_lp_reuse_flag == 0 && ( hCPE->hCoreCoder[1]->vad_flag == 0 || ( Etot_last[1] < 30.0f && ( sts[0]->hSpMusClas->tdm_lt_Etot - sts[1]->hSpMusClas->tdm_lt_Etot ) > 26.0f ) ) ) ) )
    /* 
    Else, if the sp_aud_decision0 of the secondary channel is 0 and transition counter is less or equal to 0 and  
    the unmodified coder_type of the secondary channel is unvoiced or 
     the tdm_LRTD_flag is 1 and the lp reuse flag is 0 and the signal activity flag on the secondary channel is 0 or the last energy of the secondary channel was below 30 db and the long term energy of secondary channel is 26 dB below the primary channel 
    then : 
      - the coding mode of the secondary channel is force to UNVOICED
        if tdm_LRTD_flag is set to 1
          - then the   
            if the lp reusing flag is 0 and the content of the primary channel contains active signal 
              then : 
                - the lp reusing flag is set to 1
    */
    else if ( !( sts[1]->sp_aud_decision0 ) && sts[1]->tc_cnt <= 0 
                                            && ( sts[1]->coder_type_raw == UNVOICED 
                                                        || ( hStereoTD->tdm_LRTD_flag == 1 && hStereoTD->tdm_lp_reuse_flag == 0 
                                                                        && ( hCPE->hCoreCoder[1]->vad_flag == 0 
                                                                                || ( Etot_last[1] < 30.0f && ( sts[0]->hSpMusClas->tdm_lt_Etot - sts[1]->hSpMusClas->tdm_lt_Etot ) > 26.0f ) ) ) ) )
    {
        sts[1]->coder_type = UNVOICED;
        if ( hStereoTD->tdm_LRTD_flag == 1 )
@@ -368,16 +411,40 @@ void tdm_configure_enc(
            hStereoTD->tdm_lp_reuse_flag = 0;
        }
    }
    /* 
    Else, if the coding type of the secondary is generic or INACTIVE or 
          the coder type of the secondary channel is audio and the bitrate is below or equal to 24k4 or 
          the sp_aud_decision1 flag of the primary channel is 1 and the bitrate is >= 16k4 or 
          the sp_aud_decision0 flag of the secondary channel is 1 and the bitrate is > 13k2 or 
          then :
            - the coding mode of the secondary channel is force to GENERIC
    */
    else if ( ( sts[1]->coder_type < AUDIO && sts[1]->coder_type != UNVOICED ) || /* TC and VC are not supported in secondary channel */
              ( ( sts[1]->coder_type == AUDIO && hCPE->element_brate <= IVAS_24k4 ) || ( sts[0]->sp_aud_decision1 == 1 && hCPE->element_brate >= IVAS_16k4 ) || ( sts[1]->sp_aud_decision0 == 1 && hCPE->element_brate > IVAS_13k2 ) ) )
                         ( ( sts[1]->coder_type == AUDIO && hCPE->element_brate <= IVAS_24k4 ) 
                        || ( sts[0]->sp_aud_decision1 == 1 && hCPE->element_brate >= IVAS_16k4 ) 
                        || ( sts[1]->sp_aud_decision0 == 1 && hCPE->element_brate > IVAS_13k2 ) ) )
    {
        sts[1]->coder_type = GENERIC;
    }
    /* 
    Else, if the coding type of the secondary is generic and the unmodified coder type of the primary channel is UNVOICED
          then :
            - the lp reusing flag is set to 0
    */
    else if ( sts[1]->coder_type == GENERIC && sts[1]->coder_type_raw == UNVOICED )
    {
        hStereoTD->tdm_lp_reuse_flag = 0;
    }

    /* 
      If the bitrate is > 24k4 and the content is classified as uncorrelated (or xtalk : LRTD)
        If coding type of the secondary is UNVOICED
          then :
            - the secondary channel coding type is set to GENERIC
        If coding type of the primary channel is UNVOICED
          then :
            - the primary channel coding type is set to GENERIC
            - the primary channel unmodified is set to generic
    */
    if ( hCPE->element_brate > IVAS_24k4 && hCPE->hStereoClassif->lrtd_mode == 1 )
    {
        if ( sts[1]->coder_type == UNVOICED )
@@ -391,18 +458,31 @@ void tdm_configure_enc(
            loc_coder_tyape_raw0 = GENERIC;
        }
    }

    /* 
      If the bitrate is >= 24k4 and the content is NOT classified as uncorrelated/Xtalk (normal TD) and the primary channel coder type is UNVOICED
          then :
            - the primary channel coding type is set to GENERIC
            - the primary channel unmodified is set to generic
    */
    if ( hCPE->element_brate >= IVAS_24k4 && hCPE->hStereoClassif->lrtd_mode == 0 && sts[0]->coder_type == UNVOICED )
    {
        sts[0]->coder_type = GENERIC;
        loc_coder_tyape_raw0 = GENERIC;
    }

    /* 
      If secondary channel coding type is different from GENERIC 
          then :
            - The reusing pitch flag is forced to 0
    */
    if ( sts[1]->coder_type != GENERIC )
    {
        hStereoTD->tdm_Pitch_reuse_flag = 0;
    }

    /* 
      If the attack flag is different from 0 and the secondary coding type is different from INACTIVE
          then :
            - The LP reusing flag is forced to 0
    */
    if ( attack_flag != 0 )
    {
        if ( sts[1]->coder_type != INACTIVE )
@@ -410,7 +490,11 @@ void tdm_configure_enc(
            hStereoTD->tdm_lp_reuse_flag = 0; /* Do not allow the LP filter reusing on TC or attack in the primary channel */
        }
    }

    /* 
      If the flag for SM content (Out Of Phase) is set to one and the LRTD flag is set to 0
          then :
            - The mixing ratio index will corresponds to the SM content
    */
    if ( tdm_SM_or_LRTD_Pri && hStereoTD->tdm_LRTD_flag == 0 )
    {
        tdm_ratio_bit_alloc_idx = tdm_ratio_idx_SM;
@@ -419,13 +503,31 @@ void tdm_configure_enc(
    {
        tdm_ratio_bit_alloc_idx = tdm_ratio_idx;
    }
    /* 
      If the content is classified as uncorrelated (or xtalk : LRTD) and the secondary channel coding mode is UNVOICE or GENERIC 
       and the difference between the last mixing index and the current bit allocation index is greater than 15 (more than half the total range) 
        (-> means that primary and secondary channel are kind of inversed but have not switched yet)
        Or there is less than 4 frame from the last FD frame and less than 4 frames from the last LRTD transition
          then :
            - the secondary channel coding type is set to GENERIC
        
    */
    if ( ( hCPE->hStereoClassif->lrtd_mode == 1 && sts[1]->coder_type >= UNVOICED && abs( hStereoTD->tdm_last_ratio_idx - tdm_ratio_bit_alloc_idx ) > 15 ) /* channel inversion in lrtd */
                                            || ( hStereoTD->tdm_FD2LRTD_SW_cnt < 4 && hStereoTD->tdm_last_LRTD_frame_cnt < 4 ) )
    {
        sts[1]->coder_type = GENERIC;
    }

    /* 
      In case of ISM content. 
        if the effective bit rate is below 12k 
           the lp reusing flag is forced to 1
           if the secondary channel coding type is unvoiced
             - then 
               : the secondary channel coding type is set to GENERIC
            if the effective bit rate is below 11k 
             - then 
               : the secondary channel coding type is set to INACTIVE
    */
    if ( hCPE->element_brate - nb_bits_metadata * FRAMES_PER_SEC + hCPE->brate_surplus < 12000 )
    {
        if ( sts[1]->coder_type == UNVOICED )
@@ -439,6 +541,13 @@ void tdm_configure_enc(
            sts[1]->coder_type = INACTIVE;
        }
    }
    /* 
      In case of ISM content. 
        if the effective bit rate is below 14k7 
           if the primary channel coding type is TRANSITION
             - then 
               : the primary channel coding type is set to GENERIC
    */

    if ( hCPE->element_brate - nb_bits_metadata * FRAMES_PER_SEC + hCPE->brate_surplus < 14700 )
    {
@@ -447,13 +556,32 @@ void tdm_configure_enc(
            sts[0]->coder_type = GENERIC;
        }
    }

    /* 
      Setting a temporary coder type that will be used inside the bit allocation routine. it is needed to know weather the coder type of the primary channel is TRANSITION or not
        the temporary coder type is set to AUDIO 
        if the bit rate is below 24k4
          then :
            - the temporary coding mode is set to the actual primary channel coding mode 
            if 
              last frame >= L_FRAME16k and the primary channel flag ACELP16k is 0 
              or
              last frame == L_FRAME and the primary channel flag ACELP16k is 1
              and the last core bit rate is not 0 and is not 2k4 and the primary channel unmodified coder type is not VOICED 
              then :
                - temporary coding mode is set to TRANSITION
            else if the primary channel sp_aud_decision1 or the sp_aud_decision2 flags is set to 1 or the primary coder type is different of TRANSITION
            then : 
                - temporary coding mode is set AUDIO
            
    */
    mod_ct = AUDIO;
    if ( hCPE->element_brate < IVAS_24k4 )
    {
        mod_ct = sts[0]->coder_type;
        /* Only sure TRANSITION coding modes are important for bit allocation, otherwise mod_ct is set to AUDIO only to easy debugging if needed */
        if ( ( ( sts[0]->last_L_frame >= L_FRAME16k && sts[0]->flag_ACELP16k == 0 ) || ( sts[0]->last_L_frame == L_FRAME && sts[0]->flag_ACELP16k == 1 ) ) && sts[0]->last_core_brate != FRAME_NO_DATA && sts[0]->last_core_brate != SID_2k40 && sts[0]->coder_type_raw != VOICED /*in case of CNG, this code is not reached sts[0]->core_brate != FRAME_NO_DATA && sts[0]->core_brate != SID_2k40 &&*/ )
        if ( ( ( sts[0]->last_L_frame >= L_FRAME16k && sts[0]->flag_ACELP16k == 0 ) 
                || ( sts[0]->last_L_frame == L_FRAME && sts[0]->flag_ACELP16k == 1 ) ) 
                        && sts[0]->last_core_brate != FRAME_NO_DATA && sts[0]->last_core_brate != SID_2k40 && sts[0]->coder_type_raw != VOICED )
        {
            mod_ct = TRANSITION;
        }
@@ -463,7 +591,7 @@ void tdm_configure_enc(
        }
    }

    /* Correction of tdm_inst_ratio_idx in case of TC in the seecondary channel */
    /* During LRTD content, Correction of tdm_inst_ratio_idx in case of TC in the secondary channel */
    if ( hStereoTD->flag_skip_DMX == 0 && hStereoTD->tdm_LRTD_flag == 1 && sts[1]->tc_cnt > 1 /*&& abs(hStereoTD->tdm_inst_ratio_idx-LRTD_STEREO_MID_IS_PRIM) > 5*/ )
    {
        if ( tdm_SM_or_LRTD_Pri == 0 ) /* Primary is right */