• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* gstvp9parser.c
2  *
3  *  Copyright (C) 2013-2014 Intel Corporation
4  *  Copyright (C) 2015 Intel Corporation
5  *    Author: XuGuangxin<Guangxin.Xu@intel.com>
6  *    Author: Sreerenj Balachandran<sreerenj.balachandran@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  * SECTION:gstvp9parser
25  * @title: GstVp9Parser
26  * @short_description: Convenience library for parsing vp9 video bitstream.
27  *
28  * For more details about the structures, you can refer to the
29  * specifications:
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <string.h>
37 #include <stdlib.h>
38 #include <gst/base/gstbitreader.h>
39 #include "vp9utils.h"
40 #include "gstvp9parser.h"
41 
42 #define MIN_TILE_WIDTH_B64 4
43 #define MAX_TILE_WIDTH_B64 64
44 
45 /* order of sb64, where sb64 = 64x64 */
46 #define ALIGN_SB64(w) ((w + 63) >> 6)
47 
48 GST_DEBUG_CATEGORY_STATIC (gst_vp9_parser_debug);
49 #define GST_CAT_DEFAULT gst_vp9_parser_debug
50 
51 static gboolean initialized = FALSE;
52 #define INITIALIZE_DEBUG_CATEGORY \
53   if (!initialized) { \
54     GST_DEBUG_CATEGORY_INIT (gst_vp9_parser_debug, "codecparsers_vp9", 0, \
55         "vp9 parser library"); \
56     initialized = TRUE; \
57   }
58 
59 #define gst_vp9_read_bit(br) gst_bit_reader_get_bits_uint8_unchecked(br, 1)
60 #define gst_vp9_read_bits(br, bits) gst_bit_reader_get_bits_uint32_unchecked(br, bits)
61 
62 #define GST_VP9_PARSER_GET_PRIVATE(parser)  ((GstVp9ParserPrivate *)(parser->priv))
63 
64 typedef struct _ReferenceSize
65 {
66   guint32 width;
67   guint32 height;
68 } ReferenceSize;
69 
70 typedef struct
71 {
72   /* for loop filters */
73   gint8 ref_deltas[GST_VP9_MAX_REF_LF_DELTAS];
74   gint8 mode_deltas[GST_VP9_MAX_MODE_LF_DELTAS];
75 
76   guint8 segmentation_abs_delta;
77   GstVp9SegmentationInfoData segmentation[GST_VP9_MAX_SEGMENTS];
78 
79   ReferenceSize reference[GST_VP9_REF_FRAMES];
80 } GstVp9ParserPrivate;
81 
82 static gint32
gst_vp9_read_signed_bits(GstBitReader * br,int bits)83 gst_vp9_read_signed_bits (GstBitReader * br, int bits)
84 {
85   const gint32 value = gst_vp9_read_bits (br, bits);
86   return gst_vp9_read_bit (br) ? -value : value;
87 }
88 
89 static gboolean
verify_frame_marker(GstBitReader * br)90 verify_frame_marker (GstBitReader * br)
91 {
92   guint8 frame_marker = gst_vp9_read_bits (br, 2);
93   if (frame_marker != GST_VP9_FRAME_MARKER) {
94     GST_ERROR ("Invalid VP9 Frame Marker !");
95     return FALSE;
96   }
97   return TRUE;
98 }
99 
100 static gboolean
verify_sync_code(GstBitReader * const br)101 verify_sync_code (GstBitReader * const br)
102 {
103   return (gst_vp9_read_bits (br, 24) == GST_VP9_SYNC_CODE);
104 }
105 
106 static gboolean
verify_superframe_marker(GstBitReader * br)107 verify_superframe_marker (GstBitReader * br)
108 {
109   return gst_vp9_read_bits (br, 3) == GST_VP9_SUPERFRAME_MARKER;
110 }
111 
112 static gboolean
parse_bitdepth_colorspace_sampling(GstVp9Parser * parser,GstBitReader * const br,GstVp9FrameHdr * frame_hdr)113 parse_bitdepth_colorspace_sampling (GstVp9Parser * parser,
114     GstBitReader * const br, GstVp9FrameHdr * frame_hdr)
115 {
116   if (frame_hdr->profile > GST_VP9_PROFILE_1)
117     parser->bit_depth =
118         gst_vp9_read_bit (br) ? GST_VP9_BIT_DEPTH_12 : GST_VP9_BIT_DEPTH_10;
119   else
120     parser->bit_depth = GST_VP9_BIT_DEPTH_8;
121 
122   parser->color_space = gst_vp9_read_bits (br, 3);
123   if (parser->color_space != GST_VP9_CS_SRGB) {
124     parser->color_range = gst_vp9_read_bit (br);
125 
126     if (frame_hdr->profile == GST_VP9_PROFILE_1
127         || frame_hdr->profile == GST_VP9_PROFILE_3) {
128 
129       parser->subsampling_x = gst_vp9_read_bit (br);
130       parser->subsampling_y = gst_vp9_read_bit (br);
131 
132       if (parser->subsampling_x == 1 && parser->subsampling_y == 1) {
133         GST_ERROR
134             ("4:2:0 subsampling is not supported in profile_1 or profile_3");
135         goto error;
136       }
137 
138       if (gst_vp9_read_bit (br)) {
139         GST_ERROR ("Reserved bit set!");
140         goto error;
141       }
142     } else {
143       parser->subsampling_y = parser->subsampling_x = 1;
144     }
145   } else {
146     parser->color_range = GST_VP9_CR_FULL;
147 
148     if (frame_hdr->profile == GST_VP9_PROFILE_1
149         || frame_hdr->profile == GST_VP9_PROFILE_3) {
150       if (gst_vp9_read_bit (br)) {
151         GST_ERROR ("Reserved bit set!");
152         goto error;
153       }
154     } else {
155       GST_ERROR
156           ("4:4:4 subsampling is not supported in profile_0 and profile_2");
157       goto error;
158     }
159   }
160   return TRUE;
161 
162 error:
163   return FALSE;
164 }
165 
166 static guint
parse_profile(GstBitReader * br)167 parse_profile (GstBitReader * br)
168 {
169   guint8 profile = gst_vp9_read_bit (br);
170   profile |= gst_vp9_read_bit (br) << 1;
171   if (profile > 2)
172     profile += gst_vp9_read_bit (br);
173   return profile;
174 }
175 
176 static void
parse_frame_size(GstBitReader * br,guint32 * width,guint32 * height)177 parse_frame_size (GstBitReader * br, guint32 * width, guint32 * height)
178 {
179   const guint32 w = gst_vp9_read_bits (br, 16) + 1;
180   const guint32 h = gst_vp9_read_bits (br, 16) + 1;
181   *width = w;
182   *height = h;
183 }
184 
185 static void
parse_display_frame_size(GstBitReader * br,GstVp9FrameHdr * frame_hdr)186 parse_display_frame_size (GstBitReader * br, GstVp9FrameHdr * frame_hdr)
187 {
188   frame_hdr->display_size_enabled = gst_vp9_read_bit (br);
189   if (frame_hdr->display_size_enabled)
190     parse_frame_size (br, &frame_hdr->display_width,
191         &frame_hdr->display_height);
192 }
193 
194 static void
parse_frame_size_from_refs(const GstVp9Parser * parser,GstVp9FrameHdr * frame_hdr,GstBitReader * br)195 parse_frame_size_from_refs (const GstVp9Parser * parser,
196     GstVp9FrameHdr * frame_hdr, GstBitReader * br)
197 {
198   gboolean found = FALSE;
199   int i;
200   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
201 
202   for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
203     found = gst_vp9_read_bit (br);
204 
205     if (found) {
206       guint8 idx = frame_hdr->ref_frame_indices[i];
207       frame_hdr->width = priv->reference[idx].width;
208       frame_hdr->height = priv->reference[idx].height;
209       break;
210     }
211   }
212   if (!found)
213     parse_frame_size (br, &frame_hdr->width, &frame_hdr->height);
214 }
215 
216 static GstVp9InterpolationFilter
parse_interp_filter(GstBitReader * br)217 parse_interp_filter (GstBitReader * br)
218 {
219   static const GstVp9InterpolationFilter filter_map[] = {
220     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SMOOTH,
221     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP,
222     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SHARP,
223     GST_VP9_INTERPOLATION_FILTER_BILINEAR
224   };
225 
226   return gst_vp9_read_bit (br) ? GST_VP9_INTERPOLATION_FILTER_SWITCHABLE :
227       filter_map[gst_vp9_read_bits (br, 2)];
228 }
229 
230 static void
parse_loopfilter(GstVp9LoopFilter * lf,GstBitReader * br)231 parse_loopfilter (GstVp9LoopFilter * lf, GstBitReader * br)
232 {
233   lf->filter_level = gst_vp9_read_bits (br, 6);
234   lf->sharpness_level = gst_vp9_read_bits (br, 3);
235 
236   lf->mode_ref_delta_update = 0;
237 
238   lf->mode_ref_delta_enabled = gst_vp9_read_bit (br);
239   if (lf->mode_ref_delta_enabled) {
240     lf->mode_ref_delta_update = gst_vp9_read_bit (br);
241     if (lf->mode_ref_delta_update) {
242       int i;
243       for (i = 0; i < GST_VP9_MAX_REF_LF_DELTAS; i++) {
244         lf->update_ref_deltas[i] = gst_vp9_read_bit (br);
245         if (lf->update_ref_deltas[i])
246           lf->ref_deltas[i] = gst_vp9_read_signed_bits (br, 6);
247       }
248 
249       for (i = 0; i < GST_VP9_MAX_MODE_LF_DELTAS; i++) {
250         lf->update_mode_deltas[i] = gst_vp9_read_bit (br);
251         if (lf->update_mode_deltas[i])
252           lf->mode_deltas[i] = gst_vp9_read_signed_bits (br, 6);
253       }
254     }
255   }
256 }
257 
258 static gint8
parse_delta_q(GstBitReader * br)259 parse_delta_q (GstBitReader * br)
260 {
261   return gst_vp9_read_bit (br) ? gst_vp9_read_signed_bits (br, 4) : 0;
262 }
263 
264 static void
parse_quantization(GstVp9QuantIndices * quant_indices,GstBitReader * br)265 parse_quantization (GstVp9QuantIndices * quant_indices, GstBitReader * br)
266 {
267   quant_indices->y_ac_qi = gst_vp9_read_bits (br, QINDEX_BITS);
268   quant_indices->y_dc_delta = parse_delta_q (br);
269   quant_indices->uv_dc_delta = parse_delta_q (br);
270   quant_indices->uv_ac_delta = parse_delta_q (br);
271 }
272 
273 static void
parse_segmentation(GstVp9SegmentationInfo * seg,GstBitReader * br)274 parse_segmentation (GstVp9SegmentationInfo * seg, GstBitReader * br)
275 {
276   int i;
277 
278   seg->update_map = FALSE;
279   seg->update_data = FALSE;
280 
281   seg->enabled = gst_vp9_read_bit (br);
282   if (!seg->enabled)
283     return;
284 
285   /* Segmentation map update */
286   seg->update_map = gst_vp9_read_bit (br);
287   if (seg->update_map) {
288     for (i = 0; i < GST_VP9_SEG_TREE_PROBS; i++) {
289       seg->update_tree_probs[i] = gst_vp9_read_bit (br);
290       seg->tree_probs[i] = seg->update_tree_probs[i] ?
291           gst_vp9_read_bits (br, 8) : GST_VP9_MAX_PROB;
292     }
293 
294     seg->temporal_update = gst_vp9_read_bit (br);
295     if (seg->temporal_update) {
296       for (i = 0; i < GST_VP9_PREDICTION_PROBS; i++) {
297         seg->update_pred_probs[i] = gst_vp9_read_bit (br);
298         seg->pred_probs[i] = seg->update_pred_probs[i] ?
299             gst_vp9_read_bits (br, 8) : GST_VP9_MAX_PROB;
300       }
301     } else {
302       for (i = 0; i < GST_VP9_PREDICTION_PROBS; i++)
303         seg->pred_probs[i] = GST_VP9_MAX_PROB;
304     }
305   }
306 
307   /* Segmentation data update */
308   seg->update_data = gst_vp9_read_bit (br);
309 
310   if (seg->update_data) {
311     /* clear all features */
312     memset (seg->data, 0, sizeof (seg->data));
313 
314     seg->abs_delta = gst_vp9_read_bit (br);
315 
316     for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
317       GstVp9SegmentationInfoData *seg_data = seg->data + i;
318       guint8 data;
319 
320       /* SEG_LVL_ALT_Q */
321       seg_data->alternate_quantizer_enabled = gst_vp9_read_bit (br);
322       if (seg_data->alternate_quantizer_enabled) {
323         data = gst_vp9_read_bits (br, 8);
324         seg_data->alternate_quantizer = gst_vp9_read_bit (br) ? -data : data;
325       }
326 
327       /* SEG_LVL_ALT_LF */
328       seg_data->alternate_loop_filter_enabled = gst_vp9_read_bit (br);
329       if (seg_data->alternate_loop_filter_enabled) {
330         data = gst_vp9_read_bits (br, 6);
331         seg_data->alternate_loop_filter = gst_vp9_read_bit (br) ? -data : data;
332       }
333 
334       /* SEG_LVL_REF_FRAME */
335       seg_data->reference_frame_enabled = gst_vp9_read_bit (br);
336       if (seg_data->reference_frame_enabled) {
337         seg_data->reference_frame = gst_vp9_read_bits (br, 2);
338       }
339 
340       seg_data->reference_skip = gst_vp9_read_bit (br);
341     }
342   }
343 }
344 
345 static guint32
get_max_lb_tile_cols(guint32 sb_cols)346 get_max_lb_tile_cols (guint32 sb_cols)
347 {
348   gint max_log2 = 1;
349   while ((sb_cols >> max_log2) >= MIN_TILE_WIDTH_B64)
350     ++max_log2;
351   return max_log2 - 1;
352 }
353 
354 static guint32
get_min_lb_tile_cols(guint32 sb_cols)355 get_min_lb_tile_cols (guint32 sb_cols)
356 {
357   gint min_log2 = 0;
358   while ((MAX_TILE_WIDTH_B64 << min_log2) < sb_cols)
359     ++min_log2;
360   return min_log2;
361 }
362 
363 static gboolean
parse_tile_info(GstVp9FrameHdr * frame_hdr,GstBitReader * br)364 parse_tile_info (GstVp9FrameHdr * frame_hdr, GstBitReader * br)
365 {
366   guint32 max_ones;
367   const guint32 sb_cols = ALIGN_SB64 (frame_hdr->width);
368   guint32 min_lb_tile_cols = get_min_lb_tile_cols (sb_cols);
369   guint32 max_lb_tile_cols = get_max_lb_tile_cols (sb_cols);
370 
371   g_assert (min_lb_tile_cols <= max_lb_tile_cols);
372   max_ones = max_lb_tile_cols - min_lb_tile_cols;
373 
374   /* columns */
375   frame_hdr->log2_tile_columns = min_lb_tile_cols;
376   while (max_ones-- && gst_vp9_read_bit (br))
377     frame_hdr->log2_tile_columns++;
378 
379   if (frame_hdr->log2_tile_columns > 6) {
380     GST_ERROR ("Invalid number of tile columns..!");
381     return FALSE;
382   }
383 
384   /* row */
385   frame_hdr->log2_tile_rows = gst_vp9_read_bit (br);
386   if (frame_hdr->log2_tile_rows)
387     frame_hdr->log2_tile_rows += gst_vp9_read_bit (br);
388 
389   return TRUE;
390 }
391 
392 static void
loop_filter_update(GstVp9Parser * parser,const GstVp9LoopFilter * lf)393 loop_filter_update (GstVp9Parser * parser, const GstVp9LoopFilter * lf)
394 {
395   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
396   int i;
397 
398   for (i = 0; i < GST_VP9_MAX_REF_LF_DELTAS; i++) {
399     if (lf->update_ref_deltas[i])
400       priv->ref_deltas[i] = lf->ref_deltas[i];
401   }
402 
403   for (i = 0; i < GST_VP9_MAX_MODE_LF_DELTAS; i++) {
404     if (lf->update_mode_deltas[i])
405       priv->mode_deltas[i] = lf->mode_deltas[i];
406   }
407 }
408 
409 static guint8
seg_get_base_qindex(const GstVp9Parser * parser,const GstVp9FrameHdr * frame_hdr,int segid)410 seg_get_base_qindex (const GstVp9Parser * parser,
411     const GstVp9FrameHdr * frame_hdr, int segid)
412 {
413   int seg_base = frame_hdr->quant_indices.y_ac_qi;
414   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
415   const GstVp9SegmentationInfoData *seg = priv->segmentation + segid;
416   /* DEBUG("id = %d, seg_base = %d, seg enable = %d, alt enable = %d, abs = %d, alt= %d\n",segid,
417      seg_base, frame_hdr->segmentation.enabled, seg->alternate_quantizer_enabled, priv->segmentation_abs_delta,  seg->alternate_quantizer);
418    */
419   if (frame_hdr->segmentation.enabled && seg->alternate_quantizer_enabled) {
420     if (priv->segmentation_abs_delta)
421       seg_base = seg->alternate_quantizer;
422     else
423       seg_base += seg->alternate_quantizer;
424   }
425   return CLAMP (seg_base, 0, MAXQ);
426 }
427 
428 static guint8
seg_get_filter_level(const GstVp9Parser * parser,const GstVp9FrameHdr * frame_hdr,int segid)429 seg_get_filter_level (const GstVp9Parser * parser,
430     const GstVp9FrameHdr * frame_hdr, int segid)
431 {
432   int seg_filter = frame_hdr->loopfilter.filter_level;
433   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
434   const GstVp9SegmentationInfoData *seg = priv->segmentation + segid;
435 
436   if (frame_hdr->segmentation.enabled && seg->alternate_loop_filter_enabled) {
437     if (priv->segmentation_abs_delta)
438       seg_filter = seg->alternate_loop_filter;
439     else
440       seg_filter += seg->alternate_loop_filter;
441   }
442   return CLAMP (seg_filter, 0, GST_VP9_MAX_LOOP_FILTER);
443 }
444 
445 /*save segmentation info from frame header to parser*/
446 static void
segmentation_save(GstVp9Parser * parser,const GstVp9FrameHdr * frame_hdr)447 segmentation_save (GstVp9Parser * parser, const GstVp9FrameHdr * frame_hdr)
448 {
449   const GstVp9SegmentationInfo *info = &frame_hdr->segmentation;
450   if (!info->enabled)
451     return;
452 
453   if (info->update_map) {
454     g_assert (G_N_ELEMENTS (parser->mb_segment_tree_probs) ==
455         G_N_ELEMENTS (info->tree_probs));
456     g_assert (G_N_ELEMENTS (parser->segment_pred_probs) ==
457         G_N_ELEMENTS (info->pred_probs));
458     memcpy (parser->mb_segment_tree_probs, info->tree_probs,
459         sizeof (info->tree_probs));
460     memcpy (parser->segment_pred_probs, info->pred_probs,
461         sizeof (info->pred_probs));
462   }
463 
464   if (info->update_data) {
465     GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
466     priv->segmentation_abs_delta = info->abs_delta;
467     g_assert (G_N_ELEMENTS (priv->segmentation) == G_N_ELEMENTS (info->data));
468     memcpy (priv->segmentation, info->data, sizeof (info->data));
469   }
470 }
471 
472 static void
segmentation_update(GstVp9Parser * parser,const GstVp9FrameHdr * frame_hdr)473 segmentation_update (GstVp9Parser * parser, const GstVp9FrameHdr * frame_hdr)
474 {
475   int i = 0;
476   const GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
477   const GstVp9LoopFilter *lf = &frame_hdr->loopfilter;
478   const GstVp9QuantIndices *quant_indices = &frame_hdr->quant_indices;
479   int default_filter = lf->filter_level;
480   const int scale = 1 << (default_filter >> 5);
481 
482   segmentation_save (parser, frame_hdr);
483 
484   for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
485     guint8 q = seg_get_base_qindex (parser, frame_hdr, i);
486 
487     GstVp9Segmentation *seg = parser->segmentation + i;
488     const GstVp9SegmentationInfoData *info = priv->segmentation + i;
489 
490     seg->luma_dc_quant_scale =
491         gst_vp9_dc_quant (q, quant_indices->y_dc_delta, parser->bit_depth);
492     seg->luma_ac_quant_scale = gst_vp9_ac_quant (q, 0, parser->bit_depth);
493     seg->chroma_dc_quant_scale =
494         gst_vp9_dc_quant (q, quant_indices->uv_dc_delta, parser->bit_depth);
495     seg->chroma_ac_quant_scale =
496         gst_vp9_ac_quant (q, quant_indices->uv_ac_delta, parser->bit_depth);
497 
498     if (lf->filter_level) {
499       guint8 filter = seg_get_filter_level (parser, frame_hdr, i);
500 
501       if (!lf->mode_ref_delta_enabled) {
502         memset (seg->filter_level, filter, sizeof (seg->filter_level));
503       } else {
504         int ref, mode;
505         const int intra_filter =
506             filter + priv->ref_deltas[GST_VP9_REF_FRAME_INTRA] * scale;
507         seg->filter_level[GST_VP9_REF_FRAME_INTRA][0] =
508             CLAMP (intra_filter, 0, GST_VP9_MAX_LOOP_FILTER);
509         for (ref = GST_VP9_REF_FRAME_LAST; ref < GST_VP9_REF_FRAME_MAX; ++ref) {
510           for (mode = 0; mode < GST_VP9_MAX_MODE_LF_DELTAS; ++mode) {
511             const int inter_filter = filter + priv->ref_deltas[ref] * scale
512                 + priv->mode_deltas[mode] * scale;
513             seg->filter_level[ref][mode] =
514                 CLAMP (inter_filter, 0, GST_VP9_MAX_LOOP_FILTER);
515           }
516         }
517       }
518     }
519     seg->reference_frame_enabled = info->reference_frame_enabled;;
520     seg->reference_frame = info->reference_frame;
521     seg->reference_skip = info->reference_skip;
522   }
523 }
524 
525 static void
reference_update(GstVp9Parser * parser,const GstVp9FrameHdr * const frame_hdr)526 reference_update (GstVp9Parser * parser, const GstVp9FrameHdr * const frame_hdr)
527 {
528   guint8 flag = 1;
529   guint8 refresh_frame_flags;
530   int i;
531   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
532   ReferenceSize *reference = priv->reference;
533   if (frame_hdr->frame_type == GST_VP9_KEY_FRAME) {
534     refresh_frame_flags = 0xff;
535   } else {
536     refresh_frame_flags = frame_hdr->refresh_frame_flags;
537   }
538   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
539     if (refresh_frame_flags & flag) {
540       reference[i].width = frame_hdr->width;
541       reference[i].height = frame_hdr->height;
542     }
543     flag <<= 1;
544   }
545 }
546 
547 static inline int
frame_is_intra_only(const GstVp9FrameHdr * frame_hdr)548 frame_is_intra_only (const GstVp9FrameHdr * frame_hdr)
549 {
550   return frame_hdr->frame_type == GST_VP9_KEY_FRAME || frame_hdr->intra_only;
551 }
552 
553 static void
set_default_lf_deltas(GstVp9Parser * parser)554 set_default_lf_deltas (GstVp9Parser * parser)
555 {
556   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
557   priv->ref_deltas[GST_VP9_REF_FRAME_INTRA] = 1;
558   priv->ref_deltas[GST_VP9_REF_FRAME_LAST] = 0;
559   priv->ref_deltas[GST_VP9_REF_FRAME_GOLDEN] = -1;
560   priv->ref_deltas[GST_VP9_REF_FRAME_ALTREF] = -1;
561 
562   priv->mode_deltas[0] = 0;
563   priv->mode_deltas[1] = 0;
564 }
565 
566 static void
set_default_segmentation_info(GstVp9Parser * parser)567 set_default_segmentation_info (GstVp9Parser * parser)
568 {
569   GstVp9ParserPrivate *priv = GST_VP9_PARSER_GET_PRIVATE (parser);
570 
571   memset (priv->segmentation, 0, sizeof (priv->segmentation));
572 
573   priv->segmentation_abs_delta = FALSE;
574 }
575 
576 static void
setup_past_independence(GstVp9Parser * parser,GstVp9FrameHdr * const frame_hdr)577 setup_past_independence (GstVp9Parser * parser,
578     GstVp9FrameHdr * const frame_hdr)
579 {
580   set_default_lf_deltas (parser);
581   set_default_segmentation_info (parser);
582 
583   memset (frame_hdr->ref_frame_sign_bias, 0,
584       sizeof (frame_hdr->ref_frame_sign_bias));
585 }
586 
587 static void
gst_vp9_parser_reset(GstVp9Parser * parser)588 gst_vp9_parser_reset (GstVp9Parser * parser)
589 {
590   GstVp9ParserPrivate *priv = parser->priv;
591 
592   parser->priv = NULL;
593   memset (parser->mb_segment_tree_probs, 0,
594       sizeof (parser->mb_segment_tree_probs));
595   memset (parser->segment_pred_probs, 0, sizeof (parser->segment_pred_probs));
596   memset (parser->segmentation, 0, sizeof (parser->segmentation));
597 
598   memset (priv, 0, sizeof (GstVp9ParserPrivate));
599   parser->priv = priv;
600 }
601 
602 static GstVp9ParserResult
gst_vp9_parser_update(GstVp9Parser * parser,GstVp9FrameHdr * const frame_hdr)603 gst_vp9_parser_update (GstVp9Parser * parser, GstVp9FrameHdr * const frame_hdr)
604 {
605   if (frame_hdr->frame_type == GST_VP9_KEY_FRAME)
606     gst_vp9_parser_reset (parser);
607 
608   if (frame_is_intra_only (frame_hdr) || frame_hdr->error_resilient_mode)
609     setup_past_independence (parser, frame_hdr);
610 
611   loop_filter_update (parser, &frame_hdr->loopfilter);
612   segmentation_update (parser, frame_hdr);
613   reference_update (parser, frame_hdr);
614 
615   return GST_VP9_PARSER_OK;
616 }
617 
618 
619 /******** API *************/
620 
621 /**
622  * gst_vp9_parser_new:
623  *
624  * Creates a new #GstVp9Parser. It should be freed with
625  * gst_vp9_parser_free() after use.
626  *
627  * Returns: a new #GstVp9Parser
628  *
629  * Since: 1.8
630  */
631 GstVp9Parser *
gst_vp9_parser_new(void)632 gst_vp9_parser_new (void)
633 {
634   GstVp9Parser *parser;
635   GstVp9ParserPrivate *priv;
636 
637   INITIALIZE_DEBUG_CATEGORY;
638   GST_DEBUG ("Create VP9 Parser");
639 
640   parser = g_slice_new0 (GstVp9Parser);
641   if (!parser)
642     return NULL;
643 
644   priv = g_slice_new0 (GstVp9ParserPrivate);
645   if (!priv)
646     return NULL;
647 
648   parser->priv = priv;
649 
650   return parser;
651 }
652 
653 /**
654  * gst_vp9_parser_free:
655  * @parser: the #GstVp9Parser to free
656  *
657  * Frees @parser.
658  *
659  * Since: 1.8
660  */
661 void
gst_vp9_parser_free(GstVp9Parser * parser)662 gst_vp9_parser_free (GstVp9Parser * parser)
663 {
664   if (parser) {
665     if (parser->priv) {
666       g_slice_free (GstVp9ParserPrivate, parser->priv);
667       parser->priv = NULL;
668     }
669     g_slice_free (GstVp9Parser, parser);
670   }
671 }
672 
673 /**
674  * gst_vp9_parser_parse_frame_header:
675  * @parser: The #GstVp9Parser
676  * @frame_hdr: The #GstVp9FrameHdr to fill
677  * @data: The data to parse
678  * @size: The size of the @data to parse
679  *
680  * Parses the VP9 bitstream contained in @data, and fills in @frame_hdr
681  * with the information. The @size argument represent the whole frame size.
682  *
683  * Returns: a #GstVp9ParserResult
684  *
685  * Since: 1.8
686  */
687 GstVp9ParserResult
gst_vp9_parser_parse_frame_header(GstVp9Parser * parser,GstVp9FrameHdr * frame_hdr,const guint8 * data,gsize size)688 gst_vp9_parser_parse_frame_header (GstVp9Parser * parser,
689     GstVp9FrameHdr * frame_hdr, const guint8 * data, gsize size)
690 {
691   GstBitReader bit_reader;
692   GstBitReader *br = &bit_reader;
693 
694   gst_bit_reader_init (br, data, size);
695   memset (frame_hdr, 0, sizeof (*frame_hdr));
696 
697   /* Parsing Uncompressed Data Chunk */
698 
699   if (!verify_frame_marker (br))
700     goto error;
701 
702   frame_hdr->profile = parse_profile (br);
703   if (frame_hdr->profile > GST_VP9_PROFILE_UNDEFINED) {
704     GST_ERROR ("Stream has undefined VP9  profile !");
705     goto error;
706   }
707 
708   frame_hdr->show_existing_frame = gst_vp9_read_bit (br);
709   if (frame_hdr->show_existing_frame) {
710     frame_hdr->frame_to_show = gst_vp9_read_bits (br, GST_VP9_REF_FRAMES_LOG2);
711     return GST_VP9_PARSER_OK;
712   }
713 
714   frame_hdr->frame_type = gst_vp9_read_bit (br);
715   frame_hdr->show_frame = gst_vp9_read_bit (br);
716   frame_hdr->error_resilient_mode = gst_vp9_read_bit (br);
717 
718   if (frame_hdr->frame_type == GST_VP9_KEY_FRAME) {
719 
720     if (!verify_sync_code (br)) {
721       GST_ERROR ("Invalid VP9 Key-frame sync code !");
722       goto error;
723     }
724 
725     if (!parse_bitdepth_colorspace_sampling (parser, br, frame_hdr)) {
726       GST_ERROR ("Failed to parse color_space/bit_depth info !");
727       goto error;
728     }
729 
730     parse_frame_size (br, &frame_hdr->width, &frame_hdr->height);
731 
732     parse_display_frame_size (br, frame_hdr);
733 
734   } else {
735     frame_hdr->intra_only = frame_hdr->show_frame ? 0 : gst_vp9_read_bit (br);
736     frame_hdr->reset_frame_context = frame_hdr->error_resilient_mode ?
737         0 : gst_vp9_read_bits (br, 2);
738 
739     if (frame_hdr->intra_only) {
740 
741       if (!verify_sync_code (br)) {
742         GST_ERROR ("Invalid VP9 sync code in intra-only frame !");
743         goto error;
744       }
745 
746       if (frame_hdr->profile > GST_VP9_PROFILE_0) {
747         if (!parse_bitdepth_colorspace_sampling (parser, br, frame_hdr)) {
748           GST_ERROR ("Failed to parse color_space/bit_depth info !");
749           goto error;
750         }
751       } else {
752         parser->color_space = GST_VP9_CS_BT_601;
753         parser->color_range = GST_VP9_CR_LIMITED;
754         parser->subsampling_y = parser->subsampling_x = 1;
755         parser->bit_depth = GST_VP9_BIT_DEPTH_8;
756       }
757 
758       frame_hdr->refresh_frame_flags =
759           gst_vp9_read_bits (br, GST_VP9_REF_FRAMES);
760       parse_frame_size (br, &frame_hdr->width, &frame_hdr->height);
761       parse_display_frame_size (br, frame_hdr);
762 
763     } else {
764       int i;
765       frame_hdr->refresh_frame_flags =
766           gst_vp9_read_bits (br, GST_VP9_REF_FRAMES);
767 
768       for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
769         frame_hdr->ref_frame_indices[i] =
770             gst_vp9_read_bits (br, GST_VP9_REF_FRAMES_LOG2);
771         frame_hdr->ref_frame_sign_bias[i] = gst_vp9_read_bit (br);
772       }
773 
774       parse_frame_size_from_refs (parser, frame_hdr, br);
775       parse_display_frame_size (br, frame_hdr);
776 
777       frame_hdr->allow_high_precision_mv = gst_vp9_read_bit (br);
778       frame_hdr->mcomp_filter_type = parse_interp_filter (br);
779     }
780   }
781 
782   frame_hdr->refresh_frame_context =
783       frame_hdr->error_resilient_mode ? 0 : gst_vp9_read_bit (br);
784   frame_hdr->frame_parallel_decoding_mode =
785       frame_hdr->error_resilient_mode ? 1 : gst_vp9_read_bit (br);
786   frame_hdr->frame_context_idx =
787       gst_vp9_read_bits (br, GST_VP9_FRAME_CONTEXTS_LOG2);
788 
789   /* loopfilter header  */
790   parse_loopfilter (&frame_hdr->loopfilter, br);
791 
792   /* quantization header */
793   parse_quantization (&frame_hdr->quant_indices, br);
794   /* set lossless_flag */
795   frame_hdr->lossless_flag = frame_hdr->quant_indices.y_ac_qi == 0 &&
796       frame_hdr->quant_indices.y_dc_delta == 0 &&
797       frame_hdr->quant_indices.uv_dc_delta == 0
798       && frame_hdr->quant_indices.uv_ac_delta == 0;
799 
800   /* segmentation header */
801   parse_segmentation (&frame_hdr->segmentation, br);
802 
803   /* tile header */
804   if (!parse_tile_info (frame_hdr, br)) {
805     GST_ERROR ("Failed to parse tile info...!");
806     goto error;
807   }
808 
809   /* size of the rest of the header */
810   frame_hdr->first_partition_size = gst_vp9_read_bits (br, 16);
811   if (!frame_hdr->first_partition_size) {
812     GST_ERROR ("Failed to parse the first partition size...!");
813     goto error;
814   }
815 
816   frame_hdr->frame_header_length_in_bytes =
817       (gst_bit_reader_get_pos (br) + 7) / 8;
818   return gst_vp9_parser_update (parser, frame_hdr);
819 
820 error:
821   return GST_VP9_PARSER_ERROR;
822 }
823 
824 /**
825  * gst_vp9_parser_parse_superframe_info:
826  * @parser: The #GstVp9Parser
827  * @superframe_info: The #GstVp9SuperframeInfo to fill
828  * @data: The data to parse
829  * @size: The size of the @data to parse
830  *
831  * Parses the VP9 bitstream contained in @data, and fills in @superframe_info
832  * with the information. The @size argument represent the whole superframe size.
833  * If @data is not superframe but normal frame, the parser returns
834  * GST_VP9_PARSER_OK, frame_size[0] is set to @size and frames_in_superframe is
835  * set to 1. Also this method does not validate vp9frame header and verifying
836  * the frame header is caller's responsibility.
837  *
838  * Returns: a #GstVp9ParserResult
839  *
840  * Since: 1.18
841  */
842 GstVp9ParserResult
gst_vp9_parser_parse_superframe_info(GstVp9Parser * parser,GstVp9SuperframeInfo * superframe_info,const guint8 * data,gsize size)843 gst_vp9_parser_parse_superframe_info (GstVp9Parser * parser,
844     GstVp9SuperframeInfo * superframe_info, const guint8 * data, gsize size)
845 {
846   GstBitReader header_bit_reader, index_bit_reader;
847   GstBitReader *hbr = &header_bit_reader, *ibr = &index_bit_reader;
848   guint i, j;
849 
850   g_return_val_if_fail (parser != NULL, GST_VP9_PARSER_ERROR);
851   g_return_val_if_fail (superframe_info != NULL, GST_VP9_PARSER_ERROR);
852   g_return_val_if_fail (data != NULL, GST_VP9_PARSER_ERROR);
853   g_return_val_if_fail (size > 0, GST_VP9_PARSER_ERROR);
854 
855   gst_bit_reader_init (hbr, data + size - 1, 1);
856   memset (superframe_info, 0, sizeof (GstVp9SuperframeInfo));
857 
858   /* Parsing Superframe Data Chunk */
859 
860   if (!verify_superframe_marker (hbr)) {
861     superframe_info->frame_sizes[0] = size;
862     superframe_info->frames_in_superframe = 1;
863     return GST_VP9_PARSER_OK;
864   }
865 
866   GST_DEBUG ("Parsing VP9 superframe, size %" G_GSIZE_FORMAT, size);
867 
868   superframe_info->bytes_per_framesize = gst_vp9_read_bits (hbr, 2) + 1;
869   superframe_info->frames_in_superframe = gst_vp9_read_bits (hbr, 3) + 1;
870 
871   if (superframe_info->frames_in_superframe > GST_VP9_MAX_FRAMES_IN_SUPERFRAME)
872     goto error;
873 
874   superframe_info->superframe_index_size =
875       2 +
876       superframe_info->frames_in_superframe *
877       superframe_info->bytes_per_framesize;
878 
879   gst_bit_reader_init (ibr,
880       data + size - superframe_info->superframe_index_size,
881       superframe_info->superframe_index_size);
882 
883   /* Checking that the first byte of the superframe_index matches the final byte */
884   if (gst_vp9_read_bits (ibr, 8) != data[size - 1])
885     goto error;
886 
887   for (i = 0; i < superframe_info->frames_in_superframe; i++) {
888     for (j = 0; j < superframe_info->bytes_per_framesize; j++)
889       superframe_info->frame_sizes[i] |= gst_vp9_read_bits (ibr, 8) << (j * 8);
890   }
891 
892   return GST_VP9_PARSER_OK;
893 
894 error:
895   GST_ERROR ("Failed to parse superframe");
896   return GST_VP9_PARSER_ERROR;
897 }
898