Commit 43e4bdc1 authored by Jan Kiene's avatar Jan Kiene
Browse files

refactor encoder test organization

parent 0babb6b9
Loading
Loading
Loading
Loading
+127 −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 random
import pytest
from itertools import permutations
from .. import DUT_PATH, TESTV_PATH, is_be_to_ref
from ..constants import METADATA_FOR_INPUT_FORMAT, ISM_MD_NULL, CMDL_OPTIONS_FOR_INPUT_FORMAT, ISM_MD_EXTENDED, INPUT_FORMATS_OBJECT_BASED

def create_br_switching_file(
    bitrates: np.ndarray,
    strategy: str,
    switch_time: int,
    length_frames: int = 0,
    seed: int = None,
):
    """
    Create bitrate switching pattern files on the fly and return path to it

    bitrates - array of bitrate values to include
    strategy - how to create pattern:
        "from_array" - use given array as is
        "exhaustive" - generate array where every bitrate is preceeded and followed at least once by every other bitrate
        "random" - randomly pick from bitrates
    switch_time - number of frames before next switch
    length_frames - for "random" strategy: length of pattern in frames
    seed - for "exhaustive" and "random" strategies: inject seed for shuffling/choosing from array
    """
    assert strategy in ["from_array", "exhaustive", "random"]

    seed_str = f"_{seed}" if strategy == "random" or strategy == "exhaustive" else ""
    length_str = f"-l{length_frames}" if length_frames > 0 else ""
    fname = f"br_sw_pattern-{strategy}{seed_str}-{switch_time}{length_str}"

    # TODO: maybe calculate seed based on given parameter to automatically have reproducibility
    if seed is not None:
        random.seed(seed)

    brs = np.asarray(bitrates)

    # NOTE: nothing to do for from_array
    if strategy == "exhaustive":
        # TODO: review, probably needs more work to be really exhaustive...
        n = len(brs) - 1
        permuts = list(permutations(brs, 2))
        split_permuts = [permuts[i * n : (i + 1) * n] for i in range(len(brs))]
        for i in range(len(split_permuts)):
            random.shuffle(split_permuts[i])
        split_permuts = np.asarray(split_permuts)
        brs = np.concatenate([split_permuts[:, i] for i in range(n)])
    elif strategy == "random":
        brs = np.asarray(random.choices(brs, k=length_frames))

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

    return out_path


def get_md(input_format, md_type=None):
    md_files = METADATA_FOR_INPUT_FORMAT.get(input_format, list())
    if md_type == ISM_MD_NULL:
        md_files = ["NULL" for f in md_files]
    else:
        md_files = [str(TESTV_PATH.joinpath(f)) for f in md_files]
    return md_files


def get_options(input_format, md_type=None):
    options = list(CMDL_OPTIONS_FOR_INPUT_FORMAT[input_format])
    if md_type == ISM_MD_EXTENDED:
        assert input_format in INPUT_FORMATS_OBJECT_BASED
        options[1] = f"+{options[1]}"
    md_options = get_md(input_format, md_type)
    options.extend(md_options)
    return options


def run_check(
    dut_bitstream,
    bitrate,
    sampling_rate,
    testv,
    options,
    encoder_frontend,
    is_ref_creation,
    dtx_mode=False,
):
    encoder_frontend.run(
        bitrate,
        sampling_rate,
        str(testv),
        str(dut_bitstream),
        dtx_mode=dtx_mode,
        add_option_list=options,
    )

    if not is_ref_creation and not is_be_to_ref(dut_bitstream):
        pytest.fail(f"Bitstream file differs from reference")
 No newline at end of file
+3 −106
Original line number Diff line number Diff line
@@ -29,15 +29,9 @@ the United Nations Convention on Contracts on the International Sales of Goods.
"""

import pytest
import numpy as np
from itertools import permutations
import random
from .constants import (
    METADATA_FOR_INPUT_FORMAT,
from ..constants import (
    ISM_MD_NULL,
    CMDL_OPTIONS_FOR_INPUT_FORMAT,
    ISM_MD_EXTENDED,
    INPUT_FORMATS_OBJECT_BASED,
    ENCODER_CHANNEL_BASED_AND_MASA_PARAMS,
    DTX_ON,
    DTX_OFF,
@@ -47,105 +41,8 @@ from .constants import (
    STEREO_DMX_EVS_PARAMS,
    COMBINED_FORMATS_PARAMS,
)
from . import TESTV_PATH, DUT_PATH, is_be_to_ref, get_bitstream_path, get_testv_path


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


def create_br_switching_file(
    bitrates: np.ndarray,
    strategy: str,
    switch_time: int,
    length_frames: int = 0,
    seed: int = None,
):
    """
    Create bitrate switching pattern files on the fly and return path to it

    bitrates - array of bitrate values to include
    strategy - how to create pattern:
        "from_array" - use given array as is
        "exhaustive" - generate array where every bitrate is preceeded and followed at least once by every other bitrate
        "random" - randomly pick from bitrates
    switch_time - number of frames before next switch
    length_frames - for "random" strategy: length of pattern in frames
    seed - for "exhaustive" and "random" strategies: inject seed for shuffling/choosing from array
    """
    assert strategy in ["from_array", "exhaustive", "random"]

    seed_str = f"_{seed}" if strategy == "random" or strategy == "exhaustive" else ""
    length_str = f"-l{length_frames}" if length_frames > 0 else ""
    fname = f"br_sw_pattern-{strategy}{seed_str}-{switch_time}{length_str}"

    # TODO: maybe calculate seed based on given parameter to automatically have reproducibility
    if seed is not None:
        random.seed(seed)

    brs = np.asarray(bitrates)

    # NOTE: nothing to do for from_array
    if strategy == "exhaustive":
        # TODO: review, probably needs more work to be really exhaustive...
        n = len(brs) - 1
        permuts = list(permutations(brs, 2))
        split_permuts = [permuts[i * n : (i + 1) * n] for i in range(len(brs))]
        for i in range(len(split_permuts)):
            random.shuffle(split_permuts[i])
        split_permuts = np.asarray(split_permuts)
        brs = np.concatenate([split_permuts[:, i] for i in range(n)])
    elif strategy == "random":
        brs = np.asarray(random.choices(brs, k=length_frames))

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

    return out_path


def get_md(input_format, md_type=None):
    md_files = METADATA_FOR_INPUT_FORMAT.get(input_format, list())
    if md_type == ISM_MD_NULL:
        md_files = ["NULL" for f in md_files]
    else:
        md_files = [str(TESTV_PATH.joinpath(f)) for f in md_files]
    return md_files


def get_options(input_format, md_type=None):
    options = list(CMDL_OPTIONS_FOR_INPUT_FORMAT[input_format])
    if md_type == ISM_MD_EXTENDED:
        assert input_format in INPUT_FORMATS_OBJECT_BASED
        options[1] = f"+{options[1]}"
    md_options = get_md(input_format, md_type)
    options.extend(md_options)
    return options


def run_check(
    dut_bitstream,
    bitrate,
    sampling_rate,
    testv,
    options,
    encoder_frontend,
    is_ref_creation,
    dtx_mode=False,
):
    encoder_frontend.run(
        bitrate,
        sampling_rate,
        str(testv),
        str(dut_bitstream),
        dtx_mode=dtx_mode,
        add_option_list=options,
    )

    if not is_ref_creation and not is_be_to_ref(dut_bitstream):
        pytest.fail(f"Bitstream file differs from reference")


### --------------- Actual testcase definitions ---------------
from . import get_options, run_check
from .. import get_testv_path, get_bitstream_path, DUT_PATH


# channel-based modes + MASA