1/*
2 * Range coder
3 * Copyright (c) 2004 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 */
21
22/**
23 * @file libavcodec/rangecoder.c
24 * Range coder.
25 * based upon
26 *    "Range encoding: an algorithm for removing redundancy from a digitised
27 *                     message.
28 *     G. N. N. Martin                  Presented in March 1979 to the Video &
29 *                                      Data Recording Conference,
30 *     IBM UK Scientific Center         held in Southampton July 24-27 1979."
31 *
32 */
33
34#include <string.h>
35
36#include "avcodec.h"
37#include "rangecoder.h"
38#include "bytestream.h"
39
40
41void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){
42    c->bytestream_start=
43    c->bytestream= buf;
44    c->bytestream_end= buf + buf_size;
45
46    c->low= 0;
47    c->range= 0xFF00;
48    c->outstanding_count= 0;
49    c->outstanding_byte= -1;
50}
51
52void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){
53    /* cast to avoid compiler warning */
54    ff_init_range_encoder(c, (uint8_t *) buf, buf_size);
55
56    c->low = bytestream_get_be16(&c->bytestream);
57}
58
59void ff_build_rac_states(RangeCoder *c, int factor, int max_p){
60    const int64_t one= 1LL<<32;
61    int64_t p;
62    int last_p8, p8, i;
63
64    memset(c->zero_state, 0, sizeof(c->zero_state));
65    memset(c-> one_state, 0, sizeof(c-> one_state));
66
67    last_p8= 0;
68    p= one/2;
69    for(i=0; i<128; i++){
70        p8= (256*p + one/2) >> 32; //FIXME try without the one
71        if(p8 <= last_p8) p8= last_p8+1;
72        if(last_p8 && last_p8<256 && p8<=max_p)
73            c->one_state[last_p8]= p8;
74
75        p+= ((one-p)*factor + one/2) >> 32;
76        last_p8= p8;
77    }
78
79    for(i=256-max_p; i<=max_p; i++){
80        if(c->one_state[i])
81            continue;
82
83        p= (i*one + 128) >> 8;
84        p+= ((one-p)*factor + one/2) >> 32;
85        p8= (256*p + one/2) >> 32; //FIXME try without the one
86        if(p8 <= i) p8= i+1;
87        if(p8 > max_p) p8= max_p;
88        c->one_state[    i]=     p8;
89    }
90
91    for(i=1; i<255; i++)
92        c->zero_state[i]= 256-c->one_state[256-i];
93}
94
95/**
96 *
97 * @return the number of bytes written
98 */
99int ff_rac_terminate(RangeCoder *c){
100    c->range=0xFF;
101    c->low +=0xFF;
102    renorm_encoder(c);
103    c->range=0xFF;
104    renorm_encoder(c);
105
106    assert(c->low   == 0);
107    assert(c->range >= 0x100);
108
109    return c->bytestream - c->bytestream_start;
110}
111
112#ifdef TEST
113#define SIZE 10240
114#undef random
115int main(void){
116    RangeCoder c;
117    uint8_t b[9*SIZE];
118    uint8_t r[9*SIZE];
119    int i;
120    uint8_t state[10]= {0};
121
122    ff_init_range_encoder(&c, b, SIZE);
123    ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16);
124
125    memset(state, 128, sizeof(state));
126
127    for(i=0; i<SIZE; i++){
128        r[i]= random()%7;
129    }
130
131    for(i=0; i<SIZE; i++){
132START_TIMER
133        put_rac(&c, state, r[i]&1);
134STOP_TIMER("put_rac")
135    }
136
137    ff_rac_terminate(&c);
138
139    ff_init_range_decoder(&c, b, SIZE);
140
141    memset(state, 128, sizeof(state));
142
143    for(i=0; i<SIZE; i++){
144START_TIMER
145        if( (r[i]&1) != get_rac(&c, state) )
146            av_log(NULL, AV_LOG_DEBUG, "rac failure at %d\n", i);
147STOP_TIMER("get_rac")
148    }
149
150    return 0;
151}
152#endif /* TEST */
153