1 /*
2  * gstvp9parser.h
3  *
4  *  Copyright (C) 2013-2014 Intel Corporation
5  *  Copyright (C) 2015 Intel Corporation
6  *    Author: XuGuangxin<Guangxin.Xu@intel.com>
7  *    Author: Sreerenj Balachandran<sreerenj.balachandran@intel.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 #ifndef GST_VP9_PARSER_H
26 #define GST_VP9_PARSER_H
27 
28 #ifndef GST_USE_UNSTABLE_API
29 #warning "The VP9 parsing library is unstable API and may change in future."
30 #warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
31 #endif
32 
33 #include <gst/gst.h>
34 #include <gst/codecparsers/codecparsers-prelude.h>
35 
36 G_BEGIN_DECLS
37 
38 #define GST_VP9_FRAME_MARKER 0x02
39 #define GST_VP9_SYNC_CODE 0x498342
40 
41 #define GST_VP9_MAX_LOOP_FILTER    63
42 #define GST_VP9_MAX_PROB           255
43 
44 #define GST_VP9_REFS_PER_FRAME     3
45 #define GST_VP9_REF_FRAMES_LOG2    3
46 #define GST_VP9_REF_FRAMES         (1 << GST_VP9_REF_FRAMES_LOG2)
47 
48 #define GST_VP9_FRAME_CONTEXTS_LOG2 2
49 
50 #define GST_VP9_MAX_LOOP_FILTER    63
51 #define GST_VP9_MAX_SHARPNESS      7
52 
53 #define GST_VP9_MAX_REF_LF_DELTAS  4
54 #define GST_VP9_MAX_MODE_LF_DELTAS 2
55 
56 #define GST_VP9_SEGMENT_DELTADATA  0
57 #define GST_VP9_SEGMENT_ABSDATA    1
58 
59 #define GST_VP9_MAX_SEGMENTS       8
60 #define GST_VP9_SEG_TREE_PROBS     (GST_VP9_MAX_SEGMENTS-1)
61 
62 #define GST_VP9_PREDICTION_PROBS   3
63 
64 typedef struct _GstVp9Parser               GstVp9Parser;
65 typedef struct _GstVp9FrameHdr             GstVp9FrameHdr;
66 typedef struct _GstVp9LoopFilter           GstVp9LoopFilter;
67 typedef struct _GstVp9QuantIndices         GstVp9QuantIndices;
68 typedef struct _GstVp9Segmentation         GstVp9Segmentation;
69 typedef struct _GstVp9SegmentationInfo     GstVp9SegmentationInfo;
70 typedef struct _GstVp9SegmentationInfoData GstVp9SegmentationInfoData;
71 
72 /**
73  * GstVp9ParseResult:
74  * @GST_VP9_PARSER_OK: The parsing went well
75  * @GST_VP9_PARSER_BROKEN_DATA: The data to parse is broken
76  * @GST_VP9_PARSER_NO_PACKET_ERROR: An error occured during the parsing
77  *
78  * Result type of any parsing function.
79  *
80  * Since: 1.8
81  */
82 typedef enum
83 {
84   GST_VP9_PARSER_OK,
85   GST_VP9_PARSER_BROKEN_DATA,
86   GST_VP9_PARSER_ERROR,
87 } GstVp9ParserResult;
88 
89 /**
90  * GstVp9Profile: Bitstream profiles indicated by 2-3 bits in the uncompressed header
91  * @GST_VP9_PROFILE_0: Profile 0, 8-bit 4:2:0 only.
92  * @GST_VP9_PROFILE_1: Profile 1, 8-bit 4:4:4, 4:2:2, and 4:4:0.
93  * @GST_VP9_PROFILE_2: Profile 2, 10-bit and 12-bit color only, with 4:2:0 sampling.
94  * @GST_VP9_PROFILE_3: Profile 3, 10-bit and 12-bit color only, with 4:2:2/4:4:4/4:4:0 sampling.
95  * @GST_VP9_PROFILE_UNDEFINED: Undefined profile
96  *
97  * VP9 Profiles
98  *
99  * Since: 1.8
100  */
101 typedef enum {
102   GST_VP9_PROFILE_0,
103   GST_VP9_PROFILE_1,
104   GST_VP9_PROFILE_2,
105   GST_VP9_PROFILE_3,
106   GST_VP9_PROFILE_UNDEFINED
107 } GstVP9Profile;
108 
109 /**
110  * GstVp9FrameType:
111  * @GST_VP9_KEY_FRAME: Key frame, only have intra blocks
112  * @GST_VP9_INTER_FRAME: Inter frame, both intra and inter blocks
113  *
114  * VP9 frame types
115  *
116  * Since: 1.8
117  */
118 typedef enum {
119   GST_VP9_KEY_FRAME   = 0,
120   GST_VP9_INTER_FRAME = 1
121 } GstVp9FrameType;
122 
123 /**
124  * GstVp9BitDepth:
125  * @GST_VP9_BIT_DEPTH_8: Bit depth is 8
126  * @GST_VP9_BIT_DEPTH_10 Bit depth is 10
127  * @GST_VP9_BIT_DEPTH_12:Bit depth is 12
128  *
129  * Bit depths of encoded frames
130  *
131  * Since: 1.8
132  */
133 typedef enum {
134   GST_VP9_BIT_DEPTH_8  = 8,
135   GST_VP9_BIT_DEPTH_10 = 10,
136   GST_VP9_BIT_DEPTH_12 = 12
137 } GstVp9BitDepth;
138 
139 /**
140  * GstVp9ColorSpace:
141  * @GST_VP9_CS_UNKNOWN: Unknown color space
142  * @GST_VP9_CS_BT_601: BT.601
143  * @GST_VP9_CS_BT_709: BT.709
144  * @GST_VP9_CS_SMPTE_170: SMPTE.170
145  * @GST_VP9_CS_SMPTE_240: SMPTE.240
146  * @GST_VP9_CS_BT_2020: BT.2020
147  * @GST_VP9_CS_RESERVED: Reserved
148  * @GST_VP9_CS_SRGB: sRGB
149  *
150  * Supported ColorSpace standards
151  *
152  * Since: 1.8
153  */
154 typedef enum {
155   GST_VP9_CS_UNKNOWN               = 0,
156   GST_VP9_CS_BT_601                = 1,
157   GST_VP9_CS_BT_709                = 2,
158   GST_VP9_CS_SMPTE_170             = 3,
159   GST_VP9_CS_SMPTE_240             = 4,
160   GST_VP9_CS_BT_2020               = 5,
161   GST_VP9_CS_RESERVED_2            = 6,
162   GST_VP9_CS_SRGB                  = 7
163 } GstVp9ColorSpace;
164 
165 /**
166  * GstVp9ColorRange:
167  * @GST_VP9_CR_LIMITED: Y range is [16-235], UV range is [16-240]
168  * @GST_VP9_CR_FULL: Full range for Y,U and V [0-255]
169  *
170  * Possible color value ranges
171  *
172  * Since: 1.8
173  */
174 typedef enum {
175   GST_VP9_CR_LIMITED,
176   GST_VP9_CR_FULL
177 } GstVp9ColorRange;
178 
179 /**
180  * GstVp9InterpolationFilter:
181  * @GST_VP9_INTERPOLATION_FILTER_EIGHTTAP: EightTap interpolation filter
182  * @GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SMOOTH: Smooth interpolation filter
183  * @GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SHARP: Shart interpolation filter
184  * @GST_VP9_INTERPOLATION_FILTER_BILINEAR: Bilinear interpolation filter
185  * @GST_VP9_INTERPOLATION_FILTER_SWITCHABLE: Selectable interpolation filter
186  *
187  * Interpolation Filters Types
188  *
189  * Since: 1.8
190  */
191 typedef enum {
192   GST_VP9_INTERPOLATION_FILTER_EIGHTTAP        = 0,
193   GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SMOOTH = 1,
194   GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SHARP  = 2,
195   GST_VP9_INTERPOLATION_FILTER_BILINEAR        = 3,
196   GST_VP9_INTERPOLATION_FILTER_SWITCHABLE      = 4
197 } GstVp9InterpolationFilter;
198 
199 /**
200  * GstVp9RefFrameType:
201  * @GST_VP9_REF_FRAME_INTRA: Intra reference frame
202  * @GST_VP9_REF_FRAME_LAST: Last Reference frame
203  * @GST_VP9_REF_FRAME_GOLDEN: Golden Reference frame
204  * @GST_VP9_REF_FRAME_ALTREF: Alternate Reference frame
205  * @GST_VP9_REF_FRAME_MAX:
206  *
207  * Reference Frame types
208  *
209  * Since: 1.8
210  */
211 typedef enum {
212   GST_VP9_REF_FRAME_INTRA  = 0,
213   GST_VP9_REF_FRAME_LAST   = 1,
214   GST_VP9_REF_FRAME_GOLDEN = 2,
215   GST_VP9_REF_FRAME_ALTREF = 3,
216   GST_VP9_REF_FRAME_MAX    = 4
217 } GstVp9RefFrameType;
218 
219 /**
220  * GstVp9QuantIndices:
221  * @y_ac_qi: indicates the dequantization table index used for the
222  *   luma AC coefficients
223  * @y_dc_delta: indicates the delta value that is added to the
224  *   baseline index to obtain the luma DC coefficient dequantization
225  *   index
226  * @uv_dc_delta: indicates the delta value that is added to the
227  *   baseline index to obtain the chroma DC coefficient dequantization
228  *   index
229  * @uv_ac_delta: indicates the delta value that is added to the
230  *   baseline index to obtain the chroma AC coefficient dequantization
231  *   index
232  *
233  * Dequantization indices.
234  *
235  * Since: 1.8
236  */
237 struct _GstVp9QuantIndices
238 {
239   guint8 y_ac_qi;
240   gint8 y_dc_delta;
241   gint8 uv_dc_delta;
242   gint8 uv_ac_delta;
243 };
244 
245 /**
246  * GstVp9LoopFilter:
247  * @filter_level: indicates loop filter level for the current frame
248  * @sharpness_level: indicates sharpness level for thecurrent frame
249  * @mode_ref_delta_enabled: indicate if filter adjust is on
250  * @mode_ref_delta_update: indicates if the delta values used in an
251  *   adjustment are updated in the current frame
252  * @update_ref_deltas: indicate which ref deltas are updated
253  * @ref_deltas:  Loop filter strength adjustments based on
254  *  frame type (intra, inter)
255  * @update_mode_deltas: indicate with mode deltas are updated
256  * @mode_deltas: Loop filter strength adjustments based on
257  *   mode (zero, new mv)
258  *
259  * Loop filter values
260  *
261  * Since: 1.8
262  */
263 struct _GstVp9LoopFilter {
264   gint filter_level;
265   gint sharpness_level;
266 
267   guint8 mode_ref_delta_enabled;
268   guint8 mode_ref_delta_update;
269   guint8 update_ref_deltas[GST_VP9_MAX_REF_LF_DELTAS];
270   gint8 ref_deltas[GST_VP9_MAX_REF_LF_DELTAS];
271   guint8 update_mode_deltas[GST_VP9_MAX_MODE_LF_DELTAS];
272   gint8 mode_deltas[GST_VP9_MAX_MODE_LF_DELTAS];
273 };
274 
275 /**
276  * GstVp9SegmentationInfoData:
277  * @alternate_quantizer_enabled: indicate alternate quantizer enabled at segment level
278  * @alternate_quantizer: alternate quantizer value
279  * @alternate_loop_filter_enabled: indicate alternate loop filter enabled at segment level
280  * @alternate_loop_filter: alternate loop filter
281  * @reference_frame_enabled: indicate alternate reference frame at segment level
282  * @reference_frame: alternate reference frame
283  * @reference_skip: a block skip mode that implies both the use of a (0,0)
284  *   motion vector and that no residual will be coded.
285  *
286  * Segmentation info for each segment
287  *
288  * Since: 1.8
289  */
290 struct _GstVp9SegmentationInfoData {
291   /* SEG_LVL_ALT_Q */
292   guint8 alternate_quantizer_enabled;
293   gint16 alternate_quantizer;
294 
295   /* SEG_LVL_ALT_LF */
296   guint8 alternate_loop_filter_enabled;
297   gint8 alternate_loop_filter;
298 
299   /* SEG_LVL_REF_FRAME */
300   guint8 reference_frame_enabled;
301   gint reference_frame;
302 
303   guint8 reference_skip;
304 };
305 
306 /**
307  * GstVp9SegmentationInfo:
308  * @enabled: enables the segmentation feature for the current frame
309  * @update_map: determines if segmentation is updated in the current frame
310  * @update_tree_probs: determines if tree probabilities updated or not
311  * @tree_probs: segment tree probabilities
312  * @update_pred_probs:determines if prediction probabilities updated or not
313  * @pred_probs: prediction probabilities
314  * @abs_delta: interpretation of segment data values
315  * @temporal_update: type of map update
316  * @update_data: indicates if the segment feature data
317  *   is updated in the current frame
318  * @data: segment feature data
319  *
320  * Segmentation info
321  *
322  * Since: 1.8
323  */
324 struct _GstVp9SegmentationInfo {
325   /* enable in setup_segmentation*/
326   guint8  enabled;
327   /* update_map in setup_segmentation*/
328   guint8 update_map;
329   /* tree_probs exist or not*/
330   guint8 update_tree_probs[GST_VP9_SEG_TREE_PROBS];
331   guint8 tree_probs[GST_VP9_SEG_TREE_PROBS];
332   /* pred_probs exist or not*/
333   guint8 update_pred_probs[GST_VP9_PREDICTION_PROBS];
334   guint8 pred_probs[GST_VP9_PREDICTION_PROBS];
335 
336   /* abs_delta in setup_segmentation */
337   guint8 abs_delta;
338   /* temporal_update in setup_segmentation */
339   guint8 temporal_update;
340 
341   /* update_data in setup_segmentation*/
342   guint8 update_data;
343   GstVp9SegmentationInfoData data[GST_VP9_MAX_SEGMENTS];
344 };
345 
346 /**
347  * GstVp9FrameHdr:
348  * @profile: encoded profile
349  * @show_existing_frame: display already decoded frame instead of doing the decoding
350  * @frame_to_show: which frame to show if show_existing_frame is true
351  * @frame_type: frame type
352  * @show_frame: indicate whether it is a displayable frame or not
353  * @error_resilient_mode: error resilent mode
354  * @width: frame width
355  * @height: frame height
356  * @display_size_enabled: display size enabled (cropping)
357  * @display_width: display width
358  * @display_height: display height
359  * @frame_context_idx: frame context index
360  * @intra_only: intra only frame
361  * @reset_frame_context: reset frame context
362  * @refresh_frame_flags: refresh reference frame flags
363  * @ref_frame_indices: reference frame index
364  * @ref_frame_sign_bias: sign bias for selecting altref,last and golden frames
365  * @allow_high_precision_mv: allow hight precision motion vector
366  * @mcomp_filter_type: interpolation filter type
367  * @refresh_frame_context: refresh frame context indicator
368  * @frame_parallel_decoding_mode: enable or disable parallel decoding support.
369  * @loopfilter: loopfilter values
370  * @quant_indices: quantization indeces
371  * @segmentation: segmentation info
372  * @log2_tile_rows: tile row indicator
373  * @log2_tile_columns:  tile column indicator
374  * @first_partition_size: first partition size (after the uncompressed header)
375  * @lossless_flag: lossless mode decode
376  * @frame_header_length_in_bytes: length of uncompressed header
377  *
378  * Frame header
379  *
380  * Since: 1.8
381  */
382 struct _GstVp9FrameHdr
383 {
384   guint profile;
385   guint8 show_existing_frame;
386   gint  frame_to_show;
387   guint frame_type;
388   guint8 show_frame;
389   guint8 error_resilient_mode;
390   guint32 width;
391   guint32 height;
392   guint8 display_size_enabled;
393   guint32 display_width;
394   guint32 display_height;
395   guint frame_context_idx;
396 
397   guint8 intra_only;
398   gint reset_frame_context;
399   gint refresh_frame_flags;
400 
401   gint ref_frame_indices[GST_VP9_REFS_PER_FRAME];
402   gint ref_frame_sign_bias[GST_VP9_REFS_PER_FRAME];
403   gint allow_high_precision_mv;
404   guint8 mcomp_filter_type;
405 
406   gint refresh_frame_context;
407   /* frame_parallel_decoding_mode in vp9 code*/
408   gint frame_parallel_decoding_mode;
409 
410   GstVp9LoopFilter loopfilter;
411   GstVp9QuantIndices quant_indices;
412   GstVp9SegmentationInfo segmentation;
413 
414   gint log2_tile_rows;
415   gint log2_tile_columns;
416 
417   guint32 first_partition_size;
418 
419   /* calculated values */
420   guint lossless_flag;
421   guint32 frame_header_length_in_bytes;
422 };
423 
424 /**
425  * GstVp9Segmentation:
426  * @filter_level: loop filter level
427  * @luma_ac_quant_scale: AC quant scale for luma(Y) component
428  * @luma_dc_quant_scale: DC quant scale for luma(Y) component
429  * @chroma_ac_quant_scale AC quant scale for chroma(U/V) component
430  * @chroma_dc_quant_scale: DC quant scale for chroma (U/V) component
431  * @reference_frame_enabled: alternate reference frame enablement
432  * @reference_frame: alternate reference frame
433  * @reference_skip:  a block skip mode that implies both the use of a (0,0)
434  *   motion vector and that no residual will be coded
435  *
436  * Segmentation info kept across multipe frames
437  *
438  * Since: 1.8
439  */
440 struct _GstVp9Segmentation
441 {
442   guint8 filter_level[4][2];
443   gint16 luma_ac_quant_scale;
444   gint16 luma_dc_quant_scale;
445   gint16 chroma_ac_quant_scale;
446   gint16 chroma_dc_quant_scale;
447 
448   guint8 reference_frame_enabled;
449   gint reference_frame;
450 
451   guint8 reference_skip;
452 };
453 
454 /**
455  * GstVp9Parser:
456  * @priv: GstVp9ParserPrivate struct to keep track of state variables
457  * @subsampling_x: horizontal subsampling
458  * @subsampling_y: vertical subsampling
459  * @bit_depth: bit depth of the stream
460  * @color_space: color space standard
461  * @color_range: color range standard
462  * @mb_segment_tree_probs: decoding tree probabilities
463  * @segment_pred_probs: segment prediction probabiilties
464  * @segmentation: Segmentation info
465  *
466  * Parser context that needs to be live across frames
467  *
468  * Since: 1.8
469  */
470 struct _GstVp9Parser
471 {
472   /* private stuct for tracking state variables across frames */
473   void *priv;
474 
475   gint subsampling_x;
476   gint subsampling_y;
477   guint bit_depth;
478   guint color_space;
479   guint color_range;
480 
481   guint8 mb_segment_tree_probs[GST_VP9_SEG_TREE_PROBS];
482   guint8 segment_pred_probs[GST_VP9_PREDICTION_PROBS];
483   GstVp9Segmentation segmentation[GST_VP9_MAX_SEGMENTS];
484 };
485 
486 GST_CODEC_PARSERS_API
487 GstVp9Parser *     gst_vp9_parser_new (void);
488 
489 GST_CODEC_PARSERS_API
490 GstVp9ParserResult gst_vp9_parser_parse_frame_header (GstVp9Parser* parser, GstVp9FrameHdr * frame_hdr, const guint8 * data, gsize size);
491 
492 GST_CODEC_PARSERS_API
493 void               gst_vp9_parser_free (GstVp9Parser * parser);
494 
495 G_END_DECLS
496 
497 #endif /* GST_VP9_PARSER_H */
498