2 * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3 * Copyright (C) 2009 David Conrad
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
24 * Arithmetic decoder for Dirac
25 * @author Marco Gerards <marco@gnu.org>
28 #ifndef AVCODEC_DIRAC_ARITH_H
29 #define AVCODEC_DIRAC_ARITH_H
31 #include "libavutil/x86/asm.h"
32 #include "bytestream.h"
35 enum dirac_arith_contexts {
62 // Dirac resets the arith decoder between decoding various types of data,
63 // so many contexts are never used simultaneously. Thus, we can reduce
64 // the number of contexts needed by reusing them.
65 #define CTX_SB_F1 CTX_ZP_F5
67 #define CTX_PMODE_REF1 0
68 #define CTX_PMODE_REF2 1
69 #define CTX_GLOBAL_BLOCK 2
70 #define CTX_MV_F1 CTX_ZP_F2
72 #define CTX_DC_F1 CTX_ZP_F5
80 const uint8_t *bytestream;
81 const uint8_t *bytestream_end;
83 uint16_t contexts[DIRAC_CTX_COUNT];
88 extern const uint8_t ff_dirac_next_ctx[DIRAC_CTX_COUNT];
89 extern int16_t ff_dirac_prob_branchless[256][2];
91 static inline void renorm(DiracArith *c)
94 int shift = 14 - av_log2_16bit(c->range-1) + ((c->range-1)>>15);
100 while (c->range <= 0x4000) {
108 static inline void refill(DiracArith *c)
110 int counter = c->counter;
113 int new = bytestream_get_be16(&c->bytestream);
115 // the spec defines overread bits to be 1, and streams rely on this
116 if (c->bytestream > c->bytestream_end) {
118 if (c->bytestream > c->bytestream_end+1)
121 c->bytestream = c->bytestream_end;
124 c->error = AVERROR_INVALIDDATA;
127 c->low += new << counter;
130 c->counter = counter;
133 static inline int dirac_get_arith_bit(DiracArith *c, int ctx)
135 int prob_zero = c->contexts[ctx];
136 int range_times_prob, bit;
137 unsigned low = c->low;
138 int range = c->range;
140 range_times_prob = (c->range * prob_zero) >> 16;
142 #if ARCH_X86 && HAVE_FAST_CMOV && HAVE_INLINE_ASM && HAVE_6REGS
143 low -= range_times_prob << 16;
144 range -= range_times_prob;
151 : "+q"(bit), "+r"(range), "+r"(low)
152 : "r"(c->low), "r"(c->low>>16),
153 "r"(range_times_prob)
156 bit = (low >> 16) >= range_times_prob;
158 low -= range_times_prob << 16;
159 range -= range_times_prob;
161 range = range_times_prob;
165 c->contexts[ctx] += ff_dirac_prob_branchless[prob_zero>>8][bit];
174 static inline int dirac_get_arith_uint(DiracArith *c, int follow_ctx, int data_ctx)
177 while (!dirac_get_arith_bit(c, follow_ctx)) {
178 if (ret >= 0x40000000) {
179 av_log(NULL, AV_LOG_ERROR, "dirac_get_arith_uint overflow\n");
180 c->error = AVERROR_INVALIDDATA;
184 ret += dirac_get_arith_bit(c, data_ctx);
185 follow_ctx = ff_dirac_next_ctx[follow_ctx];
190 static inline int dirac_get_arith_int(DiracArith *c, int follow_ctx, int data_ctx)
192 int ret = dirac_get_arith_uint(c, follow_ctx, data_ctx);
193 if (ret && dirac_get_arith_bit(c, data_ctx+1))
198 void ff_dirac_init_arith_tables(void);
199 void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length);
201 #endif /* AVCODEC_DIRAC_ARITH_H */