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 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.
13 // uint8_t* frame_stream;
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)) {
23 // // Got a frame parsed successfully.
27 #ifndef MEDIA_FILTERS_VP9_PARSER_H_
28 #define MEDIA_FILTERS_VP9_PARSER_H_
33 #include "base/macros.h"
34 #include "media/base/media_export.h"
35 #include "media/filters/vp9_raw_bits_reader.h"
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
{
56 enum class Vp9InterpFilter
{
57 INTERP_FILTER_SELECT
= 0,
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;
74 uint8_t tree_probs
[kNumTreeProbs
];
76 uint8_t pred_probs
[kNumPredictionProbs
];
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;
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 &&
114 struct MEDIA_EXPORT Vp9FrameHeader
{
120 bool IsKeyframe() const { return frame_type
== KEYFRAME
; }
124 bool show_existing_frame
;
125 uint8_t frame_to_show
;
127 FrameType frame_type
;
130 bool error_resilient_mode
;
133 Vp9ColorSpace color_space
;
135 uint8_t subsampling_x
;
136 uint8_t subsampling_y
;
138 // The range of width and height is 1..2^16.
141 uint32_t display_width
;
142 uint32_t display_height
;
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
{
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
);
181 // The parsing context to keep track of references.
182 struct ReferenceSlot
{
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.
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
);
220 #endif // MEDIA_FILTERS_VP9_PARSER_H_