summaryrefslogtreecommitdiff
path: root/common/vp9_header_parser.h
blob: 06bd6569e6f808f6fa84fa7f6f1d4a0c07dfff1a (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
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS.  All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#ifndef LIBWEBM_COMMON_VP9_HEADER_PARSER_H_
#define LIBWEBM_COMMON_VP9_HEADER_PARSER_H_

#include <stddef.h>
#include <stdint.h>

namespace vp9_parser {

const int kVp9FrameMarker = 2;
const int kMinTileWidthB64 = 4;
const int kMaxTileWidthB64 = 64;
const int kRefFrames = 8;
const int kRefsPerFrame = 3;
const int kRefFrames_LOG2 = 3;
const int kVpxCsBt601 = 1;
const int kVpxCsSrgb = 7;
const int kVpxCrStudioRange = 0;
const int kVpxCrFullRange = 1;
const int kMiSizeLog2 = 3;

// Class to parse the header of a VP9 frame.
class Vp9HeaderParser {
 public:
  Vp9HeaderParser()
      : frame_(NULL),
        frame_size_(0),
        bit_offset_(0),
        profile_(-1),
        show_existing_frame_(0),
        key_(0),
        altref_(0),
        error_resilient_mode_(0),
        intra_only_(0),
        reset_frame_context_(0),
        bit_depth_(0),
        color_space_(0),
        color_range_(0),
        subsampling_x_(0),
        subsampling_y_(0),
        refresh_frame_flags_(0),
        width_(0),
        height_(0),
        row_tiles_(0),
        column_tiles_(0),
        frame_parallel_mode_(0) {}

  // Parse the VP9 uncompressed header. The return values of the remaining
  // functions are only valid on success.
  bool ParseUncompressedHeader(const uint8_t* frame, size_t length);

  size_t frame_size() const { return frame_size_; }
  int profile() const { return profile_; }
  int key() const { return key_; }
  int altref() const { return altref_; }
  int error_resilient_mode() const { return error_resilient_mode_; }
  int bit_depth() const { return bit_depth_; }
  int color_space() const { return color_space_; }
  int width() const { return width_; }
  int height() const { return height_; }
  int refresh_frame_flags() const { return refresh_frame_flags_; }
  int row_tiles() const { return row_tiles_; }
  int column_tiles() const { return column_tiles_; }
  int frame_parallel_mode() const { return frame_parallel_mode_; }

 private:
  // Set the compressed VP9 frame.
  bool SetFrame(const uint8_t* frame, size_t length);

  // Returns the next bit of the frame.
  int ReadBit();

  // Returns the next |bits| of the frame.
  int VpxReadLiteral(int bits);

  // Returns true if the vp9 sync code is valid.
  bool ValidateVp9SyncCode();

  // Parses bit_depth_, color_space_, subsampling_x_, subsampling_y_, and
  // color_range_.
  void ParseColorSpace();

  // Parses width and height of the frame.
  void ParseFrameResolution();

  // Parses frame_parallel_mode_. This function skips over some features.
  void ParseFrameParallelMode();

  // Parses row and column tiles. This function skips over some features.
  void ParseTileInfo();
  void SkipDeltaQ();
  int AlignPowerOfTwo(int value, int n);

  const uint8_t* frame_;
  size_t frame_size_;
  size_t bit_offset_;
  int profile_;
  int show_existing_frame_;
  int key_;
  int altref_;
  int error_resilient_mode_;
  int intra_only_;
  int reset_frame_context_;
  int bit_depth_;
  int color_space_;
  int color_range_;
  int subsampling_x_;
  int subsampling_y_;
  int refresh_frame_flags_;
  int width_;
  int height_;
  int row_tiles_;
  int column_tiles_;
  int frame_parallel_mode_;
};

}  // namespace vp9_parser

#endif  // LIBWEBM_COMMON_VP9_HEADER_PARSER_H_