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"
36
37 #if ARCH_X86_64
38 // TODO: Benchmark and optionally enable on other 64-bit architectures.
40 #define AV_WBBUF AV_WB64
41 #define AV_WLBUF AV_WL64
42 #define BUF_BITS 64
43 #else
45 #define AV_WBBUF AV_WB32
46 #define AV_WLBUF AV_WL32
48 #endif
49
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 */
63 int buffer_size)
64 {
65 if (buffer_size < 0) {
66 buffer_size = 0;
68 }
69
71 s->buf_end =
s->buf + buffer_size;
75 }
76
77 /**
78 * Inform the compiler that a PutBitContext is flushed (i.e. if it has just
79 * been initialized or flushed). Undefined behaviour occurs if this is used
80 * with a PutBitContext for which this is not true.
81 */
83 {
85 }
86
87 /**
88 * @return the total number of bits written to the bitstream.
89 */
91 {
92 return (
s->buf_ptr -
s->buf) * 8 +
BUF_BITS -
s->bit_left;
93 }
94
95 /**
96 * @return the number of bytes output so far; may only be called
97 * when the PutBitContext is freshly initialized or flushed.
98 */
100 {
102 return s->buf_ptr -
s->buf;
103 }
104
105 /**
106 * @param round_up When set, the number of bits written so far will be
107 * rounded up to the next byte.
108 * @return the number of bytes output so far.
109 */
111 {
112 return s->buf_ptr -
s->buf + ((
BUF_BITS -
s->bit_left + (round_up ? 7 : 0)) >> 3);
113 }
114
115 /**
116 * Rebase the bit writer onto a reallocated buffer.
117 *
118 * @param buffer the buffer where to put bits
119 * @param buffer_size the size in bytes of buffer,
120 * must be large enough to hold everything written so far
121 */
123 int buffer_size)
124 {
126
127 s->buf_end =
buffer + buffer_size;
128 s->buf_ptr =
buffer + (
s->buf_ptr -
s->buf);
130 }
131
132 /**
133 * @return the number of bits available in the bitstream.
134 */
136 {
137 return (
s->buf_end -
s->buf_ptr) * 8 -
BUF_BITS +
s->bit_left;
138 }
139
140 /**
141 * @param round_up When set, the number of bits written will be
142 * rounded up to the next byte.
143 * @return the number of bytes left.
144 */
146 {
147 return s->buf_end -
s->buf_ptr - ((
BUF_BITS -
s->bit_left + (round_up ? 7 : 0)) >> 3);
148 }
149
150 /**
151 * Pad the end of the output stream with zeros.
152 */
154 {
155 #ifndef BITSTREAM_WRITER_LE
157 s->bit_buf <<=
s->bit_left;
158 #endif
161 #ifdef BITSTREAM_WRITER_LE
162 *
s->buf_ptr++ =
s->bit_buf;
164 #else
167 #endif
169 }
172 }
173
175 {
178 *
s->buf_ptr++ =
s->bit_buf;
181 }
184 }
185
186 #ifdef BITSTREAM_WRITER_LE
187 #define ff_put_string ff_put_string_unsupported_here
188 #define ff_copy_bits ff_copy_bits_unsupported_here
189 #else
190
191 /**
192 * Put the string string in the bitstream.
193 *
194 * @param terminate_string 0-terminates the written string if value is 1
195 */
197 int terminate_string);
198
199 /**
200 * Copy the content of src to the bitstream.
201 *
202 * @param length the number of bits of src to copy
203 */
205 #endif
206
208 {
210 int bit_left;
211
212 bit_buf =
s->bit_buf;
213 bit_left =
s->bit_left;
214
215 /* XXX: optimize */
216 #ifdef BITSTREAM_WRITER_LE
218 if (n >= bit_left) {
219 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
222 } else {
225 }
226 bit_buf =
value >> bit_left;
228 }
229 bit_left -= n;
230 #else
231 if (n < bit_left) {
232 bit_buf = (bit_buf << n) |
value;
233 bit_left -= n;
234 } else {
235 bit_buf <<= bit_left;
236 bit_buf |=
value >> (n - bit_left);
237 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
240 } else {
243 }
246 }
247 #endif
248
249 s->bit_buf = bit_buf;
250 s->bit_left = bit_left;
251 }
252
253 /**
254 * Write up to 31 bits into a bitstream.
255 * Use put_bits32 to write 32 bits.
256 */
258 {
261 }
262
264 {
266 int bit_left;
267
269
270 bit_buf =
s->bit_buf;
271 bit_left =
s->bit_left;
272
274 if (n >= bit_left) {
275 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
278 } else {
281 }
282 bit_buf =
value >> bit_left;
284 }
285 bit_left -= n;
286
287 s->bit_buf = bit_buf;
288 s->bit_left = bit_left;
289 }
290
292 {
294
296 }
297
298 /**
299 * Write exactly 32 bits into a bitstream.
300 */
302 {
304 int bit_left;
305
308 return;
309 }
310
311 bit_buf =
s->bit_buf;
312 bit_left =
s->bit_left;
313
314 #ifdef BITSTREAM_WRITER_LE
316 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
319 } else {
322 }
323 bit_buf = (uint64_t)
value >> bit_left;
324 #else
325 bit_buf = (uint64_t)bit_buf << bit_left;
327 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
330 } else {
333 }
335 #endif
336
337 s->bit_buf = bit_buf;
338 s->bit_left = bit_left;
339 }
340
341 /**
342 * Write up to 63 bits into a bitstream.
343 */
345 {
347
348 #if BUF_BITS >= 64
350 #else
351 if (n < 32)
353 else if (n == 32)
355 else if (n < 64) {
356 uint32_t lo =
value & 0xffffffff;
357 uint32_t hi =
value >> 32;
358 #ifdef BITSTREAM_WRITER_LE
361 #else
364 #endif
365 }
366 #endif
367 }
368
369 /**
370 * Write up to 64 bits into a bitstream.
371 */
373 {
375
376 if (n < 64) {
378 } else {
379 uint32_t lo =
value & 0xffffffff;
380 uint32_t hi =
value >> 32;
381 #ifdef BITSTREAM_WRITER_LE
384 #else
387 #endif
388 }
389 }
390
392 {
394
396 }
397
398 /**
399 * Return the pointer to the byte where the bitstream writer will put
400 * the next bit.
401 */
403 {
405 }
406
407 /**
408 * Skip the given number of bytes.
409 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
410 */
412 {
417 }
418
419 /**
420 * Skip the given number of bits.
421 * Must only be used if the actual values in the bitstream do not matter.
422 * If n is < 0 the behavior is undefined.
423 */
425 {
429 }
430
431 /**
432 * Change the end of the buffer.
433 *
434 * @param size the new size in bytes of the buffer where to put bits
435 */
437 {
439 s->buf_end =
s->buf +
size;
440 }
441
442 /**
443 * Pad the bitstream with zeros up to the next byte boundary.
444 */
446 {
448 }
449
450 #undef AV_WBBUF
451 #undef AV_WLBUF
452
453 #endif /* AVCODEC_PUT_BITS_H */