Commit 74122691 authored by Jan Kiene's avatar Jan Kiene
Browse files

add new pcm comparison to all files

parent dc847d1f
Loading
Loading
Loading
Loading

tests/cmp_custom.py

deleted100755 → 0
+0 −201
Original line number Diff line number Diff line
#!/usr/bin/env python3

__copyright__ = \
"""
(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 compare samples in 2 PCM files.

USAGE : cmp_custom.py  file_1   file_2   sample_size_in_bytes   tolerance   [end_samples_to_skip]
file_1, file_2 : files to compare
sample_size_in_bytes : 1, 2, 4, 8, these many bytes will be compared in single iteration
tolerance : abs error tolerance, will be computed based on sample_size_in_bytes
end_samples_to_skip : num of samples to be skipped at the end
"""

import sys
import platform


class CompareSamples:
    """
    A class to compare PCM samples.
    """

    def __init__(
        self,
        filename_1: str,
        filename_2: str,
        sample_size_in_bytes: int,
        tolerance: int,
        end_samples_to_skip: int,
    ):
        self.file_1 = open(filename_1, "rb")
        self.file_2 = open(filename_2, "rb")
        self.sample_size = sample_size_in_bytes
        self.tolerance = tolerance
        self.end_samples_to_skip = end_samples_to_skip
        self.samples = 0
        self.max_diff = 0
        self.max_diff_sample_num = 0
        self.diff_present = False
        self.first_diff_sample_num = 0
        self.first_diff = 0
        self.file_samples_to_read = 0
        self.file_size_1_samples = 0
        self.file_size_2_samples = 0

    def get_file_sizes(self):
        """
        Determine the file sizes in samples of the 2 PCM files.
        """
        self.file_1.seek(0, 2)
        self.file_2.seek(0, 2)
        self.file_size_1_samples = self.file_1.tell() / self.sample_size
        self.file_size_2_samples = self.file_2.tell() / self.sample_size
        self.file_samples_to_read = (
            min(self.file_size_1_samples, self.file_size_2_samples)
            - self.end_samples_to_skip
        )
        self.file_1.seek(0)
        self.file_2.seek(0)

    def print_summary(self) -> (int, str):
        """
        Print the summary of the comparison.
        """
        print("Compare Custom Report")
        print("=====================")
        print(
            f"file size in samples: file 1 = {self.file_size_1_samples},",
            f"file 2 = {self.file_size_2_samples}",
        )
        if self.file_size_1_samples != self.file_size_2_samples:
            print("WARNING !!!! file size different")
        print(f"Total number of samples compared = {self.samples}")
        if not self.diff_present:
            print("Comparison success")
            print("")
            return 0, "Comparison success"

        # comparison failed
        print(
            f"First unmatched diff ==> {self.first_diff}",
            f"at sample num {self.first_diff_sample_num}",
        )
        diff_msg = f"MAXIMUM ABS DIFF ==> {self.max_diff} at sample num {self.max_diff_sample_num}"
        print(diff_msg)
        print("Comparison failed")
        print("")
        return 1, f"Comparison failed, {diff_msg}"

    def compare_next_sample(self):
        """
        Compare the next input sample from both files.
        """
        if self.samples == self.file_samples_to_read:
            return 1
        val1_c = self.file_1.read(self.sample_size)
        val2_c = self.file_2.read(self.sample_size)
        if (len(val1_c) != self.sample_size) or (len(val2_c) != self.sample_size):
            return 1

        val1 = int.from_bytes(val1_c, byteorder="little", signed=True)
        val2 = int.from_bytes(val2_c, byteorder="little", signed=True)

        self.samples = self.samples + 1
        abs_diff = (val1 - val2) if (val1 > val2) else (val2 - val1)
        if abs_diff > self.tolerance:
            if abs_diff > self.max_diff:
                self.max_diff = abs_diff
                self.max_diff_sample_num = self.samples
            if not self.diff_present:
                self.first_diff = abs_diff
                self.first_diff_sample_num = self.samples
                self.diff_present = True
        return 0


def usage():
    print(__doc__)
    return 1, ""


def cmp_custom(
    file_1_name,
    file_2_name,
    sample_size_in_bytes_str,
    tolerance_str,
    end_samples_to_skip_str="0",
) -> (int, str):
    """
    Function to compare the samples in 2 PCM files.
    """

    # 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()
        )

    sample_size_in_bytes = int(sample_size_in_bytes_str)
    if sample_size_in_bytes not in [1, 2, 4, 8]:
        print(f"Error: unsupported sample size ({sample_size_in_bytes})")
        return usage()

    cmp_samples = CompareSamples(
        file_1_name,
        file_2_name,
        sample_size_in_bytes,
        int(tolerance_str),
        int(end_samples_to_skip_str),
    )

    cmp_samples.get_file_sizes()

    result = 0
    while result == 0:
        result = cmp_samples.compare_next_sample()

    return cmp_samples.print_summary()


def main(argv) -> int:
    if len(argv) < 5:
        return usage()
    retval, _reason = cmp_custom(*argv[1:])
    return retval


if __name__ == "__main__":
    sys.exit(main(sys.argv))
+7 −23
Original line number Diff line number Diff line
@@ -39,9 +39,9 @@ import errno
import platform
from subprocess import run
import pytest
from cmp_custom import cmp_custom
from cmp_pcm import cmp_pcm
from conftest import EncoderFrontend, DecoderFrontend
from testconfig import PARAM_FILE
from testconfig import PARAM_FILE, OC_TO_NCHANNELS


VALID_DEC_OUTPUT_CONF = [
@@ -271,12 +271,12 @@ def test_param_file_tests(
        tracefile_dec,
    )

    # compare
    if update_ref in [0, 2]:
        compare(
            f"{dut_base_path}/param_file/dec/{output_file}",
            f"{reference_path}/param_file/dec/{output_file}",
        )
        dut_file = f"{dut_base_path}/param_file/dec/{output_file}"
        ref_file = f"{reference_path}/param_file/dec/{output_file}"
        fs = int(sampling_rate) * 1000
        cmp_result, reason = cmp_pcm(dut_file, ref_file, OC_TO_NCHANNELS[output_config], fs)
        assert cmp_result == 0, reason

        # remove DUT output files when test result is OK (to save disk space)
        if not keep_files:
@@ -438,19 +438,3 @@ def decode(
            dut_out_file,
            add_option_list=add_option_list,
        )


def compare(
    pcm_file_1,
    pcm_file_2,
):
    """
    Compare two PCM files.
    Currently, both PCM files are treated like mono files.
    This is just fine when checking for bit-exactness.
    More advanced comparisons are possible and might come with a future update.
    """
    sample_size = "2"  # 16-bit samples
    tolerance = "0"  # zero tolerance for BE testing
    cmp_result, reason = cmp_custom(pcm_file_1, pcm_file_2, sample_size, tolerance)
    assert cmp_result == 0, reason
+3 −11
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ import os
import errno
import pytest

from cmp_custom import cmp_custom
from cmp_pcm import cmp_pcm
from conftest import DecoderFrontend

# params
@@ -174,16 +174,8 @@ def sba_dec_plc(
        )

        # --------------  compare cmd  --------------

        end_skip_samples = '0'

        cmp_result, reason = cmp_custom(
            dut_out_raw,
            ref_out_raw,
            "2",
            AbsTol,
            end_skip_samples
        )
        fs = int(sampling_rate) * 1000
        cmp_result, reason = cmp_pcm(dut_out_raw, ref_out_raw, OC_TO_NCHANNELS[output_config], fs)

        # report compare result
        assert cmp_result == 0, reason