Commit 54159e54 authored by Jan Kiene's avatar Jan Kiene
Browse files

restructure decoder tests as well

parent 43e4bdc1
Loading
Loading
Loading
Loading
+140 −0
Original line number Diff line number Diff line
__copyright__ = """
(C) 2022-2023 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
contributors to this repository. All Rights Reserved.

This software is protected by copyright law and by international treaties.
The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
contributors to this repository retain full ownership rights in their respective contributions in
the software. This notice grants no license of any kind, including but not limited to patent
license, nor is any license granted by implication, estoppel or otherwise.

Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
contributions.

This software is provided "AS IS", without any express or implied warranties. The software is in the
development stage. It is intended exclusively for experts who have experience with such software and
solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
and fitness for a particular purpose are hereby disclaimed and excluded.

Any dispute, controversy or claim arising under or in relation to providing this software shall be
submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
the United Nations Convention on Contracts on the International Sales of Goods.
"""

import numpy as np
import pytest
from contextlib import contextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
import subprocess
from .. import DUT_PATH, REF_PATH, TESTV_PATH, HERE, is_be_to_ref, get_bitstream_path
from ..constants import BS_PROC_FER_15, BS_PROC_JBM_DLY_PROF_5


def create_head_rotation_file(
    trajectory: np.ndarray,
    strategy: str,
    switch_time: int,
    length_frames: int,
    axis: str = None,
):
    assert strategy in ["from_array", "rotate_axis", "constant"]

    length_str = f"-l{length_frames}" if length_frames > 0 else ""
    fname = f"head_rotation_trajectory-{strategy}-{switch_time}{length_str}"

    if strategy == "from_array":
        rots = trajectory
    elif strategy == "rotate_axis":
        raise NotImplementedError("only idea for now")
    elif strategy == "constant":
        assert len(trajectory) == 1
        rots = np.repeat(trajectory, length_frames)

    out_path = DUT_PATH.joinpath(fname)
    rots.astype(np.int32).tofile(out_path)

    return out_path


def get_output_path(bitstream_path, output_format, output_sampling_rate):
    output_name = (
        f"{bitstream_path.stem}.dec-{output_format}-{output_sampling_rate}kHz.wav"
    )
    return DUT_PATH.joinpath(output_name)


@contextmanager
def get_bitstream_and_options(
    testv_name, encoder_format, bitrate, sampling_rate, dtx, processing, suffix=""
):
    """
    Utility to get either the stored reference bitstream or the processed version as a temporary file

    The reason for implementing this as a context manager instead of a simple function is that this way
    the temporary processed bitstream files are cleaned up automatically and do not exploce disk space even more...
    """
    options = list()
    with TemporaryDirectory() as tmp_dir:
        bitstream = get_bitstream_path(
            REF_PATH, testv_name, encoder_format, bitrate, sampling_rate, dtx, suffix
        )
        bitstream_out = Path(tmp_dir).joinpath(bitstream.stem + f".{processing}.192")
        if processing == BS_PROC_FER_15:
            ep_path = TESTV_PATH.joinpath("ltv_ep_015.192")
            cmd = [
                # TODO: adapt for windows
                "eid-xor",
                "-fer",
                "-vbr",
                str(bitstream),
                str(ep_path),
                str(bitstream_out),
            ]
            subprocess.run(cmd)
            bitstream = bitstream_out
        elif processing == BS_PROC_JBM_DLY_PROF_5:
            dly_profile_path = HERE.joinpath(
                "../../scripts/dly_error_profiles/dly_error_profile_5.dat"
            )
            tracefile_path = Path(tmp_dir).joinpath(bitstream_out.with_suffix(".trace"))
            # TODO: adapt for windows
            # TODO: get number of frames per packet from error profile name
            cmd = [
                "networkSimulator_g192",
                str(dly_profile_path),
                str(bitstream),
                str(bitstream_out),
                str(tracefile_path),
                "2",
                "0",
            ]
            subprocess.run(cmd)
            bitstream = bitstream_out
            options.append("-VOIP")

        yield bitstream, options


