Coverage Report

Created: 2025-11-02 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.13M
{
22
1.13M
    return clamp(z, x, y);
23
1.13M
}
unsigned int Media::Video::VP9::clip_3<unsigned int>(unsigned int, unsigned int, unsigned int)
Line
Count
Source
21
145k
{
22
145k
    return clamp(z, x, y);
23
145k
}
int Media::Video::VP9::clip_3<int>(int, int, int)
Line
Count
Source
21
920k
{
22
920k
    return clamp(z, x, y);
23
920k
}
unsigned char Media::Video::VP9::clip_3<unsigned char>(unsigned char, unsigned char, unsigned char)
Line
Count
Source
21
68.6k
{
22
68.6k
    return clamp(z, x, y);
23
68.6k
}
24
25
template<typename T>
26
u16 clip_1(u8 bit_depth, T x)
27
659M
{
28
659M
    if (x < 0) {
29
4.36M
        return 0u;
30
4.36M
    }
31
655M
    T const max = (1u << bit_depth) - 1u;
32
655M
    if (x > max)
33
4.43M
        return max;
34
651M
    return x;
35
655M
}
36
37
template<u8 bits>
38
inline u8 brev(u8 value)
39
1.79G
{
40
1.79G
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
1.79G
    static constexpr auto lookup_table = [] {
43
1.79G
        constexpr size_t value_count = 1 << bits;
44
1.79G
        Array<u8, value_count> the_table;
45
1.79G
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
1.79G
            u8 reversed = 0;
47
1.79G
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
1.79G
                auto bit = (lookup_value >> bit_index) & 1;
49
1.79G
                reversed |= bit << (bits - 1 - bit_index);
50
1.79G
            }
51
1.79G
            the_table[lookup_value] = reversed;
52
1.79G
        }
53
1.79G
        return the_table;
54
1.79G
    }();
55
56
1.79G
    return lookup_table[value];
57
1.79G
}
unsigned char Media::Video::VP9::brev<(unsigned char)2>(unsigned char)
Line
Count
Source
39
249M
{
40
249M
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
249M
    static constexpr auto lookup_table = [] {
43
249M
        constexpr size_t value_count = 1 << bits;
44
249M
        Array<u8, value_count> the_table;
45
249M
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
249M
            u8 reversed = 0;
47
249M
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
249M
                auto bit = (lookup_value >> bit_index) & 1;
49
249M
                reversed |= bit << (bits - 1 - bit_index);
50
249M
            }
51
249M
            the_table[lookup_value] = reversed;
52
249M
        }
53
249M
        return the_table;
54
249M
    }();
55
56
249M
    return lookup_table[value];
57
249M
}
unsigned char Media::Video::VP9::brev<(unsigned char)5>(unsigned char)
Line
Count
Source
39
1.35G
{
40
1.35G
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
1.35G
    static constexpr auto lookup_table = [] {
43
1.35G
        constexpr size_t value_count = 1 << bits;
44
1.35G
        Array<u8, value_count> the_table;
45
1.35G
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
1.35G
            u8 reversed = 0;
47
1.35G
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
1.35G
                auto bit = (lookup_value >> bit_index) & 1;
49
1.35G
                reversed |= bit << (bits - 1 - bit_index);
50
1.35G
            }
51
1.35G
            the_table[lookup_value] = reversed;
52
1.35G
        }
53
1.35G
        return the_table;
54
1.35G
    }();
55
56
1.35G
    return lookup_table[value];
57
1.35G
}
unsigned char Media::Video::VP9::brev<(unsigned char)3>(unsigned char)
Line
Count
Source
39
104M
{
40
104M
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
104M
    static constexpr auto lookup_table = [] {
43
104M
        constexpr size_t value_count = 1 << bits;
44
104M
        Array<u8, value_count> the_table;
45
104M
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
104M
            u8 reversed = 0;
47
104M
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
104M
                auto bit = (lookup_value >> bit_index) & 1;
49
104M
                reversed |= bit << (bits - 1 - bit_index);
50
104M
            }
51
104M
            the_table[lookup_value] = reversed;
52
104M
        }
53
104M
        return the_table;
54
104M
    }();
55
56
104M
    return lookup_table[value];
