xref: /aosp_15_r20/external/libwebm/common/vp9_header_parser.h (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1*103e46e4SHarish Mahendrakar // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2*103e46e4SHarish Mahendrakar //
3*103e46e4SHarish Mahendrakar // Use of this source code is governed by a BSD-style license
4*103e46e4SHarish Mahendrakar // that can be found in the LICENSE file in the root of the source
5*103e46e4SHarish Mahendrakar // tree. An additional intellectual property rights grant can be found
6*103e46e4SHarish Mahendrakar // in the file PATENTS.  All contributing project authors may
7*103e46e4SHarish Mahendrakar // be found in the AUTHORS file in the root of the source tree.
8*103e46e4SHarish Mahendrakar #ifndef LIBWEBM_COMMON_VP9_HEADER_PARSER_H_
9*103e46e4SHarish Mahendrakar #define LIBWEBM_COMMON_VP9_HEADER_PARSER_H_
10*103e46e4SHarish Mahendrakar 
11*103e46e4SHarish Mahendrakar #include <stddef.h>
12*103e46e4SHarish Mahendrakar #include <stdint.h>
13*103e46e4SHarish Mahendrakar 
14*103e46e4SHarish Mahendrakar namespace vp9_parser {
15*103e46e4SHarish Mahendrakar 
16*103e46e4SHarish Mahendrakar const int kVp9FrameMarker = 2;
17*103e46e4SHarish Mahendrakar const int kMinTileWidthB64 = 4;
18*103e46e4SHarish Mahendrakar const int kMaxTileWidthB64 = 64;
19*103e46e4SHarish Mahendrakar const int kRefFrames = 8;
20*103e46e4SHarish Mahendrakar const int kRefsPerFrame = 3;
21*103e46e4SHarish Mahendrakar const int kRefFrames_LOG2 = 3;
22*103e46e4SHarish Mahendrakar const int kVpxCsBt601 = 1;
23*103e46e4SHarish Mahendrakar const int kVpxCsSrgb = 7;
24*103e46e4SHarish Mahendrakar const int kVpxCrStudioRange = 0;
25*103e46e4SHarish Mahendrakar const int kVpxCrFullRange = 1;
26*103e46e4SHarish Mahendrakar const int kMiSizeLog2 = 3;
27*103e46e4SHarish Mahendrakar 
28*103e46e4SHarish Mahendrakar // Class to parse the header of a VP9 frame.
29*103e46e4SHarish Mahendrakar class Vp9HeaderParser {
30*103e46e4SHarish Mahendrakar  public:
Vp9HeaderParser()31*103e46e4SHarish Mahendrakar   Vp9HeaderParser()
32*103e46e4SHarish Mahendrakar       : frame_(NULL),
33*103e46e4SHarish Mahendrakar         frame_size_(0),
34*103e46e4SHarish Mahendrakar         bit_offset_(0),
35*103e46e4SHarish Mahendrakar         profile_(-1),
36*103e46e4SHarish Mahendrakar         show_existing_frame_(0),
37*103e46e4SHarish Mahendrakar         key_(0),
38*103e46e4SHarish Mahendrakar         altref_(0),
39*103e46e4SHarish Mahendrakar         error_resilient_mode_(0),
40*103e46e4SHarish Mahendrakar         intra_only_(0),
41*103e46e4SHarish Mahendrakar         reset_frame_context_(0),
42*103e46e4SHarish Mahendrakar         bit_depth_(0),
43*103e46e4SHarish Mahendrakar         color_space_(0),
44*103e46e4SHarish Mahendrakar         color_range_(0),
45*103e46e4SHarish Mahendrakar         subsampling_x_(0),
46*103e46e4SHarish Mahendrakar         subsampling_y_(0),
47*103e46e4SHarish Mahendrakar         refresh_frame_flags_(0),
48*103e46e4SHarish Mahendrakar         width_(0),
49*103e46e4SHarish Mahendrakar         height_(0),
50*103e46e4SHarish Mahendrakar         row_tiles_(0),
51*103e46e4SHarish Mahendrakar         column_tiles_(0),
52*103e46e4SHarish Mahendrakar         frame_parallel_mode_(0) {}
53*103e46e4SHarish Mahendrakar 
54*103e46e4SHarish Mahendrakar   // Parse the VP9 uncompressed header. The return values of the remaining
55*103e46e4SHarish Mahendrakar   // functions are only valid on success.
56*103e46e4SHarish Mahendrakar   bool ParseUncompressedHeader(const uint8_t* frame, size_t length);
57*103e46e4SHarish Mahendrakar 
frame_size()58*103e46e4SHarish Mahendrakar   size_t frame_size() const { return frame_size_; }
profile()59*103e46e4SHarish Mahendrakar   int profile() const { return profile_; }
key()60*103e46e4SHarish Mahendrakar   int key() const { return key_; }
altref()61*103e46e4SHarish Mahendrakar   int altref() const { return altref_; }
error_resilient_mode()62*103e46e4SHarish Mahendrakar   int error_resilient_mode() const { return error_resilient_mode_; }
bit_depth()63*103e46e4SHarish Mahendrakar   int bit_depth() const { return bit_depth_; }
color_space()64*103e46e4SHarish Mahendrakar   int color_space() const { return color_space_; }
width()65*103e46e4SHarish Mahendrakar   int width() const { return width_; }
height()66*103e46e4SHarish Mahendrakar   int height() const { return height_; }
display_width()67*103e46e4SHarish Mahendrakar   int display_width() const { return display_width_; }
display_height()68*103e46e4SHarish Mahendrakar   int display_height() const { return display_height_; }
refresh_frame_flags()69*103e46e4SHarish Mahendrakar   int refresh_frame_flags() const { return refresh_frame_flags_; }
row_tiles()70*103e46e4SHarish Mahendrakar   int row_tiles() const { return row_tiles_; }
column_tiles()71*103e46e4SHarish Mahendrakar   int column_tiles() const { return column_tiles_; }
frame_parallel_mode()72*103e46e4SHarish Mahendrakar   int frame_parallel_mode() const { return frame_parallel_mode_; }
73*103e46e4SHarish Mahendrakar 
74*103e46e4SHarish Mahendrakar  private:
75*103e46e4SHarish Mahendrakar   // Set the compressed VP9 frame.
76*103e46e4SHarish Mahendrakar   bool SetFrame(const uint8_t* frame, size_t length);
77*103e46e4SHarish Mahendrakar 
78*103e46e4SHarish Mahendrakar   // Returns the next bit of the frame.
79*103e46e4SHarish Mahendrakar   int ReadBit();
80*103e46e4SHarish Mahendrakar 
81*103e46e4SHarish Mahendrakar   // Returns the next |bits| of the frame.
82*103e46e4SHarish Mahendrakar   int VpxReadLiteral(int bits);
83*103e46e4SHarish Mahendrakar 
84*103e46e4SHarish Mahendrakar   // Returns true if the vp9 sync code is valid.
85*103e46e4SHarish Mahendrakar   bool ValidateVp9SyncCode();
86*103e46e4SHarish Mahendrakar 
87*103e46e4SHarish Mahendrakar   // Parses bit_depth_, color_space_, subsampling_x_, subsampling_y_, and
88*103e46e4SHarish Mahendrakar   // color_range_.
89*103e46e4SHarish Mahendrakar   void ParseColorSpace();
90*103e46e4SHarish Mahendrakar 
91*103e46e4SHarish Mahendrakar   // Parses width and height of the frame.
92*103e46e4SHarish Mahendrakar   void ParseFrameResolution();
93*103e46e4SHarish Mahendrakar 
94*103e46e4SHarish Mahendrakar   // Parses frame_parallel_mode_. This function skips over some features.
95*103e46e4SHarish Mahendrakar   void ParseFrameParallelMode();
96*103e46e4SHarish Mahendrakar 
97*103e46e4SHarish Mahendrakar   // Parses row and column tiles. This function skips over some features.
98*103e46e4SHarish Mahendrakar   void ParseTileInfo();
99*103e46e4SHarish Mahendrakar   void SkipDeltaQ();
100*103e46e4SHarish Mahendrakar   int AlignPowerOfTwo(int value, int n);
101*103e46e4SHarish Mahendrakar 
102*103e46e4SHarish Mahendrakar   const uint8_t* frame_;
103*103e46e4SHarish Mahendrakar   size_t frame_size_;
104*103e46e4SHarish Mahendrakar   size_t bit_offset_;
105*103e46e4SHarish Mahendrakar   int profile_;
106*103e46e4SHarish Mahendrakar   int show_existing_frame_;
107*103e46e4SHarish Mahendrakar   int key_;
108*103e46e4SHarish Mahendrakar   int altref_;
109*103e46e4SHarish Mahendrakar   int error_resilient_mode_;
110*103e46e4SHarish Mahendrakar   int intra_only_;
111*103e46e4SHarish Mahendrakar   int reset_frame_context_;
112*103e46e4SHarish Mahendrakar   int bit_depth_;
113*103e46e4SHarish Mahendrakar   int color_space_;
114*103e46e4SHarish Mahendrakar   int color_range_;
115*103e46e4SHarish Mahendrakar   int subsampling_x_;
116*103e46e4SHarish Mahendrakar   int subsampling_y_;
117*103e46e4SHarish Mahendrakar   int refresh_frame_flags_;
118*103e46e4SHarish Mahendrakar   int width_;
119*103e46e4SHarish Mahendrakar   int height_;
120*103e46e4SHarish Mahendrakar   int display_width_;
121*103e46e4SHarish Mahendrakar   int display_height_;
122*103e46e4SHarish Mahendrakar   int row_tiles_;
123*103e46e4SHarish Mahendrakar   int column_tiles_;
124*103e46e4SHarish Mahendrakar   int frame_parallel_mode_;
125*103e46e4SHarish Mahendrakar };
126*103e46e4SHarish Mahendrakar 
127*103e46e4SHarish Mahendrakar }  // namespace vp9_parser
128*103e46e4SHarish Mahendrakar 
129*103e46e4SHarish Mahendrakar #endif  // LIBWEBM_COMMON_VP9_HEADER_PARSER_H_
130