GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/put_bits.h Lines: 117 148 79.1 %
Date: 2021-04-19 06:27:07 Branches: 29 46 63.0 %

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
#if LIBAVCODEC_VERSION_MAJOR < 59
56
    int size_in_bits;
57
#endif
58
} PutBitContext;
59
60
/**
61
 * Initialize the PutBitContext s.
62
 *
63
 * @param buffer the buffer where to put bits
64
 * @param buffer_size the size in bytes of buffer
65
 */
66
25903917
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
67
                                 int buffer_size)
68
{
69
25903917
    if (buffer_size < 0) {
70
        buffer_size = 0;
71
        buffer      = NULL;
72
    }
73
74
25903917
    s->buf          = buffer;
75
25903917
    s->buf_end      = s->buf + buffer_size;
76
25903917
    s->buf_ptr      = s->buf;
77
25903917
    s->bit_left     = BUF_BITS;
78
25903917
    s->bit_buf      = 0;
79
25903917
}
80
81
/**
82
 * @return the total number of bits written to the bitstream.
83
 */
84
13984315
static inline int put_bits_count(PutBitContext *s)
85
{
86
13984315
    return (s->buf_ptr - s->buf) * 8 + BUF_BITS - s->bit_left;
87
}
88
89
/**
90
 * @return the number of bytes output so far; may only be called
91
 *         when the PutBitContext is freshly initialized or flushed.
92
 */
93
128402
static inline int put_bytes_output(const PutBitContext *s)
94
{
95
    av_assert2(s->bit_left == BUF_BITS);
96
128402
    return s->buf_ptr - s->buf;
97
}
98
99
/**
100
 * @param  round_up  When set, the number of bits written so far will be
101
 *                   rounded up to the next byte.
102
 * @return the number of bytes output so far.
103
 */
104
1278809
static inline int put_bytes_count(const PutBitContext *s, int round_up)
105
{
106
1278809
    return s->buf_ptr - s->buf + ((BUF_BITS - s->bit_left + (round_up ? 7 : 0)) >> 3);
107
}
108
109
/**
110
 * Rebase the bit writer onto a reallocated buffer.
111
 *
112
 * @param buffer the buffer where to put bits
113
 * @param buffer_size the size in bytes of buffer,
114
 *                    must be large enough to hold everything written so far
115
 */
116
25
static inline void rebase_put_bits(PutBitContext *s, uint8_t *buffer,
117
                                   int buffer_size)
118
{
119
25
    av_assert0(8*buffer_size >= put_bits_count(s));
120
121
25
    s->buf_end = buffer + buffer_size;
122
25
    s->buf_ptr = buffer + (s->buf_ptr - s->buf);
123
25
    s->buf     = buffer;
124
25
}
125
126
/**
127
 * @return the number of bits available in the bitstream.
128
 */
129
271794361
static inline int put_bits_left(PutBitContext* s)
130
{
131
271794361
    return (s->buf_end - s->buf_ptr) * 8 - BUF_BITS + s->bit_left;
132
}
133
134
/**
135
 * @param  round_up  When set, the number of bits written will be
136
 *                   rounded up to the next byte.
137
 * @return the number of bytes left.
138
 */
139
32701471
static inline int put_bytes_left(const PutBitContext *s, int round_up)
140
{
141
32701471
    return s->buf_end - s->buf_ptr - ((BUF_BITS - s->bit_left + (round_up ? 7 : 0)) >> 3);
142
}
143
144
/**
145
 * Pad the end of the output stream with zeros.
146
 */
147
27097276
static inline void flush_put_bits(PutBitContext *s)
148
{
149
#ifndef BITSTREAM_WRITER_LE
150
27096258
    if (s->bit_left < BUF_BITS)
151
24346040
        s->bit_buf <<= s->bit_left;
152
#endif
153
110904863
    while (s->bit_left < BUF_BITS) {
154
83807587
        av_assert0(s->buf_ptr < s->buf_end);
155
#ifdef BITSTREAM_WRITER_LE
156
3406
        *s->buf_ptr++ = s->bit_buf;
157
3406
        s->bit_buf  >>= 8;
158
#else
159
83804181
        *s->buf_ptr++ = s->bit_buf >> (BUF_BITS - 8);
160
83804181
        s->bit_buf  <<= 8;
161
#endif
162
83807587
        s->bit_left  += 8;
163
    }
164
27097276
    s->bit_left = BUF_BITS;
165
27097276
    s->bit_buf  = 0;
166
27097276
}
167
168
1263
static inline void flush_put_bits_le(PutBitContext *s)
169
{
170
6831
    while (s->bit_left < BUF_BITS) {
171
5568
        av_assert0(s->buf_ptr < s->buf_end);
172
5568
        *s->buf_ptr++ = s->bit_buf;
173
5568
        s->bit_buf  >>= 8;
174
5568
        s->bit_left  += 8;
175
    }
176
1263
    s->bit_left = BUF_BITS;
177
1263
    s->bit_buf  = 0;
178
1263
}
179
180
#if FF_API_AVPRIV_PUT_BITS
181
void avpriv_align_put_bits(PutBitContext *s);
182
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
183
#endif
184
185
#ifdef BITSTREAM_WRITER_LE
186
#define ff_put_string ff_put_string_unsupported_here
187
#define ff_copy_bits ff_copy_bits_unsupported_here
188
#else
189
190
/**
191
 * Put the string string in the bitstream.
192
 *
193
 * @param terminate_string 0-terminates the written string if value is 1
194
 */
