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