annotate rangecoder.h @ 9369:539e18dfc86a libavcodec

Check spatial_decomposition_count too, fixes another crash.
author michael
date Thu, 09 Apr 2009 18:37:26 +0000
parents e9d9d946f213
children 7dd2a45249a9
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 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2334
25448d0bc924 range coder
michael
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
25448d0bc924 range coder
michael
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2334
25448d0bc924 range coder
michael
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2334
25448d0bc924 range coder
michael
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25448d0bc924 range coder
michael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25448d0bc924 range coder
michael
parents:
diff changeset
15 * Lesser General Public License for more details.
25448d0bc924 range coder
michael
parents:
diff changeset
16 *
25448d0bc924 range coder
michael
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2334
25448d0bc924 range coder
michael
parents:
diff changeset
20 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
21
2334
25448d0bc924 range coder
michael
parents:
diff changeset
22 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 7760
diff changeset
23 * @file libavcodec/rangecoder.h
2334
25448d0bc924 range coder
michael
parents:
diff changeset
24 * Range coder.
25448d0bc924 range coder
michael
parents:
diff changeset
25 */
25448d0bc924 range coder
michael
parents:
diff changeset
26
7760
c4a4495715dd Globally rename the header inclusion guard names.
stefano
parents: 6763
diff changeset
27 #ifndef AVCODEC_RANGECODER_H
c4a4495715dd Globally rename the header inclusion guard names.
stefano
parents: 6763
diff changeset
28 #define AVCODEC_RANGECODER_H
5163
9ecbfc0c82bf add multiple inclusion guards to headers
mru
parents: 5162
diff changeset
29
5162
4394344397d8 include all prerequisites in header files
mru
parents: 5083
diff changeset
30 #include <stdint.h>
4394344397d8 include all prerequisites in header files
mru
parents: 5083
diff changeset
31 #include <assert.h>
6763
f7cbb7733146 Use full path for #includes from another directory.
diego
parents: 5830
diff changeset
32 #include "libavutil/common.h"
5162
4394344397d8 include all prerequisites in header files
mru
parents: 5083
diff changeset
33
2334
25448d0bc924 range coder
michael
parents:
diff changeset
34 typedef struct RangeCoder{
25448d0bc924 range coder
michael
parents:
diff changeset
35 int low;
25448d0bc924 range coder
michael
parents:
diff changeset
36 int range;
25448d0bc924 range coder
michael
parents:
diff changeset
37 int outstanding_count;
25448d0bc924 range coder
michael
parents:
diff changeset
38 int outstanding_byte;
25448d0bc924 range coder
michael
parents:
diff changeset
39 uint8_t zero_state[256];
25448d0bc924 range coder
michael
parents:
diff changeset
40 uint8_t one_state[256];
25448d0bc924 range coder
michael
parents:
diff changeset
41 uint8_t *bytestream_start;
25448d0bc924 range coder
michael
parents:
diff changeset
42 uint8_t *bytestream;
25448d0bc924 range coder
michael
parents:
diff changeset
43 uint8_t *bytestream_end;
25448d0bc924 range coder
michael
parents:
diff changeset
44 }RangeCoder;
25448d0bc924 range coder
michael
parents:
diff changeset
45
25448d0bc924 range coder
michael
parents:
diff changeset
46 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
25448d0bc924 range coder
michael
parents:
diff changeset
47 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
25448d0bc924 range coder
michael
parents:
diff changeset
48 int ff_rac_terminate(RangeCoder *c);
25448d0bc924 range coder
michael
parents:
diff changeset
49 void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
25448d0bc924 range coder
michael
parents:
diff changeset
50
25448d0bc924 range coder
michael
parents:
diff changeset
51 static inline void renorm_encoder(RangeCoder *c){
25448d0bc924 range coder
michael
parents:
diff changeset
52 //FIXME optimize
25448d0bc924 range coder
michael
parents:
diff changeset
53 while(c->range < 0x100){
25448d0bc924 range coder
michael
parents:
diff changeset
54 if(c->outstanding_byte < 0){
25448d0bc924 range coder
michael
parents:
diff changeset
55 c->outstanding_byte= c->low>>8;
25448d0bc924 range coder
michael
parents:
diff changeset
56 }else if(c->low <= 0xFF00){
25448d0bc924 range coder
michael
parents:
diff changeset
57 *c->bytestream++ = c->outstanding_byte;
25448d0bc924 range coder
michael
parents:
diff changeset
58 for(;c->outstanding_count; c->outstanding_count--)
25448d0bc924 range coder
michael
parents:
diff changeset
59 *c->bytestream++ = 0xFF;
25448d0bc924 range coder
michael
parents:
diff changeset
60 c->outstanding_byte= c->low>>8;
25448d0bc924 range coder
michael
parents:
diff changeset
61 }else if(c->low >= 0x10000){
25448d0bc924 range coder
michael
parents:
diff changeset
62 *c->bytestream++ = c->outstanding_byte + 1;
25448d0bc924 range coder
michael
parents:
diff changeset
63 for(;c->outstanding_count; c->outstanding_count--)
25448d0bc924 range coder
michael
parents:
diff changeset
64 *c->bytestream++ = 0x00;
25448d0bc924 range coder
michael
parents:
diff changeset
65 c->outstanding_byte= (c->low>>8) & 0xFF;
25448d0bc924 range coder
michael
parents:
diff changeset
66 }else{
25448d0bc924 range coder
michael
parents:
diff changeset
67 c->outstanding_count++;
25448d0bc924 range coder
michael
parents:
diff changeset
68 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
69
2334
25448d0bc924 range coder
michael
parents:
diff changeset
70 c->low = (c->low & 0xFF)<<8;
25448d0bc924 range coder
michael
parents:
diff changeset
71 c->range <<= 8;
25448d0bc924 range coder
michael
parents:
diff changeset
72 }
25448d0bc924 range coder
michael
parents:
diff changeset
73 }
25448d0bc924 range coder
michael
parents:
diff changeset
74
5082
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
75 static inline int get_rac_count(RangeCoder *c){
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
76 int x= c->bytestream - c->bytestream_start + c->outstanding_count;
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
77 if(c->outstanding_byte >= 0)
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
78 x++;
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
79 return 8*x - av_log2(c->range);
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
80 }
176ac8353f48 fix assertion failure
michael
parents: 3947
diff changeset
81
2334
25448d0bc924 range coder
michael
parents:
diff changeset
82 static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){
25448d0bc924 range coder
michael
parents:
diff changeset
83 int range1= (c->range * (*state)) >> 8;
25448d0bc924 range coder
michael
parents:
diff changeset
84
25448d0bc924 range coder
michael
parents:
diff changeset
85 assert(*state);
25448d0bc924 range coder
michael
parents:
diff changeset
86 assert(range1 < c->range);
25448d0bc924 range coder
michael
parents:
diff changeset
87 assert(range1 > 0);
25448d0bc924 range coder
michael
parents:
diff changeset
88 if(!bit){
25448d0bc924 range coder
michael
parents:
diff changeset
89 c->range -= range1;
25448d0bc924 range coder
michael
parents:
diff changeset
90 *state= c->zero_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
91 }else{
25448d0bc924 range coder
michael
parents:
diff changeset
92 c->low += c->range - range1;
25448d0bc924 range coder
michael
parents:
diff changeset
93 c->range = range1;
25448d0bc924 range coder
michael
parents:
diff changeset
94 *state= c->one_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
95 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
96
2334
25448d0bc924 range coder
michael
parents:
diff changeset
97 renorm_encoder(c);
25448d0bc924 range coder
michael
parents:
diff changeset
98 }
25448d0bc924 range coder
michael
parents:
diff changeset
99
25448d0bc924 range coder
michael
parents:
diff changeset
100 static inline void refill(RangeCoder *c){
25448d0bc924 range coder
michael
parents:
diff changeset
101 if(c->range < 0x100){
25448d0bc924 range coder
michael
parents:
diff changeset
102 c->range <<= 8;
25448d0bc924 range coder
michael
parents:
diff changeset
103 c->low <<= 8;
25448d0bc924 range coder
michael
parents:
diff changeset
104 if(c->bytestream < c->bytestream_end)
25448d0bc924 range coder
michael
parents:
diff changeset
105 c->low+= c->bytestream[0];
25448d0bc924 range coder
michael
parents:
diff changeset
106 c->bytestream++;
25448d0bc924 range coder
michael
parents:
diff changeset
107 }
25448d0bc924 range coder
michael
parents:
diff changeset
108 }
25448d0bc924 range coder
michael
parents:
diff changeset
109
25448d0bc924 range coder
michael
parents:
diff changeset
110 static inline int get_rac(RangeCoder *c, uint8_t * const state){
25448d0bc924 range coder
michael
parents:
diff changeset
111 int range1= (c->range * (*state)) >> 8;
5083
ce36118abbbb rename attribute_unused to av_unused and moves its declaration to common.h
benoit
parents: 5082
diff changeset
112 int av_unused one_mask;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
113
2334
25448d0bc924 range coder
michael
parents:
diff changeset
114 c->range -= range1;
25448d0bc924 range coder
michael
parents:
diff changeset
115 #if 1
25448d0bc924 range coder
michael
parents:
diff changeset
116 if(c->low < c->range){
25448d0bc924 range coder
michael
parents:
diff changeset
117 *state= c->zero_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
118 refill(c);
25448d0bc924 range coder
michael
parents:
diff changeset
119 return 0;
25448d0bc924 range coder
michael
parents:
diff changeset
120 }else{
25448d0bc924 range coder
michael
parents:
diff changeset
121 c->low -= c->range;
25448d0bc924 range coder
michael
parents:
diff changeset
122 *state= c->one_state[*state];
25448d0bc924 range coder
michael
parents:
diff changeset
123 c->range = range1;
25448d0bc924 range coder
michael
parents:
diff changeset
124 refill(c);
25448d0bc924 range coder
michael
parents:
diff changeset
125 return 1;
25448d0bc924 range coder
michael
parents:
diff changeset
126 }
25448d0bc924 range coder
michael
parents:
diff changeset
127 #else
25448d0bc924 range coder
michael
parents:
diff changeset
128 one_mask= (c->range - c->low-1)>>31;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
129
2334
25448d0bc924 range coder
michael
parents:
diff changeset
130 c->low -= c->range & one_mask;
25448d0bc924 range coder
michael
parents:
diff changeset
131 c->range += (range1 - c->range) & one_mask;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
132
2334
25448d0bc924 range coder
michael
parents:
diff changeset
133 *state= c->zero_state[(*state) + (256&one_mask)];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2522
diff changeset
134
2334
25448d0bc924 range coder
michael
parents:
diff changeset
135 refill(c);
25448d0bc924 range coder
michael
parents:
diff changeset
136
25448d0bc924 range coder
michael
parents:
diff changeset
137 return one_mask&1;
25448d0bc924 range coder
michael
parents:
diff changeset
138 #endif
25448d0bc924 range coder
michael
parents:
diff changeset
139 }
25448d0bc924 range coder
michael
parents:
diff changeset
140
7760
c4a4495715dd Globally rename the header inclusion guard names.
stefano
parents: 6763
diff changeset
141 #endif /* AVCODEC_RANGECODER_H */