Commit 7ac849f1 authored by Jan Kiene's avatar Jan Kiene
Browse files

add starting br and no repetitions for random br pattern

parent 41948d5e
Loading
Loading
Loading
Loading
+47 −6
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import numpy as np
import random
import pytest
from itertools import permutations
from typing import Optional
from .. import is_be_to_ref, get_bitstream_path
from ..constants import (
    METADATA_FOR_INPUT_FORMAT,
@@ -48,12 +49,34 @@ from ..constants import (
)


def create_br_switching_file(
def random_choice(
    values: list, n, allow_repetitions: bool = False, last_init=None, seed=None
):
    """
    Generator for randomly picking from a list of values.
    """
    # create copy to be safe against external modifications
    assert len(values) > 1

    values = list(values)
    last = last_init
    if seed is not None:
        random.seed(seed)

    for i in range(n):
        while ((curr := random.choice(values)) == last) and not allow_repetitions:
            pass
        yield curr
        last = curr


def create_br_switching_pattern(
    bitrates: np.ndarray,
    strategy: str,
    switch_time: int = 1,
    length_frames: int = 0,
    seed: int = None,
    seed: Optional[int] = None,
    starting_br: Optional[int] = None,
):
    """
    Create bitrate switching pattern files on the fly and return path to it
@@ -66,8 +89,10 @@ def create_br_switching_file(
    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
    starting_br - for "exhaustive" and "random" strategies: start pattern with given bitrate, has to be contained in bitrates
    """
    assert strategy in ["from_array", "exhaustive", "random"]
    assert starting_br is None or starting_br in bitrates

    seed_str = f"_{seed}" if strategy == "random" or strategy == "exhaustive" else ""
    length_str = f"-l{length_frames}" if length_frames > 0 else ""
@@ -89,14 +114,30 @@ def create_br_switching_file(
        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(bitrates, k=length_frames))
        rand_brs = list()
        if starting_br is not None:
            rand_brs = [starting_br]
            length_frames -= 1

        rand_brs += [
            x
            for x in random_choice(
                bitrates,
                length_frames,
                allow_repetitions=False,
                last_init=starting_br,
                seed=seed,
            )
        ]
        brs = np.asarray(rand_brs)

    brs = np.repeat(brs, switch_time).astype(np.int32)
    return brs

    out_path = DUT_PATH.joinpath(fname)
    brs.tofile(out_path)
    # out_path = DUT_PATH.joinpath(fname)
    # brs.tofile(out_path)

    return out_path
    # return out_path


def get_md(input_format, md_type=None):
+1 −1
Original line number Diff line number Diff line
@@ -56,5 +56,5 @@ def test_encoder_br_switching_stereo(
        options,
        dut_encoder_frontend,
        update_ref == 1,
        dtx
        dtx,
    )