def run_check(
    ref_bitstream,
    output_format,
    sampling_rate,
    dut_output,
    options,
    decoder_frontend,
    is_ref_creation,
):
    decoder_frontend.run(
        output_format, sampling_rate, ref_bitstream, dut_output, add_option_list=options
    )

    if not is_ref_creation and not is_be_to_ref(dut_output):
        pytest.fail(f"Decoder output differs from reference")
+3 −121
Original line number Diff line number Diff line
@@ -29,127 +29,9 @@ the United Nations Convention on Contracts on the International Sales of Goods.
"""

import pytest
import numpy as np
from . import (
    is_be_to_ref,
    get_bitstream_path,
    get_testv_path,
    REF_PATH,
    DUT_PATH,
    TESTV_PATH,
    HERE,
)
from .constants import *
from pathlib import Path
import subprocess
import random
from contextlib import contextmanager
from tempfile import TemporaryDirectory


### --------------- Helper functions ---------------


def create_head_rotation_file(
    trajectory: np.ndarray,
    strategy: str,
    switch_time: int,
    length_frames: int,
    axis: str = None,
):
    assert strategy in ["from_array", "rotate_axis", "constant"]

    length_str = f"-l{length_frames}" if length_frames > 0 else ""
    fname = f"head_rotation_trajectory-{strategy}-{switch_time}{length_str}"

    if strategy == "from_array":
        rots = trajectory
    elif strategy == "rotate_axis":
        raise NotImplementedError("only idea for now")
    elif strategy == "constant":
        assert len(trajectory) == 1
        rots = np.repeat(trajectory, length_frames)

    out_path = DUT_PATH.joinpath(fname)
    rots.astype(np.int32).tofile(out_path)

    return out_path


def get_output_path(bitstream_path, output_format, output_sampling_rate):
    output_name = (
        f"{bitstream_path.stem}.dec-{output_format}-{output_sampling_rate}kHz.wav"
    )
    return DUT_PATH.joinpath(output_name)


@contextmanager
def get_bitstream_and_options(
    testv_name, encoder_format, bitrate, sampling_rate, dtx, processing, suffix=""
):
    """
    Utility to get either the stored reference bitstream or the processed version as a temporary file

    The reason for implementing this as a context manager instead of a simple function is that this way
    the temporary processed bitstream files are cleaned up automatically and do not exploce disk space even more...
    """
    options = list()
    with TemporaryDirectory() as tmp_dir:
        bitstream = get_bitstream_path(
            REF_PATH, testv_name, encoder_format, bitrate, sampling_rate, dtx, suffix
        )
        bitstream_out = Path(tmp_dir).joinpath(bitstream.stem + f".{processing}.192")
        if processing == BS_PROC_FER_15:
            ep_path = TESTV_PATH.joinpath("ltv_ep_015.192")
            cmd = [
                # TODO: adapt for windows
                "eid-xor",
                "-fer",
                "-vbr",
                str(bitstream),
                str(ep_path),
                str(bitstream_out),
            ]
            subprocess.run(cmd)
            bitstream = bitstream_out
        elif processing == BS_PROC_JBM_DLY_PROF_5:
            dly_profile_path = HERE.joinpath(
                "../../scripts/dly_error_profiles/dly_error_profile_5.dat"
            )
            tracefile_path = Path(tmp_dir).joinpath(bitstream_out.with_suffix(".trace"))
            # TODO: adapt for windows
            # TODO: get number of frames per packet from error profile name
            cmd = [
                "networkSimulator_g192",
                str(dly_profile_path),
                str(bitstream),
                str(bitstream_out),
                str(tracefile_path),
                "2",
                "0",
            ]
            subprocess.run(cmd)
            bitstream = bitstream_out
            options.append("-VOIP")

        yield bitstream, options


def run_check(
    ref_bitstream,
    output_format,
    sampling_rate,
    dut_output,
    options,
    decoder_frontend,
    is_ref_creation,
):
    decoder_frontend.run(
        output_format, sampling_rate, ref_bitstream, dut_output, add_option_list=options
    )

    if not is_ref_creation and not is_be_to_ref(dut_output):
        pytest.fail(f"Decoder output differs from reference")
from . import get_bitstream_and_options, get_output_path, run_check
from .. import get_testv_path
from ..constants import *


### --------------- Actual testcase definitions ---------------