Commit ad0203af authored by BOHMRR's avatar BOHMRR
Browse files

SBA pytest: added cut_pcm.py to use shorter FOA test vectors to speed up the testing

parent 0987fbd2
Loading
Loading
Loading
Loading
+113 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

__license__ = """
(C) 2022 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.
"""

__doc__ = """
Script to cut samples from a 16-bit PCM file.

USAGE : cut_pcm.py  in_file_pcm  out_file_pcm  num_channels  sample_rate  start  duration  [gain]
in_file_pcm:  input PCM file
out_file_pcm: output PCM file
num_channels: number of channels
sample_rate:  sample rate in Hz
start:        first sample from input file in seconds
duration:     duration in seconds
gain:         optional gain value to apply to the copied samples
"""

import sys
import platform


def usage():
    print(__doc__)
    return 1


def cut_samples(in_file, out_file, num_channels, sample_rate, start, duration, gain="1.0"):
    """
    Function to cut samples from a 16-bit PCM file.
    """

    # check for python >= 3.7
    if sys.version_info[0] < 3 or sys.version_info[1] < 7:
        sys.exit("This script is written for Python >= 3.7. Found: " + platform.python_version())

    # all input parameters are strings - convert some
    num_ch = int(num_channels)
    fs = int(sample_rate)
    start_sec = float(start)
    dur_sec = float(duration)
    gain_f = float(gain)

    with open(in_file, "rb") as fid_in:
        fid_in.seek(0, 2)
        num_in_samples = fid_in.tell() / 2
        num_in_samples_ch = num_in_samples / num_ch
        num_samples_to_skip = int(start_sec * fs)
        dur_samples = dur_sec * fs
        if num_samples_to_skip + dur_samples > num_in_samples_ch:
            sys.exit(
                f"requested too many samples ({num_samples_to_skip}+{dur_samples})"
                + f" - input is too short ({num_in_samples_ch})"
            )
        num_bytes_to_skip = num_samples_to_skip * num_ch * 2
        num_bytes_to_copy = dur_samples * num_ch * 2
        fid_in.seek(num_bytes_to_skip, 0)
        num_clip = 0
        with open(out_file, "wb") as fid_out:
            bytes_written = 0
            while bytes_written < num_bytes_to_copy:
                data = fid_in.read(2)
                val = int.from_bytes(data, byteorder="little", signed=True)
                val = int(val * gain_f)
                if val > 32767:
                    val = 32767
                    num_clip += 1
                if val < -32768:
                    val = -32768
                    num_clip += 1
                data = val.to_bytes(2, byteorder="little", signed=True)
                written = fid_out.write(data)
                assert written == 2, f"Error writing data: {written} != {2}"
                bytes_written += 2
        if num_clip:
            print(f"{num_clip} output samples have been clipped.")


def main(argv):
    if len(argv) < 7:
        return usage()
    return cut_samples(*argv[1:])


if __name__ == "__main__":
    sys.exit(main(sys.argv))
+4 −0
Original line number Diff line number Diff line
@@ -149,6 +149,10 @@ def spar_foa_ref_enc(
        tag_out += '_AGC1'

    input_path = f"{test_vector_path}/{tag}.pcm"
    cut_file = f"{test_vector_path}/{tag}_cut.pcm"
    # we expect that the reference generation for create_ref did already run and the cut PCM files are available
    assert os.path.exists(cut_file)
    input_path = cut_file

    ref_pkt_file = f"{ref_out_dir}/{tag_out}.pkt"

+13 −11
Original line number Diff line number Diff line
@@ -43,10 +43,9 @@ sys.path.append('scripts/ivas_pytests/')
sys.path.append('scripts/ivas_pytests/tests/')
from il2mm import il2mm
from cmp_custom import cmp_custom
from cut_pcm import cut_samples
from conftest import EncoderFrontend, DecoderFrontend

# disable SPAR HOA tests until SPAR_HOA macro is enabled
SPAR_HOA_TEST = 1
#params
tag_list = ['stvFOA']
tag_list_HOA2 = ['test_HOA2_v3']
@@ -234,10 +233,6 @@ def test_spar_hoa2_enc_system(
    ivas_br,
    tag
):
    # until SPAR HOA is enabled
    if not SPAR_HOA_TEST:
        pytest.skip()

    fs = '48'
    dtx = '0'
    agc = -1
@@ -303,10 +298,6 @@ def test_spar_hoa3_enc_system(
    ivas_br,
    tag
):
    # until SPAR HOA is enabled
    if not SPAR_HOA_TEST:
        pytest.skip()

    fs = '48'
    dtx = '0'
    agc = -1
@@ -542,6 +533,17 @@ def spar_foa_enc(
    bypass_mode = bypass if bypass >= 0 else None
    dtx_mode = dtx == '1'

    if in_extension == '.pcm':
        # cut input PCM file - currently with fixed (i.e. not test dependant) values
        num_channels = "4" # currently only FOA inputs end up, here
        cut_from = "0.0"
        cut_len = "5.0"
        cut_gain = "1.0"
        cut_file = f"{test_vector_path}/{tag_in}_cut{in_extension}"
        if not os.path.exists(cut_file):
            cut_samples(input_path, cut_file, num_channels, sampling_rate + "000", cut_from, cut_len, cut_gain)
        input_path = cut_file

    if ref_encoder_path:
        ref_encoder = EncoderFrontend(ref_encoder_path, "REF")
        # call REF encoder