195
void ff_put_string(PutBitContext *pb, const char *string,
196
                       int terminate_string);
197
198
/**
199
 * Copy the content of src to the bitstream.
200
 *
201
 * @param length the number of bits of src to copy
202
 */
203
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
204
#endif
205
206
2483185429
static inline void put_bits_no_assert(PutBitContext *s, int n, BitBuf value)
207
{
208
    BitBuf bit_buf;
209
    int bit_left;
210
211
2483185429
    bit_buf  = s->bit_buf;
212
2483185429
    bit_left = s->bit_left;
213
214
    /* XXX: optimize */
215
#ifdef BITSTREAM_WRITER_LE
216
2868805
    bit_buf |= value << (BUF_BITS - bit_left);
217
2868805
    if (n >= bit_left) {
218
137390
        if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
219
137390
            AV_WLBUF(s->buf_ptr, bit_buf);
220
137390
            s->buf_ptr += sizeof(BitBuf);
221
        } else {
222
            av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
223
            av_assert2(0);
224
        }
225
137390
        bit_buf     = value >> bit_left;
226
137390
        bit_left   += BUF_BITS;
227
    }
228
2868805
    bit_left -= n;
229
#else
230
2480316624
    if (n < bit_left) {
231
2308829291
        bit_buf     = (bit_buf << n) | value;
232
2308829291
        bit_left   -= n;
233
    } else {
234
171487333
        bit_buf   <<= bit_left;
235
171487333
        bit_buf    |= value >> (n - bit_left);
236
171487333
        if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
237
171487333
            AV_WBBUF(s->buf_ptr, bit_buf);
238
171487333
            s->buf_ptr += sizeof(BitBuf);
239
        } else {
240
            av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
241
            av_assert2(0);
242
        }
243
171487333
        bit_left   += BUF_BITS - n;
244
171487333
        bit_buf     = value;
245
    }
246
#endif
247
248
2483185429
    s->bit_buf  = bit_buf;
249
2483185429
    s->bit_left = bit_left;
250
2483185429
}
251
252
/**
253
 * Write up to 31 bits into a bitstream.
254
 * Use put_bits32 to write 32 bits.
255
 */
256
2479456538
static inline void put_bits(PutBitContext *s, int n, BitBuf value)
257
{
258
    av_assert2(n <= 31 && value < (1UL << n));
259
2479456538
    put_bits_no_assert(s, n, value);
260
2479456538
}
261
262
8360325
static inline void put_bits_le(PutBitContext *s, int n, BitBuf value)
263
{
264
    BitBuf bit_buf;
265
    int bit_left;
266
267
    av_assert2(n <= 31 && value < (1UL << n));
268
269
8360325
    bit_buf  = s->bit_buf;
270
8360325
    bit_left = s->bit_left;
271
272
8360325
    bit_buf |= value << (BUF_BITS - bit_left);
273
8360325
    if (n >= bit_left) {
274
646946
        if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
275
646946
            AV_WLBUF(s->buf_ptr, bit_buf);
276
646946
            s->buf_ptr += sizeof(BitBuf);
277
        } else {
278
            av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
279
            av_assert2(0);
280
        }
281
646946
        bit_buf     = value >> bit_left;
282
646946
        bit_left   += BUF_BITS;
283
    }
284
8360325
    bit_left -= n;
285
286
8360325
    s->bit_buf  = bit_buf;
287
8360325
    s->bit_left = bit_left;
288
8360325
}
289
290
198842189
static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
291
{
292
    av_assert2(n >= 0 && n <= 31);
293
294
198842189
    put_bits(pb, n, av_mod_uintp2(value, n));
295
198842189
}
296
297
/**
298
 * Write exactly 32 bits into a bitstream.
299
 */
