Commit 7d578b52 authored by lefort's avatar lefort
Browse files

Test of binary files added.

parent 1fb64ee0
Loading
Loading
Loading
Loading
+46 −4
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@
"""

from pathlib import Path

import re

TESTS_DIR = Path(__file__).parent
SCRIPTS_DIR = TESTS_DIR.parents[1].joinpath("scripts").resolve()
@@ -42,11 +42,11 @@ HRTF_BINARY_DIR = SCRIPTS_DIR.joinpath("binauralRenderer_interface", "bin")
DEC_BINARY_DIR = TESTS_DIR.joinpath("dec_out_bin")

ENCODER_CMD = [
    str(TESTS_DIR.parent.parent.joinpath("IVAS_cod"))
    str(TESTS_DIR.parent.parent.joinpath("build/IVAS_cod"))
]

DECODER_CMD = [
    str(TESTS_DIR.parent.parent.joinpath("IVAS_dec"))
    str(TESTS_DIR.parent.parent.joinpath("build/IVAS_dec"))
]

HRTF_BINARY_FILE = "default_rom_{}kHz.bin"
@@ -89,3 +89,45 @@ FORMAT_TO_METADATA_FILES = {

OUTPUT_FORMATS_BINAURAL = ["BINAURAL", "BINAURAL_ROOM_IR"] # "BINAURAL_ROOM_REVERB"
HR_TRAJECTORIES_TO_TEST = ["headrot_case00_3000_q", "headrot"]


with open(str(TESTS_DIR.parent.parent.joinpath("lib_com")) + "/ivas_cnst.h", "r") as ivas_cnst_file:

    ivas_cnst = ivas_cnst_file.read()

    # Declare renderer_types
    rtid = ivas_cnst.find('RENDERER_TYPE;')
    rte_c = ivas_cnst[ivas_cnst[:rtid].rfind("{") : ivas_cnst[:rtid].rfind("}")]
    enumid = 0
    for rtid1 in [m.start() for m in re.finditer('RENDERER_', rte_c)] :
        rtid2 = rte_c[rtid1:].find(",")
        if rtid2 == -1:
            rtid2 = rte_c[rtid1:].find(" ")
        globals()[rte_c[rtid1 : rtid1 + rtid2]] = enumid
        enumid += 1

    # Declare binaural_input_audio_config
    biacid = ivas_cnst.find('BINAURAL_INPUT_AUDIO_CONFIG;')
    biace_c = ivas_cnst[ivas_cnst[:biacid].rfind("{") : ivas_cnst[:biacid].rfind("}")]
    enumid = 0
    for biacid1 in [m.start() for m in re.finditer('BINAURAL_INPUT_AUDIO_CONFIG_', biace_c)] :
        biacid2 = biace_c[biacid1:].find(",")
        if biacid2 == -1:
            biacid2 = biace_c[biacid1:].find(" ")
        globals()[biace_c[biacid1 : biacid1 + biacid2]] = enumid
        enumid += 1

REQ_HRTF_CONFIG = [
    {"renderer_type": RENDERER_BINAURAL_FASTCONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_COMBINED, "sampling_frequency" : 48000},
    {"renderer_type": RENDERER_BINAURAL_FASTCONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_HOA3, "sampling_frequency" : 48000},
    {"renderer_type": RENDERER_BINAURAL_FASTCONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_HOA2, "sampling_frequency" : 48000},
    {"renderer_type": RENDERER_BINAURAL_FASTCONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_FOA, "sampling_frequency" : 48000},
    {"renderer_type": RENDERER_BINAURAL_PARAMETRIC_ROOM, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_UNDEFINED, "sampling_frequency" : 48000},
    {"renderer_type": RENDERER_BINAURAL_OBJECTS_TD, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_UNDEFINED, "sampling_frequency" : -1},
    {"renderer_type": RENDERER_BINAURAL_MIXER_CONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_COMBINED, "sampling_frequency" : -1},
    {"renderer_type": RENDERER_BINAURAL_MIXER_CONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_HOA3, "sampling_frequency" : -1},
    {"renderer_type": RENDERER_BINAURAL_MIXER_CONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_HOA2, "sampling_frequency" : -1},
    {"renderer_type": RENDERER_BINAURAL_MIXER_CONV, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_FOA, "sampling_frequency" : -1},
    {"renderer_type": RENDERER_BINAURAL_MIXER_CONV_ROOM, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_COMBINED, "sampling_frequency" : -1},
    {"renderer_type": RENDERER_BINAURAL_REVERB_ALL, "input_configuration" : BINAURAL_INPUT_AUDIO_CONFIG_UNDEFINED, "sampling_frequency" : -1},
]
 No newline at end of file
+10 −0
Original line number Diff line number Diff line
@@ -33,6 +33,16 @@ import pytest

from tests.binaural.utils import *

""" Binary file """

@pytest.mark.parametrize("out_fs", SAMPLE_RATE)
def test_binary_file(test_info, out_fs):

    check_binary_file(
        test_info, 
        out_fs,
    )

""" Multichannel """

@pytest.mark.parametrize("out_fmt", OUTPUT_FORMATS_BINAURAL)
+104 −0
Original line number Diff line number Diff line
@@ -185,6 +185,108 @@ def get_option_list_str(option_list):
    return option_list_str


def check_binary_file(test_info, out_fs):
    
    with open(str(HRTF_BINARY_DIR.joinpath(HRTF_BINARY_FILE.format(out_fs))), 'rb') as file:
        
        binary_data = file.read()

        # [Declaration of the binary file]
        #      File Identifier         (8 bytes)
        #      Size of file in bytes   (4 bytes)
        #      Number of HRTF          (2 bytes)
        #      Max length of HRTF data (4 bytes)

        binary_file_size = os.path.getsize(str(HRTF_BINARY_DIR.joinpath(HRTF_BINARY_FILE.format(out_fs))))
        if binary_file_size < 18 :
            pytest.fail("HRTF binary file not compliant (size of file header)")

        file_header = binary_data[:18]

        file_identifier = file_header[:8].decode()
        if file_identifier != "IVASHRTF" :
            pytest.fail("Header of HRTF binary file not compliant (identifier)")

        file_size = int.from_bytes(file_header[8:12], byteorder='little')
        if file_size != binary_file_size  :
            pytest.fail("Header of HRTF binary file not compliant (file size)")

        nb_hrtf = int.from_bytes(file_header[12:14], byteorder='little')
        max_data_size = int.from_bytes(file_header[14:18], byteorder='little')
        
        read_bid = 18

        hrtf_cnt = 0
        raw_data_size_max = 0
        hrtf_config_file = []
        while read_bid < file_size :

            if read_bid + 16 > file_size  :
                pytest.fail("Reading of HRTF header failed")

            # HRTF Header                                                              */
            #      Renderer type         (4 bytes) : See "RENDERER_TYPE"               */
            #      Input configuration   (4 bytes) : See "BINAURAL_INPUT_AUDIO_CONFIG" */
            #      Sampling Frequency    (4 bytes)                                     */
            #      Raw data size         (4 bytes)                                     */

            hrtf_header = binary_data[read_bid:read_bid+16]

            renderer_type = int.from_bytes(hrtf_header[:4], byteorder='little')
            input_configuration = int.from_bytes(hrtf_header[4:8], byteorder='little')
            sampling_frequency = int.from_bytes(hrtf_header[8:12], byteorder='little')
            raw_data_size = int.from_bytes(hrtf_header[12:16], byteorder='little')

            if  (renderer_type != RENDERER_BINAURAL_MIXER_CONV) \
            and (renderer_type != RENDERER_BINAURAL_MIXER_CONV_ROOM) \
            and (renderer_type != RENDERER_BINAURAL_FASTCONV) \
            and (renderer_type != RENDERER_BINAURAL_FASTCONV_ROOM) \
            and (renderer_type != RENDERER_BINAURAL_PARAMETRIC) \
            and (renderer_type != RENDERER_BINAURAL_PARAMETRIC_ROOM) \
            and (renderer_type != RENDERER_BINAURAL_OBJECTS_TD) \
            and (renderer_type != RENDERER_BINAURAL_REVERB_ALL) :
                pytest.fail("Header of HRTF binary file not compliant (renderer type)")

            if  (input_configuration != BINAURAL_INPUT_AUDIO_CONFIG_COMBINED) \
            and (input_configuration != BINAURAL_INPUT_AUDIO_CONFIG_HOA3) \
            and (input_configuration != BINAURAL_INPUT_AUDIO_CONFIG_HOA2) \
            and (input_configuration != BINAURAL_INPUT_AUDIO_CONFIG_FOA) \
            and (input_configuration != BINAURAL_INPUT_AUDIO_CONFIG_UNDEFINED) :
                pytest.fail("Header of HRTF binary file not compliant (input configuration)")

            if  (sampling_frequency != 16000) \
            and (sampling_frequency != 32000) \
            and (sampling_frequency != 48000) :
                pytest.fail("Header of HRTF binary file not compliant (sampling frequency)")

            if raw_data_size_max < raw_data_size:
                raw_data_size_max = raw_data_size

            hrtf_config_file.append({"renderer_type": renderer_type, "input_configuration" : input_configuration, "sampling_frequency" : sampling_frequency})

            # hrtf_raw_data => Tested in compare_rom_vs_binary
            read_bid += 16 + raw_data_size
            
            hrtf_cnt += 1

        if read_bid != file_size  :
            pytest.fail("Inconsistent size of binary file")

        if hrtf_cnt != nb_hrtf  :
            pytest.fail("Inconsistent number of HRTF in binary file")

        if raw_data_size_max != max_data_size:
            pytest.fail("Inconsistent max data size")

        for hrtf_config in REQ_HRTF_CONFIG :
            config = next((cfg for cfg in hrtf_config_file if (cfg["renderer_type"] == hrtf_config["renderer_type"]) and (cfg["input_configuration"] == hrtf_config["input_configuration"])), None)
            if config == None:
                pytest.fail(f"Configuration (renderer_type : {hrtf_config['renderer_type']} - input_configuration : {hrtf_config['input_configuration']}) not found in binary file!")
            if (((hrtf_config["sampling_frequency"] == -1) and (config["sampling_frequency"] != int(out_fs)*1000)) \
             or ((hrtf_config["sampling_frequency"] != -1) and (config["sampling_frequency"] != hrtf_config["sampling_frequency"]))):
                pytest.fail(f"Bad sampling frequency for configuration (renderer_type : {hrtf_config['renderer_type']} - input_configuration : {hrtf_config['input_configuration']}) : {config['sampling_frequency']} instead of {int(out_fs)*1000}!")


def compare_rom_vs_binary(
    test_info, 
    option_list_enc,
@@ -198,6 +300,8 @@ def compare_rom_vs_binary(
    option_str = "_".join(get_option_list_str(option_list_enc))
    file_ext = f"_{option_str}_{bitrate}_{in_fs}-{out_fs}_{out_fmt}"

    check_binary_file(out_fs)

    input_path = TESTV_DIR.joinpath(in_file).with_suffix(".wav")
    bitstream_path = BITSTREAM_DIR.joinpath(in_file + file_ext)
    run_encoder(bitrate, in_fs, input_path, bitstream_path, add_option_list=option_list_enc)