Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
33 | #include "libavutil/attributes.h" | ||
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 | |||
58 | static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET; | ||
59 | static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET; | ||
60 | static const uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET; | ||
61 | static const uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET; | ||
62 | |||
63 | #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate) | ||
64 | 20345 | static void refill(CABACContext *c){ | |
65 | #if CABAC_BITS == 16 | ||
66 | 20345 | c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); | |
67 | #else | ||
68 | c->low+= c->bytestream[0]<<1; | ||
69 | #endif | ||
70 | 20345 | c->low -= CABAC_MASK; | |
71 | #if !UNCHECKED_BITSTREAM_READER | ||
72 |
2/2✓ Branch 0 taken 18075 times.
✓ Branch 1 taken 47 times.
|
18122 | if (c->bytestream < c->bytestream_end) |
73 | #endif | ||
74 | 20298 | c->bytestream += CABAC_BITS / 8; | |
75 | 20345 | } | |
76 | #endif | ||
77 | |||
78 | #ifndef get_cabac_terminate | ||
79 | 10065039 | static inline void renorm_cabac_decoder_once(CABACContext *c){ | |
80 | 10065039 | int shift= (uint32_t)(c->range - 0x100)>>31; | |
81 | 10065039 | c->range<<= shift; | |
82 | 10065039 | c->low <<= shift; | |
83 |
2/2✓ Branch 0 taken 20345 times.
✓ Branch 1 taken 10044694 times.
|
10065039 | if(!(c->low & CABAC_MASK)) |
84 | 20345 | refill(c); | |
85 | 10065039 | } | |
86 | #endif | ||
87 | |||
88 | #ifndef get_cabac_inline | ||
89 | static 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 | ||
116 | static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){ | ||
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 | |||
140 | 90569746 | static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){ | |
141 | 90569746 | return get_cabac_inline(c,state); | |
142 | } | ||
143 | |||
144 | 539402324 | static int av_unused get_cabac(CABACContext *c, uint8_t * const state){ | |
145 | 539402324 | return get_cabac_inline(c,state); | |
146 | } | ||
147 | |||
148 | #ifndef get_cabac_bypass | ||
149 | static int av_unused get_cabac_bypass(CABACContext *c){ | ||
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 | ||
167 | static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){ | ||
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 | ||
187 | 10093358 | static int av_unused get_cabac_terminate(CABACContext *c){ | |
188 | 10093358 | c->range -= 2; | |
189 |
2/2✓ Branch 0 taken 10065039 times.
✓ Branch 1 taken 28319 times.
|
10093358 | if(c->low < c->range<<(CABAC_BITS+1)){ |
190 | 10065039 | renorm_cabac_decoder_once(c); | |
191 | 10065039 | return 0; | |
192 | }else{ | ||
193 | 28319 | 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 | ||
203 | ✗ | static av_unused 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 */ | ||
222 |