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