Commit 9bb06dc9 authored by vaclav's avatar vaclav
Browse files

fix for MC format with HR + sanity checks for invalid bitstream signaling

parent f4fda707
Loading
Loading
Loading
Loading
Loading
+104 −10
Original line number Diff line number Diff line
@@ -105,7 +105,7 @@ ivas_error ivas_dec_get_format(
{
    int16_t k, idx, num_bits_read;
    int16_t nchan_ism, element_mode_flag;
    int16_t sba_order, sba_analysis_order;
    int16_t sba_order, sba_planar, sba_analysis_order;
    int32_t ivas_total_brate;
    uint16_t *bit_stream_orig;
    AUDIO_CONFIG signaled_config;
@@ -122,6 +122,16 @@ ivas_error ivas_dec_get_format(

    ivas_read_format( st_ivas, &num_bits_read );

    if ( st_ivas->ini_frame > 0 && st_ivas->ivas_format != st_ivas->last_ivas_format &&
         !( st_ivas->ivas_format == MASA_FORMAT && st_ivas->last_ivas_format == MASA_ISM_FORMAT ) &&
         !( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->last_ivas_format == MASA_FORMAT ) )
    {
#ifdef DEBUGGING
        fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
#endif
        return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
    }

    /*-------------------------------------------------------------------*
     * Read other signaling (ISM/MC mode, number of channels, etc.)
     *-------------------------------------------------------------------*/
@@ -151,6 +161,14 @@ ivas_error ivas_dec_get_format(
                k--;
            }

            if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
            }

            st_ivas->nchan_ism = nchan_ism;
            st_ivas->ism_mode = ivas_ism_mode_select( nchan_ism, ivas_total_brate );

@@ -163,13 +181,29 @@ ivas_error ivas_dec_get_format(
        else if ( st_ivas->ivas_format == SBA_FORMAT )
        {
            /* read Ambisonic (SBA) planar flag */
            /*sba_planar = st_ivas->bit_stream[num_bits_read];*/
            sba_planar = st_ivas->bit_stream[num_bits_read];
            num_bits_read += SBA_PLANAR_BITS;

            if ( st_ivas->ini_frame > 0 && sba_planar != st_ivas->sba_planar )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the SBA planar/3D layout is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA planar flag signalled!" );
            }

            /* read Ambisonic (SBA) order */
            sba_order = st_ivas->bit_stream[num_bits_read + 1];
            sba_order += 2 * st_ivas->bit_stream[num_bits_read];

            if ( st_ivas->ini_frame > 0 && sba_order != st_ivas->sba_order )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the SBA order is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA order signalled!" );
            }

            sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, sba_order );
            st_ivas->nchan_transport = ivas_get_sba_num_TCs( ivas_total_brate, sba_analysis_order );
        }
@@ -188,9 +222,9 @@ ivas_error ivas_dec_get_format(

            /* this should be non-zero if original input format was MASA_ISM_FORMAT */
            st_ivas->ism_mode = ISM_MODE_NONE;
            st_ivas->nchan_ism = st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 3] + 2 * st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 2];
            nchan_ism = st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 3] + 2 * st_ivas->bit_stream[( ivas_total_brate / FRAMES_PER_SEC ) - 2];

            if ( st_ivas->nchan_ism > 0 )
            if ( nchan_ism > 0 )
            {
                /* the input_ivas_format should be MASA_ISM_FORMAT, but we cannot initialize it now */
                /* info about the number of objects:
@@ -200,34 +234,76 @@ ivas_error ivas_dec_get_format(
                          '11' - MASA_ISM_FORMAT at the encoder, with 1 or 2 objects
                          reading if 1 or 2 objects is performed later
                */
                st_ivas->nchan_ism = 5 - st_ivas->nchan_ism;
                if ( st_ivas->nchan_transport == 1 && st_ivas->nchan_ism == 2 )
                nchan_ism = 5 - nchan_ism;
                if ( st_ivas->nchan_transport == 1 && nchan_ism == 2 )
                {
                    st_ivas->nchan_ism = 1;
                    nchan_ism = 1;
                }

                /* for MASA_ISM_FORMAT at input the number of MASA transport channels is always 2 and the corresponding bit is not used here*/
                st_ivas->nchan_transport = 2;
                element_mode_flag = 1;
            }
            else
            {
                nchan_ism = 5;
            }

            if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
            }

            st_ivas->nchan_ism = nchan_ism;
        }
        else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
        {
            st_ivas->nchan_transport = 2; /* always 2 MASA transport channels */

            /* the number of objects are written at the end of the bitstream */
            st_ivas->nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
            nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
            st_ivas->ism_mode = ivas_omasa_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );

            if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
            }

            st_ivas->nchan_ism = nchan_ism;
        }
        else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
        {
            /* the number of objects is written at the end of the bitstream, in the SBA metadata */
            st_ivas->nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;
            nchan_ism = 2 * st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 1] + st_ivas->bit_stream[ivas_total_brate / FRAMES_PER_SEC - 2] + 1;

            if ( st_ivas->ini_frame > 0 && nchan_ism != st_ivas->nchan_ism )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the number of ISMs is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong number of objects signalled!" );
            }

            st_ivas->nchan_ism = nchan_ism;

            /* read Ambisonic (SBA) planar flag */
            /*sba_planar = st_ivas->bit_stream[num_bits_read];*/
            sba_planar = st_ivas->bit_stream[num_bits_read];
            num_bits_read += SBA_PLANAR_BITS;

            if ( st_ivas->ini_frame > 0 && sba_planar != st_ivas->sba_planar )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the SBA planar/3D layout is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA planar flag signalled!" );
            }

            /* read Ambisonic (SBA) order (0 for signaling OSBA format at low bitrates)*/
            sba_order = st_ivas->bit_stream[num_bits_read + 1];
            sba_order += 2 * st_ivas->bit_stream[num_bits_read];
@@ -241,6 +317,14 @@ ivas_error ivas_dec_get_format(
                num_bits_read += SBA_ORDER_BITS;
            }

            if ( st_ivas->ini_frame > 0 && sba_order != st_ivas->sba_order )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Changing the SBA order is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong SBA order signalled!" );
            }

            st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->nchan_ism );

            sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, sba_order );
@@ -260,7 +344,17 @@ ivas_error ivas_dec_get_format(
            num_bits_read += MC_LS_SETUP_BITS;

            signaled_config = ivas_mc_map_ls_setup_to_output_config( idx );

            if ( st_ivas->ini_frame > 0 && st_ivas->transport_config != signaled_config )
            {
#ifdef DEBUGGING
                fprintf( stderr, "\nError: Switching of MC configurations is not supported!\n" );
#endif
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "wrong MC configuration signalled!" );
            }

            st_ivas->mc_mode = ivas_mc_mode_select( ivas_mc_map_output_config_to_mc_ls_setup( signaled_config ), st_ivas->hDecoderConfig->ivas_total_brate );
            st_ivas->transport_config = signaled_config;
        }

        /*-------------------------------------------------------------------*