Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libwebp/src/utils/bit_writer_utils.h
21151 views
1
// Copyright 2011 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Bit writing and boolean coder
11
//
12
// Author: Skal ([email protected])
13
14
#ifndef WEBP_UTILS_BIT_WRITER_UTILS_H_
15
#define WEBP_UTILS_BIT_WRITER_UTILS_H_
16
17
#include <stddef.h>
18
19
#include "src/webp/types.h"
20
21
#ifdef __cplusplus
22
extern "C" {
23
#endif
24
25
//------------------------------------------------------------------------------
26
// Bit-writing
27
28
typedef struct VP8BitWriter VP8BitWriter;
29
struct VP8BitWriter {
30
int32_t range; // range-1
31
int32_t value;
32
int run; // number of outstanding bits
33
int nb_bits; // number of pending bits
34
uint8_t* buf; // internal buffer. Re-allocated regularly. Not owned.
35
size_t pos;
36
size_t max_pos;
37
int error; // true in case of error
38
};
39
40
// Initialize the object. Allocates some initial memory based on expected_size.
41
int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size);
42
// Finalize the bitstream coding. Returns a pointer to the internal buffer.
43
uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw);
44
// Release any pending memory and zeroes the object. Not a mandatory call.
45
// Only useful in case of error, when the internal buffer hasn't been grabbed!
46
void VP8BitWriterWipeOut(VP8BitWriter* const bw);
47
48
int VP8PutBit(VP8BitWriter* const bw, int bit, int prob);
49
int VP8PutBitUniform(VP8BitWriter* const bw, int bit);
50
void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits);
51
void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits);
52
53
// Appends some bytes to the internal buffer. Data is copied.
54
int VP8BitWriterAppend(VP8BitWriter* const bw,
55
const uint8_t* data, size_t size);
56
57
// return approximate write position (in bits)
58
static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {
59
const uint64_t nb_bits = 8 + bw->nb_bits; // bw->nb_bits is <= 0, note
60
return (bw->pos + bw->run) * 8 + nb_bits;
61
}
62
63
// Returns a pointer to the internal buffer.
64
static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) {
65
return bw->buf;
66
}
67
// Returns the size of the internal buffer.
68
static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) {
69
return bw->pos;
70
}
71
72
//------------------------------------------------------------------------------
73
// VP8LBitWriter
74
75
#if defined(__x86_64__) || defined(_M_X64) // 64bit
76
typedef uint64_t vp8l_atype_t; // accumulator type
77
typedef uint32_t vp8l_wtype_t; // writing type
78
#define WSWAP HToLE32
79
#define VP8L_WRITER_BYTES 4 // sizeof(vp8l_wtype_t)
80
#define VP8L_WRITER_BITS 32 // 8 * sizeof(vp8l_wtype_t)
81
#define VP8L_WRITER_MAX_BITS 64 // 8 * sizeof(vp8l_atype_t)
82
#else
83
typedef uint32_t vp8l_atype_t;
84
typedef uint16_t vp8l_wtype_t;
85
#define WSWAP HToLE16
86
#define VP8L_WRITER_BYTES 2
87
#define VP8L_WRITER_BITS 16
88
#define VP8L_WRITER_MAX_BITS 32
89
#endif
90
91
typedef struct {
92
vp8l_atype_t bits; // bit accumulator
93
int used; // number of bits used in accumulator
94
uint8_t* buf; // start of buffer
95
uint8_t* cur; // current write position
96
uint8_t* end; // end of buffer
97
98
// After all bits are written (VP8LBitWriterFinish()), the caller must observe
99
// the state of 'error'. A value of 1 indicates that a memory allocation
100
// failure has happened during bit writing. A value of 0 indicates successful
101
// writing of bits.
102
int error;
103
} VP8LBitWriter;
104
105
static WEBP_INLINE size_t VP8LBitWriterNumBytes(const VP8LBitWriter* const bw) {
106
return (bw->cur - bw->buf) + ((bw->used + 7) >> 3);
107
}
108
109
// Returns false in case of memory allocation error.
110
int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size);
111
// Returns false in case of memory allocation error.
112
int VP8LBitWriterClone(const VP8LBitWriter* const src,
113
VP8LBitWriter* const dst);
114
// Finalize the bitstream coding. Returns a pointer to the internal buffer.
115
uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw);
116
// Release any pending memory and zeroes the object.
117
void VP8LBitWriterWipeOut(VP8LBitWriter* const bw);
118
// Resets the cursor of the BitWriter bw to when it was like in bw_init.
119
void VP8LBitWriterReset(const VP8LBitWriter* const bw_init,
120
VP8LBitWriter* const bw);
121
// Swaps the memory held by two BitWriters.
122
void VP8LBitWriterSwap(VP8LBitWriter* const src, VP8LBitWriter* const dst);
123
124
// Internal function for VP8LPutBits flushing 32 bits from the written state.
125
void VP8LPutBitsFlushBits(VP8LBitWriter* const bw);
126
127
// PutBits internal function used in the 16 bit vp8l_wtype_t case.
128
void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits);
129
130
// This function writes bits into bytes in increasing addresses (little endian),
131
// and within a byte least-significant-bit first.
132
// This function can write up to 32 bits in one go, but VP8LBitReader can only
133
// read 24 bits max (VP8L_MAX_NUM_BIT_READ).
134
// VP8LBitWriter's 'error' flag is set in case of memory allocation error.
135
static WEBP_INLINE void VP8LPutBits(VP8LBitWriter* const bw,
136
uint32_t bits, int n_bits) {
137
if (sizeof(vp8l_wtype_t) == 4) {
138
if (n_bits > 0) {
139
if (bw->used >= 32) {
140
VP8LPutBitsFlushBits(bw);
141
}
142
bw->bits |= (vp8l_atype_t)bits << bw->used;
143
bw->used += n_bits;
144
}
145
} else {
146
VP8LPutBitsInternal(bw, bits, n_bits);
147
}
148
}
149
150
//------------------------------------------------------------------------------
151
152
#ifdef __cplusplus
153
} // extern "C"
154
#endif
155
156
#endif // WEBP_UTILS_BIT_WRITER_UTILS_H_
157
158