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