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 #else
44 #define AV_WBBUF AV_WB32
45 #define AV_WLBUF AV_WL32
46 #endif
47
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 * @return the total number of bits written to the bitstream.
79 */
81 {
82 return (
s->buf_ptr -
s->buf) * 8 +
BUF_BITS -
s->bit_left;
83 }
84
85 /**
86 * @return the number of bytes output so far; may only be called
87 * when the PutBitContext is freshly initialized or flushed.
88 */
90 {
92 return s->buf_ptr -
s->buf;
93 }
94
95 /**
96 * @param round_up When set, the number of bits written so far will be
97 * rounded up to the next byte.
98 * @return the number of bytes output so far.
99 */
101 {
102 return s->buf_ptr -
s->buf + ((
BUF_BITS -
s->bit_left + (round_up ? 7 : 0)) >> 3);
103 }
104
105 /**
106 * Rebase the bit writer onto a reallocated buffer.
107 *
108 * @param buffer the buffer where to put bits
109 * @param buffer_size the size in bytes of buffer,
110 * must be large enough to hold everything written so far
111 */
113 int buffer_size)
114 {
116
117 s->buf_end =
buffer + buffer_size;
118 s->buf_ptr =
buffer + (
s->buf_ptr -
s->buf);
120 }
121
122 /**
123 * @return the number of bits available in the bitstream.
124 */
126 {
127 return (
s->buf_end -
s->buf_ptr) * 8 -
BUF_BITS +
s->bit_left;
128 }
129
130 /**
131 * @param round_up When set, the number of bits written will be
132 * rounded up to the next byte.
133 * @return the number of bytes left.
134 */
136 {
137 return s->buf_end -
s->buf_ptr - ((
BUF_BITS -
s->bit_left + (round_up ? 7 : 0)) >> 3);
138 }
139
140 /**
141 * Pad the end of the output stream with zeros.
142 */
144 {
145 #ifndef BITSTREAM_WRITER_LE
147 s->bit_buf <<=
s->bit_left;
148 #endif
151 #ifdef BITSTREAM_WRITER_LE
152 *
s->buf_ptr++ =
s->bit_buf;
154 #else
157 #endif
159 }
162 }
163
165 {
168 *
s->buf_ptr++ =
s->bit_buf;
171 }
174 }
175
176 #ifdef BITSTREAM_WRITER_LE
177 #define ff_put_string ff_put_string_unsupported_here
178 #define ff_copy_bits ff_copy_bits_unsupported_here
179 #else
180
181 /**
182 * Put the string string in the bitstream.
183 *
184 * @param terminate_string 0-terminates the written string if value is 1
185 */
187 int terminate_string);
188
189 /**
190 * Copy the content of src to the bitstream.
191 *
192 * @param length the number of bits of src to copy
193 */
195 #endif
196
198 {
200 int bit_left;
201
202 bit_buf =
s->bit_buf;
203 bit_left =
s->bit_left;
204
205 /* XXX: optimize */
206 #ifdef BITSTREAM_WRITER_LE
208 if (n >= bit_left) {
209 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
212 } else {
215 }
216 bit_buf =
value >> bit_left;
218 }
219 bit_left -= n;
220 #else
221 if (n < bit_left) {
222 bit_buf = (bit_buf << n) |
value;
223 bit_left -= n;
224 } else {
225 bit_buf <<= bit_left;
226 bit_buf |=
value >> (n - bit_left);
227 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
230 } else {
233 }
236 }
237 #endif
238
239 s->bit_buf = bit_buf;
240 s->bit_left = bit_left;
241 }
242
243 /**
244 * Write up to 31 bits into a bitstream.
245 * Use put_bits32 to write 32 bits.
246 */
248 {
251 }
252
254 {
256 int bit_left;
257
259
260 bit_buf =
s->bit_buf;
261 bit_left =
s->bit_left;
262
264 if (n >= bit_left) {
265 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
268 } else {
271 }
272 bit_buf =
value >> bit_left;
274 }
275 bit_left -= n;
276
277 s->bit_buf = bit_buf;
278 s->bit_left = bit_left;
279 }
280
282 {
284
286 }
287
288 /**
289 * Write exactly 32 bits into a bitstream.
290 */
292 {
294 int bit_left;
295
298 return;
299 }
300
301 bit_buf =
s->bit_buf;
302 bit_left =
s->bit_left;
303
304 #ifdef BITSTREAM_WRITER_LE
306 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
309 } else {
312 }
313 bit_buf = (uint64_t)
value >> bit_left;
314 #else
315 bit_buf = (uint64_t)bit_buf << bit_left;
317 if (
s->buf_end -
s->buf_ptr >=
sizeof(
BitBuf)) {
320 } else {
323 }
325 #endif
326
327 s->bit_buf = bit_buf;
328 s->bit_left = bit_left;
329 }
330
331 /**
332 * Write up to 64 bits into a bitstream.
333 */
335 {
337
338 if (n < 32)
340 else if (n == 32)
342 else if (n < 64) {
343 uint32_t lo =
value & 0xffffffff;
344 uint32_t hi =
value >> 32;
345 #ifdef BITSTREAM_WRITER_LE
348 #else
351 #endif
352 } else {
353 uint32_t lo =
value & 0xffffffff;
354 uint32_t hi =
value >> 32;
355 #ifdef BITSTREAM_WRITER_LE
358 #else
361 #endif
362
363 }
364 }
365
367 {
369
371 }
372
373 /**
374 * Return the pointer to the byte where the bitstream writer will put
375 * the next bit.
376 */
378 {
380 }
381
382 /**
383 * Skip the given number of bytes.
384 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
385 */
387 {
392 }
393
394 /**
395 * Skip the given number of bits.
396 * Must only be used if the actual values in the bitstream do not matter.
397 * If n is < 0 the behavior is undefined.
398 */
400 {
404 }
405
406 /**
407 * Change the end of the buffer.
408 *
409 * @param size the new size in bytes of the buffer where to put bits
410 */
412 {
414 s->buf_end =
s->buf +
size;
415 }
416
417 /**
418 * Pad the bitstream with zeros up to the next byte boundary.
419 */
421 {
423 }
424
425 #undef AV_WBBUF
426 #undef AV_WLBUF
427
428 #endif /* AVCODEC_PUT_BITS_H */