3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
25 * "Range encoding: an algorithm for removing redundancy from a digitised
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."
37 #include "rangecoder.h"
40 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){
43 c->bytestream_end= buf + buf_size;
47 c->outstanding_count= 0;
48 c->outstanding_byte= -1;
51 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){
52 ff_init_range_encoder(c, buf, buf_size);
54 c->low =(*c->bytestream++)<<8;
55 c->low+= *c->bytestream++;
58 void ff_build_rac_states(RangeCoder *c, int factor, int max_p){
59 const int64_t one= 1LL<<32;
63 memset(c->zero_state, 0, sizeof(c->zero_state));
64 memset(c-> one_state, 0, sizeof(c-> one_state));
71 p= (i*one + 128) >> 8;
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;
80 c->one_state[last_p8]= p8;
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;
96 p+= ((one-p)*factor + one/2) >> 32;
100 for(i=256-max_p; i<=max_p; i++){
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
108 if(p8 > max_p) p8= max_p;
109 c->one_state[ i]= p8;
113 c->zero_state[i]= 256-c->one_state[256-i];
116 av_log(NULL, AV_LOG_DEBUG, "%3d %3d\n", i, c->one_state[i]);
122 * @return the number of bytes written
124 int ff_rac_terminate(RangeCoder *c){
132 assert(c->range >= 0x100);
134 return c->bytestream - c->bytestream_start;
144 uint8_t state[10]= {0};
146 ff_init_range_encoder(&c, b, SIZE);
147 ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16);
149 memset(state, 128, sizeof(state));
151 for(i=0; i<SIZE; i++){
156 for(i=0; i<SIZE; i++){
158 put_rac(&c, state, r[i]&1);
159 STOP_TIMER("put_rac")
162 ff_put_rac_terminate(&c);
164 ff_init_range_decoder(&c, b, SIZE);
166 memset(state, 128, sizeof(state));
168 for(i=0; i<SIZE; i++){
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")