Mercurial > libavcodec.hg
annotate rangecoder.h @ 5162:4394344397d8 libavcodec
include all prerequisites in header files
author | mru |
---|---|
date | Sat, 16 Jun 2007 22:59:13 +0000 |
parents | ce36118abbbb |
children | 9ecbfc0c82bf |
rev | line source |
---|---|
2334 | 1 /* |
2 * Range coder | |
3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
2334 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
2334 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
2334 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
2334 | 20 * |
21 */ | |
2967 | 22 |
2334 | 23 /** |
24 * @file rangecoder.h | |
25 * Range coder. | |
26 */ | |
27 | |
5162 | 28 #include <stdint.h> |
29 #include <assert.h> | |
30 #include "common.h" | |
31 | |
2334 | 32 typedef struct RangeCoder{ |
33 int low; | |
34 int range; | |
35 int outstanding_count; | |
36 int outstanding_byte; | |
37 uint8_t zero_state[256]; | |
38 uint8_t one_state[256]; | |
39 uint8_t *bytestream_start; | |
40 uint8_t *bytestream; | |
41 uint8_t *bytestream_end; | |
42 }RangeCoder; | |
43 | |
44 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size); | |
45 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size); | |
46 int ff_rac_terminate(RangeCoder *c); | |
47 void ff_build_rac_states(RangeCoder *c, int factor, int max_p); | |
48 | |
49 static inline void renorm_encoder(RangeCoder *c){ | |
50 //FIXME optimize | |
51 while(c->range < 0x100){ | |
52 if(c->outstanding_byte < 0){ | |
53 c->outstanding_byte= c->low>>8; | |
54 }else if(c->low <= 0xFF00){ | |
55 *c->bytestream++ = c->outstanding_byte; | |
56 for(;c->outstanding_count; c->outstanding_count--) | |
57 *c->bytestream++ = 0xFF; | |
58 c->outstanding_byte= c->low>>8; | |
59 }else if(c->low >= 0x10000){ | |
60 *c->bytestream++ = c->outstanding_byte + 1; | |
61 for(;c->outstanding_count; c->outstanding_count--) | |
62 *c->bytestream++ = 0x00; | |
63 c->outstanding_byte= (c->low>>8) & 0xFF; | |
64 }else{ | |
65 c->outstanding_count++; | |
66 } | |
2967 | 67 |
2334 | 68 c->low = (c->low & 0xFF)<<8; |
69 c->range <<= 8; | |
70 } | |
71 } | |
72 | |
5082 | 73 static inline int get_rac_count(RangeCoder *c){ |
74 int x= c->bytestream - c->bytestream_start + c->outstanding_count; | |
75 if(c->outstanding_byte >= 0) | |
76 x++; | |
77 return 8*x - av_log2(c->range); | |
78 } | |
79 | |
2334 | 80 static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){ |
81 int range1= (c->range * (*state)) >> 8; | |
82 | |
83 assert(*state); | |
84 assert(range1 < c->range); | |
85 assert(range1 > 0); | |
86 if(!bit){ | |
87 c->range -= range1; | |
88 *state= c->zero_state[*state]; | |
89 }else{ | |
90 c->low += c->range - range1; | |
91 c->range = range1; | |
92 *state= c->one_state[*state]; | |
93 } | |
2967 | 94 |
2334 | 95 renorm_encoder(c); |
96 } | |
97 | |
98 static inline void refill(RangeCoder *c){ | |
99 if(c->range < 0x100){ | |
100 c->range <<= 8; | |
101 c->low <<= 8; | |
102 if(c->bytestream < c->bytestream_end) | |
103 c->low+= c->bytestream[0]; | |
104 c->bytestream++; | |
105 } | |
106 } | |
107 | |
108 static inline int get_rac(RangeCoder *c, uint8_t * const state){ | |
109 int range1= (c->range * (*state)) >> 8; | |
5083
ce36118abbbb
rename attribute_unused to av_unused and moves its declaration to common.h
benoit
parents:
5082
diff
changeset
|
110 int av_unused one_mask; |
2967 | 111 |
2334 | 112 c->range -= range1; |
113 #if 1 | |
114 if(c->low < c->range){ | |
115 *state= c->zero_state[*state]; | |
116 refill(c); | |
117 return 0; | |
118 }else{ | |
119 c->low -= c->range; | |
120 *state= c->one_state[*state]; | |
121 c->range = range1; | |
122 refill(c); | |
123 return 1; | |
124 } | |
125 #else | |
126 one_mask= (c->range - c->low-1)>>31; | |
2967 | 127 |
2334 | 128 c->low -= c->range & one_mask; |
129 c->range += (range1 - c->range) & one_mask; | |
2967 | 130 |
2334 | 131 *state= c->zero_state[(*state) + (256&one_mask)]; |
2967 | 132 |
2334 | 133 refill(c); |
134 | |
135 return one_mask&1; | |
136 #endif | |
137 } | |
138 |