2 * ELS (Entropy Logarithmic-Scale) decoder
4 * Copyright (c) 2013 Maxim Poliakovski
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Entropy Logarithmic-Scale binary arithmetic decoder
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
37 /* ELS coder constants and structures. */
38 #define ELS_JOTS_PER_BYTE 36
39 #define ELS_MAX (1 << 24)
40 #define RUNG_SPACE (64 * sizeof(ElsRungNode))
42 /* ELS coder tables. */
43 static const struct Ladder {
91 { -2, -13, 152, 164 },
129 { -1, -30, 78, 100 },
131 { -1, -29, 80, 102 },
133 { -1, -29, 82, 104 },
135 { -1, -28, 84, 104 },
137 { -1, -28, 86, 108 },
139 { -1, -27, 88, 108 },
141 { -1, -27, 90, 112 },
143 { -1, -26, 92, 112 },
145 { -1, -26, 94, 114 },
147 { -1, -25, 96, 116 },
148 { -1, -20, 101, 83 },
149 { -1, -25, 98, 118 },
150 { -1, -21, 103, 83 },
151 { -1, -24, 100, 120 },
152 { -1, -21, 105, 85 },
153 { -1, -24, 102, 122 },
154 { -1, -22, 107, 87 },
155 { -1, -23, 104, 124 },
156 { -1, -22, 109, 89 },
157 { -1, -23, 106, 126 },
158 { -1, -22, 111, 91 },
159 { -1, -22, 108, 128 },
160 { -1, -23, 113, 93 },
161 { -1, -22, 110, 130 },
162 { -1, -23, 115, 95 },
163 { -1, -22, 112, 132 },
164 { -1, -24, 117, 97 },
165 { -1, -21, 114, 134 },
166 { -1, -24, 119, 99 },
167 { -1, -21, 116, 136 },
168 { -1, -25, 121, 101 },
169 { -1, -20, 118, 136 },
170 { -1, -25, 123, 103 },
171 { -1, -20, 120, 138 },
172 { -1, -26, 125, 105 },
173 { -1, -20, 122, 140 },
174 { -1, -26, 127, 107 },
175 { -1, -19, 124, 142 },
176 { -1, -27, 129, 107 },
177 { -1, -19, 126, 144 },
178 { -1, -27, 131, 111 },
179 { -1, -18, 128, 146 },
180 { -1, -28, 133, 111 },
181 { -1, -18, 130, 146 },
182 { -1, -28, 135, 115 },
183 { -1, -18, 132, 148 },
184 { -1, -29, 137, 115 },
185 { -1, -17, 134, 150 },
186 { -1, -29, 139, 117 },
187 { -1, -17, 136, 152 },
188 { -1, -30, 141, 119 },
189 { -1, -16, 138, 152 },
190 { -1, -31, 143, 121 },
191 { -1, -16, 140, 154 },
192 { -1, -31, 145, 123 },
193 { -1, -15, 142, 156 },
194 { -1, -32, 147, 125 },
195 { -1, -15, 144, 158 },
196 { -1, -33, 149, 127 },
197 { -1, -15, 146, 158 },
198 { -1, -34, 151, 129 },
199 { -1, -14, 148, 160 },
200 { -1, -35, 153, 131 },
201 { -1, -14, 150, 160 },
202 { -1, -36, 155, 133 },
203 { -2, -13, 152, 162 },
204 { -1, -37, 157, 135 },
205 { -2, -12, 154, 164 },
206 { -1, -39, 159, 137 },
207 { -2, -12, 156, 164 },
208 { -1, -41, 161, 139 },
209 { -2, -11, 158, 166 },
210 { -1, -43, 163, 141 },
211 { -2, -10, 160, 166 },
212 { -1, -46, 165, 143 },
213 { -3, -9, 162, 168 },
214 { -1, -51, 167, 143 },
215 { -3, -8, 164, 170 },
216 { -1, -61, 169, 145 },
217 { -4, -7, 166, 170 },
218 { -1, -72, 169, 145 },
220 { 0, -108, 171, 171 },
221 { 0, -108, 172, 172 },
222 { -6, -5, 173, 173 },
225 static const uint32_t els_exp_tab[ELS_JOTS_PER_BYTE * 4 + 1] = {
226 0, 0, 0, 0, 0, 0, 0, 0,
227 0, 0, 0, 0, 0, 0, 0, 0,
228 0, 0, 0, 0, 0, 0, 0, 0,
229 0, 0, 0, 0, 0, 0, 0, 0,
230 0, 0, 0, 0, 1, 1, 1, 1,
231 1, 2, 2, 2, 3, 4, 4, 5,
232 6, 7, 8, 10, 11, 13, 16, 18,
233 21, 25, 29, 34, 40, 47, 54, 64,
234 74, 87, 101, 118, 138, 161, 188, 219,
235 256, 298, 348, 406, 474, 552, 645, 752,
236 877, 1024, 1194, 1393, 1625, 1896, 2211, 2580,
237 3010, 3511, 4096, 4778, 5573, 6501, 7584, 8847,
238 10321, 12040, 14045, 16384, 19112, 22295, 26007, 30339,
239 35391, 41285, 48160, 56180, 65536, 76288, 89088, 103936,
240 121344, 141312, 165120, 192512, 224512, 262144, 305664, 356608,
241 416000, 485376, 566016, 660480, 770560, 898816, 1048576, 1223168,
242 1426688, 1664256, 1941504, 2264832, 2642176, 3082240, 3595520, 4194304,
243 4892672, 5707520, 6657792, 7766784, 9060096, 10568960, 12328960, 14382080,
247 void ff_els_decoder_init(ElsDecCtx *ctx, const uint8_t *in, size_t data_size)
251 /* consume up to 3 bytes from the input data */
252 if (data_size >= 3) {
253 ctx->x = AV_RB24(in);
255 } else if (data_size == 2) {
256 ctx->x = AV_RB16(in);
263 ctx->in_buf = in + nbytes;
264 ctx->data_size = data_size - nbytes;
266 ctx->j = ELS_JOTS_PER_BYTE;
268 ctx->diff = FFMIN(ELS_MAX - ctx->x,
269 ELS_MAX - els_exp_tab[ELS_JOTS_PER_BYTE * 4 - 1]);
272 void ff_els_decoder_uninit(ElsUnsignedRung *rung)
274 av_freep(&rung->rem_rung_list);
277 static int els_import_byte(ElsDecCtx *ctx)
279 if (!ctx->data_size) {
280 ctx->err = AVERROR_EOF;
283 ctx->x = (ctx->x << 8) | *ctx->in_buf++;
285 ctx->j += ELS_JOTS_PER_BYTE;
291 int ff_els_decode_bit(ElsDecCtx *ctx, uint8_t *rung)
294 const uint32_t *pAllowable = &els_exp_tab[ELS_JOTS_PER_BYTE * 3];
299 z = pAllowable[ctx->j + Ladder[*rung].ALps];
303 return *rung & 1; /* shortcut for x < t > pAllowable[j - 1] */
305 if (ctx->t > ctx->x) { /* decode most probable symbol (MPS) */
306 ctx->j += Ladder[*rung].AMps;
307 while (ctx->t > pAllowable[ctx->j])
310 if (ctx->j <= 0) { /* MPS: import one byte from bytestream. */
311 ret = els_import_byte(ctx);
318 *rung = Ladder[*rung].next0;
319 } else { /* decode less probable symbol (LPS) */
323 ctx->j += Ladder[*rung].ALps;
325 /* LPS: import one byte from bytestream. */
327 ret = els_import_byte(ctx);
331 /* LPS: import second byte from bytestream. */
333 ret = els_import_byte(ctx);
336 while (pAllowable[ctx->j - 1] >= z)
342 *rung = Ladder[*rung].next1;
345 ctx->diff = FFMIN(z - ctx->x, z - pAllowable[ctx->j - 1]);
350 unsigned ff_els_decode_unsigned(ElsDecCtx *ctx, ElsUnsignedRung *ur)
353 ElsRungNode *rung_node;
358 /* decode unary prefix */
359 for (n = 0; n < ELS_EXPGOLOMB_LEN + 1; n++)
360 if (ff_els_decode_bit(ctx, &ur->prefix_rung[n]))
363 /* handle the error/overflow case */
364 if (ctx->err || n >= ELS_EXPGOLOMB_LEN) {
365 ctx->err = AVERROR_INVALIDDATA;
369 /* handle the zero case */
373 /* initialize probability tree */
374 if (!ur->rem_rung_list) {
375 ur->rem_rung_list = av_realloc(NULL, RUNG_SPACE);
376 if (!ur->rem_rung_list) {
377 ctx->err = AVERROR(ENOMEM);
380 memset(ur->rem_rung_list, 0, RUNG_SPACE);
381 ur->rung_list_size = RUNG_SPACE;
382 ur->avail_index = ELS_EXPGOLOMB_LEN;
385 /* decode the remainder */
386 for (i = 0, r = 0, bit = 0; i < n; i++) {
388 rung_node = &ur->rem_rung_list[n];
390 if (!rung_node->next_index) {
391 if (ur->rung_list_size <= (ur->avail_index + 2) * sizeof(ElsRungNode)) {
392 // remember rung_node position
393 ptrdiff_t pos = rung_node - ur->rem_rung_list;
394 ctx->err = av_reallocp(&ur->rem_rung_list,
400 memset((uint8_t *) ur->rem_rung_list + ur->rung_list_size, 0,
402 ur->rung_list_size += RUNG_SPACE;
403 // restore rung_node position in the new list
404 rung_node = &ur->rem_rung_list[pos];
406 rung_node->next_index = ur->avail_index;
407 ur->avail_index += 2;
409 rung_node = &ur->rem_rung_list[rung_node->next_index + bit];
412 bit = ff_els_decode_bit(ctx, &rung_node->rung);
419 return (1 << n) - 1 + r; /* make value from exp golomb code */