Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibMedia/Video/VP9/Utilities.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
3
 * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#pragma once
9
10
#include <AK/Types.h>
11
#include <LibGfx/Size.h>
12
13
#include "LookupTables.h"
14
15
namespace Media::Video::VP9 {
16
17
// FIXME: Once everything is working, replace this with plain clamp
18
// since parameter order is different
19
template<typename T>
20
T clip_3(T x, T y, T z)
21
1.04M
{
22
1.04M
    return clamp(z, x, y);
23
1.04M
}
unsigned int Media::Video::VP9::clip_3<unsigned int>(unsigned int, unsigned int, unsigned int)
Line
Count
Source
21
140k
{
22
140k
    return clamp(z, x, y);
23
140k
}
int Media::Video::VP9::clip_3<int>(int, int, int)
Line
Count
Source
21
834k
{
22
834k
    return clamp(z, x, y);
23
834k
}
unsigned char Media::Video::VP9::clip_3<unsigned char>(unsigned char, unsigned char, unsigned char)
Line
Count
Source
21
72.0k
{
22
72.0k
    return clamp(z, x, y);
23
72.0k
}
24
25
template<typename T>
26
u16 clip_1(u8 bit_depth, T x)
27
915M
{
28
915M
    if (x < 0) {
29
5.16M
        return 0u;
30
5.16M
    }
31
910M
    T const max = (1u << bit_depth) - 1u;
32
910M
    if (x > max)
33
5.21M
        return max;
34
905M
    return x;
35
910M
}
36
37
template<u8 bits>
38
inline u8 brev(u8 value)
39
2.54G
{
40
2.54G
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
2.54G
    static constexpr auto lookup_table = [] {
43
2.54G
        constexpr size_t value_count = 1 << bits;
44
2.54G
        Array<u8, value_count> the_table;
45
2.54G
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
2.54G
            u8 reversed = 0;
47
2.54G
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
2.54G
                auto bit = (lookup_value >> bit_index) & 1;
49
2.54G
                reversed |= bit << (bits - 1 - bit_index);
50
2.54G
            }
51
2.54G
            the_table[lookup_value] = reversed;
52
2.54G
        }
53
2.54G
        return the_table;
54
2.54G
    }();
55
56
2.54G
    return lookup_table[value];
57
2.54G
}
unsigned char Media::Video::VP9::brev<(unsigned char)2>(unsigned char)
Line
Count
Source
39
300M
{
40
300M
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
300M
    static constexpr auto lookup_table = [] {
43
300M
        constexpr size_t value_count = 1 << bits;
44
300M
        Array<u8, value_count> the_table;
45
300M
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
300M
            u8 reversed = 0;
47
300M
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
300M
                auto bit = (lookup_value >> bit_index) & 1;
49
300M
                reversed |= bit << (bits - 1 - bit_index);
50
300M
            }
51
300M
            the_table[lookup_value] = reversed;
52
300M
        }
53
300M
        return the_table;
54
300M
    }();
55
56
300M
    return lookup_table[value];
57
300M
}
unsigned char Media::Video::VP9::brev<(unsigned char)5>(unsigned char)
Line
Count
Source
39
1.98G
{
40
1.98G
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
1.98G
    static constexpr auto lookup_table = [] {
43
1.98G
        constexpr size_t value_count = 1 << bits;
44
1.98G
        Array<u8, value_count> the_table;
45
1.98G
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
1.98G
            u8 reversed = 0;
47
1.98G
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
1.98G
                auto bit = (lookup_value >> bit_index) & 1;
49
1.98G
                reversed |= bit << (bits - 1 - bit_index);
50
1.98G
            }
51
1.98G
            the_table[lookup_value] = reversed;
52
1.98G
        }
53
1.98G
        return the_table;
54
1.98G
    }();
55
56
1.98G
    return lookup_table[value];
57
1.98G
}
unsigned char Media::Video::VP9::brev<(unsigned char)3>(unsigned char)
Line
Count
Source
39
115M
{
40
115M
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
115M
    static constexpr auto lookup_table = [] {
43
115M
        constexpr size_t value_count = 1 << bits;
44
115M
        Array<u8, value_count> the_table;
45
115M
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
115M
            u8 reversed = 0;
47
115M
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
115M
                auto bit = (lookup_value >> bit_index) & 1;
49
115M
                reversed |= bit << (bits - 1 - bit_index);
50
115M
            }
51
115M
            the_table[lookup_value] = reversed;
52
115M
        }
