1 /* gstav1parser.c
2 *
3 * Copyright (C) 2018 Georg Ottinger
4 * Copyright (C) 2019-2020 Intel Corporation
5 * Author: Georg Ottinger <g.ottinger@gmx.at>
6 * Author: Junyan He <junyan.he@hotmail.com>
7 * Author: Victor Jaquez <vjaquez@igalia.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24 /**
25 * SECTION:gstav1parser
26 * @title: GstAV1Parser
27 * @short_description: Convenience library for parsing AV1 video bitstream.
28 *
29 * For more details about the structures, you can refer to the AV1 Bitstream &
30 * Decoding Process Specification V1.0.0
31 * [specification](https://aomediacodec.github.io/av1-spec/av1-spec.pdf)
32 *
33 * It offers you bitstream parsing of low overhead bistream format (Section 5)
34 * or Annex B according to the setting of the parser. By calling the function of
35 * gst_av1_parser_reset(), user can switch between bistream mode and Annex B mode.
36 *
37 * To retrieve OBUs and parse its headers, you should firstly call the function of
38 * gst_av1_parser_identify_one_obu() to get the OBU type if succeeds or just discard
39 * the data if fails.
40 *
41 * Then, depending on the #GstAV1OBUType of the newly parsed #GstAV1OBU,
42 * you should call the differents functions to parse the structure details:
43 *
44 * * #GST_AV1_OBU_SEQUENCE_HEADER: gst_av1_parser_parse_sequence_header_obu()
45 *
46 * * #GST_AV1_OBU_TEMPORAL_DELIMITER: gst_av1_parser_parse_temporal_delimiter_obu()
47 *
48 * * #GST_AV1_OBU_FRAME: gst_av1_parser_parse_frame_obu()
49 *
50 * * #GST_AV1_OBU_FRAME_HEADER: gst_av1_parser_parse_frame_header_obu()
51 *
52 * * #GST_AV1_OBU_TILE_GROUP: gst_av1_parser_parse_tile_group_obu()
53 *
54 * * #GST_AV1_OBU_METADATA: gst_av1_parser_parse_metadata_obu()
55 *
56 * * #GST_AV1_OBU_REDUNDANT_FRAME_HEADER: gst_av1_parser_parse_frame_header_obu()
57 *
58 * * #GST_AV1_OBU_TILE_LIST: gst_av1_parser_parse_tile_list_obu()
59 *
60 * Note: Some parser functions are dependent on information provided in the sequence
61 * header and reference frame's information. It maintains a state inside itself, which
62 * contains all global vars and reference information during the whole parsing process.
63 * Calling gst_av1_parser_reset() or a new sequence's arriving can clear and reset this
64 * inside state.
65 *
66 * After successfully handled a frame(for example, decode a frame successfully), you
67 * should call gst_av1_parser_reference_frame_update() to update the parser's inside
68 * state(such as reference information, global segmentation information, etc).
69 *
70 * Note: If the frame is actived by show_existing_frame in #GST_AV1_OBU_FRAME_HEADER,
71 * the function of gst_av1_parser_reference_frame_loading() should be called before
72 * really showing that frame.
73 *
74 * @since: 1.18.00
75 */
76
77 #ifdef HAVE_CONFIG_H
78 #include "config.h"
79 #endif
80
81 #include "gstav1parser.h"
82
83 #include <gst/base/gstbitreader.h>
84
85 #include <string.h>
86 #include <stdlib.h>
87
88 #ifndef GST_DISABLE_GST_DEBUG
89 #define GST_CAT_DEFAULT gst_av1_debug_category_get ()
90 static GstDebugCategory *
gst_av1_debug_category_get(void)91 gst_av1_debug_category_get (void)
92 {
93 static gsize cat_gonce = 0;
94
95 if (g_once_init_enter (&cat_gonce)) {
96 GstDebugCategory *cat = NULL;
97
98 GST_DEBUG_CATEGORY_INIT (cat, "codecparsers_av1", 0, "av1 parse library");
99
100 g_once_init_leave (&cat_gonce, (gsize) cat);
101 }
102
103 return (GstDebugCategory *) cat_gonce;
104 }
105 #endif /* GST_DISABLE_GST_DEBUG */
106
107 #define AV1_READ_BIT(br) ((guint8) gst_bit_reader_get_bits_uint32_unchecked (br, 1))
108 #define AV1_READ_UINT8(br) ((guint8) gst_bit_reader_get_bits_uint32_unchecked (br, 8))
109 #define AV1_READ_UINT16(br) ((guint16) gst_bit_reader_get_bits_uint32_unchecked (br, 16))
110 #define AV1_READ_UINT32(br) gst_bit_reader_get_bits_uint32_unchecked (br, 32)
111 #define AV1_READ_UINT64(br) gst_bit_reader_get_bits_uint64_unchecked (br, 64)
112 #define AV1_READ_BITS(br, nbits) \
113 ((nbits <= 32) ? (gst_bit_reader_get_bits_uint32_unchecked (br, nbits)) : \
114 (gst_bit_reader_get_bits_uint64_unchecked (br, nbits)))
115
116 static guint64
av1_read_bits_checked(GstBitReader * br,guint nbits,GstAV1ParserResult * retval,const char * func_name,gint line)117 av1_read_bits_checked (GstBitReader * br, guint nbits,
118 GstAV1ParserResult * retval, const char *func_name, gint line)
119 {
120 guint64 read_bits = 0;
121 gboolean result;
122
123 if (nbits <= 64)
124 result = gst_bit_reader_get_bits_uint64 (br, &read_bits, nbits);
125 else
126 result = FALSE;
127
128 if (result == TRUE) {
129 *retval = GST_AV1_PARSER_OK;
130 return read_bits;
131 } else {
132 *retval = GST_AV1_PARSER_NO_MORE_DATA;
133 GST_WARNING ("Read %d bits failed in func: %s, line %d", nbits, func_name,
134 line);
135 return 0;
136 }
137 }
138
139 #define AV1_READ_BIT_CHECKED(br, ret) \
140 ((guint8) av1_read_bits_checked (br, 1, ret, __func__, __LINE__))
141 #define AV1_READ_UINT8_CHECKED(br, ret) \
142 ((guint8) av1_read_bits_checked (br, 8, ret, __func__, __LINE__))
143 #define AV1_READ_UINT16_CHECKED(br, ret) \
144 ((guint16) av1_read_bits_checked (br, 16, ret, __func__, __LINE__))
145 #define AV1_READ_UINT32_CHECKED(br, ret) \
146 ((guint32) av1_read_bits_checked (br, 32, ret, __func__, __LINE__))
147 #define AV1_READ_BITS_CHECKED(br, nbits, ret) \
148 av1_read_bits_checked (br, nbits, ret, __func__, __LINE__)
149
150 #define AV1_REMAINING_BYTES(br) (gst_bit_reader_get_remaining (br) / 8)
151 #define AV1_REMAINING_BITS(br) (gst_bit_reader_get_remaining (br))
152
153 /*************************************
154 * *
155 * Helperfunctions *
156 * *
157 *************************************/
158
159 /* 4.7
160 *
161 * floor of the base 2 logarithm of the input x */
162 static gint
av1_helpers_floor_log2(guint32 x)163 av1_helpers_floor_log2 (guint32 x)
164 {
165 gint s = 0;
166
167 while (x != 0) {
168 x = x >> 1;
169 s++;
170 }
171 return s - 1;
172 }
173
174 /* 5.9.16 Tile size calculation
175 *
176 * returns the smallest value for k such that blkSize << k is greater
177 * than or equal to target */
178 static gint
av1_helper_tile_log2(gint blkSize,gint target)179 av1_helper_tile_log2 (gint blkSize, gint target)
180 {
181 gint k;
182 for (k = 0; (blkSize << k) < target; k++);
183 return k;
184 }
185
186 /* 5.9.29 */
187 static gint
av1_helper_inverse_recenter(gint r,gint v)188 av1_helper_inverse_recenter (gint r, gint v)
189 {
190 if (v > 2 * r)
191 return v;
192 else if (v & 1)
193 return r - ((v + 1) >> 1);
194 else
195 return r + (v >> 1);
196 }
197
198 /* Shift down with rounding for use when n >= 0, value >= 0 */
199 static guint64
av1_helper_round_power_of_two(guint64 value,guint16 n)200 av1_helper_round_power_of_two (guint64 value, guint16 n)
201 {
202 return (value + (((guint64) (1) << n) >> 1)) >> n;
203 }
204
205 /* Shift down with rounding for signed integers, for use when n >= 0 */
206 static gint64
av1_helper_round_power_of_two_signed(gint64 value,guint16 n)207 av1_helper_round_power_of_two_signed (gint64 value, guint16 n)
208 {
209 return (value < 0) ? -((gint64) (av1_helper_round_power_of_two (-value, n)))
210 : (gint64) av1_helper_round_power_of_two (value, n);
211 }
212
213 static gint
av1_helper_msb(guint n)214 av1_helper_msb (guint n)
215 {
216 int log = 0;
217 guint value = n;
218 int i;
219
220 g_assert (n != 0);
221
222 for (i = 4; i >= 0; --i) {
223 const gint shift = (1 << i);
224 const guint x = value >> shift;
225 if (x != 0) {
226 value = x;
227 log += shift;
228 }
229 }
230 return log;
231 }
232
233 static const guint16 div_lut[GST_AV1_DIV_LUT_NUM + 1] = {
234 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
235 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
236 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
237 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
238 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
239 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
240 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
241 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
242 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
243 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
244 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
245 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
246 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
247 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
248 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
249 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
250 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
251 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
252 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
253 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
254 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
255 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
256 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
257 8240, 8224, 8208, 8192,
258 };
259
260 static gint16
av1_helper_resolve_divisor_32(guint32 D,gint16 * shift)261 av1_helper_resolve_divisor_32 (guint32 D, gint16 * shift)
262 {
263 gint32 f;
264 gint32 e;
265
266 *shift = av1_helper_msb (D);
267 // e is obtained from D after resetting the most significant 1 bit.
268 e = D - ((guint32) 1 << *shift);
269 // Get the most significant DIV_LUT_BITS (8) bits of e into f
270 if (*shift > GST_AV1_DIV_LUT_BITS)
271 f = av1_helper_round_power_of_two (e, *shift - GST_AV1_DIV_LUT_BITS);
272 else
273 f = e << (GST_AV1_DIV_LUT_BITS - *shift);
274 g_assert (f <= GST_AV1_DIV_LUT_NUM);
275 *shift += GST_AV1_DIV_LUT_PREC_BITS;
276 // Use f as lookup into the precomputed table of multipliers
277 return div_lut[f];
278 }
279
280 /*************************************
281 * *
282 * Bitstream Functions *
283 * *
284 *************************************/
285 /* 4.10.5
286 *
287 * Unsigned integer represented by a variable number of little-endian
288 * bytes. */
289 static guint32
av1_bitstreamfn_leb128(GstBitReader * br,GstAV1ParserResult * retval)290 av1_bitstreamfn_leb128 (GstBitReader * br, GstAV1ParserResult * retval)
291 {
292 guint8 leb128_byte = 0;
293 guint64 value = 0;
294 gint i;
295
296 for (i = 0; i < 8; i++) {
297 leb128_byte = AV1_READ_UINT8_CHECKED (br, retval);
298 if (*retval != GST_AV1_PARSER_OK)
299 return 0;
300
301 value |= (((gint) leb128_byte & 0x7f) << (i * 7));
302 if (!(leb128_byte & 0x80))
303 break;
304 }
305
306 /* check for bitstream conformance see chapter4.10.5 */
307 if (value < G_MAXUINT32) {
308 return (guint32) value;
309 } else {
310 GST_WARNING ("invalid leb128");
311 *retval = GST_AV1_PARSER_BITSTREAM_ERROR;
312 return 0;
313 }
314 }
315
316 /* 4.10.3
317 *
318 * Variable length unsigned n-bit number appearing directly in the
319 * bitstream */
320 static guint32
av1_bitstreamfn_uvlc(GstBitReader * br,GstAV1ParserResult * retval)321 av1_bitstreamfn_uvlc (GstBitReader * br, GstAV1ParserResult * retval)
322 {
323 guint8 leadingZero = 0;
324 guint32 readv;
325 guint32 value;
326 gboolean done;
327
328 while (1) {
329 done = AV1_READ_BIT_CHECKED (br, retval);
330 if (*retval != GST_AV1_PARSER_OK) {
331 GST_WARNING ("invalid uvlc");
332 return 0;
333 }
334
335 if (done)
336 break;
337 leadingZero++;
338 }
339
340 if (leadingZero >= 32) {
341 value = G_MAXUINT32;
342 return value;
343 }
344 readv = AV1_READ_BITS_CHECKED (br, leadingZero, retval);
345 if (*retval != GST_AV1_PARSER_OK) {
346 GST_WARNING ("invalid uvlc");
347 return 0;
348 }
349
350 value = readv + (1 << leadingZero) - 1;
351
352 return value;
353 }
354
355 /* 4.10.6
356 *
357 * Signed integer converted from an n bits unsigned integer in the
358 * bitstream */
359 static guint32
av1_bitstreamfn_su(GstBitReader * br,guint8 n,GstAV1ParserResult * retval)360 av1_bitstreamfn_su (GstBitReader * br, guint8 n, GstAV1ParserResult * retval)
361 {
362 guint32 v, signMask;
363
364 v = AV1_READ_BITS_CHECKED (br, n, retval);
365 if (*retval != GST_AV1_PARSER_OK)
366 return 0;
367
368 signMask = 1 << (n - 1);
369 if (v & signMask)
370 return v - 2 * signMask;
371 else
372 return v;
373 }
374
375 /* 4.10.7
376 *
377 * Unsigned encoded integer with maximum number of values n */
378 static guint32
av1_bitstreamfn_ns(GstBitReader * br,guint32 n,GstAV1ParserResult * retval)379 av1_bitstreamfn_ns (GstBitReader * br, guint32 n, GstAV1ParserResult * retval)
380 {
381 gint w, m, v;
382 gint extra_bit;
383
384 w = av1_helpers_floor_log2 (n) + 1;
385 m = (1 << w) - n;
386 v = AV1_READ_BITS_CHECKED (br, w - 1, retval);
387 if (*retval != GST_AV1_PARSER_OK)
388 return 0;
389
390 if (v < m)
391 return v;
392 extra_bit = AV1_READ_BITS_CHECKED (br, 1, retval);
393 if (*retval != GST_AV1_PARSER_OK)
394 return 0;
395
396 return (v << 1) - m + extra_bit;
397 }
398
399 /* 4.10.4
400 *
401 * Unsigned little-endian n-byte number appearing directly in the
402 * bitstream */
403 static guint
av1_bitstreamfn_le(GstBitReader * br,guint8 n,GstAV1ParserResult * retval)404 av1_bitstreamfn_le (GstBitReader * br, guint8 n, GstAV1ParserResult * retval)
405 {
406 guint t = 0;
407 guint8 byte;
408 gint i;
409
410 for (i = 0; i < n; i++) {
411 byte = AV1_READ_BITS_CHECKED (br, 8, retval);
412 if (*retval != GST_AV1_PARSER_OK)
413 return 0;
414
415 t += (byte << (i * 8));
416 }
417 return t;
418 }
419
420 /* 5.9.13
421 *
422 * Delta quantizer */
423 static gint8
av1_bitstreamfn_delta_q(GstBitReader * br,GstAV1ParserResult * retval)424 av1_bitstreamfn_delta_q (GstBitReader * br, GstAV1ParserResult * retval)
425 {
426 guint8 delta_coded;
427
428 delta_coded = AV1_READ_BIT_CHECKED (br, retval);
429 if (*retval != GST_AV1_PARSER_OK)
430 return 0;
431
432 if (delta_coded) {
433 gint delta_q = av1_bitstreamfn_su (br, 7, retval);
434 if (*retval != GST_AV1_PARSER_OK)
435 return 0;
436 return delta_q;
437 } else {
438 return 0;
439 }
440
441 return 0;
442 }
443
444 /* 5.3.4 */
445 static GstAV1ParserResult
av1_bitstreamfn_trailing_bits(GstBitReader * br,guint32 nbBits)446 av1_bitstreamfn_trailing_bits (GstBitReader * br, guint32 nbBits)
447 {
448 guint8 trailing_one_bit, trailing_zero_bit;
449
450 g_assert (nbBits);
451
452 trailing_one_bit = AV1_READ_BIT (br);
453 if (trailing_one_bit != 1) {
454 return GST_AV1_PARSER_BITSTREAM_ERROR;
455 }
456
457 nbBits--;
458 while (nbBits > 0) {
459 trailing_zero_bit = AV1_READ_BIT (br);
460 if (trailing_zero_bit != 0)
461 return GST_AV1_PARSER_BITSTREAM_ERROR;
462 nbBits--;
463 }
464
465 return GST_AV1_PARSER_OK;
466 }
467
468 static GstAV1ParserResult
av1_skip_trailing_bits(GstAV1Parser * parser,GstBitReader * br,GstAV1OBU * obu)469 av1_skip_trailing_bits (GstAV1Parser * parser, GstBitReader * br,
470 GstAV1OBU * obu)
471 {
472 guint32 payloadBits = gst_bit_reader_get_pos (br);
473 GstAV1ParserResult ret;
474
475 if (obu->obu_size > 0
476 && obu->obu_type != GST_AV1_OBU_TILE_GROUP
477 && obu->obu_type != GST_AV1_OBU_TILE_LIST
478 && obu->obu_type != GST_AV1_OBU_FRAME) {
479 if (payloadBits >= obu->obu_size * 8)
480 return GST_AV1_PARSER_NO_MORE_DATA;
481
482 ret = av1_bitstreamfn_trailing_bits (br, obu->obu_size * 8 - payloadBits);
483 if (ret != GST_AV1_PARSER_OK)
484 return ret;
485 }
486 return GST_AV1_PARSER_OK;
487 }
488
489 static gboolean
av1_seq_level_idx_is_valid(GstAV1SeqLevels seq_level_idx)490 av1_seq_level_idx_is_valid (GstAV1SeqLevels seq_level_idx)
491 {
492 return seq_level_idx == GST_AV1_SEQ_LEVEL_MAX
493 || (seq_level_idx < GST_AV1_SEQ_LEVELS
494 /* The following levels are currently undefined. */
495 && seq_level_idx != GST_AV1_SEQ_LEVEL_2_2
496 && seq_level_idx != GST_AV1_SEQ_LEVEL_2_3
497 && seq_level_idx != GST_AV1_SEQ_LEVEL_3_2
498 && seq_level_idx != GST_AV1_SEQ_LEVEL_3_3
499 && seq_level_idx != GST_AV1_SEQ_LEVEL_4_2
500 && seq_level_idx != GST_AV1_SEQ_LEVEL_4_3
501 && seq_level_idx != GST_AV1_SEQ_LEVEL_7_0
502 && seq_level_idx != GST_AV1_SEQ_LEVEL_7_1
503 && seq_level_idx != GST_AV1_SEQ_LEVEL_7_2
504 && seq_level_idx != GST_AV1_SEQ_LEVEL_7_3);
505 }
506
507 static void
av1_parser_init_sequence_header(GstAV1SequenceHeaderOBU * seq_header)508 av1_parser_init_sequence_header (GstAV1SequenceHeaderOBU * seq_header)
509 {
510 memset (seq_header, 0, sizeof (*seq_header));
511 seq_header->bit_depth = 8;
512 seq_header->num_planes = 1;
513 }
514
515 /*************************************
516 * *
517 * Parser Functions *
518 * *
519 *************************************/
520 static void
gst_av1_parse_reset_state(GstAV1Parser * parser,gboolean free_sps)521 gst_av1_parse_reset_state (GstAV1Parser * parser, gboolean free_sps)
522 {
523 parser->state.begin_first_frame = FALSE;
524
525 parser->state.prev_frame_id = 0;
526 parser->state.current_frame_id = 0;
527 memset (&parser->state.ref_info, 0, sizeof (parser->state.ref_info));
528 parser->state.frame_width = 0;
529 parser->state.frame_height = 0;
530 parser->state.upscaled_width = 0;
531 parser->state.mi_cols = 0;
532 parser->state.mi_rows = 0;
533 parser->state.render_width = 0;
534 parser->state.render_height = 0;
535
536 memset (parser->state.mi_col_starts, 0, sizeof (parser->state.mi_col_starts));
537 memset (parser->state.mi_row_starts, 0, sizeof (parser->state.mi_row_starts));
538
539 parser->state.tile_cols_log2 = 0;
540 parser->state.tile_cols = 0;
541 parser->state.tile_rows_log2 = 0;
542 parser->state.tile_rows = 0;
543 parser->state.tile_size_bytes = 0;
544
545 parser->state.seen_frame_header = 0;
546
547 if (free_sps) {
548 parser->state.sequence_changed = FALSE;
549
550 if (parser->seq_header) {
551 g_slice_free (GstAV1SequenceHeaderOBU, parser->seq_header);
552 parser->seq_header = NULL;
553 }
554 }
555 }
556
557 /**
558 * gst_av1_parser_reset:
559 * @parser: the #GstAV1Parser
560 * @annex_b: indicate whether conforms to annex b
561 *
562 * Reset the current #GstAV1Parser's state totally.
563 *
564 * Since: 1.18
565 */
566 void
gst_av1_parser_reset(GstAV1Parser * parser,gboolean annex_b)567 gst_av1_parser_reset (GstAV1Parser * parser, gboolean annex_b)
568 {
569 g_return_if_fail (parser != NULL);
570
571 parser->annex_b = annex_b;
572 if (parser->annex_b)
573 gst_av1_parser_reset_annex_b (parser);
574
575 gst_av1_parse_reset_state (parser, TRUE);
576 }
577
578 /**
579 * gst_av1_parser_reset_annex_b:
580 * @parser: the #GstAV1Parser
581 *
582 * Only reset the current #GstAV1Parser's annex b context.
583 * The other part of the state is kept.
584 *
585 * Since: 1.20
586 */
587 void
gst_av1_parser_reset_annex_b(GstAV1Parser * parser)588 gst_av1_parser_reset_annex_b (GstAV1Parser * parser)
589 {
590 g_return_if_fail (parser != NULL);
591 g_return_if_fail (parser->annex_b);
592
593 if (parser->temporal_unit_consumed < parser->temporal_unit_size)
594 GST_DEBUG ("temporal_unit_consumed: %d, temporal_unit_size:%d, "
595 "discard the left %d bytes for a temporal_unit.",
596 parser->temporal_unit_consumed, parser->temporal_unit_size,
597 parser->temporal_unit_size - parser->temporal_unit_consumed);
598
599 if (parser->frame_unit_consumed < parser->frame_unit_size)
600 GST_DEBUG (" frame_unit_consumed %d, frame_unit_size: %d "
601 "discard the left %d bytes for a frame_unit.",
602 parser->frame_unit_consumed, parser->frame_unit_size,
603 parser->frame_unit_size - parser->frame_unit_consumed);
604
605 parser->temporal_unit_consumed = 0;
606 parser->temporal_unit_size = 0;
607 parser->frame_unit_consumed = 0;
608 parser->frame_unit_size = 0;
609 }
610
611 /* 5.3.2 */
612 static GstAV1ParserResult
gst_av1_parse_obu_header(GstAV1Parser * parser,GstBitReader * br,GstAV1OBUHeader * obu_header)613 gst_av1_parse_obu_header (GstAV1Parser * parser, GstBitReader * br,
614 GstAV1OBUHeader * obu_header)
615 {
616 guint8 obu_forbidden_bit;
617 guint8 obu_reserved_1bit;
618 guint8 obu_extension_header_reserved_3bits;
619 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
620
621 if (AV1_REMAINING_BYTES (br) < 1) {
622 ret = GST_AV1_PARSER_NO_MORE_DATA;
623 goto error;
624 }
625
626 obu_forbidden_bit = AV1_READ_BIT (br);
627 if (obu_forbidden_bit != 0) {
628 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
629 goto error;
630 }
631
632 obu_header->obu_type = AV1_READ_BITS (br, 4);
633 obu_header->obu_extention_flag = AV1_READ_BIT (br);
634 obu_header->obu_has_size_field = AV1_READ_BIT (br);
635 obu_reserved_1bit = AV1_READ_BIT (br);
636 if (obu_reserved_1bit != 0) {
637 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
638 goto error;
639 }
640
641 if (obu_header->obu_extention_flag) {
642 if (AV1_REMAINING_BYTES (br) < 1) {
643 ret = GST_AV1_PARSER_NO_MORE_DATA;
644 goto error;
645 }
646
647 /* 5.3.3 OBU extension header */
648 obu_header->obu_temporal_id = AV1_READ_BITS (br, 3);
649 obu_header->obu_spatial_id = AV1_READ_BITS (br, 2);
650 obu_extension_header_reserved_3bits = AV1_READ_BITS (br, 3);
651 if (obu_extension_header_reserved_3bits != 0) {
652 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
653 goto error;
654 }
655 }
656
657 return GST_AV1_PARSER_OK;
658
659 error:
660 GST_WARNING ("parse OBU header error %d", ret);
661 return ret;
662 }
663
664 /**
665 * gst_av1_parser_identify_one_obu:
666 * @parser: the #GstAV1Parser
667 * @data: the data to parse
668 * @size: the size of @data
669 * @obu: a #GstAV1OBU to store the identified result
670 * @consumed: (out): the consumed data size
671 *
672 * Identify one @obu's type from the incoming data stream. This function
673 * should be called first to know the type of @obu before other parse APIs.
674 *
675 * Returns: The #GstAV1ParserResult.
676 *
677 * Since: 1.18
678 */
679 GstAV1ParserResult
gst_av1_parser_identify_one_obu(GstAV1Parser * parser,const guint8 * data,guint32 size,GstAV1OBU * obu,guint32 * consumed)680 gst_av1_parser_identify_one_obu (GstAV1Parser * parser, const guint8 * data,
681 guint32 size, GstAV1OBU * obu, guint32 * consumed)
682 {
683 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
684 GstBitReader br;
685 guint obu_length = 0;
686 guint32 used;
687
688 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
689 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
690 g_return_val_if_fail (data != NULL, GST_AV1_PARSER_INVALID_OPERATION);
691 g_return_val_if_fail (consumed != NULL, GST_AV1_PARSER_INVALID_OPERATION);
692
693 *consumed = 0;
694 memset (obu, 0, sizeof (*obu));
695
696 if (parser->annex_b) {
697 GST_LOG ("temporal_unit_consumed: %d, temporal_unit_size:%d,"
698 " frame_unit_consumed %d, frame_unit_size: %d",
699 parser->temporal_unit_consumed, parser->temporal_unit_size,
700 parser->frame_unit_consumed, parser->frame_unit_size);
701 }
702
703 if (!size) {
704 return ret = GST_AV1_PARSER_NO_MORE_DATA;
705 goto error;
706 }
707
708 /* parse the data size if annex_b */
709 if (parser->annex_b) {
710 guint last_pos;
711 annex_b_again:
712 last_pos = 0;
713
714 if (*consumed > size)
715 goto error;
716 if (*consumed == size) {
717 ret = GST_AV1_PARSER_NO_MORE_DATA;
718 goto error;
719 }
720 gst_bit_reader_init (&br, data + *consumed, size - *consumed);
721
722 if (parser->temporal_unit_consumed > parser->temporal_unit_size)
723 goto error;
724
725 if (parser->temporal_unit_consumed &&
726 parser->temporal_unit_consumed == parser->temporal_unit_size) {
727 GST_LOG ("Complete a temporal unit of size %d",
728 parser->temporal_unit_size);
729 parser->temporal_unit_consumed = parser->temporal_unit_size = 0;
730 }
731
732 if (parser->temporal_unit_size == 0) {
733 parser->temporal_unit_size = av1_bitstreamfn_leb128 (&br, &ret);
734 if (ret != GST_AV1_PARSER_OK)
735 goto error;
736
737 g_assert (gst_bit_reader_get_pos (&br) % 8 == 0);
738 used = (gst_bit_reader_get_pos (&br) / 8 - last_pos);
739 last_pos = gst_bit_reader_get_pos (&br) / 8;
740 *consumed += used;
741
742 if (parser->temporal_unit_consumed == parser->temporal_unit_size) {
743 /* Some extreme case like a temporal unit just
744 hold a temporal_unit_size = 0 */
745 goto annex_b_again;
746 }
747 }
748
749 if (parser->frame_unit_consumed > parser->frame_unit_size)
750 goto error;
751
752 if (parser->frame_unit_consumed &&
753 parser->frame_unit_consumed == parser->frame_unit_size) {
754 GST_LOG ("Complete a frame unit of size %d", parser->frame_unit_size);
755 parser->frame_unit_size = parser->frame_unit_consumed = 0;
756 }
757
758 if (parser->frame_unit_size == 0) {
759 parser->frame_unit_size = av1_bitstreamfn_leb128 (&br, &ret);
760 if (ret != GST_AV1_PARSER_OK)
761 goto error;
762
763 g_assert (gst_bit_reader_get_pos (&br) % 8 == 0);
764 used = gst_bit_reader_get_pos (&br) / 8 - last_pos;
765 last_pos = gst_bit_reader_get_pos (&br) / 8;
766 *consumed += used;
767 parser->temporal_unit_consumed += used;
768
769 if (parser->frame_unit_size >
770 parser->temporal_unit_size - parser->temporal_unit_consumed) {
771 GST_INFO ("Error stream, frame unit size %d, bigger than the left"
772 "temporal unit size %d", parser->frame_unit_size,
773 parser->temporal_unit_size - parser->temporal_unit_consumed);
774 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
775 goto error;
776 }
777
778 if (parser->temporal_unit_consumed == parser->temporal_unit_size ||
779 parser->frame_unit_consumed == parser->frame_unit_size) {
780 /* Some extreme case like a temporal unit just hold a frame_unit_size,
781 or a frame unit just hold frame_unit_size = 0 */
782 goto annex_b_again;
783 }
784 }
785
786 obu_length = av1_bitstreamfn_leb128 (&br, &ret);
787 if (ret != GST_AV1_PARSER_OK)
788 goto error;
789
790 if (obu_length > parser->frame_unit_size - parser->frame_unit_consumed) {
791 GST_INFO ("Error stream, obu_length is %d, bigger than the left"
792 "frame unit size %d", obu_length,
793 parser->frame_unit_size - parser->frame_unit_consumed);
794 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
795 goto error;
796 }
797 /* update the consumed */
798 used = gst_bit_reader_get_pos (&br) / 8 - last_pos;
799 last_pos = gst_bit_reader_get_pos (&br) / 8;
800 *consumed += used;
801 parser->temporal_unit_consumed += used;
802 parser->frame_unit_consumed += used;
803
804 if (obu_length == 0) {
805 /* An empty obu? let continue to the next */
806 ret = GST_AV1_PARSER_DROP;
807 goto error;
808 }
809 }
810
811 if (*consumed > size)
812 goto error;
813 if (*consumed == size) {
814 ret = GST_AV1_PARSER_NO_MORE_DATA;
815 goto error;
816 }
817 gst_bit_reader_init (&br, data + *consumed, size - *consumed);
818
819 ret = gst_av1_parse_obu_header (parser, &br, &obu->header);
820 if (ret != GST_AV1_PARSER_OK)
821 goto error;
822
823 obu->obu_type = obu->header.obu_type;
824 GST_LOG ("identify obu type is %d", obu->obu_type);
825
826 if (obu->header.obu_has_size_field) {
827 guint size_sz = gst_bit_reader_get_pos (&br) / 8;
828
829 obu->obu_size = av1_bitstreamfn_leb128 (&br, &ret);
830 if (ret != GST_AV1_PARSER_OK)
831 goto error;
832
833 size_sz = gst_bit_reader_get_pos (&br) / 8 - size_sz;
834 if (obu_length
835 && obu_length - 1 - obu->header.obu_extention_flag - size_sz !=
836 obu->obu_size) {
837 /* If obu_size and obu_length are both present, but inconsistent,
838 then the packed bitstream is deemed invalid. */
839 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
840 goto error;
841 }
842
843 if (AV1_REMAINING_BYTES (&br) < obu->obu_size) {
844 ret = GST_AV1_PARSER_NO_MORE_DATA;
845 goto error;
846 }
847 } else {
848 if (obu_length == 0) {
849 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
850 goto error;
851 }
852
853 obu->obu_size = obu_length - 1 - obu->header.obu_extention_flag;
854 }
855
856 g_assert (gst_bit_reader_get_pos (&br) % 8 == 0);
857 used = gst_bit_reader_get_pos (&br) / 8;
858 /* fail if not a complete obu */
859 if (size - *consumed - used < obu->obu_size) {
860 ret = GST_AV1_PARSER_NO_MORE_DATA;
861 goto error;
862 }
863
864 /* update the consumed */
865 *consumed += used;
866 if (parser->annex_b) {
867 parser->temporal_unit_consumed += used;
868 parser->frame_unit_consumed += used;
869 }
870
871 obu->data = (guint8 *) (data + *consumed);
872
873 *consumed += obu->obu_size;
874 if (parser->annex_b) {
875 parser->temporal_unit_consumed += obu->obu_size;
876 parser->frame_unit_consumed += obu->obu_size;
877 }
878
879 if (obu->obu_type != GST_AV1_OBU_SEQUENCE_HEADER
880 && obu->obu_type != GST_AV1_OBU_TEMPORAL_DELIMITER
881 && parser->state.operating_point_idc && obu->header.obu_extention_flag) {
882 guint32 inTemporalLayer =
883 (parser->state.operating_point_idc >> obu->header.obu_temporal_id) & 1;
884 guint32 inSpatialLayer =
885 (parser->state.operating_point_idc >> (obu->header.obu_spatial_id +
886 8)) & 1;
887 if (!inTemporalLayer || !inSpatialLayer) {
888 ret = GST_AV1_PARSER_DROP;
889 goto error;
890 }
891 }
892
893 return GST_AV1_PARSER_OK;
894
895 error:
896 GST_WARNING ("can not identify obu, error %d", ret);
897 return ret;
898 }
899
900 /* 5.5.2 */
901 static GstAV1ParserResult
gst_av1_parse_color_config(GstAV1Parser * parser,GstBitReader * br,GstAV1SequenceHeaderOBU * seq_header,GstAV1ColorConfig * color_config)902 gst_av1_parse_color_config (GstAV1Parser * parser, GstBitReader * br,
903 GstAV1SequenceHeaderOBU * seq_header, GstAV1ColorConfig * color_config)
904 {
905 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
906
907 color_config->high_bitdepth = AV1_READ_BIT_CHECKED (br, &ret);
908 if (ret != GST_AV1_PARSER_OK)
909 goto error;
910
911 if (seq_header->seq_profile == GST_AV1_PROFILE_2
912 && color_config->high_bitdepth) {
913 color_config->twelve_bit = AV1_READ_BIT_CHECKED (br, &ret);
914 if (ret != GST_AV1_PARSER_OK)
915 goto error;
916
917 seq_header->bit_depth = color_config->twelve_bit ? 12 : 10;
918 } else if (seq_header->seq_profile <= GST_AV1_PROFILE_2) {
919 seq_header->bit_depth = color_config->high_bitdepth ? 10 : 8;
920 } else {
921 GST_INFO ("Unsupported profile/bit-depth combination");
922 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
923 goto error;
924 }
925
926 if (seq_header->seq_profile == GST_AV1_PROFILE_1)
927 color_config->mono_chrome = 0;
928 else {
929 color_config->mono_chrome = AV1_READ_BIT_CHECKED (br, &ret);
930 if (ret != GST_AV1_PARSER_OK)
931 goto error;
932 }
933 seq_header->num_planes = color_config->mono_chrome ? 1 : 3;
934
935 color_config->color_description_present_flag =
936 AV1_READ_BIT_CHECKED (br, &ret);
937 if (ret != GST_AV1_PARSER_OK)
938 goto error;
939
940 if (color_config->color_description_present_flag) {
941 if (AV1_REMAINING_BITS (br) < 8 + 8 + 8) {
942 ret = GST_AV1_PARSER_NO_MORE_DATA;
943 goto error;
944 }
945 color_config->color_primaries = AV1_READ_BITS (br, 8);
946 color_config->transfer_characteristics = AV1_READ_BITS (br, 8);
947 color_config->matrix_coefficients = AV1_READ_BITS (br, 8);
948 } else {
949 color_config->color_primaries = GST_AV1_CP_UNSPECIFIED;
950 color_config->transfer_characteristics = GST_AV1_TC_UNSPECIFIED;
951 color_config->matrix_coefficients = GST_AV1_MC_UNSPECIFIED;
952 }
953
954 if (color_config->mono_chrome) {
955 color_config->color_range = AV1_READ_BIT_CHECKED (br, &ret);
956 if (ret != GST_AV1_PARSER_OK)
957 goto error;
958
959 color_config->subsampling_x = 1;
960 color_config->subsampling_y = 1;
961 color_config->chroma_sample_position = GST_AV1_CSP_UNKNOWN;
962 color_config->separate_uv_delta_q = 0;
963 goto success;
964 } else if (color_config->color_primaries == GST_AV1_CP_BT_709 &&
965 color_config->transfer_characteristics == GST_AV1_TC_SRGB &&
966 color_config->matrix_coefficients == GST_AV1_MC_IDENTITY) {
967 color_config->color_range = 1;
968 color_config->subsampling_x = 0;
969 color_config->subsampling_y = 0;
970 if (!(seq_header->seq_profile == GST_AV1_PROFILE_1 ||
971 (seq_header->seq_profile == GST_AV1_PROFILE_2
972 && seq_header->bit_depth == 12))) {
973 GST_INFO ("sRGB colorspace not compatible with specified profile");
974 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
975 goto error;
976 }
977 } else {
978 color_config->color_range = AV1_READ_BIT_CHECKED (br, &ret);
979 if (ret != GST_AV1_PARSER_OK)
980 goto error;
981
982 if (seq_header->seq_profile == GST_AV1_PROFILE_0) {
983 /* 420 only */
984 color_config->subsampling_x = 1;
985 color_config->subsampling_y = 1;
986 } else if (seq_header->seq_profile == GST_AV1_PROFILE_1) {
987 /* 444 only */
988 color_config->subsampling_x = 0;
989 color_config->subsampling_y = 0;
990 } else {
991 g_assert (seq_header->seq_profile == GST_AV1_PROFILE_2);
992 if (seq_header->bit_depth == 12) {
993 color_config->subsampling_x = AV1_READ_BIT_CHECKED (br, &ret);
994 if (ret != GST_AV1_PARSER_OK)
995 goto error;
996
997 if (color_config->subsampling_x) {
998 /* 422 or 420 */
999 color_config->subsampling_y = AV1_READ_BIT_CHECKED (br, &ret);
1000 if (ret != GST_AV1_PARSER_OK)
1001 goto error;
1002 } else
1003 /* 444 */
1004 color_config->subsampling_y = 0;
1005 } else {
1006 /* 422 */
1007 color_config->subsampling_x = 1;
1008 color_config->subsampling_y = 0;
1009 }
1010 }
1011
1012 if (color_config->matrix_coefficients == GST_AV1_MC_IDENTITY &&
1013 (color_config->subsampling_x || color_config->subsampling_y)) {
1014 GST_INFO ("Identity CICP Matrix incompatible with"
1015 " non 4:4:4 color sampling");
1016 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
1017 goto error;
1018 }
1019
1020 if (color_config->subsampling_x && color_config->subsampling_y) {
1021 color_config->chroma_sample_position =
1022 AV1_READ_BITS_CHECKED (br, 2, &ret);
1023 if (ret != GST_AV1_PARSER_OK)
1024 goto error;
1025 }
1026 }
1027
1028 color_config->separate_uv_delta_q = AV1_READ_BIT_CHECKED (br, &ret);
1029 if (ret != GST_AV1_PARSER_OK)
1030 goto error;
1031
1032 if (!(color_config->subsampling_x == 0 && color_config->subsampling_y == 0) &&
1033 !(color_config->subsampling_x == 1 && color_config->subsampling_y == 1) &&
1034 !(color_config->subsampling_x == 1 && color_config->subsampling_y == 0)) {
1035 GST_INFO ("Only 4:4:4, 4:2:2 and 4:2:0 are currently supported, "
1036 "%d %d subsampling is not supported.\n",
1037 color_config->subsampling_x, color_config->subsampling_y);
1038 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
1039 goto error;
1040 }
1041
1042 success:
1043 return GST_AV1_PARSER_OK;
1044
1045 error:
1046 GST_WARNING ("parse color config error %d", ret);
1047 return ret;
1048 }
1049
1050 /* 5.5.3 */
1051 static GstAV1ParserResult
gst_av1_parse_timing_info(GstAV1Parser * parser,GstBitReader * br,GstAV1TimingInfo * timing_info)1052 gst_av1_parse_timing_info (GstAV1Parser * parser, GstBitReader * br,
1053 GstAV1TimingInfo * timing_info)
1054 {
1055 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
1056
1057 if (AV1_REMAINING_BITS (br) < 32 + 32 + 1) {
1058 ret = GST_AV1_PARSER_NO_MORE_DATA;
1059 goto error;
1060 }
1061
1062 timing_info->num_units_in_display_tick = AV1_READ_UINT32 (br);
1063 timing_info->time_scale = AV1_READ_UINT32 (br);
1064 if (timing_info->num_units_in_display_tick == 0 ||
1065 timing_info->time_scale == 0) {
1066 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
1067 goto error;
1068 }
1069
1070 timing_info->equal_picture_interval = AV1_READ_BIT (br);
1071 if (timing_info->equal_picture_interval) {
1072 timing_info->num_ticks_per_picture_minus_1 =
1073 av1_bitstreamfn_uvlc (br, &ret);
1074 if (ret != GST_AV1_PARSER_OK)
1075 goto error;
1076
1077 if (timing_info->num_ticks_per_picture_minus_1 == G_MAXUINT) {
1078 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
1079 goto error;
1080 }
1081 }
1082
1083 return GST_AV1_PARSER_OK;
1084
1085 error:
1086 GST_WARNING ("parse timing info error %d", ret);
1087 return ret;
1088 }
1089
1090 /* 5.5.4 */
1091 static GstAV1ParserResult
gst_av1_parse_decoder_model_info(GstAV1Parser * parser,GstBitReader * br,GstAV1DecoderModelInfo * decoder_model_info)1092 gst_av1_parse_decoder_model_info (GstAV1Parser * parser, GstBitReader * br,
1093 GstAV1DecoderModelInfo * decoder_model_info)
1094 {
1095 if (AV1_REMAINING_BITS (br) < 5 + 32 + 5 + 5)
1096 return GST_AV1_PARSER_NO_MORE_DATA;
1097
1098 decoder_model_info->buffer_delay_length_minus_1 = AV1_READ_BITS (br, 5);
1099 decoder_model_info->num_units_in_decoding_tick = AV1_READ_BITS (br, 32);
1100 decoder_model_info->buffer_removal_time_length_minus_1 =
1101 AV1_READ_BITS (br, 5);
1102 decoder_model_info->frame_presentation_time_length_minus_1 =
1103 AV1_READ_BITS (br, 5);
1104
1105 return GST_AV1_PARSER_OK;
1106 }
1107
1108 /* 5.5.5 */
1109 static GstAV1ParserResult
gst_av1_parse_operating_parameters_info(GstAV1Parser * parser,GstBitReader * br,GstAV1SequenceHeaderOBU * seq_header,GstAV1OperatingPoint * op_point)1110 gst_av1_parse_operating_parameters_info (GstAV1Parser * parser,
1111 GstBitReader * br, GstAV1SequenceHeaderOBU * seq_header,
1112 GstAV1OperatingPoint * op_point)
1113 {
1114 guint32 n = seq_header->decoder_model_info.buffer_delay_length_minus_1 + 1;
1115
1116 if (AV1_REMAINING_BITS (br) < n + n + 1)
1117 return GST_AV1_PARSER_NO_MORE_DATA;
1118
1119 op_point->decoder_buffer_delay = AV1_READ_BITS (br, n);
1120 op_point->encoder_buffer_delay = AV1_READ_BITS (br, n);
1121 op_point->low_delay_mode_flag = AV1_READ_BIT (br);
1122 return GST_AV1_PARSER_OK;
1123 }
1124
1125 /* 5.5.1 General sequence header OBU */
1126 /**
1127 * gst_av1_parser_parse_sequence_header_obu:
1128 * @parser: the #GstAV1Parser
1129 * @obu: a #GstAV1OBU to be parsed
1130 * @seq_header: a #GstAV1SequenceHeaderOBU to store the parsed result.
1131 *
1132 * Parse one sequence header @obu based on the @parser context, store the
1133 * result in the @seq_header.
1134 *
1135 * Returns: The #GstAV1ParserResult.
1136 *
1137 * Since: 1.18
1138 */
1139 GstAV1ParserResult
gst_av1_parser_parse_sequence_header_obu(GstAV1Parser * parser,GstAV1OBU * obu,GstAV1SequenceHeaderOBU * seq_header)1140 gst_av1_parser_parse_sequence_header_obu (GstAV1Parser * parser,
1141 GstAV1OBU * obu, GstAV1SequenceHeaderOBU * seq_header)
1142 {
1143 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
1144 gint i;
1145 GstBitReader bit_reader;
1146 GstBitReader *br = &bit_reader;
1147
1148 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1149 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1150 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_SEQUENCE_HEADER,
1151 GST_AV1_PARSER_INVALID_OPERATION);
1152 g_return_val_if_fail (seq_header != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1153
1154 av1_parser_init_sequence_header (seq_header);
1155 gst_bit_reader_init (br, obu->data, obu->obu_size);
1156
1157 if (AV1_REMAINING_BITS (br) < 8) {
1158 retval = GST_AV1_PARSER_NO_MORE_DATA;
1159 goto error;
1160 }
1161
1162 seq_header->seq_profile = AV1_READ_BITS (br, 3);
1163 if (seq_header->seq_profile > GST_AV1_PROFILE_2) {
1164 GST_INFO ("Unsupported profile %d", seq_header->seq_profile);
1165 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1166 goto error;
1167 }
1168
1169 seq_header->still_picture = AV1_READ_BIT (br);
1170 seq_header->reduced_still_picture_header = AV1_READ_BIT (br);
1171 if (!seq_header->still_picture && seq_header->reduced_still_picture_header) {
1172 GST_INFO (" If reduced_still_picture_header is equal to 1, it is a"
1173 " requirement of bitstream conformance that still_picture is equal"
1174 " to 1. ");
1175 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1176 goto error;
1177 }
1178
1179 if (seq_header->reduced_still_picture_header) {
1180 seq_header->timing_info_present_flag = 0;
1181 seq_header->decoder_model_info_present_flag = 0;
1182 seq_header->initial_display_delay_present_flag = 0;
1183 seq_header->operating_points_cnt_minus_1 = 0;
1184 seq_header->operating_points[0].idc = 0;
1185 seq_header->operating_points[0].seq_level_idx = AV1_READ_BITS (br, 5);
1186 if (!av1_seq_level_idx_is_valid
1187 (seq_header->operating_points[0].seq_level_idx)) {
1188 GST_INFO ("The seq_level_idx is unsupported");
1189 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1190 goto error;
1191 }
1192 seq_header->operating_points[0].seq_tier = 0;
1193 seq_header->operating_points[0].decoder_model_present_for_this_op = 0;
1194 seq_header->operating_points[0].initial_display_delay_present_for_this_op =
1195 0;
1196 } else {
1197 seq_header->timing_info_present_flag = AV1_READ_BIT (br);
1198
1199 if (seq_header->timing_info_present_flag) {
1200 retval =
1201 gst_av1_parse_timing_info (parser, br, &(seq_header->timing_info));
1202 if (retval != GST_AV1_PARSER_OK)
1203 goto error;
1204
1205 seq_header->decoder_model_info_present_flag =
1206 AV1_READ_BIT_CHECKED (br, &retval);
1207 if (retval != GST_AV1_PARSER_OK)
1208 goto error;
1209
1210 if (seq_header->decoder_model_info_present_flag) {
1211 retval = gst_av1_parse_decoder_model_info (parser, br,
1212 &(seq_header->decoder_model_info));
1213 if (retval != GST_AV1_PARSER_OK)
1214 goto error;
1215 }
1216 } else {
1217 seq_header->decoder_model_info_present_flag = 0;
1218 }
1219
1220 if (AV1_REMAINING_BITS (br) < 6) {
1221 retval = GST_AV1_PARSER_NO_MORE_DATA;
1222 goto error;
1223 }
1224 seq_header->initial_display_delay_present_flag = AV1_READ_BIT (br);
1225 seq_header->operating_points_cnt_minus_1 = AV1_READ_BITS (br, 5);
1226 if (seq_header->operating_points_cnt_minus_1 + 1 >
1227 GST_AV1_MAX_OPERATING_POINTS) {
1228 GST_INFO ("The operating points number %d is too big",
1229 seq_header->operating_points_cnt_minus_1 + 1);
1230 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1231 goto error;
1232 }
1233
1234 for (i = 0; i < seq_header->operating_points_cnt_minus_1 + 1; i++) {
1235 if (AV1_REMAINING_BITS (br) < 17) {
1236 retval = GST_AV1_PARSER_NO_MORE_DATA;
1237 goto error;
1238 }
1239 seq_header->operating_points[i].idc = AV1_READ_BITS (br, 12);
1240 seq_header->operating_points[i].seq_level_idx = AV1_READ_BITS (br, 5);
1241 if (!av1_seq_level_idx_is_valid
1242 (seq_header->operating_points[0].seq_level_idx)) {
1243 GST_INFO ("The seq_level_idx is unsupported");
1244 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1245 goto error;
1246 }
1247 if (seq_header->operating_points[i].seq_level_idx > GST_AV1_SEQ_LEVEL_3_3) {
1248 seq_header->operating_points[i].seq_tier = AV1_READ_BIT (br);
1249 } else {
1250 seq_header->operating_points[i].seq_tier = 0;
1251 }
1252 if (seq_header->decoder_model_info_present_flag) {
1253 seq_header->operating_points[i].decoder_model_present_for_this_op =
1254 AV1_READ_BIT (br);
1255 if (seq_header->operating_points[i].decoder_model_present_for_this_op)
1256 retval =
1257 gst_av1_parse_operating_parameters_info (parser, br, seq_header,
1258 &(seq_header->operating_points[i]));
1259 if (retval != GST_AV1_PARSER_OK)
1260 goto error;
1261 } else {
1262 seq_header->operating_points[i].decoder_model_present_for_this_op = 0;
1263 }
1264
1265 if (seq_header->initial_display_delay_present_flag) {
1266 seq_header->
1267 operating_points[i].initial_display_delay_present_for_this_op =
1268 AV1_READ_BIT_CHECKED (br, &retval);
1269 if (retval != GST_AV1_PARSER_OK)
1270 goto error;
1271
1272 if (seq_header->
1273 operating_points[i].initial_display_delay_present_for_this_op) {
1274 seq_header->operating_points[i].initial_display_delay_minus_1 =
1275 AV1_READ_BITS_CHECKED (br, 4, &retval);
1276 if (retval != GST_AV1_PARSER_OK)
1277 goto error;
1278
1279 if (seq_header->operating_points[i].initial_display_delay_minus_1 +
1280 1 > 10) {
1281 GST_INFO ("AV1 does not support more than 10 decoded frames delay");
1282 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1283 goto error;
1284 }
1285 } else {
1286 seq_header->operating_points[i].initial_display_delay_minus_1 = 9;
1287 }
1288 } else {
1289 seq_header->
1290 operating_points[i].initial_display_delay_present_for_this_op = 0;
1291 seq_header->operating_points[i].initial_display_delay_minus_1 = 9;
1292 }
1293 }
1294 }
1295
1296 /* Let user decide the operatingPoint,
1297 implemented by calling gst_av1_parser_set_operating_point()
1298 operatingPoint = choose_operating_point( )
1299 operating_point_idc = operating_point_idc[ operatingPoint ] */
1300
1301 if (AV1_REMAINING_BITS (br) < 4 + 4 +
1302 (seq_header->frame_width_bits_minus_1 + 1) +
1303 (seq_header->frame_height_bits_minus_1 + 1)) {
1304 retval = GST_AV1_PARSER_NO_MORE_DATA;
1305 goto error;
1306 }
1307
1308 seq_header->frame_width_bits_minus_1 = AV1_READ_BITS (br, 4);
1309 seq_header->frame_height_bits_minus_1 = AV1_READ_BITS (br, 4);
1310 seq_header->max_frame_width_minus_1 =
1311 AV1_READ_BITS (br, seq_header->frame_width_bits_minus_1 + 1);
1312 seq_header->max_frame_height_minus_1 =
1313 AV1_READ_BITS (br, seq_header->frame_height_bits_minus_1 + 1);
1314
1315 if (seq_header->reduced_still_picture_header)
1316 seq_header->frame_id_numbers_present_flag = 0;
1317 else {
1318 seq_header->frame_id_numbers_present_flag =
1319 AV1_READ_BIT_CHECKED (br, &retval);
1320 if (retval != GST_AV1_PARSER_OK)
1321 goto error;
1322 }
1323
1324 if (seq_header->frame_id_numbers_present_flag) {
1325 if (AV1_REMAINING_BITS (br) < 4 + 3) {
1326 retval = GST_AV1_PARSER_NO_MORE_DATA;
1327 goto error;
1328 }
1329 seq_header->delta_frame_id_length_minus_2 = AV1_READ_BITS (br, 4);
1330 seq_header->additional_frame_id_length_minus_1 = AV1_READ_BITS (br, 3);
1331
1332 if (seq_header->additional_frame_id_length_minus_1 + 1 +
1333 seq_header->delta_frame_id_length_minus_2 + 2 > 16) {
1334 GST_INFO ("Invalid frame_id_length");
1335 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
1336 goto error;
1337 }
1338 }
1339
1340 if (AV1_REMAINING_BITS (br) < 3) {
1341 retval = GST_AV1_PARSER_NO_MORE_DATA;
1342 goto error;
1343 }
1344 seq_header->use_128x128_superblock = AV1_READ_BIT (br);
1345 seq_header->enable_filter_intra = AV1_READ_BIT (br);
1346 seq_header->enable_intra_edge_filter = AV1_READ_BIT (br);
1347
1348 if (seq_header->reduced_still_picture_header) {
1349 seq_header->enable_interintra_compound = 0;
1350 seq_header->enable_masked_compound = 0;
1351 seq_header->enable_warped_motion = 0;
1352 seq_header->enable_dual_filter = 0;
1353 seq_header->enable_order_hint = 0;
1354 seq_header->enable_jnt_comp = 0;
1355 seq_header->enable_ref_frame_mvs = 0;
1356 seq_header->seq_force_screen_content_tools =
1357 GST_AV1_SELECT_SCREEN_CONTENT_TOOLS;
1358 seq_header->seq_force_integer_mv = GST_AV1_SELECT_INTEGER_MV;
1359 seq_header->order_hint_bits_minus_1 = -1;
1360 seq_header->order_hint_bits = 0;
1361 } else {
1362 if (AV1_REMAINING_BITS (br) < 5) {
1363 retval = GST_AV1_PARSER_NO_MORE_DATA;
1364 goto error;
1365 }
1366 seq_header->enable_interintra_compound = AV1_READ_BIT (br);
1367 seq_header->enable_masked_compound = AV1_READ_BIT (br);
1368 seq_header->enable_warped_motion = AV1_READ_BIT (br);
1369 seq_header->enable_dual_filter = AV1_READ_BIT (br);
1370 seq_header->enable_order_hint = AV1_READ_BIT (br);
1371 if (seq_header->enable_order_hint) {
1372 if (AV1_REMAINING_BITS (br) < 2) {
1373 retval = GST_AV1_PARSER_NO_MORE_DATA;
1374 goto error;
1375 }
1376 seq_header->enable_jnt_comp = AV1_READ_BIT (br);
1377 seq_header->enable_ref_frame_mvs = AV1_READ_BIT (br);
1378 } else {
1379 seq_header->enable_jnt_comp = 0;
1380 seq_header->enable_ref_frame_mvs = 0;
1381 }
1382
1383 seq_header->seq_choose_screen_content_tools =
1384 AV1_READ_BIT_CHECKED (br, &retval);
1385 if (retval != GST_AV1_PARSER_OK)
1386 goto error;
1387 if (seq_header->seq_choose_screen_content_tools)
1388 seq_header->seq_force_screen_content_tools =
1389 GST_AV1_SELECT_SCREEN_CONTENT_TOOLS;
1390 else {
1391 seq_header->seq_force_screen_content_tools =
1392 AV1_READ_BIT_CHECKED (br, &retval);
1393 if (retval != GST_AV1_PARSER_OK)
1394 goto error;
1395 }
1396
1397 if (seq_header->seq_force_screen_content_tools > 0) {
1398 seq_header->seq_choose_integer_mv = AV1_READ_BIT_CHECKED (br, &retval);
1399 if (retval != GST_AV1_PARSER_OK)
1400 goto error;
1401 if (seq_header->seq_choose_integer_mv)
1402 seq_header->seq_force_integer_mv = GST_AV1_SELECT_INTEGER_MV;
1403 else {
1404 seq_header->seq_force_integer_mv = AV1_READ_BIT_CHECKED (br, &retval);
1405 if (retval != GST_AV1_PARSER_OK)
1406 goto error;
1407 }
1408 } else {
1409 seq_header->seq_force_integer_mv = GST_AV1_SELECT_INTEGER_MV;
1410 }
1411 if (seq_header->enable_order_hint) {
1412 seq_header->order_hint_bits_minus_1 =
1413 AV1_READ_BITS_CHECKED (br, 3, &retval);
1414 if (retval != GST_AV1_PARSER_OK)
1415 goto error;
1416 seq_header->order_hint_bits = seq_header->order_hint_bits_minus_1 + 1;
1417 } else {
1418 seq_header->order_hint_bits_minus_1 = -1;
1419 seq_header->order_hint_bits = 0;
1420 }
1421 }
1422
1423 if (AV1_REMAINING_BITS (br) < 3) {
1424 retval = GST_AV1_PARSER_NO_MORE_DATA;
1425 goto error;
1426 }
1427 seq_header->enable_superres = AV1_READ_BIT (br);
1428 seq_header->enable_cdef = AV1_READ_BIT (br);
1429 seq_header->enable_restoration = AV1_READ_BIT (br);
1430
1431 retval = gst_av1_parse_color_config (parser, br, seq_header,
1432 &seq_header->color_config);
1433 if (retval != GST_AV1_PARSER_OK)
1434 goto error;
1435
1436 seq_header->film_grain_params_present = AV1_READ_BIT_CHECKED (br, &retval);
1437 if (retval != GST_AV1_PARSER_OK)
1438 goto error;
1439
1440 retval = av1_skip_trailing_bits (parser, br, obu);
1441 if (retval != GST_AV1_PARSER_OK)
1442 goto error;
1443
1444 if (parser->seq_header) {
1445 if (!memcmp (parser->seq_header, seq_header,
1446 sizeof (GstAV1SequenceHeaderOBU)))
1447 goto success;
1448
1449 g_slice_free (GstAV1SequenceHeaderOBU, parser->seq_header);
1450 }
1451
1452 parser->seq_header = g_slice_dup (GstAV1SequenceHeaderOBU, seq_header);
1453 gst_av1_parse_reset_state (parser, FALSE);
1454
1455 /* choose_operating_point() set the operating_point */
1456 if (parser->state.operating_point < 0 ||
1457 parser->state.operating_point >
1458 seq_header->operating_points_cnt_minus_1) {
1459 GST_WARNING ("Invalid operating_point %d set by user, just use 0",
1460 parser->state.operating_point);
1461 parser->state.operating_point_idc = seq_header->operating_points[0].idc;
1462 } else {
1463 parser->state.operating_point_idc =
1464 seq_header->operating_points[parser->state.operating_point].idc;
1465 }
1466
1467 parser->state.sequence_changed = TRUE;
1468
1469 success:
1470 return GST_AV1_PARSER_OK;
1471
1472 error:
1473 GST_WARNING ("parse sequence header error %d", retval);
1474 return retval;
1475 }
1476
1477 /* 5.6 */
1478 /**
1479 * gst_av1_parser_parse_temporal_delimiter_obu:
1480 * @parser: the #GstAV1Parser
1481 * @obu: a #GstAV1OBU to be parsed
1482 *
1483 * Parse one temporal delimiter @obu based on the @parser context.
1484 * The temporal delimiter is just delimiter and contains no content.
1485 *
1486 * Returns: The #GstAV1ParserResult.
1487 *
1488 * Since: 1.18
1489 */
1490 GstAV1ParserResult
gst_av1_parser_parse_temporal_delimiter_obu(GstAV1Parser * parser,GstAV1OBU * obu)1491 gst_av1_parser_parse_temporal_delimiter_obu (GstAV1Parser * parser,
1492 GstAV1OBU * obu)
1493 {
1494 GstBitReader bit_reader;
1495 GstAV1ParserResult ret;
1496
1497 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1498 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1499 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_TEMPORAL_DELIMITER,
1500 GST_AV1_PARSER_INVALID_OPERATION);
1501
1502 gst_bit_reader_init (&bit_reader, obu->data, obu->obu_size);
1503
1504 parser->state.seen_frame_header = 0;
1505
1506 ret = av1_skip_trailing_bits (parser, &bit_reader, obu);
1507 if (ret != GST_AV1_PARSER_OK)
1508 GST_WARNING ("parse temporal delimiter error %d", ret);
1509
1510 return ret;
1511 }
1512
1513 /* 5.8.2 */
1514 static GstAV1ParserResult
gst_av1_parse_metadata_itut_t35(GstAV1Parser * parser,GstBitReader * br,GstAV1MetadataITUT_T35 * itut_t35)1515 gst_av1_parse_metadata_itut_t35 (GstAV1Parser * parser, GstBitReader * br,
1516 GstAV1MetadataITUT_T35 * itut_t35)
1517 {
1518 GstAV1ParserResult ret;
1519
1520 itut_t35->itu_t_t35_country_code = AV1_READ_BITS_CHECKED (br, 8, &ret);
1521 if (ret != GST_AV1_PARSER_OK)
1522 return ret;
1523
1524 if (itut_t35->itu_t_t35_country_code == 0xFF) {
1525 itut_t35->itu_t_t35_country_code_extention_byte =
1526 AV1_READ_BITS_CHECKED (br, 8, &ret);
1527 if (ret != GST_AV1_PARSER_OK)
1528 return ret;
1529 }
1530 /* itu_t_t35_payload_bytes is not defined in specification.
1531 Just skip this part. */
1532
1533 return GST_AV1_PARSER_OK;
1534 }
1535
1536 /* 5.8.3 */
1537 static GstAV1ParserResult
gst_av1_parse_metadata_hdr_cll(GstAV1Parser * parser,GstBitReader * br,GstAV1MetadataHdrCll * hdr_cll)1538 gst_av1_parse_metadata_hdr_cll (GstAV1Parser * parser, GstBitReader * br,
1539 GstAV1MetadataHdrCll * hdr_cll)
1540 {
1541 if (AV1_REMAINING_BITS (br) < 32)
1542 return GST_AV1_PARSER_NO_MORE_DATA;
1543
1544 hdr_cll->max_cll = AV1_READ_UINT16 (br);
1545 hdr_cll->max_fall = AV1_READ_UINT16 (br);
1546
1547 return GST_AV1_PARSER_OK;
1548 }
1549
1550 /* 5.8.4 */
1551 static GstAV1ParserResult
gst_av1_parse_metadata_hdr_mdcv(GstAV1Parser * parser,GstBitReader * br,GstAV1MetadataHdrMdcv * hdr_mdcv)1552 gst_av1_parse_metadata_hdr_mdcv (GstAV1Parser * parser, GstBitReader * br,
1553 GstAV1MetadataHdrMdcv * hdr_mdcv)
1554 {
1555 gint i;
1556
1557 if (AV1_REMAINING_BITS (br) < 3 * (16 + 16) + 16 + 16 + 32 + 32)
1558 return GST_AV1_PARSER_NO_MORE_DATA;
1559
1560 for (i = 0; i < 3; i++) {
1561 hdr_mdcv->primary_chromaticity_x[i] = AV1_READ_UINT16 (br);
1562 hdr_mdcv->primary_chromaticity_y[i] = AV1_READ_UINT16 (br);
1563 }
1564
1565 hdr_mdcv->white_point_chromaticity_x = AV1_READ_UINT16 (br);
1566 hdr_mdcv->white_point_chromaticity_y = AV1_READ_UINT16 (br);
1567
1568 hdr_mdcv->luminance_max = AV1_READ_UINT32 (br);
1569 hdr_mdcv->luminance_min = AV1_READ_UINT32 (br);
1570
1571 return GST_AV1_PARSER_OK;
1572 }
1573
1574 /* 5.8.5 */
1575 static GstAV1ParserResult
gst_av1_parse_metadata_scalability(GstAV1Parser * parser,GstBitReader * br,GstAV1MetadataScalability * scalability)1576 gst_av1_parse_metadata_scalability (GstAV1Parser * parser,
1577 GstBitReader * br, GstAV1MetadataScalability * scalability)
1578 {
1579 gint i, j;
1580 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
1581 guint8 scalability_structure_reserved_3bits;
1582
1583 scalability->scalability_mode_idc = AV1_READ_UINT8_CHECKED (br, &ret);
1584 if (ret != GST_AV1_PARSER_OK)
1585 goto error;
1586
1587 if (scalability->scalability_mode_idc != GST_AV1_SCALABILITY_SS)
1588 goto success;
1589
1590 if (AV1_REMAINING_BITS (br) < 8) {
1591 ret = GST_AV1_PARSER_NO_MORE_DATA;
1592 goto error;
1593 }
1594
1595 /* 5.8.6 */
1596 scalability->spatial_layers_cnt_minus_1 = AV1_READ_BITS (br, 2);
1597 scalability->spatial_layer_dimensions_present_flag = AV1_READ_BIT (br);
1598 scalability->spatial_layer_description_present_flag = AV1_READ_BIT (br);
1599 scalability->temporal_group_description_present_flag = AV1_READ_BIT (br);
1600 scalability_structure_reserved_3bits = AV1_READ_BITS (br, 3);
1601 /* scalability_structure_reserved_3bits: must be set to zero and
1602 be ignored by decoders. */
1603 if (scalability_structure_reserved_3bits) {
1604 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
1605 goto error;
1606 }
1607
1608 if (scalability->spatial_layer_dimensions_present_flag) {
1609 for (i = 0; i <= scalability->spatial_layers_cnt_minus_1; i++) {
1610 if (AV1_REMAINING_BITS (br) < 16 * 2) {
1611 ret = GST_AV1_PARSER_NO_MORE_DATA;
1612 goto error;
1613 }
1614
1615 scalability->spatial_layer_max_width[i] = AV1_READ_UINT16 (br);
1616 scalability->spatial_layer_max_height[i] = AV1_READ_UINT16 (br);
1617 }
1618 }
1619
1620 if (scalability->spatial_layer_description_present_flag) {
1621 for (i = 0; i <= scalability->spatial_layers_cnt_minus_1; i++) {
1622 scalability->spatial_layer_ref_id[i] = AV1_READ_UINT8_CHECKED (br, &ret);
1623 if (ret != GST_AV1_PARSER_OK)
1624 goto error;
1625 }
1626 }
1627
1628 if (scalability->temporal_group_description_present_flag) {
1629 scalability->temporal_group_size = AV1_READ_UINT8_CHECKED (br, &ret);
1630 if (ret != GST_AV1_PARSER_OK)
1631 goto error;
1632
1633 for (i = 0; i < scalability->temporal_group_size; i++) {
1634 if (AV1_REMAINING_BITS (br) < 8) {
1635 ret = GST_AV1_PARSER_NO_MORE_DATA;
1636 goto error;
1637 }
1638
1639 scalability->temporal_group_temporal_id[i] = AV1_READ_BITS (br, 3);
1640 scalability->temporal_group_temporal_switching_up_point_flag[i] =
1641 AV1_READ_BIT (br);
1642 scalability->temporal_group_spatial_switching_up_point_flag[i] =
1643 AV1_READ_BIT (br);
1644 scalability->temporal_group_ref_cnt[i] = AV1_READ_BITS (br, 3);
1645 for (j = 0; j < scalability->temporal_group_ref_cnt[i]; j++) {
1646 scalability->temporal_group_ref_pic_diff[i][j] =
1647 AV1_READ_UINT8_CHECKED (br, &ret);
1648 if (ret != GST_AV1_PARSER_OK)
1649 goto error;
1650 }
1651 }
1652 }
1653
1654 success:
1655 return GST_AV1_PARSER_OK;
1656
1657 error:
1658 GST_WARNING ("parse metadata scalability error %d", ret);
1659 return ret;
1660 }
1661
1662 /* 5.8.7 */
1663 static GstAV1ParserResult
gst_av1_parse_metadata_timecode(GstAV1Parser * parser,GstBitReader * br,GstAV1MetadataTimecode * timecode)1664 gst_av1_parse_metadata_timecode (GstAV1Parser * parser, GstBitReader * br,
1665 GstAV1MetadataTimecode * timecode)
1666 {
1667 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
1668
1669 if (AV1_REMAINING_BITS (br) < 17) {
1670 ret = GST_AV1_PARSER_NO_MORE_DATA;
1671 goto error;
1672 }
1673
1674 timecode->counting_type = AV1_READ_BITS (br, 5);
1675 timecode->full_timestamp_flag = AV1_READ_BIT (br);
1676 timecode->discontinuity_flag = AV1_READ_BIT (br);
1677 timecode->cnt_dropped_flag = AV1_READ_BIT (br);
1678 timecode->n_frames = AV1_READ_BITS (br, 9);
1679
1680 if (timecode->full_timestamp_flag) {
1681 if (AV1_REMAINING_BITS (br) < 17) {
1682 ret = GST_AV1_PARSER_NO_MORE_DATA;
1683 goto error;
1684 }
1685 timecode->seconds_value = AV1_READ_BITS (br, 6);
1686 timecode->minutes_value = AV1_READ_BITS (br, 6);
1687 timecode->hours_value = AV1_READ_BITS (br, 5);
1688 } else {
1689 timecode->seconds_flag = AV1_READ_BIT_CHECKED (br, &ret);
1690 if (ret != GST_AV1_PARSER_OK)
1691 goto error;
1692
1693 if (timecode->seconds_flag) {
1694 if (AV1_REMAINING_BITS (br) < 7) {
1695 ret = GST_AV1_PARSER_NO_MORE_DATA;
1696 goto error;
1697 }
1698 timecode->seconds_value = AV1_READ_BITS (br, 6);
1699 timecode->minutes_flag = AV1_READ_BIT (br);
1700
1701 if (timecode->minutes_flag) {
1702 if (AV1_REMAINING_BITS (br) < 7) {
1703 ret = GST_AV1_PARSER_NO_MORE_DATA;
1704 goto error;
1705 }
1706 timecode->minutes_value = AV1_READ_BITS (br, 6);
1707 timecode->hours_flag = AV1_READ_BIT (br);
1708
1709 if (timecode->hours_flag) {
1710 timecode->hours_value = AV1_READ_BITS_CHECKED (br, 6, &ret);
1711 if (ret != GST_AV1_PARSER_OK)
1712 goto error;
1713 }
1714 }
1715 }
1716 }
1717
1718 timecode->time_offset_length = AV1_READ_BITS_CHECKED (br, 5, &ret);
1719 if (ret != GST_AV1_PARSER_OK)
1720 goto error;
1721
1722 if (timecode->time_offset_length > 0) {
1723 timecode->time_offset_value =
1724 AV1_READ_BITS_CHECKED (br, timecode->time_offset_length, &ret);
1725 if (ret != GST_AV1_PARSER_OK)
1726 goto error;
1727 }
1728
1729 return GST_AV1_PARSER_OK;
1730
1731 error:
1732 GST_WARNING ("parse metadata timecode error %d", ret);
1733 return ret;
1734 }
1735
1736 /* 5.8.1 */
1737 /**
1738 * gst_av1_parser_parse_metadata_obu:
1739 * @parser: the #GstAV1Parser
1740 * @obu: a #GstAV1OBU to be parsed
1741 * @metadata: a #GstAV1MetadataOBU to store the parsed result.
1742 *
1743 * Parse one meta data @obu based on the @parser context.
1744 *
1745 * Returns: The #GstAV1ParserResult.
1746 *
1747 * Since: 1.18
1748 */
1749 GstAV1ParserResult
gst_av1_parser_parse_metadata_obu(GstAV1Parser * parser,GstAV1OBU * obu,GstAV1MetadataOBU * metadata)1750 gst_av1_parser_parse_metadata_obu (GstAV1Parser * parser, GstAV1OBU * obu,
1751 GstAV1MetadataOBU * metadata)
1752 {
1753 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
1754 GstBitReader bit_reader;
1755
1756 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1757 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1758 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_METADATA,
1759 GST_AV1_PARSER_INVALID_OPERATION);
1760 g_return_val_if_fail (metadata != NULL, GST_AV1_PARSER_INVALID_OPERATION);
1761
1762 gst_bit_reader_init (&bit_reader, obu->data, obu->obu_size);
1763
1764 memset (metadata, 0, sizeof (*metadata));
1765
1766 metadata->metadata_type = av1_bitstreamfn_leb128 (&bit_reader, &retval);
1767 if (retval != GST_AV1_PARSER_OK)
1768 goto error;
1769
1770 switch (metadata->metadata_type) {
1771 case GST_AV1_METADATA_TYPE_ITUT_T35:
1772 retval = gst_av1_parse_metadata_itut_t35 (parser,
1773 &bit_reader, &(metadata->itut_t35));
1774 break;
1775 case GST_AV1_METADATA_TYPE_HDR_CLL:
1776 retval = gst_av1_parse_metadata_hdr_cll (parser,
1777 &bit_reader, &(metadata->hdr_cll));
1778 break;
1779 case GST_AV1_METADATA_TYPE_HDR_MDCV:
1780 retval = gst_av1_parse_metadata_hdr_mdcv (parser,
1781 &bit_reader, &(metadata->hdr_mdcv));
1782 break;
1783 case GST_AV1_METADATA_TYPE_SCALABILITY:
1784 retval = gst_av1_parse_metadata_scalability (parser,
1785 &bit_reader, &(metadata->scalability));
1786 break;
1787 case GST_AV1_METADATA_TYPE_TIMECODE:
1788 retval = gst_av1_parse_metadata_timecode (parser,
1789 &bit_reader, &(metadata->timecode));
1790 break;
1791 default:
1792 return GST_AV1_PARSER_BITSTREAM_ERROR;
1793 }
1794
1795 if (retval != GST_AV1_PARSER_OK)
1796 goto error;
1797
1798 retval = av1_skip_trailing_bits (parser, &bit_reader, obu);
1799 if (retval != GST_AV1_PARSER_OK) {
1800 GST_WARNING ("Metadata type %d may have wrong trailings.",
1801 metadata->metadata_type);
1802 retval = GST_AV1_PARSER_OK;
1803 }
1804
1805 return retval;
1806
1807 error:
1808 GST_WARNING ("parse metadata error %d", retval);
1809 return retval;
1810 }
1811
1812 /* 5.9.8 */
1813 static GstAV1ParserResult
gst_av1_parse_superres_params_compute_image_size(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)1814 gst_av1_parse_superres_params_compute_image_size (GstAV1Parser * parser,
1815 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
1816 {
1817 GstAV1ParserResult ret;
1818 GstAV1SequenceHeaderOBU *seq_header;
1819
1820 g_assert (parser->seq_header);
1821 seq_header = parser->seq_header;
1822
1823 if (seq_header->enable_superres) {
1824 frame_header->use_superres = AV1_READ_BIT_CHECKED (br, &ret);
1825 if (ret != GST_AV1_PARSER_OK)
1826 return ret;
1827 } else {
1828 frame_header->use_superres = 0;
1829 }
1830
1831 if (frame_header->use_superres) {
1832 guint8 coded_denom;
1833 coded_denom = AV1_READ_BITS_CHECKED (br, GST_AV1_SUPERRES_DENOM_BITS, &ret);
1834 if (ret != GST_AV1_PARSER_OK)
1835 return ret;
1836
1837 frame_header->superres_denom = coded_denom + GST_AV1_SUPERRES_DENOM_MIN;
1838 } else {
1839 frame_header->superres_denom = GST_AV1_SUPERRES_NUM;
1840 }
1841 parser->state.upscaled_width = parser->state.frame_width;
1842 parser->state.frame_width =
1843 (parser->state.upscaled_width * GST_AV1_SUPERRES_NUM +
1844 (frame_header->superres_denom / 2)) / frame_header->superres_denom;
1845
1846 /* 5.9.9 compute_image_size */
1847 parser->state.mi_cols = 2 * ((parser->state.frame_width + 7) >> 3);
1848 parser->state.mi_rows = 2 * ((parser->state.frame_height + 7) >> 3);
1849
1850 return GST_AV1_PARSER_OK;
1851 }
1852
1853 /* 5.9.5 */
1854 static GstAV1ParserResult
gst_av1_parse_frame_size(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)1855 gst_av1_parse_frame_size (GstAV1Parser * parser, GstBitReader * br,
1856 GstAV1FrameHeaderOBU * frame_header)
1857 {
1858 GstAV1ParserResult retval;
1859 GstAV1SequenceHeaderOBU *seq_header;
1860
1861 g_assert (parser->seq_header);
1862 seq_header = parser->seq_header;
1863
1864 if (frame_header->frame_size_override_flag) {
1865 guint16 frame_width_minus_1;
1866 guint16 frame_height_minus_1;
1867
1868 if (AV1_REMAINING_BITS (br) <
1869 seq_header->frame_width_bits_minus_1 + 1 +
1870 seq_header->frame_height_bits_minus_1 + 1)
1871 return GST_AV1_PARSER_NO_MORE_DATA;
1872
1873 frame_width_minus_1 =
1874 AV1_READ_BITS (br, seq_header->frame_width_bits_minus_1 + 1);
1875 frame_height_minus_1 =
1876 AV1_READ_BITS (br, seq_header->frame_height_bits_minus_1 + 1);
1877 parser->state.frame_width = frame_width_minus_1 + 1;
1878 parser->state.frame_height = frame_height_minus_1 + 1;
1879 } else {
1880 parser->state.frame_width = seq_header->max_frame_width_minus_1 + 1;
1881 parser->state.frame_height = seq_header->max_frame_height_minus_1 + 1;
1882 }
1883
1884 retval = gst_av1_parse_superres_params_compute_image_size (parser, br,
1885 frame_header);
1886 return retval;
1887 }
1888
1889 /* 5.9.6 */
1890 static GstAV1ParserResult
gst_av1_parse_render_size(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)1891 gst_av1_parse_render_size (GstAV1Parser * parser, GstBitReader * br,
1892 GstAV1FrameHeaderOBU * frame_header)
1893 {
1894 GstAV1ParserResult retval;
1895
1896 frame_header->render_and_frame_size_different =
1897 AV1_READ_BIT_CHECKED (br, &retval);
1898 if (retval != GST_AV1_PARSER_OK)
1899 return retval;
1900
1901 if (frame_header->render_and_frame_size_different) {
1902 guint16 render_width_minus_1;
1903 guint16 render_height_minus_1;
1904
1905 if (AV1_REMAINING_BITS (br) < 16 + 16)
1906 return GST_AV1_PARSER_NO_MORE_DATA;
1907
1908 render_width_minus_1 = AV1_READ_UINT16 (br);
1909 render_height_minus_1 = AV1_READ_UINT16 (br);
1910 parser->state.render_width = render_width_minus_1 + 1;
1911 parser->state.render_height = render_height_minus_1 + 1;
1912 } else {
1913 parser->state.render_width = parser->state.upscaled_width;
1914 parser->state.render_height = parser->state.frame_height;
1915 }
1916
1917 return GST_AV1_PARSER_OK;
1918 }
1919
1920 /* 5.9.7 */
1921 static GstAV1ParserResult
gst_av1_parse_frame_size_with_refs(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)1922 gst_av1_parse_frame_size_with_refs (GstAV1Parser * parser,
1923 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
1924 {
1925 GstAV1ParserResult retval;
1926 GstAV1ReferenceFrameInfo *ref_info;
1927 gboolean found_ref = FALSE;
1928 gint i;
1929
1930 ref_info = &(parser->state.ref_info);
1931
1932 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
1933 found_ref = AV1_READ_BIT_CHECKED (br, &retval);
1934 if (retval != GST_AV1_PARSER_OK)
1935 return retval;
1936
1937 if (found_ref == 1) {
1938 gint ref_idx = frame_header->ref_frame_idx[i];
1939 parser->state.upscaled_width =
1940 ref_info->entry[ref_idx].ref_upscaled_width;
1941 parser->state.frame_width = parser->state.upscaled_width;
1942 parser->state.frame_height = ref_info->entry[ref_idx].ref_frame_height;
1943 parser->state.render_width = ref_info->entry[ref_idx].ref_render_width;
1944 parser->state.render_height = ref_info->entry[ref_idx].ref_render_height;
1945 break;
1946 }
1947 }
1948 if (found_ref == 0) {
1949 retval = gst_av1_parse_frame_size (parser, br, frame_header);
1950 if (retval != GST_AV1_PARSER_OK)
1951 return retval;
1952
1953 retval = gst_av1_parse_render_size (parser, br, frame_header);
1954 if (retval != GST_AV1_PARSER_OK)
1955 return retval;
1956 } else {
1957 retval = gst_av1_parse_superres_params_compute_image_size (parser, br,
1958 frame_header);
1959 if (retval != GST_AV1_PARSER_OK)
1960 return retval;
1961 }
1962
1963 return GST_AV1_PARSER_OK;
1964 }
1965
1966 /* 5.9.12 */
1967 static GstAV1ParserResult
gst_av1_parse_quantization_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)1968 gst_av1_parse_quantization_params (GstAV1Parser * parser, GstBitReader * br,
1969 GstAV1FrameHeaderOBU * frame_header)
1970 {
1971 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
1972 GstAV1ColorConfig *color_config;
1973 GstAV1QuantizationParams *quant_params = &(frame_header->quantization_params);
1974
1975 g_assert (parser->seq_header);
1976
1977 color_config = &(parser->seq_header->color_config);
1978
1979 quant_params->base_q_idx = AV1_READ_UINT8_CHECKED (br, &retval);
1980 if (retval != GST_AV1_PARSER_OK)
1981 goto error;
1982
1983 frame_header->quantization_params.delta_q_y_dc =
1984 av1_bitstreamfn_delta_q (br, &retval);
1985 if (retval != GST_AV1_PARSER_OK)
1986 goto error;
1987
1988 if (parser->seq_header->num_planes > 1) {
1989 if (color_config->separate_uv_delta_q) {
1990 quant_params->diff_uv_delta = AV1_READ_BIT_CHECKED (br, &retval);
1991 if (retval != GST_AV1_PARSER_OK)
1992 goto error;
1993 } else {
1994 quant_params->diff_uv_delta = 0;
1995 }
1996 frame_header->quantization_params.delta_q_u_dc =
1997 av1_bitstreamfn_delta_q (br, &retval);
1998 if (retval != GST_AV1_PARSER_OK)
1999 goto error;
2000
2001 frame_header->quantization_params.delta_q_u_ac =
2002 av1_bitstreamfn_delta_q (br, &retval);
2003 if (retval != GST_AV1_PARSER_OK)
2004 goto error;
2005
2006 if (quant_params->diff_uv_delta) {
2007 frame_header->quantization_params.delta_q_v_dc =
2008 av1_bitstreamfn_delta_q (br, &retval);
2009 if (retval != GST_AV1_PARSER_OK)
2010 goto error;
2011
2012 frame_header->quantization_params.delta_q_v_ac =
2013 av1_bitstreamfn_delta_q (br, &retval);
2014 if (retval != GST_AV1_PARSER_OK)
2015 goto error;
2016 } else {
2017 frame_header->quantization_params.delta_q_v_dc =
2018 frame_header->quantization_params.delta_q_u_dc;
2019 frame_header->quantization_params.delta_q_v_ac =
2020 frame_header->quantization_params.delta_q_u_ac;
2021 }
2022 } else {
2023 frame_header->quantization_params.delta_q_u_dc = 0;
2024 frame_header->quantization_params.delta_q_u_ac = 0;
2025 frame_header->quantization_params.delta_q_v_dc = 0;
2026 frame_header->quantization_params.delta_q_v_ac = 0;
2027 }
2028
2029 quant_params->using_qmatrix = AV1_READ_BIT_CHECKED (br, &retval);
2030 if (retval != GST_AV1_PARSER_OK)
2031 goto error;
2032
2033 if (quant_params->using_qmatrix) {
2034 if (AV1_REMAINING_BITS (br) < 4 + 4) {
2035 retval = GST_AV1_PARSER_NO_MORE_DATA;
2036 goto error;
2037 }
2038
2039 quant_params->qm_y = AV1_READ_BITS (br, 4);
2040 quant_params->qm_u = AV1_READ_BITS (br, 4);
2041
2042 if (!color_config->separate_uv_delta_q) {
2043 quant_params->qm_v = quant_params->qm_u;
2044 } else {
2045 quant_params->qm_v = AV1_READ_BITS_CHECKED (br, 4, &retval);
2046 if (retval != GST_AV1_PARSER_OK)
2047 goto error;
2048 }
2049 }
2050
2051 return GST_AV1_PARSER_OK;
2052
2053 error:
2054 GST_WARNING ("parse quantization params error %d", retval);
2055 return retval;
2056 }
2057
2058 /* 5.9.14 */
2059 static GstAV1ParserResult
gst_av1_parse_segmentation_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2060 gst_av1_parse_segmentation_params (GstAV1Parser * parser, GstBitReader * br,
2061 GstAV1FrameHeaderOBU * frame_header)
2062 {
2063 gint i, j;
2064 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
2065 gint clipped_value /* clippedValue */ ;
2066 GstAV1SegmenationParams *seg_params;
2067 gint feature_value = 0;
2068
2069 const guint8 segmentation_feature_bits[GST_AV1_SEG_LVL_MAX] = {
2070 8, 6, 6, 6, 6, 3, 0, 0
2071 };
2072 const guint8 segmentation_feature_signed[GST_AV1_SEG_LVL_MAX] = {
2073 1, 1, 1, 1, 1, 0, 0, 0
2074 };
2075 const guint8 segmentation_feature_max[GST_AV1_SEG_LVL_MAX] = {
2076 255, GST_AV1_MAX_LOOP_FILTER, GST_AV1_MAX_LOOP_FILTER,
2077 GST_AV1_MAX_LOOP_FILTER, GST_AV1_MAX_LOOP_FILTER, 7, 0, 0
2078 };
2079
2080 seg_params = &frame_header->segmentation_params;
2081
2082 seg_params->segmentation_enabled = AV1_READ_BIT_CHECKED (br, &retval);
2083 if (retval != GST_AV1_PARSER_OK)
2084 goto error;
2085
2086 if (seg_params->segmentation_enabled) {
2087 if (frame_header->primary_ref_frame == GST_AV1_PRIMARY_REF_NONE) {
2088 seg_params->segmentation_update_map = 1;
2089 seg_params->segmentation_temporal_update = 0;
2090 seg_params->segmentation_update_data = 1;
2091 } else {
2092 seg_params->segmentation_update_map = AV1_READ_BIT_CHECKED (br, &retval);
2093 if (retval != GST_AV1_PARSER_OK)
2094 goto error;
2095
2096 if (seg_params->segmentation_update_map) {
2097 seg_params->segmentation_temporal_update =
2098 AV1_READ_BIT_CHECKED (br, &retval);
2099 if (retval != GST_AV1_PARSER_OK)
2100 goto error;
2101 }
2102 seg_params->segmentation_update_data = AV1_READ_BIT_CHECKED (br, &retval);
2103 if (retval != GST_AV1_PARSER_OK)
2104 goto error;
2105 }
2106
2107 if (seg_params->segmentation_update_data) {
2108 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
2109 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
2110 seg_params->feature_enabled[i][j] =
2111 AV1_READ_BIT_CHECKED (br, &retval);
2112 if (retval != GST_AV1_PARSER_OK)
2113 goto error;
2114
2115 clipped_value = 0;
2116 feature_value = 0;
2117 if (seg_params->feature_enabled[i][j]) {
2118 gint bits_to_read = segmentation_feature_bits[j];
2119 gint limit = segmentation_feature_max[j];
2120 if (segmentation_feature_signed[j]) {
2121 feature_value =
2122 av1_bitstreamfn_su (br, 1 + bits_to_read, &retval);
2123 if (retval != GST_AV1_PARSER_OK)
2124 goto error;
2125
2126 clipped_value = CLAMP (feature_value, limit * (-1), limit);
2127 } else {
2128 feature_value = AV1_READ_BITS_CHECKED (br, bits_to_read, &retval);
2129 if (retval != GST_AV1_PARSER_OK)
2130 goto error;
2131
2132 clipped_value = CLAMP (feature_value, 0, limit);
2133 }
2134 }
2135 seg_params->feature_data[i][j] = clipped_value;
2136 }
2137 }
2138 } else {
2139 /* Copy it from prime_ref */
2140 g_assert (frame_header->primary_ref_frame != GST_AV1_PRIMARY_REF_NONE);
2141 g_assert (parser->state.ref_info.
2142 entry[frame_header->ref_frame_idx[frame_header->primary_ref_frame]].
2143 ref_valid);
2144 memcpy (seg_params,
2145 &parser->state.ref_info.
2146 entry[frame_header->ref_frame_idx[frame_header->
2147 primary_ref_frame]].ref_segmentation_params,
2148 sizeof (GstAV1SegmenationParams));
2149
2150 seg_params->segmentation_update_map = 0;
2151 seg_params->segmentation_temporal_update = 0;
2152 seg_params->segmentation_update_data = 0;
2153 }
2154 } else {
2155 seg_params->segmentation_update_map = 0;
2156 seg_params->segmentation_temporal_update = 0;
2157 seg_params->segmentation_update_data = 0;
2158 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
2159 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
2160 seg_params->feature_enabled[i][j] = 0;
2161 seg_params->feature_data[i][j] = 0;
2162 }
2163 }
2164 }
2165
2166 seg_params->seg_id_pre_skip = 0;
2167 seg_params->last_active_seg_id = 0;
2168 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
2169 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
2170 if (seg_params->feature_enabled[i][j]) {
2171 seg_params->last_active_seg_id = i;
2172 if (j >= GST_AV1_SEG_LVL_REF_FRAME) {
2173 seg_params->seg_id_pre_skip = 1;
2174 }
2175 }
2176 }
2177 }
2178
2179 return GST_AV1_PARSER_OK;
2180
2181 error:
2182 GST_WARNING ("parse segmentation params error %d", retval);
2183 return retval;
2184 }
2185
2186 /* 5.9.15 */
2187 static GstAV1ParserResult
gst_av1_parse_tile_info(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2188 gst_av1_parse_tile_info (GstAV1Parser * parser, GstBitReader * br,
2189 GstAV1FrameHeaderOBU * frame_header)
2190 {
2191 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
2192 GstAV1SequenceHeaderOBU *seq_header;
2193 GstAV1TileInfo *tile_info;
2194 gint i;
2195 gint start_sb /* startSb */ ;
2196 gint sb_cols /* sbCols */ ;
2197 gint sb_rows /* sbRows */ ;
2198 gint sb_shift /*sbShift */ ;
2199 gint sb_size /* sbSize */ ;
2200 gint max_tile_width_sb /* maxTileWidthSb */ ;
2201 gint max_tile_height_sb /* maxTileHeightSb */ ;
2202 gint max_tile_area_sb /* maxTileAreaSb */ ;
2203 gint min_log2_tile_cols /* minLog2TileCols */ ;
2204 gint max_log2_tile_cols /* maxLog2TileCols */ ;
2205 gint min_log2_tile_rows /* minLog2TileRows */ ;
2206 gint max_log2_tile_rows /* maxLog2TileRows */ ;
2207 gint min_log2_tiles /* minLog2Tiles */ ;
2208 gint tile_width_sb /* tileWidthSb */ ;
2209 gint tile_height_sb /* tileHeightSb */ ;
2210 gint max_width /* maxWidth */ , max_height /* maxHeight */ ;
2211 gint size_sb /* sizeSb */ ;
2212 gint widest_tile_sb /* widestTileSb */ ;
2213
2214 g_assert (parser->seq_header);
2215 seq_header = parser->seq_header;
2216 tile_info = &frame_header->tile_info;
2217
2218 sb_cols = seq_header->use_128x128_superblock ?
2219 ((parser->state.mi_cols + 31) >> 5) : ((parser->state.mi_cols + 15) >> 4);
2220 sb_rows = seq_header->use_128x128_superblock ? ((parser->state.mi_rows +
2221 31) >> 5) : ((parser->state.mi_rows + 15) >> 4);
2222 sb_shift = seq_header->use_128x128_superblock ? 5 : 4;
2223 sb_size = sb_shift + 2;
2224 max_tile_width_sb = GST_AV1_MAX_TILE_WIDTH >> sb_size;
2225 max_tile_area_sb = GST_AV1_MAX_TILE_AREA >> (2 * sb_size);
2226 min_log2_tile_cols = av1_helper_tile_log2 (max_tile_width_sb, sb_cols);
2227 max_log2_tile_cols = av1_helper_tile_log2 (1, MIN (sb_cols,
2228 GST_AV1_MAX_TILE_COLS));
2229 max_log2_tile_rows = av1_helper_tile_log2 (1, MIN (sb_rows,
2230 GST_AV1_MAX_TILE_ROWS));
2231 min_log2_tiles = MAX (min_log2_tile_cols,
2232 av1_helper_tile_log2 (max_tile_area_sb, sb_rows * sb_cols));
2233
2234 tile_info->uniform_tile_spacing_flag = AV1_READ_BIT_CHECKED (br, &retval);
2235 if (retval != GST_AV1_PARSER_OK)
2236 goto error;
2237
2238 if (tile_info->uniform_tile_spacing_flag) {
2239 parser->state.tile_cols_log2 = min_log2_tile_cols;
2240 while (parser->state.tile_cols_log2 < max_log2_tile_cols) {
2241 gint increment_tile_cols_log2 = AV1_READ_BIT_CHECKED (br, &retval);
2242 if (retval != GST_AV1_PARSER_OK)
2243 goto error;
2244
2245 if (increment_tile_cols_log2 == 1)
2246 parser->state.tile_cols_log2++;
2247 else
2248 break;
2249 }
2250 tile_width_sb = (sb_cols + (1 << parser->state.tile_cols_log2) -
2251 1) >> parser->state.tile_cols_log2;
2252 i = 0;
2253 for (start_sb = 0; start_sb < sb_cols; start_sb += tile_width_sb) {
2254 parser->state.mi_col_starts[i] = start_sb << sb_shift;
2255 i += 1;
2256 }
2257 parser->state.mi_col_starts[i] = parser->state.mi_cols;
2258 parser->state.tile_cols = i;
2259
2260 while (i >= 1) {
2261 tile_info->width_in_sbs_minus_1[i - 1] =
2262 ((parser->state.mi_col_starts[i] - parser->state.mi_col_starts[i - 1]
2263 + ((1 << sb_shift) - 1)) >> sb_shift) - 1;
2264 i--;
2265 }
2266
2267 min_log2_tile_rows = MAX (min_log2_tiles - parser->state.tile_cols_log2, 0);
2268 parser->state.tile_rows_log2 = min_log2_tile_rows;
2269 while (parser->state.tile_rows_log2 < max_log2_tile_rows) {
2270 tile_info->increment_tile_rows_log2 = AV1_READ_BIT_CHECKED (br, &retval);
2271 if (retval != GST_AV1_PARSER_OK)
2272 goto error;
2273
2274 if (tile_info->increment_tile_rows_log2 == 1)
2275 parser->state.tile_rows_log2++;
2276 else
2277 break;
2278 }
2279 tile_height_sb = (sb_rows + (1 << parser->state.tile_rows_log2) -
2280 1) >> parser->state.tile_rows_log2;
2281 i = 0;
2282 for (start_sb = 0; start_sb < sb_rows; start_sb += tile_height_sb) {
2283 parser->state.mi_row_starts[i] = start_sb << sb_shift;
2284 i += 1;
2285 }
2286 parser->state.mi_row_starts[i] = parser->state.mi_rows;
2287 parser->state.tile_rows = i;
2288 while (i >= 1) {
2289 tile_info->height_in_sbs_minus_1[i - 1] =
2290 ((parser->state.mi_row_starts[i] - parser->state.mi_row_starts[i - 1]
2291 + ((1 << sb_shift) - 1)) >> sb_shift) - 1;
2292 i--;
2293 }
2294 } else {
2295 widest_tile_sb = 0;
2296 start_sb = 0;
2297 for (i = 0; start_sb < sb_cols; i++) {
2298 parser->state.mi_col_starts[i] = start_sb << sb_shift;
2299 max_width = MIN (sb_cols - start_sb, max_tile_width_sb);
2300 tile_info->width_in_sbs_minus_1[i] =
2301 av1_bitstreamfn_ns (br, max_width, &retval);
2302 if (retval != GST_AV1_PARSER_OK)
2303 goto error;
2304
2305 size_sb = tile_info->width_in_sbs_minus_1[i] + 1;
2306 widest_tile_sb = MAX (size_sb, widest_tile_sb);
2307 start_sb += size_sb;
2308 }
2309 parser->state.mi_col_starts[i] = parser->state.mi_cols;
2310 parser->state.tile_cols = i;
2311 parser->state.tile_cols_log2 =
2312 av1_helper_tile_log2 (1, parser->state.tile_cols);
2313
2314 if (min_log2_tiles > 0)
2315 max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
2316 else
2317 max_tile_area_sb = sb_rows * sb_cols;
2318
2319 max_tile_height_sb = MAX (max_tile_area_sb / widest_tile_sb, 1);
2320
2321 start_sb = 0;
2322 for (i = 0; start_sb < sb_rows; i++) {
2323 parser->state.mi_row_starts[i] = start_sb << sb_shift;
2324 max_height = MIN (sb_rows - start_sb, max_tile_height_sb);
2325 tile_info->height_in_sbs_minus_1[i] =
2326 av1_bitstreamfn_ns (br, max_height, &retval);
2327 if (retval != GST_AV1_PARSER_OK)
2328 goto error;
2329
2330 size_sb = tile_info->height_in_sbs_minus_1[i] + 1;
2331 start_sb += size_sb;
2332 }
2333
2334 parser->state.mi_row_starts[i] = parser->state.mi_rows;
2335 parser->state.tile_rows = i;
2336 parser->state.tile_rows_log2 =
2337 av1_helper_tile_log2 (1, parser->state.tile_rows);
2338 }
2339
2340 if (parser->state.tile_cols_log2 > 0 || parser->state.tile_rows_log2 > 0) {
2341 tile_info->context_update_tile_id =
2342 AV1_READ_BITS_CHECKED (br,
2343 parser->state.tile_cols_log2 + parser->state.tile_rows_log2, &retval);
2344 if (retval != GST_AV1_PARSER_OK)
2345 goto error;
2346
2347 tile_info->tile_size_bytes_minus_1 = AV1_READ_BITS_CHECKED (br, 2, &retval);
2348 if (retval != GST_AV1_PARSER_OK)
2349 goto error;
2350
2351 parser->state.tile_size_bytes = tile_info->tile_size_bytes_minus_1 + 1;
2352 } else {
2353 tile_info->context_update_tile_id = 0;
2354 }
2355
2356 memcpy (tile_info->mi_col_starts, parser->state.mi_col_starts,
2357 sizeof (guint32) * (GST_AV1_MAX_TILE_COLS + 1));
2358 memcpy (tile_info->mi_row_starts, parser->state.mi_row_starts,
2359 sizeof (guint32) * (GST_AV1_MAX_TILE_ROWS + 1));
2360 tile_info->tile_cols_log2 = parser->state.tile_cols_log2;
2361 tile_info->tile_cols = parser->state.tile_cols;
2362 tile_info->tile_rows_log2 = parser->state.tile_rows_log2;
2363 tile_info->tile_rows = parser->state.tile_rows;
2364 tile_info->tile_size_bytes = parser->state.tile_size_bytes;
2365
2366 return GST_AV1_PARSER_OK;
2367
2368 error:
2369 GST_WARNING ("parse tile info error %d", retval);
2370 return retval;
2371 }
2372
2373 static GstAV1ParserResult
gst_av1_parse_loop_filter_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2374 gst_av1_parse_loop_filter_params (GstAV1Parser * parser,
2375 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
2376 {
2377 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
2378 GstAV1LoopFilterParams *lf_params;
2379 gint i;
2380 guint8 update_ref_deltas = 0;
2381 guint8 update_mode_deltas = 0;
2382
2383 g_assert (parser->seq_header);
2384
2385 lf_params = &frame_header->loop_filter_params;
2386
2387 if (frame_header->coded_lossless || frame_header->allow_intrabc) {
2388 lf_params->loop_filter_level[0] = 0;
2389 lf_params->loop_filter_level[1] = 0;
2390 lf_params->loop_filter_ref_deltas[GST_AV1_REF_INTRA_FRAME] = 1;
2391 lf_params->loop_filter_ref_deltas[GST_AV1_REF_LAST_FRAME] = 0;
2392 lf_params->loop_filter_ref_deltas[GST_AV1_REF_LAST2_FRAME] = 0;
2393 lf_params->loop_filter_ref_deltas[GST_AV1_REF_LAST3_FRAME] = 0;
2394 lf_params->loop_filter_ref_deltas[GST_AV1_REF_BWDREF_FRAME] = 0;
2395 lf_params->loop_filter_ref_deltas[GST_AV1_REF_GOLDEN_FRAME] = -1;
2396 lf_params->loop_filter_ref_deltas[GST_AV1_REF_ALTREF_FRAME] = -1;
2397 lf_params->loop_filter_ref_deltas[GST_AV1_REF_ALTREF2_FRAME] = -1;
2398 for (i = 0; i < 2; i++)
2399 lf_params->loop_filter_mode_deltas[i] = 0;
2400
2401 goto success;
2402 }
2403
2404 if (AV1_REMAINING_BITS (br) < 6 + 6) {
2405 retval = GST_AV1_PARSER_NO_MORE_DATA;
2406 goto error;
2407 }
2408
2409 lf_params->loop_filter_level[0] = AV1_READ_BITS (br, 6);
2410 lf_params->loop_filter_level[1] = AV1_READ_BITS (br, 6);
2411 if (parser->seq_header->num_planes > 1) {
2412 if (lf_params->loop_filter_level[0] || lf_params->loop_filter_level[1]) {
2413 if (AV1_REMAINING_BITS (br) < 6 + 6) {
2414 retval = GST_AV1_PARSER_NO_MORE_DATA;
2415 goto error;
2416 }
2417
2418 lf_params->loop_filter_level[2] = AV1_READ_BITS (br, 6);
2419 lf_params->loop_filter_level[3] = AV1_READ_BITS (br, 6);
2420 }
2421 }
2422
2423 if (AV1_REMAINING_BITS (br) < 3 + 1) {
2424 retval = GST_AV1_PARSER_NO_MORE_DATA;
2425 goto error;
2426 }
2427
2428 lf_params->loop_filter_sharpness = AV1_READ_BITS (br, 3);
2429
2430 lf_params->loop_filter_delta_enabled = AV1_READ_BIT (br);
2431 if (lf_params->loop_filter_delta_enabled) {
2432 lf_params->loop_filter_delta_update = AV1_READ_BIT_CHECKED (br, &retval);
2433 if (retval != GST_AV1_PARSER_OK)
2434 goto error;
2435
2436 if (lf_params->loop_filter_delta_update) {
2437 for (i = 0; i < GST_AV1_TOTAL_REFS_PER_FRAME; i++) {
2438 update_ref_deltas = AV1_READ_BIT_CHECKED (br, &retval);
2439 if (retval != GST_AV1_PARSER_OK)
2440 goto error;
2441
2442 if (update_ref_deltas) {
2443 lf_params->loop_filter_ref_deltas[i] =
2444 av1_bitstreamfn_su (br, 7, &retval);
2445 if (retval != GST_AV1_PARSER_OK)
2446 goto error;
2447 }
2448 }
2449 for (i = 0; i < 2; i++) {
2450 update_mode_deltas = AV1_READ_BIT_CHECKED (br, &retval);
2451 if (retval != GST_AV1_PARSER_OK)
2452 goto error;
2453
2454 if (update_mode_deltas) {
2455 lf_params->loop_filter_mode_deltas[i] =
2456 av1_bitstreamfn_su (br, 7, &retval);
2457 if (retval != GST_AV1_PARSER_OK)
2458 goto error;
2459 }
2460 }
2461 }
2462 }
2463
2464 success:
2465 return GST_AV1_PARSER_OK;
2466
2467 error:
2468 GST_WARNING ("parse loop filter params error %d", retval);
2469 return retval;
2470 }
2471
2472 /* 5.9.17 */
2473 static GstAV1ParserResult
gst_av1_parse_delta_q_params(GstAV1Parser * parser,GstBitReader * br,GstAV1QuantizationParams * quant_params)2474 gst_av1_parse_delta_q_params (GstAV1Parser * parser,
2475 GstBitReader * br, GstAV1QuantizationParams * quant_params)
2476 {
2477 GstAV1ParserResult retval;
2478
2479 quant_params->delta_q_res = 0;
2480 quant_params->delta_q_present = 0;
2481 if (quant_params->base_q_idx > 0) {
2482 quant_params->delta_q_present = AV1_READ_BIT_CHECKED (br, &retval);
2483 if (retval != GST_AV1_PARSER_OK)
2484 return retval;
2485 }
2486
2487 if (quant_params->delta_q_present) {
2488 quant_params->delta_q_res = AV1_READ_BITS_CHECKED (br, 2, &retval);
2489 if (retval != GST_AV1_PARSER_OK)
2490 return retval;
2491 }
2492
2493 return GST_AV1_PARSER_OK;
2494 }
2495
2496 /* 5.9.18 */
2497 static GstAV1ParserResult
gst_av1_parse_delta_lf_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2498 gst_av1_parse_delta_lf_params (GstAV1Parser * parser,
2499 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
2500 {
2501 GstAV1ParserResult retval;
2502 GstAV1LoopFilterParams *lf_params;
2503
2504 lf_params = &frame_header->loop_filter_params;
2505 lf_params->delta_lf_present = 0;
2506 lf_params->delta_lf_res = 0;
2507 lf_params->delta_lf_multi = 0;
2508
2509 if (frame_header->quantization_params.delta_q_present) {
2510 if (!frame_header->allow_intrabc) {
2511 lf_params->delta_lf_present = AV1_READ_BIT_CHECKED (br, &retval);
2512 if (retval != GST_AV1_PARSER_OK)
2513 return retval;
2514 }
2515 if (lf_params->delta_lf_present) {
2516 if (AV1_REMAINING_BITS (br) < 2 + 1)
2517 return GST_AV1_PARSER_NO_MORE_DATA;
2518 lf_params->delta_lf_res = AV1_READ_BITS (br, 2);
2519 lf_params->delta_lf_multi = AV1_READ_BIT (br);
2520 }
2521 }
2522
2523 return GST_AV1_PARSER_OK;
2524 }
2525
2526 /* 5.9.19 */
2527 static GstAV1ParserResult
gst_av1_parse_cdef_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2528 gst_av1_parse_cdef_params (GstAV1Parser * parser, GstBitReader * br,
2529 GstAV1FrameHeaderOBU * frame_header)
2530 {
2531 GstAV1SequenceHeaderOBU *seq_header;
2532 GstAV1CDEFParams *cdef_params;
2533 guint8 cdef_damping_minus_3;
2534 gint i;
2535
2536 g_assert (parser->seq_header);
2537
2538 cdef_params = &frame_header->cdef_params;
2539 seq_header = parser->seq_header;
2540
2541 if (frame_header->coded_lossless || frame_header->allow_intrabc
2542 || !seq_header->enable_cdef) {
2543 cdef_params->cdef_bits = 0;
2544 cdef_params->cdef_y_pri_strength[0] = 0;
2545 cdef_params->cdef_y_sec_strength[0] = 0;
2546 cdef_params->cdef_uv_pri_strength[0] = 0;
2547 cdef_params->cdef_uv_sec_strength[0] = 0;
2548 cdef_params->cdef_damping = 3;
2549 return GST_AV1_PARSER_OK;
2550 }
2551
2552 if (AV1_REMAINING_BITS (br) < 2 + 2)
2553 return GST_AV1_PARSER_NO_MORE_DATA;
2554
2555 cdef_damping_minus_3 = AV1_READ_BITS (br, 2);
2556 cdef_params->cdef_damping = cdef_damping_minus_3 + 3;
2557 cdef_params->cdef_bits = AV1_READ_BITS (br, 2);
2558 for (i = 0; i < (1 << cdef_params->cdef_bits); i++) {
2559 if (AV1_REMAINING_BITS (br) < 4 + 2)
2560 return GST_AV1_PARSER_NO_MORE_DATA;
2561
2562 cdef_params->cdef_y_pri_strength[i] = AV1_READ_BITS (br, 4);
2563 cdef_params->cdef_y_sec_strength[i] = AV1_READ_BITS (br, 2);
2564 if (cdef_params->cdef_y_sec_strength[i] == 3)
2565 cdef_params->cdef_y_sec_strength[i] += 1;
2566
2567 if (parser->seq_header->num_planes > 1) {
2568 if (AV1_REMAINING_BITS (br) < 4 + 2)
2569 return GST_AV1_PARSER_NO_MORE_DATA;
2570
2571 cdef_params->cdef_uv_pri_strength[i] = AV1_READ_BITS (br, 4);
2572 cdef_params->cdef_uv_sec_strength[i] = AV1_READ_BITS (br, 2);
2573 if (cdef_params->cdef_uv_sec_strength[i] == 3)
2574 cdef_params->cdef_uv_sec_strength[i] += 1;
2575 }
2576 }
2577
2578 return GST_AV1_PARSER_OK;
2579 }
2580
2581 /* 5.9.20 */
2582 static GstAV1ParserResult
gst_av1_parse_loop_restoration_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2583 gst_av1_parse_loop_restoration_params (GstAV1Parser * parser,
2584 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
2585 {
2586 GstAV1LoopRestorationParams *lr_params;
2587 GstAV1SequenceHeaderOBU *seq_header;
2588 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
2589 guint8 lr_type;
2590 gint i;
2591 guint8 use_chroma_lr /* useChromaLr */ ;
2592 const GstAV1FrameRestorationType remap_lr_type /* Remap_Lr_Type */ [4] = {
2593 GST_AV1_FRAME_RESTORE_NONE,
2594 GST_AV1_FRAME_RESTORE_SWITCHABLE,
2595 GST_AV1_FRAME_RESTORE_WIENER, GST_AV1_FRAME_RESTORE_SGRPROJ
2596 };
2597
2598 g_assert (parser->seq_header);
2599
2600 lr_params = &frame_header->loop_restoration_params;
2601 seq_header = parser->seq_header;
2602
2603 if (frame_header->all_lossless || frame_header->allow_intrabc
2604 || !seq_header->enable_restoration) {
2605 for (i = 0; i < GST_AV1_MAX_NUM_PLANES; i++)
2606 lr_params->frame_restoration_type[i] = GST_AV1_FRAME_RESTORE_NONE;
2607
2608 lr_params->uses_lr = 0;
2609 goto success;
2610 }
2611
2612 lr_params->uses_lr = 0;
2613 use_chroma_lr = 0;
2614 for (i = 0; i < seq_header->num_planes; i++) {
2615 lr_type = AV1_READ_BITS_CHECKED (br, 2, &retval);
2616 if (retval != GST_AV1_PARSER_OK)
2617 goto error;
2618
2619 lr_params->frame_restoration_type[i] = remap_lr_type[lr_type];
2620 if (lr_params->frame_restoration_type[i] != GST_AV1_FRAME_RESTORE_NONE) {
2621 lr_params->uses_lr = 1;
2622 if (i > 0) {
2623 use_chroma_lr = 1;
2624 }
2625 }
2626 }
2627
2628 if (lr_params->uses_lr) {
2629 if (seq_header->use_128x128_superblock) {
2630 lr_params->lr_unit_shift = AV1_READ_BIT_CHECKED (br, &retval);
2631 if (retval != GST_AV1_PARSER_OK)
2632 goto error;
2633
2634 lr_params->lr_unit_shift++;
2635 } else {
2636 guint8 lr_unit_extra_shift;
2637
2638 lr_params->lr_unit_shift = AV1_READ_BIT_CHECKED (br, &retval);
2639 if (retval != GST_AV1_PARSER_OK)
2640 goto error;
2641
2642 if (lr_params->lr_unit_shift) {
2643 lr_unit_extra_shift = AV1_READ_BIT_CHECKED (br, &retval);
2644 if (retval != GST_AV1_PARSER_OK)
2645 goto error;
2646
2647 lr_params->lr_unit_shift += lr_unit_extra_shift;
2648 }
2649 }
2650
2651 lr_params->loop_restoration_size[0] =
2652 GST_AV1_RESTORATION_TILESIZE_MAX >> (2 - lr_params->lr_unit_shift);
2653 if (seq_header->color_config.subsampling_x
2654 && seq_header->color_config.subsampling_y && use_chroma_lr) {
2655 lr_params->lr_uv_shift = AV1_READ_BIT_CHECKED (br, &retval);
2656 if (retval != GST_AV1_PARSER_OK)
2657 goto error;
2658 } else {
2659 lr_params->lr_uv_shift = 0;
2660 }
2661
2662 lr_params->loop_restoration_size[1] =
2663 lr_params->loop_restoration_size[0] >> lr_params->lr_uv_shift;
2664 lr_params->loop_restoration_size[2] =
2665 lr_params->loop_restoration_size[0] >> lr_params->lr_uv_shift;
2666 }
2667
2668 success:
2669 return GST_AV1_PARSER_OK;
2670
2671 error:
2672 GST_WARNING ("parse loop restoration params error %d", retval);
2673 return retval;
2674 }
2675
2676 /* 5.9.21 */
2677 static GstAV1ParserResult
gst_av1_parse_tx_mode(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2678 gst_av1_parse_tx_mode (GstAV1Parser * parser, GstBitReader * br,
2679 GstAV1FrameHeaderOBU * frame_header)
2680 {
2681 GstAV1ParserResult retval;
2682
2683 if (frame_header->coded_lossless == 1) {
2684 frame_header->tx_mode = GST_AV1_TX_MODE_ONLY_4x4;
2685 } else {
2686 frame_header->tx_mode_select = AV1_READ_BIT_CHECKED (br, &retval);
2687 if (retval != GST_AV1_PARSER_OK)
2688 return retval;
2689
2690 if (frame_header->tx_mode_select) {
2691 frame_header->tx_mode = GST_AV1_TX_MODE_SELECT;
2692 } else {
2693 frame_header->tx_mode = GST_AV1_TX_MODE_LARGEST;
2694 }
2695 }
2696
2697 return GST_AV1_PARSER_OK;
2698 }
2699
2700 /* 5.9.3 */
2701 static gint
gst_av1_get_relative_dist(GstAV1SequenceHeaderOBU * seq_header,gint a,gint b)2702 gst_av1_get_relative_dist (GstAV1SequenceHeaderOBU * seq_header, gint a, gint b)
2703 {
2704 gint m, diff;
2705 if (!seq_header->enable_order_hint)
2706 return 0;
2707 diff = a - b;
2708 m = 1 << seq_header->order_hint_bits_minus_1;
2709 diff = (diff & (m - 1)) - (diff & m);
2710 return diff;
2711 }
2712
2713 /* 5.9.22 */
2714 static GstAV1ParserResult
gst_av1_parse_skip_mode_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2715 gst_av1_parse_skip_mode_params (GstAV1Parser * parser, GstBitReader * br,
2716 GstAV1FrameHeaderOBU * frame_header)
2717 {
2718 GstAV1ReferenceFrameInfo *ref_info;
2719 GstAV1SequenceHeaderOBU *seq_header;
2720 gint i;
2721 gint skip_mode_allowed /* skipModeAllowed */ ;
2722 GstAV1ParserResult retval;
2723
2724 g_assert (parser->seq_header);
2725
2726 seq_header = parser->seq_header;
2727 ref_info = &(parser->state.ref_info);
2728 skip_mode_allowed = 0;
2729 if (frame_header->frame_is_intra || !frame_header->reference_select
2730 || !seq_header->enable_order_hint) {
2731 skip_mode_allowed = 0;
2732 } else {
2733 gint forward_idx = -1 /* forwardIdx */ ;
2734 gint forward_hint = 0 /* forwardHint */ ;
2735 gint backward_idx = -1 /* backwardIdx */ ;
2736 gint backward_hint = 0 /* backwardHint */ ;
2737 gint ref_hint = 0 /* refHint */ ;
2738
2739 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
2740 ref_hint = ref_info->entry[frame_header->ref_frame_idx[i]].ref_order_hint;
2741 if (gst_av1_get_relative_dist (parser->seq_header, ref_hint,
2742 frame_header->order_hint) < 0) {
2743 if (forward_idx < 0
2744 || gst_av1_get_relative_dist (parser->seq_header, ref_hint,
2745 forward_hint) > 0) {
2746 forward_idx = i;
2747 forward_hint = ref_hint;
2748 }
2749 } else
2750 if (gst_av1_get_relative_dist (parser->seq_header, ref_hint,
2751 frame_header->order_hint) > 0) {
2752 if (backward_idx < 0
2753 || gst_av1_get_relative_dist (parser->seq_header, ref_hint,
2754 backward_hint) < 0) {
2755 backward_idx = i;
2756 backward_hint = ref_hint;
2757 }
2758 }
2759 }
2760
2761 if (forward_idx < 0) {
2762 skip_mode_allowed = 0;
2763 } else if (backward_idx >= 0) {
2764 skip_mode_allowed = 1;
2765 frame_header->skip_mode_frame[0] =
2766 GST_AV1_REF_LAST_FRAME + MIN (forward_idx, backward_idx);
2767 frame_header->skip_mode_frame[1] =
2768 GST_AV1_REF_LAST_FRAME + MAX (forward_idx, backward_idx);
2769 } else {
2770 gint second_forward_idx = -1 /* secondForwardIdx */ ;
2771 gint second_forward_hint = 0 /* secondForwardHint */ ;
2772 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
2773 ref_hint =
2774 ref_info->entry[frame_header->ref_frame_idx[i]].ref_order_hint;
2775 if (gst_av1_get_relative_dist (parser->seq_header, ref_hint,
2776 forward_hint) < 0) {
2777 if (second_forward_idx < 0
2778 || gst_av1_get_relative_dist (parser->seq_header, ref_hint,
2779 second_forward_hint) > 0) {
2780 second_forward_idx = i;
2781 second_forward_hint = ref_hint;
2782 }
2783 }
2784 }
2785
2786 if (second_forward_idx < 0) {
2787 skip_mode_allowed = 0;
2788 } else {
2789 skip_mode_allowed = 1;
2790 frame_header->skip_mode_frame[0] =
2791 GST_AV1_REF_LAST_FRAME + MIN (forward_idx, second_forward_idx);
2792 frame_header->skip_mode_frame[1] =
2793 GST_AV1_REF_LAST_FRAME + MAX (forward_idx, second_forward_idx);
2794 }
2795 }
2796 }
2797
2798 if (skip_mode_allowed) {
2799 frame_header->skip_mode_present = AV1_READ_BIT_CHECKED (br, &retval);
2800 if (retval != GST_AV1_PARSER_OK)
2801 return retval;
2802 } else {
2803 frame_header->skip_mode_present = 0;
2804 }
2805
2806 return GST_AV1_PARSER_OK;
2807 }
2808
2809 /* 5.9.28 */
2810 static gint
gst_av1_decode_subexp(GstBitReader * br,gint numSyms,GstAV1ParserResult * retval)2811 gst_av1_decode_subexp (GstBitReader * br, gint numSyms,
2812 GstAV1ParserResult * retval)
2813 {
2814 gint i = 0;
2815 gint mk = 0;
2816 gint k = 3;
2817 gint subexp_final_bits = 0;
2818 gint subexp_more_bits = 0;
2819 gint subexp_bits = 0;
2820
2821 while (1) {
2822 gint b2 = i ? k + i - 1 : k;
2823 gint a = 1 << b2;
2824 if (numSyms <= mk + 3 * a) {
2825 subexp_final_bits = av1_bitstreamfn_ns (br, numSyms - mk, retval);
2826 if (*retval != GST_AV1_PARSER_OK)
2827 return 0;
2828 return subexp_final_bits + mk;
2829 } else {
2830 subexp_more_bits = AV1_READ_BITS_CHECKED (br, 1, retval);
2831 if (*retval != GST_AV1_PARSER_OK)
2832 return 0;
2833 if (subexp_more_bits) {
2834 i++;
2835 mk += a;
2836 } else {
2837 subexp_bits = AV1_READ_BITS_CHECKED (br, b2, retval);
2838 if (*retval != GST_AV1_PARSER_OK)
2839 return 0;
2840 return subexp_bits + mk;
2841 }
2842 }
2843 }
2844 }
2845
2846 /* 5.9.27 */
2847 static gint
gst_av1_decode_unsigned_subexp_with_ref(GstBitReader * br,gint mx,gint r,GstAV1ParserResult * retval)2848 gst_av1_decode_unsigned_subexp_with_ref (GstBitReader * br, gint mx,
2849 gint r, GstAV1ParserResult * retval)
2850 {
2851 gint v;
2852
2853 v = gst_av1_decode_subexp (br, mx, retval);
2854 if ((r << 1) <= mx) {
2855 return av1_helper_inverse_recenter (r, v);
2856 } else {
2857 return mx - 1 - av1_helper_inverse_recenter (mx - 1 - r, v);
2858 }
2859 }
2860
2861 /* 5.9.26 */
2862 static gint
gst_av1_decode_signed_subexp_with_ref(GstBitReader * br,gint low,gint high,gint r,GstAV1ParserResult * retval)2863 gst_av1_decode_signed_subexp_with_ref (GstBitReader * br, gint low,
2864 gint high, gint r, GstAV1ParserResult * retval)
2865 {
2866 return gst_av1_decode_unsigned_subexp_with_ref (br,
2867 high - low, r - low, retval) + low;
2868 }
2869
2870 /* 5.9.25 */
2871 static GstAV1ParserResult
gst_av1_parse_global_param(GstAV1Parser * parser,GstAV1FrameHeaderOBU * frame_header,GstBitReader * br,GstAV1GlobalMotionParams * gm_params,GstAV1WarpModelType type,gint32 prev_gm_params[GST_AV1_NUM_REF_FRAMES][6],gint ref,gint idx)2872 gst_av1_parse_global_param (GstAV1Parser * parser,
2873 GstAV1FrameHeaderOBU * frame_header, GstBitReader * br,
2874 GstAV1GlobalMotionParams * gm_params, GstAV1WarpModelType type,
2875 gint32 prev_gm_params[GST_AV1_NUM_REF_FRAMES][6], gint ref, gint idx)
2876 {
2877 GstAV1ParserResult retval;
2878 gint prec_diff /* precDiff */ , wm_round, mx, r;
2879 gint abs_bits /* absBits */ = GST_AV1_GM_ABS_ALPHA_BITS;
2880 gint prec_bits /* precBits */ = GST_AV1_GM_ALPHA_PREC_BITS;
2881 gint sub;
2882
2883 if (idx < 2) {
2884 if (type == GST_AV1_WARP_MODEL_TRANSLATION) {
2885 abs_bits =
2886 GST_AV1_GM_ABS_TRANS_ONLY_BITS -
2887 (frame_header->allow_high_precision_mv ? 0 : 1);
2888 prec_bits =
2889 GST_AV1_GM_TRANS_ONLY_PREC_BITS -
2890 (frame_header->allow_high_precision_mv ? 0 : 1);
2891 } else {
2892 abs_bits = GST_AV1_GM_ABS_TRANS_BITS;
2893 prec_bits = GST_AV1_GM_TRANS_PREC_BITS;
2894 }
2895 }
2896
2897 prec_diff = GST_AV1_WARPEDMODEL_PREC_BITS - prec_bits;
2898 wm_round = (idx % 3) == 2 ? (1 << GST_AV1_WARPEDMODEL_PREC_BITS) : 0;
2899 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
2900 mx = (1 << abs_bits);
2901 r = (prev_gm_params[ref][idx] >> prec_diff) - sub;
2902 gm_params->gm_params[ref][idx] =
2903 (gst_av1_decode_signed_subexp_with_ref (br, -mx, mx + 1, r,
2904 &retval) << prec_diff) + wm_round;
2905 if (retval != GST_AV1_PARSER_OK)
2906 return retval;
2907 return GST_AV1_PARSER_OK;
2908 }
2909
2910 static gboolean
gst_av1_parser_is_shear_params_valid(gint32 gm_params[6])2911 gst_av1_parser_is_shear_params_valid (gint32 gm_params[6])
2912 {
2913 const gint32 *mat = gm_params;
2914 gint16 alpha, beta, gamma, delta;
2915 gint16 shift;
2916 gint16 y;
2917 gint16 v;
2918 guint i;
2919 gboolean default_warp_params;
2920
2921 if (!(mat[2] > 0))
2922 return FALSE;
2923
2924 default_warp_params = TRUE;
2925 for (i = 0; i < 6; i++) {
2926 if (gm_params[i] != ((i % 3 == 2) ? 1 << GST_AV1_WARPEDMODEL_PREC_BITS : 0)) {
2927 default_warp_params = FALSE;
2928 break;
2929 }
2930 }
2931 if (default_warp_params)
2932 return TRUE;
2933
2934 alpha = CLAMP (mat[2] - (1 << GST_AV1_WARPEDMODEL_PREC_BITS),
2935 G_MININT16, G_MAXINT16);
2936 beta = CLAMP (mat[3], G_MININT16, G_MAXINT16);
2937 y = av1_helper_resolve_divisor_32 (ABS (mat[2]), &shift)
2938 * (mat[2] < 0 ? -1 : 1);
2939 v = ((gint64) mat[4] * (1 << GST_AV1_WARPEDMODEL_PREC_BITS)) * y;
2940 gamma =
2941 CLAMP ((gint) av1_helper_round_power_of_two_signed (v, shift), G_MININT16,
2942 G_MAXINT16);
2943 v = ((gint64) mat[3] * mat[4]) * y;
2944 delta =
2945 CLAMP (mat[5] - (gint) av1_helper_round_power_of_two_signed (v,
2946 shift) - (1 << GST_AV1_WARPEDMODEL_PREC_BITS), G_MININT16,
2947 G_MAXINT16);
2948
2949 alpha =
2950 av1_helper_round_power_of_two_signed (alpha,
2951 GST_AV1_WARP_PARAM_REDUCE_BITS) * (1 << GST_AV1_WARP_PARAM_REDUCE_BITS);
2952 beta =
2953 av1_helper_round_power_of_two_signed (beta,
2954 GST_AV1_WARP_PARAM_REDUCE_BITS) * (1 << GST_AV1_WARP_PARAM_REDUCE_BITS);
2955 gamma =
2956 av1_helper_round_power_of_two_signed (gamma,
2957 GST_AV1_WARP_PARAM_REDUCE_BITS) * (1 << GST_AV1_WARP_PARAM_REDUCE_BITS);
2958 delta =
2959 av1_helper_round_power_of_two_signed (delta,
2960 GST_AV1_WARP_PARAM_REDUCE_BITS) * (1 << GST_AV1_WARP_PARAM_REDUCE_BITS);
2961
2962 if ((4 * ABS (alpha) + 7 * ABS (beta) >= (1 << GST_AV1_WARPEDMODEL_PREC_BITS))
2963 || (4 * ABS (gamma) + 4 * ABS (delta) >=
2964 (1 << GST_AV1_WARPEDMODEL_PREC_BITS)))
2965 return FALSE;
2966
2967 return TRUE;
2968 }
2969
2970 /* 5.9.24 */
2971 static GstAV1ParserResult
gst_av1_parse_global_motion_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)2972 gst_av1_parse_global_motion_params (GstAV1Parser * parser,
2973 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
2974 {
2975 GstAV1WarpModelType type;
2976 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
2977 gint i, ref;
2978 GstAV1GlobalMotionParams *gm_params = &(frame_header->global_motion_params);
2979 gint32 prev_gm_params[GST_AV1_NUM_REF_FRAMES][6] /* PrevGmParams */ ;
2980
2981 /* init value */
2982 gm_params->gm_type[GST_AV1_REF_INTRA_FRAME] = GST_AV1_WARP_MODEL_IDENTITY;
2983 for (ref = GST_AV1_REF_LAST_FRAME; ref <= GST_AV1_REF_ALTREF_FRAME; ref++) {
2984 gm_params->invalid[ref] = 0;
2985 gm_params->gm_type[ref] = GST_AV1_WARP_MODEL_IDENTITY;
2986 for (i = 0; i < 6; i++) {
2987 gm_params->gm_params[ref][i] =
2988 ((i % 3 == 2) ? 1 << GST_AV1_WARPEDMODEL_PREC_BITS : 0);
2989 }
2990 }
2991
2992 if (frame_header->frame_is_intra)
2993 goto success;
2994
2995 if (frame_header->primary_ref_frame != GST_AV1_PRIMARY_REF_NONE) {
2996 GstAV1GlobalMotionParams *ref_global_motion_params =
2997 &parser->state.ref_info.entry[frame_header->
2998 ref_frame_idx[frame_header->primary_ref_frame]].
2999 ref_global_motion_params;
3000 memcpy (prev_gm_params, ref_global_motion_params->gm_params,
3001 sizeof (gint32) * GST_AV1_NUM_REF_FRAMES * 6);
3002 } else {
3003 for (ref = GST_AV1_REF_INTRA_FRAME; ref < GST_AV1_NUM_REF_FRAMES; ref++)
3004 for (i = 0; i < 6; i++)
3005 prev_gm_params[ref][i] =
3006 ((i % 3 == 2) ? 1 << GST_AV1_WARPEDMODEL_PREC_BITS : 0);
3007 }
3008
3009 for (ref = GST_AV1_REF_LAST_FRAME; ref <= GST_AV1_REF_ALTREF_FRAME; ref++) {
3010 gm_params->is_global[ref] = AV1_READ_BIT_CHECKED (br, &retval);
3011 if (retval != GST_AV1_PARSER_OK)
3012 goto error;
3013
3014 if (gm_params->is_global[ref]) {
3015 gm_params->is_rot_zoom[ref] = AV1_READ_BIT_CHECKED (br, &retval);
3016 if (retval != GST_AV1_PARSER_OK)
3017 goto error;
3018
3019 if (gm_params->is_rot_zoom[ref]) {
3020 type = GST_AV1_WARP_MODEL_ROTZOOM;
3021 } else {
3022 gm_params->is_translation[ref] = AV1_READ_BIT_CHECKED (br, &retval);
3023 if (retval != GST_AV1_PARSER_OK)
3024 goto error;
3025 type =
3026 gm_params->is_translation[ref] ? GST_AV1_WARP_MODEL_TRANSLATION :
3027 GST_AV1_WARP_MODEL_AFFINE;
3028 }
3029 } else {
3030 type = GST_AV1_WARP_MODEL_IDENTITY;
3031 }
3032 gm_params->gm_type[ref] = type;
3033
3034 if (type >= GST_AV1_WARP_MODEL_ROTZOOM) {
3035 retval =
3036 gst_av1_parse_global_param (parser, frame_header, br, gm_params, type,
3037 prev_gm_params, ref, 2);
3038 if (retval != GST_AV1_PARSER_OK)
3039 goto error;
3040
3041 retval =
3042 gst_av1_parse_global_param (parser, frame_header, br, gm_params, type,
3043 prev_gm_params, ref, 3);
3044 if (retval != GST_AV1_PARSER_OK)
3045 goto error;
3046
3047 if (type == GST_AV1_WARP_MODEL_AFFINE) {
3048 retval =
3049 gst_av1_parse_global_param (parser, frame_header, br, gm_params,
3050 type, prev_gm_params, ref, 4);
3051 if (retval != GST_AV1_PARSER_OK)
3052 goto error;
3053
3054 retval =
3055 gst_av1_parse_global_param (parser, frame_header, br, gm_params,
3056 type, prev_gm_params, ref, 5);
3057 if (retval != GST_AV1_PARSER_OK)
3058 goto error;
3059 } else {
3060 gm_params->gm_params[ref][4] = gm_params->gm_params[ref][3] * (-1);
3061 gm_params->gm_params[ref][5] = gm_params->gm_params[ref][2];
3062 }
3063 }
3064 if (type >= GST_AV1_WARP_MODEL_TRANSLATION) {
3065 retval =
3066 gst_av1_parse_global_param (parser, frame_header, br, gm_params, type,
3067 prev_gm_params, ref, 0);
3068 if (retval != GST_AV1_PARSER_OK)
3069 goto error;
3070 retval =
3071 gst_av1_parse_global_param (parser, frame_header, br, gm_params, type,
3072 prev_gm_params, ref, 1);
3073 if (retval != GST_AV1_PARSER_OK)
3074 goto error;
3075 }
3076
3077 if (type <= GST_AV1_WARP_MODEL_AFFINE)
3078 gm_params->invalid[ref] =
3079 !gst_av1_parser_is_shear_params_valid (gm_params->gm_params[ref]);
3080 }
3081
3082 success:
3083 return GST_AV1_PARSER_OK;
3084
3085 error:
3086 GST_WARNING ("parse global motion params error %d", retval);
3087 return retval;
3088 }
3089
3090 /* 5.9.30 */
3091 static GstAV1ParserResult
gst_av1_parse_film_grain_params(GstAV1Parser * parser,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)3092 gst_av1_parse_film_grain_params (GstAV1Parser * parser, GstBitReader * br,
3093 GstAV1FrameHeaderOBU * frame_header)
3094 {
3095 GstAV1FilmGrainParams *fg_params;
3096 GstAV1SequenceHeaderOBU *seq_header;
3097 gint i;
3098 gint num_pos_chroma /* numPosChroma */ , num_pos_luma /* numPosLuma */ ;
3099 GstAV1ParserResult ret = GST_AV1_PARSER_OK;
3100
3101 g_assert (parser->seq_header);
3102
3103 fg_params = &frame_header->film_grain_params;
3104 seq_header = parser->seq_header;
3105 if (!seq_header->film_grain_params_present || (!frame_header->show_frame
3106 && !frame_header->showable_frame)) {
3107 /* reset_grain_params() is a function call that indicates that all the
3108 syntax elements read in film_grain_params should be set equal to 0. */
3109 memset (fg_params, 0, sizeof (*fg_params));
3110 goto success;
3111 }
3112
3113 fg_params->apply_grain = AV1_READ_BIT_CHECKED (br, &ret);
3114 if (ret != GST_AV1_PARSER_OK)
3115 goto error;
3116 if (!fg_params->apply_grain) {
3117 /* reset_grain_params() */
3118 memset (fg_params, 0, sizeof (*fg_params));
3119 goto success;
3120 }
3121
3122 fg_params->grain_seed = AV1_READ_UINT16_CHECKED (br, &ret);
3123 if (ret != GST_AV1_PARSER_OK)
3124 goto error;
3125
3126 if (frame_header->frame_type == GST_AV1_INTER_FRAME) {
3127 fg_params->update_grain = AV1_READ_BIT_CHECKED (br, &ret);
3128 if (ret != GST_AV1_PARSER_OK)
3129 goto error;
3130 } else {
3131 fg_params->update_grain = 1;
3132 }
3133
3134 if (!fg_params->update_grain) {
3135 guint16 temp_grain_seed /* tempGrainSeed */ ;
3136 gint j;
3137 gboolean found = FALSE;
3138
3139 fg_params->film_grain_params_ref_idx = AV1_READ_BITS_CHECKED (br, 3, &ret);
3140 if (ret != GST_AV1_PARSER_OK)
3141 goto error;
3142
3143 for (j = 0; j < GST_AV1_REFS_PER_FRAME; j++) {
3144 if (frame_header->ref_frame_idx[j] ==
3145 fg_params->film_grain_params_ref_idx) {
3146 found = TRUE;
3147 break;
3148 }
3149 }
3150
3151 if (!found) {
3152 GST_INFO ("Invalid film grain reference idx %d.",
3153 fg_params->film_grain_params_ref_idx);
3154 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
3155 goto error;
3156 }
3157
3158 if (!parser->state.ref_info.entry[fg_params->film_grain_params_ref_idx].
3159 ref_valid) {
3160 GST_INFO ("Invalid ref info of film grain idx %d.",
3161 fg_params->film_grain_params_ref_idx);
3162 ret = GST_AV1_PARSER_BITSTREAM_ERROR;
3163 goto error;
3164 }
3165
3166 temp_grain_seed = fg_params->grain_seed;
3167 memcpy (fg_params,
3168 &parser->state.ref_info.entry[fg_params->film_grain_params_ref_idx].
3169 ref_film_grain_params, sizeof (GstAV1FilmGrainParams));
3170 fg_params->grain_seed = temp_grain_seed;
3171
3172 goto success;
3173 }
3174
3175 fg_params->num_y_points = AV1_READ_BITS_CHECKED (br, 4, &ret);
3176 if (ret != GST_AV1_PARSER_OK)
3177 goto error;
3178
3179 for (i = 0; i < fg_params->num_y_points; i++) {
3180 if (AV1_REMAINING_BITS (br) < 8 + 8) {
3181 ret = GST_AV1_PARSER_NO_MORE_DATA;
3182 goto error;
3183 }
3184 fg_params->point_y_value[i] = AV1_READ_UINT8 (br);
3185 fg_params->point_y_scaling[i] = AV1_READ_UINT8 (br);
3186 }
3187
3188 if (seq_header->color_config.mono_chrome) {
3189 fg_params->chroma_scaling_from_luma = 0;
3190 } else {
3191 fg_params->chroma_scaling_from_luma = AV1_READ_BIT_CHECKED (br, &ret);
3192 if (ret != GST_AV1_PARSER_OK)
3193 goto error;
3194 }
3195
3196 if (seq_header->color_config.mono_chrome
3197 || fg_params->chroma_scaling_from_luma
3198 || (seq_header->color_config.subsampling_x == 1
3199 && seq_header->color_config.subsampling_y == 1
3200 && fg_params->num_y_points == 0)) {
3201 fg_params->num_cb_points = 0;
3202 fg_params->num_cr_points = 0;
3203 } else {
3204 fg_params->num_cb_points = AV1_READ_BITS_CHECKED (br, 4, &ret);
3205 if (ret != GST_AV1_PARSER_OK)
3206 goto error;
3207 for (i = 0; i < fg_params->num_cb_points; i++) {
3208 if (AV1_REMAINING_BITS (br) < 8 + 8) {
3209 ret = GST_AV1_PARSER_NO_MORE_DATA;
3210 goto error;
3211 }
3212 fg_params->point_cb_value[i] = AV1_READ_UINT8 (br);
3213 fg_params->point_cb_scaling[i] = AV1_READ_UINT8 (br);
3214 }
3215
3216 fg_params->num_cr_points = AV1_READ_BITS_CHECKED (br, 4, &ret);
3217 if (ret != GST_AV1_PARSER_OK)
3218 goto error;
3219 for (i = 0; i < fg_params->num_cr_points; i++) {
3220 if (AV1_REMAINING_BITS (br) < 8 + 8) {
3221 ret = GST_AV1_PARSER_NO_MORE_DATA;
3222 goto error;
3223 }
3224 fg_params->point_cr_value[i] = AV1_READ_UINT8 (br);
3225 fg_params->point_cr_scaling[i] = AV1_READ_UINT8 (br);
3226 }
3227 }
3228
3229 fg_params->grain_scaling_minus_8 = AV1_READ_BITS_CHECKED (br, 2, &ret);
3230 if (ret != GST_AV1_PARSER_OK)
3231 goto error;
3232
3233 fg_params->ar_coeff_lag = AV1_READ_BITS_CHECKED (br, 2, &ret);
3234 if (ret != GST_AV1_PARSER_OK)
3235 goto error;
3236
3237 num_pos_luma = 2 * fg_params->ar_coeff_lag * (fg_params->ar_coeff_lag + 1);
3238 if (fg_params->num_y_points) {
3239 num_pos_chroma = num_pos_luma + 1;
3240 for (i = 0; i < num_pos_luma; i++) {
3241 fg_params->ar_coeffs_y_plus_128[i] = AV1_READ_UINT8_CHECKED (br, &ret);
3242 if (ret != GST_AV1_PARSER_OK)
3243 goto error;
3244 }
3245 } else {
3246 num_pos_chroma = num_pos_luma;
3247 }
3248
3249 if (fg_params->chroma_scaling_from_luma || fg_params->num_cb_points) {
3250 for (i = 0; i < num_pos_chroma; i++) {
3251 fg_params->ar_coeffs_cb_plus_128[i] = AV1_READ_UINT8_CHECKED (br, &ret);
3252 if (ret != GST_AV1_PARSER_OK)
3253 goto error;
3254 }
3255 }
3256
3257 if (fg_params->chroma_scaling_from_luma || fg_params->num_cr_points) {
3258 for (i = 0; i < num_pos_chroma; i++) {
3259 fg_params->ar_coeffs_cr_plus_128[i] = AV1_READ_UINT8_CHECKED (br, &ret);
3260 if (ret != GST_AV1_PARSER_OK)
3261 goto error;
3262 }
3263 }
3264
3265 if (AV1_REMAINING_BITS (br) < 2 + 2) {
3266 ret = GST_AV1_PARSER_NO_MORE_DATA;
3267 goto error;
3268 }
3269 fg_params->ar_coeff_shift_minus_6 = AV1_READ_BITS (br, 2);
3270 fg_params->grain_scale_shift = AV1_READ_BITS (br, 2);
3271
3272 if (fg_params->num_cb_points) {
3273 if (AV1_REMAINING_BITS (br) < 8 + 8 + 9) {
3274 ret = GST_AV1_PARSER_NO_MORE_DATA;
3275 goto error;
3276 }
3277 fg_params->cb_mult = AV1_READ_BITS (br, 8);
3278 fg_params->cb_luma_mult = AV1_READ_BITS (br, 8);
3279 fg_params->cb_offset = AV1_READ_BITS (br, 9);
3280 }
3281
3282 if (fg_params->num_cr_points) {
3283 if (AV1_REMAINING_BITS (br) < 8 + 8 + 9) {
3284 ret = GST_AV1_PARSER_NO_MORE_DATA;
3285 goto error;
3286 }
3287 fg_params->cr_mult = AV1_READ_BITS (br, 8);
3288 fg_params->cr_luma_mult = AV1_READ_BITS (br, 8);
3289 fg_params->cr_offset = AV1_READ_BITS (br, 9);
3290 }
3291
3292 if (AV1_REMAINING_BITS (br) < 2) {
3293 ret = GST_AV1_PARSER_NO_MORE_DATA;
3294 goto error;
3295 }
3296 fg_params->overlap_flag = AV1_READ_BIT (br);
3297 fg_params->clip_to_restricted_range = AV1_READ_BIT (br);
3298
3299 success:
3300 return GST_AV1_PARSER_OK;
3301
3302 error:
3303 GST_WARNING ("parse film grain params error %d", ret);
3304 return ret;
3305 }
3306
3307 /* 5.9.4 */
3308 static void
gst_av1_mark_ref_frames(GstAV1Parser * parser,GstBitReader * br,gint idLen)3309 gst_av1_mark_ref_frames (GstAV1Parser * parser, GstBitReader * br, gint idLen)
3310 {
3311 GstAV1ReferenceFrameInfo *ref_info;
3312 GstAV1SequenceHeaderOBU *seq_header;
3313 gint i, diff_len /* diffLen */ ;
3314
3315 seq_header = parser->seq_header;
3316 ref_info = &(parser->state.ref_info);
3317 diff_len = seq_header->delta_frame_id_length_minus_2 + 2;
3318
3319 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3320 if (parser->state.current_frame_id > (1 << diff_len)) {
3321 if (ref_info->entry[i].ref_frame_id > parser->state.current_frame_id
3322 || ref_info->entry[i].ref_frame_id <
3323 (parser->state.current_frame_id - (1 << diff_len)))
3324 ref_info->entry[i].ref_valid = 0;
3325 } else {
3326 if (ref_info->entry[i].ref_frame_id > parser->state.current_frame_id
3327 && ref_info->entry[i].ref_frame_id <
3328 ((1 << idLen) + parser->state.current_frame_id - (1 << diff_len)))
3329 ref_info->entry[i].ref_valid = 0;
3330 }
3331 }
3332 }
3333
3334 /* 5.11.14 */
3335 static gboolean
gst_av1_seg_feature_active_idx(GstAV1Parser * parser,GstAV1FrameHeaderOBU * frame_header,gint idx,gint feature)3336 gst_av1_seg_feature_active_idx (GstAV1Parser * parser,
3337 GstAV1FrameHeaderOBU * frame_header, gint idx, gint feature)
3338 {
3339 return frame_header->segmentation_params.segmentation_enabled
3340 && frame_header->segmentation_params.feature_enabled[idx][feature];
3341 }
3342
3343 /* 7.12.2 */
3344 static gint
gst_av1_get_qindex(GstAV1Parser * parser,GstAV1FrameHeaderOBU * frame_header,gboolean ignoreDeltaQ,gint segmentId)3345 gst_av1_get_qindex (GstAV1Parser * parser,
3346 GstAV1FrameHeaderOBU * frame_header, gboolean ignoreDeltaQ, gint segmentId)
3347 {
3348 gint qindex;
3349 if (gst_av1_seg_feature_active_idx (parser, frame_header, segmentId,
3350 GST_AV1_SEG_LVL_ALT_Q)) {
3351 gint data =
3352 frame_header->
3353 segmentation_params.feature_data[segmentId][GST_AV1_SEG_LVL_ALT_Q];
3354 qindex = frame_header->quantization_params.base_q_idx + data;
3355 if (ignoreDeltaQ == 0 && frame_header->quantization_params.delta_q_present)
3356 qindex = qindex + frame_header->quantization_params.delta_q_res;
3357 return CLAMP (qindex, 0, 255);
3358 } else
3359 return frame_header->quantization_params.base_q_idx;
3360 }
3361
3362 /* 7.8 */
3363 static void
gst_av1_set_frame_refs(GstAV1Parser * parser,GstAV1SequenceHeaderOBU * seq_header,GstAV1FrameHeaderOBU * frame_header)3364 gst_av1_set_frame_refs (GstAV1Parser * parser,
3365 GstAV1SequenceHeaderOBU * seq_header, GstAV1FrameHeaderOBU * frame_header)
3366 {
3367 const GstAV1ReferenceFrame ref_frame_list[GST_AV1_REFS_PER_FRAME - 2] = {
3368 GST_AV1_REF_LAST2_FRAME,
3369 GST_AV1_REF_LAST3_FRAME,
3370 GST_AV1_REF_BWDREF_FRAME,
3371 GST_AV1_REF_ALTREF2_FRAME,
3372 GST_AV1_REF_ALTREF_FRAME
3373 };
3374 gboolean used_frame[GST_AV1_NUM_REF_FRAMES];
3375 gint shifted_order_hints[GST_AV1_NUM_REF_FRAMES];
3376 gint cur_frame_hint = 1 << (seq_header->order_hint_bits - 1);
3377 gint last_order_hint, earliest_order_hint;
3378 gint ref, hint;
3379 gint i, j;
3380
3381 g_assert (seq_header->enable_order_hint);
3382 g_assert (seq_header->order_hint_bits_minus_1 >= 0);
3383
3384 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++)
3385 frame_header->ref_frame_idx[i] = -1;
3386 frame_header->ref_frame_idx[GST_AV1_REF_LAST_FRAME -
3387 GST_AV1_REF_LAST_FRAME] = frame_header->last_frame_idx;
3388 frame_header->ref_frame_idx[GST_AV1_REF_GOLDEN_FRAME -
3389 GST_AV1_REF_LAST_FRAME] = frame_header->gold_frame_idx;
3390
3391 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++)
3392 used_frame[i] = 0;
3393 used_frame[frame_header->last_frame_idx] = 1;
3394 used_frame[frame_header->gold_frame_idx] = 1;
3395
3396 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++)
3397 shifted_order_hints[i] = cur_frame_hint +
3398 gst_av1_get_relative_dist (seq_header,
3399 parser->state.ref_info.entry[i].ref_order_hint,
3400 frame_header->order_hint);
3401
3402 last_order_hint = shifted_order_hints[frame_header->last_frame_idx];
3403
3404 /* === Backward Reference Frames === */
3405 /* The ALTREF_FRAME reference is set to be a backward
3406 reference to the frame with highest output order. */
3407 ref = -1;
3408 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3409 hint = shifted_order_hints[i];
3410 if (!used_frame[i] && hint >= cur_frame_hint
3411 && (ref < 0 || hint >= last_order_hint)) {
3412 ref = i;
3413 last_order_hint = hint;
3414 }
3415 }
3416 if (ref >= 0) {
3417 frame_header->ref_frame_idx[GST_AV1_REF_ALTREF_FRAME -
3418 GST_AV1_REF_LAST_FRAME] = ref;
3419 used_frame[ref] = 1;
3420 }
3421
3422 /* The BWDREF_FRAME reference is set to be a backward reference
3423 to the closest frame. */
3424 ref = -1;
3425 earliest_order_hint = last_order_hint;
3426 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3427 hint = shifted_order_hints[i];
3428 if (!used_frame[i] && hint >= cur_frame_hint
3429 && (ref < 0 || hint < earliest_order_hint)) {
3430 ref = i;
3431 earliest_order_hint = hint;
3432 }
3433 }
3434 if (ref >= 0) {
3435 frame_header->ref_frame_idx[GST_AV1_REF_BWDREF_FRAME -
3436 GST_AV1_REF_LAST_FRAME] = ref;
3437 used_frame[ref] = 1;
3438 }
3439
3440 /* The ALTREF2_FRAME reference is set to the next closest
3441 backward reference. */
3442 ref = -1;
3443 earliest_order_hint = last_order_hint;
3444 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3445 hint = shifted_order_hints[i];
3446 if (!used_frame[i] && hint >= cur_frame_hint
3447 && (ref < 0 || hint < earliest_order_hint)) {
3448 ref = i;
3449 earliest_order_hint = hint;
3450 }
3451 }
3452 if (ref >= 0) {
3453 frame_header->ref_frame_idx[GST_AV1_REF_ALTREF2_FRAME -
3454 GST_AV1_REF_LAST_FRAME] = ref;
3455 used_frame[ref] = 1;
3456 }
3457
3458 /* === Forward Reference Frames === */
3459
3460 /* The remaining references are set to be forward references
3461 in anti-chronological order. */
3462 last_order_hint = 0;
3463 for (i = 0; i < GST_AV1_REFS_PER_FRAME - 2; i++) {
3464 GstAV1ReferenceFrame ref_frame = ref_frame_list[i];
3465 if (frame_header->ref_frame_idx[ref_frame - GST_AV1_REF_LAST_FRAME] < 0) {
3466 ref = -1;
3467 for (j = 0; j < GST_AV1_NUM_REF_FRAMES; j++) {
3468 hint = shifted_order_hints[j];
3469 if (!used_frame[j] && hint < cur_frame_hint &&
3470 (ref < 0 || hint >= last_order_hint)) {
3471 ref = j;
3472 last_order_hint = hint;
3473 }
3474 }
3475
3476 if (ref >= 0) {
3477 frame_header->ref_frame_idx[ref_frame - GST_AV1_REF_LAST_FRAME] = ref;
3478 used_frame[ref] = 1;
3479 }
3480 }
3481 }
3482
3483 /* Finally, any remaining references are set to the reference frame
3484 with smallest output order. */
3485 ref = -1;
3486 earliest_order_hint = cur_frame_hint * 2;
3487 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3488 hint = shifted_order_hints[i];
3489 if (ref < 0 || hint < earliest_order_hint) {
3490 ref = i;
3491 earliest_order_hint = hint;
3492 }
3493 }
3494 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++)
3495 if (frame_header->ref_frame_idx[i] < 0)
3496 frame_header->ref_frame_idx[i] = ref;
3497 }
3498
3499 /* 5.9.2 */
3500 static GstAV1ParserResult
gst_av1_parse_uncompressed_frame_header(GstAV1Parser * parser,GstAV1OBU * obu,GstBitReader * br,GstAV1FrameHeaderOBU * frame_header)3501 gst_av1_parse_uncompressed_frame_header (GstAV1Parser * parser, GstAV1OBU * obu,
3502 GstBitReader * br, GstAV1FrameHeaderOBU * frame_header)
3503 {
3504 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
3505 GstAV1ReferenceFrameInfo *ref_info;
3506 GstAV1SequenceHeaderOBU *seq_header;
3507 gint i, op_num /* opNum */ ;
3508 gint segment_id /* segmentId */ , all_frames /* allFrames */ ;
3509 gint id_len /* idLen */ = 0;
3510
3511 if (!parser->seq_header) {
3512 GST_WARNING ("Missing OBU Reference: seq_header");
3513 retval = GST_AV1_PARSER_MISSING_OBU_REFERENCE;
3514 goto error;
3515 }
3516
3517 seq_header = parser->seq_header;
3518 ref_info = &(parser->state.ref_info);
3519 if (seq_header->frame_id_numbers_present_flag)
3520 id_len = seq_header->additional_frame_id_length_minus_1 + 1 +
3521 seq_header->delta_frame_id_length_minus_2 + 2;
3522 all_frames = (1 << GST_AV1_NUM_REF_FRAMES) - 1;
3523
3524 if (seq_header->reduced_still_picture_header) {
3525 frame_header->show_existing_frame = 0;
3526 frame_header->frame_type = GST_AV1_KEY_FRAME;
3527 frame_header->frame_is_intra = 1;
3528 frame_header->show_frame = 1;
3529 frame_header->showable_frame = 0;
3530 if (parser->state.sequence_changed) {
3531 /* This is the start of a new coded video sequence. */
3532 parser->state.sequence_changed = 0;
3533 parser->state.begin_first_frame = 1;
3534 }
3535 } else {
3536 frame_header->show_existing_frame = AV1_READ_BIT_CHECKED (br, &retval);
3537 if (retval != GST_AV1_PARSER_OK)
3538 goto error;
3539
3540 if (frame_header->show_existing_frame) {
3541 if (parser->state.sequence_changed) {
3542 GST_INFO ("New sequence header starts with a show_existing_frame.");
3543 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3544 goto error;
3545 }
3546
3547 frame_header->frame_to_show_map_idx =
3548 AV1_READ_BITS_CHECKED (br, 3, &retval);
3549 if (retval != GST_AV1_PARSER_OK)
3550 goto error;
3551
3552 if (!ref_info->entry[frame_header->frame_to_show_map_idx].ref_valid) {
3553 GST_INFO ("The frame_to_show %d is invalid.",
3554 frame_header->frame_to_show_map_idx);
3555 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3556 goto error;
3557 }
3558
3559 if (seq_header->decoder_model_info_present_flag
3560 && !seq_header->timing_info.equal_picture_interval)
3561 frame_header->frame_presentation_time =
3562 AV1_READ_BITS_CHECKED (br,
3563 seq_header->
3564 decoder_model_info.frame_presentation_time_length_minus_1 + 1,
3565 &retval);
3566 if (retval != GST_AV1_PARSER_OK)
3567 goto error;
3568
3569 frame_header->refresh_frame_flags = 0;
3570 if (seq_header->frame_id_numbers_present_flag) {
3571 g_assert (id_len > 0);
3572 frame_header->display_frame_id =
3573 AV1_READ_BITS_CHECKED (br, id_len, &retval);
3574 if (retval != GST_AV1_PARSER_OK)
3575 goto error;
3576 if (frame_header->display_frame_id !=
3577 ref_info->entry[frame_header->frame_to_show_map_idx].ref_frame_id) {
3578 GST_INFO ("Reference frame ID mismatch");
3579 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3580 goto error;
3581 }
3582 }
3583
3584 frame_header->frame_type =
3585 ref_info->entry[frame_header->frame_to_show_map_idx].ref_frame_type;
3586 if (frame_header->frame_type == GST_AV1_KEY_FRAME) {
3587 frame_header->refresh_frame_flags = all_frames;
3588 }
3589
3590 /* just use the frame_to_show's grain_params
3591 * if (seq_header->film_grain_params_present)
3592 * load_grain_params () */
3593
3594 goto success;
3595 }
3596
3597 frame_header->frame_type = AV1_READ_BITS_CHECKED (br, 2, &retval);
3598 if (retval != GST_AV1_PARSER_OK)
3599 goto error;
3600
3601 if (parser->state.sequence_changed) {
3602 if (frame_header->frame_type == GST_AV1_KEY_FRAME) {
3603 /* This is the start of a new coded video sequence. */
3604 parser->state.sequence_changed = FALSE;
3605 parser->state.begin_first_frame = TRUE;
3606 } else {
3607 GST_INFO ("Sequence header has changed without a keyframe.");
3608 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3609 goto error;
3610 }
3611 }
3612
3613 frame_header->frame_is_intra =
3614 (frame_header->frame_type == GST_AV1_INTRA_ONLY_FRAME
3615 || frame_header->frame_type == GST_AV1_KEY_FRAME);
3616
3617 frame_header->show_frame = AV1_READ_BIT_CHECKED (br, &retval);
3618 if (retval != GST_AV1_PARSER_OK)
3619 goto error;
3620
3621 if (seq_header->still_picture &&
3622 (frame_header->frame_type != GST_AV1_KEY_FRAME
3623 || !frame_header->show_frame)) {
3624 GST_INFO ("Still pictures must be coded as shown keyframes");
3625 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3626 goto error;
3627 }
3628
3629 if (frame_header->show_frame
3630 && seq_header->decoder_model_info_present_flag
3631 && !seq_header->timing_info.equal_picture_interval) {
3632 frame_header->frame_presentation_time =
3633 AV1_READ_BITS_CHECKED (br,
3634 seq_header->decoder_model_info.
3635 frame_presentation_time_length_minus_1 + 1, &retval);
3636 if (retval != GST_AV1_PARSER_OK)
3637 goto error;
3638 }
3639
3640 if (frame_header->show_frame) {
3641 frame_header->showable_frame =
3642 (frame_header->frame_type != GST_AV1_KEY_FRAME);
3643 } else {
3644 frame_header->showable_frame = AV1_READ_BIT_CHECKED (br, &retval);
3645 if (retval != GST_AV1_PARSER_OK)
3646 goto error;
3647 }
3648
3649 if (frame_header->frame_type == GST_AV1_SWITCH_FRAME
3650 || (frame_header->frame_type == GST_AV1_KEY_FRAME
3651 && frame_header->show_frame))
3652 frame_header->error_resilient_mode = 1;
3653 else {
3654 frame_header->error_resilient_mode = AV1_READ_BIT_CHECKED (br, &retval);
3655 if (retval != GST_AV1_PARSER_OK)
3656 goto error;
3657 }
3658 }
3659
3660 if (frame_header->frame_type == GST_AV1_KEY_FRAME && frame_header->show_frame) {
3661 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3662 ref_info->entry[i].ref_valid = 0;
3663 ref_info->entry[i].ref_order_hint = 0;
3664 }
3665 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
3666 frame_header->order_hints[GST_AV1_REF_LAST_FRAME + i] = 0;
3667 }
3668 }
3669
3670 frame_header->disable_cdf_update = AV1_READ_BIT_CHECKED (br, &retval);
3671 if (retval != GST_AV1_PARSER_OK)
3672 goto error;
3673
3674 if (seq_header->seq_force_screen_content_tools ==
3675 GST_AV1_SELECT_SCREEN_CONTENT_TOOLS) {
3676 frame_header->allow_screen_content_tools =
3677 AV1_READ_BIT_CHECKED (br, &retval);
3678 if (retval != GST_AV1_PARSER_OK)
3679 goto error;
3680 } else {
3681 frame_header->allow_screen_content_tools =
3682 seq_header->seq_force_screen_content_tools;
3683 }
3684
3685 if (frame_header->allow_screen_content_tools) {
3686 if (seq_header->seq_force_integer_mv == GST_AV1_SELECT_INTEGER_MV) {
3687 frame_header->force_integer_mv = AV1_READ_BIT_CHECKED (br, &retval);
3688 if (retval != GST_AV1_PARSER_OK)
3689 goto error;
3690 } else {
3691 frame_header->force_integer_mv = seq_header->seq_force_integer_mv;
3692 }
3693 } else {
3694 frame_header->force_integer_mv = 0;
3695 }
3696
3697 if (frame_header->frame_is_intra) {
3698 frame_header->force_integer_mv = 1;
3699 }
3700
3701 if (seq_header->frame_id_numbers_present_flag) {
3702 gboolean have_prev_frame_id =
3703 !parser->state.begin_first_frame &&
3704 (!(frame_header->frame_type == GST_AV1_KEY_FRAME
3705 && frame_header->show_frame));
3706 if (have_prev_frame_id)
3707 parser->state.prev_frame_id = parser->state.current_frame_id;
3708
3709 g_assert (id_len > 0);
3710 frame_header->current_frame_id =
3711 AV1_READ_BITS_CHECKED (br, id_len, &retval);
3712 if (retval != GST_AV1_PARSER_OK)
3713 goto error;
3714
3715 parser->state.current_frame_id = frame_header->current_frame_id;
3716 /* Check whether the id and id diff is valid */
3717 if (have_prev_frame_id) {
3718 gint32 diff_frame_id;
3719 if (parser->state.current_frame_id > parser->state.prev_frame_id) {
3720 diff_frame_id =
3721 parser->state.current_frame_id - parser->state.prev_frame_id;
3722 } else {
3723 diff_frame_id = (1 << id_len) +
3724 parser->state.current_frame_id - parser->state.prev_frame_id;
3725 }
3726 if (parser->state.current_frame_id == parser->state.prev_frame_id ||
3727 diff_frame_id >= (1 << (id_len - 1))) {
3728 GST_INFO ("Invalid value of current_frame_id");
3729 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3730 goto error;
3731 }
3732 }
3733
3734 gst_av1_mark_ref_frames (parser, br, id_len);
3735 } else {
3736 frame_header->current_frame_id = 0;
3737 parser->state.prev_frame_id = parser->state.current_frame_id;
3738 parser->state.current_frame_id = frame_header->current_frame_id;
3739 }
3740
3741 if (frame_header->frame_type == GST_AV1_SWITCH_FRAME) {
3742 frame_header->frame_size_override_flag = 1;
3743 } else if (seq_header->reduced_still_picture_header) {
3744 frame_header->frame_size_override_flag = 0;
3745 } else {
3746 frame_header->frame_size_override_flag = AV1_READ_BIT_CHECKED (br, &retval);
3747 if (retval != GST_AV1_PARSER_OK)
3748 goto error;
3749 }
3750
3751 frame_header->order_hint =
3752 AV1_READ_BITS_CHECKED (br, seq_header->order_hint_bits_minus_1 + 1,
3753 &retval);
3754 if (retval != GST_AV1_PARSER_OK)
3755 goto error;
3756
3757 if (frame_header->frame_is_intra || frame_header->error_resilient_mode) {
3758 frame_header->primary_ref_frame = GST_AV1_PRIMARY_REF_NONE;
3759 } else {
3760 frame_header->primary_ref_frame = AV1_READ_BITS_CHECKED (br, 3, &retval);
3761 if (retval != GST_AV1_PARSER_OK)
3762 goto error;
3763 }
3764
3765 if (seq_header->decoder_model_info_present_flag) {
3766 frame_header->buffer_removal_time_present_flag =
3767 AV1_READ_BIT_CHECKED (br, &retval);
3768 if (retval != GST_AV1_PARSER_OK)
3769 goto error;
3770
3771 if (frame_header->buffer_removal_time_present_flag) {
3772 for (op_num = 0; op_num <= seq_header->operating_points_cnt_minus_1;
3773 op_num++) {
3774 if (seq_header->
3775 operating_points[op_num].decoder_model_present_for_this_op) {
3776 gint op_pt_idc = seq_header->operating_points[op_num].idc;
3777 gint in_temporal_layer =
3778 (op_pt_idc >> obu->header.obu_temporal_id) & 1;
3779 gint in_spatial_layer =
3780 (op_pt_idc >> (obu->header.obu_spatial_id + 8)) & 1;
3781 if (op_pt_idc == 0 || (in_temporal_layer && in_spatial_layer)) {
3782 frame_header->buffer_removal_time[op_num] =
3783 AV1_READ_BITS_CHECKED (br,
3784 seq_header->decoder_model_info.
3785 buffer_removal_time_length_minus_1 + 1, &retval);
3786 if (retval != GST_AV1_PARSER_OK)
3787 goto error;
3788 } else {
3789 frame_header->buffer_removal_time[op_num] = 0;
3790 }
3791 } else {
3792 frame_header->buffer_removal_time[op_num] = 0;
3793 }
3794 }
3795 }
3796 }
3797
3798 frame_header->allow_high_precision_mv = 0;
3799 frame_header->use_ref_frame_mvs = 0;
3800 frame_header->allow_intrabc = 0;
3801 if (frame_header->frame_type == GST_AV1_SWITCH_FRAME ||
3802 (frame_header->frame_type == GST_AV1_KEY_FRAME
3803 && frame_header->show_frame)) {
3804 frame_header->refresh_frame_flags = all_frames;
3805 } else {
3806 frame_header->refresh_frame_flags = AV1_READ_UINT8_CHECKED (br, &retval);
3807 if (retval != GST_AV1_PARSER_OK)
3808 goto error;
3809 }
3810 if (frame_header->frame_type == GST_AV1_INTRA_ONLY_FRAME) {
3811 if (frame_header->refresh_frame_flags == 0xFF) {
3812 GST_INFO ("Intra only frames cannot have refresh flags 0xFF");
3813 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3814 goto error;
3815 }
3816 }
3817
3818 if (!frame_header->frame_is_intra
3819 || frame_header->refresh_frame_flags != all_frames) {
3820 if (frame_header->error_resilient_mode && seq_header->enable_order_hint) {
3821 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
3822 frame_header->ref_order_hint[i] = AV1_READ_BITS_CHECKED (br,
3823 seq_header->order_hint_bits_minus_1 + 1, &retval);
3824 if (retval != GST_AV1_PARSER_OK)
3825 goto error;
3826
3827 if (frame_header->ref_order_hint[i] !=
3828 ref_info->entry[i].ref_order_hint)
3829 ref_info->entry[i].ref_valid = 0;
3830 }
3831 }
3832 }
3833
3834 if (frame_header->frame_is_intra) {
3835 retval = gst_av1_parse_frame_size (parser, br, frame_header);
3836 if (retval != GST_AV1_PARSER_OK)
3837 goto error;
3838 retval = gst_av1_parse_render_size (parser, br, frame_header);
3839 if (retval != GST_AV1_PARSER_OK)
3840 goto error;
3841 if (frame_header->allow_screen_content_tools
3842 && parser->state.upscaled_width == parser->state.frame_width) {
3843 frame_header->allow_intrabc = AV1_READ_BIT_CHECKED (br, &retval);
3844 if (retval != GST_AV1_PARSER_OK)
3845 goto error;
3846 }
3847
3848 frame_header->upscaled_width = parser->state.upscaled_width;
3849 frame_header->frame_width = parser->state.frame_width;
3850 frame_header->frame_height = parser->state.frame_height;
3851 frame_header->render_width = parser->state.render_width;
3852 frame_header->render_height = parser->state.render_height;
3853 } else {
3854 if (!seq_header->enable_order_hint) {
3855 frame_header->frame_refs_short_signaling = 0;
3856 } else {
3857 frame_header->frame_refs_short_signaling =
3858 AV1_READ_BIT_CHECKED (br, &retval);
3859 if (retval != GST_AV1_PARSER_OK)
3860 goto error;
3861
3862 if (frame_header->frame_refs_short_signaling) {
3863 if (AV1_REMAINING_BITS (br) < 3 + 3) {
3864 retval = GST_AV1_PARSER_NO_MORE_DATA;
3865 goto error;
3866 }
3867 frame_header->last_frame_idx = AV1_READ_BITS (br, 3);
3868 frame_header->gold_frame_idx = AV1_READ_BITS (br, 3);
3869 gst_av1_set_frame_refs (parser, seq_header, frame_header);
3870 }
3871 }
3872
3873 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
3874 if (!frame_header->frame_refs_short_signaling) {
3875 frame_header->ref_frame_idx[i] = AV1_READ_BITS_CHECKED (br, 3, &retval);
3876 if (retval != GST_AV1_PARSER_OK)
3877 goto error;
3878 }
3879
3880 if (seq_header->frame_id_numbers_present_flag) {
3881 gint32 delta_frame_id /* DeltaFrameId */ ;
3882 gint32 expected_frame_id;
3883 guint32 delta_frame_id_minus_1;
3884
3885 g_assert (id_len > 0);
3886
3887 delta_frame_id_minus_1 = AV1_READ_BITS_CHECKED (br,
3888 seq_header->delta_frame_id_length_minus_2 + 2, &retval);
3889 if (retval != GST_AV1_PARSER_OK)
3890 goto error;
3891
3892 delta_frame_id = delta_frame_id_minus_1 + 1;
3893 expected_frame_id = (frame_header->current_frame_id + (1 << id_len) -
3894 delta_frame_id) % (1 << id_len);
3895 if (expected_frame_id !=
3896 parser->state.ref_info.entry[frame_header->
3897 ref_frame_idx[i]].ref_frame_id) {
3898 GST_INFO ("Reference buffer frame ID mismatch, expectedFrameId"
3899 " is %d wihle ref frame id is %d", expected_frame_id,
3900 parser->state.ref_info.entry[frame_header->
3901 ref_frame_idx[i]].ref_frame_id);
3902 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3903 goto error;
3904 }
3905 }
3906 }
3907
3908 if (frame_header->frame_size_override_flag
3909 && !frame_header->error_resilient_mode) {
3910 retval = gst_av1_parse_frame_size_with_refs (parser, br, frame_header);
3911 if (retval != GST_AV1_PARSER_OK)
3912 goto error;
3913 } else {
3914 retval = gst_av1_parse_frame_size (parser, br, frame_header);
3915 if (retval != GST_AV1_PARSER_OK)
3916 goto error;
3917 retval = gst_av1_parse_render_size (parser, br, frame_header);
3918 if (retval != GST_AV1_PARSER_OK)
3919 goto error;
3920 }
3921 frame_header->upscaled_width = parser->state.upscaled_width;
3922 frame_header->frame_width = parser->state.frame_width;
3923 frame_header->frame_height = parser->state.frame_height;
3924 frame_header->render_width = parser->state.render_width;
3925 frame_header->render_height = parser->state.render_height;
3926
3927 if (frame_header->force_integer_mv) {
3928 frame_header->allow_high_precision_mv = 0;
3929 } else {
3930 frame_header->allow_high_precision_mv =
3931 AV1_READ_BIT_CHECKED (br, &retval);
3932 if (retval != GST_AV1_PARSER_OK)
3933 goto error;
3934 }
3935
3936 /* read_interpolation_filter() expand */
3937 frame_header->is_filter_switchable = AV1_READ_BIT_CHECKED (br, &retval);
3938 if (retval != GST_AV1_PARSER_OK)
3939 goto error;
3940
3941 if (frame_header->is_filter_switchable) {
3942 frame_header->interpolation_filter =
3943 GST_AV1_INTERPOLATION_FILTER_SWITCHABLE;
3944 } else {
3945 frame_header->interpolation_filter =
3946 AV1_READ_BITS_CHECKED (br, 2, &retval);
3947 if (retval != GST_AV1_PARSER_OK)
3948 goto error;
3949 }
3950
3951 frame_header->is_motion_mode_switchable =
3952 AV1_READ_BIT_CHECKED (br, &retval);
3953 if (retval != GST_AV1_PARSER_OK)
3954 goto error;
3955
3956 if (frame_header->error_resilient_mode || !seq_header->enable_ref_frame_mvs) {
3957 frame_header->use_ref_frame_mvs = 0;
3958 } else {
3959 frame_header->use_ref_frame_mvs = AV1_READ_BIT_CHECKED (br, &retval);
3960 if (retval != GST_AV1_PARSER_OK)
3961 goto error;
3962 }
3963 }
3964
3965 if (!frame_header->frame_is_intra) {
3966 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
3967 gint refFrame = GST_AV1_REF_LAST_FRAME + i;
3968 gint hint =
3969 ref_info->entry[frame_header->ref_frame_idx[i]].ref_order_hint;
3970 frame_header->order_hints[refFrame] = hint;
3971 if (!seq_header->enable_order_hint) {
3972 frame_header->ref_frame_sign_bias[refFrame] = 0;
3973 } else {
3974 frame_header->ref_frame_sign_bias[refFrame] =
3975 (gst_av1_get_relative_dist (parser->seq_header, hint,
3976 frame_header->order_hint) > 0);
3977 }
3978 }
3979 }
3980
3981 if (seq_header->reduced_still_picture_header
3982 || frame_header->disable_cdf_update)
3983 frame_header->disable_frame_end_update_cdf = 1;
3984 else {
3985 frame_header->disable_frame_end_update_cdf =
3986 AV1_READ_BIT_CHECKED (br, &retval);
3987 if (retval != GST_AV1_PARSER_OK)
3988 goto error;
3989 }
3990
3991 if (frame_header->primary_ref_frame != GST_AV1_PRIMARY_REF_NONE &&
3992 !ref_info->entry[frame_header->ref_frame_idx[frame_header->
3993 primary_ref_frame]].ref_valid) {
3994 GST_INFO ("Primary ref point to an invalid frame");
3995 retval = GST_AV1_PARSER_BITSTREAM_ERROR;
3996 goto error;
3997 }
3998
3999 if (frame_header->primary_ref_frame == GST_AV1_PRIMARY_REF_NONE) {
4000 /* do something in setup_past_independence() of parser level */
4001 gint8 *loop_filter_ref_deltas =
4002 frame_header->loop_filter_params.loop_filter_ref_deltas;
4003
4004 frame_header->loop_filter_params.loop_filter_delta_enabled = 1;
4005 loop_filter_ref_deltas[GST_AV1_REF_INTRA_FRAME] = 1;
4006 loop_filter_ref_deltas[GST_AV1_REF_LAST_FRAME] = 0;
4007 loop_filter_ref_deltas[GST_AV1_REF_LAST2_FRAME] = 0;
4008 loop_filter_ref_deltas[GST_AV1_REF_LAST3_FRAME] = 0;
4009 loop_filter_ref_deltas[GST_AV1_REF_BWDREF_FRAME] = 0;
4010 loop_filter_ref_deltas[GST_AV1_REF_GOLDEN_FRAME] = -1;
4011 loop_filter_ref_deltas[GST_AV1_REF_ALTREF_FRAME] = -1;
4012 loop_filter_ref_deltas[GST_AV1_REF_ALTREF2_FRAME] = -1;
4013 frame_header->loop_filter_params.loop_filter_mode_deltas[0] = 0;
4014 frame_header->loop_filter_params.loop_filter_mode_deltas[1] = 0;
4015 } else {
4016 /* do something in load_previous() of parser level */
4017 /* load_loop_filter_params() */
4018 GstAV1LoopFilterParams *ref_lf_params =
4019 &parser->state.ref_info.entry[frame_header->
4020 ref_frame_idx[frame_header->primary_ref_frame]].ref_lf_params;
4021 gint8 *loop_filter_ref_deltas =
4022 frame_header->loop_filter_params.loop_filter_ref_deltas;
4023
4024 /* Copy all from prime_ref */
4025 g_assert (parser->state.ref_info.
4026 entry[frame_header->ref_frame_idx[frame_header->primary_ref_frame]].
4027 ref_valid);
4028 loop_filter_ref_deltas[GST_AV1_REF_INTRA_FRAME] =
4029 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_INTRA_FRAME];
4030 loop_filter_ref_deltas[GST_AV1_REF_LAST_FRAME] =
4031 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_LAST_FRAME];
4032 loop_filter_ref_deltas[GST_AV1_REF_LAST2_FRAME] =
4033 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_LAST2_FRAME];
4034 loop_filter_ref_deltas[GST_AV1_REF_LAST3_FRAME] =
4035 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_LAST3_FRAME];
4036 loop_filter_ref_deltas[GST_AV1_REF_BWDREF_FRAME] =
4037 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_BWDREF_FRAME];
4038 loop_filter_ref_deltas[GST_AV1_REF_GOLDEN_FRAME] =
4039 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_GOLDEN_FRAME];
4040 loop_filter_ref_deltas[GST_AV1_REF_ALTREF2_FRAME] =
4041 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_ALTREF2_FRAME];
4042 loop_filter_ref_deltas[GST_AV1_REF_ALTREF_FRAME] =
4043 ref_lf_params->loop_filter_ref_deltas[GST_AV1_REF_ALTREF_FRAME];
4044 for (i = 0; i < 2; i++)
4045 frame_header->loop_filter_params.loop_filter_mode_deltas[i] =
4046 ref_lf_params->loop_filter_mode_deltas[i];
4047 }
4048
4049 /* @TODO:
4050 if ( primary_ref_frame == PRIMARY_REF_NONE ) {
4051 init_non_coeff_cdfs( )
4052 } else {
4053 load_cdfs( ref_frame_idx[primary_ref_frame] )
4054 }
4055 */
4056 /* @TODO:
4057 if ( use_ref_frame_mvs == 1 )
4058 motion_field_estimation( )
4059 */
4060
4061 retval = gst_av1_parse_tile_info (parser, br, frame_header);
4062 if (retval != GST_AV1_PARSER_OK)
4063 goto error;
4064
4065 retval = gst_av1_parse_quantization_params (parser, br, frame_header);
4066 if (retval != GST_AV1_PARSER_OK)
4067 goto error;
4068
4069 retval = gst_av1_parse_segmentation_params (parser, br, frame_header);
4070 if (retval != GST_AV1_PARSER_OK)
4071 goto error;
4072
4073 retval = gst_av1_parse_delta_q_params (parser, br,
4074 &(frame_header->quantization_params));
4075 if (retval != GST_AV1_PARSER_OK)
4076 goto error;
4077
4078 retval = gst_av1_parse_delta_lf_params (parser, br, frame_header);
4079 if (retval != GST_AV1_PARSER_OK)
4080 goto error;
4081
4082 /* @TODO:
4083 if ( primary_ref_frame == PRIMARY_REF_NONE ) {
4084 init_coeff_cdfs( )
4085 } else {
4086 load_previous_segment_ids( )
4087 }
4088 */
4089
4090 frame_header->coded_lossless = 1;
4091 for (segment_id = 0; segment_id < GST_AV1_MAX_SEGMENTS; segment_id++) {
4092 gint qindex = gst_av1_get_qindex (parser, frame_header, 1, segment_id);
4093 frame_header->lossless_array[segment_id] = (qindex == 0)
4094 && (frame_header->quantization_params.delta_q_y_dc == 0)
4095 && (frame_header->quantization_params.delta_q_u_ac == 0)
4096 && (frame_header->quantization_params.delta_q_u_dc == 0)
4097 && (frame_header->quantization_params.delta_q_v_ac == 0)
4098 && (frame_header->quantization_params.delta_q_v_dc == 0);
4099 if (!frame_header->lossless_array[segment_id])
4100 frame_header->coded_lossless = 0;
4101 if (frame_header->quantization_params.using_qmatrix) {
4102 if (frame_header->lossless_array[segment_id]) {
4103 frame_header->seg_qm_Level[0][segment_id] = 15;
4104 frame_header->seg_qm_Level[1][segment_id] = 15;
4105 frame_header->seg_qm_Level[2][segment_id] = 15;
4106 } else {
4107 frame_header->seg_qm_Level[0][segment_id] =
4108 frame_header->quantization_params.qm_y;
4109 frame_header->seg_qm_Level[1][segment_id] =
4110 frame_header->quantization_params.qm_u;
4111 frame_header->seg_qm_Level[2][segment_id] =
4112 frame_header->quantization_params.qm_v;
4113 }
4114 }
4115 }
4116 frame_header->all_lossless = frame_header->coded_lossless
4117 && (parser->state.frame_width == parser->state.upscaled_width);
4118
4119 retval = gst_av1_parse_loop_filter_params (parser, br, frame_header);
4120 if (retval != GST_AV1_PARSER_OK)
4121 goto error;
4122
4123 retval = gst_av1_parse_cdef_params (parser, br, frame_header);
4124 if (retval != GST_AV1_PARSER_OK)
4125 goto error;
4126
4127 retval = gst_av1_parse_loop_restoration_params (parser, br, frame_header);
4128 if (retval != GST_AV1_PARSER_OK)
4129 goto error;
4130
4131 retval = gst_av1_parse_tx_mode (parser, br, frame_header);
4132 if (retval != GST_AV1_PARSER_OK)
4133 goto error;
4134
4135 /* 5.9.23 inlined frame_reference_mode () */
4136 if (frame_header->frame_is_intra) {
4137 frame_header->reference_select = 0;
4138 } else {
4139 frame_header->reference_select = AV1_READ_BIT_CHECKED (br, &retval);
4140 if (retval != GST_AV1_PARSER_OK)
4141 goto error;
4142 }
4143
4144 retval = gst_av1_parse_skip_mode_params (parser, br, frame_header);
4145 if (retval != GST_AV1_PARSER_OK)
4146 goto error;
4147
4148 if (frame_header->frame_is_intra ||
4149 frame_header->error_resilient_mode || !seq_header->enable_warped_motion)
4150 frame_header->allow_warped_motion = 0;
4151 else {
4152 frame_header->allow_warped_motion = AV1_READ_BIT_CHECKED (br, &retval);
4153 if (retval != GST_AV1_PARSER_OK)
4154 goto error;
4155 }
4156
4157 frame_header->reduced_tx_set = AV1_READ_BIT_CHECKED (br, &retval);
4158 if (retval != GST_AV1_PARSER_OK)
4159 goto error;
4160
4161 retval = gst_av1_parse_global_motion_params (parser, br, frame_header);
4162 if (retval != GST_AV1_PARSER_OK)
4163 goto error;
4164
4165 retval = gst_av1_parse_film_grain_params (parser, br, frame_header);
4166 if (retval != GST_AV1_PARSER_OK)
4167 goto error;
4168
4169 success:
4170 return GST_AV1_PARSER_OK;
4171
4172 error:
4173 GST_WARNING ("parse uncompressed frame header error %d", retval);
4174 return retval;
4175 }
4176
4177 /* 7.21 */
4178 /**
4179 * gst_av1_parser_reference_frame_loading:
4180 * @parser: the #GstAV1Parser
4181 * @frame_header: a #GstAV1FrameHeaderOBU to load
4182 *
4183 * Load the context of @frame_header to parser's state. This function is
4184 * used when we want to show already parsed frames before.
4185 *
4186 * Returns: The #GstAV1ParserResult.
4187 *
4188 * Since: 1.18
4189 */
4190 GstAV1ParserResult
gst_av1_parser_reference_frame_loading(GstAV1Parser * parser,GstAV1FrameHeaderOBU * frame_header)4191 gst_av1_parser_reference_frame_loading (GstAV1Parser * parser,
4192 GstAV1FrameHeaderOBU * frame_header)
4193 {
4194 GstAV1ReferenceFrameInfo *ref_info;
4195 GstAV1TileInfo *ref_tile_info;
4196
4197 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4198 g_return_val_if_fail (frame_header != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4199
4200 if (!parser->seq_header) {
4201 GST_WARNING ("Missing OBU Reference: seq_header");
4202 return GST_AV1_PARSER_MISSING_OBU_REFERENCE;
4203 }
4204
4205 ref_info = &(parser->state.ref_info);
4206
4207 if (frame_header->frame_to_show_map_idx > GST_AV1_NUM_REF_FRAMES - 1)
4208 return GST_AV1_PARSER_BITSTREAM_ERROR;
4209
4210 g_assert (ref_info->entry[frame_header->frame_to_show_map_idx].ref_valid);
4211
4212 parser->state.current_frame_id =
4213 ref_info->entry[frame_header->frame_to_show_map_idx].ref_frame_id;
4214 parser->state.upscaled_width =
4215 ref_info->entry[frame_header->frame_to_show_map_idx].ref_upscaled_width;
4216 parser->state.frame_width =
4217 ref_info->entry[frame_header->frame_to_show_map_idx].ref_frame_width;
4218 parser->state.frame_height =
4219 ref_info->entry[frame_header->frame_to_show_map_idx].ref_frame_height;
4220 parser->state.render_width =
4221 ref_info->entry[frame_header->frame_to_show_map_idx].ref_render_width;
4222 parser->state.render_height =
4223 ref_info->entry[frame_header->frame_to_show_map_idx].ref_render_height;
4224 parser->state.mi_cols =
4225 ref_info->entry[frame_header->frame_to_show_map_idx].ref_mi_cols;
4226 parser->state.mi_rows =
4227 ref_info->entry[frame_header->frame_to_show_map_idx].ref_mi_rows;
4228
4229 ref_tile_info =
4230 &ref_info->entry[frame_header->frame_to_show_map_idx].ref_tile_info;
4231
4232 memcpy (parser->state.mi_col_starts, ref_tile_info->mi_col_starts,
4233 sizeof (guint32) * (GST_AV1_MAX_TILE_COLS + 1));
4234 memcpy (parser->state.mi_row_starts, ref_tile_info->mi_row_starts,
4235 sizeof (guint32) * (GST_AV1_MAX_TILE_ROWS + 1));
4236 parser->state.tile_cols_log2 = ref_tile_info->tile_cols_log2;
4237 parser->state.tile_cols = ref_tile_info->tile_cols;
4238 parser->state.tile_rows_log2 = ref_tile_info->tile_rows_log2;
4239 parser->state.tile_rows = ref_tile_info->tile_rows;
4240 parser->state.tile_size_bytes = ref_tile_info->tile_size_bytes;
4241
4242 return GST_AV1_PARSER_OK;
4243 }
4244
4245 /**
4246 * gst_av1_parser_reference_frame_update:
4247 * @parser: the #GstAV1Parser
4248 * @frame_header: a #GstAV1FrameHeaderOBU to load
4249 *
4250 * Update the context of @frame_header to parser's state. This function is
4251 * used when we finish one frame's decoding/showing, and need to update info
4252 * such as reference, global parameters.
4253 *
4254 * Returns: The #GstAV1ParserResult.
4255 *
4256 * Since: 1.18
4257 */
4258 GstAV1ParserResult
gst_av1_parser_reference_frame_update(GstAV1Parser * parser,GstAV1FrameHeaderOBU * frame_header)4259 gst_av1_parser_reference_frame_update (GstAV1Parser * parser,
4260 GstAV1FrameHeaderOBU * frame_header)
4261 {
4262 gint i;
4263 GstAV1SequenceHeaderOBU *seq_header;
4264 GstAV1ReferenceFrameInfo *ref_info;
4265
4266 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4267 g_return_val_if_fail (frame_header != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4268
4269 if (!parser->seq_header) {
4270 GST_WARNING ("Missing OBU Reference: seq_header");
4271 return GST_AV1_PARSER_MISSING_OBU_REFERENCE;
4272 }
4273
4274 seq_header = parser->seq_header;
4275 ref_info = &(parser->state.ref_info);
4276 if (frame_header->frame_type == GST_AV1_INTRA_ONLY_FRAME
4277 && frame_header->refresh_frame_flags == 0xff)
4278 return GST_AV1_PARSER_BITSTREAM_ERROR;
4279
4280 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
4281 if ((frame_header->refresh_frame_flags >> i) & 1) {
4282 ref_info->entry[i].ref_valid = 1;
4283 ref_info->entry[i].ref_frame_id = frame_header->current_frame_id;
4284 ref_info->entry[i].ref_frame_type = frame_header->frame_type;
4285 ref_info->entry[i].ref_upscaled_width = frame_header->upscaled_width;
4286 ref_info->entry[i].ref_frame_width = frame_header->frame_width;
4287 ref_info->entry[i].ref_frame_height = frame_header->frame_height;
4288 ref_info->entry[i].ref_render_width = frame_header->render_width;
4289 ref_info->entry[i].ref_render_height = frame_header->render_height;
4290 ref_info->entry[i].ref_order_hint = frame_header->order_hint;
4291 ref_info->entry[i].ref_mi_cols = parser->state.mi_cols;
4292 ref_info->entry[i].ref_mi_rows = parser->state.mi_rows;
4293 ref_info->entry[i].ref_subsampling_x =
4294 seq_header->color_config.subsampling_x;
4295 ref_info->entry[i].ref_subsampling_y =
4296 seq_header->color_config.subsampling_y;
4297 ref_info->entry[i].ref_bit_depth = seq_header->bit_depth;
4298 ref_info->entry[i].ref_segmentation_params =
4299 frame_header->segmentation_params;
4300 ref_info->entry[i].ref_global_motion_params =
4301 frame_header->global_motion_params;
4302 ref_info->entry[i].ref_lf_params = frame_header->loop_filter_params;
4303 ref_info->entry[i].ref_tile_info = frame_header->tile_info;
4304 if (seq_header->film_grain_params_present)
4305 ref_info->entry[i].ref_film_grain_params =
4306 frame_header->film_grain_params;
4307 }
4308 }
4309
4310 return GST_AV1_PARSER_OK;
4311 }
4312
4313 /* 5.12.1 */
4314 /**
4315 * gst_av1_parser_parse_tile_list_obu:
4316 * @parser: the #GstAV1Parser
4317 * @obu: a #GstAV1OBU to be parsed
4318 * @tile_list: a #GstAV1TileListOBU to store the parsed result.
4319 *
4320 * Parse one tile list @obu based on the @parser context, store the result
4321 * in the @tile_list. It is for large scale tile coding mode.
4322 *
4323 * Returns: The #GstAV1ParserResult.
4324 *
4325 * Since: 1.18
4326 */
4327 GstAV1ParserResult
gst_av1_parser_parse_tile_list_obu(GstAV1Parser * parser,GstAV1OBU * obu,GstAV1TileListOBU * tile_list)4328 gst_av1_parser_parse_tile_list_obu (GstAV1Parser * parser,
4329 GstAV1OBU * obu, GstAV1TileListOBU * tile_list)
4330 {
4331 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
4332 GstBitReader *br;
4333 GstBitReader bitreader;
4334 gint tile;
4335
4336 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4337 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4338 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_TILE_LIST,
4339 GST_AV1_PARSER_INVALID_OPERATION);
4340 g_return_val_if_fail (tile_list != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4341
4342 br = &bitreader;
4343 memset (tile_list, 0, sizeof (*tile_list));
4344 gst_bit_reader_init (br, obu->data, obu->obu_size);
4345 if (AV1_REMAINING_BITS (br) < 8 + 8 + 16) {
4346 retval = GST_AV1_PARSER_NO_MORE_DATA;
4347 goto error;
4348 }
4349
4350 tile_list->output_frame_width_in_tiles_minus_1 = AV1_READ_BITS (br, 8);
4351 tile_list->output_frame_height_in_tiles_minus_1 = AV1_READ_BITS (br, 8);
4352 tile_list->tile_count_minus_1 = AV1_READ_BITS (br, 16);
4353 for (tile = 0; tile <= tile_list->tile_count_minus_1; tile++) {
4354 if (AV1_REMAINING_BITS (br) < 8 + 8 + 8 + 16) {
4355 retval = GST_AV1_PARSER_NO_MORE_DATA;
4356 goto error;
4357 }
4358 tile_list->entry[tile].anchor_frame_idx = AV1_READ_BITS (br, 8);
4359 tile_list->entry[tile].anchor_tile_row = AV1_READ_BITS (br, 8);
4360 tile_list->entry[tile].anchor_tile_col = AV1_READ_BITS (br, 8);
4361 tile_list->entry[tile].tile_data_size_minus_1 = AV1_READ_BITS (br, 16);
4362
4363 g_assert (gst_bit_reader_get_pos (br) % 8 == 0);
4364
4365 tile_list->entry[tile].coded_tile_data =
4366 obu->data + gst_bit_reader_get_pos (br) / 8;
4367 /* skip the coded_tile_data */
4368 if (!gst_bit_reader_skip (br,
4369 tile_list->entry[tile].tile_data_size_minus_1 + 1)) {
4370 retval = GST_AV1_PARSER_NO_MORE_DATA;
4371 goto error;
4372 }
4373 }
4374
4375 retval = av1_skip_trailing_bits (parser, br, obu);
4376 if (retval != GST_AV1_PARSER_OK)
4377 goto error;
4378
4379 return GST_AV1_PARSER_OK;
4380
4381 error:
4382 GST_WARNING ("parse tile list error %d", retval);
4383 return retval;
4384 }
4385
4386 /* 5.11.1 */
4387 static GstAV1ParserResult
gst_av1_parse_tile_group(GstAV1Parser * parser,GstBitReader * br,GstAV1TileGroupOBU * tile_group)4388 gst_av1_parse_tile_group (GstAV1Parser * parser, GstBitReader * br,
4389 GstAV1TileGroupOBU * tile_group)
4390 {
4391 GstAV1ParserResult retval = GST_AV1_PARSER_OK;
4392 gint tile_num /* TileNum */ , end_bit_pos /* endBitPos */ ;
4393 gint header_bytes /* headerBytes */ , start_bitpos /* startBitPos */ ;
4394 guint32 sz = AV1_REMAINING_BYTES (br);
4395 guint32 tile_row /* tileRow */ ;
4396 guint32 tile_col /* tileCol */ ;
4397 guint32 tile_size /* tileSize */ ;
4398
4399 memset (tile_group, 0, sizeof (*tile_group));
4400 tile_group->num_tiles = parser->state.tile_cols * parser->state.tile_rows;
4401 start_bitpos = gst_bit_reader_get_pos (br);
4402 tile_group->tile_start_and_end_present_flag = 0;
4403
4404 if (tile_group->num_tiles > 1) {
4405 tile_group->tile_start_and_end_present_flag =
4406 AV1_READ_BIT_CHECKED (br, &retval);
4407 if (retval != GST_AV1_PARSER_OK)
4408 goto error;
4409 }
4410 if (tile_group->num_tiles == 1
4411 || !tile_group->tile_start_and_end_present_flag) {
4412 tile_group->tg_start = 0;
4413 tile_group->tg_end = tile_group->num_tiles - 1;
4414 } else {
4415 gint tileBits = parser->state.tile_cols_log2 + parser->state.tile_rows_log2;
4416 tile_group->tg_start = AV1_READ_BITS_CHECKED (br, tileBits, &retval);
4417 if (retval != GST_AV1_PARSER_OK)
4418 goto error;
4419
4420 tile_group->tg_end = AV1_READ_BITS_CHECKED (br, tileBits, &retval);
4421 if (retval != GST_AV1_PARSER_OK)
4422 goto error;
4423 }
4424
4425 if (tile_group->tg_end < tile_group->tg_start) {
4426 retval = GST_AV1_PARSER_NO_MORE_DATA;
4427 goto error;
4428 }
4429
4430 if (!gst_bit_reader_skip_to_byte (br)) {
4431 retval = GST_AV1_PARSER_NO_MORE_DATA;
4432 goto error;
4433 }
4434
4435 end_bit_pos = gst_bit_reader_get_pos (br);
4436 header_bytes = (end_bit_pos - start_bitpos) / 8;
4437 sz -= header_bytes;
4438
4439 for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end;
4440 tile_num++) {
4441 tile_row = tile_num / parser->state.tile_cols;
4442 tile_col = tile_num % parser->state.tile_cols;
4443 /* if last tile */
4444 if (tile_num == tile_group->tg_end) {
4445 tile_size = sz;
4446 } else {
4447 gint tile_size_minus_1 = av1_bitstreamfn_le (br,
4448 parser->state.tile_size_bytes, &retval);
4449 if (retval != GST_AV1_PARSER_OK)
4450 goto error;
4451 tile_size = tile_size_minus_1 + 1;
4452 sz -= (tile_size + parser->state.tile_size_bytes);
4453 }
4454
4455 tile_group->entry[tile_num].tile_size = tile_size;
4456 tile_group->entry[tile_num].tile_offset = gst_bit_reader_get_pos (br) / 8;
4457 tile_group->entry[tile_num].tile_row = tile_row;
4458 tile_group->entry[tile_num].tile_col = tile_col;
4459
4460 tile_group->entry[tile_num].mi_row_start =
4461 parser->state.mi_row_starts[tile_row];
4462 tile_group->entry[tile_num].mi_row_end =
4463 parser->state.mi_row_starts[tile_row + 1];
4464 tile_group->entry[tile_num].mi_col_start =
4465 parser->state.mi_col_starts[tile_col];
4466 tile_group->entry[tile_num].mi_col_end =
4467 parser->state.mi_col_starts[tile_col + 1];
4468 /* Not implement here, the real decoder process
4469 init_symbol( tileSize )
4470 decode_tile( )
4471 exit_symbol( )
4472 */
4473
4474 /* Skip the real data to the next one */
4475 if (tile_num < tile_group->tg_end &&
4476 !gst_bit_reader_skip (br, tile_size * 8)) {
4477 retval = GST_AV1_PARSER_NO_MORE_DATA;
4478 goto error;
4479 }
4480 }
4481
4482 if (tile_group->tg_end == tile_group->num_tiles - 1) {
4483 /* Not implement here, the real decoder process
4484 if ( !disable_frame_end_update_cdf ) {
4485 frame_end_update_cdf( )
4486 }
4487 decode_frame_wrapup( )
4488 */
4489 parser->state.seen_frame_header = 0;
4490 }
4491
4492 return GST_AV1_PARSER_OK;
4493
4494 error:
4495 GST_WARNING ("parse tile group error %d", retval);
4496 return retval;
4497 }
4498
4499 /**
4500 * gst_av1_parser_parse_tile_group_obu:
4501 * @parser: the #GstAV1Parser
4502 * @obu: a #GstAV1OBU to be parsed
4503 * @tile_group: a #GstAV1TileGroupOBU to store the parsed result.
4504 *
4505 * Parse one tile group @obu based on the @parser context, store the result
4506 * in the @tile_group.
4507 *
4508 * Returns: The #GstAV1ParserResult.
4509 *
4510 * Since: 1.18
4511 */
4512 GstAV1ParserResult
gst_av1_parser_parse_tile_group_obu(GstAV1Parser * parser,GstAV1OBU * obu,GstAV1TileGroupOBU * tile_group)4513 gst_av1_parser_parse_tile_group_obu (GstAV1Parser * parser, GstAV1OBU * obu,
4514 GstAV1TileGroupOBU * tile_group)
4515 {
4516 GstAV1ParserResult ret;
4517 GstBitReader bit_reader;
4518
4519 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4520 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4521 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_TILE_GROUP,
4522 GST_AV1_PARSER_INVALID_OPERATION);
4523 g_return_val_if_fail (tile_group != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4524
4525 if (!parser->state.seen_frame_header) {
4526 GST_WARNING ("Missing OBU Reference: frame_header");
4527 return GST_AV1_PARSER_MISSING_OBU_REFERENCE;
4528 }
4529
4530 gst_bit_reader_init (&bit_reader, obu->data, obu->obu_size);
4531 ret = gst_av1_parse_tile_group (parser, &bit_reader, tile_group);
4532 return ret;
4533 }
4534
4535 static GstAV1ParserResult
gst_av1_parse_frame_header(GstAV1Parser * parser,GstAV1OBU * obu,GstBitReader * bit_reader,GstAV1FrameHeaderOBU * frame_header)4536 gst_av1_parse_frame_header (GstAV1Parser * parser, GstAV1OBU * obu,
4537 GstBitReader * bit_reader, GstAV1FrameHeaderOBU * frame_header)
4538 {
4539 GstAV1ParserResult ret;
4540 guint i;
4541
4542 memset (frame_header, 0, sizeof (*frame_header));
4543 frame_header->frame_is_intra = 1;
4544 frame_header->last_frame_idx = -1;
4545 frame_header->gold_frame_idx = -1;
4546 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++)
4547 frame_header->ref_frame_idx[i] = -1;
4548
4549 ret = gst_av1_parse_uncompressed_frame_header (parser, obu, bit_reader,
4550 frame_header);
4551 if (ret != GST_AV1_PARSER_OK)
4552 return ret;
4553
4554 if (frame_header->show_existing_frame) {
4555 parser->state.seen_frame_header = 0;
4556 } else {
4557 parser->state.seen_frame_header = 1;
4558 }
4559
4560 return GST_AV1_PARSER_OK;
4561 }
4562
4563 /**
4564 * gst_av1_parser_parse_frame_header_obu:
4565 * @parser: the #GstAV1Parser
4566 * @obu: a #GstAV1OBU to be parsed
4567 * @frame_header: a #GstAV1FrameHeaderOBU to store the parsed result.
4568 *
4569 * Parse one frame header @obu based on the @parser context, store the result
4570 * in the @frame.
4571 *
4572 * Returns: The #GstAV1ParserResult.
4573 *
4574 * Since: 1.18
4575 */
4576 GstAV1ParserResult
gst_av1_parser_parse_frame_header_obu(GstAV1Parser * parser,GstAV1OBU * obu,GstAV1FrameHeaderOBU * frame_header)4577 gst_av1_parser_parse_frame_header_obu (GstAV1Parser * parser,
4578 GstAV1OBU * obu, GstAV1FrameHeaderOBU * frame_header)
4579 {
4580 GstAV1ParserResult ret;
4581 GstBitReader bit_reader;
4582
4583 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4584 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4585 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_FRAME_HEADER ||
4586 obu->obu_type == GST_AV1_OBU_REDUNDANT_FRAME_HEADER,
4587 GST_AV1_PARSER_INVALID_OPERATION);
4588 g_return_val_if_fail (frame_header != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4589
4590 if (obu->obu_type == GST_AV1_OBU_REDUNDANT_FRAME_HEADER
4591 && !parser->state.seen_frame_header) {
4592 GST_WARNING ("no seen of frame header while get redundant frame header");
4593 return GST_AV1_PARSER_BITSTREAM_ERROR;
4594 }
4595
4596 if (obu->obu_type == GST_AV1_OBU_FRAME_HEADER
4597 && parser->state.seen_frame_header) {
4598 GST_WARNING ("already seen a frame header");
4599 return GST_AV1_PARSER_BITSTREAM_ERROR;
4600 }
4601
4602 gst_bit_reader_init (&bit_reader, obu->data, obu->obu_size);
4603 ret = gst_av1_parse_frame_header (parser, obu, &bit_reader, frame_header);
4604 if (ret != GST_AV1_PARSER_OK)
4605 return ret;
4606
4607 ret = av1_skip_trailing_bits (parser, &bit_reader, obu);
4608 return ret;
4609 }
4610
4611 /**
4612 * gst_av1_parser_parse_frame_obu:
4613 * @parser: the #GstAV1Parser
4614 * @obu: a #GstAV1OBU to be parsed
4615 * @frame: a #GstAV1FrameOBU to store the parsed result.
4616 *
4617 * Parse one frame @obu based on the @parser context, store the result
4618 * in the @frame.
4619 *
4620 * Returns: The #GstAV1ParserResult.
4621 *
4622 * Since: 1.18
4623 */
4624 GstAV1ParserResult
gst_av1_parser_parse_frame_obu(GstAV1Parser * parser,GstAV1OBU * obu,GstAV1FrameOBU * frame)4625 gst_av1_parser_parse_frame_obu (GstAV1Parser * parser, GstAV1OBU * obu,
4626 GstAV1FrameOBU * frame)
4627 {
4628 GstAV1ParserResult retval;
4629 GstBitReader bit_reader;
4630
4631 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4632 g_return_val_if_fail (obu != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4633 g_return_val_if_fail (obu->obu_type == GST_AV1_OBU_FRAME,
4634 GST_AV1_PARSER_INVALID_OPERATION);
4635 g_return_val_if_fail (frame != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4636
4637 if (parser->state.seen_frame_header) {
4638 GST_WARNING ("already seen a frame header");
4639 return GST_AV1_PARSER_BITSTREAM_ERROR;
4640 }
4641
4642 gst_bit_reader_init (&bit_reader, obu->data, obu->obu_size);
4643 retval = gst_av1_parse_frame_header (parser, obu, &bit_reader,
4644 &(frame->frame_header));
4645 if (retval != GST_AV1_PARSER_OK)
4646 return retval;
4647
4648 if (!gst_bit_reader_skip_to_byte (&bit_reader))
4649 return GST_AV1_PARSER_NO_MORE_DATA;
4650
4651 retval = gst_av1_parse_tile_group (parser, &bit_reader, &(frame->tile_group));
4652 return retval;
4653 }
4654
4655 /**
4656 * gst_av1_parser_set_operating_point:
4657 * @parser: the #GstAV1Parser
4658 * @operating_point: the operating point to set
4659 *
4660 * Set the operating point to filter OBUs.
4661 *
4662 * Returns: The #GstAV1ParserResult.
4663 *
4664 * Since: 1.20
4665 */
4666 GstAV1ParserResult
gst_av1_parser_set_operating_point(GstAV1Parser * parser,gint32 operating_point)4667 gst_av1_parser_set_operating_point (GstAV1Parser * parser,
4668 gint32 operating_point)
4669 {
4670 g_return_val_if_fail (parser != NULL, GST_AV1_PARSER_INVALID_OPERATION);
4671 g_return_val_if_fail (operating_point >= 0, GST_AV1_PARSER_INVALID_OPERATION);
4672
4673 if (parser->seq_header &&
4674 operating_point > parser->seq_header->operating_points_cnt_minus_1)
4675 return GST_AV1_PARSER_INVALID_OPERATION;
4676
4677 /* Decide whether it is valid when sequence comes. */
4678 parser->state.operating_point = operating_point;
4679 return GST_AV1_PARSER_OK;
4680 }
4681
4682 /**
4683 * gst_av1_parser_new:
4684 *
4685 * Allocates a new #GstAV1Parser,
4686 *
4687 * Returns: (transfer full): a newly-allocated #GstAV1Parser
4688 *
4689 * Since: 1.18
4690 */
4691 GstAV1Parser *
gst_av1_parser_new(void)4692 gst_av1_parser_new (void)
4693 {
4694 return g_slice_new0 (GstAV1Parser);
4695 }
4696
4697 /**
4698 * gst_av1_parser_free:
4699 * @parser: the #GstAV1Parser to free
4700 *
4701 * If parser is not %NULL, frees its allocated memory.
4702 *
4703 * It cannot be used afterwards.
4704 *
4705 * Since: 1.18
4706 */
4707 void
gst_av1_parser_free(GstAV1Parser * parser)4708 gst_av1_parser_free (GstAV1Parser * parser)
4709 {
4710 g_return_if_fail (parser != NULL);
4711
4712 if (parser->seq_header)
4713 g_slice_free (GstAV1SequenceHeaderOBU, parser->seq_header);
4714 g_slice_free (GstAV1Parser, parser);
4715 }
4716