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