[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / vp8_parser.h
blobdc7bc241173d845fa9d2c5ce960e4d35ac0f51c4
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains an implementation of a VP8 raw stream parser,
6 // as defined in RFC 6386.
8 #ifndef MEDIA_FILTERS_VP8_PARSER_H_
9 #define MEDIA_FILTERS_VP8_PARSER_H_
11 #include "media/base/media_export.h"
12 #include "media/filters/vp8_bool_decoder.h"
14 namespace media {
16 // See spec for definitions of values/fields.
17 const size_t kMaxMBSegments = 4;
18 const size_t kNumMBFeatureTreeProbs = 3;
20 // Member of Vp8FrameHeader and will be 0-initialized
21 // in Vp8FrameHeader's constructor.
22 struct MEDIA_EXPORT Vp8SegmentationHeader {
23 enum SegmentFeatureMode { FEATURE_MODE_DELTA = 0, FEATURE_MODE_ABSOLUTE = 1 };
25 bool segmentation_enabled;
26 bool update_mb_segmentation_map;
27 bool update_segment_feature_data;
28 SegmentFeatureMode segment_feature_mode;
30 int8_t quantizer_update_value[kMaxMBSegments];
31 int8_t lf_update_value[kMaxMBSegments];
32 static const int kDefaultSegmentProb = 255;
33 uint8_t segment_prob[kNumMBFeatureTreeProbs];
36 const size_t kNumBlockContexts = 4;
38 // Member of Vp8FrameHeader and will be 0-initialized
39 // in Vp8FrameHeader's constructor.
40 struct MEDIA_EXPORT Vp8LoopFilterHeader {
41 enum Type { LOOP_FILTER_TYPE_NORMAL = 0, LOOP_FILTER_TYPE_SIMPLE = 1 };
42 Type type;
43 uint8_t level;
44 uint8_t sharpness_level;
45 bool loop_filter_adj_enable;
46 bool mode_ref_lf_delta_update;
48 int8_t ref_frame_delta[kNumBlockContexts];
49 int8_t mb_mode_delta[kNumBlockContexts];
52 // Member of Vp8FrameHeader and will be 0-initialized
53 // in Vp8FrameHeader's constructor.
54 struct MEDIA_EXPORT Vp8QuantizationHeader {
55 uint8_t y_ac_qi;
56 int8_t y_dc_delta;
57 int8_t y2_dc_delta;
58 int8_t y2_ac_delta;
59 int8_t uv_dc_delta;
60 int8_t uv_ac_delta;
63 const size_t kNumBlockTypes = 4;
64 const size_t kNumCoeffBands = 8;
65 const size_t kNumPrevCoeffContexts = 3;
66 const size_t kNumEntropyNodes = 11;
68 const size_t kNumMVContexts = 2;
69 const size_t kNumMVProbs = 19;
71 const size_t kNumYModeProbs = 4;
72 const size_t kNumUVModeProbs = 3;
74 // Member of Vp8FrameHeader and will be 0-initialized
75 // in Vp8FrameHeader's constructor.
76 struct Vp8EntropyHeader {
77 uint8_t coeff_probs[kNumBlockTypes][kNumCoeffBands][kNumPrevCoeffContexts]
78 [kNumEntropyNodes];
80 uint8_t y_mode_probs[kNumYModeProbs];
81 uint8_t uv_mode_probs[kNumUVModeProbs];
83 uint8_t mv_probs[kNumMVContexts][kNumMVProbs];
86 const size_t kMaxDCTPartitions = 8;
88 struct MEDIA_EXPORT Vp8FrameHeader {
89 Vp8FrameHeader();
91 enum FrameType { KEYFRAME = 0, INTERFRAME = 1 };
92 bool IsKeyframe() const { return key_frame == KEYFRAME; }
94 enum GoldenRefreshMode {
95 COPY_LAST_TO_GOLDEN = 1,
96 COPY_ALT_TO_GOLDEN = 2,
99 enum AltRefreshMode {
100 COPY_LAST_TO_ALT = 1,
101 COPY_GOLDEN_TO_ALT = 2,
104 FrameType key_frame;
105 uint8_t version;
106 bool is_experimental;
107 bool show_frame;
108 size_t first_part_size;
110 uint16_t width;
111 uint8_t horizontal_scale;
112 uint16_t height;
113 uint8_t vertical_scale;
115 Vp8SegmentationHeader segmentation_hdr;
116 Vp8LoopFilterHeader loopfilter_hdr;
117 Vp8QuantizationHeader quantization_hdr;
119 size_t num_of_dct_partitions;
121 Vp8EntropyHeader entropy_hdr;
123 bool refresh_entropy_probs;
124 bool refresh_golden_frame;
125 bool refresh_alternate_frame;
126 GoldenRefreshMode copy_buffer_to_golden;
127 AltRefreshMode copy_buffer_to_alternate;
128 uint8_t sign_bias_golden;
129 uint8_t sign_bias_alternate;
130 bool refresh_last;
132 bool mb_no_skip_coeff;
133 uint8_t prob_skip_false;
134 uint8_t prob_intra;
135 uint8_t prob_last;
136 uint8_t prob_gf;
138 const uint8_t* data;
139 size_t frame_size;
141 size_t dct_partition_sizes[kMaxDCTPartitions];
142 // Offset in bytes from data.
143 off_t first_part_offset;
144 // Offset in bits from first_part_offset.
145 off_t macroblock_bit_offset;
147 // Bool decoder state
148 uint8_t bool_dec_range;
149 uint8_t bool_dec_value;
150 uint8_t bool_dec_count;
153 // A parser for raw VP8 streams as specified in RFC 6386.
154 class MEDIA_EXPORT Vp8Parser {
155 public:
156 Vp8Parser();
157 ~Vp8Parser();
159 // Try to parse exactly one VP8 frame starting at |ptr| and of size |size|,
160 // filling the parsed data in |fhdr|. Return true on success.
161 // Size has to be exactly the size of the frame and coming from the caller,
162 // who needs to acquire it from elsewhere (normally from a container).
163 bool ParseFrame(const uint8_t* ptr, size_t size, Vp8FrameHeader* fhdr);
165 private:
166 bool ParseFrameTag(Vp8FrameHeader* fhdr);
167 bool ParseFrameHeader(Vp8FrameHeader* fhdr);
169 bool ParseSegmentationHeader(bool keyframe);
170 bool ParseLoopFilterHeader(bool keyframe);
171 bool ParseQuantizationHeader(Vp8QuantizationHeader* qhdr);
172 bool ParseTokenProbs(Vp8EntropyHeader* ehdr, bool update_curr_probs);
173 bool ParseIntraProbs(Vp8EntropyHeader* ehdr,
174 bool update_curr_probs,
175 bool keyframe);
176 bool ParseMVProbs(Vp8EntropyHeader* ehdr, bool update_curr_probs);
177 bool ParsePartitions(Vp8FrameHeader* fhdr);
178 void ResetProbs();
180 // These persist across calls to ParseFrame() and may be used and/or updated
181 // for subsequent frames if the stream instructs us to do so.
182 Vp8SegmentationHeader curr_segmentation_hdr_;
183 Vp8LoopFilterHeader curr_loopfilter_hdr_;
184 Vp8EntropyHeader curr_entropy_hdr_;
186 const uint8_t* stream_;
187 size_t bytes_left_;
188 Vp8BoolDecoder bd_;
190 DISALLOW_COPY_AND_ASSIGN(Vp8Parser);
193 } // namespace media
195 #endif // MEDIA_FILTERS_VP8_PARSER_H_