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