summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibVideo/VP9/TreeParser.cpp
blob: ac06e66b2f7256d46b99b125e9a025e9c7b9e7b5 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "TreeParser.h"
#include "LookupTables.h"

namespace Video::VP9 {

int TreeParser::parse_tree(SyntaxElementType type)
{
    auto tree_selection = select_tree(type);
    if (tree_selection.is_single_value())
        return tree_selection.get_single_value();
    auto tree = tree_selection.get_tree_value();
    int n = 0;
    do {
        n = tree[n + m_bit_stream->read_bool(select_tree_probability(type, n >> 1))];
    } while (n > 0);
    return -n;
}

/*
 * Select a tree value based on the type of syntax element being parsed, as well as some parser state, as specified in section 9.3.1
 */
TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type)
{
    switch (type) {
    case SyntaxElementType::Partition:
        if (m_has_rows && m_has_cols)
            return { partition_tree };
        if (m_has_cols)
            return { cols_partition_tree };
        if (m_has_rows)
            return { rows_partition_tree };
        return { PartitionSplit };
    case SyntaxElementType::DefaultIntraMode:
    case SyntaxElementType::DefaultUVMode:
    case SyntaxElementType::IntraMode:
    case SyntaxElementType::SubIntraMode:
    case SyntaxElementType::UVMode:
        return { intra_mode_tree };
    case SyntaxElementType::SegmentID:
        return { segment_tree };
    case SyntaxElementType::Skip:
    case SyntaxElementType::SegIDPredicted:
    case SyntaxElementType::IsInter:
    case SyntaxElementType::CompMode:
    case SyntaxElementType::CompRef:
    case SyntaxElementType::SingleRefP1:
    case SyntaxElementType::SingleRefP2:
    case SyntaxElementType::MVSign:
    case SyntaxElementType::MVClass0Bit:
    case SyntaxElementType::MVBit:
    case SyntaxElementType::MoreCoefs:
        return { binary_tree };
    case SyntaxElementType::TXSize:
        if (m_max_tx_size == TX_32x32)
            return { tx_size_32_tree };
        if (m_max_tx_size == TX_16x16)
            return { tx_size_16_tree };
        return { tx_size_8_tree };
    case SyntaxElementType::InterMode:
        return { inter_mode_tree };
    case SyntaxElementType::InterpFilter:
        return { interp_filter_tree };
    case SyntaxElementType::MVJoint:
        return { mv_joint_tree };
    case SyntaxElementType::MVClass:
        return { mv_class_tree };
    case SyntaxElementType::MVClass0FR:
    case SyntaxElementType::MVFR:
        return { mv_fr_tree };
    case SyntaxElementType::MVClass0HP:
    case SyntaxElementType::MVHP:
        if (m_use_hp)
            return { binary_tree };
        return { 1 };
    case SyntaxElementType::Token:
        return { token_tree };
    }
    VERIFY_NOT_REACHED();
}

/*
 * Select a probability with which to read a boolean when decoding a tree, as specified in section 9.3.2
 */
u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node)
{
    switch (type) {
    case SyntaxElementType::Partition:
        return calculate_partition_probability(node);
    case SyntaxElementType::DefaultIntraMode:
        break;
    case SyntaxElementType::DefaultUVMode:
        break;
    case SyntaxElementType::IntraMode:
        break;
    case SyntaxElementType::SubIntraMode:
        break;
    case SyntaxElementType::UVMode:
        break;
    case SyntaxElementType::SegmentID:
        break;
    case SyntaxElementType::Skip:
        break;
    case SyntaxElementType::SegIDPredicted:
        break;
    case SyntaxElementType::IsInter:
        break;
    case SyntaxElementType::CompMode:
        break;
    case SyntaxElementType::CompRef:
        break;
    case SyntaxElementType::SingleRefP1:
        break;
    case SyntaxElementType::SingleRefP2:
        break;
    case SyntaxElementType::MVSign:
        break;
    case SyntaxElementType::MVClass0Bit:
        break;
    case SyntaxElementType::MVBit:
        break;
    case SyntaxElementType::TXSize:
        break;
    case SyntaxElementType::InterMode:
        break;
    case SyntaxElementType::InterpFilter:
        break;
    case SyntaxElementType::MVJoint:
        break;
    case SyntaxElementType::MVClass:
        break;
    case SyntaxElementType::MVClass0FR:
        break;
    case SyntaxElementType::MVClass0HP:
        break;
    case SyntaxElementType::MVFR:
        break;
    case SyntaxElementType::MVHP:
        break;
    case SyntaxElementType::Token:
        break;
    case SyntaxElementType::MoreCoefs:
        break;
    }
    TODO();
}

u8 TreeParser::calculate_partition_probability(u8 node)
{
    int node2;
    if (m_has_rows && m_has_cols) {
        node2 = node;
    } else if (m_has_cols) {
        node2 = 1;
    } else {
        node2 = 2;
    }

    u32 above = 0;
    u32 left = 0;
    auto bsl = mi_width_log2_lookup[m_block_subsize];
    auto block_offset = mi_width_log2_lookup[Block_64x64] - bsl;
    for (auto i = 0; i < m_num_8x8; i++) {
        above |= m_above_partition_context[m_col + i];
        left |= m_left_partition_context[m_row + i];
    }
    above = (above & (1 << block_offset)) > 0;
    left = (left & (1 << block_offset)) > 0;
    auto ctx = bsl * 4 + left * 2 + above;
    if (m_frame_is_intra)
        return m_probability_tables.kf_partition_probs()[ctx][node2];
    return m_probability_tables.partition_probs()[ctx][node2];
}

TreeParser::TreeSelection::TreeSelection(const int* values)
    : m_is_single_value(false)
    , m_value { .m_tree = values }
{
}

TreeParser::TreeSelection::TreeSelection(int value)
    : m_is_single_value(true)
    , m_value { .m_value = value }
{
}

}