GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/put_bits.h Lines: 114 145 78.6 %
Date: 2021-01-20 23:14:43 Branches: 26 42 61.9 %

Line Branch Exec Source
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 {
52
    BitBuf bit_buf;
53
    int bit_left;
54
    uint8_t *buf, *buf_ptr, *buf_end;
55
    int size_in_bits;
56
} PutBitContext;
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
25853294
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
65
                                 int buffer_size)
66
{
67
25853294
    if (buffer_size < 0) {
68
        buffer_size = 0;
69
        buffer      = NULL;
70
    }
71
72
25853294
    s->size_in_bits = 8 * buffer_size;
73
25853294
    s->buf          = buffer;
74
25853294
    s->buf_end      = s->buf + buffer_size;
75
25853294
    s->buf_ptr      = s->buf;
76
25853294
    s->bit_left     = BUF_BITS;
77
25853294
    s->bit_buf      = 0;
78
25853294
}
79
80
/**
81
 * @return the total number of bits written to the bitstream.
82
 */
83
48037332
static inline int put_bits_count(PutBitContext *s)
84
{
85
48037332
    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
 */
95
25
static inline void rebase_put_bits(PutBitContext *s, uint8_t *buffer,
96
                                   int buffer_size)
97
{
98
25
    av_assert0(8*buffer_size >= put_bits_count(s));
99
100
25
    s->buf_end = buffer + buffer_size;
101
25
    s->buf_ptr = buffer + (s->buf_ptr - s->buf);
102
25
    s->buf     = buffer;
103
25
    s->size_in_bits = 8 * buffer_size;
104
25
}
105
106
/**
107
 * @return the number of bits available in the bitstream.
108
 */
109
271353359
static inline int put_bits_left(PutBitContext* s)
110
{
111
271353359
    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
27045614
static inline void flush_put_bits(PutBitContext *s)
118
{
119
#ifndef BITSTREAM_WRITER_LE
120
27044596
    if (s->bit_left < BUF_BITS)
121
24294965
        s->bit_buf <<= s->bit_left;
122
#endif
123
110636759
    while (s->bit_left < BUF_BITS) {
124
83591145
        av_assert0(s->buf_ptr < s->buf_end);
125
#ifdef BITSTREAM_WRITER_LE
126
3406
        *s->buf_ptr++ = s->bit_buf;
127
3406
        s->bit_buf  >>= 8;
128
#else
129
83587739
        *s->buf_ptr++ = s->bit_buf >> (BUF_BITS - 8);
130
83587739
        s->bit_buf  <<= 8;
131
#endif
132
83591145
        s->bit_left  += 8;
133
    }
134
27045614
    s->bit_left = BUF_BITS;
135
27045614
    s->bit_buf  = 0;
136
27045614
}
137
138
1263
static inline void flush_put_bits_le(PutBitContext *s)
139
{
140
6831
    while (s->bit_left < BUF_BITS) {
141
5568
        av_assert0(s->buf_ptr < s->buf_end);
142
5568
        *s->buf_ptr++ = s->bit_buf;
143
5568
        s->bit_buf  >>= 8;
144
5568
        s->bit_left  += 8;
145
    }
146
1263
    s->bit_left = BUF_BITS;
147
1263
    s->bit_buf  = 0;
148
1263
}
149
150
#if FF_API_AVPRIV_PUT_BITS
151
void avpriv_align_put_bits(PutBitContext *s);
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
2503531545
static inline void put_bits_no_assert(PutBitContext *s, int n, BitBuf value)
177
{
178
    BitBuf bit_buf;
179
    int bit_left;
180
181
2503531545
    bit_buf  = s->bit_buf;
182
2503531545
    bit_left = s->bit_left;
183
184
    /* XXX: optimize */
185
#ifdef BITSTREAM_WRITER_LE
186
2868805
    bit_buf |= value << (BUF_BITS - bit_left);
187
2868805
    if (n >= bit_left) {
188
137390
        if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
189
137390
            AV_WLBUF(s->buf_ptr, bit_buf);
190
137390
            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
137390
        bit_buf     = value >> bit_left;
196
137390
        bit_left   += BUF_BITS;
197
    }
198
2868805
    bit_left -= n;
199
#else
200
2500662740
    if (n < bit_left) {
201
2326973676
        bit_buf     = (bit_buf << n) | value;
202
2326973676
        bit_left   -= n;
203
    } else {
204
173689064
        bit_buf   <<= bit_left;
205
173689064
        bit_buf    |= value >> (n - bit_left);
206
173689064
        if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
207
173689064
            AV_WBBUF(s->buf_ptr, bit_buf);
208
173689064
            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
173689064
        bit_left   += BUF_BITS - n;
214
173689064
        bit_buf     = value;
215
    }
216
#endif
217
218
2503531545
    s->bit_buf  = bit_buf;
219
2503531545
    s->bit_left = bit_left;
220
2503531545
}
221
222
/**
223
 * Write up to 31 bits into a bitstream.
224
 * Use put_bits32 to write 32 bits.
225
 */
226
2499853552
static inline void put_bits(PutBitContext *s, int n, BitBuf value)
227
{
228
    av_assert2(n <= 31 && value < (1UL << n));
229
2499853552
    put_bits_no_assert(s, n, value);
230
2499853552
}
231
232
8360325
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
8360325
    bit_buf  = s->bit_buf;
240
8360325
    bit_left = s->bit_left;
241
242
8360325
    bit_buf |= value << (BUF_BITS - bit_left);
243
8360325
    if (n >= bit_left) {
244
646946
        if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
245
646946
            AV_WLBUF(s->buf_ptr, bit_buf);
246
646946
            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
646946
        bit_buf     = value >> bit_left;
252
646946
        bit_left   += BUF_BITS;
253
    }
254
8360325
    bit_left -= n;
255
256
8360325
    s->bit_buf  = bit_buf;
257
8360325
    s->bit_left = bit_left;
258
8360325
}
259
260
198827031
static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
261
{
262
    av_assert2(n >= 0 && n <= 31);
263
264
198827031
    put_bits(pb, n, av_mod_uintp2(value, n));
265
198827031
}
266
267
/**
268
 * Write exactly 32 bits into a bitstream.
269
 */
270
3677993
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
271
{
272
    BitBuf bit_buf;
273
    int bit_left;
274
275
3677993
    if (BUF_BITS > 32) {
276
3677993
        put_bits_no_assert(s, 32, value);
277
3677993
        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
8175
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
8175
    if (n < 32)
318
4079
        put_bits(s, n, value);
319
4096
    else if (n == 32)
320
        put_bits32(s, value);
321
4096
    else if (n < 64) {
322
4096
        uint32_t lo = value & 0xffffffff;
323
4096
        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
4096
        put_bits(s, n - 32, hi);
329
4096
        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
8175
}
344
345
/**
346
 * Return the pointer to the byte where the bitstream writer will put
347
 * the next bit.
348
 */
349
639863
static inline uint8_t *put_bits_ptr(PutBitContext *s)
350
{
351
639863
    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
168047
static inline void skip_put_bytes(PutBitContext *s, int n)
359
{
360
    av_assert2((put_bits_count(s) & 7) == 0);
361
    av_assert2(s->bit_left == BUF_BITS);
362
168047
    av_assert0(n <= s->buf_end - s->buf_ptr);
363
168047
    s->buf_ptr += n;
364
168047
}
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
4054
static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
384
{
385
4054
    av_assert0(size <= INT_MAX/8 - BUF_BITS);
386
4054
    s->buf_end = s->buf + size;
387
4054
    s->size_in_bits = 8*size;
388
4054
}
389
390
/**
391
 * Pad the bitstream with zeros up to the next byte boundary.
392
 */
393
401186
static inline void align_put_bits(PutBitContext *s)
394
{
395
401186
    put_bits(s, s->bit_left & 7, 0);
396
401186
}
397
398
#undef AV_WBBUF
399
#undef AV_WLBUF
400
401
#endif /* AVCODEC_PUT_BITS_H */