57
104M
}
unsigned char Media::Video::VP9::brev<(unsigned char)4>(unsigned char)
Line
Count
Source
39
83.1M
{
40
83.1M
    static_assert(bits <= 8, "brev() expects an 8-bit value.");
41
42
83.1M
    static constexpr auto lookup_table = [] {
43
83.1M
        constexpr size_t value_count = 1 << bits;
44
83.1M
        Array<u8, value_count> the_table;
45
83.1M
        for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
46
83.1M
            u8 reversed = 0;
47
83.1M
            for (u8 bit_index = 0; bit_index < bits; bit_index++) {
48
83.1M
                auto bit = (lookup_value >> bit_index) & 1;
49
83.1M
                reversed |= bit << (bits - 1 - bit_index);
50
83.1M
            }
51
83.1M
            the_table[lookup_value] = reversed;
52
83.1M
        }
53
83.1M
        return the_table;
54
83.1M
    }();
55
56
83.1M
    return lookup_table[value];
57
83.1M
}
58
59
inline BlockSubsize get_subsampled_block_size(BlockSubsize size, bool subsampling_x, bool subsampling_y)
60
1.80M
{
61
1.80M
    return ss_size_lookup[size < Block_8x8 ? Block_8x8 : size][subsampling_x][subsampling_y];
62
1.80M
}
63
64
inline Gfx::Size<u8> block_size_to_blocks(BlockSubsize size)
65
451k
{
66
451k
    return Gfx::Size<u8>(num_8x8_blocks_wide_lookup[size], num_8x8_blocks_high_lookup[size]);
67
451k
}
68
69
inline Gfx::Size<u8> block_size_to_sub_blocks(BlockSubsize size)
70
1.90M
{
71
1.90M
    return Gfx::Size<u8>(num_4x4_blocks_wide_lookup[size], num_4x4_blocks_high_lookup[size]);
72
1.90M
}
73
74
template<Integral T>
75
inline T blocks_to_superblocks(T blocks)
76
16.7k
{
77
16.7k
    return blocks >> 3;
78
16.7k
}
79
80
template<Integral T>
81
inline T superblocks_to_blocks(T superblocks)
82
14.7k
{
83
14.7k
    return superblocks << 3;
84
14.7k
}
85
86
template<Integral T>
87
inline T blocks_ceiled_to_superblocks(T blocks)
88
16.7k
{
89
16.7k
    return blocks_to_superblocks(blocks + 7);
90
16.7k
}
91
92
template<Integral T>
93
inline T blocks_to_sub_blocks(T blocks)
94
64.7M
{
95
64.7M
    return blocks << 1;
96
64.7M
}
97
98
template<Integral T>
99
inline T sub_blocks_to_blocks(T sub_blocks)
100
4.18k
{
101
4.18k
    return sub_blocks >> 1;
102
4.18k
}
103
104
template<Integral T>
105
inline T sub_blocks_to_pixels(T sub_blocks)
106
117M
{
107
117M
    return sub_blocks << 2;
108
117M
}
_ZN5Media5Video3VP920sub_blocks_to_pixelsITkN2AK8Concepts8IntegralEjEET_S5_
Line
Count
Source
106
104M
{
107
104M
    return sub_blocks << 2;
108
104M
}
_ZN5Media5Video3VP920sub_blocks_to_pixelsITkN2AK8Concepts8IntegralEiEET_S5_
Line
Count
Source
106
170k
{
107
170k
    return sub_blocks << 2;
108
170k
}
_ZN5Media5Video3VP920sub_blocks_to_pixelsITkN2AK8Concepts8IntegralEhEET_S5_
Line
Count
Source
106
11.9M
{
107
11.9M
    return sub_blocks << 2;
108
11.9M
}
109
110
template<Integral T>
111
inline T pixels_to_sub_blocks(T pixels)
112
4.18k
{
113
4.18k
    return pixels >> 2;
114
4.18k
}
115
116
template<Integral T>
117
inline T blocks_to_pixels(T blocks)
118
63.7M
{
119
63.7M
    return sub_blocks_to_pixels(blocks_to_sub_blocks(blocks));
120
63.7M
}
121
122
template<Integral T>
123
inline T pixels_to_blocks(T pixels)
124
4.18k
{
125
4.18k
    return sub_blocks_to_blocks(pixels_to_sub_blocks(pixels));
126
4.18k
}
127
128
inline u8 transform_size_to_sub_blocks(TransformSize transform_size)
129
23.9M
{
130
23.9M
    return 1 << transform_size;
131
23.9M
}
132
133
}