Mercurial > libavcodec.hg
annotate rangecoder.c @ 2589:a5a62827f195 libavcodec
Snow Slicing patch by (Yartrebo) yartrebo earthlink net
author | michael |
---|---|
date | Sun, 03 Apr 2005 15:43:57 +0000 |
parents | e25782262d7d |
children | 95bac7109ff0 |
rev | line source |
---|---|
2334 | 1 /* |
2 * Range coder | |
3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 * | |
19 */ | |
20 | |
21 /** | |
22 * @file rangecoder.c | |
23 * Range coder. | |
24 * based upon | |
25 * "Range encoding: an algorithm for removing redundancy from a digitised | |
26 * message. | |
27 * G. N. N. Martin Presented in March 1979 to the Video & | |
28 * Data Recording Conference, | |
29 * IBM UK Scientific Center held in Southampton July 24-27 1979." | |
30 * | |
31 */ | |
32 | |
33 #include <string.h> | |
34 | |
35 #include "avcodec.h" | |
36 #include "common.h" | |
37 #include "rangecoder.h" | |
38 | |
39 | |
40 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){ | |
41 c->bytestream_start= | |
42 c->bytestream= buf; | |
43 c->bytestream_end= buf + buf_size; | |
44 | |
45 c->low= 0; | |
46 c->range= 0xFF00; | |
47 c->outstanding_count= 0; | |
48 c->outstanding_byte= -1; | |
49 } | |
50 | |
51 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){ | |
52 ff_init_range_encoder(c, buf, buf_size); | |
53 | |
54 c->low =(*c->bytestream++)<<8; | |
55 c->low+= *c->bytestream++; | |
56 } | |
57 | |
58 void ff_build_rac_states(RangeCoder *c, int factor, int max_p){ | |
59 const int64_t one= 1LL<<32; | |
60 int64_t p; | |
2522
e25782262d7d
kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents:
2334
diff
changeset
|
61 int last_p8, p8, i; |
2334 | 62 |
63 memset(c->zero_state, 0, sizeof(c->zero_state)); | |
64 memset(c-> one_state, 0, sizeof(c-> one_state)); | |
65 | |
66 #if 0 | |
67 for(i=1; i<256; i++){ | |
68 if(c->one_state[i]) | |
69 continue; | |
70 | |
71 p= (i*one + 128) >> 8; | |
72 last_p8= i; | |
73 for(;;){ | |
74 p+= ((one-p)*factor + one/2) >> 32; | |
75 p8= (256*p + one/2) >> 32; //FIXME try without the one | |
76 if(p8 <= last_p8) p8= last_p8+1; | |
77 if(p8 > max_p) p8= max_p; | |
78 if(p8 < last_p8) | |
79 break; | |
80 c->one_state[last_p8]= p8; | |
81 if(p8 == last_p8) | |
82 break; | |
83 last_p8= p8; | |
84 } | |
85 } | |
86 #endif | |
87 #if 1 | |
88 last_p8= 0; | |
89 p= one/2; | |
90 for(i=0; i<128; i++){ | |
91 p8= (256*p + one/2) >> 32; //FIXME try without the one | |
92 if(p8 <= last_p8) p8= last_p8+1; | |
93 if(last_p8 && last_p8<256 && p8<=max_p) | |
94 c->one_state[last_p8]= p8; | |
95 | |
96 p+= ((one-p)*factor + one/2) >> 32; | |
97 last_p8= p8; | |
98 } | |
99 #endif | |
100 for(i=256-max_p; i<=max_p; i++){ | |
101 if(c->one_state[i]) | |
102 continue; | |
103 | |
104 p= (i*one + 128) >> 8; | |
105 p+= ((one-p)*factor + one/2) >> 32; | |
106 p8= (256*p + one/2) >> 32; //FIXME try without the one | |
107 if(p8 <= i) p8= i+1; | |
108 if(p8 > max_p) p8= max_p; | |
109 c->one_state[ i]= p8; | |
110 } | |
111 | |
112 for(i=0; i<256; i++) | |
113 c->zero_state[i]= 256-c->one_state[256-i]; | |
114 #if 0 | |
115 for(i=0; i<256; i++) | |
116 av_log(NULL, AV_LOG_DEBUG, "%3d %3d\n", i, c->one_state[i]); | |
117 #endif | |
118 } | |
119 | |
120 /** | |
121 * | |
122 * @return the number of bytes written | |
123 */ | |
124 int ff_rac_terminate(RangeCoder *c){ | |
125 c->range=0xFF; | |
126 c->low +=0xFF; | |
127 renorm_encoder(c); | |
128 c->range=0xFF; | |
129 renorm_encoder(c); | |
130 | |
131 assert(c->low == 0); | |
132 assert(c->range >= 0x100); | |
133 | |
134 return c->bytestream - c->bytestream_start; | |
135 } | |
136 | |
137 #if 0 //selftest | |
138 #define SIZE 10240 | |
139 int main(){ | |
140 RangeCoder c; | |
141 uint8_t b[9*SIZE]; | |
142 uint8_t r[9*SIZE]; | |
143 int i; | |
144 uint8_t state[10]= {0}; | |
145 | |
146 ff_init_range_encoder(&c, b, SIZE); | |
147 ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16); | |
148 | |
149 memset(state, 128, sizeof(state)); | |
150 | |
151 for(i=0; i<SIZE; i++){ | |
152 r[i]= random()%7; | |
153 } | |
154 | |
155 | |
156 for(i=0; i<SIZE; i++){ | |
157 START_TIMER | |
158 put_rac(&c, state, r[i]&1); | |
159 STOP_TIMER("put_rac") | |
160 } | |
161 | |
162 ff_put_rac_terminate(&c); | |
163 | |
164 ff_init_range_decoder(&c, b, SIZE); | |
165 | |
166 memset(state, 128, sizeof(state)); | |
167 | |
168 for(i=0; i<SIZE; i++){ | |
169 START_TIMER | |
170 if( (r[i]&1) != get_rac(&c, state) ) | |
171 av_log(NULL, AV_LOG_DEBUG, "rac failure at %d\n", i); | |
172 STOP_TIMER("get_rac") | |
173 } | |
174 | |
175 return 0; | |
176 } | |
177 | |
178 #endif |