summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibVideo/VP9/TreeParser.h
blob: 02cfaf2c8677fa8f9380a911e2da298c7d3e8fc8 (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
/*
 * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "BitStream.h"
#include "Enums.h"
#include "ProbabilityTables.h"
#include "SyntaxElementCounter.h"

namespace Video::VP9 {

class Decoder;

class TreeParser {
public:
    explicit TreeParser(Decoder& decoder)
        : m_decoder(decoder)
    {
    }

    class TreeSelection {
    public:
        union TreeSelectionValue {
            int const* m_tree;
            int m_value;
        };

        TreeSelection(int const* values);
        TreeSelection(int value);

        bool is_single_value() const { return m_is_single_value; }
        int get_single_value() const { return m_value.m_value; }
        int const* get_tree_value() const { return m_value.m_tree; }

    private:
        bool m_is_single_value;
        TreeSelectionValue m_value;
    };

    /* (9.3.3) */
    template<typename T = int>
    T parse_tree(SyntaxElementType type);
    /* (9.3.1) */
    TreeSelection select_tree(SyntaxElementType type);
    /* (9.3.2) */
    u8 select_tree_probability(SyntaxElementType type, u8 node);
    /* (9.3.4) */
    void count_syntax_element(SyntaxElementType type, int value);

    void set_default_intra_mode_variables(u8 idx, u8 idy)
    {
        m_idx = idx;
        m_idy = idy;
    }

private:
    u8 calculate_partition_probability(u8 node);
    u8 calculate_default_intra_mode_probability(u8 node);
    u8 calculate_default_uv_mode_probability(u8 node);
    u8 calculate_intra_mode_probability(u8 node);
    u8 calculate_sub_intra_mode_probability(u8 node);
    u8 calculate_uv_mode_probability(u8 node);
    u8 calculate_segment_id_probability(u8 node);
    u8 calculate_skip_probability();
    u8 calculate_seg_id_predicted_probability();
    u8 calculate_is_inter_probability();
    u8 calculate_comp_mode_probability();
    u8 calculate_comp_ref_probability();
    u8 calculate_single_ref_p1_probability();
    u8 calculate_single_ref_p2_probability();
    u8 calculate_tx_size_probability(u8 node);
    u8 calculate_inter_mode_probability(u8 node);
    u8 calculate_interp_filter_probability(u8 node);

    Decoder& m_decoder;
    // m_ctx is a member variable because it is required for syntax element counting (section 9.3.4)
    u8 m_ctx { 0 };

    // These are variables necessary for parsing tree data, but aren't useful otherwise, so they're
    // not stored in the Decoder itself.
    u8 m_idx { 0 };
    u8 m_idy { 0 };
};

}