comparison loco.c @ 2558:2b01396ab483 libavcodec

optimize & simplify
author michael
date Wed, 09 Mar 2005 23:48:17 +0000
parents eace30b70601
children 1e8fbc2b64e0
comparison
equal deleted inserted replaced
2557:dd8c4d4b8abc 2558:2b01396ab483
40 40
41 typedef struct RICEContext{ 41 typedef struct RICEContext{
42 GetBitContext gb; 42 GetBitContext gb;
43 int save, run, run2; /* internal rice decoder state */ 43 int save, run, run2; /* internal rice decoder state */
44 int sum, count; /* sum and count for getting rice parameter */ 44 int sum, count; /* sum and count for getting rice parameter */
45 int lossy;
45 }RICEContext; 46 }RICEContext;
46
47 /* could use get_sr_golomb() but is behaves differently on numbers like Rice(2, -64) */
48 static inline int get_rice(GetBitContext *gb, int K)
49 {
50 int i;
51 int V = 0;
52 for(i = 0; !get_bits1(gb); i++);
53 V = i;
54 for(i = 0; i < K; i++) V = (V << 1) | get_bits1(gb);
55 if(V & 1) return (V + 1) >> 1;
56 return -(V >> 1);
57 }
58
59 static inline int get_u_rice(GetBitContext *gb, int K)
60 {
61 int i;
62 int V = 0;
63 for(i = 0; !get_bits1(gb); i++);
64 V = i;
65 for(i = 0; i < K; i++) V = (V << 1) | get_bits1(gb);
66 return V;
67 }
68 47
69 static int loco_get_rice_param(RICEContext *r) 48 static int loco_get_rice_param(RICEContext *r)
70 { 49 {
71 int cnt = 0; 50 int cnt = 0;
72 int val = r->count; 51 int val = r->count;
79 return cnt; 58 return cnt;
80 } 59 }
81 60
82 static inline void loco_update_rice_param(RICEContext *r, int val) 61 static inline void loco_update_rice_param(RICEContext *r, int val)
83 { 62 {
84 if (val < 0)
85 val = -val;
86 r->sum += val; 63 r->sum += val;
87 r->count++; 64 r->count++;
88 65
89 if(r->count == 16) { 66 if(r->count == 16) {
90 r->sum >>= 1; 67 r->sum >>= 1;
93 } 70 }
94 71
95 static inline int loco_get_rice(RICEContext *r) 72 static inline int loco_get_rice(RICEContext *r)
96 { 73 {
97 int v; 74 int v;
98
99 if (r->run > 0) { /* we have zero run */ 75 if (r->run > 0) { /* we have zero run */
100 r->run--; 76 r->run--;
77 loco_update_rice_param(r, 0);
101 return 0; 78 return 0;
102 } 79 }
103 v = -get_rice(&r->gb, loco_get_rice_param(r)); 80 v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
81 loco_update_rice_param(r, (v+1)>>1);
104 if (!v) { 82 if (!v) {
105 if (r->save >= 0) { 83 if (r->save >= 0) {
106 r->run = get_u_rice(&r->gb, 2); 84 r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
107 if(r->run > 1) 85 if(r->run > 1)
108 r->save += r->run + 1; 86 r->save += r->run + 1;
109 else 87 else
110 r->save -= 3; 88 r->save -= 3;
111 } 89 }
112 else 90 else
113 r->run2++; 91 r->run2++;
114 } else if (r->run2 > 0) { 92 } else {
115 if (r->run2 > 2) 93 v = ((v>>1) + r->lossy) ^ -(v&1);
116 r->save += r->run2; 94 if (r->run2 > 0) {
117 else 95 if (r->run2 > 2)
118 r->save -= 3; 96 r->save += r->run2;
119 r->run2 = 0; 97 else
98 r->save -= 3;
99 r->run2 = 0;
100 }
120 } 101 }
121 102
122 return v; 103 return v;
123 } 104 }
124 105
125 /* LOCO main predictor - LOCO-I/JPEG-LS predictor */ 106 /* LOCO main predictor - LOCO-I/JPEG-LS predictor */
126 static inline int loco_predict(uint8_t* data, int stride, int step) 107 static inline int loco_predict(uint8_t* data, int stride, int step)
127 { 108 {
128 int max_ab, min_ab;
129 int a, b, c; 109 int a, b, c;
130 110
131 a = data[-stride]; 111 a = data[-stride];
132 b = data[-step]; 112 b = data[-step];
133 c = data[-stride - step]; 113 c = data[-stride - step];
134 114
135 max_ab = (a > b) ? a : b; 115 return mid_pred(a, a + b - c, b);
136 min_ab = (a < b) ? a : b;
137
138 if (c >= max_ab) return min_ab;
139 if (c <= min_ab) return max_ab;
140 return (a + b - c);
141 } 116 }
142 117
143 static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height, 118 static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height,
144 int stride, uint8_t *buf, int buf_size, int step) 119 int stride, uint8_t *buf, int buf_size, int step)
145 { 120 {
149 124
150 init_get_bits(&rc.gb, buf, buf_size*8); 125 init_get_bits(&rc.gb, buf, buf_size*8);
151 rc.save = 0; 126 rc.save = 0;
152 rc.run = 0; 127 rc.run = 0;
153 rc.run2 = 0; 128 rc.run2 = 0;
129 rc.lossy = l->lossy;
154 130
155 rc.sum = 8; 131 rc.sum = 8;
156 rc.count = 1; 132 rc.count = 1;
157 133
158 /* restore top left pixel */ 134 /* restore top left pixel */
159 val = loco_get_rice(&rc); 135 val = loco_get_rice(&rc);
160 loco_update_rice_param(&rc, val);
161 if (val < 0) val -= l->lossy;
162 if (val > 0) val += l->lossy;
163 data[0] = 128 + val; 136 data[0] = 128 + val;
164 /* restore top line */ 137 /* restore top line */
165 for (i = 1; i < width; i++) { 138 for (i = 1; i < width; i++) {
166 val = loco_get_rice(&rc); 139 val = loco_get_rice(&rc);
167 loco_update_rice_param(&rc, val);
168 if (val < 0) val -= l->lossy;
169 if (val > 0) val += l->lossy;
170 data[i * step] = data[i * step - step] + val; 140 data[i * step] = data[i * step - step] + val;
171 } 141 }
172 data += stride; 142 data += stride;
173 for (j = 1; j < height; j++) { 143 for (j = 1; j < height; j++) {
174 /* restore left column */ 144 /* restore left column */
175 val = loco_get_rice(&rc); 145 val = loco_get_rice(&rc);
176 loco_update_rice_param(&rc, val);
177 if (val < 0) val -= l->lossy;
178 if (val > 0) val += l->lossy;
179 data[0] = data[-stride] + val; 146 data[0] = data[-stride] + val;
180 /* restore all other pixels */ 147 /* restore all other pixels */
181 for (i = 1; i < width; i++) { 148 for (i = 1; i < width; i++) {
182 val = loco_get_rice(&rc); 149 val = loco_get_rice(&rc);
183 loco_update_rice_param(&rc, val);
184 if (val < 0) val -= l->lossy;
185 if (val > 0) val += l->lossy;
186 data[i * step] = loco_predict(&data[i * step], stride, step) + val; 150 data[i * step] = loco_predict(&data[i * step], stride, step) + val;
187 } 151 }
188 data += stride; 152 data += stride;
189 } 153 }
190 154
300 break; 264 break;
301 default: 265 default:
302 av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode); 266 av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
303 return -1; 267 return -1;
304 } 268 }
269 if(avctx->debug & FF_DEBUG_PICT_INFO)
270 av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
305 271
306 return 0; 272 return 0;
307 } 273 }
308 274
309 AVCodec loco_decoder = { 275 AVCodec loco_decoder = {