/* * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #include guint ceil_log2 (guint32 v); typedef struct { const guint8 *data; guint size; guint n_epb; /* Number of emulation prevention bytes */ guint byte; /* Byte position */ guint bits_in_cache; /* bitpos in the cache of next bit */ guint8 first_byte; guint64 cache; /* cached bytes */ } NalReader; G_GNUC_INTERNAL void init_nal (NalReader * nr, const guint8 * data, guint size); G_GNUC_INTERNAL gboolean _read (NalReader * nr, guint nbits); G_GNUC_INTERNAL gboolean _skip (NalReader * nr, guint nbits); G_GNUC_INTERNAL gboolean _skip_long (NalReader * nr, guint nbits); G_GNUC_INTERNAL guint _get_pos (const NalReader * nr); G_GNUC_INTERNAL guint _get_remaining (const NalReader * nr); G_GNUC_INTERNAL guint _get_epb_count (const NalReader * nr); G_GNUC_INTERNAL gboolean _is_byte_aligned (NalReader * nr); G_GNUC_INTERNAL gboolean _has_more_data (NalReader * nr); #define _READ_BITS_H(bits) \ G_GNUC_INTERNAL \ gboolean _get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits) _READ_BITS_H (8); _READ_BITS_H (16); _READ_BITS_H (32); #define _PEEK_BITS_H(bits) \ G_GNUC_INTERNAL \ gboolean _peek_bits_uint##bits (const NalReader *nr, guint##bits *val, guint nbits) _PEEK_BITS_H (8); G_GNUC_INTERNAL gboolean _get_ue (NalReader * nr, guint32 * val); G_GNUC_INTERNAL gboolean _get_se (NalReader * nr, gint32 * val); #define CHECK_ALLOWED_MAX(val, max) { \ if (val > max) { \ GST_WARNING ("value greater than max. value: %d, max %d", \ val, max); \ goto error; \ } \ } #define CHECK_ALLOWED(val, min, max) { \ if (val < min || val > max) { \ GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \ val, min, max); \ goto error; \ } \ } #define READ_UINT8(nr, val, nbits) { \ if (!_get_bits_uint8 (nr, &val, nbits)) { \ GST_WARNING ("failed to read uint8, nbits: %d", nbits); \ goto error; \ } \ } #define READ_UINT16(nr, val, nbits) { \ if (!_get_bits_uint16 (nr, &val, nbits)) { \ GST_WARNING ("failed to read uint16, nbits: %d", nbits); \ goto error; \ } \ } #define READ_UINT32(nr, val, nbits) { \ if (!_get_bits_uint32 (nr, &val, nbits)) { \ GST_WARNING ("failed to read uint32, nbits: %d", nbits); \ goto error; \ } \ } #define READ_UINT64(nr, val, nbits) { \ if (!_get_bits_uint64 (nr, &val, nbits)) { \ GST_WARNING ("failed to read uint32, nbits: %d", nbits); \ goto error; \ } \ } #define READ_UE(nr, val) { \ if (!_get_ue (nr, &val)) { \ GST_WARNING ("failed to read UE"); \ goto error; \ } \ } #define READ_UE_ALLOWED(nr, val, min, max) { \ guint32 tmp; \ READ_UE (nr, tmp); \ CHECK_ALLOWED (tmp, min, max); \ val = tmp; \ } #define READ_UE_MAX(nr, val, max) { \ guint32 tmp; \ READ_UE (nr, tmp); \ CHECK_ALLOWED_MAX (tmp, max); \ val = tmp; \ } #define READ_SE(nr, val) { \ if (!_get_se (nr, &val)) { \ GST_WARNING ("failed to read SE"); \ goto error; \ } \ } #define READ_SE_ALLOWED(nr, val, min, max) { \ gint32 tmp; \ READ_SE (nr, tmp); \ CHECK_ALLOWED (tmp, min, max); \ val = tmp; \ } G_GNUC_INTERNAL gint scan_for_start_codes (const guint8 * data, guint size);