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 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
66 c->low -= CABAC_MASK;
67#if !UNCHECKED_BITSTREAM_READER
68 if (c->bytestream < c->bytestream_end)
69#endif
70 c->bytestream += CABAC_BITS / 8;
71}
72#endif
73
74#ifndef get_cabac_terminate
76 int shift= (uint32_t)(c->range - 0x100)>>31;
77 c->range<<= shift;
78 c->low <<= shift;
79 if(!(c->low & CABAC_MASK))
80 refill(c);
81}
82#endif
83
84#ifndef get_cabac_inline
85static void refill2(CABACContext *c){
86 int i;
87 unsigned x;
88#if !HAVE_FAST_CLZ
89 x= c->low ^ (c->low-1);
90 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
91#else
92 i = ff_ctz(c->low) - CABAC_BITS;
93#endif
94
95 x= -CABAC_MASK;
96
97 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
98
99 c->low += x<<i;
100#if !UNCHECKED_BITSTREAM_READER
101 if (c->bytestream < c->bytestream_end)
102#endif
103 c->bytestream += CABAC_BITS/8;
104}
105#endif
106
107#ifndef get_cabac_inline
109 int s = *state;
110 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
111 int bit, lps_mask;
112
113 c->range -= RangeLPS;
114 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
115
116 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
117 c->range += (RangeLPS - c->range) & lps_mask;
118
119 s^=lps_mask;
120 *state= (ff_h264_mlps_state+128)[s];
121 bit= s&1;
122
123 lps_mask= ff_h264_norm_shift[c->range];
124 c->range<<= lps_mask;
125 c->low <<= lps_mask;
126 if(!(c->low & CABAC_MASK))
127 refill2(c);
128 return bit;
129}
130#endif
131
133 return get_cabac_inline(c,state);
134}
135
136av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
137 return get_cabac_inline(c,state);
138}
139
140#ifndef get_cabac_bypass
142 int range;
143 c->low += c->low;
144
145 if(!(c->low & CABAC_MASK))
146 refill(c);
147
148 range= c->range<<(CABAC_BITS+1);
149 if(c->low < range){
150 return 0;
151 }else{
152 c->low -= range;
153 return 1;
154 }
155}
156#endif
157
158#ifndef get_cabac_bypass_sign
160 int range, mask;
161 c->low += c->low;
162
163 if(!(c->low & CABAC_MASK))
164 refill(c);
165
166 range= c->range<<(CABAC_BITS+1);
167 c->low -= range;
168 mask= c->low >> 31;
169 range &= mask;
170 c->low += range;
171 return (val^mask)-mask;
172}
173#endif
174
175/**
176 * @return the number of bytes read or 0 if no end
177 */
178#ifndef get_cabac_terminate
180 c->range -= 2;
181 if(c->low < c->range<<(CABAC_BITS+1)){
183 return 0;
184 }else{
185 return c->bytestream - c->bytestream_start;
186 }
187}
188#endif
189
190/**
191 * Skip @p n bytes and reset the decoder.
192 * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
193 */
194#ifndef skip_bytes
195av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
196 const uint8_t *ptr = c->bytestream;
197
198 if (c->low & 0x1)
199 ptr--;
200 if (c->low & 0x1FF)
201 ptr--;
202 if ((int) (c->bytestream_end - ptr) < n)
203 return NULL;
204 if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
205 return NULL;
206
207 return ptr;
208}
209#endif
210
211#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
#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 struct @232240247175332074307271266321204261331170211232 state
static double c[64]