1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* FFv1 codec
*
* Copyright (c) 2024 Lynne <dev@lynne.ee>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
struct RangeCoder {
u8buf bytestream_start;
u8buf bytestream;
uint low;
uint16_t range;
uint16_t outstanding_count;
uint8_t outstanding_byte;
};
/* Full renorm version that can handle outstanding_byte == 0xFF */
void renorm_encoder_full(inout RangeCoder c)
{
int bs_cnt = 0;
if (c.outstanding_byte == 0xFF) {
c.outstanding_byte = uint8_t(c.low >> 8);
} else if (c.low <= 0xFF00) {
c.bytestream[bs_cnt++].v = c.outstanding_byte;
uint16_t cnt = c.outstanding_count;
for (; cnt > 0; cnt--)
c.bytestream[bs_cnt++].v = uint8_t(0xFF);
c.outstanding_count = uint16_t(0);
c.outstanding_byte = uint8_t(c.low >> 8);
} else if (c.low >= 0x10000) {
c.bytestream[bs_cnt++].v = c.outstanding_byte + uint8_t(1);
uint16_t cnt = c.outstanding_count;
for (; cnt > 0; cnt--)
c.bytestream[bs_cnt++].v = uint8_t(0x00);
c.outstanding_count = uint16_t(0);
c.outstanding_byte = uint8_t(bitfieldExtract(c.low, 8, 8));
} else {
c.outstanding_count++;
}
c.bytestream = OFFBUF(u8buf, c.bytestream, bs_cnt);
c.range <<= 8;
c.low = bitfieldInsert(0, c.low, 8, 8);
}
/* Cannot deal with outstanding_byte == -1 in the name of speed */
void renorm_encoder(inout RangeCoder c)
{
uint16_t oc = c.outstanding_count + uint16_t(1);
uint low = c.low;
c.range <<= 8;
c.low = bitfieldInsert(0, low, 8, 8);
if (low > 0xFF00 && low < 0x10000) {
c.outstanding_count = oc;
return;
}
u8buf bs = c.bytestream;
uint8_t outstanding_byte = c.outstanding_byte;
c.bytestream = OFFBUF(u8buf, bs, oc);
c.outstanding_count = uint16_t(0);
c.outstanding_byte = uint8_t(low >> 8);
uint8_t obs = uint8_t(low > 0xFF00);
uint8_t fill = obs - uint8_t(1); /* unsigned underflow */
bs[0].v = outstanding_byte + obs;
for (int i = 1; i < oc; i++)
bs[i].v = fill;
}
void put_rac_norenorm(inout RangeCoder c, uint64_t state, bool bit)
{
u8buf sb = u8buf(state);
uint val = uint(sb.v);
uint16_t range1 = uint16_t((uint(c.range) * val) >> 8);
#ifdef DEBUG
if (val == 0)
debugPrintfEXT("Error: state is zero (addr: 0x%lx)", uint64_t(sb));
if (range1 >= c.range)
debugPrintfEXT("Error: range1 >= c.range");
if (range1 <= 0)
debugPrintfEXT("Error: range1 <= 0");
#endif
uint16_t diff = c.range - range1;
if (bit) {
c.low += diff;
c.range = range1;
} else {
c.range = diff;
}
sb.v = zero_one_state[(uint(bit) << 8) + val];
#ifdef DEBUG
if (sb.v == 0)
debugPrintfEXT("Error: inserted zero state from tab %i idx %i", bit, val);
#endif
}
/* Equiprobable bit */
void put_rac_equi(inout RangeCoder c, bool bit)
{
uint16_t range1 = c.range >> 1;
#ifdef DEBUG
if (range1 >= c.range)
debugPrintfEXT("Error: range1 >= c.range");
if (range1 <= 0)
debugPrintfEXT("Error: range1 <= 0");
#endif
if (bit) {
c.low += c.range - range1;
c.range = range1;
} else {
c.range -= range1;
}
if (c.range < 0x100)
renorm_encoder(c);
}
void put_rac_terminate(inout RangeCoder c)
{
uint16_t range1 = uint16_t((uint(c.range) * 129) >> 8);
#ifdef DEBUG
if (range1 >= c.range)
debugPrintfEXT("Error: range1 >= c.range");
if (range1 <= 0)
debugPrintfEXT("Error: range1 <= 0");
#endif
c.range -= range1;
if (c.range < 0x100)
renorm_encoder(c);
}
/* Return the number of bytes written. */
uint32_t rac_terminate(inout RangeCoder c)
{
put_rac_terminate(c);
c.range = uint16_t(0xFF);
c.low += 0xFF;
renorm_encoder(c);
c.range = uint16_t(0xFF);
renorm_encoder(c);
#ifdef DEBUG
if (c.low != 0)
debugPrintfEXT("Error: c.low != 0");
if (c.range < 0x100)
debugPrintfEXT("Error: range < 0x100");
#endif
return uint32_t(uint64_t(c.bytestream) - uint64_t(c.bytestream_start));
}
void rac_init(out RangeCoder r, u8buf data, uint64_t buf_size)
{
r.bytestream_start = data;
r.bytestream = data;
r.low = 0;
r.range = uint16_t(0xFF00);
r.outstanding_count = uint16_t(0);
r.outstanding_byte = uint8_t(0xFF);
}
|