2 * Copyright (C) 2016 Open Broadcast Systems Ltd.
3 * Author 2016 Rostislav Pehlivanov <rpehlivanov@obe.tv>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "dirac_vlc.h"
24 #define LUT_SIZE (1 << LUT_BITS)
25 #define RSIZE_BITS (CHAR_BIT*sizeof(residual))
27 #define CONVERT_TO_RESIDUE(a, b) \
28 (((residual)(a)) << (RSIZE_BITS - (b)))
30 #define INIT_RESIDUE(N, I, B) \
31 residual N = B ? CONVERT_TO_RESIDUE(I, B) : 0; \
32 av_unused int32_t N ## _bits = B
34 int ff_dirac_golomb_read_32bit(DiracGolombLUT *lut_ctx, const uint8_t *buf,
35 int bytes, uint8_t *_dst, int coeffs)
38 int32_t *dst = (int32_t *)_dst;
39 DiracGolombLUT *future[4], *l = &lut_ctx[2*LUT_SIZE + buf[0]];
40 INIT_RESIDUE(res, 0, 0);
42 #define APPEND_RESIDUE(N, M) \
43 N |= M >> (N ## _bits); \
44 N ## _bits += (M ## _bits)
46 for (b = 1; b <= bytes; b++) {
47 future[0] = &lut_ctx[buf[b]];
48 future[1] = future[0] + 1*LUT_SIZE;
49 future[2] = future[0] + 2*LUT_SIZE;
50 future[3] = future[0] + 3*LUT_SIZE;
52 if ((c_idx + 1) > coeffs)
55 /* res_bits is a hint for better branch prediction */
56 if (res_bits && l->sign) {
58 APPEND_RESIDUE(res, l->preamble);
59 for (i = 0; i < (res_bits >> 1) - 1; i++) {
61 coeff |= (res >> (RSIZE_BITS - 2*i - 2)) & 1;
63 dst[c_idx++] = l->sign * (coeff - 1);
67 memcpy(&dst[c_idx], l->ready, LUT_BITS*sizeof(int32_t));
68 c_idx += l->ready_num;
70 APPEND_RESIDUE(res, l->leftover);
72 l = future[l->need_s ? 3 : !res_bits ? 2 : res_bits & 1];
78 int ff_dirac_golomb_read_16bit(DiracGolombLUT *lut_ctx, const uint8_t *buf,
79 int bytes, uint8_t *_dst, int coeffs)
82 int16_t *dst = (int16_t *)_dst;
83 DiracGolombLUT *future[4], *l = &lut_ctx[2*LUT_SIZE + buf[0]];
84 INIT_RESIDUE(res, 0, 0);
86 #define APPEND_RESIDUE(N, M) \
87 N |= M >> (N ## _bits); \
88 N ## _bits += (M ## _bits)
90 for (b = 1; b <= bytes; b++) {
91 future[0] = &lut_ctx[buf[b]];
92 future[1] = future[0] + 1*LUT_SIZE;
93 future[2] = future[0] + 2*LUT_SIZE;
94 future[3] = future[0] + 3*LUT_SIZE;
96 if ((c_idx + 1) > coeffs)
99 if (res_bits && l->sign) {
101 APPEND_RESIDUE(res, l->preamble);
102 for (i = 0; i < (res_bits >> 1) - 1; i++) {
104 coeff |= (res >> (RSIZE_BITS - 2*i - 2)) & 1;
106 dst[c_idx++] = l->sign * (coeff - 1);
110 for (i = 0; i < LUT_BITS; i++)
111 dst[c_idx + i] = l->ready[i];
112 c_idx += l->ready_num;
114 APPEND_RESIDUE(res, l->leftover);
116 l = future[l->need_s ? 3 : !res_bits ? 2 : res_bits & 1];
122 /* Searches for golomb codes in a residue */
123 static inline void search_for_golomb(DiracGolombLUT *l, residual r, int bits)
125 int r_count = RSIZE_BITS - 1;
126 int bits_start, bits_tot = bits, need_sign = 0;
128 #define READ_BIT(N) (((N) >> (N ## _count--)) & 1)
132 bits_start = (RSIZE_BITS - 1) - r_count;
147 l->ready[l->ready_num] = coef - 1;
148 if (l->ready[l->ready_num]) {
153 l->ready[l->ready_num] *= READ_BIT(r) ? -1 : +1;
162 l->leftover = r << bits_start;
163 l->leftover_bits = bits_tot - bits_start;
164 l->need_s = need_sign;
167 /* Parity LUTs - even and odd bit end positions */
168 static void generate_parity_lut(DiracGolombLUT *lut, int even)
171 for (idx = 0; idx < LUT_SIZE; idx++) {
172 DiracGolombLUT *l = &lut[idx];
173 int symbol_end_loc = -1;
177 INIT_RESIDUE(res, idx, LUT_BITS);
179 for (i = 0; i < LUT_BITS; i++) {
180 const int cond = even ? (i & 1) : !(i & 1);
181 if (((res >> (RSIZE_BITS - i - 1)) & 1) && cond) {
182 symbol_end_loc = i + 2;
187 if (symbol_end_loc < 0 || symbol_end_loc > LUT_BITS) {
189 l->preamble_bits = 0;
190 l->leftover_bits = LUT_BITS;
191 l->leftover = CONVERT_TO_RESIDUE(idx, l->leftover_bits);
197 /* Gets bits 0 through to (symbol_end_loc - 1) inclusive */
198 code = idx >> ((LUT_BITS - 1) - (symbol_end_loc - 1));
199 code &= ((1 << LUT_BITS) - 1) >> (LUT_BITS - symbol_end_loc);
200 l->preamble_bits = symbol_end_loc;
201 l->preamble = CONVERT_TO_RESIDUE(code, l->preamble_bits);
202 l->sign = ((l->preamble >> (RSIZE_BITS - l->preamble_bits)) & 1) ? -1 : +1;
204 search_for_golomb(l, res << symbol_end_loc, LUT_BITS - symbol_end_loc);
208 /* Reset (off == 0) and needs-one-more-bit (off == 1) LUTs */
209 static void generate_offset_lut(DiracGolombLUT *lut, int off)
212 for (idx = 0; idx < LUT_SIZE; idx++) {
213 DiracGolombLUT *l = &lut[idx];
215 INIT_RESIDUE(res, idx, LUT_BITS);
217 l->preamble = CONVERT_TO_RESIDUE(res >> (RSIZE_BITS - off), off);
218 l->preamble_bits = off;
219 l->sign = ((l->preamble >> (RSIZE_BITS - l->preamble_bits)) & 1) ? -1 : +1;
221 search_for_golomb(l, res << off, LUT_BITS - off);
225 av_cold int ff_dirac_golomb_reader_init(DiracGolombLUT **lut_ctx)
229 if (!(lut = av_calloc(4*LUT_SIZE, sizeof(DiracGolombLUT))))
230 return AVERROR(ENOMEM);
232 generate_parity_lut(&lut[0*LUT_SIZE], 0);
233 generate_parity_lut(&lut[1*LUT_SIZE], 1);
234 generate_offset_lut(&lut[2*LUT_SIZE], 0);
235 generate_offset_lut(&lut[3*LUT_SIZE], 1);
242 av_cold void ff_dirac_golomb_reader_end(DiracGolombLUT **lut_ctx)