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