Mercurial > libavcodec.hg
comparison loco.c @ 2530:eace30b70601 libavcodec
go LOCO, courtesy of Kostya Shishkov
author | melanson |
---|---|
date | Tue, 01 Mar 2005 02:24:58 +0000 |
parents | |
children | 2b01396ab483 |
comparison
equal
deleted
inserted
replaced
2529:f6a13db551aa | 2530:eace30b70601 |
---|---|
1 /* | |
2 * LOCO codec | |
3 * Copyright (c) 2005 Konstantin Shishkov | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 * | |
19 */ | |
20 | |
21 /** | |
22 * @file loco.c | |
23 * LOCO codec. | |
24 */ | |
25 | |
26 #include "avcodec.h" | |
27 #include "common.h" | |
28 #include "bitstream.h" | |
29 #include "golomb.h" | |
30 | |
31 enum LOCO_MODE {LOCO_UNKN=0, LOCO_CYUY2=-1, LOCO_CRGB=-2, LOCO_CYV12=-3, | |
32 LOCO_YUY2=1, LOCO_UYVY=2, LOCO_RGB=3, LOCO_RGBA=4, LOCO_YV12=5}; | |
33 | |
34 typedef struct LOCOContext{ | |
35 AVCodecContext *avctx; | |
36 AVFrame pic; | |
37 int lossy; | |
38 int mode; | |
39 } LOCOContext; | |
40 | |
41 typedef struct RICEContext{ | |
42 GetBitContext gb; | |
43 int save, run, run2; /* internal rice decoder state */ | |
44 int sum, count; /* sum and count for getting rice parameter */ | |
45 }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 | |
69 static int loco_get_rice_param(RICEContext *r) | |
70 { | |
71 int cnt = 0; | |
72 int val = r->count; | |
73 | |
74 while(r->sum > val && cnt < 9) { | |
75 val <<= 1; | |
76 cnt++; | |
77 } | |
78 | |
79 return cnt; | |
80 } | |
81 | |
82 static inline void loco_update_rice_param(RICEContext *r, int val) | |
83 { | |
84 if (val < 0) | |
85 val = -val; | |
86 r->sum += val; | |
87 r->count++; | |
88 | |
89 if(r->count == 16) { | |
90 r->sum >>= 1; | |
91 r->count >>= 1; | |
92 } | |
93 } | |
94 | |
95 static inline int loco_get_rice(RICEContext *r) | |
96 { | |
97 int v; | |
98 | |
99 if (r->run > 0) { /* we have zero run */ | |
100 r->run--; | |
101 return 0; | |
102 } | |
103 v = -get_rice(&r->gb, loco_get_rice_param(r)); | |
104 if (!v) { | |
105 if (r->save >= 0) { | |
106 r->run = get_u_rice(&r->gb, 2); | |
107 if(r->run > 1) | |
108 r->save += r->run + 1; | |
109 else | |
110 r->save -= 3; | |
111 } | |
112 else | |
113 r->run2++; | |
114 } else if (r->run2 > 0) { | |
115 if (r->run2 > 2) | |
116 r->save += r->run2; | |
117 else | |
118 r->save -= 3; | |
119 r->run2 = 0; | |
120 } | |
121 | |
122 return v; | |
123 } | |
124 | |
125 /* LOCO main predictor - LOCO-I/JPEG-LS predictor */ | |
126 static inline int loco_predict(uint8_t* data, int stride, int step) | |
127 { | |
128 int max_ab, min_ab; | |
129 int a, b, c; | |
130 | |
131 a = data[-stride]; | |
132 b = data[-step]; | |
133 c = data[-stride - step]; | |
134 | |
135 max_ab = (a > b) ? a : 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 } | |
142 | |
143 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) | |
145 { | |
146 RICEContext rc; | |
147 int val; | |
148 int i, j; | |
149 | |
150 init_get_bits(&rc.gb, buf, buf_size*8); | |
151 rc.save = 0; | |
152 rc.run = 0; | |
153 rc.run2 = 0; | |
154 | |
155 rc.sum = 8; | |
156 rc.count = 1; | |
157 | |
158 /* restore top left pixel */ | |
159 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; | |
164 /* restore top line */ | |
165 for (i = 1; i < width; i++) { | |
166 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; | |
171 } | |
172 data += stride; | |
173 for (j = 1; j < height; j++) { | |
174 /* restore left column */ | |
175 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; | |
180 /* restore all other pixels */ | |
181 for (i = 1; i < width; i++) { | |
182 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; | |
187 } | |
188 data += stride; | |
189 } | |
190 | |
191 return ((get_bits_count(&rc.gb) + 7) >> 3); | |
192 } | |
193 | |
194 static int decode_frame(AVCodecContext *avctx, | |
195 void *data, int *data_size, | |
196 uint8_t *buf, int buf_size) | |
197 { | |
198 LOCOContext * const l = avctx->priv_data; | |
199 AVFrame * const p= (AVFrame*)&l->pic; | |
200 int decoded; | |
201 | |
202 if(p->data[0]) | |
203 avctx->release_buffer(avctx, p); | |
204 | |
205 p->reference = 0; | |
206 if(avctx->get_buffer(avctx, p) < 0){ | |
207 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
208 return -1; | |
209 } | |
210 p->key_frame = 1; | |
211 | |
212 switch(l->mode) { | |
213 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY: | |
214 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height, | |
215 p->linesize[0], buf, buf_size, 1); | |
216 buf += decoded; buf_size -= decoded; | |
217 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height, | |
218 p->linesize[1], buf, buf_size, 1); | |
219 buf += decoded; buf_size -= decoded; | |
220 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height, | |
221 p->linesize[2], buf, buf_size, 1); | |
222 break; | |
223 case LOCO_CYV12: case LOCO_YV12: | |
224 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height, | |
225 p->linesize[0], buf, buf_size, 1); | |
226 buf += decoded; buf_size -= decoded; | |
227 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2, | |
228 p->linesize[1], buf, buf_size, 1); | |
229 buf += decoded; buf_size -= decoded; | |
230 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2, | |
231 p->linesize[2], buf, buf_size, 1); | |
232 break; | |
233 case LOCO_CRGB: case LOCO_RGB: | |
234 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height, | |
235 p->linesize[0], buf, buf_size, 3); | |
236 buf += decoded; buf_size -= decoded; | |
237 decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height, | |
238 p->linesize[0], buf, buf_size, 3); | |
239 buf += decoded; buf_size -= decoded; | |
240 decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height, | |
241 p->linesize[0], buf, buf_size, 3); | |
242 break; | |
243 case LOCO_RGBA: | |
244 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height, | |
245 p->linesize[0], buf, buf_size, 4); | |
246 buf += decoded; buf_size -= decoded; | |
247 decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height, | |
248 p->linesize[0], buf, buf_size, 4); | |
249 buf += decoded; buf_size -= decoded; | |
250 decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height, | |
251 p->linesize[0], buf, buf_size, 4); | |
252 buf += decoded; buf_size -= decoded; | |
253 decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height, | |
254 p->linesize[0], buf, buf_size, 4); | |
255 break; | |
256 } | |
257 | |
258 *data_size = sizeof(AVFrame); | |
259 *(AVFrame*)data = l->pic; | |
260 | |
261 return buf_size; | |
262 } | |
263 | |
264 static int decode_init(AVCodecContext *avctx){ | |
265 LOCOContext * const l = avctx->priv_data; | |
266 int version; | |
267 | |
268 l->avctx = avctx; | |
269 if (avctx->extradata_size < 12) { | |
270 av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n", | |
271 avctx->extradata_size); | |
272 return -1; | |
273 } | |
274 version = LE_32(avctx->extradata); | |
275 switch(version) { | |
276 case 1: | |
277 l->lossy = 0; | |
278 break; | |
279 case 2: | |
280 l->lossy = LE_32(avctx->extradata + 8); | |
281 break; | |
282 default: | |
283 l->lossy = LE_32(avctx->extradata + 8); | |
284 av_log(avctx, AV_LOG_INFO, "This is LOCO codec version %i, please upload file for study\n", version); | |
285 } | |
286 | |
287 l->mode = LE_32(avctx->extradata + 4); | |
288 switch(l->mode) { | |
289 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY: | |
290 avctx->pix_fmt = PIX_FMT_YUV422P; | |
291 break; | |
292 case LOCO_CRGB: case LOCO_RGB: | |
293 avctx->pix_fmt = PIX_FMT_BGR24; | |
294 break; | |
295 case LOCO_CYV12: case LOCO_YV12: | |
296 avctx->pix_fmt = PIX_FMT_YUV420P; | |
297 break; | |
298 case LOCO_RGBA: | |
299 avctx->pix_fmt = PIX_FMT_RGBA32; | |
300 break; | |
301 default: | |
302 av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode); | |
303 return -1; | |
304 } | |
305 | |
306 return 0; | |
307 } | |
308 | |
309 AVCodec loco_decoder = { | |
310 "loco", | |
311 CODEC_TYPE_VIDEO, | |
312 CODEC_ID_LOCO, | |
313 sizeof(LOCOContext), | |
314 decode_init, | |
315 NULL, | |
316 NULL, | |
317 decode_frame, | |
318 CODEC_CAP_DR1, | |
319 }; |