blob: 35b7873b9cc425052498844fae07a0e41dd3ffcb [file] [log] [blame]
Alexandra Hájková8fe551e2016-04-22 07:43:521/*
2 * Copyright (c) 2016 Alexandra Hájková
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * bitstream reader API header.
24 */
25
Anton Khirnov91779702021-01-28 12:23:2326/*
27 * Bit order (endianness) is controlled by #defining BITSTREAM_BE and/or
28 * BITSTREAM_LE before #including this header. The corresponding bitreading
29 * functions are provided as bits_*_be()/bits_*_le() respectively.
30 *
31 * If neither or only BITSTREAM_BE is defined, then the default (unsuffixed)
32 * bits_*() will resolve to the big-endian implementation. If only BITSTREAM_LE
33 * is defined, little-endian will be the default.
34 *
35 * If both are defined, then the default can be controlled by defining at most
36 * one of BITSTREAM_DEFAULT_LE/BE. When BITSTREAM_DEFAULT_* is not defined, no
37 * default is provided and you must always explicitly use the _be() or _le()
38 * variants.
39 */
40
Alexandra Hájková8fe551e2016-04-22 07:43:5241#ifndef AVCODEC_BITSTREAM_H
42#define AVCODEC_BITSTREAM_H
43
44#include <stdint.h>
45
46#include "config.h"
47
48#include "libavutil/avassert.h"
49#include "libavutil/common.h"
50#include "libavutil/intreadwrite.h"
51#include "libavutil/log.h"
52
53#include "mathops.h"
54#include "vlc.h"
55
56#ifndef UNCHECKED_BITSTREAM_READER
57#define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
58#endif
59
Anton Khirnov91779702021-01-28 12:23:2360// select the default endianness, if any
61#if defined(BITSTREAM_LE) && defined(BITSTREAM_BE)
Alexandra Hájková8fe551e2016-04-22 07:43:5262
Anton Khirnov91779702021-01-28 12:23:2363# if defined(BITSTREAM_DEFAULT_BE) && defined(BITSTREAM_DEFAULT_LE)
64# error "At most one of BITSTREAM_DEFAULT_BE/LE must be defined"
65# elif defined(BITSTREAM_DEFAULT_BE)
66# define BITS_DEFAULT_BE
67# elif defined(BITSTREAM_DEFAULT_LE)
68# define BITS_DEFAULT_LE
69# endif
70
71#elif defined(BITSTREAM_LE)
72# define BITS_DEFAULT_LE
73#else // select BE if nothing is requested explicitly
74# define BITS_DEFAULT_BE
75# define BITSTREAM_WANT_BE
Alexandra Hájková8fe551e2016-04-22 07:43:5276#endif
77
Anton Khirnov91779702021-01-28 12:23:2378#if defined(BITS_DEFAULT_LE)
Alexandra Hájková8fe551e2016-04-22 07:43:5279
Anton Khirnov91779702021-01-28 12:23:2380# define BitstreamContext BitstreamContextLE
81# define bits_init bits_init_le
82# define bits_init8 bits_init8_le
83# define bits_tell bits_tell_le
84# define bits_size bits_size_le
85# define bits_left bits_left_le
86# define bits_read_bit bits_read_bit_le
87# define bits_read_nz bits_read_nz_le
88# define bits_read bits_read_le
89# define bits_read_63 bits_read_63_le
90# define bits_read_64 bits_read_64_le
91# define bits_read_signed bits_read_signed_le
Anton Khirnov0b333102023-01-15 09:21:3692# define bits_read_signed_nz bits_read_signed_nz_le
Anton Khirnov91779702021-01-28 12:23:2393# define bits_peek_nz bits_peek_nz_le
94# define bits_peek bits_peek_le
95# define bits_peek_signed bits_peek_signed_le
Anton Khirnov0b333102023-01-15 09:21:3696# define bits_peek_signed_nz bits_peek_signed_nz_le
Anton Khirnov91779702021-01-28 12:23:2397# define bits_skip bits_skip_le
98# define bits_seek bits_seek_le
99# define bits_align bits_align_le
100# define bits_read_xbits bits_read_xbits_le
101# define bits_decode012 bits_decode012_le
102# define bits_decode210 bits_decode210_le
103# define bits_apply_sign bits_apply_sign_le
104# define bits_read_vlc bits_read_vlc_le
Paul B Mahol29b9fca2023-08-27 19:18:46105# define bits_read_vlc_multi bits_read_vlc_multi_le
Alexandra Hájková8fe551e2016-04-22 07:43:52106
Anton Khirnov91779702021-01-28 12:23:23107#elif defined(BITS_DEFAULT_BE)
108
109# define BitstreamContext BitstreamContextBE
110# define bits_init bits_init_be
111# define bits_init8 bits_init8_be
112# define bits_tell bits_tell_be
113# define bits_size bits_size_be
114# define bits_left bits_left_be
115# define bits_read_bit bits_read_bit_be
116# define bits_read_nz bits_read_nz_be
117# define bits_read bits_read_be
118# define bits_read_63 bits_read_63_be
119# define bits_read_64 bits_read_64_be
120# define bits_read_signed bits_read_signed_be
Anton Khirnov0b333102023-01-15 09:21:36121# define bits_read_signed_nz bits_read_signed_nz_be
Anton Khirnov91779702021-01-28 12:23:23122# define bits_peek_nz bits_peek_nz_be
123# define bits_peek bits_peek_be
124# define bits_peek_signed bits_peek_signed_be
Anton Khirnov0b333102023-01-15 09:21:36125# define bits_peek_signed_nz bits_peek_signed_nz_be
Anton Khirnov91779702021-01-28 12:23:23126# define bits_skip bits_skip_be
127# define bits_seek bits_seek_be
128# define bits_align bits_align_be
129# define bits_read_xbits bits_read_xbits_be
130# define bits_decode012 bits_decode012_be
131# define bits_decode210 bits_decode210_be
132# define bits_apply_sign bits_apply_sign_be
133# define bits_read_vlc bits_read_vlc_be
Paul B Mahol29b9fca2023-08-27 19:18:46134# define bits_read_vlc_multi bits_read_vlc_multi_be
Anton Khirnov91779702021-01-28 12:23:23135
Alexandra Hájková8fe551e2016-04-22 07:43:52136#endif
137
Anton Khirnov91779702021-01-28 12:23:23138#undef BITS_DEFAULT_LE
139#undef BITS_DEFAULT_BE
Alexandra Hájková8fe551e2016-04-22 07:43:52140
141#define BITS_RL_VLC(level, run, bc, table, bits, max_depth) \
142 do { \
143 int n, nb_bits; \
144 unsigned int index = bits_peek(bc, bits); \
145 level = table[index].level; \
146 n = table[index].len; \
147 \
148 if (max_depth > 1 && n < 0) { \
149 bits_skip(bc, bits); \
150 \
151 nb_bits = -n; \
152 \
153 index = bits_peek(bc, nb_bits) + level; \
154 level = table[index].level; \
155 n = table[index].len; \
156 if (max_depth > 2 && n < 0) { \
157 bits_skip(bc, nb_bits); \
158 nb_bits = -n; \
159 \
160 index = bits_peek(bc, nb_bits) + level; \
161 level = table[index].level; \
162 n = table[index].len; \
163 } \
164 } \
165 run = table[index].run; \
166 bits_skip(bc, n); \
167 } while (0)
168
169#endif /* AVCODEC_BITSTREAM_H */
Anton Khirnov91779702021-01-28 12:23:23170
171// the following is deliberately outside of the standard #include guards
172
173#if defined(BITSTREAM_LE) && !defined(BITSTREAM_WANT_LE)
174# define BITSTREAM_WANT_LE
175#endif
176
177#if defined(BITSTREAM_BE) && !defined(BITSTREAM_WANT_BE)
178# define BITSTREAM_WANT_BE
179#endif
180
181#if defined(BITSTREAM_WANT_LE) && !defined(AVCODEC_BITSTREAM_LE)
182#define AVCODEC_BITSTREAM_LE
183
184#define BITSTREAM_TEMPLATE_LE
185#include "bitstream_template.h"
186#undef BITSTREAM_TEMPLATE_LE
187
188#endif
189
190#if defined(BITSTREAM_WANT_BE) && !defined(AVCODEC_BITSTREAM_BE)
191#define AVCODEC_BITSTREAM_BE
192
193#include "bitstream_template.h"
194
195#endif
196
197#undef BITSTREAM_WANT_LE
198#undef BITSTREAM_WANT_BE