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.
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"
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 };
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
{
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
]
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
{
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,
100 COPY_LAST_TO_ALT
= 1,
101 COPY_GOLDEN_TO_ALT
= 2,
106 bool is_experimental
;
108 size_t first_part_size
;
111 uint8_t horizontal_scale
;
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
;
132 bool mb_no_skip_coeff
;
133 uint8_t prob_skip_false
;
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
{
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
);
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
, bool update_curr_probs
);
174 bool ParseMVProbs(Vp8EntropyHeader
* ehdr
, bool update_curr_probs
);
175 bool ParsePartitions(Vp8FrameHeader
* fhdr
);
176 void ResetProbs(Vp8EntropyHeader
* ehdr
);
178 // These persist across calls to ParseFrame() and may be used and/or updated
179 // for subsequent frames if the stream instructs us to do so.
180 Vp8SegmentationHeader curr_segmentation_hdr_
;
181 Vp8LoopFilterHeader curr_loopfilter_hdr_
;
182 Vp8EntropyHeader curr_entropy_hdr_
;
184 const uint8_t* stream_
;
188 DISALLOW_COPY_AND_ASSIGN(Vp8Parser
);
193 #endif // MEDIA_FILTERS_VP8_PARSER_H_