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