FFmpeg
Loading...
Searching...
No Matches
cabac_functions.h
Go to the documentation of this file.
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * Context Adaptive Binary Arithmetic Coder inline functions
25 */
26
27#ifndef AVCODEC_CABAC_FUNCTIONS_H
28#define AVCODEC_CABAC_FUNCTIONS_H
29
30#include <stddef.h>
31#include <stdint.h>
32
34#include "libavutil/intmath.h"
35#include "cabac.h"
36#include "config.h"
37
38#ifndef UNCHECKED_BITSTREAM_READER
39#define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
40#endif
41
42#if ARCH_AARCH64
43# include "aarch64/cabac.h"
44#endif
45#if ARCH_ARM
46# include "arm/cabac.h"
47#endif
48#if ARCH_X86
49# include "x86/cabac.h"
50#endif
51#if ARCH_MIPS
52# include "mips/cabac.h"
53#endif
54#if ARCH_LOONGARCH64
55# include "loongarch/cabac.h"
56#endif
57
62
63#if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
64static void refill(CABACContext *c){
65#if CABAC_BITS == 16
66 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67#else
68 c->low+= c->bytestream[0]<<1;
69#endif
70 c->low -= CABAC_MASK;
71#if !UNCHECKED_BITSTREAM_READER
72 if (c->bytestream < c->bytestream_end)
73#endif
74 c->bytestream += CABAC_BITS / 8;
75}
76#endif
77
78#ifndef get_cabac_terminate
80 int shift= (uint32_t)(c->range - 0x100)>>31;
81 c->range<<= shift;
82 c->low <<= shift;
83 if(!(c->low & CABAC_MASK))
84 refill(c);
85}
86#endif
87
88#ifndef get_cabac_inline
89static void refill2(CABACContext *c){
90 int i;
91 unsigned x;
92#if !HAVE_FAST_CLZ
93 x= c->low ^ (c->low-1);
94 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
95#else
96 i = ff_ctz(c->low) - CABAC_BITS;
97#endif
98
99 x= -CABAC_MASK;
100
101#if CABAC_BITS == 16
102 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
103#else
104 x+= c->bytestream[0]<<1;
105#endif
106
107 c->low += x<<i;
108#if !UNCHECKED_BITSTREAM_READER
109 if (c->bytestream < c->bytestream_end)
110#endif
111 c->bytestream += CABAC_BITS/8;
112}
113#endif
114
115#ifndef get_cabac_inline
117 int s = *state;
118 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
119 int bit, lps_mask;
120
121 c->range -= RangeLPS;
122 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
123
124 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
125 c->range += (RangeLPS - c->range) & lps_mask;
126
127 s^=lps_mask;
128 *state= (ff_h264_mlps_state+128)[s];
129 bit= s&1;
130
131 lps_mask= ff_h264_norm_shift[c->range];
132 c->range<<= lps_mask;
133 c->low <<= lps_mask;
134 if(!(c->low & CABAC_MASK))
135 refill2(c);
136 return bit;
137}
138#endif
139
141 return get_cabac_inline(c,state);
142}
143
144av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
145 return get_cabac_inline(c,state);
146}
147
148#ifndef get_cabac_bypass
150 int range;
151 c->low += c->low;
152
153 if(!(c->low & CABAC_MASK))
154 refill(c);
155
156 range= c->range<<(CABAC_BITS+1);
157 if(c->low < range){
158 return 0;
159 }else{
160 c->low -= range;
161 return 1;
162 }
163}
164#endif
165
166#ifndef get_cabac_bypass_sign
168 int range, mask;
169 c->low += c->low;
170
171 if(!(c->low & CABAC_MASK))
172 refill(c);
173
174 range= c->range<<(CABAC_BITS+1);
175 c->low -= range;
176 mask= c->low >> 31;
177 range &= mask;
178 c->low += range;
179 return (val^mask)-mask;
180}
181#endif
182
183/**
184 * @return the number of bytes read or 0 if no end
185 */
186#ifndef get_cabac_terminate
188 c->range -= 2;
189 if(c->low < c->range<<(CABAC_BITS+1)){
191 return 0;
192 }else{
193 return c->bytestream - c->bytestream_start;
194 }
195}
196#endif
197
198/**
199 * Skip @p n bytes and reset the decoder.
200 * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
201 */
202#ifndef skip_bytes
203av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
204 const uint8_t *ptr = c->bytestream;
205
206 if (c->low & 0x1)
207 ptr--;
208#if CABAC_BITS == 16
209 if (c->low & 0x1FF)
210 ptr--;
211#endif
212 if ((int) (c->bytestream_end - ptr) < n)
213 return NULL;
214 if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
215 return NULL;
216
217 return ptr;
218}
219#endif
220
221#endif /* AVCODEC_CABAC_FUNCTIONS_H */
static double val(void *priv, double ch)
Definition aeval.c:77
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition cabac.c:162
Context Adaptive Binary Arithmetic Coder.
#define CABAC_BITS
Definition cabac.h:38
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition cabac.h:36
#define CABAC_MASK
Definition cabac.h:39
#define H264_LPS_RANGE_OFFSET
Definition cabac.h:34
#define H264_MLPS_STATE_OFFSET
Definition cabac.h:35
#define H264_NORM_SHIFT_OFFSET
Definition cabac.h:33
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
static void refill(CABACContext *c)
static void refill2(CABACContext *c)
static void renorm_cabac_decoder_once(CABACContext *c)
static const uint8_t *const ff_h264_last_coeff_flag_offset_8x8
static const uint8_t *const ff_h264_norm_shift
static av_unused int get_cabac_terminate(CABACContext *c)
av_unused static av_noinline int get_cabac_noinline(CABACContext *c, uint8_t *const state)
static av_unused int get_cabac(CABACContext *c, uint8_t *const state)
static const uint8_t *const ff_h264_lps_range
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
static const uint8_t *const ff_h264_mlps_state
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static struct @346255127015250356166251341105367306144006377143 state
#define ff_ctz
Definition intmath.h:105
static int shift(int a, int b)
Definition bonk.c:261
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_noinline
Definition attributes.h:97
#define av_unused
Definition attributes.h:164
#define get_cabac_bypass
Definition cabac.h:149
#define get_cabac_bypass_sign
Definition cabac.h:193
#define get_cabac_inline
Definition cabac.h:120
static const uint16_t mask[17]
Definition lzw.c:38
enum AVColorRange range
static double c[64]