Commit 14b375eb authored by Jan Kiene's avatar Jan Kiene
Browse files

add function for br switching file creation

parent c010fe50
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -29,6 +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,
    ISM_MD_NULL,
@@ -50,6 +53,48 @@ from . import TESTV_PATH, DUT_PATH, is_be_to_ref, get_bitstream_path, get_testv_
### --------------- Helper functions ---------------


def create_br_switching_file(bitrates: np.ndarray, strategy: str, switch_time: int, length_frames: int, 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_list" - 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_list", "exhaustive", "random"]

    seed_str = f"_{seed}" if strategy == "random" else ""
    fname = f"br_sw_pattern-{strategy}{seed_str}-{switch_time}"

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

    if strategy == "from_list":
        brs = bitrates.astype(np.int32)
    elif strategy == "exhaustive":
        # TODO: review, probably needs more work to be really exhaustive...
        n = len(bitrates) - 1
        permuts = permutations(bitrates, 2)
        split_permuts = [permuts[i * n: (i + 1) * n] for i in range(len(bitrates))]
        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)]).astype(np.int32)
    elif strategy == "random":
        brs = np.asarray(random.choices(bitrates, k=length_frames)).astype(np.int32)

    out_path = DUT_PATH.joinpath(fname)
    brs.astype.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: