summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibVideo/VP9/Utilities.h
blob: 516256285ba936e17d868341650e40d5117f91a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
 * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Types.h>
#include <LibGfx/Size.h>

#include "LookupTables.h"

namespace Video::VP9 {

// FIXME: Once everything is working, replace this with plain clamp
// since parameter order is different
template<typename T>
T clip_3(T x, T y, T z)
{
    return clamp(z, x, y);
}

template<typename T>
u16 clip_1(u8 bit_depth, T x)
{
    if (x < 0) {
        return 0u;
    }
    const T max = (1u << bit_depth) - 1u;
    if (x > max)
        return max;
    return x;
}

template<typename T, typename C>
inline T brev(C bit_count, T value)
{
    T result = 0;
    for (C i = 0; i < bit_count; i++) {
        auto bit = (value >> i) & 1;
        result |= bit << (bit_count - 1 - i);
    }
    return result;
}

inline BlockSubsize get_subsampled_block_size(BlockSubsize size, bool subsampling_x, bool subsampling_y)
{
    return ss_size_lookup[size < Block_8x8 ? Block_8x8 : size][subsampling_x][subsampling_y];
}

inline Gfx::Size<u8> block_size_to_blocks(BlockSubsize size)
{
    return Gfx::Size<u8>(num_8x8_blocks_wide_lookup[size], num_8x8_blocks_high_lookup[size]);
}

inline Gfx::Size<u8> block_size_to_sub_blocks(BlockSubsize size)
{
    return Gfx::Size<u8>(num_4x4_blocks_wide_lookup[size], num_4x4_blocks_high_lookup[size]);
}

template<Integral T>
inline T blocks_to_superblocks(T blocks)
{
    return blocks >> 3;
}

template<Integral T>
inline T superblocks_to_blocks(T superblocks)
{
    return superblocks << 3;
}

template<Integral T>
inline T blocks_to_sub_blocks(T blocks)
{
    return blocks << 1;
}

template<Integral T>
inline T sub_blocks_to_blocks(T sub_blocks)
{
    return sub_blocks >> 1;
}

template<Integral T>
inline T sub_blocks_to_pixels(T sub_blocks)
{
    return sub_blocks << 2;
}

template<Integral T>
inline T pixels_to_sub_blocks(T pixels)
{
    return pixels >> 2;
}

template<Integral T>
inline T blocks_to_pixels(T blocks)
{
    return sub_blocks_to_pixels(blocks_to_sub_blocks(blocks));
}

template<Integral T>
inline T pixels_to_blocks(T pixels)
{
    return sub_blocks_to_blocks(pixels_to_sub_blocks(pixels));
}

inline u8 transform_size_to_sub_blocks(TransformSize transform_size)
{
    return 1 << transform_size;
}

}