xref: /aosp_15_r20/external/webrtc/modules/video_coding/utility/vp9_uncompressed_header_parser.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_VIDEO_CODING_UTILITY_VP9_UNCOMPRESSED_HEADER_PARSER_H_
12 #define MODULES_VIDEO_CODING_UTILITY_VP9_UNCOMPRESSED_HEADER_PARSER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <array>
18 #include <bitset>
19 #include <string>
20 
21 #include "absl/types/optional.h"
22 #include "api/array_view.h"
23 #include "modules/video_coding/utility/vp9_constants.h"
24 
25 namespace webrtc {
26 
27 namespace vp9 {
28 
29 // Gets the QP, QP range: [0, 255].
30 // Returns true on success, false otherwise.
31 bool GetQp(const uint8_t* buf, size_t length, int* qp);
32 
33 }  // namespace vp9
34 
35 // Bit depth per channel. Support varies by profile.
36 enum class Vp9BitDept : uint8_t {
37   k8Bit = 8,
38   k10Bit = 10,
39   k12Bit = 12,
40 };
41 
42 enum class Vp9ColorSpace : uint8_t {
43   CS_UNKNOWN = 0,    // Unknown (in this case the color space must be signaled
44                      // outside the VP9 bitstream).
45   CS_BT_601 = 1,     // CS_BT_601 Rec. ITU-R BT.601-7
46   CS_BT_709 = 2,     // Rec. ITU-R BT.709-6
47   CS_SMPTE_170 = 3,  // SMPTE-170
48   CS_SMPTE_240 = 4,  // SMPTE-240
49   CS_BT_2020 = 5,    // Rec. ITU-R BT.2020-2
50   CS_RESERVED = 6,   // Reserved
51   CS_RGB = 7,        // sRGB (IEC 61966-2-1)
52 };
53 
54 enum class Vp9ColorRange {
55   kStudio,  // Studio swing:
56             // For BitDepth equals 8:
57             //     Y is between 16 and 235 inclusive.
58             //     U and V are between 16 and 240 inclusive.
59             // For BitDepth equals 10:
60             //     Y is between 64 and 940 inclusive.
61             //     U and V are between 64 and 960 inclusive.
62             // For BitDepth equals 12:
63             //     Y is between 256 and 3760.
64             //     U and V are between 256 and 3840 inclusive.
65   kFull     // Full swing; no restriction on Y, U, V values.
66 };
67 
68 enum class Vp9YuvSubsampling {
69   k444,
70   k440,
71   k422,
72   k420,
73 };
74 
75 enum Vp9ReferenceFrame : int {
76   kNone = -1,
77   kIntra = 0,
78   kLast = 1,
79   kGolden = 2,
80   kAltref = 3,
81 };
82 
83 enum class Vp9InterpolationFilter : uint8_t {
84   kEightTap = 0,
85   kEightTapSmooth = 1,
86   kEightTapSharp = 2,
87   kBilinear = 3,
88   kSwitchable = 4
89 };
90 
91 struct Vp9UncompressedHeader {
92   int profile = 0;  // Profiles 0-3 are valid.
93   absl::optional<uint8_t> show_existing_frame;
94   bool is_keyframe = false;
95   bool show_frame = false;
96   bool error_resilient = false;
97   Vp9BitDept bit_detph = Vp9BitDept::k8Bit;
98   absl::optional<Vp9ColorSpace> color_space;
99   absl::optional<Vp9ColorRange> color_range;
100   absl::optional<Vp9YuvSubsampling> sub_sampling;
101   int frame_width = 0;
102   int frame_height = 0;
103   int render_width = 0;
104   int render_height = 0;
105   // Width/height of the tiles used (in units of 8x8 blocks).
106   size_t tile_cols_log2 = 0;  // tile_cols = 1 << tile_cols_log2
107   size_t tile_rows_log2 = 0;  // tile_rows = 1 << tile_rows_log2
108   absl::optional<size_t> render_size_offset_bits;
109   Vp9InterpolationFilter interpolation_filter =
110       Vp9InterpolationFilter::kEightTap;
111   bool allow_high_precision_mv = false;
112   int base_qp = 0;
113   bool is_lossless = false;
114   uint8_t frame_context_idx = 0;
115 
116   bool segmentation_enabled = false;
117   absl::optional<std::array<uint8_t, 7>> segmentation_tree_probs;
118   absl::optional<std::array<uint8_t, 3>> segmentation_pred_prob;
119   bool segmentation_is_delta = false;
120   std::array<std::array<absl::optional<int>, kVp9SegLvlMax>, kVp9MaxSegments>
121       segmentation_features;
122 
123   // Which of the 8 reference buffers may be used as references for this frame.
124   // -1 indicates not used (e.g. {-1, -1, -1} for intra-only frames).
125   std::array<int, kVp9RefsPerFrame> reference_buffers = {-1, -1, -1};
126   // Sign bias corresponding to reference buffers, where the index is a
127   // ReferenceFrame.
128   // false/0 indidate backwards reference, true/1 indicate forwards reference).
129   std::bitset<kVp9MaxRefFrames> reference_buffers_sign_bias = 0;
130 
131   // Indicates which reference buffer [0,7] to infer the frame size from.
132   absl::optional<int> infer_size_from_reference;
133   // Which of the 8 reference buffers are updated by this frame.
134   std::bitset<kVp9NumRefFrames> updated_buffers = 0;
135 
136   // Header sizes, in bytes.
137   uint32_t uncompressed_header_size = 0;
138   uint32_t compressed_header_size = 0;
139 
is_intra_onlyVp9UncompressedHeader140   bool is_intra_only() const {
141     return reference_buffers[0] == -1 && reference_buffers[1] == -1 &&
142            reference_buffers[2] == -1;
143   }
144 
145   std::string ToString() const;
146 };
147 
148 // Parses the uncompressed header and populates (most) values in a
149 // UncompressedHeader struct. Returns nullopt on failure.
150 absl::optional<Vp9UncompressedHeader> ParseUncompressedVp9Header(
151     rtc::ArrayView<const uint8_t> buf);
152 
153 }  // namespace webrtc
154 
155 #endif  // MODULES_VIDEO_CODING_UTILITY_VP9_UNCOMPRESSED_HEADER_PARSER_H_
156