]> git.sesse.net Git - ffmpeg/blob - libavcodec/cabac_functions.h
roqaudioenc: return AVERROR codes instead of -1
[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 const uint8_t ff_h264_norm_shift[512];
40 extern uint8_t ff_h264_mlps_state[4*64];
41 extern uint8_t ff_h264_lps_range[4*2*64];  ///< rangeTabLPS
42
43 static void refill(CABACContext *c){
44 #if CABAC_BITS == 16
45         c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
46 #else
47         c->low+= c->bytestream[0]<<1;
48 #endif
49     c->low -= CABAC_MASK;
50     c->bytestream+= CABAC_BITS/8;
51 }
52
53 static inline void renorm_cabac_decoder_once(CABACContext *c){
54     int shift= (uint32_t)(c->range - 0x100)>>31;
55     c->range<<= shift;
56     c->low  <<= shift;
57     if(!(c->low & CABAC_MASK))
58         refill(c);
59 }
60
61 #ifndef get_cabac_inline
62 static void refill2(CABACContext *c){
63     int i, x;
64
65     x= c->low ^ (c->low-1);
66     i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
67
68     x= -CABAC_MASK;
69
70 #if CABAC_BITS == 16
71         x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
72 #else
73         x+= c->bytestream[0]<<1;
74 #endif
75
76     c->low += x<<i;
77     c->bytestream+= CABAC_BITS/8;
78 }
79
80 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
81     int s = *state;
82     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
83     int bit, lps_mask;
84
85     c->range -= RangeLPS;
86     lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
87
88     c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
89     c->range += (RangeLPS - c->range) & lps_mask;
90
91     s^=lps_mask;
92     *state= (ff_h264_mlps_state+128)[s];
93     bit= s&1;
94
95     lps_mask= ff_h264_norm_shift[c->range];
96     c->range<<= lps_mask;
97     c->low  <<= lps_mask;
98     if(!(c->low & CABAC_MASK))
99         refill2(c);
100     return bit;
101 }
102 #endif
103
104 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
105     return get_cabac_inline(c,state);
106 }
107
108 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
109     return get_cabac_inline(c,state);
110 }
111
112 static int av_unused get_cabac_bypass(CABACContext *c){
113     int range;
114     c->low += c->low;
115
116     if(!(c->low & CABAC_MASK))
117         refill(c);
118
119     range= c->range<<(CABAC_BITS+1);
120     if(c->low < range){
121         return 0;
122     }else{
123         c->low -= range;
124         return 1;
125     }
126 }
127
128
129 #ifndef get_cabac_bypass_sign
130 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
131     int range, mask;
132     c->low += c->low;
133
134     if(!(c->low & CABAC_MASK))
135         refill(c);
136
137     range= c->range<<(CABAC_BITS+1);
138     c->low -= range;
139     mask= c->low >> 31;
140     range &= mask;
141     c->low += range;
142     return (val^mask)-mask;
143 }
144 #endif
145
146 /**
147  *
148  * @return the number of bytes read or 0 if no end
149  */
150 static int av_unused get_cabac_terminate(CABACContext *c){
151     c->range -= 2;
152     if(c->low < c->range<<(CABAC_BITS+1)){
153         renorm_cabac_decoder_once(c);
154         return 0;
155     }else{
156         return c->bytestream - c->bytestream_start;
157     }
158 }
159
160 #endif /* AVCODEC_CABAC_FUNCTIONS_H */