• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /*
21  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are
25  * met:
26  *
27  *   * Redistributions of source code must retain the above copyright
28  *     notice, this list of conditions and the following disclaimer.
29  *
30  *   * Redistributions in binary form must reproduce the above copyright
31  *     notice, this list of conditions and the following disclaimer in
32  *     the documentation and/or other materials provided with the
33  *     distribution.
34  *
35  *   * Neither the name of Google, nor the WebM Project, nor the names
36  *     of its contributors may be used to endorse or promote products
37  *     derived from this software without specific prior written
38  *     permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
51  */
52 
53 /**
54  * SECTION:gstvp9statefulparser
55  * @title: GstVp9StatefulParser
56  * @short_description: Convenience library for parsing vp9 video bitstream.
57  *
58  * This object is used to parse VP9 bitstream header.
59  *
60  * Since: 1.20
61  *
62  */
63 
64 #ifdef HAVE_CONFIG_H
65 #include "config.h"
66 #endif
67 
68 #include <gst/base/gstbitreader.h>
69 #include "gstvp9statefulparser.h"
70 #include <string.h>
71 
72 #ifndef GST_DISABLE_GST_DEBUG
73 #define GST_CAT_DEFAULT ensure_debug_category()
74 static GstDebugCategory *
ensure_debug_category(void)75 ensure_debug_category (void)
76 {
77   static gsize cat_gonce = 0;
78 
79   if (g_once_init_enter (&cat_gonce)) {
80     gsize cat_done;
81 
82     cat_done = (gsize) _gst_debug_category_new ("codecparsers_vp9stateful", 0,
83         "VP9 parser library");
84 
85     g_once_init_leave (&cat_gonce, cat_done);
86   }
87 
88   return (GstDebugCategory *) cat_gonce;
89 }
90 #else
91 #define ensure_debug_category()
92 #endif /* GST_DISABLE_GST_DEBUG */
93 
94 #define VP9_READ_UINT8(val,nbits) G_STMT_START { \
95   if (!gst_bit_reader_get_bits_uint8 (br, &val, nbits)) { \
96     GST_ERROR ("failed to read uint8 for '" G_STRINGIFY (val) "', nbits: %d", nbits); \
97     return GST_VP9_PARSER_BROKEN_DATA; \
98   } \
99 } G_STMT_END
100 
101 #define VP9_READ_UINT16(val,nbits) G_STMT_START { \
102   if (!gst_bit_reader_get_bits_uint16 (br, &val, nbits)) { \
103     GST_ERROR ("failed to read uint16 for '" G_STRINGIFY (val) "', nbits: %d", nbits); \
104     return GST_VP9_PARSER_BROKEN_DATA; \
105   } \
106 } G_STMT_END
107 
108 #define VP9_READ_UINT32(val,nbits) G_STMT_START { \
109   if (!gst_bit_reader_get_bits_uint32 (br, &val, nbits)) { \
110     GST_ERROR ("failed to read uint32 for '" G_STRINGIFY (val) "', nbits: %d", nbits); \
111     return GST_VP9_PARSER_BROKEN_DATA; \
112   } \
113 } G_STMT_END
114 
115 #define VP9_READ_BIT(val) VP9_READ_UINT8(val, 1)
116 
117 #define VP9_READ_SIGNED_8(val,nbits) G_STMT_START { \
118   guint8 _value; \
119   guint8 _negative; \
120   VP9_READ_UINT8(_value, nbits); \
121   VP9_READ_BIT(_negative); \
122   if (_negative) { \
123     val = (gint8) _value * -1; \
124   } else { \
125     val = _value; \
126   } \
127 } G_STMT_END
128 
129 #define VP9_READ_SIGNED_16(val,nbits) G_STMT_START { \
130   guint16 _value; \
131   guint8 _negative; \
132   VP9_READ_UINT16(_value, nbits); \
133   VP9_READ_BIT(_negative); \
134   if (_negative) { \
135     val = (gint16) _value * -1; \
136   } else { \
137     val = _value; \
138   } \
139 } G_STMT_END
140 
141 #define CHECK_ALLOWED_WITH_DEBUG(dbg, val, min, max) { \
142   if (val < min || val > max) { \
143     GST_WARNING ("value for '" dbg "' not in allowed range. value: %d, range %d-%d", \
144                      val, min, max); \
145     return GST_VP9_PARSER_ERROR; \
146   } \
147 }
148 
149 #define CHECK_ALLOWED(val, min, max) \
150   CHECK_ALLOWED_WITH_DEBUG (G_STRINGIFY (val), val, min, max)
151 
152 typedef struct _Vp9BoolDecoder
153 {
154   guint64 value;
155   guint32 range;
156   guint32 bits_left;
157   gint count_to_fill;
158   GstBitReader *bit_reader;
159   gboolean out_of_bits;
160 } Vp9BoolDecoder;
161 
162 /* how much to shift to get range > 128 */
163 const static guint8 bool_shift_table[256] = {
164   0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
165   3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
166   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
167   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
168   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
169   1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175 };
176 
177 static const guint8 inv_map_table[255] = {
178   7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
179   189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
180   10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
181   25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
182   40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
183   55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
184   70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
185   86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
186   101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
187   116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
188   131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
189   146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
190   161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
191   177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
192   192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
193   207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
194   222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
195   237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
196   252, 253, 253,
197 };
198 
199 static void
fill_bool(Vp9BoolDecoder * bd)200 fill_bool (Vp9BoolDecoder * bd)
201 {
202   guint max_bits_to_read;
203   guint bits_to_read;
204   guint64 data;
205 
206   if (G_UNLIKELY (bd->bits_left < bd->count_to_fill)) {
207     GST_ERROR
208         ("Invalid VP9 bitstream: the boolean decoder ran out of bits to read");
209     bd->out_of_bits = TRUE;
210     return;
211   }
212 
213   max_bits_to_read =
214       8 * (sizeof (bd->value) - sizeof (guint8)) + bd->count_to_fill;
215   bits_to_read = MIN (max_bits_to_read, bd->bits_left);
216 
217   data =
218       gst_bit_reader_get_bits_uint64_unchecked (bd->bit_reader, bits_to_read);
219 
220   bd->value |= data << (max_bits_to_read - bits_to_read);
221   bd->count_to_fill -= bits_to_read;
222   bd->bits_left -= bits_to_read;
223 }
224 
225 static gboolean
read_bool(Vp9BoolDecoder * bd,guint8 probability)226 read_bool (Vp9BoolDecoder * bd, guint8 probability)
227 {
228   guint64 split;
229   guint64 big_split;
230   guint count;
231   gboolean bit;
232 
233   if (bd->count_to_fill > 0)
234     fill_bool (bd);
235 
236   split = 1 + (((bd->range - 1) * probability) >> 8);
237   big_split = split << 8 * (sizeof (bd->value) - sizeof (guint8));
238 
239   if (bd->value < big_split) {
240     bd->range = split;
241     bit = FALSE;
242   } else {
243     bd->range -= split;
244     bd->value -= big_split;
245     bit = TRUE;
246   }
247 
248   count = bool_shift_table[bd->range];
249   bd->range <<= count;
250   bd->value <<= count;
251   bd->count_to_fill += count;
252 
253   return bit;
254 }
255 
256 static guint
read_literal(Vp9BoolDecoder * bd,guint n)257 read_literal (Vp9BoolDecoder * bd, guint n)
258 {
259   guint ret = 0;
260   guint i;
261 
262   for (i = 0; G_UNLIKELY (!bd->out_of_bits) && i < n; i++) {
263     ret = 2 * ret + read_bool (bd, 128);
264   }
265 
266   return ret;
267 }
268 
269 static GstVp9ParserResult
init_bool(Vp9BoolDecoder * bd,GstBitReader * br,guint size_in_bytes)270 init_bool (Vp9BoolDecoder * bd, GstBitReader * br, guint size_in_bytes)
271 {
272   gboolean marker_bit;
273 
274   if (size_in_bytes < 1)
275     GST_ERROR ("VP9 Boolean Decoder has no bits to read");
276 
277   if ((gst_bit_reader_get_pos (br) % 8) != 0)
278     GST_ERROR ("VP9 Boolean Decoder was passed an unaligned buffer");
279 
280   bd->value = 0;
281   bd->range = 255;
282   bd->bits_left = 8 * size_in_bytes;
283   bd->bit_reader = br;
284   bd->count_to_fill = 8;
285   bd->out_of_bits = FALSE;
286 
287   marker_bit = read_literal (bd, 1);
288   if (marker_bit != 0) {
289     GST_ERROR ("Marker bit should be zero was %d", marker_bit);
290     return GST_VP9_PARSER_BROKEN_DATA;
291   }
292 
293   return GST_VP9_PARSER_OK;
294 }
295 
296 static GstVp9ParserResult
exit_bool(Vp9BoolDecoder * bd)297 exit_bool (Vp9BoolDecoder * bd)
298 {
299   guint8 padding;
300   guint8 bits = bd->bits_left;
301   guint8 n;
302 
303   while (bits) {
304     n = MIN (bits, 8);
305     padding = gst_bit_reader_get_bits_uint32_unchecked (bd->bit_reader, n);
306     if (padding != 0 || (n < 8 && (padding & 0xe0) == 0xc0)) {
307       GST_ERROR
308           ("Invalid padding at end of frame. Total padding bits is %d and the wrong byte is: %x",
309           bd->bits_left, padding);
310       return GST_VP9_PARSER_BROKEN_DATA;
311     }
312     bits -= n;
313   }
314 
315   return GST_VP9_PARSER_OK;
316 }
317 
318 static guint
decode_term_subexp(Vp9BoolDecoder * bd)319 decode_term_subexp (Vp9BoolDecoder * bd)
320 {
321   guint8 bit;
322   guint v;
323   /* only coded if update_prob is set */
324   gboolean prob_is_coded_in_bitstream;
325   guint delta;
326 
327   prob_is_coded_in_bitstream = read_bool (bd, 252);
328   if (!prob_is_coded_in_bitstream)
329     return 0;
330 
331   bit = read_literal (bd, 1);
332   if (bit == 0) {
333     delta = read_literal (bd, 4);
334     goto end;
335   }
336 
337   bit = read_literal (bd, 1);
338   if (bit == 0) {
339     delta = read_literal (bd, 4) + 16;
340     goto end;
341   }
342 
343   bit = read_literal (bd, 1);
344   if (bit == 0) {
345     delta = read_literal (bd, 5) + 32;
346     goto end;
347   }
348 
349   v = read_literal (bd, 7);
350   if (v < 65) {
351     delta = v + 64;
352     goto end;
353   }
354 
355   bit = read_literal (bd, 1);
356   delta = (v << 1) - 1 + bit;
357 end:
358   return inv_map_table[delta];
359 }
360 
361 static guint8
read_mv_prob(Vp9BoolDecoder * bd)362 read_mv_prob (Vp9BoolDecoder * bd)
363 {
364   gboolean update_mv_prob;
365   guint8 mv_prob;
366   guint8 prob = 0;
367 
368   update_mv_prob = read_bool (bd, 252);
369   if (update_mv_prob) {
370     mv_prob = read_literal (bd, 7);
371     prob = (mv_prob << 1) | 1;
372   }
373 
374   return prob;
375 }
376 
377 static GstVp9ParserResult
parse_mv_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)378 parse_mv_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
379 {
380   guint i, j, k;
381 
382   for (j = 0; j < GST_VP9_MV_JOINTS - 1; j++)
383     hdr->delta_probabilities.mv.joint[j] = read_mv_prob (bd);
384 
385   for (i = 0; i < 2; i++) {
386     hdr->delta_probabilities.mv.sign[i] = read_mv_prob (bd);
387 
388     for (j = 0; j < GST_VP9_MV_CLASSES - 1; j++)
389       hdr->delta_probabilities.mv.klass[i][j] = read_mv_prob (bd);
390 
391     hdr->delta_probabilities.mv.class0_bit[i] = read_mv_prob (bd);
392 
393     for (j = 0; j < GST_VP9_MV_OFFSET_BITS; j++)
394       hdr->delta_probabilities.mv.bits[i][j] = read_mv_prob (bd);
395   }
396 
397   for (i = 0; i < 2; i++) {
398     for (j = 0; j < GST_VP9_CLASS0_SIZE; j++)
399       for (k = 0; k < GST_VP9_MV_FR_SIZE - 1; k++)
400         hdr->delta_probabilities.mv.class0_fr[i][j][k] = read_mv_prob (bd);
401 
402     for (k = 0; k < GST_VP9_MV_FR_SIZE - 1; k++)
403       hdr->delta_probabilities.mv.fr[i][k] = read_mv_prob (bd);
404   }
405 
406   if (hdr->allow_high_precision_mv) {
407     for (i = 0; i < 2; i++) {
408       hdr->delta_probabilities.mv.class0_hp[i] = read_mv_prob (bd);
409       hdr->delta_probabilities.mv.hp[i] = read_mv_prob (bd);
410     }
411 
412   }
413 
414   return GST_VP9_PARSER_OK;
415 }
416 
417 static GstVp9ParserResult
parse_partition_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)418 parse_partition_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
419 {
420   guint i, j;
421 
422   for (i = 0; i < GST_VP9_PARTITION_CONTEXTS; i++)
423     for (j = 0; j < GST_VP9_PARTITION_TYPES - 1; j++)
424       hdr->delta_probabilities.partition[i][j] = decode_term_subexp (bd);
425 
426   return GST_VP9_PARSER_OK;
427 }
428 
429 static GstVp9ParserResult
parse_y_mode_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)430 parse_y_mode_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
431 {
432   guint i, j;
433 
434   for (i = 0; i < GST_VP9_BLOCK_SIZE_GROUPS; i++)
435     for (j = 0; j < GST_VP9_INTRA_MODES - 1; j++)
436       hdr->delta_probabilities.y_mode[i][j] = decode_term_subexp (bd);
437 
438   return GST_VP9_PARSER_OK;
439 }
440 
441 static GstVp9ParserResult
parse_frame_reference_mode_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)442 parse_frame_reference_mode_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
443 {
444   guint i;
445 
446   if (hdr->reference_mode == GST_VP9_REFERENCE_MODE_SELECT)
447     for (i = 0; i < GST_VP9_COMP_MODE_CONTEXTS; i++)
448       hdr->delta_probabilities.comp_mode[i] = decode_term_subexp (bd);
449 
450   if (hdr->reference_mode != GST_VP9_REFERENCE_MODE_COMPOUND_REFERENCE)
451     for (i = 0; i < GST_VP9_REF_CONTEXTS; i++) {
452       hdr->delta_probabilities.single_ref[i][0] = decode_term_subexp (bd);
453       hdr->delta_probabilities.single_ref[i][1] = decode_term_subexp (bd);
454     }
455 
456   if (hdr->reference_mode != GST_VP9_REFERENCE_MODE_SINGLE_REFERENCE)
457     for (i = 0; i < GST_VP9_REF_CONTEXTS; i++)
458       hdr->delta_probabilities.comp_ref[i] = decode_term_subexp (bd);
459 
460   return GST_VP9_PARSER_OK;
461 }
462 
463 static GstVp9ParserResult
parse_frame_reference(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)464 parse_frame_reference (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
465 {
466   gboolean compound_ref_allowed = FALSE;
467   guint8 non_single_reference;
468   guint8 reference_select;
469   guint i;
470 
471   for (i = GST_VP9_REF_FRAME_LAST; i < GST_VP9_REFS_PER_FRAME; i++)
472     if (hdr->ref_frame_sign_bias[i + 1] !=
473         hdr->ref_frame_sign_bias[GST_VP9_REF_FRAME_LAST])
474       compound_ref_allowed = TRUE;
475 
476   if (compound_ref_allowed) {
477     non_single_reference = read_literal (bd, 1);
478     if (!non_single_reference)
479       hdr->reference_mode = GST_VP9_REFERENCE_MODE_SINGLE_REFERENCE;
480     else {
481       reference_select = read_literal (bd, 1);
482       if (!reference_select)
483         hdr->reference_mode = GST_VP9_REFERENCE_MODE_COMPOUND_REFERENCE;
484       else
485         hdr->reference_mode = GST_VP9_REFERENCE_MODE_SELECT;
486     }
487   } else
488     hdr->reference_mode = GST_VP9_REFERENCE_MODE_SINGLE_REFERENCE;
489 
490   return GST_VP9_PARSER_OK;
491 }
492 
493 static GstVp9ParserResult
parse_is_inter_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)494 parse_is_inter_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
495 {
496   guint i;
497 
498   for (i = 0; i < GST_VP9_IS_INTER_CONTEXTS; i++)
499     hdr->delta_probabilities.is_inter[i] = decode_term_subexp (bd);
500 
501   return GST_VP9_PARSER_OK;
502 }
503 
504 static GstVp9ParserResult
parse_interp_filter_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)505 parse_interp_filter_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
506 {
507   guint i, j;
508 
509   for (i = 0; i < GST_VP9_INTERP_FILTER_CONTEXTS; i++)
510     for (j = 0; j < GST_VP9_SWITCHABLE_FILTERS - 1; j++)
511       hdr->delta_probabilities.interp_filter[i][j] = decode_term_subexp (bd);
512 
513   return GST_VP9_PARSER_OK;
514 }
515 
516 static GstVp9ParserResult
parse_inter_mode_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)517 parse_inter_mode_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
518 {
519   guint i, j;
520 
521   for (i = 0; i < GST_VP9_INTER_MODE_CONTEXTS; i++)
522     for (j = 0; j < GST_VP9_INTER_MODES - 1; j++)
523       hdr->delta_probabilities.inter_mode[i][j] = decode_term_subexp (bd);
524 
525   return GST_VP9_PARSER_OK;
526 }
527 
528 static GstVp9ParserResult
parse_skip_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)529 parse_skip_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
530 {
531   guint i;
532 
533   for (i = 0; i < GST_VP9_SKIP_CONTEXTS; i++)
534     hdr->delta_probabilities.skip[i] = decode_term_subexp (bd);
535 
536   return GST_VP9_PARSER_OK;
537 }
538 
539 static GstVp9ParserResult
parse_coef_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)540 parse_coef_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
541 {
542   GstVp9TxSize tx_size, max_tx_size;
543   guint8 i, j, k, l, m;
544   guint8 update_probs;
545 
546   static const guint8 tx_mode_to_biggest_tx_size[GST_VP9_TX_MODES] = {
547     GST_VP9_TX_4x4,
548     GST_VP9_TX_8x8,
549     GST_VP9_TX_16x16,
550     GST_VP9_TX_32x32,
551     GST_VP9_TX_32x32,
552   };
553 
554   max_tx_size = tx_mode_to_biggest_tx_size[hdr->tx_mode];
555   for (tx_size = GST_VP9_TX_4x4; tx_size <= max_tx_size; tx_size++) {
556     update_probs = read_literal (bd, 1);
557     if (update_probs) {
558       for (i = 0; i < 2; i++)
559         for (j = 0; j < 2; j++)
560           for (k = 0; k < 6; k++)
561             for (l = 0; l < ((k == 0) ? 3 : 6); l++)
562               for (m = 0; m < 3; m++)
563                 hdr->delta_probabilities.coef[tx_size][i][j][k][l][m] =
564                     decode_term_subexp (bd);
565     }
566   }
567 
568   return GST_VP9_PARSER_OK;
569 }
570 
571 static GstVp9ParserResult
parse_tx_mode_probs(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)572 parse_tx_mode_probs (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
573 {
574   guint i, j;
575 
576   for (i = 0; i < GST_VP9_TX_SIZE_CONTEXTS; i++)
577     for (j = 0; j < GST_VP9_TX_SIZES - 3; j++)
578       hdr->delta_probabilities.tx_probs_8x8[i][j] = decode_term_subexp (bd);
579 
580   for (i = 0; i < GST_VP9_TX_SIZE_CONTEXTS; i++)
581     for (j = 0; j < GST_VP9_TX_SIZES - 2; j++)
582       hdr->delta_probabilities.tx_probs_16x16[i][j] = decode_term_subexp (bd);
583 
584   for (i = 0; i < GST_VP9_TX_SIZE_CONTEXTS; i++)
585     for (j = 0; j < GST_VP9_TX_SIZES - 1; j++)
586       hdr->delta_probabilities.tx_probs_32x32[i][j] = decode_term_subexp (bd);
587 
588   return GST_VP9_PARSER_OK;
589 }
590 
591 static GstVp9ParserResult
parse_tx_mode(GstVp9FrameHeader * hdr,Vp9BoolDecoder * bd)592 parse_tx_mode (GstVp9FrameHeader * hdr, Vp9BoolDecoder * bd)
593 {
594   guint8 tx_mode;
595   guint8 tx_mode_select;
596 
597   if (hdr->lossless_flag) {
598     hdr->tx_mode = GST_VP9_TX_MODE_ONLY_4x4;
599     return GST_VP9_PARSER_OK;
600   }
601 
602   tx_mode = read_literal (bd, 2);
603   if (tx_mode == GST_VP9_TX_MODE_ALLOW_32x32) {
604     tx_mode_select = read_literal (bd, 1);
605     tx_mode += tx_mode_select;
606   }
607 
608   hdr->tx_mode = tx_mode;
609   return GST_VP9_PARSER_OK;
610 }
611 
612 static GstVp9ParserResult
parse_compressed_header(GstVp9StatefulParser * self,GstVp9FrameHeader * hdr,GstBitReader * br)613 parse_compressed_header (GstVp9StatefulParser * self, GstVp9FrameHeader * hdr,
614     GstBitReader * br)
615 {
616   GstVp9ParserResult rst;
617   gboolean frame_is_intra_only;
618   Vp9BoolDecoder bd;
619 
620   /* consume trailing bits */
621   while (gst_bit_reader_get_pos (br) & 0x7)
622     gst_bit_reader_get_bits_uint8_unchecked (br, 1);
623 
624   rst = init_bool (&bd, br, hdr->header_size_in_bytes);
625   if (rst != GST_VP9_PARSER_OK) {
626     GST_ERROR ("Failed to init the boolean decoder.");
627     return rst;
628   }
629 
630   rst = parse_tx_mode (hdr, &bd);
631   if (rst != GST_VP9_PARSER_OK)
632     return rst;
633 
634   if (hdr->tx_mode == GST_VP9_TX_MODE_SELECT) {
635     rst = parse_tx_mode_probs (hdr, &bd);
636     if (rst != GST_VP9_PARSER_OK)
637       return rst;
638   }
639 
640   rst = parse_coef_probs (hdr, &bd);
641   if (rst != GST_VP9_PARSER_OK)
642     return rst;
643 
644   rst = parse_skip_probs (hdr, &bd);
645   if (rst != GST_VP9_PARSER_OK)
646     return rst;
647 
648   frame_is_intra_only = (hdr->frame_type == GST_VP9_KEY_FRAME
649       || hdr->intra_only);
650 
651   if (!frame_is_intra_only) {
652     rst = parse_inter_mode_probs (hdr, &bd);
653     if (rst != GST_VP9_PARSER_OK)
654       return rst;
655 
656     if (hdr->interpolation_filter == GST_VP9_INTERPOLATION_FILTER_SWITCHABLE) {
657       rst = parse_interp_filter_probs (hdr, &bd);
658       if (rst != GST_VP9_PARSER_OK)
659         return rst;
660     }
661 
662     rst = parse_is_inter_probs (hdr, &bd);
663     if (rst != GST_VP9_PARSER_OK)
664       return rst;
665 
666     rst = parse_frame_reference (hdr, &bd);
667     if (rst != GST_VP9_PARSER_OK)
668       return rst;
669 
670     rst = parse_frame_reference_mode_probs (hdr, &bd);
671     if (rst != GST_VP9_PARSER_OK)
672       return rst;
673 
674     rst = parse_y_mode_probs (hdr, &bd);
675     if (rst != GST_VP9_PARSER_OK)
676       return rst;
677 
678     rst = parse_partition_probs (hdr, &bd);
679     if (rst != GST_VP9_PARSER_OK)
680       return rst;
681 
682     rst = parse_mv_probs (hdr, &bd);
683     if (rst != GST_VP9_PARSER_OK)
684       return rst;
685   }
686 
687   rst = exit_bool (&bd);
688   if (rst != GST_VP9_PARSER_OK) {
689     GST_ERROR ("The boolean decoder did not exit cleanly.");
690     return rst;
691   }
692 
693   return GST_VP9_PARSER_OK;
694 }
695 
696 static const gint16 dc_qlookup[256] = {
697   4, 8, 8, 9, 10, 11, 12, 12,
698   13, 14, 15, 16, 17, 18, 19, 19,
699   20, 21, 22, 23, 24, 25, 26, 26,
700   27, 28, 29, 30, 31, 32, 32, 33,
701   34, 35, 36, 37, 38, 38, 39, 40,
702   41, 42, 43, 43, 44, 45, 46, 47,
703   48, 48, 49, 50, 51, 52, 53, 53,
704   54, 55, 56, 57, 57, 58, 59, 60,
705   61, 62, 62, 63, 64, 65, 66, 66,
706   67, 68, 69, 70, 70, 71, 72, 73,
707   74, 74, 75, 76, 77, 78, 78, 79,
708   80, 81, 81, 82, 83, 84, 85, 85,
709   87, 88, 90, 92, 93, 95, 96, 98,
710   99, 101, 102, 104, 105, 107, 108, 110,
711   111, 113, 114, 116, 117, 118, 120, 121,
712   123, 125, 127, 129, 131, 134, 136, 138,
713   140, 142, 144, 146, 148, 150, 152, 154,
714   156, 158, 161, 164, 166, 169, 172, 174,
715   177, 180, 182, 185, 187, 190, 192, 195,
716   199, 202, 205, 208, 211, 214, 217, 220,
717   223, 226, 230, 233, 237, 240, 243, 247,
718   250, 253, 257, 261, 265, 269, 272, 276,
719   280, 284, 288, 292, 296, 300, 304, 309,
720   313, 317, 322, 326, 330, 335, 340, 344,
721   349, 354, 359, 364, 369, 374, 379, 384,
722   389, 395, 400, 406, 411, 417, 423, 429,
723   435, 441, 447, 454, 461, 467, 475, 482,
724   489, 497, 505, 513, 522, 530, 539, 549,
725   559, 569, 579, 590, 602, 614, 626, 640,
726   654, 668, 684, 700, 717, 736, 755, 775,
727   796, 819, 843, 869, 896, 925, 955, 988,
728   1022, 1058, 1098, 1139, 1184, 1232, 1282, 1336,
729 };
730 
731 static const gint16 dc_qlookup_10[256] = {
732   4, 9, 10, 13, 15, 17, 20, 22,
733   25, 28, 31, 34, 37, 40, 43, 47,
734   50, 53, 57, 60, 64, 68, 71, 75,
735   78, 82, 86, 90, 93, 97, 101, 105,
736   109, 113, 116, 120, 124, 128, 132, 136,
737   140, 143, 147, 151, 155, 159, 163, 166,
738   170, 174, 178, 182, 185, 189, 193, 197,
739   200, 204, 208, 212, 215, 219, 223, 226,
740   230, 233, 237, 241, 244, 248, 251, 255,
741   259, 262, 266, 269, 273, 276, 280, 283,
742   287, 290, 293, 297, 300, 304, 307, 310,
743   314, 317, 321, 324, 327, 331, 334, 337,
744   343, 350, 356, 362, 369, 375, 381, 387,
745   394, 400, 406, 412, 418, 424, 430, 436,
746   442, 448, 454, 460, 466, 472, 478, 484,
747   490, 499, 507, 516, 525, 533, 542, 550,
748   559, 567, 576, 584, 592, 601, 609, 617,
749   625, 634, 644, 655, 666, 676, 687, 698,
750   708, 718, 729, 739, 749, 759, 770, 782,
751   795, 807, 819, 831, 844, 856, 868, 880,
752   891, 906, 920, 933, 947, 961, 975, 988,
753   1001, 1015, 1030, 1045, 1061, 1076, 1090, 1105,
754   1120, 1137, 1153, 1170, 1186, 1202, 1218, 1236,
755   1253, 1271, 1288, 1306, 1323, 1342, 1361, 1379,
756   1398, 1416, 1436, 1456, 1476, 1496, 1516, 1537,
757   1559, 1580, 1601, 1624, 1647, 1670, 1692, 1717,
758   1741, 1766, 1791, 1817, 1844, 1871, 1900, 1929,
759   1958, 1990, 2021, 2054, 2088, 2123, 2159, 2197,
760   2236, 2276, 2319, 2363, 2410, 2458, 2508, 2561,
761   2616, 2675, 2737, 2802, 2871, 2944, 3020, 3102,
762   3188, 3280, 3375, 3478, 3586, 3702, 3823, 3953,
763   4089, 4236, 4394, 4559, 4737, 4929, 5130, 5347,
764 };
765 
766 static const gint16 dc_qlookup_12[256] = {
767   4, 12, 18, 25, 33, 41, 50, 60,
768   70, 80, 91, 103, 115, 127, 140, 153,
769   166, 180, 194, 208, 222, 237, 251, 266,
770   281, 296, 312, 327, 343, 358, 374, 390,
771   405, 421, 437, 453, 469, 484, 500, 516,
772   532, 548, 564, 580, 596, 611, 627, 643,
773   659, 674, 690, 706, 721, 737, 752, 768,
774   783, 798, 814, 829, 844, 859, 874, 889,
775   904, 919, 934, 949, 964, 978, 993, 1008,
776   1022, 1037, 1051, 1065, 1080, 1094, 1108, 1122,
777   1136, 1151, 1165, 1179, 1192, 1206, 1220, 1234,
778   1248, 1261, 1275, 1288, 1302, 1315, 1329, 1342,
779   1368, 1393, 1419, 1444, 1469, 1494, 1519, 1544,
780   1569, 1594, 1618, 1643, 1668, 1692, 1717, 1741,
781   1765, 1789, 1814, 1838, 1862, 1885, 1909, 1933,
782   1957, 1992, 2027, 2061, 2096, 2130, 2165, 2199,
783   2233, 2267, 2300, 2334, 2367, 2400, 2434, 2467,
784   2499, 2532, 2575, 2618, 2661, 2704, 2746, 2788,
785   2830, 2872, 2913, 2954, 2995, 3036, 3076, 3127,
786   3177, 3226, 3275, 3324, 3373, 3421, 3469, 3517,
787   3565, 3621, 3677, 3733, 3788, 3843, 3897, 3951,
788   4005, 4058, 4119, 4181, 4241, 4301, 4361, 4420,
789   4479, 4546, 4612, 4677, 4742, 4807, 4871, 4942,
790   5013, 5083, 5153, 5222, 5291, 5367, 5442, 5517,
791   5591, 5665, 5745, 5825, 5905, 5984, 6063, 6149,
792   6234, 6319, 6404, 6495, 6587, 6678, 6769, 6867,
793   6966, 7064, 7163, 7269, 7376, 7483, 7599, 7715,
794   7832, 7958, 8085, 8214, 8352, 8492, 8635, 8788,
795   8945, 9104, 9275, 9450, 9639, 9832, 10031, 10245,
796   10465, 10702, 10946, 11210, 11482, 11776, 12081, 12409,
797   12750, 13118, 13501, 13913, 14343, 14807, 15290, 15812,
798   16356, 16943, 17575, 18237, 18949, 19718, 20521, 21387,
799 };
800 
801 static const gint16 ac_qlookup[256] = {
802   4, 8, 9, 10, 11, 12, 13, 14,
803   15, 16, 17, 18, 19, 20, 21, 22,
804   23, 24, 25, 26, 27, 28, 29, 30,
805   31, 32, 33, 34, 35, 36, 37, 38,
806   39, 40, 41, 42, 43, 44, 45, 46,
807   47, 48, 49, 50, 51, 52, 53, 54,
808   55, 56, 57, 58, 59, 60, 61, 62,
809   63, 64, 65, 66, 67, 68, 69, 70,
810   71, 72, 73, 74, 75, 76, 77, 78,
811   79, 80, 81, 82, 83, 84, 85, 86,
812   87, 88, 89, 90, 91, 92, 93, 94,
813   95, 96, 97, 98, 99, 100, 101, 102,
814   104, 106, 108, 110, 112, 114, 116, 118,
815   120, 122, 124, 126, 128, 130, 132, 134,
816   136, 138, 140, 142, 144, 146, 148, 150,
817   152, 155, 158, 161, 164, 167, 170, 173,
818   176, 179, 182, 185, 188, 191, 194, 197,
819   200, 203, 207, 211, 215, 219, 223, 227,
820   231, 235, 239, 243, 247, 251, 255, 260,
821   265, 270, 275, 280, 285, 290, 295, 300,
822   305, 311, 317, 323, 329, 335, 341, 347,
823   353, 359, 366, 373, 380, 387, 394, 401,
824   408, 416, 424, 432, 440, 448, 456, 465,
825   474, 483, 492, 501, 510, 520, 530, 540,
826   550, 560, 571, 582, 593, 604, 615, 627,
827   639, 651, 663, 676, 689, 702, 715, 729,
828   743, 757, 771, 786, 801, 816, 832, 848,
829   864, 881, 898, 915, 933, 951, 969, 988,
830   1007, 1026, 1046, 1066, 1087, 1108, 1129, 1151,
831   1173, 1196, 1219, 1243, 1267, 1292, 1317, 1343,
832   1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567,
833   1597, 1628, 1660, 1692, 1725, 1759, 1793, 1828,
834 };
835 
836 static const gint16 ac_qlookup_10[256] = {
837   4, 9, 11, 13, 16, 18, 21, 24,
838   27, 30, 33, 37, 40, 44, 48, 51,
839   55, 59, 63, 67, 71, 75, 79, 83,
840   88, 92, 96, 100, 105, 109, 114, 118,
841   122, 127, 131, 136, 140, 145, 149, 154,
842   158, 163, 168, 172, 177, 181, 186, 190,
843   195, 199, 204, 208, 213, 217, 222, 226,
844   231, 235, 240, 244, 249, 253, 258, 262,
845   267, 271, 275, 280, 284, 289, 293, 297,
846   302, 306, 311, 315, 319, 324, 328, 332,
847   337, 341, 345, 349, 354, 358, 362, 367,
848   371, 375, 379, 384, 388, 392, 396, 401,
849   409, 417, 425, 433, 441, 449, 458, 466,
850   474, 482, 490, 498, 506, 514, 523, 531,
851   539, 547, 555, 563, 571, 579, 588, 596,
852   604, 616, 628, 640, 652, 664, 676, 688,
853   700, 713, 725, 737, 749, 761, 773, 785,
854   797, 809, 825, 841, 857, 873, 889, 905,
855   922, 938, 954, 970, 986, 1002, 1018, 1038,
856   1058, 1078, 1098, 1118, 1138, 1158, 1178, 1198,
857   1218, 1242, 1266, 1290, 1314, 1338, 1362, 1386,
858   1411, 1435, 1463, 1491, 1519, 1547, 1575, 1603,
859   1631, 1663, 1695, 1727, 1759, 1791, 1823, 1859,
860   1895, 1931, 1967, 2003, 2039, 2079, 2119, 2159,
861   2199, 2239, 2283, 2327, 2371, 2415, 2459, 2507,
862   2555, 2603, 2651, 2703, 2755, 2807, 2859, 2915,
863   2971, 3027, 3083, 3143, 3203, 3263, 3327, 3391,
864   3455, 3523, 3591, 3659, 3731, 3803, 3876, 3952,
865   4028, 4104, 4184, 4264, 4348, 4432, 4516, 4604,
866   4692, 4784, 4876, 4972, 5068, 5168, 5268, 5372,
867   5476, 5584, 5692, 5804, 5916, 6032, 6148, 6268,
868   6388, 6512, 6640, 6768, 6900, 7036, 7172, 7312,
869 };
870 
871 static const gint16 ac_qlookup_12[256] = {
872   4, 13, 19, 27, 35, 44, 54, 64,
873   75, 87, 99, 112, 126, 139, 154, 168,
874   183, 199, 214, 230, 247, 263, 280, 297,
875   314, 331, 349, 366, 384, 402, 420, 438,
876   456, 475, 493, 511, 530, 548, 567, 586,
877   604, 623, 642, 660, 679, 698, 716, 735,
878   753, 772, 791, 809, 828, 846, 865, 884,
879   902, 920, 939, 957, 976, 994, 1012, 1030,
880   1049, 1067, 1085, 1103, 1121, 1139, 1157, 1175,
881   1193, 1211, 1229, 1246, 1264, 1282, 1299, 1317,
882   1335, 1352, 1370, 1387, 1405, 1422, 1440, 1457,
883   1474, 1491, 1509, 1526, 1543, 1560, 1577, 1595,
884   1627, 1660, 1693, 1725, 1758, 1791, 1824, 1856,
885   1889, 1922, 1954, 1987, 2020, 2052, 2085, 2118,
886   2150, 2183, 2216, 2248, 2281, 2313, 2346, 2378,
887   2411, 2459, 2508, 2556, 2605, 2653, 2701, 2750,
888   2798, 2847, 2895, 2943, 2992, 3040, 3088, 3137,
889   3185, 3234, 3298, 3362, 3426, 3491, 3555, 3619,
890   3684, 3748, 3812, 3876, 3941, 4005, 4069, 4149,
891   4230, 4310, 4390, 4470, 4550, 4631, 4711, 4791,
892   4871, 4967, 5064, 5160, 5256, 5352, 5448, 5544,
893   5641, 5737, 5849, 5961, 6073, 6185, 6297, 6410,
894   6522, 6650, 6778, 6906, 7034, 7162, 7290, 7435,
895   7579, 7723, 7867, 8011, 8155, 8315, 8475, 8635,
896   8795, 8956, 9132, 9308, 9484, 9660, 9836, 10028,
897   10220, 10412, 10604, 10812, 11020, 11228, 11437, 11661,
898   11885, 12109, 12333, 12573, 12813, 13053, 13309, 13565,
899   13821, 14093, 14365, 14637, 14925, 15213, 15502, 15806,
900   16110, 16414, 16734, 17054, 17390, 17726, 18062, 18414,
901   18766, 19134, 19502, 19886, 20270, 20670, 21070, 21486,
902   21902, 22334, 22766, 23214, 23662, 24126, 24590, 25070,
903   25551, 26047, 26559, 27071, 27599, 28143, 28687, 29247,
904 };
905 
906 static GstVp9ParserResult
parse_frame_marker(GstBitReader * br)907 parse_frame_marker (GstBitReader * br)
908 {
909   guint8 frame_marker;
910 
911   VP9_READ_UINT8 (frame_marker, 2);
912 
913   if (frame_marker != GST_VP9_FRAME_MARKER) {
914     GST_ERROR ("Invalid VP9 Frame Marker");
915     return GST_VP9_PARSER_ERROR;
916   }
917 
918   return GST_VP9_PARSER_OK;
919 }
920 
921 static GstVp9ParserResult
parse_frame_sync_code(GstBitReader * br)922 parse_frame_sync_code (GstBitReader * br)
923 {
924   guint32 code;
925 
926   VP9_READ_UINT32 (code, 24);
927   if (code != GST_VP9_SYNC_CODE) {
928     GST_ERROR ("%d is not VP9 sync code", code);
929     return GST_VP9_PARSER_ERROR;
930   }
931 
932   return GST_VP9_PARSER_OK;
933 }
934 
935 /* 6.2.2 Color config syntax */
936 static GstVp9ParserResult
parse_color_config(GstVp9StatefulParser * self,GstBitReader * br,GstVp9FrameHeader * header)937 parse_color_config (GstVp9StatefulParser * self, GstBitReader * br,
938     GstVp9FrameHeader * header)
939 {
940   guint8 bit = 0;
941 
942   if (header->profile >= GST_VP9_PROFILE_2) {
943     VP9_READ_BIT (bit);
944     if (bit) {
945       header->bit_depth = GST_VP9_BIT_DEPTH_12;
946     } else {
947       header->bit_depth = GST_VP9_BIT_DEPTH_10;
948     }
949   } else {
950     header->bit_depth = GST_VP9_BIT_DEPTH_8;
951   }
952 
953   VP9_READ_UINT8 (header->color_space, 3);
954   if (header->color_space != GST_VP9_CS_SRGB) {
955     VP9_READ_BIT (header->color_range);
956 
957     if (header->profile == GST_VP9_PROFILE_1
958         || header->profile == GST_VP9_PROFILE_3) {
959       VP9_READ_BIT (header->subsampling_x);
960       VP9_READ_BIT (header->subsampling_y);
961 
962       if (header->subsampling_x == 1 && header->subsampling_y == 1) {
963         GST_ERROR
964             ("4:2:0 subsampling is not supported in profile_1 or profile_3");
965         return GST_VP9_PARSER_ERROR;
966       }
967 
968       /* reserved bit */
969       VP9_READ_BIT (bit);
970     } else {
971       header->subsampling_y = header->subsampling_x = 1;
972     }
973   } else {
974     header->color_range = GST_VP9_CR_FULL;
975     if (header->profile == GST_VP9_PROFILE_1
976         || header->profile == GST_VP9_PROFILE_3) {
977       /* reserved bit */
978       VP9_READ_BIT (bit);
979     } else {
980       GST_ERROR
981           ("4:4:4 subsampling is not supported in profile_0 and profile_2");
982       return GST_VP9_PARSER_ERROR;
983     }
984   }
985 
986   self->bit_depth = header->bit_depth;
987   self->color_space = header->color_space;
988   self->subsampling_x = header->subsampling_x;
989   self->subsampling_y = header->subsampling_y;
990   self->color_range = header->color_range;
991 
992   return GST_VP9_PARSER_OK;
993 }
994 
995 /* 6.2 Uncompressed header syntax */
996 static GstVp9ParserResult
parse_profile(GstBitReader * br,guint8 * profile)997 parse_profile (GstBitReader * br, guint8 * profile)
998 {
999   guint8 profile_low_bit, profile_high_bit, ret, bit;
1000 
1001   VP9_READ_BIT (profile_low_bit);
1002   VP9_READ_BIT (profile_high_bit);
1003 
1004   ret = (profile_high_bit << 1) | profile_low_bit;
1005   if (ret == 3) {
1006     /* reserved bit */
1007     VP9_READ_BIT (bit);
1008   }
1009 
1010   *profile = ret;
1011 
1012   return GST_VP9_PARSER_OK;
1013 }
1014 
1015 /* 6.2.6 Compute image size syntax */
1016 static void
compute_image_size(GstVp9StatefulParser * self,guint32 width,guint32 height)1017 compute_image_size (GstVp9StatefulParser * self, guint32 width, guint32 height)
1018 {
1019   self->mi_cols = (width + 7) >> 3;
1020   self->mi_rows = (height + 7) >> 3;
1021   self->sb64_cols = (self->mi_cols + 7) >> 3;
1022   self->sb64_rows = (self->mi_rows + 7) >> 3;
1023 }
1024 
1025 static GstVp9ParserResult
parse_frame_or_render_size(GstBitReader * br,guint32 * width,guint32 * height)1026 parse_frame_or_render_size (GstBitReader * br,
1027     guint32 * width, guint32 * height)
1028 {
1029   guint32 width_minus_1;
1030   guint32 height_minus_1;
1031 
1032   VP9_READ_UINT32 (width_minus_1, 16);
1033   VP9_READ_UINT32 (height_minus_1, 16);
1034 
1035   *width = width_minus_1 + 1;
1036   *height = height_minus_1 + 1;
1037 
1038   return GST_VP9_PARSER_OK;
1039 }
1040 
1041 /* 6.2.3 Frame size syntax */
1042 static GstVp9ParserResult
parse_frame_size(GstVp9StatefulParser * self,GstBitReader * br,guint32 * width,guint32 * height)1043 parse_frame_size (GstVp9StatefulParser * self, GstBitReader * br,
1044     guint32 * width, guint32 * height)
1045 {
1046   GstVp9ParserResult rst;
1047 
1048   rst = parse_frame_or_render_size (br, width, height);
1049   if (rst != GST_VP9_PARSER_OK) {
1050     GST_ERROR ("Failed to parse frame size");
1051     return rst;
1052   }
1053 
1054   compute_image_size (self, *width, *height);
1055 
1056   return GST_VP9_PARSER_OK;
1057 }
1058 
1059 /* 6.2.4 Render size syntax */
1060 static GstVp9ParserResult
parse_render_size(GstBitReader * br,GstVp9FrameHeader * header)1061 parse_render_size (GstBitReader * br, GstVp9FrameHeader * header)
1062 {
1063   VP9_READ_BIT (header->render_and_frame_size_different);
1064   if (header->render_and_frame_size_different) {
1065     return parse_frame_or_render_size (br,
1066         &header->render_width, &header->render_height);
1067   } else {
1068     header->render_width = header->width;
1069     header->render_height = header->height;
1070   }
1071 
1072   return GST_VP9_PARSER_OK;
1073 }
1074 
1075 /* 6.2.5 Frame size with refs syntax */
1076 static GstVp9ParserResult
parse_frame_size_with_refs(GstVp9StatefulParser * self,GstBitReader * br,GstVp9FrameHeader * header)1077 parse_frame_size_with_refs (GstVp9StatefulParser * self, GstBitReader * br,
1078     GstVp9FrameHeader * header)
1079 {
1080   guint8 found_ref = 0;
1081   guint i;
1082 
1083   for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
1084     VP9_READ_BIT (found_ref);
1085 
1086     if (found_ref) {
1087       guint8 idx = header->ref_frame_idx[i];
1088 
1089       header->width = self->reference[idx].width;
1090       header->height = self->reference[idx].height;
1091       break;
1092     }
1093   }
1094 
1095   if (found_ref == 0) {
1096     GstVp9ParserResult rst;
1097 
1098     rst = parse_frame_size (self, br, &header->width, &header->height);
1099     if (rst != GST_VP9_PARSER_OK) {
1100       GST_ERROR ("Failed to parse frame size without refs");
1101       return rst;
1102     }
1103   } else {
1104     compute_image_size (self, header->width, header->height);
1105   }
1106 
1107   return parse_render_size (br, header);
1108 }
1109 
1110 /* 6.2.7 Interpolation filter syntax */
1111 static GstVp9ParserResult
read_interpolation_filter(GstBitReader * br,GstVp9FrameHeader * header)1112 read_interpolation_filter (GstBitReader * br, GstVp9FrameHeader * header)
1113 {
1114   static const GstVp9InterpolationFilter filter_map[] = {
1115     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SMOOTH,
1116     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP,
1117     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SHARP,
1118     GST_VP9_INTERPOLATION_FILTER_BILINEAR
1119   };
1120   guint8 is_filter_switchable;
1121 
1122   VP9_READ_BIT (is_filter_switchable);
1123   if (is_filter_switchable) {
1124     header->interpolation_filter = GST_VP9_INTERPOLATION_FILTER_SWITCHABLE;
1125   } else {
1126     guint8 map_val;
1127 
1128     VP9_READ_UINT8 (map_val, 2);
1129     header->interpolation_filter = filter_map[map_val];
1130   }
1131 
1132   return GST_VP9_PARSER_OK;
1133 }
1134 
1135 /* 6.2.8 Loop filter params syntax */
1136 static GstVp9ParserResult
parse_loop_filter_params(GstBitReader * br,GstVp9LoopFilterParams * params)1137 parse_loop_filter_params (GstBitReader * br, GstVp9LoopFilterParams * params)
1138 {
1139   VP9_READ_UINT8 (params->loop_filter_level, 6);
1140   VP9_READ_UINT8 (params->loop_filter_sharpness, 3);
1141   VP9_READ_BIT (params->loop_filter_delta_enabled);
1142 
1143   if (params->loop_filter_delta_enabled) {
1144     VP9_READ_BIT (params->loop_filter_delta_update);
1145     if (params->loop_filter_delta_update) {
1146       guint i;
1147 
1148       for (i = 0; i < GST_VP9_MAX_REF_LF_DELTAS; i++) {
1149         VP9_READ_BIT (params->update_ref_delta[i]);
1150         if (params->update_ref_delta[i]) {
1151           VP9_READ_SIGNED_8 (params->loop_filter_ref_deltas[i], 6);
1152         }
1153       }
1154 
1155       for (i = 0; i < GST_VP9_MAX_MODE_LF_DELTAS; i++) {
1156         VP9_READ_BIT (params->update_mode_delta[i]);
1157         if (params->update_mode_delta[i])
1158           VP9_READ_SIGNED_8 (params->loop_filter_mode_deltas[i], 6);
1159       }
1160     }
1161   }
1162 
1163   return GST_VP9_PARSER_OK;
1164 }
1165 
1166 /* 6.2.10 Delta quantizer syntax */
1167 static inline GstVp9ParserResult
parse_delta_q(GstBitReader * br,gint8 * value)1168 parse_delta_q (GstBitReader * br, gint8 * value)
1169 {
1170   guint8 read_signed;
1171   gint8 delta_q;
1172 
1173   VP9_READ_BIT (read_signed);
1174   if (!read_signed) {
1175     *value = 0;
1176     return GST_VP9_PARSER_OK;
1177   }
1178 
1179   VP9_READ_SIGNED_8 (delta_q, 4);
1180   *value = delta_q;
1181 
1182   return GST_VP9_PARSER_OK;
1183 }
1184 
1185 /* 6.2.9 Quantization params syntax */
1186 static GstVp9ParserResult
parse_quantization_params(GstBitReader * br,GstVp9FrameHeader * header)1187 parse_quantization_params (GstBitReader * br, GstVp9FrameHeader * header)
1188 {
1189   GstVp9QuantizationParams *params = &header->quantization_params;
1190   GstVp9ParserResult rst;
1191 
1192   VP9_READ_UINT8 (params->base_q_idx, 8);
1193   rst = parse_delta_q (br, &params->delta_q_y_dc);
1194   if (rst != GST_VP9_PARSER_OK)
1195     return rst;
1196 
1197   rst = parse_delta_q (br, &params->delta_q_uv_dc);
1198   if (rst != GST_VP9_PARSER_OK)
1199     return rst;
1200 
1201   rst = parse_delta_q (br, &params->delta_q_uv_ac);
1202   if (rst != GST_VP9_PARSER_OK)
1203     return rst;
1204 
1205   header->lossless_flag = params->base_q_idx == 0 && params->delta_q_y_dc == 0
1206       && params->delta_q_uv_dc == 0 && params->delta_q_uv_ac == 0;
1207 
1208   return GST_VP9_PARSER_OK;
1209 }
1210 
1211 /* 6.2.12 Probability syntax */
1212 static GstVp9ParserResult
read_prob(GstBitReader * br,guint8 * val)1213 read_prob (GstBitReader * br, guint8 * val)
1214 {
1215   guint8 prob = GST_VP9_MAX_PROB;
1216   guint8 prob_coded;
1217 
1218   VP9_READ_BIT (prob_coded);
1219 
1220   if (prob_coded)
1221     VP9_READ_UINT8 (prob, 8);
1222 
1223   *val = prob;
1224 
1225   return GST_VP9_PARSER_OK;
1226 }
1227 
1228 /* 6.2.11 Segmentation params syntax */
1229 static GstVp9ParserResult
parse_segmentation_params(GstBitReader * br,GstVp9SegmentationParams * params)1230 parse_segmentation_params (GstBitReader * br, GstVp9SegmentationParams * params)
1231 {
1232   guint i;
1233   GstVp9ParserResult rst;
1234 
1235   params->segmentation_update_map = 0;
1236   params->segmentation_update_data = 0;
1237   params->segmentation_temporal_update = 0;
1238 
1239   VP9_READ_BIT (params->segmentation_enabled);
1240   if (!params->segmentation_enabled)
1241     return GST_VP9_PARSER_OK;
1242 
1243   VP9_READ_BIT (params->segmentation_update_map);
1244   if (params->segmentation_update_map) {
1245     for (i = 0; i < GST_VP9_SEG_TREE_PROBS; i++) {
1246       rst = read_prob (br, &params->segmentation_tree_probs[i]);
1247       if (rst != GST_VP9_PARSER_OK) {
1248         GST_ERROR ("Failed to read segmentation_tree_probs[%d]", i);
1249         return rst;
1250       }
1251     }
1252 
1253     VP9_READ_BIT (params->segmentation_temporal_update);
1254     if (params->segmentation_temporal_update) {
1255       for (i = 0; i < GST_VP9_PREDICTION_PROBS; i++) {
1256         rst = read_prob (br, &params->segmentation_pred_prob[i]);
1257         if (rst != GST_VP9_PARSER_OK) {
1258           GST_ERROR ("Failed to read segmentation_pred_prob[%d]", i);
1259           return rst;
1260         }
1261       }
1262     } else {
1263       for (i = 0; i < GST_VP9_PREDICTION_PROBS; i++)
1264         params->segmentation_pred_prob[i] = GST_VP9_MAX_PROB;
1265     }
1266   }
1267 
1268   VP9_READ_BIT (params->segmentation_update_data);
1269   if (params->segmentation_update_data) {
1270     VP9_READ_BIT (params->segmentation_abs_or_delta_update);
1271 
1272     for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
1273       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_Q]);
1274       if (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_Q]) {
1275         VP9_READ_SIGNED_16 (params->feature_data[i][GST_VP9_SEG_LVL_ALT_Q], 8);
1276       } else {
1277         params->feature_data[i][GST_VP9_SEG_LVL_ALT_Q] = 0;
1278       }
1279 
1280       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_L]);
1281       if (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_L]) {
1282         VP9_READ_SIGNED_8 (params->feature_data[i][GST_VP9_SEG_LVL_ALT_L], 6);
1283       } else {
1284         params->feature_data[i][GST_VP9_SEG_LVL_ALT_L] = 0;
1285       }
1286 
1287       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME]);
1288       if (params->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME]) {
1289         guint8 val;
1290 
1291         VP9_READ_UINT8 (val, 2);
1292         params->feature_data[i][GST_VP9_SEG_LVL_REF_FRAME] = val;
1293       } else {
1294         params->feature_data[i][GST_VP9_SEG_LVL_REF_FRAME] = 0;
1295       }
1296 
1297       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_SEG_LVL_SKIP]);
1298     }
1299   }
1300   return GST_VP9_PARSER_OK;
1301 }
1302 
1303 /* 6.2.14 Tile size calculation */
1304 static guint
calc_min_log2_tile_cols(guint32 sb64_cols)1305 calc_min_log2_tile_cols (guint32 sb64_cols)
1306 {
1307   guint minLog2 = 0;
1308   static const guint MAX_TILE_WIDTH_B64 = 64;
1309 
1310   while ((MAX_TILE_WIDTH_B64 << minLog2) < sb64_cols)
1311     minLog2++;
1312 
1313   return minLog2;
1314 }
1315 
1316 static guint
calc_max_log2_tile_cols(guint32 sb64_cols)1317 calc_max_log2_tile_cols (guint32 sb64_cols)
1318 {
1319   guint maxLog2 = 1;
1320   static const guint MIN_TILE_WIDTH_B64 = 4;
1321 
1322   while ((sb64_cols >> maxLog2) >= MIN_TILE_WIDTH_B64)
1323     maxLog2++;
1324 
1325   return maxLog2 - 1;
1326 }
1327 
1328 /* 6.2.13 Tile info syntax */
1329 static GstVp9ParserResult
parse_tile_info(GstVp9StatefulParser * self,GstBitReader * br,GstVp9FrameHeader * header)1330 parse_tile_info (GstVp9StatefulParser * self, GstBitReader * br,
1331     GstVp9FrameHeader * header)
1332 {
1333   guint32 minLog2TileCols = calc_min_log2_tile_cols (self->sb64_cols);
1334   guint32 maxLog2TileCols = calc_max_log2_tile_cols (self->sb64_cols);
1335 
1336   header->tile_cols_log2 = minLog2TileCols;
1337 
1338   while (header->tile_cols_log2 < maxLog2TileCols) {
1339     guint8 increment_tile_cols_log2;
1340 
1341     VP9_READ_BIT (increment_tile_cols_log2);
1342     if (increment_tile_cols_log2)
1343       header->tile_cols_log2++;
1344     else
1345       break;
1346   }
1347 
1348   if (header->tile_cols_log2 > 6) {
1349     GST_ERROR ("Invalid number of tile columns");
1350     return GST_VP9_PARSER_ERROR;
1351   }
1352 
1353   VP9_READ_BIT (header->tile_rows_log2);
1354   if (header->tile_rows_log2) {
1355     guint8 increment_tile_rows_log2;
1356 
1357     VP9_READ_BIT (increment_tile_rows_log2);
1358     header->tile_rows_log2 += increment_tile_rows_log2;
1359   }
1360 
1361   return GST_VP9_PARSER_OK;
1362 }
1363 
1364 /* 7.2 Uncompressed header semantics */
1365 static void
setup_past_independence(GstVp9StatefulParser * self,GstVp9FrameHeader * header)1366 setup_past_independence (GstVp9StatefulParser * self,
1367     GstVp9FrameHeader * header)
1368 {
1369   memset (self->segmentation_params.feature_enabled,
1370       0, sizeof (self->segmentation_params.feature_enabled));
1371   memset (self->segmentation_params.feature_data,
1372       0, sizeof (self->segmentation_params.feature_data));
1373 
1374   self->segmentation_params.segmentation_abs_or_delta_update = 0;
1375 
1376   self->loop_filter_params.loop_filter_delta_enabled = 1;
1377   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_INTRA] = 1;
1378   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_LAST] = 0;
1379   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_GOLDEN] =
1380       -1;
1381   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_ALTREF] =
1382       -1;
1383 
1384   memset (self->loop_filter_params.loop_filter_mode_deltas, 0,
1385       sizeof (self->loop_filter_params.loop_filter_mode_deltas));
1386   memset (header->ref_frame_sign_bias, 0, sizeof (header->ref_frame_sign_bias));
1387 }
1388 
1389 /**
1390  * gst_vp9_stateful_parser_new:
1391  *
1392  * Creates a new #GstVp9StatefulParser. It should be freed with
1393  * gst_vp9_stateful_parser_free() after use.
1394  *
1395  * Returns: a new #GstVp9StatefulParser
1396  *
1397  * Since: 1.20
1398  */
1399 GstVp9StatefulParser *
gst_vp9_stateful_parser_new(void)1400 gst_vp9_stateful_parser_new (void)
1401 {
1402   GstVp9StatefulParser *parser;
1403 
1404   parser = g_new0 (GstVp9StatefulParser, 1);
1405 
1406   return parser;
1407 }
1408 
1409 /**
1410  * gst_vp9_stateful_parser_free:
1411  * @parser: the #GstVp9StatefulParser to free
1412  *
1413  * Frees @parser.
1414  *
1415  * Since: 1.20
1416  */
1417 void
gst_vp9_stateful_parser_free(GstVp9StatefulParser * parser)1418 gst_vp9_stateful_parser_free (GstVp9StatefulParser * parser)
1419 {
1420   g_free (parser);
1421 }
1422 
1423 /**
1424  * gst_vp9_stateful_parser_parse_compressed_frame_header:
1425  * @parser: The #GstVp9StatefulParser
1426  * @header: The #GstVp9FrameHeader to fill
1427  * @data: The data to parse
1428  * @size: The size of the @data to parse
1429  *
1430  * Parses the compressed information in the VP9 bitstream contained in @data,
1431  * and fills in @header with the parsed values.
1432  * The @size argument represent the whole frame size.
1433  *
1434  * Returns: a #GstVp9ParserResult
1435  *
1436  * Since: 1.20
1437  */
1438 
1439 GstVp9ParserResult
gst_vp9_stateful_parser_parse_compressed_frame_header(GstVp9StatefulParser * parser,GstVp9FrameHeader * header,const guint8 * data,gsize size)1440 gst_vp9_stateful_parser_parse_compressed_frame_header (GstVp9StatefulParser *
1441     parser, GstVp9FrameHeader * header, const guint8 * data, gsize size)
1442 {
1443   GstVp9ParserResult rst = GST_VP9_PARSER_OK;
1444   GstBitReader bit_reader;
1445   GstBitReader *br = &bit_reader;
1446 
1447   gst_bit_reader_init (br, data, size);
1448 
1449   rst = parse_compressed_header (parser, header, br);
1450   if (rst != GST_VP9_PARSER_OK) {
1451     GST_ERROR ("Failed to parse the compressed header");
1452     return GST_VP9_PARSER_ERROR;
1453   }
1454 
1455   return rst;
1456 }
1457 
1458 /**
1459  * gst_vp9_stateful_parser_parse_uncompressed_frame_header:
1460  * @parser: The #GstVp9StatefulParser
1461  * @header: The #GstVp9FrameHeader to fill
1462  * @data: The data to parse
1463  * @size: The size of the @data to parse
1464  *
1465  * Parses the VP9 bitstream contained in @data, and fills in @header
1466  * with the information. The @size argument represent the whole frame size.
1467  *
1468  * Returns: a #GstVp9ParserResult
1469  *
1470  * Since: 1.20
1471  */
1472 GstVp9ParserResult
gst_vp9_stateful_parser_parse_uncompressed_frame_header(GstVp9StatefulParser * parser,GstVp9FrameHeader * header,const guint8 * data,gsize size)1473 gst_vp9_stateful_parser_parse_uncompressed_frame_header (GstVp9StatefulParser *
1474     parser, GstVp9FrameHeader * header, const guint8 * data, gsize size)
1475 {
1476   GstBitReader bit_reader;
1477   GstBitReader *br = &bit_reader;
1478   gboolean frame_is_intra = FALSE;
1479   GstVp9ParserResult rst = GST_VP9_PARSER_OK;
1480   guint i;
1481 
1482   g_return_val_if_fail (parser, GST_VP9_PARSER_ERROR);
1483   g_return_val_if_fail (header, GST_VP9_PARSER_ERROR);
1484   g_return_val_if_fail (data, GST_VP9_PARSER_ERROR);
1485   g_return_val_if_fail (size, GST_VP9_PARSER_ERROR);
1486 
1487   gst_bit_reader_init (br, data, size);
1488   memset (header, 0, sizeof (GstVp9FrameHeader));
1489 
1490   /* Parsing Uncompressed Data Chunk */
1491   rst = parse_frame_marker (br);
1492   if (rst != GST_VP9_PARSER_OK)
1493     return rst;
1494 
1495   rst = parse_profile (br, &header->profile);
1496   if (rst != GST_VP9_PARSER_OK)
1497     return rst;
1498 
1499   CHECK_ALLOWED (header->profile, GST_VP9_PROFILE_0, GST_VP9_PROFILE_3);
1500 
1501   VP9_READ_BIT (header->show_existing_frame);
1502   if (header->show_existing_frame) {
1503     VP9_READ_UINT8 (header->frame_to_show_map_idx, 3);
1504     return GST_VP9_PARSER_OK;
1505   }
1506 
1507   VP9_READ_BIT (header->frame_type);
1508   VP9_READ_BIT (header->show_frame);
1509   VP9_READ_BIT (header->error_resilient_mode);
1510 
1511   if (header->frame_type == GST_VP9_KEY_FRAME) {
1512     rst = parse_frame_sync_code (br);
1513     if (rst != GST_VP9_PARSER_OK) {
1514       GST_ERROR ("Invalid VP9 sync code in keyframe");
1515       return rst;
1516     }
1517 
1518     rst = parse_color_config (parser, br, header);
1519     if (rst != GST_VP9_PARSER_OK) {
1520       GST_ERROR ("Failed to parse color config of keyframe");
1521       return rst;
1522     }
1523 
1524     rst = parse_frame_size (parser, br, &header->width, &header->height);
1525     if (rst != GST_VP9_PARSER_OK) {
1526       GST_ERROR ("Failed to parse frame size of keyframe");
1527       return rst;
1528     }
1529 
1530     rst = parse_render_size (br, header);
1531     if (rst != GST_VP9_PARSER_OK) {
1532       GST_ERROR ("Failed to parse render size of keyframe");
1533       return rst;
1534     }
1535 
1536     header->refresh_frame_flags = 0xff;
1537     frame_is_intra = TRUE;
1538   } else {
1539     if (header->show_frame == 0)
1540       VP9_READ_BIT (header->intra_only);
1541 
1542     frame_is_intra = header->intra_only;
1543     if (header->error_resilient_mode == 0)
1544       VP9_READ_UINT8 (header->reset_frame_context, 2);
1545 
1546     if (header->intra_only) {
1547       rst = parse_frame_sync_code (br);
1548       if (rst != GST_VP9_PARSER_OK) {
1549         GST_ERROR ("Invalid VP9 sync code in intra-only frame");
1550         return rst;
1551       }
1552 
1553       if (header->profile > GST_VP9_PROFILE_0) {
1554         rst = parse_color_config (parser, br, header);
1555         if (rst != GST_VP9_PARSER_OK) {
1556           GST_ERROR ("Failed to parse color config of intra-only frame");
1557           return rst;
1558         }
1559       } else {
1560         parser->color_space = header->color_space = GST_VP9_CS_BT_601;
1561         parser->color_range = header->color_range = GST_VP9_CR_LIMITED;
1562         parser->subsampling_x = parser->subsampling_y =
1563             header->subsampling_x = header->subsampling_y = 1;
1564         parser->bit_depth = header->bit_depth = GST_VP9_BIT_DEPTH_8;
1565       }
1566 
1567       VP9_READ_UINT8 (header->refresh_frame_flags, 8);
1568       rst = parse_frame_size (parser, br, &header->width, &header->height);
1569       if (rst != GST_VP9_PARSER_OK) {
1570         GST_ERROR ("Failed to pase frame size of intra-only frame");
1571         return rst;
1572       }
1573 
1574       rst = parse_render_size (br, header);
1575       if (rst != GST_VP9_PARSER_OK) {
1576         GST_ERROR ("Failed to parse render size of intra-only frame");
1577         return rst;
1578       }
1579     } else {
1580       /* copy color_config from previously parsed one */
1581       header->color_space = parser->color_space;
1582       header->color_range = parser->color_range;
1583       header->subsampling_x = parser->subsampling_x;
1584       header->subsampling_y = parser->subsampling_y;
1585       header->bit_depth = parser->bit_depth;
1586 
1587       VP9_READ_UINT8 (header->refresh_frame_flags, 8);
1588       for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
1589         VP9_READ_UINT8 (header->ref_frame_idx[i], 3);
1590         VP9_READ_BIT (header->ref_frame_sign_bias[GST_VP9_REF_FRAME_LAST + i]);
1591       }
1592 
1593       rst = parse_frame_size_with_refs (parser, br, header);
1594       if (rst != GST_VP9_PARSER_OK) {
1595         GST_ERROR ("Failed to parse frame size with refs");
1596         return rst;
1597       }
1598 
1599       VP9_READ_BIT (header->allow_high_precision_mv);
1600       rst = read_interpolation_filter (br, header);
1601       if (rst != GST_VP9_PARSER_OK) {
1602         GST_ERROR ("Failed to read interpolation filter information");
1603         return rst;
1604       }
1605     }
1606   }
1607 
1608   if (!header->error_resilient_mode) {
1609     VP9_READ_BIT (header->refresh_frame_context);
1610     VP9_READ_BIT (header->frame_parallel_decoding_mode);
1611   } else {
1612     header->refresh_frame_context = 0;
1613     header->frame_parallel_decoding_mode = 1;
1614   }
1615 
1616   VP9_READ_UINT8 (header->frame_context_idx, 2);
1617 
1618   if (frame_is_intra || header->error_resilient_mode)
1619     setup_past_independence (parser, header);
1620 
1621   /* First update our own table, and we will copy to frame header later */
1622   rst = parse_loop_filter_params (br, &parser->loop_filter_params);
1623   if (rst != GST_VP9_PARSER_OK) {
1624     GST_ERROR ("Failed to parse loop filter params");
1625     return rst;
1626   }
1627 
1628   rst = parse_quantization_params (br, header);
1629   if (rst != GST_VP9_PARSER_OK) {
1630     GST_ERROR ("Failed to parse quantization params");
1631     return rst;
1632   }
1633 
1634   /* Also update our own table, then it will be copied later */
1635   rst = parse_segmentation_params (br, &parser->segmentation_params);
1636   if (rst != GST_VP9_PARSER_OK) {
1637     GST_ERROR ("Failed to parse segmentation params");
1638     return rst;
1639   }
1640 
1641   rst = parse_tile_info (parser, br, header);
1642   if (rst != GST_VP9_PARSER_OK) {
1643     GST_ERROR ("Failed to parse tile info");
1644     return rst;
1645   }
1646 
1647   VP9_READ_UINT16 (header->header_size_in_bytes, 16);
1648   if (!header->header_size_in_bytes) {
1649     GST_ERROR ("Failed to parse header size in bytes");
1650     return GST_VP9_PARSER_ERROR;
1651   }
1652 
1653   /* copy our values to header */
1654   memcpy (&header->loop_filter_params, &parser->loop_filter_params,
1655       sizeof (GstVp9LoopFilterParams));
1656   memcpy (&header->segmentation_params, &parser->segmentation_params,
1657       sizeof (GstVp9SegmentationParams));
1658 
1659   /* And update reference frames */
1660   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
1661     guint8 flag = (1 << i);
1662     if ((header->refresh_frame_flags & flag) != 0) {
1663       parser->reference[i].width = header->width;
1664       parser->reference[i].height = header->height;
1665     }
1666   }
1667 
1668   header->frame_header_length_in_bytes = (gst_bit_reader_get_pos (br) + 7) / 8;
1669 
1670 
1671   return GST_VP9_PARSER_OK;
1672 }
1673 
1674 /**
1675  * gst_vp9_seg_feature_active:
1676  * @params: a #GstVp9SegmentationParams
1677  * @segment_id: a segment id
1678  * @feature: a segmentation feature
1679  *
1680  * An implementation of "seg_feature_active" function specified in
1681  * "6.4.9 Segmentation feature active syntax"
1682  *
1683  * Returns: %TRUE if feature is active
1684  *
1685  * Since: 1.20
1686  */
1687 gboolean
gst_vp9_seg_feature_active(const GstVp9SegmentationParams * params,guint8 segment_id,guint8 feature)1688 gst_vp9_seg_feature_active (const GstVp9SegmentationParams * params,
1689     guint8 segment_id, guint8 feature)
1690 {
1691   g_return_val_if_fail (params != NULL, FALSE);
1692   g_return_val_if_fail (segment_id < GST_VP9_MAX_SEGMENTS, FALSE);
1693   g_return_val_if_fail (feature < GST_VP9_SEG_LVL_MAX, FALSE);
1694 
1695   return params->segmentation_enabled &&
1696       params->feature_enabled[segment_id][feature];
1697 }
1698 
1699 /**
1700  * gst_vp9_get_qindex:
1701  * @segmentation_params: a #GstVp9SegmentationParams
1702  * @quantization_params: a #GstVp9QuantizationParams
1703  * @segment_id: a segment id
1704  *
1705  * An implementation of "get_qindex" function specfied in
1706  * "8.6.1 Dequantization functions"
1707  *
1708  * Returns: the quantizer index
1709  *
1710  * Since: 1.20
1711  */
1712 guint8
gst_vp9_get_qindex(const GstVp9SegmentationParams * segmentation_params,const GstVp9QuantizationParams * quantization_params,guint8 segment_id)1713 gst_vp9_get_qindex (const GstVp9SegmentationParams * segmentation_params,
1714     const GstVp9QuantizationParams * quantization_params, guint8 segment_id)
1715 {
1716   guint8 base_q_index;
1717 
1718   g_return_val_if_fail (segmentation_params != NULL, 0);
1719   g_return_val_if_fail (quantization_params != NULL, 0);
1720   g_return_val_if_fail (segment_id < GST_VP9_MAX_SEGMENTS, 0);
1721 
1722   base_q_index = quantization_params->base_q_idx;
1723 
1724   if (gst_vp9_seg_feature_active (segmentation_params, segment_id,
1725           GST_VP9_SEG_LVL_ALT_Q)) {
1726     gint data =
1727         segmentation_params->feature_data[segment_id][GST_VP9_SEG_LVL_ALT_Q];
1728 
1729     if (!segmentation_params->segmentation_abs_or_delta_update)
1730       data += base_q_index;
1731 
1732     return CLAMP (data, 0, 255);
1733   }
1734 
1735   return base_q_index;
1736 }
1737 
1738 /**
1739  * gst_vp9_get_dc_quant:
1740  * @qindex: the quantizer index
1741  * @delta_q_dc: a delta_q_dc value
1742  * @bit_depth: coded bit depth
1743  *
1744  * An implementation of "dc_q" function specified in
1745  * "8.6.1 Dequantization functions"
1746  *
1747  * Returns: the quantizer value for the dc coefficient
1748  *
1749  * Since: 1.20
1750  */
1751 gint16
gst_vp9_get_dc_quant(guint8 qindex,gint8 delta_q_dc,guint8 bit_depth)1752 gst_vp9_get_dc_quant (guint8 qindex, gint8 delta_q_dc, guint8 bit_depth)
1753 {
1754   guint8 q_table_idx = CLAMP (qindex + delta_q_dc, 0, 255);
1755 
1756   switch (bit_depth) {
1757     case 8:
1758       return dc_qlookup[q_table_idx];
1759     case 10:
1760       return dc_qlookup_10[q_table_idx];
1761     case 12:
1762       return dc_qlookup_12[q_table_idx];
1763     default:
1764       GST_WARNING ("Unhandled bitdepth %d", bit_depth);
1765       break;
1766   }
1767 
1768   return -1;
1769 }
1770 
1771 /**
1772  * gst_vp9_get_ac_quant:
1773  * @qindex: the quantizer index
1774  * @delta_q_ac: a delta_q_ac value
1775  * @bit_depth: coded bit depth
1776  *
1777  * An implementation of "ac_q" function specified in
1778  * "8.6.1 Dequantization functions"
1779  *
1780  * Returns: the quantizer value for the ac coefficient
1781  *
1782  * Since: 1.20
1783  */
1784 gint16
gst_vp9_get_ac_quant(guint8 qindex,gint8 delta_q_ac,guint8 bit_depth)1785 gst_vp9_get_ac_quant (guint8 qindex, gint8 delta_q_ac, guint8 bit_depth)
1786 {
1787   guint8 q_table_idx = CLAMP (qindex + delta_q_ac, 0, 255);
1788 
1789   switch (bit_depth) {
1790     case 8:
1791       return ac_qlookup[q_table_idx];
1792     case 10:
1793       return ac_qlookup_10[q_table_idx];
1794     case 12:
1795       return ac_qlookup_12[q_table_idx];
1796     default:
1797       GST_WARNING ("Unhandled bitdepth %d", bit_depth);
1798       break;
1799   }
1800 
1801   return -1;
1802 }
1803