Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
view libavcodec/cell/cabac_spu.h @ 2:897f711a7157
rearrange to work with autoconf
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Tue, 25 Sep 2012 15:55:33 +0200 |
| parents | |
| children |
line 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 */
22 /**
23 * @file
24 * Context Adaptive Binary Arithmetic Coder.
25 */
27 #ifndef AVCODEC_CABAC_H
28 #define AVCODEC_CABAC_H
30 //#undef NDEBUG
31 #include <assert.h>
32 #include "h264_dma.h"
33 #include "libavutil/x86_cpu.h"
34 #include "libavutil/attributes.h"
36 #define CABAC_BITS 16
37 #define CABAC_MASK ((1<<CABAC_BITS)-1)
38 #define BRANCHLESS_CABAC_DECODER 1
40 typedef struct CABACContext{
41 int low;
42 int range;
43 int outstanding_count;
44 #ifdef STRICT_LIMITS
45 int symCount;
46 #endif
47 const uint8_t *bytestream_ea_start;
48 const uint8_t *bytestream_ea;
49 const uint8_t *bytestream_ea_end;
50 int slot;
51 int bufsize;
53 uint8_t *bytestream_start;
54 uint8_t *bytestream;
55 uint8_t *bytestream_end;
56 uint8_t cabac_state[460];
57 }CABACContext;
59 extern uint8_t ff_h264_mlps_state[4*64];
60 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
61 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
62 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
63 extern const uint8_t ff_h264_norm_shift[512];
65 void ff_init_cabac_states(void);
67 extern DECLARE_ALIGNED(128,uint8_t, bytestream_ls[4096]);
68 extern int bytecount;
69 static inline void dma_cabac(CABACContext *c){
70 bytecount++;
71 if (c->bytestream == c->bytestream_end){
72 if (c->bufsize>0){
73 int size = (c->bufsize > sizeof(bytestream_ls)) ? sizeof(bytestream_ls) : c->bufsize;
74 int align = size &0xF;
75 int dma_size = size + (align? 16-align : 0);
77 spu_dma_get(bytestream_ls, (unsigned) c->bytestream_ea, dma_size, ED_raw);
78 wait_dma_id(ED_raw);
79 c->bytestream = bytestream_ls;
80 c->bytestream_end = &bytestream_ls[size];
81 c->bytestream_ea += dma_size;
82 c->bufsize -= size;
83 }
84 bytecount =0;
85 }else if((unsigned)c->bytestream > (unsigned)c->bytestream_end +2){
86 //fprintf(stderr, "Read beyond end of frame %d\n", c->bufsize);
87 bytecount =0;
88 }
89 }
91 static void refill(CABACContext *c){
92 dma_cabac(c);
94 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
96 c->low -= CABAC_MASK;
97 c->bytestream+= CABAC_BITS/8;
98 }
100 static void refill2(CABACContext *c){
101 int i, x;
103 dma_cabac(c);
105 x= c->low ^ (c->low-1);
106 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
108 x= -CABAC_MASK;
110 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
112 c->low += x<<i;
113 c->bytestream+= CABAC_BITS/8;
114 }
116 static inline void renorm_cabac_decoder(CABACContext *c){
117 while(c->range < 0x100){
118 c->range+= c->range;
119 c->low+= c->low;
120 if(!(c->low & CABAC_MASK))
121 refill(c);
122 }
123 }
125 static inline void renorm_cabac_decoder_once(CABACContext *c){
127 int shift= (uint32_t)(c->range - 0x100)>>31;
128 c->range<<= shift;
129 c->low <<= shift;
131 if(!(c->low & CABAC_MASK))
132 refill(c);
133 }
135 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
137 int s = *state;
138 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
139 int bit, lps_mask av_unused;
141 c->range -= RangeLPS;
142 #ifndef BRANCHLESS_CABAC_DECODER
143 if(c->low < (c->range<<(CABAC_BITS+1))){
144 bit= s&1;
145 *state= ff_h264_mps_state[s];
146 renorm_cabac_decoder_once(c);
147 }else{
148 bit= ff_h264_norm_shift[RangeLPS];
149 c->low -= (c->range<<(CABAC_BITS+1));
150 *state= ff_h264_lps_state[s];
151 c->range = RangeLPS<<bit;
152 c->low <<= bit;
153 bit= (s&1)^1;
155 if(!(c->low & CABAC_MASK)){
156 refill2(c);
157 }
158 }
159 #else /* BRANCHLESS_CABAC_DECODER */
160 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
162 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
163 c->range += (RangeLPS - c->range) & lps_mask;
165 s^=lps_mask;
166 *state= (ff_h264_mlps_state+128)[s];
167 bit= s&1;
169 lps_mask= ff_h264_norm_shift[c->range];
170 c->range<<= lps_mask;
171 c->low <<= lps_mask;
172 if(!(c->low & CABAC_MASK))
173 refill2(c);
174 #endif /* BRANCHLESS_CABAC_DECODER */
176 return bit;
177 }
179 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
180 return get_cabac_inline(c, state);
181 }
183 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
184 return get_cabac_inline(c, state);
185 }
187 static int av_unused get_cabac_bypass(CABACContext *c){
189 int range;
190 c->low += c->low;
192 if(!(c->low & CABAC_MASK))
193 refill(c);
195 range= c->range<<(CABAC_BITS+1);
196 if(c->low < range){
197 return 0;
198 }else{
199 c->low -= range;
200 return 1;
201 }
202 }
204 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
205 int range, mask;
206 c->low += c->low;
208 if(!(c->low & CABAC_MASK))
209 refill(c);
211 range= c->range<<(CABAC_BITS+1);
212 c->low -= range;
213 mask= c->low >> 31;
214 range &= mask;
215 c->low += range;
216 return (val^mask)-mask;
217 }
219 /**
220 *
221 * @return the number of bytes read or 0 if no end
222 */
223 static int av_unused get_cabac_terminate(CABACContext *c){
224 c->range -= 2;
225 if(c->low < c->range<<(CABAC_BITS+1)){
226 renorm_cabac_decoder_once(c);
227 return 0;
228 }else{
229 return c->bytestream - c->bytestream_start;
230 }
231 }
233 #endif /* AVCODEC_CABAC_H */