53
115M
        return the_table;
54
115M
    }();
55
56
115M
    return lookup_table[value];
57
115M
}
unsigned char Media::Video::VP9::brev<(unsigned char)4>(unsigned char)
Line
Count
Source
39
138M
{
40
138M
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
138M
    static constexpr auto lookup_table = [] {
43
138M
        constexpr size_t value_count = 1 << bits;
44
138M
        Array<u8, value_count> the_table;
45
138M
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
138M
            u8 reversed = 0;
47
138M
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
138M
                auto bit = (lookup_value >> bit_index) & 1;
49
138M
                reversed |= bit << (bits - 1 - bit_index);
50
138M
            }
51
138M
            the_table[lookup_value] = reversed;
52
138M
        }
53
138M
        return the_table;
54
138M
    }();
55
56
138M
    return lookup_table[value];
57
138M
}
58
59
inline BlockSubsize get_subsampled_block_size(BlockSubsize size, bool subsampling_x, bool subsampling_y)
60
2.51M
{
61
2.51M
    return ss_size_lookup[size < Block_8x8 ? Block_8x8 : size][subsampling_x][subsampling_y];
62
2.51M
}
63
64
inline Gfx::Size<u8> block_size_to_blocks(BlockSubsize size)
65
630k
{
66
630k
    return Gfx::Size<u8>(num_8x8_blocks_wide_lookup[size], num_8x8_blocks_high_lookup[size]);
67
630k
}
68
69
inline Gfx::Size<u8> block_size_to_sub_blocks(BlockSubsize size)
70
2.69M
{
71
2.69M
    return Gfx::Size<u8>(num_4x4_blocks_wide_lookup[size], num_4x4_blocks_high_lookup[size]);
72
2.69M
}
73
74
template<Integral T>
75
inline T blocks_to_superblocks(T blocks)
76
17.4k
{
77
17.4k
    return blocks >> 3;
78
17.4k
}
79
80
template<Integral T>
81
inline T superblocks_to_blocks(T superblocks)
82
15.2k
{
83
15.2k
    return superblocks << 3;
84
15.2k
}
85
86
template<Integral T>
87
inline T blocks_ceiled_to_superblocks(T blocks)
88
17.4k
{
89
17.4k
    return blocks_to_superblocks(blocks + 7);
90
17.4k
}
91
92
template<Integral T>
93
inline T blocks_to_sub_blocks(T blocks)
94
75.3M
{
95
75.3M
    return blocks << 1;
96
75.3M
}
97
98
template<Integral T>
99
inline T sub_blocks_to_blocks(T sub_blocks)
100
4.38k
{
101
4.38k
    return sub_blocks >> 1;
102
4.38k
}
103
104
template<Integral T>
105
inline T sub_blocks_to_pixels(T sub_blocks)
106
133M
{
107
133M
    return sub_blocks << 2;
108
133M
}
_ZN5Media5Video3VP920sub_blocks_to_pixelsITkN2AK8Concepts8IntegralEjEET_S5_
Line
Count
Source
106
120M
{
107
120M
    return sub_blocks << 2;
108
120M
}
_ZN5Media5Video3VP920sub_blocks_to_pixelsITkN2AK8Concepts8IntegralEiEET_S5_
Line
Count
Source
106
148k
{
107
148k
    return sub_blocks << 2;
108
148k
}
_ZN5Media5Video3VP920sub_blocks_to_pixelsITkN2AK8Concepts8IntegralEhEET_S5_
Line
Count
Source
106
12.9M
{
107
12.9M
    return sub_blocks << 2;
108
12.9M
}
109
110
template<Integral T>
111
inline T pixels_to_sub_blocks(T pixels)
112
4.38k
{
113
4.38k
    return pixels >> 2;
114
4.38k
}
115
116
template<Integral T>
117
inline T blocks_to_pixels(T blocks)
118
74.0M
{
119
74.0M
    return sub_blocks_to_pixels(blocks_to_sub_blocks(blocks));
120
74.0M
}
121
122
template<Integral T>
123
inline T pixels_to_blocks(T pixels)
124
4.38k
{
125
4.38k
    return sub_blocks_to_blocks(pixels_to_sub_blocks(pixels));
126
4.38k
}
127
128
inline u8 transform_size_to_sub_blocks(TransformSize transform_size)
129
27.4M
{
130
27.4M
    return 1 << transform_size;
131
27.4M
}
132
133
}