300
3728891
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
301
{
302
    BitBuf bit_buf;
303
    int bit_left;
304
305
3728891
    if (BUF_BITS > 32) {
306
3728891
        put_bits_no_assert(s, 32, value);
307
3728891
        return;
308
    }
309
310
    bit_buf  = s->bit_buf;
311
    bit_left = s->bit_left;
312
313
#ifdef BITSTREAM_WRITER_LE
314
    bit_buf |= (BitBuf)value << (BUF_BITS - bit_left);
315
    if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
316
        AV_WLBUF(s->buf_ptr, bit_buf);
317
        s->buf_ptr += sizeof(BitBuf);
318
    } else {
319
        av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
320
        av_assert2(0);
321
    }
322
    bit_buf     = (uint64_t)value >> bit_left;
323
#else
324
    bit_buf     = (uint64_t)bit_buf << bit_left;
325
    bit_buf    |= (BitBuf)value >> (BUF_BITS - bit_left);
326
    if (s->buf_end - s->buf_ptr >= sizeof(BitBuf)) {
327
        AV_WBBUF(s->buf_ptr, bit_buf);
328
        s->buf_ptr += sizeof(BitBuf);
329
    } else {
330
        av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
331
        av_assert2(0);
332
    }
333
    bit_buf     = value;
334
#endif
335
336
    s->bit_buf  = bit_buf;
337
    s->bit_left = bit_left;
338
}
339
340
/**
341
 * Write up to 64 bits into a bitstream.
342
 */
343
8175
static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
344
{
345
    av_assert2((n == 64) || (n < 64 && value < (UINT64_C(1) << n)));
346
347
8175
    if (n < 32)
348
4079
        put_bits(s, n, value);
349
4096
    else if (n == 32)
350
        put_bits32(s, value);
351
4096
    else if (n < 64) {
352
4096
        uint32_t lo = value & 0xffffffff;
353
4096
        uint32_t hi = value >> 32;
354
#ifdef BITSTREAM_WRITER_LE
355
        put_bits32(s, lo);
356
        put_bits(s, n - 32, hi);
357
#else
358
4096
        put_bits(s, n - 32, hi);
359
4096
        put_bits32(s, lo);
360
#endif
361
    } else {
362
        uint32_t lo = value & 0xffffffff;
363
        uint32_t hi = value >> 32;
364
#ifdef BITSTREAM_WRITER_LE
365
        put_bits32(s, lo);
366
        put_bits32(s, hi);
367
#else
368
        put_bits32(s, hi);
369
        put_bits32(s, lo);
370
#endif
371
372
    }
373
8175
}
374
375
/**
376
 * Return the pointer to the byte where the bitstream writer will put
377
 * the next bit.
378
 */
379
21971002
static inline uint8_t *put_bits_ptr(PutBitContext *s)
380
{
381
21971002
    return s->buf_ptr;
382
}
383
384
/**
385
 * Skip the given number of bytes.
386
 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
387
 */
388
168086
static inline void skip_put_bytes(PutBitContext *s, int n)
389
{
390
    av_assert2((put_bits_count(s) & 7) == 0);
391
    av_assert2(s->bit_left == BUF_BITS);
392
168086
    av_assert0(n <= s->buf_end - s->buf_ptr);
393
168086
    s->buf_ptr += n;
394
168086
}
395
396
/**
397
 * Skip the given number of bits.
398
 * Must only be used if the actual values in the bitstream do not matter.
399
 * If n is < 0 the behavior is undefined.
400
 */
401
static inline void skip_put_bits(PutBitContext *s, int n)
402
{
403
    unsigned bits = BUF_BITS - s->bit_left + n;
404
    s->buf_ptr += sizeof(BitBuf) * (bits / BUF_BITS);
405
    s->bit_left = BUF_BITS - (bits & (BUF_BITS - 1));
406
}
407
408
/**
409
 * Change the end of the buffer.
410
 *
411
 * @param size the new size in bytes of the buffer where to put bits
412
 */
413
4054
static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
414
{
415
4054
    av_assert0(size <= INT_MAX/8 - BUF_BITS);
416
4054
    s->buf_end = s->buf + size;
417
4054
}
418
419
/**
420
 * Pad the bitstream with zeros up to the next byte boundary.
421
 */
422
402173
static inline void align_put_bits(PutBitContext *s)
423
{
424
402173
    put_bits(s, s->bit_left & 7, 0);
425
402173
}
426
427
#undef AV_WBBUF
428
#undef AV_WLBUF
429
430
#endif /* AVCODEC_PUT_BITS_H */