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