Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6147 | serge | 1 | /* |
2 | * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder |
||
3 | * Copyright (c) 2003 Michael Niedermayer |
||
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 |
||
31 | |||
32 | #include "cabac.h" |
||
33 | #include "config.h" |
||
34 | |||
35 | #ifndef UNCHECKED_BITSTREAM_READER |
||
36 | #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER |
||
37 | #endif |
||
38 | |||
39 | #if ARCH_AARCH64 |
||
40 | # include "aarch64/cabac.h" |
||
41 | #endif |
||
42 | #if ARCH_ARM |
||
43 | # include "arm/cabac.h" |
||
44 | #endif |
||
45 | #if ARCH_X86 |
||
46 | # include "x86/cabac.h" |
||
47 | #endif |
||
48 | |||
49 | static CABAC_TABLE_CONST uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET; |
||
50 | static CABAC_TABLE_CONST uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET; |
||
51 | static CABAC_TABLE_CONST uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET; |
||
52 | static CABAC_TABLE_CONST uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET; |
||
53 | |||
54 | static void refill(CABACContext *c){ |
||
55 | #if CABAC_BITS == 16 |
||
56 | c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); |
||
57 | #else |
||
58 | c->low+= c->bytestream[0]<<1; |
||
59 | #endif |
||
60 | c->low -= CABAC_MASK; |
||
61 | #if !UNCHECKED_BITSTREAM_READER |
||
62 | if (c->bytestream < c->bytestream_end) |
||
63 | #endif |
||
64 | c->bytestream += CABAC_BITS / 8; |
||
65 | } |
||
66 | |||
67 | static inline void renorm_cabac_decoder_once(CABACContext *c){ |
||
68 | int shift= (uint32_t)(c->range - 0x100)>>31; |
||
69 | c->range<<= shift; |
||
70 | c->low <<= shift; |
||
71 | if(!(c->low & CABAC_MASK)) |
||
72 | refill(c); |
||
73 | } |
||
74 | |||
75 | #ifndef get_cabac_inline |
||
76 | static void refill2(CABACContext *c){ |
||
77 | int i; |
||
78 | unsigned x; |
||
79 | |||
80 | x= c->low ^ (c->low-1); |
||
81 | i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)]; |
||
82 | |||
83 | x= -CABAC_MASK; |
||
84 | |||
85 | #if CABAC_BITS == 16 |
||
86 | x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); |
||
87 | #else |
||
88 | x+= c->bytestream[0]<<1; |
||
89 | #endif |
||
90 | |||
91 | c->low += x< |
||
92 | #if !UNCHECKED_BITSTREAM_READER |
||
93 | if (c->bytestream < c->bytestream_end) |
||
94 | #endif |
||
95 | c->bytestream += CABAC_BITS/8; |
||
96 | } |
||
97 | |||
98 | static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){ |
||
99 | int s = *state; |
||
100 | int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s]; |
||
101 | int bit, lps_mask; |
||
102 | |||
103 | c->range -= RangeLPS; |
||
104 | lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31; |
||
105 | |||
106 | c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask; |
||
107 | c->range += (RangeLPS - c->range) & lps_mask; |
||
108 | |||
109 | s^=lps_mask; |
||
110 | *state= (ff_h264_mlps_state+128)[s]; |
||
111 | bit= s&1; |
||
112 | |||
113 | lps_mask= ff_h264_norm_shift[c->range]; |
||
114 | c->range<<= lps_mask; |
||
115 | c->low <<= lps_mask; |
||
116 | if(!(c->low & CABAC_MASK)) |
||
117 | refill2(c); |
||
118 | return bit; |
||
119 | } |
||
120 | #endif |
||
121 | |||
122 | static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){ |
||
123 | return get_cabac_inline(c,state); |
||
124 | } |
||
125 | |||
126 | static int av_unused get_cabac(CABACContext *c, uint8_t * const state){ |
||
127 | return get_cabac_inline(c,state); |
||
128 | } |
||
129 | |||
130 | #ifndef get_cabac_bypass |
||
131 | static int av_unused get_cabac_bypass(CABACContext *c){ |
||
132 | int range; |
||
133 | c->low += c->low; |
||
134 | |||
135 | if(!(c->low & CABAC_MASK)) |
||
136 | refill(c); |
||
137 | |||
138 | range= c->range<<(CABAC_BITS+1); |
||
139 | if(c->low < range){ |
||
140 | return 0; |
||
141 | }else{ |
||
142 | c->low -= range; |
||
143 | return 1; |
||
144 | } |
||
145 | } |
||
146 | #endif |
||
147 | |||
148 | #ifndef get_cabac_bypass_sign |
||
149 | static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){ |
||
150 | int range, mask; |
||
151 | c->low += c->low; |
||
152 | |||
153 | if(!(c->low & CABAC_MASK)) |
||
154 | refill(c); |
||
155 | |||
156 | range= c->range<<(CABAC_BITS+1); |
||
157 | c->low -= range; |
||
158 | mask= c->low >> 31; |
||
159 | range &= mask; |
||
160 | c->low += range; |
||
161 | return (val^mask)-mask; |
||
162 | } |
||
163 | #endif |
||
164 | |||
165 | /** |
||
166 | * |
||
167 | * @return the number of bytes read or 0 if no end |
||
168 | */ |
||
169 | static int av_unused get_cabac_terminate(CABACContext *c){ |
||
170 | c->range -= 2; |
||
171 | if(c->low < c->range<<(CABAC_BITS+1)){ |
||
172 | renorm_cabac_decoder_once(c); |
||
173 | return 0; |
||
174 | }else{ |
||
175 | return c->bytestream - c->bytestream_start; |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Skip @p n bytes and reset the decoder. |
||
181 | * @return the address of the first skipped byte or NULL if there's less than @p n bytes left |
||
182 | */ |
||
183 | static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) { |
||
184 | const uint8_t *ptr = c->bytestream; |
||
185 | |||
186 | if (c->low & 0x1) |
||
187 | ptr--; |
||
188 | #if CABAC_BITS == 16 |
||
189 | if (c->low & 0x1FF) |
||
190 | ptr--; |
||
191 | #endif |
||
192 | if ((int) (c->bytestream_end - ptr) < n) |
||
193 | return NULL; |
||
194 | if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0) |
||
195 | return NULL; |
||
196 | |||
197 | return ptr; |
||
198 | } |
||
199 | |||
200 | #endif /* AVCODEC_CABAC_FUNCTIONS_H */>>(CABAC_BITS+1)){ |