1 /*
2  * gstvp8parser.h - VP8 parser
3  *
4  * Copyright (C) 2013-2014 Intel Corporation
5  *   Author: Halley Zhao <halley.zhao@intel.com>
6  *   Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef GST_VP8_PARSER_H
25 #define GST_VP8_PARSER_H
26 
27 #include <gst/gst.h>
28 #include <gst/codecparsers/codecparsers-prelude.h>
29 
30 G_BEGIN_DECLS
31 
32 typedef struct _GstVp8FrameHdr          GstVp8FrameHdr;
33 typedef struct _GstVp8QuantIndices      GstVp8QuantIndices;
34 typedef struct _GstVp8Segmentation      GstVp8Segmentation;
35 typedef struct _GstVp8MbLfAdjustments   GstVp8MbLfAdjustments;
36 typedef struct _GstVp8TokenProbs        GstVp8TokenProbs;
37 typedef struct _GstVp8MvProbs           GstVp8MvProbs;
38 typedef struct _GstVp8ModeProbs         GstVp8ModeProbs;
39 typedef struct _GstVp8Parser            GstVp8Parser;
40 
41 /**
42  * GstVp8ParserResult:
43  * @GST_VP8_PARSER_OK: The parsing succeeded
44  * @GST_VP8_PARSER_BROKEN_DATA: The data to parse is broken
45  * @GST_VP8_PARSER_ERROR: An error accured when parsing
46  *
47  * The result of parsing VP8 data.
48  */
49 typedef enum {
50   GST_VP8_PARSER_OK,
51   GST_VP8_PARSER_BROKEN_DATA,
52   GST_VP8_PARSER_ERROR,
53 } GstVp8ParserResult;
54 
55 /**
56  * GstVpQuantIndices:
57  * @y_ac_qi: indicates the dequantization table index used for the
58  *   luma AC coefficients
59  * @y_dc_delta: indicates the delta value that is added to the
60  *   baseline index to obtain the luma DC coefficient dequantization
61  *   index
62  * @y2_dc_delta: indicates the delta value that is added to the
63  *   baseline index to obtain the Y2 block DC coefficient dequantization
64  *   index
65  * @y2_ac_delta: indicates the delta value that is added to the
66  *   baseline index to obtain the Y2 block AC coefficient dequantization
67  *   index
68  * @uv_dc_delta: indicates the delta value that is added to the
69  *   baseline index to obtain the chroma DC coefficient dequantization
70  *   index
71  * @uv_ac_delta: indicates the delta value that is added to the
72  *   baseline index to obtain the chroma AC coefficient dequantization
73  *   index
74  *
75  * Dequantization indices.
76  */
77 struct _GstVp8QuantIndices
78 {
79   guint8 y_ac_qi;
80   gint8 y_dc_delta;
81   gint8 y2_dc_delta;
82   gint8 y2_ac_delta;
83   gint8 uv_dc_delta;
84   gint8 uv_ac_delta;
85 };
86 
87 /**
88  * GstVp8Segmentation:
89  * @segmentation_enabled: enables the segmentation feature for the
90  *   current frame
91  * @update_mb_segmentation_map: determines if the MB segmentation map
92  *   is updated in the current frame
93  * @update_segment_feature_data: indicates if the segment feature data
94  *   is updated in the current frame
95  * @segment_feature_mode: indicates the feature data update mode (0:
96  *   delta, 1: absolute value)
97  * @quantizer_update_value: indicates the update value for the segment
98  *   quantizer
99  * @lf_update_value: indicates the update value for the loop filter level
100  * @segment_prob: indicates the branch probabilities of the segment_id
101  *   decoding tree
102  *
103  * Segmentation feature data.
104  */
105 struct _GstVp8Segmentation
106 {
107   guint8 segmentation_enabled;
108   guint8 update_mb_segmentation_map;
109   guint8 update_segment_feature_data;
110 
111   /* if update_segment_feature_data == 1 */
112   guint8 segment_feature_mode;
113   gint8 quantizer_update_value[4];
114   gint8 lf_update_value[4];
115 
116   /* if update_mb_segmentation_map == 1 */
117   guint8 segment_prob[3];
118 };
119 
120 /**
121  * GstVp8MbLfAdjustments:
122  * @loop_filter_adj_enable: indicates if the MB-level loop filter
123  *   adjustment is on for the current frame
124  * @mode_ref_lf_delta_update: indicates if the delta values used in an
125  *   adjustment are updated in the current frame
126  * @ref_frame_delta: indicates the adjustment delta value
127  *   corresponding to a certain used reference frame
128  * @mb_mode_delta: indicates the adjustment delta value corresponding
129  *   to a certain MB prediction mode
130  *
131  * MB-level loop filter adjustments.
132  */
133 struct _GstVp8MbLfAdjustments
134 {
135   guint8 loop_filter_adj_enable;
136   guint8 mode_ref_lf_delta_update;
137 
138   /* if mode_ref_lf_delta_update == 1 */
139   gint8 ref_frame_delta[4];
140   gint8 mb_mode_delta[4];
141 };
142 
143 /**
144  * GstVp8TokenProbs:
145  * @prob: token probability
146  *
147  * Token probabilities, with cumulative updates applied.
148  *
149  * Each probability value in this matrix is live across frames, until
150  * they are reset to their default values on key frame.
151  */
152 struct _GstVp8TokenProbs
153 {
154   guint8 prob[4][8][3][11];
155 };
156 
157 /**
158  * GstVp8MvProbs:
159  * @prob: MV probability
160  *
161  * Probabilities used for motion vector decoding, with cumulative
162  * updates applied.
163  *
164  * Each probability value in this matrix is live across frames, until
165  * they are reset to their default values on key frame.
166  */
167 struct _GstVp8MvProbs
168 {
169   guint8 prob[2][19];
170 };
171 
172 /**
173  * GstVp8ModeProbs:
174  * @y_prob: indicates the branch probabilities of the luma
175  *   intra-prediction mode decoding tree
176  * @uv_prob: indicates the branch probabilities of the chroma
177  *   intra-prediction mode decoding tree
178  *
179  * Probabilities used for intra-prediction mode decoding tree.
180  *
181  * Each probability value in thie structure is live across frames,
182  * until they are reset to their default values on key frame.
183  */
184 struct _GstVp8ModeProbs
185 {
186   guint8 y_prob[4];
187   guint8 uv_prob[3];
188 };
189 
190 /**
191  * GstVp8FrameHdr:
192  * @key_frame: indicates whether the frame is a key frame (1), or an
193  *   inter frame (0)
194  * @version: version number
195  * @show_frame: indicates whether the frame is meant to be displayed (1),
196  *   or not (0)
197  * @data_chunk_size: the size in bytes of the Uncompressed Data Chunk
198  * @first_part_size: the size in bytes of the first partition (control
199  *   partition), excluding the uncompressed data chunk
200  * @width: the frame width in pixels
201  * @height: the frame height in pixels
202  * @horiz_scale_code: horizontal scale code value
203  * @vert_scale_code: vertical scale code value
204  * @color_space: defines the YUV color space of the sequence
205  * @clamping_type: specifies if the decoder is required to clamp the
206  *   reconstructed pixel values
207  * @filter_type: determines whether the normal or the simple loop
208  *   filter is used
209  * @loop_filter_level: controls the deblocking filter
210  * @sharpness_level: controls the deblocking filter
211  * @log2_nbr_of_dct_partitions: determines the number of separate
212  *   partitions containing the DCT coefficients of the macroblocks
213  * @partition_size: determines the size of each separate partition
214  *   containing the DCT coefficients of the macroblocks, including the
215  *   very last one (calculated size)
216  * @quant_indices: dequantization indices (see #GstVp8QuantIndices)
217  * @token_probs: token probabilities (see #GstVp8TokenProbs)
218  * @mv_probs: probabilities used for motion vector decoding
219  *   (see #GstVp8MvProbs)
220  * @mode_probs: probabilities used for intra-prediction mode decoding
221  *   tree (see #GstVp8ModeProbs)
222  * @refresh_entropy_probs: determines whether updated token
223  *   probabilities are used only for this frame or until further update
224  * @refresh_golden_frame: determines if the current decoded frame
225  *   refreshes the golden frame
226  * @refresh_alternate_frame: determines if the current decoded frame
227  *   refreshes the alternate reference frame
228  * @copy_buffer_to_golden: determines if the golden reference is
229  *   replaced by another reference
230  * @copy_buffer_to_alternate: determines if the alternate reference is
231  *   replaced by another reference
232  * @sign_bias_golden: controls the sign of motion vectors when the
233  *   golden frame is referenced
234  * @sign_bias_alternate: controls the sign of motion vectors when the
235  *   alternate frame is referenced
236  * @refresh_last: determines if the current decoded frame refreshes
237  *   the last frame reference buffer
238  * @mb_no_skip_coeff: enables (0) or disables (1) the skipping of
239  *   macroblocks containing no non-zero coefficients
240  * @prob_skip_false: indicates the probability that the macroblock is
241  *   not skipped
242  * @prob_intra: indicates the probability of an intra macroblock
243  * @prob_last: indicates the probability that the last reference frame
244  *   is used for inter-prediction
245  * @prob_gf: indicates the probability that the golden reference frame
246  *   is used for inter-prediction
247  * @rd_range: last range decoder value for "Range"
248  * @rd_value: last range decoder value for "Value"
249  * @rd_count: number of bits left in range decoder "Value" (@rd_value)
250  * @header_size: the size in bits of the Frame Header, thus excluding
251  *   any Uncompressed Data Chunk bytes
252  *
253  * Frame header.
254  */
255 struct _GstVp8FrameHdr
256 {
257   guint8 key_frame;
258   guint8 version;
259   guint8 show_frame;
260   guint8 data_chunk_size;
261   guint32 first_part_size;
262 
263   /* if key_frame == 1 */
264   guint16 width;
265   guint16 height;
266   guint8 horiz_scale_code;
267   guint8 vert_scale_code;
268   guint8 color_space;
269   guint8 clamping_type;
270 
271   guint8 filter_type;
272   guint8 loop_filter_level;
273   guint8 sharpness_level;
274   guint8 log2_nbr_of_dct_partitions;
275   guint partition_size[8];
276 
277   GstVp8QuantIndices quant_indices;
278   GstVp8TokenProbs token_probs;
279   GstVp8MvProbs mv_probs;
280   GstVp8ModeProbs mode_probs;
281 
282   guint8 refresh_entropy_probs;
283   guint8 refresh_last;
284   /* if key_frame != 1 */
285   guint8 refresh_golden_frame;
286   guint8 refresh_alternate_frame;
287   guint8 copy_buffer_to_golden;
288   guint8 copy_buffer_to_alternate;
289   guint8 sign_bias_golden;
290   guint8 sign_bias_alternate;
291 
292   guint8 mb_no_skip_coeff;
293   guint8 prob_skip_false;
294 
295   /* if key_frame != 1 */
296   guint8 prob_intra;
297   guint8 prob_last;
298   guint8 prob_gf;
299 
300   /* Range decoder state */
301   guint8 rd_range;
302   guint8 rd_value;
303   guint8 rd_count;
304 
305   /* Size of the Frame Header in bits */
306   guint header_size;
307 };
308 
309 /**
310  * GstVp8Parser:
311  * @segmentation: segmentation feature data
312  * @mb_lf_adjust: MB-level loop filter adjustments
313  * @token_probs: token probabilities
314  * @mv_probs: probabilities used for motion vector decoding
315  * @mode_probs: probabilities used for intra-prediction mode decoding tree.
316  *
317  * Parser context that needs to be live across frames. For instance
318  * the probabilities tables stored in #GstVp8FrameHdr may depend on
319  * the previous frames.
320  */
321 struct _GstVp8Parser
322 {
323   GstVp8Segmentation segmentation;
324   GstVp8MbLfAdjustments mb_lf_adjust;
325   GstVp8TokenProbs token_probs;
326   GstVp8MvProbs mv_probs;
327   GstVp8ModeProbs mode_probs;
328 };
329 
330 GST_CODEC_PARSERS_API
331 void                gst_vp8_parser_init (GstVp8Parser * parser);
332 
333 GST_CODEC_PARSERS_API
334 GstVp8ParserResult  gst_vp8_parser_parse_frame_header (GstVp8Parser   * parser,
335                                                        GstVp8FrameHdr * frame_hdr,
336                                                        const guint8   * data,
337                                                        gsize            size);
338 
339 G_END_DECLS
340 
341 #endif /* GST_VP8_PARSER_H */
342