a2fac3551903192138c65b941c0f5bb299086061
[libav.git] / libavcodec / rangecoder.h
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 /**
24 * @file rangecoder.h
25 * Range coder.
26 */
27
28 #include <stdint.h>
29 #include <assert.h>
30 #include "common.h"
31
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 }
67
68 c->low = (c->low & 0xFF)<<8;
69 c->range <<= 8;
70 }
71 }
72
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
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 }
94
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;
110 int av_unused one_mask;
111
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;
127
128 c->low -= c->range & one_mask;
129 c->range += (range1 - c->range) & one_mask;
130
131 *state= c->zero_state[(*state) + (256&one_mask)];
132
133 refill(c);
134
135 return one_mask&1;
136 #endif
137 }
138