annotate rangecoder.h @ 3188:04636faaa720 libavcodec

asserts to check if assumed conditions really are true
author michael
date Fri, 10 Mar 2006 22:00:36 +0000
parents 0b546eab515d
children c8c591fe26f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2334
25448d0bc924 range coder
michael
parents:
diff changeset
1 /*
25448d0bc924 range coder
michael
parents:
diff changeset
2 * Range coder
25448d0bc924 range coder
michael
parents:
diff changeset
3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
25448d0bc924 range coder
michael
parents:
diff changeset
4 *
25448d0bc924 range coder
michael
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
25448d0bc924 range coder
michael
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
25448d0bc924 range coder
michael
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
25448d0bc924 range coder
michael
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
25448d0bc924 range coder
michael
parents:
diff changeset
9 *
25448d0bc924 range coder
michael
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
25448d0bc924 range coder
michael
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25448d0bc924 range coder
michael
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25448d0bc924 range coder
michael
parents:
diff changeset
13 * Lesser General Public License for more details.
25448d0bc924 range coder
michael
parents:
diff changeset
14 *
25448d0bc924 range coder
michael
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
25448d0bc924 range coder
michael
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2334
25448d0bc924 range coder
michael
parents:
diff changeset
18 *
25448d0bc924 range coder
michael
parents:
diff changeset
19 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
20
2334
25448d0bc924 range coder
michael
parents:
diff changeset
21 /**
25448d0bc924 range coder
michael
parents:
diff changeset
22 * @file rangecoder.h
25448d0bc924 range coder
michael
parents:
diff changeset
23 * Range coder.
25448d0bc924 range coder
michael
parents:
diff changeset
24 */
25448d0bc924 range coder
michael
parents:
diff changeset
25
25448d0bc924 range coder
michael
parents:
diff changeset
26 typedef struct RangeCoder{
25448d0bc924 range coder
michael
parents:
diff changeset
27 int low;
25448d0bc924 range coder
michael
parents:
diff changeset
28 int range;
25448d0bc924 range coder
michael
parents:
diff changeset
29 int outstanding_count;
25448d0bc924 range coder
michael
parents:
diff changeset
30 int outstanding_byte;
25448d0bc924 range coder
michael
parents:
diff changeset
31 uint8_t zero_state[256];
25448d0bc924 range coder
michael
parents:
diff changeset
32 uint8_t one_state[256];
25448d0bc924 range coder
michael
parents:
diff changeset
33 uint8_t *bytestream_start;
25448d0bc924 range coder
michael
parents:
diff changeset
34 uint8_t *bytestream;
25448d0bc924 range coder
michael
parents:
diff changeset
35 uint8_t *bytestream_end;
25448d0bc924 range coder
michael
parents:
diff changeset
36 }RangeCoder;
25448d0bc924 range coder
michael
parents:
diff changeset
37
25448d0bc924 range coder
michael
parents:
diff changeset
38 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
25448d0bc924 range coder
michael
parents:
diff changeset
39 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
25448d0bc924 range coder
michael
parents:
diff changeset
40 int ff_rac_terminate(RangeCoder *c);
25448d0bc924 range coder
michael
parents:
diff changeset
41 void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
25448d0bc924 range coder
michael
parents:
diff changeset
42
25448d0bc924 range coder
michael
parents:
diff changeset
43 static inline void renorm_encoder(RangeCoder *c){
25448d0bc924 range coder
michael
parents:
diff changeset
44 //FIXME optimize
25448d0bc924 range coder
michael
parents:
diff changeset
45 while(c->range < 0x100){
25448d0bc924 range coder
michael
parents:
diff changeset
46 if(c->outstanding_byte < 0){
25448d0bc924 range coder
michael
parents:
diff changeset
47 c->outstanding_byte= c->low>>8;
25448d0bc924 range coder
michael
parents:
diff changeset
48 }else if(c->low <= 0xFF00){
25448d0bc924 range coder
michael
parents:
diff changeset
49 *c->bytestream++ = c->outstanding_byte;
25448d0bc924 range coder
michael
parents:
diff changeset
50 for(;c->outstanding_count; c->outstanding_count--)
25448d0bc924 range coder
michael
parents:
diff changeset
51 *c->bytestream++ = 0xFF;
25448d0bc924 range coder
michael
parents:
diff changeset
52 c->outstanding_byte= c->low>>8;
25448d0bc924 range coder
michael
parents:
diff changeset
53 }else if(c->low >= 0x10000){
25448d0bc924 range coder
michael
parents:
diff changeset
54 *c->bytestream++ = c->outstanding_byte + 1;
25448d0bc924 range coder
michael
parents:
diff changeset
55 for(;c->outstanding_count; c->outstanding_count--)
25448d0bc924 range coder
michael
parents:
diff changeset
56 *c->bytestream++ = 0x00;
25448d0bc924 range coder
michael
parents:
diff changeset
57 c->outstanding_byte= (c->low>>8) & 0xFF;
25448d0bc924 range coder
michael
parents:
diff changeset
58 }else{
25448d0bc924 range coder
michael
parents:
diff changeset
59 c->outstanding_count++;
25448d0bc924 range coder
michael
parents:
diff changeset
60 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
61
2334
25448d0bc924 range coder
michael
parents:
diff changeset
62 c->low = (c->low & 0xFF)<<8;
25448d0bc924 range coder
michael
parents:
diff changeset
63 c->range <<= 8;
25448d0bc924 range coder
michael
parents:
diff changeset
64 }
25448d0bc924 range coder
michael
parents:
diff changeset
65 }
25448d0bc924 range coder
michael
parents:
diff changeset
66
25448d0bc924 range coder
michael
parents:
diff changeset
67 static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){
25448d0bc924 range coder
michael
parents:
diff changeset
68 int range1= (c->range * (*state)) >> 8;
25448d0bc924 range coder
michael
parents:
diff changeset
69
25448d0bc924 range coder
michael
parents:
diff changeset
70 assert(*state);
25448d0bc924 range coder
michael
parents:
diff changeset
71 assert(range1 < c->range);
25448d0bc924 range coder
michael
parents:
diff changeset
72 assert(range1 > 0);
25448d0bc924 range coder
michael
parents:
diff changeset
73 if(!bit){
25448d0bc924 range coder
michael
parents:
diff changeset
74 c->range -= range1;
25448d0bc924 range coder
michael
parents:
diff changeset
75 *state= c->zero_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
76 }else{
25448d0bc924 range coder
michael
parents:
diff changeset
77 c->low += c->range - range1;
25448d0bc924 range coder
michael
parents:
diff changeset
78 c->range = range1;
25448d0bc924 range coder
michael
parents:
diff changeset
79 *state= c->one_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
80 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
81
2334
25448d0bc924 range coder
michael
parents:
diff changeset
82 renorm_encoder(c);
25448d0bc924 range coder
michael
parents:
diff changeset
83 }
25448d0bc924 range coder
michael
parents:
diff changeset
84
25448d0bc924 range coder
michael
parents:
diff changeset
85 static inline void refill(RangeCoder *c){
25448d0bc924 range coder
michael
parents:
diff changeset
86 if(c->range < 0x100){
25448d0bc924 range coder
michael
parents:
diff changeset
87 c->range <<= 8;
25448d0bc924 range coder
michael
parents:
diff changeset
88 c->low <<= 8;
25448d0bc924 range coder
michael
parents:
diff changeset
89 if(c->bytestream < c->bytestream_end)
25448d0bc924 range coder
michael
parents:
diff changeset
90 c->low+= c->bytestream[0];
25448d0bc924 range coder
michael
parents:
diff changeset
91 c->bytestream++;
25448d0bc924 range coder
michael
parents:
diff changeset
92 }
25448d0bc924 range coder
michael
parents:
diff changeset
93 }
25448d0bc924 range coder
michael
parents:
diff changeset
94
25448d0bc924 range coder
michael
parents:
diff changeset
95 static inline int get_rac(RangeCoder *c, uint8_t * const state){
25448d0bc924 range coder
michael
parents:
diff changeset
96 int range1= (c->range * (*state)) >> 8;
2522
e25782262d7d kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents: 2337
diff changeset
97 int attribute_unused one_mask;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
98
2334
25448d0bc924 range coder
michael
parents:
diff changeset
99 c->range -= range1;
25448d0bc924 range coder
michael
parents:
diff changeset
100 #if 1
25448d0bc924 range coder
michael
parents:
diff changeset
101 if(c->low < c->range){
25448d0bc924 range coder
michael
parents:
diff changeset
102 *state= c->zero_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
103 refill(c);
25448d0bc924 range coder
michael
parents:
diff changeset
104 return 0;
25448d0bc924 range coder
michael
parents:
diff changeset
105 }else{
25448d0bc924 range coder
michael
parents:
diff changeset
106 c->low -= c->range;
25448d0bc924 range coder
michael
parents:
diff changeset
107 *state= c->one_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
108 c->range = range1;
25448d0bc924 range coder
michael
parents:
diff changeset
109 refill(c);
25448d0bc924 range coder
michael
parents:
diff changeset
110 return 1;
25448d0bc924 range coder
michael
parents:
diff changeset
111 }
25448d0bc924 range coder
michael
parents:
diff changeset
112 #else
25448d0bc924 range coder
michael
parents:
diff changeset
113 one_mask= (c->range - c->low-1)>>31;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
114
2334
25448d0bc924 range coder
michael
parents:
diff changeset
115 c->low -= c->range & one_mask;
25448d0bc924 range coder
michael
parents:
diff changeset
116 c->range += (range1 - c->range) & one_mask;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
117
2334
25448d0bc924 range coder
michael
parents:
diff changeset
118 *state= c->zero_state[(*state) + (256&one_mask)];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
119
2334
25448d0bc924 range coder
michael
parents:
diff changeset
120 refill(c);
25448d0bc924 range coder
michael
parents:
diff changeset
121
25448d0bc924 range coder
michael
parents:
diff changeset
122 return one_mask&1;
25448d0bc924 range coder
michael
parents:
diff changeset
123 #endif
25448d0bc924 range coder
michael
parents:
diff changeset
124 }
25448d0bc924 range coder
michael
parents:
diff changeset
125