OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file contains an implementation of a VP9 bitstream parser. The main | 5 // This file contains an implementation of a VP9 bitstream parser. The main |
6 // purpose of this parser is to support hardware decode acceleration. Some | 6 // purpose of this parser is to support hardware decode acceleration. Some |
7 // accelerators, e.g. libva which implements VA-API, require the caller | 7 // accelerators, e.g. libva which implements VA-API, require the caller |
8 // (chrome) to feed them parsed VP9 frame header. | 8 // (chrome) to feed them parsed VP9 frame header. |
9 // | 9 // |
10 // Example usage: | 10 // See content::VP9Decoder for example usage. |
11 // { | |
12 // Vp9Parser parser; | |
13 // uint8_t* frame_stream; | |
14 // size_t frame_size; | |
15 // | 11 // |
16 // // Get frames from, say, WebM parser or IVF parser. | |
17 // while (GetVp9Frame(&frame_stream, &frame_size)) { | |
18 // Vp9FrameHeader header; | |
19 // if (!parser.ParseFrame(frame_stream, frame_size, &header)) { | |
20 // // Parse failed. | |
21 // return false; | |
22 // } | |
23 // // Got a frame parsed successfully. | |
24 // } | |
25 // } | |
26 | |
27 #ifndef MEDIA_FILTERS_VP9_PARSER_H_ | 12 #ifndef MEDIA_FILTERS_VP9_PARSER_H_ |
28 #define MEDIA_FILTERS_VP9_PARSER_H_ | 13 #define MEDIA_FILTERS_VP9_PARSER_H_ |
29 | 14 |
30 #include <stddef.h> | 15 #include <stddef.h> |
31 #include <stdint.h> | 16 #include <stdint.h> |
32 | 17 |
18 #include <deque> | |
19 | |
33 #include "base/macros.h" | 20 #include "base/macros.h" |
34 #include "media/base/media_export.h" | 21 #include "media/base/media_export.h" |
35 #include "media/filters/vp9_raw_bits_reader.h" | 22 #include "media/filters/vp9_raw_bits_reader.h" |
36 | 23 |
37 namespace media { | 24 namespace media { |
38 | 25 |
39 const int kVp9MaxProfile = 4; | 26 const int kVp9MaxProfile = 4; |
40 const int kVp9NumRefFramesLog2 = 3; | 27 const int kVp9NumRefFramesLog2 = 3; |
41 const int kVp9NumRefFrames = 1 << kVp9NumRefFramesLog2; | 28 const size_t kVp9NumRefFrames = 1 << kVp9NumRefFramesLog2; |
42 const uint8_t kVp9MaxProb = 255; | 29 const uint8_t kVp9MaxProb = 255; |
43 const int kVp9NumRefsPerFrame = 3; | 30 const size_t kVp9NumRefsPerFrame = 3; |
44 | 31 |
45 enum class Vp9ColorSpace { | 32 enum class Vp9ColorSpace { |
46 UNKNOWN = 0, | 33 UNKNOWN = 0, |
47 BT_601 = 1, | 34 BT_601 = 1, |
48 BT_709 = 2, | 35 BT_709 = 2, |
49 SMPTE_170 = 3, | 36 SMPTE_170 = 3, |
50 SMPTE_240 = 4, | 37 SMPTE_240 = 4, |
51 BT_2020 = 5, | 38 BT_2020 = 5, |
52 RESERVED = 6, | 39 RESERVED = 6, |
53 SRGB = 7, | 40 SRGB = 7, |
54 }; | 41 }; |
55 | 42 |
56 enum class Vp9InterpFilter { | 43 enum Vp9InterpFilter { |
57 INTERP_FILTER_SELECT = 0, | 44 EIGHTTAP = 0, |
kcwu
2015/08/31 09:58:35
ah, sorry for the bug
Pawel Osciak
2015/09/08 06:43:49
No worries.
| |
58 EIGHTTAP_SMOOTH = 1, | 45 EIGHTTAP_SMOOTH = 1, |
59 EIGHTTAP = 2, | 46 EIGHTTAP_SHARP = 2, |
60 EIGHTTAP_SHARP = 3, | 47 BILINEAR = 3, |
61 BILINEAR = 4, | 48 SWICHABLE = 4, |
62 }; | 49 }; |
63 | 50 |
64 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseFrame. | |
65 struct MEDIA_EXPORT Vp9Segmentation { | 51 struct MEDIA_EXPORT Vp9Segmentation { |
66 static const int kNumSegments = 8; | 52 static const size_t kNumSegments = 8; |
67 static const int kNumTreeProbs = kNumSegments - 1; | 53 static const size_t kNumTreeProbs = kNumSegments - 1; |
68 static const int kNumPredictionProbs = 3; | 54 static const size_t kNumPredictionProbs = 3; |
69 static const int kNumFeatures = 4; | 55 enum SegmentLevelFeature { |
56 SEG_LVL_ALT_Q = 0, | |
57 SEG_LVL_ALT_LF = 1, | |
58 SEG_LVL_REF_FRAME = 2, | |
59 SEG_LVL_SKIP = 3, | |
60 SEG_LVL_MAX | |
61 }; | |
70 | 62 |
71 bool enabled; | 63 bool enabled; |
72 | 64 |
73 bool update_map; | 65 bool update_map; |
74 uint8_t tree_probs[kNumTreeProbs]; | 66 uint8_t tree_probs[kNumTreeProbs]; |
75 bool temporal_update; | 67 bool temporal_update; |
76 uint8_t pred_probs[kNumPredictionProbs]; | 68 uint8_t pred_probs[kNumPredictionProbs]; |
77 | 69 |
78 bool update_data; | 70 bool update_data; |
79 bool abs_delta; | 71 bool abs_delta; |
80 bool feature_enabled[kNumSegments][kNumFeatures]; | 72 bool feature_enabled[kNumSegments][SEG_LVL_MAX]; |
81 int8_t feature_data[kNumSegments][kNumFeatures]; | 73 int8_t feature_data[kNumSegments][SEG_LVL_MAX]; |
74 | |
75 int16_t y_dequant[kNumSegments][2]; | |
76 int16_t uv_dequant[kNumSegments][2]; | |
77 | |
78 bool FeatureEnabled(size_t seg_id, SegmentLevelFeature feature) const { | |
79 return feature_enabled[seg_id][feature]; | |
80 } | |
81 | |
82 int8_t FeatureData(size_t seg_id, SegmentLevelFeature feature) const { | |
83 return feature_data[seg_id][feature]; | |
84 } | |
82 }; | 85 }; |
83 | 86 |
84 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseFrame. | |
85 struct MEDIA_EXPORT Vp9LoopFilter { | 87 struct MEDIA_EXPORT Vp9LoopFilter { |
86 static const int kNumRefDeltas = 4; | 88 enum Vp9FrameType { |
87 static const int kNumModeDeltas = 2; | 89 VP9_FRAME_INTRA = 0, |
90 VP9_FRAME_LAST = 1, | |
91 VP9_FRAME_GOLDEN = 2, | |
92 VP9_FRAME_ALTREF = 3, | |
93 VP9_FRAME_MAX = 4, | |
94 }; | |
95 | |
96 static const size_t kNumModeDeltas = 2; | |
88 | 97 |
89 uint8_t filter_level; | 98 uint8_t filter_level; |
90 uint8_t sharpness_level; | 99 uint8_t sharpness_level; |
91 | 100 |
92 bool mode_ref_delta_enabled; | 101 bool mode_ref_delta_enabled; |
93 bool mode_ref_delta_update; | 102 bool mode_ref_delta_update; |
94 bool update_ref_deltas[kNumRefDeltas]; | 103 bool update_ref_deltas[VP9_FRAME_MAX]; |
95 int8_t ref_deltas[kNumRefDeltas]; | 104 int8_t ref_deltas[VP9_FRAME_MAX]; |
96 bool update_mode_deltas[kNumModeDeltas]; | 105 bool update_mode_deltas[kNumModeDeltas]; |
97 int8_t mode_deltas[kNumModeDeltas]; | 106 int8_t mode_deltas[kNumModeDeltas]; |
107 | |
108 uint8_t lvl[Vp9Segmentation::kNumSegments][VP9_FRAME_MAX][kNumModeDeltas]; | |
98 }; | 109 }; |
99 | 110 |
100 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseFrame. | 111 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseNextFrame. |
101 struct MEDIA_EXPORT Vp9QuantizationParams { | 112 struct MEDIA_EXPORT Vp9QuantizationParams { |
102 bool IsLossless() const { | 113 bool IsLossless() const { |
103 return base_qindex == 0 && y_dc_delta == 0 && uv_dc_delta == 0 && | 114 return base_qindex == 0 && y_dc_delta == 0 && uv_dc_delta == 0 && |
104 uv_ac_delta == 0; | 115 uv_ac_delta == 0; |
105 } | 116 } |
106 | 117 |
107 uint8_t base_qindex; | 118 uint8_t base_qindex; |
108 int8_t y_dc_delta; | 119 int8_t y_dc_delta; |
109 int8_t uv_dc_delta; | 120 int8_t uv_dc_delta; |
110 int8_t uv_ac_delta; | 121 int8_t uv_ac_delta; |
111 }; | 122 }; |
112 | 123 |
113 // VP9 frame header. | 124 // VP9 frame header. |
114 struct MEDIA_EXPORT Vp9FrameHeader { | 125 struct MEDIA_EXPORT Vp9FrameHeader { |
115 enum FrameType { | 126 enum FrameType { |
116 KEYFRAME = 0, | 127 KEYFRAME = 0, |
117 INTERFRAME = 1, | 128 INTERFRAME = 1, |
118 }; | 129 }; |
119 | 130 |
120 bool IsKeyframe() const { return frame_type == KEYFRAME; } | 131 bool IsKeyframe() const { return frame_type == KEYFRAME; } |
132 bool RefreshFlag(size_t i) const { return refresh_flags & (1u << i); } | |
kcwu
2015/08/31 09:58:35
|reader_| is big endian and MSB first inside integ
Pawel Osciak
2015/09/08 06:43:49
Yes, I was surprised too, but spec and libvpx both
| |
121 | 133 |
122 uint8_t profile; | 134 uint8_t profile; |
123 | 135 |
124 bool show_existing_frame; | 136 bool show_existing_frame; |
125 uint8_t frame_to_show; | 137 uint8_t frame_to_show; |
126 | 138 |
127 FrameType frame_type; | 139 FrameType frame_type; |
128 | 140 |
129 bool show_frame; | 141 bool show_frame; |
130 bool error_resilient_mode; | 142 bool error_resilient_mode; |
131 | 143 |
132 uint8_t bit_depth; | 144 uint8_t bit_depth; |
133 Vp9ColorSpace color_space; | 145 Vp9ColorSpace color_space; |
134 bool yuv_range; | 146 bool yuv_range; |
135 uint8_t subsampling_x; | 147 uint8_t subsampling_x; |
136 uint8_t subsampling_y; | 148 uint8_t subsampling_y; |
137 | 149 |
138 // The range of width and height is 1..2^16. | 150 // The range of width and height is 1..2^16. |
139 uint32_t width; | 151 uint32_t width; |
140 uint32_t height; | 152 uint32_t height; |
141 uint32_t display_width; | 153 uint32_t display_width; |
142 uint32_t display_height; | 154 uint32_t display_height; |
143 | 155 |
144 bool intra_only; | 156 bool intra_only; |
145 uint8_t reset_context; | 157 uint8_t reset_context; |
146 bool refresh_flag[kVp9NumRefFrames]; | 158 uint8_t refresh_flags; |
147 uint8_t frame_refs[kVp9NumRefsPerFrame]; | 159 uint8_t frame_refs[kVp9NumRefsPerFrame]; |
148 bool ref_sign_biases[kVp9NumRefsPerFrame]; | 160 bool ref_sign_biases[kVp9NumRefsPerFrame]; |
149 bool allow_high_precision_mv; | 161 bool allow_high_precision_mv; |
150 Vp9InterpFilter interp_filter; | 162 Vp9InterpFilter interp_filter; |
151 | 163 |
152 bool refresh_frame_context; | 164 bool refresh_frame_context; |
153 bool frame_parallel_decoding_mode; | 165 bool frame_parallel_decoding_mode; |
154 uint8_t frame_context_idx; | 166 uint8_t frame_context_idx; |
155 | 167 |
156 Vp9LoopFilter loop_filter; | |
157 Vp9QuantizationParams quant_params; | 168 Vp9QuantizationParams quant_params; |
158 Vp9Segmentation segment; | |
159 | 169 |
160 uint8_t log2_tile_cols; | 170 uint8_t log2_tile_cols; |
161 uint8_t log2_tile_rows; | 171 uint8_t log2_tile_rows; |
162 | 172 |
173 // Pointer to the beginning of frame data. It is a responsibility of the | |
174 // client of the Vp9Parser to maintain validity of this data while it is | |
175 // being used outside of that class. | |
176 const uint8_t* data; | |
177 | |
178 // Size of |data| in bytes. | |
179 size_t frame_size; | |
180 | |
163 // Size of compressed header in bytes. | 181 // Size of compressed header in bytes. |
164 size_t first_partition_size; | 182 size_t first_partition_size; |
165 | 183 |
166 // Size of uncompressed header in bytes. | 184 // Size of uncompressed header in bytes. |
167 size_t uncompressed_header_size; | 185 size_t uncompressed_header_size; |
168 }; | 186 }; |
169 | 187 |
170 // A parser for VP9 bitstream. | 188 // A parser for VP9 bitstream. |
171 class MEDIA_EXPORT Vp9Parser { | 189 class MEDIA_EXPORT Vp9Parser { |
172 public: | 190 public: |
191 // ParseNextFrame() return values. See documentation for ParseNextFrame(). | |
192 enum Result { | |
193 kOk, | |
194 kInvalidStream, | |
195 kEOStream, | |
196 }; | |
197 | |
173 Vp9Parser(); | 198 Vp9Parser(); |
174 | 199 |
175 // Parses one frame and fills parsing result to |fhdr|. Returns true on | 200 // Set a new stream buffer to read from, starting at |stream| and of size |
176 // success, false otherwise. | 201 // |stream_size| in bytes. |stream| must point to the beginning of a single |
177 // |stream| is the address of VP9 bitstream with |size|. | 202 // frame or a single superframe, is owned by caller and must remain valid |
178 bool ParseFrame(const uint8_t* stream, size_t size, Vp9FrameHeader* fhdr); | 203 // until the next call to SetStream(). |
204 void SetStream(const uint8_t* stream, off_t stream_size); | |
205 | |
206 // Parse the next frame in the current stream buffer, filling |fhdr| with | |
207 // the parsed frame header and updating current segmentation and loop filter | |
208 // state. Return kOk if a frame has successfully been parsed, kEOStream if | |
209 // there is no more data in the current stream buffer, or kInvalidStream | |
210 // on error. | |
211 Result ParseNextFrame(Vp9FrameHeader* fhdr); | |
212 | |
213 // Return current segmentation state. | |
214 const Vp9Segmentation& GetSegmentation() const { return segmentation_; } | |
215 | |
216 // Return current loop filter state. | |
217 const Vp9LoopFilter& GetLoopFilter() const { return loop_filter_; } | |
179 | 218 |
180 private: | 219 private: |
181 // The parsing context to keep track of references. | 220 // The parsing context to keep track of references. |
182 struct ReferenceSlot { | 221 struct ReferenceSlot { |
183 uint32_t width; | 222 uint32_t width; |
184 uint32_t height; | 223 uint32_t height; |
185 }; | 224 }; |
186 | 225 |
226 bool ParseSuperframe(); | |
187 uint8_t ReadProfile(); | 227 uint8_t ReadProfile(); |
188 bool VerifySyncCode(); | 228 bool VerifySyncCode(); |
189 bool ReadBitDepthColorSpaceSampling(Vp9FrameHeader* fhdr); | 229 bool ReadBitDepthColorSpaceSampling(Vp9FrameHeader* fhdr); |
190 void ReadFrameSize(Vp9FrameHeader* fhdr); | 230 void ReadFrameSize(Vp9FrameHeader* fhdr); |
191 bool ReadFrameSizeFromRefs(Vp9FrameHeader* fhdr); | 231 bool ReadFrameSizeFromRefs(Vp9FrameHeader* fhdr); |
192 void ReadDisplayFrameSize(Vp9FrameHeader* fhdr); | 232 void ReadDisplayFrameSize(Vp9FrameHeader* fhdr); |
193 Vp9InterpFilter ReadInterpFilter(); | 233 Vp9InterpFilter ReadInterpFilter(); |
194 void ReadLoopFilter(Vp9LoopFilter* loop_filter); | 234 void ReadLoopFilter(); |
195 void ReadQuantization(Vp9QuantizationParams* quants); | 235 void ReadQuantization(Vp9QuantizationParams* quants); |
196 void ReadSegmentationMap(Vp9Segmentation* segment); | 236 void ReadSegmentationMap(); |
197 void ReadSegmentationData(Vp9Segmentation* segment); | 237 void ReadSegmentationData(); |
198 void ReadSegmentation(Vp9Segmentation* segment); | 238 void ReadSegmentation(); |
199 void ReadTiles(Vp9FrameHeader* fhdr); | 239 void ReadTiles(Vp9FrameHeader* fhdr); |
200 bool ParseUncompressedHeader(Vp9FrameHeader* fhdr); | 240 bool ParseUncompressedHeader(const uint8_t* stream, |
241 off_t frame_size, | |
242 Vp9FrameHeader* fhdr); | |
201 void UpdateSlots(const Vp9FrameHeader* fhdr); | 243 void UpdateSlots(const Vp9FrameHeader* fhdr); |
202 | 244 |
203 // Start address of VP9 bitstream buffer. | 245 void ResetLoopfilter(); |
246 void SetupPastIndependence(); | |
247 size_t GetQIndex(const Vp9QuantizationParams& quant, size_t segid) const; | |
248 void SetupSegmentationDequant(const Vp9QuantizationParams& quant); | |
249 void SetupLoopFilter(); | |
250 | |
251 // Current address in the bitstream buffer. | |
204 const uint8_t* stream_; | 252 const uint8_t* stream_; |
205 | 253 |
206 // Size of |stream_| in bytes. | 254 // Remaining bytes in stream_. |
207 size_t size_; | 255 off_t bytes_left_; |
256 | |
257 // Stores start pointer and size of each frame within the current superframe. | |
258 struct FrameInfo { | |
259 FrameInfo(const uint8_t* ptr, off_t size); | |
260 | |
261 // Starting address of the frame. | |
262 const uint8_t* ptr; | |
263 | |
264 // Size of the frame in bytes. | |
265 off_t size; | |
266 }; | |
267 | |
268 // FrameInfo for the remaining frames in the current superframe to be parsed. | |
269 std::deque<FrameInfo> frames_; | |
208 | 270 |
209 // Raw bits decoder for uncompressed frame header. | 271 // Raw bits decoder for uncompressed frame header. |
210 Vp9RawBitsReader reader_; | 272 Vp9RawBitsReader reader_; |
211 | 273 |
274 // Segmentation and loop filter state that persists across frames. | |
275 Vp9Segmentation segmentation_; | |
276 Vp9LoopFilter loop_filter_; | |
277 | |
212 // The parsing context to keep track of references. | 278 // The parsing context to keep track of references. |
213 ReferenceSlot ref_slots_[kVp9NumRefFrames]; | 279 ReferenceSlot ref_slots_[kVp9NumRefFrames]; |
214 | 280 |
215 DISALLOW_COPY_AND_ASSIGN(Vp9Parser); | 281 DISALLOW_COPY_AND_ASSIGN(Vp9Parser); |
216 }; | 282 }; |
217 | 283 |
218 } // namespace media | 284 } // namespace media |
219 | 285 |
220 #endif // MEDIA_FILTERS_VP9_PARSER_H_ | 286 #endif // MEDIA_FILTERS_VP9_PARSER_H_ |
OLD | NEW |