Roll src/third_party/WebKit 787a07c:716df21 (svn 201034:201036)
[chromium-blink-merge.git] / media / filters / vp9_parser.h
blob877043179096747e3cc30ba2aaa7bac9117d5df2
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 VP9 bitstream parser. The main
6 // purpose of this parser is to support hardware decode acceleration. Some
7 // accelerators, e.g. libva which implements VA-API, require the caller
8 // (chrome) to feed them parsed VP9 frame header.
9 //
10 // Example usage:
11 // {
12 // Vp9Parser parser;
13 // uint8_t* frame_stream;
14 // size_t frame_size;
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 // }
27 #ifndef MEDIA_FILTERS_VP9_PARSER_H_
28 #define MEDIA_FILTERS_VP9_PARSER_H_
30 #include <stddef.h>
31 #include <stdint.h>
33 #include "base/macros.h"
34 #include "media/base/media_export.h"
35 #include "media/filters/vp9_raw_bits_reader.h"
37 namespace media {
39 const int kVp9MaxProfile = 4;
40 const int kVp9NumRefFramesLog2 = 3;
41 const int kVp9NumRefFrames = 1 << kVp9NumRefFramesLog2;
42 const uint8_t kVp9MaxProb = 255;
43 const int kVp9NumRefsPerFrame = 3;
45 enum class Vp9ColorSpace {
46 UNKNOWN = 0,
47 BT_601 = 1,
48 BT_709 = 2,
49 SMPTE_170 = 3,
50 SMPTE_240 = 4,
51 BT_2020 = 5,
52 RESERVED = 6,
53 SRGB = 7,
56 enum class Vp9InterpFilter {
57 INTERP_FILTER_SELECT = 0,
58 EIGHTTAP_SMOOTH = 1,
59 EIGHTTAP = 2,
60 EIGHTTAP_SHARP = 3,
61 BILINEAR = 4,
64 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseFrame.
65 struct MEDIA_EXPORT Vp9Segmentation {
66 static const int kNumSegments = 8;
67 static const int kNumTreeProbs = kNumSegments - 1;
68 static const int kNumPredictionProbs = 3;
69 static const int kNumFeatures = 4;
71 bool enabled;
73 bool update_map;
74 uint8_t tree_probs[kNumTreeProbs];
75 bool temporal_update;
76 uint8_t pred_probs[kNumPredictionProbs];
78 bool update_data;
79 bool abs_delta;
80 bool feature_enabled[kNumSegments][kNumFeatures];
81 int8_t feature_data[kNumSegments][kNumFeatures];
84 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseFrame.
85 struct MEDIA_EXPORT Vp9LoopFilter {
86 static const int kNumRefDeltas = 4;
87 static const int kNumModeDeltas = 2;
89 uint8_t filter_level;
90 uint8_t sharpness_level;
92 bool mode_ref_delta_enabled;
93 bool mode_ref_delta_update;
94 bool update_ref_deltas[kNumRefDeltas];
95 int8_t ref_deltas[kNumRefDeltas];
96 bool update_mode_deltas[kNumModeDeltas];
97 int8_t mode_deltas[kNumModeDeltas];
100 // Members of Vp9FrameHeader will be 0-initialized by Vp9Parser::ParseFrame.
101 struct MEDIA_EXPORT Vp9QuantizationParams {
102 bool IsLossless() const {
103 return base_qindex == 0 && y_dc_delta == 0 && uv_dc_delta == 0 &&
104 uv_ac_delta == 0;
107 uint8_t base_qindex;
108 int8_t y_dc_delta;
109 int8_t uv_dc_delta;
110 int8_t uv_ac_delta;
113 // VP9 frame header.
114 struct MEDIA_EXPORT Vp9FrameHeader {
115 enum FrameType {
116 KEYFRAME = 0,
117 INTERFRAME = 1,
120 bool IsKeyframe() const { return frame_type == KEYFRAME; }
122 uint8_t profile;
124 bool show_existing_frame;
125 uint8_t frame_to_show;
127 FrameType frame_type;
129 bool show_frame;
130 bool error_resilient_mode;
132 uint8_t bit_depth;
133 Vp9ColorSpace color_space;
134 bool yuv_range;
135 uint8_t subsampling_x;
136 uint8_t subsampling_y;
138 // The range of width and height is 1..2^16.
139 uint32_t width;
140 uint32_t height;
141 uint32_t display_width;
142 uint32_t display_height;
144 bool intra_only;
145 uint8_t reset_context;
146 bool refresh_flag[kVp9NumRefFrames];
147 uint8_t frame_refs[kVp9NumRefsPerFrame];
148 bool ref_sign_biases[kVp9NumRefsPerFrame];
149 bool allow_high_precision_mv;
150 Vp9InterpFilter interp_filter;
152 bool refresh_frame_context;
153 bool frame_parallel_decoding_mode;
154 uint8_t frame_context_idx;
156 Vp9LoopFilter loop_filter;
157 Vp9QuantizationParams quant_params;
158 Vp9Segmentation segment;
160 uint8_t log2_tile_cols;
161 uint8_t log2_tile_rows;
163 // Size of compressed header in bytes.
164 size_t first_partition_size;
166 // Size of uncompressed header in bytes.
167 size_t uncompressed_header_size;
170 // A parser for VP9 bitstream.
171 class MEDIA_EXPORT Vp9Parser {
172 public:
173 Vp9Parser();
175 // Parses one frame and fills parsing result to |fhdr|. Returns true on
176 // success, false otherwise.
177 // |stream| is the address of VP9 bitstream with |size|.
178 bool ParseFrame(const uint8_t* stream, size_t size, Vp9FrameHeader* fhdr);
180 private:
181 // The parsing context to keep track of references.
182 struct ReferenceSlot {
183 uint32_t width;
184 uint32_t height;
187 uint8_t ReadProfile();
188 bool VerifySyncCode();
189 bool ReadBitDepthColorSpaceSampling(Vp9FrameHeader* fhdr);
190 void ReadFrameSize(Vp9FrameHeader* fhdr);
191 bool ReadFrameSizeFromRefs(Vp9FrameHeader* fhdr);
192 void ReadDisplayFrameSize(Vp9FrameHeader* fhdr);
193 Vp9InterpFilter ReadInterpFilter();
194 void ReadLoopFilter(Vp9LoopFilter* loop_filter);
195 void ReadQuantization(Vp9QuantizationParams* quants);
196 void ReadSegmentationMap(Vp9Segmentation* segment);
197 void ReadSegmentationData(Vp9Segmentation* segment);
198 void ReadSegmentation(Vp9Segmentation* segment);
199 void ReadTiles(Vp9FrameHeader* fhdr);
200 bool ParseUncompressedHeader(Vp9FrameHeader* fhdr);
201 void UpdateSlots(const Vp9FrameHeader* fhdr);
203 // Start address of VP9 bitstream buffer.
204 const uint8_t* stream_;
206 // Size of |stream_| in bytes.
207 size_t size_;
209 // Raw bits decoder for uncompressed frame header.
210 Vp9RawBitsReader reader_;
212 // The parsing context to keep track of references.
213 ReferenceSlot ref_slots_[kVp9NumRefFrames];
215 DISALLOW_COPY_AND_ASSIGN(Vp9Parser);
218 } // namespace media
220 #endif // MEDIA_FILTERS_VP9_PARSER_H_