FFmpeg
put_bits.h
Go to the documentation of this file.
1 /*
2  * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
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 writer API
24  */
25 
26 #ifndef AVCODEC_PUT_BITS_H
27 #define AVCODEC_PUT_BITS_H
28 
29 #include <stdint.h>
30 #include <stddef.h>
31 
32 #include "config.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/avassert.h"
35 
36 #if ARCH_X86_64
37 // TODO: Benchmark and optionally enable on other 64-bit architectures.
38 typedef uint64_t BitBuf;
39 #define AV_WBBUF AV_WB64
40 #define AV_WLBUF AV_WL64
41 #else
42 typedef uint32_t BitBuf;
43 #define AV_WBBUF AV_WB32
44 #define AV_WLBUF AV_WL32
45 #endif
46 
47 static const int BUF_BITS = 8 * sizeof(BitBuf);
48 
49 typedef struct PutBitContext {
51  int bit_left;
55 
56 /**
57  * Initialize the PutBitContext s.
58  *
59  * @param buffer the buffer where to put bits
60  * @param buffer_size the size in bytes of buffer
61  */
62 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
63  int buffer_size)
64 {
65  if (buffer_size < 0) {
66  buffer_size = 0;
67  buffer = NULL;
68  }
69 
70  s->size_in_bits = 8 * buffer_size;
71  s->buf = buffer;
72  s->buf_end = s->buf + buffer_size;
73  s->buf_ptr = s->buf;
74  s->bit_left = BUF_BITS;
75  s->bit_buf = 0;
76 }
77 
78 /**
79  * @return the total number of bits written to the bitstream.
80  */
81 static inline int put_bits_count(PutBitContext *s)
82 {
83  return (s->buf_ptr - s->buf) * 8 + BUF_BITS - s->bit_left;
84 }
85 
86 /**
87  * Rebase the bit writer onto a reallocated buffer.
88  *
89  * @param buffer the buffer where to put bits
90  * @param buffer_size the size in bytes of buffer,
91  * must be large enough to hold everything written so far
92  */
94  int buffer_size)
95 {
96  av_assert0(8*buffer_size >= put_bits_count(s));
97 
98  s->buf_end = buffer + buffer_size;
99  s->buf_ptr = buffer + (s->buf_ptr - s->buf);
100  s->buf = buffer;
101  s->size_in_bits = 8 * buffer_size;
102 }
103 
104 /**
105  * @return the number of bits available in the bitstream.
106  */
107 static inline int put_bits_left(PutBitContext* s)
108 {
109  return (s->buf_end - s->buf_ptr) * 8 - BUF_BITS + s->bit_left;
110 }
111 
112 /**
113  * Pad the end of the output stream with zeros.
114  */
115 static inline void flush_put_bits(PutBitContext *s)
116 {
117 #ifndef BITSTREAM_WRITER_LE
118  if (s->bit_left < BUF_BITS)
119  s->bit_buf <<= s->bit_left;
120 #endif
121  while (s->bit_left < BUF_BITS) {
122  av_assert0(s->buf_ptr < s->buf_end);
123 #ifdef BITSTREAM_WRITER_LE
124  *s->buf_ptr++ = s->bit_buf;
125  s->bit_buf >>= 8;
126 #else
127  *s->buf_ptr++ = s->bit_buf >> (BUF_BITS - 8);
128  s->bit_buf <<= 8;
129 #endif
130  s->bit_left += 8;
131  }
132  s->bit_left = BUF_BITS;
133  s->bit_buf = 0;
134 }
135 
136 static inline void flush_put_bits_le(PutBitContext *s)
137 {
138  while (s->bit_left < BUF_BITS) {
139  av_assert0(s->buf_ptr < s->buf_end);
140  *s->buf_ptr++ = s->bit_buf;
141  s->bit_buf >>= 8;
142  s->bit_left += 8;
143  }
144  s->bit_left = BUF_BITS;
145  s->bit_buf = 0;
146 }
147 
148 #ifdef BITSTREAM_WRITER_LE
149 #define avpriv_align_put_bits align_put_bits_unsupported_here
150 #define avpriv_put_string ff_put_string_unsupported_here
151 #define avpriv_copy_bits avpriv_copy_bits_unsupported_here
152 #else
153 /**
154  * Pad the bitstream with zeros up to the next byte boundary.
155  */
157 
158 /**
159  * Put the string string in the bitstream.
160  *
161  * @param terminate_string 0-terminates the written string if value is 1
162  */
163 void avpriv_put_string(PutBitContext *pb, const char *string,
164  int terminate_string);
165 
166 /**
167  * Copy the content of src to the bitstream.
168  *
169  * @param length the number of bits of src to copy
170  */
171 void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
172 #endif
173 
174 static inline void put_bits_no_assert(PutBitContext *s, int n, BitBuf value)
175 {
176  BitBuf bit_buf;
177  int bit_left;
178 
179  bit_buf = s->bit_buf;
180  bit_left = s->bit_left;
181 
182  /* XXX: optimize */
183 #ifdef BITSTREAM_WRITER_LE
184  bit_buf |= value << (BUF_BITS - bit_left);
185  if (n >= bit_left) {
186  if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
187  AV_WLBUF(s->buf_ptr, bit_buf);
188  s->buf_ptr += sizeof(BitBuf);
189  } else {
190  av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
191  av_assert2(0);
192  }
193  bit_buf = value >> bit_left;
194  bit_left += BUF_BITS;
195  }
196  bit_left -= n;
197 #else
198  if (n < bit_left) {
199  bit_buf = (bit_buf << n) | value;
200  bit_left -= n;
201  } else {
202  bit_buf <<= bit_left;
203  bit_buf |= value >> (n - bit_left);
204  if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
205  AV_WBBUF(s->buf_ptr, bit_buf);
206  s->buf_ptr += sizeof(BitBuf);
207  } else {
208  av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
209  av_assert2(0);
210  }
211  bit_left += BUF_BITS - n;
212  bit_buf = value;
213  }
214 #endif
215 
216  s->bit_buf = bit_buf;
217  s->bit_left = bit_left;
218 }
219 
220 /**
221  * Write up to 31 bits into a bitstream.
222  * Use put_bits32 to write 32 bits.
223  */
224 static inline void put_bits(PutBitContext *s, int n, BitBuf value)
225 {
226  av_assert2(n <= 31 && value < (1UL << n));
227  put_bits_no_assert(s, n, value);
228 }
229 
230 static inline void put_bits_le(PutBitContext *s, int n, BitBuf value)
231 {
232  BitBuf bit_buf;
233  int bit_left;
234 
235  av_assert2(n <= 31 && value < (1UL << n));
236 
237  bit_buf = s->bit_buf;
238  bit_left = s->bit_left;
239 
240  bit_buf |= value << (BUF_BITS - bit_left);
241  if (n >= bit_left) {
242  if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
243  AV_WLBUF(s->buf_ptr, bit_buf);
244  s->buf_ptr += sizeof(BitBuf);
245  } else {
246  av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
247  av_assert2(0);
248  }
249  bit_buf = value >> bit_left;
250  bit_left += BUF_BITS;
251  }
252  bit_left -= n;
253 
254  s->bit_buf = bit_buf;
255  s->bit_left = bit_left;
256 }
257 
258 static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
259 {
260  av_assert2(n >= 0 && n <= 31);
261 
262  put_bits(pb, n, av_mod_uintp2(value, n));
263 }
264 
265 /**
266  * Write exactly 32 bits into a bitstream.
267  */
268 static void av_unused put_bits32(PutBitContext *s, uint32_t value)
269 {
270  BitBuf bit_buf;
271  int bit_left;
272 
273  if (BUF_BITS > 32) {
274  put_bits_no_assert(s, 32, value);
275  return;
276  }
277 
278  bit_buf = s->bit_buf;
279  bit_left = s->bit_left;
280 
281 #ifdef BITSTREAM_WRITER_LE
282  bit_buf |= (BitBuf)value << (BUF_BITS - bit_left);
283  if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
284  AV_WLBUF(s->buf_ptr, bit_buf);
285  s->buf_ptr += sizeof(BitBuf);
286  } else {
287  av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
288  av_assert2(0);
289  }
290  bit_buf = (uint64_t)value >> bit_left;
291 #else
292  bit_buf = (uint64_t)bit_buf << bit_left;
293  bit_buf |= (BitBuf)value >> (BUF_BITS - bit_left);
294  if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
295  AV_WBBUF(s->buf_ptr, bit_buf);
296  s->buf_ptr += sizeof(BitBuf);
297  } else {
298  av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
299  av_assert2(0);
300  }
301  bit_buf = value;
302 #endif
303 
304  s->bit_buf = bit_buf;
305  s->bit_left = bit_left;
306 }
307 
308 /**
309  * Write up to 64 bits into a bitstream.
310  */
311 static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
312 {
313  av_assert2((n == 64) || (n < 64 && value < (UINT64_C(1) << n)));
314 
315  if (n < 32)
316  put_bits(s, n, value);
317  else if (n == 32)
318  put_bits32(s, value);
319  else if (n < 64) {
320  uint32_t lo = value & 0xffffffff;
321  uint32_t hi = value >> 32;
322 #ifdef BITSTREAM_WRITER_LE
323  put_bits32(s, lo);
324  put_bits(s, n - 32, hi);
325 #else
326  put_bits(s, n - 32, hi);
327  put_bits32(s, lo);
328 #endif
329  } else {
330  uint32_t lo = value & 0xffffffff;
331  uint32_t hi = value >> 32;
332 #ifdef BITSTREAM_WRITER_LE
333  put_bits32(s, lo);
334  put_bits32(s, hi);
335 #else
336  put_bits32(s, hi);
337  put_bits32(s, lo);
338 #endif
339 
340  }
341 }
342 
343 /**
344  * Return the pointer to the byte where the bitstream writer will put
345  * the next bit.
346  */
348 {
349  return s->buf_ptr;
350 }
351 
352 /**
353  * Skip the given number of bytes.
354  * PutBitContext must be flushed & aligned to a byte boundary before calling this.
355  */
356 static inline void skip_put_bytes(PutBitContext *s, int n)
357 {
358  av_assert2((put_bits_count(s) & 7) == 0);
360  av_assert0(n <= s->buf_end - s->buf_ptr);
361  s->buf_ptr += n;
362 }
363 
364 /**
365  * Skip the given number of bits.
366  * Must only be used if the actual values in the bitstream do not matter.
367  * If n is 0 the behavior is undefined.
368  */
369 static inline void skip_put_bits(PutBitContext *s, int n)
370 {
371  s->bit_left -= n;
372  s->buf_ptr -= sizeof(BitBuf) * ((unsigned)s->bit_left / BUF_BITS);
373  s->bit_left &= (BUF_BITS - 1);
374 }
375 
376 /**
377  * Change the end of the buffer.
378  *
379  * @param size the new size in bytes of the buffer where to put bits
380  */
381 static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
382 {
383  av_assert0(size <= INT_MAX/8 - BUF_BITS);
384  s->buf_end = s->buf + size;
385  s->size_in_bits = 8*size;
386 }
387 
388 #undef AV_WBBUF
389 #undef AV_WLBUF
390 
391 #endif /* AVCODEC_PUT_BITS_H */
#define AV_WLBUF
Definition: put_bits.h:44
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:268
#define NULL
Definition: coverity.c:32
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:53
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:258
static void skip_put_bits(PutBitContext *s, int n)
Skip the given number of bits.
Definition: put_bits.h:369
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
static void rebase_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Rebase the bit writer onto a reallocated buffer.
Definition: put_bits.h:93
ptrdiff_t size
Definition: opengl_enc.c:100
#define av_log(a,...)
#define src
Definition: vp8dsp.c:254
static void put_bits64(PutBitContext *s, int n, uint64_t value)
Write up to 64 bits into a bitstream.
Definition: put_bits.h:311
int size_in_bits
Definition: put_bits.h:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:347
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:107
uint32_t BitBuf
Definition: put_bits.h:42
uint8_t * buf
Definition: put_bits.h:52
simple assert() macros that are a bit more flexible than ISO C assert().
GLsizei GLsizei * length
Definition: opengl_enc.c:114
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:81
static void flush_put_bits_le(PutBitContext *s)
Definition: put_bits.h:136
static void put_bits_le(PutBitContext *s, int n, BitBuf value)
Definition: put_bits.h:230
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:356
BitBuf bit_buf
Definition: put_bits.h:50
int32_t
#define s(width, name)
Definition: cbs_vp9.c:257
static const int BUF_BITS
Definition: put_bits.h:47
static void set_put_bits_buffer_size(PutBitContext *s, int size)
Change the end of the buffer.
Definition: put_bits.h:381
int bit_left
Definition: put_bits.h:51
uint8_t * buf_end
Definition: put_bits.h:52
uint8_t * buf_ptr
Definition: put_bits.h:52
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:64
Definition: kmvc.c:54
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:48
static void put_bits(PutBitContext *s, int n, BitBuf value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:224
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:115
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
#define AV_WBBUF
Definition: put_bits.h:43
static void put_bits_no_assert(PutBitContext *s, int n, BitBuf value)
Definition: put_bits.h:174
GLuint buffer
Definition: opengl_enc.c:101
#define av_unused
Definition: attributes.h:131