Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / codecs / gstvp9statefulparser.c
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 *
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 static const gint16 dc_qlookup[256] = {
153   4, 8, 8, 9, 10, 11, 12, 12,
154   13, 14, 15, 16, 17, 18, 19, 19,
155   20, 21, 22, 23, 24, 25, 26, 26,
156   27, 28, 29, 30, 31, 32, 32, 33,
157   34, 35, 36, 37, 38, 38, 39, 40,
158   41, 42, 43, 43, 44, 45, 46, 47,
159   48, 48, 49, 50, 51, 52, 53, 53,
160   54, 55, 56, 57, 57, 58, 59, 60,
161   61, 62, 62, 63, 64, 65, 66, 66,
162   67, 68, 69, 70, 70, 71, 72, 73,
163   74, 74, 75, 76, 77, 78, 78, 79,
164   80, 81, 81, 82, 83, 84, 85, 85,
165   87, 88, 90, 92, 93, 95, 96, 98,
166   99, 101, 102, 104, 105, 107, 108, 110,
167   111, 113, 114, 116, 117, 118, 120, 121,
168   123, 125, 127, 129, 131, 134, 136, 138,
169   140, 142, 144, 146, 148, 150, 152, 154,
170   156, 158, 161, 164, 166, 169, 172, 174,
171   177, 180, 182, 185, 187, 190, 192, 195,
172   199, 202, 205, 208, 211, 214, 217, 220,
173   223, 226, 230, 233, 237, 240, 243, 247,
174   250, 253, 257, 261, 265, 269, 272, 276,
175   280, 284, 288, 292, 296, 300, 304, 309,
176   313, 317, 322, 326, 330, 335, 340, 344,
177   349, 354, 359, 364, 369, 374, 379, 384,
178   389, 395, 400, 406, 411, 417, 423, 429,
179   435, 441, 447, 454, 461, 467, 475, 482,
180   489, 497, 505, 513, 522, 530, 539, 549,
181   559, 569, 579, 590, 602, 614, 626, 640,
182   654, 668, 684, 700, 717, 736, 755, 775,
183   796, 819, 843, 869, 896, 925, 955, 988,
184   1022, 1058, 1098, 1139, 1184, 1232, 1282, 1336,
185 };
186
187 static const gint16 dc_qlookup_10[256] = {
188   4, 9, 10, 13, 15, 17, 20, 22,
189   25, 28, 31, 34, 37, 40, 43, 47,
190   50, 53, 57, 60, 64, 68, 71, 75,
191   78, 82, 86, 90, 93, 97, 101, 105,
192   109, 113, 116, 120, 124, 128, 132, 136,
193   140, 143, 147, 151, 155, 159, 163, 166,
194   170, 174, 178, 182, 185, 189, 193, 197,
195   200, 204, 208, 212, 215, 219, 223, 226,
196   230, 233, 237, 241, 244, 248, 251, 255,
197   259, 262, 266, 269, 273, 276, 280, 283,
198   287, 290, 293, 297, 300, 304, 307, 310,
199   314, 317, 321, 324, 327, 331, 334, 337,
200   343, 350, 356, 362, 369, 375, 381, 387,
201   394, 400, 406, 412, 418, 424, 430, 436,
202   442, 448, 454, 460, 466, 472, 478, 484,
203   490, 499, 507, 516, 525, 533, 542, 550,
204   559, 567, 576, 584, 592, 601, 609, 617,
205   625, 634, 644, 655, 666, 676, 687, 698,
206   708, 718, 729, 739, 749, 759, 770, 782,
207   795, 807, 819, 831, 844, 856, 868, 880,
208   891, 906, 920, 933, 947, 961, 975, 988,
209   1001, 1015, 1030, 1045, 1061, 1076, 1090, 1105,
210   1120, 1137, 1153, 1170, 1186, 1202, 1218, 1236,
211   1253, 1271, 1288, 1306, 1323, 1342, 1361, 1379,
212   1398, 1416, 1436, 1456, 1476, 1496, 1516, 1537,
213   1559, 1580, 1601, 1624, 1647, 1670, 1692, 1717,
214   1741, 1766, 1791, 1817, 1844, 1871, 1900, 1929,
215   1958, 1990, 2021, 2054, 2088, 2123, 2159, 2197,
216   2236, 2276, 2319, 2363, 2410, 2458, 2508, 2561,
217   2616, 2675, 2737, 2802, 2871, 2944, 3020, 3102,
218   3188, 3280, 3375, 3478, 3586, 3702, 3823, 3953,
219   4089, 4236, 4394, 4559, 4737, 4929, 5130, 5347,
220 };
221
222 static const gint16 dc_qlookup_12[256] = {
223   4, 12, 18, 25, 33, 41, 50, 60,
224   70, 80, 91, 103, 115, 127, 140, 153,
225   166, 180, 194, 208, 222, 237, 251, 266,
226   281, 296, 312, 327, 343, 358, 374, 390,
227   405, 421, 437, 453, 469, 484, 500, 516,
228   532, 548, 564, 580, 596, 611, 627, 643,
229   659, 674, 690, 706, 721, 737, 752, 768,
230   783, 798, 814, 829, 844, 859, 874, 889,
231   904, 919, 934, 949, 964, 978, 993, 1008,
232   1022, 1037, 1051, 1065, 1080, 1094, 1108, 1122,
233   1136, 1151, 1165, 1179, 1192, 1206, 1220, 1234,
234   1248, 1261, 1275, 1288, 1302, 1315, 1329, 1342,
235   1368, 1393, 1419, 1444, 1469, 1494, 1519, 1544,
236   1569, 1594, 1618, 1643, 1668, 1692, 1717, 1741,
237   1765, 1789, 1814, 1838, 1862, 1885, 1909, 1933,
238   1957, 1992, 2027, 2061, 2096, 2130, 2165, 2199,
239   2233, 2267, 2300, 2334, 2367, 2400, 2434, 2467,
240   2499, 2532, 2575, 2618, 2661, 2704, 2746, 2788,
241   2830, 2872, 2913, 2954, 2995, 3036, 3076, 3127,
242   3177, 3226, 3275, 3324, 3373, 3421, 3469, 3517,
243   3565, 3621, 3677, 3733, 3788, 3843, 3897, 3951,
244   4005, 4058, 4119, 4181, 4241, 4301, 4361, 4420,
245   4479, 4546, 4612, 4677, 4742, 4807, 4871, 4942,
246   5013, 5083, 5153, 5222, 5291, 5367, 5442, 5517,
247   5591, 5665, 5745, 5825, 5905, 5984, 6063, 6149,
248   6234, 6319, 6404, 6495, 6587, 6678, 6769, 6867,
249   6966, 7064, 7163, 7269, 7376, 7483, 7599, 7715,
250   7832, 7958, 8085, 8214, 8352, 8492, 8635, 8788,
251   8945, 9104, 9275, 9450, 9639, 9832, 10031, 10245,
252   10465, 10702, 10946, 11210, 11482, 11776, 12081, 12409,
253   12750, 13118, 13501, 13913, 14343, 14807, 15290, 15812,
254   16356, 16943, 17575, 18237, 18949, 19718, 20521, 21387,
255 };
256
257 static const gint16 ac_qlookup[256] = {
258   4, 8, 9, 10, 11, 12, 13, 14,
259   15, 16, 17, 18, 19, 20, 21, 22,
260   23, 24, 25, 26, 27, 28, 29, 30,
261   31, 32, 33, 34, 35, 36, 37, 38,
262   39, 40, 41, 42, 43, 44, 45, 46,
263   47, 48, 49, 50, 51, 52, 53, 54,
264   55, 56, 57, 58, 59, 60, 61, 62,
265   63, 64, 65, 66, 67, 68, 69, 70,
266   71, 72, 73, 74, 75, 76, 77, 78,
267   79, 80, 81, 82, 83, 84, 85, 86,
268   87, 88, 89, 90, 91, 92, 93, 94,
269   95, 96, 97, 98, 99, 100, 101, 102,
270   104, 106, 108, 110, 112, 114, 116, 118,
271   120, 122, 124, 126, 128, 130, 132, 134,
272   136, 138, 140, 142, 144, 146, 148, 150,
273   152, 155, 158, 161, 164, 167, 170, 173,
274   176, 179, 182, 185, 188, 191, 194, 197,
275   200, 203, 207, 211, 215, 219, 223, 227,
276   231, 235, 239, 243, 247, 251, 255, 260,
277   265, 270, 275, 280, 285, 290, 295, 300,
278   305, 311, 317, 323, 329, 335, 341, 347,
279   353, 359, 366, 373, 380, 387, 394, 401,
280   408, 416, 424, 432, 440, 448, 456, 465,
281   474, 483, 492, 501, 510, 520, 530, 540,
282   550, 560, 571, 582, 593, 604, 615, 627,
283   639, 651, 663, 676, 689, 702, 715, 729,
284   743, 757, 771, 786, 801, 816, 832, 848,
285   864, 881, 898, 915, 933, 951, 969, 988,
286   1007, 1026, 1046, 1066, 1087, 1108, 1129, 1151,
287   1173, 1196, 1219, 1243, 1267, 1292, 1317, 1343,
288   1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567,
289   1597, 1628, 1660, 1692, 1725, 1759, 1793, 1828,
290 };
291
292 static const gint16 ac_qlookup_10[256] = {
293   4, 9, 11, 13, 16, 18, 21, 24,
294   27, 30, 33, 37, 40, 44, 48, 51,
295   55, 59, 63, 67, 71, 75, 79, 83,
296   88, 92, 96, 100, 105, 109, 114, 118,
297   122, 127, 131, 136, 140, 145, 149, 154,
298   158, 163, 168, 172, 177, 181, 186, 190,
299   195, 199, 204, 208, 213, 217, 222, 226,
300   231, 235, 240, 244, 249, 253, 258, 262,
301   267, 271, 275, 280, 284, 289, 293, 297,
302   302, 306, 311, 315, 319, 324, 328, 332,
303   337, 341, 345, 349, 354, 358, 362, 367,
304   371, 375, 379, 384, 388, 392, 396, 401,
305   409, 417, 425, 433, 441, 449, 458, 466,
306   474, 482, 490, 498, 506, 514, 523, 531,
307   539, 547, 555, 563, 571, 579, 588, 596,
308   604, 616, 628, 640, 652, 664, 676, 688,
309   700, 713, 725, 737, 749, 761, 773, 785,
310   797, 809, 825, 841, 857, 873, 889, 905,
311   922, 938, 954, 970, 986, 1002, 1018, 1038,
312   1058, 1078, 1098, 1118, 1138, 1158, 1178, 1198,
313   1218, 1242, 1266, 1290, 1314, 1338, 1362, 1386,
314   1411, 1435, 1463, 1491, 1519, 1547, 1575, 1603,
315   1631, 1663, 1695, 1727, 1759, 1791, 1823, 1859,
316   1895, 1931, 1967, 2003, 2039, 2079, 2119, 2159,
317   2199, 2239, 2283, 2327, 2371, 2415, 2459, 2507,
318   2555, 2603, 2651, 2703, 2755, 2807, 2859, 2915,
319   2971, 3027, 3083, 3143, 3203, 3263, 3327, 3391,
320   3455, 3523, 3591, 3659, 3731, 3803, 3876, 3952,
321   4028, 4104, 4184, 4264, 4348, 4432, 4516, 4604,
322   4692, 4784, 4876, 4972, 5068, 5168, 5268, 5372,
323   5476, 5584, 5692, 5804, 5916, 6032, 6148, 6268,
324   6388, 6512, 6640, 6768, 6900, 7036, 7172, 7312,
325 };
326
327 static const gint16 ac_qlookup_12[256] = {
328   4, 13, 19, 27, 35, 44, 54, 64,
329   75, 87, 99, 112, 126, 139, 154, 168,
330   183, 199, 214, 230, 247, 263, 280, 297,
331   314, 331, 349, 366, 384, 402, 420, 438,
332   456, 475, 493, 511, 530, 548, 567, 586,
333   604, 623, 642, 660, 679, 698, 716, 735,
334   753, 772, 791, 809, 828, 846, 865, 884,
335   902, 920, 939, 957, 976, 994, 1012, 1030,
336   1049, 1067, 1085, 1103, 1121, 1139, 1157, 1175,
337   1193, 1211, 1229, 1246, 1264, 1282, 1299, 1317,
338   1335, 1352, 1370, 1387, 1405, 1422, 1440, 1457,
339   1474, 1491, 1509, 1526, 1543, 1560, 1577, 1595,
340   1627, 1660, 1693, 1725, 1758, 1791, 1824, 1856,
341   1889, 1922, 1954, 1987, 2020, 2052, 2085, 2118,
342   2150, 2183, 2216, 2248, 2281, 2313, 2346, 2378,
343   2411, 2459, 2508, 2556, 2605, 2653, 2701, 2750,
344   2798, 2847, 2895, 2943, 2992, 3040, 3088, 3137,
345   3185, 3234, 3298, 3362, 3426, 3491, 3555, 3619,
346   3684, 3748, 3812, 3876, 3941, 4005, 4069, 4149,
347   4230, 4310, 4390, 4470, 4550, 4631, 4711, 4791,
348   4871, 4967, 5064, 5160, 5256, 5352, 5448, 5544,
349   5641, 5737, 5849, 5961, 6073, 6185, 6297, 6410,
350   6522, 6650, 6778, 6906, 7034, 7162, 7290, 7435,
351   7579, 7723, 7867, 8011, 8155, 8315, 8475, 8635,
352   8795, 8956, 9132, 9308, 9484, 9660, 9836, 10028,
353   10220, 10412, 10604, 10812, 11020, 11228, 11437, 11661,
354   11885, 12109, 12333, 12573, 12813, 13053, 13309, 13565,
355   13821, 14093, 14365, 14637, 14925, 15213, 15502, 15806,
356   16110, 16414, 16734, 17054, 17390, 17726, 18062, 18414,
357   18766, 19134, 19502, 19886, 20270, 20670, 21070, 21486,
358   21902, 22334, 22766, 23214, 23662, 24126, 24590, 25070,
359   25551, 26047, 26559, 27071, 27599, 28143, 28687, 29247,
360 };
361
362 static GstVp9ParserResult
363 parse_frame_marker (GstBitReader * br)
364 {
365   guint8 frame_marker;
366
367   VP9_READ_UINT8 (frame_marker, 2);
368
369   if (frame_marker != GST_VP9_FRAME_MARKER) {
370     GST_ERROR ("Invalid VP9 Frame Marker");
371     return GST_VP9_PARSER_ERROR;
372   }
373
374   return GST_VP9_PARSER_OK;
375 }
376
377 static GstVp9ParserResult
378 parse_frame_sync_code (GstBitReader * br)
379 {
380   guint32 code;
381
382   VP9_READ_UINT32 (code, 24);
383   if (code != GST_VP9_SYNC_CODE) {
384     GST_ERROR ("%d is not VP9 sync code", code);
385     return GST_VP9_PARSER_ERROR;
386   }
387
388   return GST_VP9_PARSER_OK;
389 }
390
391 /* 6.2.2 Color config syntax */
392 static GstVp9ParserResult
393 parse_color_config (GstVp9StatefulParser * self, GstBitReader * br,
394     GstVp9FrameHeader * header)
395 {
396   guint8 bit = 0;
397
398   if (header->profile >= GST_VP9_PROFILE_2) {
399     VP9_READ_BIT (bit);
400     if (bit) {
401       header->bit_depth = GST_VP9_BIT_DEPTH_12;
402     } else {
403       header->bit_depth = GST_VP9_BIT_DEPTH_10;
404     }
405   } else {
406     header->bit_depth = GST_VP9_BIT_DEPTH_8;
407   }
408
409   VP9_READ_UINT8 (header->color_space, 3);
410   if (header->color_space != GST_VP9_CS_SRGB) {
411     VP9_READ_BIT (header->color_range);
412
413     if (header->profile == GST_VP9_PROFILE_1
414         || header->profile == GST_VP9_PROFILE_3) {
415       VP9_READ_BIT (header->subsampling_x);
416       VP9_READ_BIT (header->subsampling_y);
417
418       if (header->subsampling_x == 1 && header->subsampling_y == 1) {
419         GST_ERROR
420             ("4:2:0 subsampling is not supported in profile_1 or profile_3");
421         return GST_VP9_PARSER_ERROR;
422       }
423
424       /* reserved bit */
425       VP9_READ_BIT (bit);
426     } else {
427       header->subsampling_y = header->subsampling_x = 1;
428     }
429   } else {
430     header->color_range = GST_VP9_CR_FULL;
431     if (header->profile == GST_VP9_PROFILE_1
432         || header->profile == GST_VP9_PROFILE_3) {
433       /* reserved bit */
434       VP9_READ_BIT (bit);
435     } else {
436       GST_ERROR
437           ("4:4:4 subsampling is not supported in profile_0 and profile_2");
438       return GST_VP9_PARSER_ERROR;
439     }
440   }
441
442   self->bit_depth = header->bit_depth;
443   self->color_space = header->color_space;
444   self->subsampling_x = header->subsampling_x;
445   self->subsampling_y = header->subsampling_y;
446   self->color_range = header->color_range;
447
448   return GST_VP9_PARSER_OK;
449 }
450
451 /* 6.2 Uncompressed header syntax */
452 static GstVp9ParserResult
453 parse_profile (GstBitReader * br, guint8 * profile)
454 {
455   guint8 profile_low_bit, profile_high_bit, ret, bit;
456
457   VP9_READ_BIT (profile_low_bit);
458   VP9_READ_BIT (profile_high_bit);
459
460   ret = (profile_high_bit << 1) | profile_low_bit;
461   if (ret == 3) {
462     /* reserved bit */
463     VP9_READ_BIT (bit);
464   }
465
466   *profile = ret;
467
468   return GST_VP9_PARSER_OK;
469 }
470
471 /* 6.2.6 Compute image size syntax */
472 static void
473 compute_image_size (GstVp9StatefulParser * self, guint32 width, guint32 height)
474 {
475   self->mi_cols = (width + 7) >> 3;
476   self->mi_rows = (height + 7) >> 3;
477   self->sb64_cols = (self->mi_cols + 7) >> 3;
478   self->sb64_rows = (self->mi_rows + 7) >> 3;
479 }
480
481 static GstVp9ParserResult
482 parse_frame_or_render_size (GstBitReader * br,
483     guint32 * width, guint32 * height)
484 {
485   guint32 width_minus_1;
486   guint32 height_minus_1;
487
488   VP9_READ_UINT32 (width_minus_1, 16);
489   VP9_READ_UINT32 (height_minus_1, 16);
490
491   *width = width_minus_1 + 1;
492   *height = height_minus_1 + 1;
493
494   return GST_VP9_PARSER_OK;
495 }
496
497 /* 6.2.3 Frame size syntax */
498 static GstVp9ParserResult
499 parse_frame_size (GstVp9StatefulParser * self, GstBitReader * br,
500     guint32 * width, guint32 * height)
501 {
502   GstVp9ParserResult rst;
503
504   rst = parse_frame_or_render_size (br, width, height);
505   if (rst != GST_VP9_PARSER_OK) {
506     GST_ERROR ("Failed to parse frame size");
507     return rst;
508   }
509
510   compute_image_size (self, *width, *height);
511
512   return GST_VP9_PARSER_OK;
513 }
514
515 /* 6.2.4 Render size syntax */
516 static GstVp9ParserResult
517 parse_render_size (GstBitReader * br, GstVp9FrameHeader * header)
518 {
519   VP9_READ_BIT (header->render_and_frame_size_different);
520   if (header->render_and_frame_size_different) {
521     return parse_frame_or_render_size (br,
522         &header->render_width, &header->render_height);
523   } else {
524     header->render_width = header->width;
525     header->render_height = header->height;
526   }
527
528   return GST_VP9_PARSER_OK;
529 }
530
531 /* 6.2.5 Frame size with refs syntax */
532 static GstVp9ParserResult
533 parse_frame_size_with_refs (GstVp9StatefulParser * self, GstBitReader * br,
534     GstVp9FrameHeader * header)
535 {
536   guint8 found_ref = 0;
537   guint i;
538
539   for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
540     VP9_READ_BIT (found_ref);
541
542     if (found_ref) {
543       guint8 idx = header->ref_frame_idx[i];
544
545       header->width = self->reference[idx].width;
546       header->height = self->reference[idx].height;
547       break;
548     }
549   }
550
551   if (found_ref == 0) {
552     GstVp9ParserResult rst;
553
554     rst = parse_frame_size (self, br, &header->width, &header->height);
555     if (rst != GST_VP9_PARSER_OK) {
556       GST_ERROR ("Failed to parse frame size without refs");
557       return rst;
558     }
559   } else {
560     compute_image_size (self, header->width, header->height);
561   }
562
563   return parse_render_size (br, header);
564 }
565
566 /* 6.2.7 Interpolation filter syntax */
567 static GstVp9ParserResult
568 read_interpolation_filter (GstBitReader * br, GstVp9FrameHeader * header)
569 {
570   static const GstVp9InterpolationFilter filter_map[] = {
571     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SMOOTH,
572     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP,
573     GST_VP9_INTERPOLATION_FILTER_EIGHTTAP_SHARP,
574     GST_VP9_INTERPOLATION_FILTER_BILINEAR
575   };
576   guint8 is_filter_switchable;
577
578   VP9_READ_BIT (is_filter_switchable);
579   if (is_filter_switchable) {
580     header->interpolation_filter = GST_VP9_INTERPOLATION_FILTER_SWITCHABLE;
581   } else {
582     guint8 map_val;
583
584     VP9_READ_UINT8 (map_val, 2);
585     header->interpolation_filter = filter_map[map_val];
586   }
587
588   return GST_VP9_PARSER_OK;
589 }
590
591 /* 6.2.8 Loop filter params syntax */
592 static GstVp9ParserResult
593 parse_loop_filter_params (GstBitReader * br, GstVp9LoopFilterParams * params)
594 {
595   VP9_READ_UINT8 (params->loop_filter_level, 6);
596   VP9_READ_UINT8 (params->loop_filter_sharpness, 3);
597   VP9_READ_BIT (params->loop_filter_delta_enabled);
598
599   if (params->loop_filter_delta_enabled) {
600     VP9_READ_BIT (params->loop_filter_delta_update);
601     if (params->loop_filter_delta_update) {
602       guint i;
603
604       for (i = 0; i < GST_VP9_MAX_REF_LF_DELTAS; i++) {
605         VP9_READ_BIT (params->update_ref_delta[i]);
606         if (params->update_ref_delta[i]) {
607           VP9_READ_SIGNED_8 (params->loop_filter_ref_deltas[i], 6);
608         }
609       }
610
611       for (i = 0; i < GST_VP9_MAX_MODE_LF_DELTAS; i++) {
612         VP9_READ_BIT (params->update_mode_delta[i]);
613         if (params->update_mode_delta[i])
614           VP9_READ_SIGNED_8 (params->loop_filter_mode_deltas[i], 6);
615       }
616     }
617   }
618
619   return GST_VP9_PARSER_OK;
620 }
621
622 /* 6.2.10 Delta quantizer syntax */
623 static inline GstVp9ParserResult
624 parse_delta_q (GstBitReader * br, gint8 * value)
625 {
626   guint8 read_signed;
627   gint8 delta_q;
628
629   VP9_READ_BIT (read_signed);
630   if (!read_signed) {
631     *value = 0;
632     return GST_VP9_PARSER_OK;
633   }
634
635   VP9_READ_SIGNED_8 (delta_q, 4);
636   *value = delta_q;
637
638   return GST_VP9_PARSER_OK;
639 }
640
641 /* 6.2.9 Quantization params syntax */
642 static GstVp9ParserResult
643 parse_quantization_params (GstBitReader * br, GstVp9FrameHeader * header)
644 {
645   GstVp9QuantizationParams *params = &header->quantization_params;
646   GstVp9ParserResult rst;
647
648   VP9_READ_UINT8 (params->base_q_idx, 8);
649   rst = parse_delta_q (br, &params->delta_q_y_dc);
650   if (rst != GST_VP9_PARSER_OK)
651     return rst;
652
653   rst = parse_delta_q (br, &params->delta_q_uv_dc);
654   if (rst != GST_VP9_PARSER_OK)
655     return rst;
656
657   rst = parse_delta_q (br, &params->delta_q_uv_ac);
658   if (rst != GST_VP9_PARSER_OK)
659     return rst;
660
661   header->lossless_flag = params->base_q_idx == 0 && params->delta_q_y_dc == 0
662       && params->delta_q_uv_dc == 0 && params->delta_q_uv_ac == 0;
663
664   return GST_VP9_PARSER_OK;
665 }
666
667 /* 6.2.12 Probability syntax */
668 static GstVp9ParserResult
669 read_prob (GstBitReader * br, guint8 * val)
670 {
671   guint8 prob = GST_VP9_MAX_PROB;
672   guint8 prob_coded;
673
674   VP9_READ_BIT (prob_coded);
675
676   if (prob_coded)
677     VP9_READ_UINT8 (prob, 8);
678
679   *val = prob;
680
681   return GST_VP9_PARSER_OK;
682 }
683
684 /* 6.2.11 Segmentation params syntax */
685 static GstVp9ParserResult
686 parse_segmentation_params (GstBitReader * br, GstVp9SegmentationParams * params)
687 {
688   guint i;
689   GstVp9ParserResult rst;
690
691   params->segmentation_update_map = 0;
692   params->segmentation_update_data = 0;
693   params->segmentation_temporal_update = 0;
694
695   VP9_READ_BIT (params->segmentation_enabled);
696   if (!params->segmentation_enabled)
697     return GST_VP9_PARSER_OK;
698
699   VP9_READ_BIT (params->segmentation_update_map);
700   if (params->segmentation_update_map) {
701     for (i = 0; i < GST_VP9_SEG_TREE_PROBS; i++) {
702       rst = read_prob (br, &params->segmentation_tree_probs[i]);
703       if (rst != GST_VP9_PARSER_OK) {
704         GST_ERROR ("Failed to read segmentation_tree_probs[%d]", i);
705         return rst;
706       }
707     }
708
709     VP9_READ_BIT (params->segmentation_temporal_update);
710     if (params->segmentation_temporal_update) {
711       for (i = 0; i < GST_VP9_PREDICTION_PROBS; i++) {
712         rst = read_prob (br, &params->segmentation_pred_prob[i]);
713         if (rst != GST_VP9_PARSER_OK) {
714           GST_ERROR ("Failed to read segmentation_pred_prob[%d]", i);
715           return rst;
716         }
717       }
718     } else {
719       for (i = 0; i < GST_VP9_PREDICTION_PROBS; i++)
720         params->segmentation_pred_prob[i] = GST_VP9_MAX_PROB;
721     }
722   }
723
724   VP9_READ_BIT (params->segmentation_update_data);
725   if (params->segmentation_update_data) {
726     VP9_READ_BIT (params->segmentation_abs_or_delta_update);
727
728     for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
729       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_Q]);
730       if (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_Q]) {
731         VP9_READ_SIGNED_16 (params->feature_data[i][GST_VP9_SEG_LVL_ALT_Q], 8);
732       } else {
733         params->feature_data[i][GST_VP9_SEG_LVL_ALT_Q] = 0;
734       }
735
736       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_L]);
737       if (params->feature_enabled[i][GST_VP9_SEG_LVL_ALT_L]) {
738         VP9_READ_SIGNED_8 (params->feature_data[i][GST_VP9_SEG_LVL_ALT_L], 6);
739       } else {
740         params->feature_data[i][GST_VP9_SEG_LVL_ALT_L] = 0;
741       }
742
743       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME]);
744       if (params->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME]) {
745         guint8 val;
746
747         VP9_READ_UINT8 (val, 2);
748         params->feature_data[i][GST_VP9_SEG_LVL_REF_FRAME] = val;
749       } else {
750         params->feature_data[i][GST_VP9_SEG_LVL_REF_FRAME] = 0;
751       }
752
753       VP9_READ_BIT (params->feature_enabled[i][GST_VP9_SEG_SEG_LVL_SKIP]);
754     }
755   }
756
757   return GST_VP9_PARSER_OK;
758 }
759
760 /* 6.2.14 Tile size calculation */
761 static guint
762 calc_min_log2_tile_cols (guint32 sb64_cols)
763 {
764   guint minLog2 = 0;
765   static const guint MAX_TILE_WIDTH_B64 = 64;
766
767   while ((MAX_TILE_WIDTH_B64 << minLog2) < sb64_cols)
768     minLog2++;
769
770   return minLog2;
771 }
772
773 static guint
774 calc_max_log2_tile_cols (guint32 sb64_cols)
775 {
776   guint maxLog2 = 1;
777   static const guint MIN_TILE_WIDTH_B64 = 4;
778
779   while ((sb64_cols >> maxLog2) >= MIN_TILE_WIDTH_B64)
780     maxLog2++;
781
782   return maxLog2 - 1;
783 }
784
785 /* 6.2.13 Tile info syntax */
786 static GstVp9ParserResult
787 parse_tile_info (GstVp9StatefulParser * self, GstBitReader * br,
788     GstVp9FrameHeader * header)
789 {
790   guint32 minLog2TileCols = calc_min_log2_tile_cols (self->sb64_cols);
791   guint32 maxLog2TileCols = calc_max_log2_tile_cols (self->sb64_cols);
792
793   header->tile_cols_log2 = minLog2TileCols;
794
795   while (header->tile_cols_log2 < maxLog2TileCols) {
796     guint8 increment_tile_cols_log2;
797
798     VP9_READ_BIT (increment_tile_cols_log2);
799     if (increment_tile_cols_log2)
800       header->tile_cols_log2++;
801     else
802       break;
803   }
804
805   if (header->tile_cols_log2 > 6) {
806     GST_ERROR ("Invalid number of tile columns");
807     return GST_VP9_PARSER_ERROR;
808   }
809
810   VP9_READ_BIT (header->tile_rows_log2);
811   if (header->tile_rows_log2) {
812     guint8 increment_tile_rows_log2;
813
814     VP9_READ_BIT (increment_tile_rows_log2);
815     header->tile_rows_log2 += increment_tile_rows_log2;
816   }
817
818   return GST_VP9_PARSER_OK;
819 }
820
821 /* 7.2 Uncompressed header semantics */
822 static void
823 setup_past_independence (GstVp9StatefulParser * self,
824     GstVp9FrameHeader * header)
825 {
826   memset (self->segmentation_params.feature_enabled,
827       0, sizeof (self->segmentation_params.feature_enabled));
828   memset (self->segmentation_params.feature_data,
829       0, sizeof (self->segmentation_params.feature_data));
830
831   self->segmentation_params.segmentation_abs_or_delta_update = 0;
832
833   self->loop_filter_params.loop_filter_delta_enabled = 1;
834   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_INTRA] = 1;
835   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_LAST] = 0;
836   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_GOLDEN] =
837       -1;
838   self->loop_filter_params.loop_filter_ref_deltas[GST_VP9_REF_FRAME_ALTREF] =
839       -1;
840
841   memset (self->loop_filter_params.loop_filter_mode_deltas, 0,
842       sizeof (self->loop_filter_params.loop_filter_mode_deltas));
843   memset (header->ref_frame_sign_bias, 0, sizeof (header->ref_frame_sign_bias));
844 }
845
846 /**
847  * gst_vp9_stateful_parser_new:
848  *
849  * Creates a new #GstVp9StatefulParser. It should be freed with
850  * gst_vp9_stateful_parser_free() after use.
851  *
852  * Returns: a new #GstVp9StatefulParser
853  *
854  * Since: 1.20
855  */
856 GstVp9StatefulParser *
857 gst_vp9_stateful_parser_new (void)
858 {
859   GstVp9StatefulParser *parser;
860
861   parser = g_new0 (GstVp9StatefulParser, 1);
862
863   return parser;
864 }
865
866 /**
867  * gst_vp9_stateful_parser_free:
868  * @parser: the #GstVp9StatefulParser to free
869  *
870  * Frees @parser.
871  *
872  * Since: 1.20
873  */
874 void
875 gst_vp9_stateful_parser_free (GstVp9StatefulParser * parser)
876 {
877   g_free (parser);
878 }
879
880 /**
881  * gst_vp9_stateful_parser_parse_frame_header:
882  * @parser: The #GstVp9StatefulParser
883  * @header: The #GstVp9FrameHeader to fill
884  * @data: The data to parse
885  * @size: The size of the @data to parse
886  *
887  * Parses the VP9 bitstream contained in @data, and fills in @header
888  * with the information. The @size argument represent the whole frame size.
889  *
890  * Returns: a #GstVp9ParserResult
891  *
892  * Since: 1.20
893  */
894 GstVp9ParserResult
895 gst_vp9_stateful_parser_parse_frame_header (GstVp9StatefulParser * parser,
896     GstVp9FrameHeader * header, const guint8 * data, gsize size)
897 {
898   GstBitReader bit_reader;
899   GstBitReader *br = &bit_reader;
900   gboolean frame_is_intra = FALSE;
901   GstVp9ParserResult rst = GST_VP9_PARSER_OK;
902   guint i;
903
904   g_return_val_if_fail (parser, GST_VP9_PARSER_ERROR);
905   g_return_val_if_fail (header, GST_VP9_PARSER_ERROR);
906   g_return_val_if_fail (data, GST_VP9_PARSER_ERROR);
907   g_return_val_if_fail (size, GST_VP9_PARSER_ERROR);
908
909   gst_bit_reader_init (br, data, size);
910   memset (header, 0, sizeof (GstVp9FrameHeader));
911
912   /* Parsing Uncompressed Data Chunk */
913   rst = parse_frame_marker (br);
914   if (rst != GST_VP9_PARSER_OK)
915     return rst;
916
917   rst = parse_profile (br, &header->profile);
918   if (rst != GST_VP9_PARSER_OK)
919     return rst;
920
921   CHECK_ALLOWED (header->profile, GST_VP9_PROFILE_0, GST_VP9_PROFILE_3);
922
923   VP9_READ_BIT (header->show_existing_frame);
924   if (header->show_existing_frame) {
925     VP9_READ_UINT8 (header->frame_to_show_map_idx, 3);
926     return GST_VP9_PARSER_OK;
927   }
928
929   VP9_READ_BIT (header->frame_type);
930   VP9_READ_BIT (header->show_frame);
931   VP9_READ_BIT (header->error_resilient_mode);
932
933   if (header->frame_type == GST_VP9_KEY_FRAME) {
934     rst = parse_frame_sync_code (br);
935     if (rst != GST_VP9_PARSER_OK) {
936       GST_ERROR ("Invalid VP9 sync code in keyframe");
937       return rst;
938     }
939
940     rst = parse_color_config (parser, br, header);
941     if (rst != GST_VP9_PARSER_OK) {
942       GST_ERROR ("Failed to parse color config of keyframe");
943       return rst;
944     }
945
946     rst = parse_frame_size (parser, br, &header->width, &header->height);
947     if (rst != GST_VP9_PARSER_OK) {
948       GST_ERROR ("Failed to parse frame size of keyframe");
949       return rst;
950     }
951
952     rst = parse_render_size (br, header);
953     if (rst != GST_VP9_PARSER_OK) {
954       GST_ERROR ("Failed to parse render size of keyframe");
955       return rst;
956     }
957
958     header->refresh_frame_flags = 0xff;
959     frame_is_intra = TRUE;
960   } else {
961     if (header->show_frame == 0)
962       VP9_READ_BIT (header->intra_only);
963
964     frame_is_intra = header->intra_only;
965     if (header->error_resilient_mode == 0)
966       VP9_READ_UINT8 (header->reset_frame_context, 2);
967
968     if (header->intra_only) {
969       rst = parse_frame_sync_code (br);
970       if (rst != GST_VP9_PARSER_OK) {
971         GST_ERROR ("Invalid VP9 sync code in intra-only frame");
972         return rst;
973       }
974
975       if (header->profile > GST_VP9_PROFILE_0) {
976         rst = parse_color_config (parser, br, header);
977         if (rst != GST_VP9_PARSER_OK) {
978           GST_ERROR ("Failed to parse color config of intra-only frame");
979           return rst;
980         }
981       } else {
982         parser->color_space = header->color_space = GST_VP9_CS_BT_601;
983         parser->color_range = header->color_range = GST_VP9_CR_LIMITED;
984         parser->subsampling_x = parser->subsampling_y =
985             header->subsampling_x = header->subsampling_y = 1;
986         parser->bit_depth = header->bit_depth = GST_VP9_BIT_DEPTH_8;
987       }
988
989       VP9_READ_UINT8 (header->refresh_frame_flags, 8);
990       rst = parse_frame_size (parser, br, &header->width, &header->height);
991       if (rst != GST_VP9_PARSER_OK) {
992         GST_ERROR ("Failed to pase frame size of intra-only frame");
993         return rst;
994       }
995
996       rst = parse_render_size (br, header);
997       if (rst != GST_VP9_PARSER_OK) {
998         GST_ERROR ("Failed to parse render size of intra-only frame");
999         return rst;
1000       }
1001     } else {
1002       /* copy color_config from previously parsed one */
1003       header->color_space = parser->color_space;
1004       header->color_range = parser->color_range;
1005       header->subsampling_x = parser->subsampling_x;
1006       header->subsampling_y = parser->subsampling_y;
1007       header->bit_depth = parser->bit_depth;
1008
1009       VP9_READ_UINT8 (header->refresh_frame_flags, 8);
1010       for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
1011         VP9_READ_UINT8 (header->ref_frame_idx[i], 3);
1012         VP9_READ_BIT (header->ref_frame_sign_bias[GST_VP9_REF_FRAME_LAST + i]);
1013       }
1014
1015       rst = parse_frame_size_with_refs (parser, br, header);
1016       if (rst != GST_VP9_PARSER_OK) {
1017         GST_ERROR ("Failed to parse frame size with refs");
1018         return rst;
1019       }
1020
1021       VP9_READ_BIT (header->allow_high_precision_mv);
1022       rst = read_interpolation_filter (br, header);
1023       if (rst != GST_VP9_PARSER_OK) {
1024         GST_ERROR ("Failed to read interpolation filter information");
1025         return rst;
1026       }
1027     }
1028   }
1029
1030   if (!header->error_resilient_mode) {
1031     VP9_READ_BIT (header->refresh_frame_context);
1032     VP9_READ_BIT (header->frame_parallel_decoding_mode);
1033   } else {
1034     header->refresh_frame_context = 0;
1035     header->frame_parallel_decoding_mode = 1;
1036   }
1037
1038   VP9_READ_UINT8 (header->frame_context_idx, 2);
1039
1040   if (frame_is_intra || header->error_resilient_mode)
1041     setup_past_independence (parser, header);
1042
1043   /* First update our own table, and we will copy to frame header later */
1044   rst = parse_loop_filter_params (br, &parser->loop_filter_params);
1045   if (rst != GST_VP9_PARSER_OK) {
1046     GST_ERROR ("Failed to parse loop filter params");
1047     return rst;
1048   }
1049
1050   rst = parse_quantization_params (br, header);
1051   if (rst != GST_VP9_PARSER_OK) {
1052     GST_ERROR ("Failed to parse quantization params");
1053     return rst;
1054   }
1055
1056   /* Also update our own table, then it will be copied later */
1057   rst = parse_segmentation_params (br, &parser->segmentation_params);
1058   if (rst != GST_VP9_PARSER_OK) {
1059     GST_ERROR ("Failed to parse segmentation params");
1060     return rst;
1061   }
1062
1063   rst = parse_tile_info (parser, br, header);
1064   if (rst != GST_VP9_PARSER_OK) {
1065     GST_ERROR ("Failed to parse tile info");
1066     return rst;
1067   }
1068
1069   VP9_READ_UINT16 (header->header_size_in_bytes, 16);
1070   if (!header->header_size_in_bytes) {
1071     GST_ERROR ("Failed to parse header size in bytes");
1072     return GST_VP9_PARSER_ERROR;
1073   }
1074
1075   /* copy our values to header */
1076   memcpy (&header->loop_filter_params, &parser->loop_filter_params,
1077       sizeof (GstVp9LoopFilterParams));
1078   memcpy (&header->segmentation_params, &parser->segmentation_params,
1079       sizeof (GstVp9SegmentationParams));
1080
1081   /* And update reference frames */
1082   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
1083     guint8 flag = (1 << i);
1084     if ((header->refresh_frame_flags & flag) != 0) {
1085       parser->reference[i].width = header->width;
1086       parser->reference[i].height = header->height;
1087     }
1088   }
1089
1090   header->frame_header_length_in_bytes = (gst_bit_reader_get_pos (br) + 7) / 8;
1091
1092   return GST_VP9_PARSER_OK;
1093 }
1094
1095 /**
1096  * gst_vp9_seg_feature_active:
1097  * @params: a #GstVp9SegmentationParams
1098  * @segment_id: a segment id
1099  * @feature: a segmentation feature
1100  *
1101  * An implementation of "seg_feature_active" function specified in
1102  * "6.4.9 Segmentation feature active syntax"
1103  *
1104  * Returns: %TRUE if feature is active
1105  *
1106  * Since: 1.20
1107  */
1108 gboolean
1109 gst_vp9_seg_feature_active (const GstVp9SegmentationParams * params,
1110     guint8 segment_id, guint8 feature)
1111 {
1112   g_return_val_if_fail (params != NULL, FALSE);
1113   g_return_val_if_fail (segment_id < GST_VP9_MAX_SEGMENTS, FALSE);
1114   g_return_val_if_fail (feature < GST_VP9_SEG_LVL_MAX, FALSE);
1115
1116   return params->segmentation_enabled &&
1117       params->feature_enabled[segment_id][feature];
1118 }
1119
1120 /**
1121  * gst_vp9_get_qindex:
1122  * @segmentation_params: a #GstVp9SegmentationParams
1123  * @quantization_params: a #GstVp9QuantizationParams
1124  * @segment_id: a segment id
1125  *
1126  * An implementation of "get_qindex" function specfied in
1127  * "8.6.1 Dequantization functions"
1128  *
1129  * Returns: the quantizer index
1130  *
1131  * Since: 1.20
1132  */
1133 guint8
1134 gst_vp9_get_qindex (const GstVp9SegmentationParams * segmentation_params,
1135     const GstVp9QuantizationParams * quantization_params, guint8 segment_id)
1136 {
1137   guint8 base_q_index;
1138
1139   g_return_val_if_fail (segmentation_params != NULL, 0);
1140   g_return_val_if_fail (quantization_params != NULL, 0);
1141   g_return_val_if_fail (segment_id < GST_VP9_MAX_SEGMENTS, 0);
1142
1143   base_q_index = quantization_params->base_q_idx;
1144
1145   if (gst_vp9_seg_feature_active (segmentation_params, segment_id,
1146           GST_VP9_SEG_LVL_ALT_Q)) {
1147     gint data =
1148         segmentation_params->feature_data[segment_id][GST_VP9_SEG_LVL_ALT_Q];
1149
1150     if (!segmentation_params->segmentation_abs_or_delta_update)
1151       data += base_q_index;
1152
1153     return CLAMP (data, 0, 255);
1154   }
1155
1156   return base_q_index;
1157 }
1158
1159 /**
1160  * gst_vp9_get_dc_quant:
1161  * @qindex: the quantizer index
1162  * @delta_q_dc: a delta_q_dc value
1163  * @bit_depth: coded bit depth
1164  *
1165  * An implementation of "dc_q" function specified in
1166  * "8.6.1 Dequantization functions"
1167  *
1168  * Returns: the quantizer value for the dc coefficient
1169  *
1170  * Since: 1.20
1171  */
1172 gint16
1173 gst_vp9_get_dc_quant (guint8 qindex, gint8 delta_q_dc, guint8 bit_depth)
1174 {
1175   guint8 q_table_idx = CLAMP (qindex + delta_q_dc, 0, 255);
1176
1177   switch (bit_depth) {
1178     case 8:
1179       return dc_qlookup[q_table_idx];
1180     case 10:
1181       return dc_qlookup_10[q_table_idx];
1182     case 12:
1183       return dc_qlookup_12[q_table_idx];
1184     default:
1185       GST_WARNING ("Unhandled bitdepth %d", bit_depth);
1186       break;
1187   }
1188
1189   return -1;
1190 }
1191
1192 /**
1193  * gst_vp9_get_ac_quant:
1194  * @qindex: the quantizer index
1195  * @delta_q_ac: a delta_q_ac value
1196  * @bit_depth: coded bit depth
1197  *
1198  * An implementation of "ac_q" function specified in
1199  * "8.6.1 Dequantization functions"
1200  *
1201  * Returns: the quantizer value for the ac coefficient
1202  *
1203  * Since: 1.20
1204  */
1205 gint16
1206 gst_vp9_get_ac_quant (guint8 qindex, gint8 delta_q_ac, guint8 bit_depth)
1207 {
1208   guint8 q_table_idx = CLAMP (qindex + delta_q_ac, 0, 255);
1209
1210   switch (bit_depth) {
1211     case 8:
1212       return ac_qlookup[q_table_idx];
1213     case 10:
1214       return ac_qlookup_10[q_table_idx];
1215     case 12:
1216       return ac_qlookup_12[q_table_idx];
1217     default:
1218       GST_WARNING ("Unhandled bitdepth %d", bit_depth);
1219       break;
1220   }
1221
1222   return -1;
1223 }