1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# -----------------------------------------------------------------------------
4# Copyright 2021-2022 Arm Limited
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may not
7# use this file except in compliance with the License. You may obtain a copy
8# of the License at:
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17# -----------------------------------------------------------------------------
18"""
19A benchmarking sweep helper, which can generate a performance-vs-quality sweep
20for a single input images. Like other test functionality, this uses structured
21image directory layouts for determining image settings to pass to the codec.
22"""
23
24import argparse
25import os
26import platform
27import sys
28
29import testlib.encoder as te
30import testlib.image as ti
31
32
33def parse_command_line():
34    """
35    Parse the command line.
36
37    Returns:
38        Namespace: The parsed command line container.
39    """
40    parser = argparse.ArgumentParser()
41
42    # All reference encoders
43    parser.add_argument("--step", dest="step", default="10", type=int, help="step size")
44    parser.add_argument("--repeats", dest="repeats", type=int, default=1, help="repeats")
45
46    parser.add_argument(dest="image", default=None,
47                        help="select the test image to run")
48
49    args = parser.parse_args()
50    return args
51
52
53def main():
54    """
55    The main function.
56
57    Returns:
58        int: The process return code.
59    """
60    # Parse command lines
61    args = parse_command_line()
62
63    blockSizes = ["4x4", "5x5", "6x6", "8x8", "10x10"]
64    repeats = max(args.repeats, 1)
65    step = max(args.step, 1)
66
67    image = ti.TestImage(args.image)
68    codec = te.Encoder2x("avx2")
69
70    print("Block Size, Quality, PSNR (dB), Coding Time (s), Coding Rate (MT/s)")
71
72    for blockSize in blockSizes:
73        for quality in range(0, 101, args.step):
74            localRepeats = repeats
75            if quality < 20:
76                localRepeats = localRepeats * 2
77            if quality < 40:
78                localRepeats = localRepeats * 2
79
80            results = codec.run_test(image, blockSize, f"{quality}", localRepeats, False)
81            psnr = results[0]
82            codingTime = results[2]
83            mts = results[3]
84
85            print(f"{blockSize}, {quality}, {psnr}, {codingTime}, {mts}")
86
87    return 0
88
89
90if __name__ == "__main__":
91    sys.exit(main())
92