5003
|
1 /*
|
|
2 * JPEG-LS decoder
|
|
3 * Copyright (c) 2003 Michael Niedermayer
|
|
4 * Copyright (c) 2006 Konstantin Shishkov
|
|
5 *
|
|
6 * This file is part of FFmpeg.
|
|
7 *
|
|
8 * FFmpeg is free software; you can redistribute it and/or
|
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
10 * License as published by the Free Software Foundation; either
|
|
11 * version 2.1 of the License, or (at your option) any later version.
|
|
12 *
|
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 * Lesser General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
19 * License along with FFmpeg; if not, write to the Free Software
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
21 */
|
|
22
|
|
23 /**
|
|
24 * @file jpeglsdec.c
|
|
25 * JPEG-LS decoder.
|
|
26 */
|
|
27
|
|
28 #include "avcodec.h"
|
|
29 #include "bitstream.h"
|
|
30 #include "golomb.h"
|
|
31 #include "mjpeg.h"
|
|
32 #include "jpegls.h"
|
|
33 #include "jpeglsdec.h"
|
|
34
|
|
35
|
|
36 /*
|
|
37 * Uncomment this to significantly speed up decoding of broken JPEG-LS
|
|
38 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
|
|
39 *
|
|
40 * There is no Golomb code with length >= 32 bits possible, so check and
|
|
41 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
|
|
42 * on this errors.
|
|
43 */
|
|
44 //#define JLS_BROKEN
|
|
45
|
|
46
|
|
47 /**
|
|
48 * Decode LSE block with initialization parameters
|
|
49 */
|
|
50 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
|
|
51 {
|
|
52 int len, id;
|
|
53
|
|
54 /* XXX: verify len field validity */
|
|
55 len = get_bits(&s->gb, 16);
|
|
56 id = get_bits(&s->gb, 8);
|
|
57
|
|
58 switch(id){
|
|
59 case 1:
|
|
60 s->maxval= get_bits(&s->gb, 16);
|
|
61 s->t1= get_bits(&s->gb, 16);
|
|
62 s->t2= get_bits(&s->gb, 16);
|
|
63 s->t3= get_bits(&s->gb, 16);
|
|
64 s->reset= get_bits(&s->gb, 16);
|
|
65
|
|
66 // ff_jpegls_reset_coding_parameters(s, 0);
|
|
67 //FIXME quant table?
|
|
68 break;
|
|
69 case 2:
|
|
70 case 3:
|
|
71 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
|
|
72 return -1;
|
|
73 case 4:
|
|
74 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
|
|
75 return -1;
|
|
76 default:
|
|
77 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
|
|
78 return -1;
|
|
79 }
|
|
80 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
|
|
81
|
|
82 return 0;
|
|
83 }
|
|
84
|
|
85 /**
|
|
86 * Get context-dependent Golomb code, decode it and update context
|
|
87 */
|
|
88 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
|
|
89 int k, ret;
|
|
90
|
|
91 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
|
|
92
|
|
93 #ifdef JLS_BROKEN
|
|
94 if(!show_bits_long(gb, 32))return -1;
|
|
95 #endif
|
|
96 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
|
|
97
|
|
98 /* decode mapped error */
|
|
99 if(ret & 1)
|
|
100 ret = -((ret + 1) >> 1);
|
|
101 else
|
|
102 ret >>= 1;
|
|
103
|
|
104 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
|
|
105 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
|
|
106 ret = -(ret + 1);
|
|
107
|
|
108 ret= ff_jpegls_update_state_regular(state, Q, ret);
|
|
109
|
|
110 return ret;
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Get Golomb code, decode it and update state for run termination
|
|
115 */
|
|
116 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
|
|
117 int k, ret, temp, map;
|
|
118 int Q = 365 + RItype;
|
|
119
|
|
120 temp= state->A[Q];
|
|
121 if(RItype)
|
|
122 temp += state->N[Q] >> 1;
|
|
123
|
|
124 for(k = 0; (state->N[Q] << k) < temp; k++);
|
|
125
|
|
126 #ifdef JLS_BROKEN
|
|
127 if(!show_bits_long(gb, 32))return -1;
|
|
128 #endif
|
|
129 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
|
|
130
|
|
131 /* decode mapped error */
|
|
132 map = 0;
|
|
133 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
|
|
134 map = 1;
|
|
135 ret += RItype + map;
|
|
136
|
|
137 if(ret & 1){
|
|
138 ret = map - ((ret + 1) >> 1);
|
|
139 state->B[Q]++;
|
|
140 } else {
|
|
141 ret = ret >> 1;
|
|
142 }
|
|
143
|
|
144 /* update state */
|
|
145 state->A[Q] += FFABS(ret) - RItype;
|
|
146 ret *= state->twonear;
|
|
147 ff_jpegls_downscale_state(state, Q);
|
|
148
|
|
149 return ret;
|
|
150 }
|
|
151
|
|
152 /**
|
|
153 * Decode one line of image
|
|
154 */
|
|
155 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
|
|
156 int i, x = 0;
|
|
157 int Ra, Rb, Rc, Rd;
|
|
158 int D0, D1, D2;
|
|
159
|
|
160 while(x < w) {
|
|
161 int err, pred;
|
|
162
|
|
163 /* compute gradients */
|
|
164 Ra = x ? R(dst, x - stride) : R(last, x);
|
|
165 Rb = R(last, x);
|
|
166 Rc = x ? R(last, x - stride) : last2;
|
|
167 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
|
|
168 D0 = Rd - Rb;
|
|
169 D1 = Rb - Rc;
|
|
170 D2 = Rc - Ra;
|
|
171 /* run mode */
|
|
172 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
|
|
173 int r;
|
|
174 int RItype;
|
|
175
|
|
176 /* decode full runs while available */
|
|
177 while(get_bits1(&s->gb)) {
|
|
178 int r;
|
|
179 r = 1 << ff_log2_run[state->run_index[comp]];
|
|
180 if(x + r * stride > w) {
|
|
181 r = (w - x) / stride;
|
|
182 }
|
|
183 for(i = 0; i < r; i++) {
|
|
184 W(dst, x, Ra);
|
|
185 x += stride;
|
|
186 }
|
|
187 /* if EOL reached, we stop decoding */
|
|
188 if(r != (1 << ff_log2_run[state->run_index[comp]]))
|
|
189 return;
|
|
190 if(state->run_index[comp] < 31)
|
|
191 state->run_index[comp]++;
|
|
192 if(x + stride > w)
|
|
193 return;
|
|
194 }
|
|
195 /* decode aborted run */
|
|
196 r = ff_log2_run[state->run_index[comp]];
|
|
197 if(r)
|
|
198 r = get_bits_long(&s->gb, r);
|
|
199 for(i = 0; i < r; i++) {
|
|
200 W(dst, x, Ra);
|
|
201 x += stride;
|
|
202 }
|
|
203
|
|
204 /* decode run termination value */
|
|
205 Rb = R(last, x);
|
|
206 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
|
|
207 err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
|
|
208 if(state->run_index[comp])
|
|
209 state->run_index[comp]--;
|
|
210
|
|
211 if(state->near && RItype){
|
|
212 pred = Ra + err;
|
|
213 } else {
|
|
214 if(Rb < Ra)
|
|
215 pred = Rb - err;
|
|
216 else
|
|
217 pred = Rb + err;
|
|
218 }
|
|
219 } else { /* regular mode */
|
|
220 int context, sign;
|
|
221
|
|
222 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
|
|
223 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
|
|
224
|
|
225 if(context < 0){
|
|
226 context = -context;
|
|
227 sign = 1;
|
|
228 }else{
|
|
229 sign = 0;
|
|
230 }
|
|
231
|
|
232 if(sign){
|
|
233 pred = av_clip(pred - state->C[context], 0, state->maxval);
|
|
234 err = -ls_get_code_regular(&s->gb, state, context);
|
|
235 } else {
|
|
236 pred = av_clip(pred + state->C[context], 0, state->maxval);
|
|
237 err = ls_get_code_regular(&s->gb, state, context);
|
|
238 }
|
|
239
|
|
240 /* we have to do something more for near-lossless coding */
|
|
241 pred += err;
|
|
242 }
|
|
243 if(state->near){
|
|
244 if(pred < -state->near)
|
|
245 pred += state->range * state->twonear;
|
|
246 else if(pred > state->maxval + state->near)
|
|
247 pred -= state->range * state->twonear;
|
|
248 pred = av_clip(pred, 0, state->maxval);
|
|
249 }
|
|
250
|
|
251 pred &= state->maxval;
|
|
252 W(dst, x, pred);
|
|
253 x += stride;
|
|
254 }
|
|
255 }
|
|
256
|
|
257 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
|
|
258 int i, t = 0;
|
|
259 uint8_t *zero, *last, *cur;
|
|
260 JLSState *state;
|
|
261 int off = 0, stride = 1, width, shift;
|
|
262
|
|
263 zero = av_mallocz(s->picture.linesize[0]);
|
|
264 last = zero;
|
|
265 cur = s->picture.data[0];
|
|
266
|
|
267 state = av_mallocz(sizeof(JLSState));
|
|
268 /* initialize JPEG-LS state from JPEG parameters */
|
|
269 state->near = near;
|
|
270 state->bpp = (s->bits < 2) ? 2 : s->bits;
|
|
271 state->maxval = s->maxval;
|
|
272 state->T1 = s->t1;
|
|
273 state->T2 = s->t2;
|
|
274 state->T3 = s->t3;
|
|
275 state->reset = s->reset;
|
|
276 ff_jpegls_reset_coding_parameters(state, 0);
|
|
277 ff_jpegls_init_state(state);
|
|
278
|
|
279 if(s->bits <= 8)
|
|
280 shift = point_transform + (8 - s->bits);
|
|
281 else
|
|
282 shift = point_transform + (16 - s->bits);
|
|
283
|
|
284 // av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
|
|
285 // av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
|
|
286 if(ilv == 0) { /* separate planes */
|
|
287 off = s->cur_scan - 1;
|
|
288 stride = (s->nb_components > 1) ? 3 : 1;
|
|
289 width = s->width * stride;
|
|
290 cur += off;
|
|
291 for(i = 0; i < s->height; i++) {
|
|
292 if(s->bits <= 8){
|
|
293 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
|
|
294 t = last[0];
|
|
295 }else{
|
|
296 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
|
|
297 t = *((uint16_t*)last);
|
|
298 }
|
|
299 last = cur;
|
|
300 cur += s->picture.linesize[0];
|
|
301
|
|
302 if (s->restart_interval && !--s->restart_count) {
|
|
303 align_get_bits(&s->gb);
|
|
304 skip_bits(&s->gb, 16); /* skip RSTn */
|
|
305 }
|
|
306 }
|
|
307 } else if(ilv == 1) { /* line interleaving */
|
|
308 int j;
|
|
309 int Rc[3] = {0, 0, 0};
|
|
310 memset(cur, 0, s->picture.linesize[0]);
|
|
311 width = s->width * 3;
|
|
312 for(i = 0; i < s->height; i++) {
|
|
313 for(j = 0; j < 3; j++) {
|
|
314 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
|
|
315 Rc[j] = last[j];
|
|
316
|
|
317 if (s->restart_interval && !--s->restart_count) {
|
|
318 align_get_bits(&s->gb);
|
|
319 skip_bits(&s->gb, 16); /* skip RSTn */
|
|
320 }
|
|
321 }
|
|
322 last = cur;
|
|
323 cur += s->picture.linesize[0];
|
|
324 }
|
|
325 } else if(ilv == 2) { /* sample interleaving */
|
|
326 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
|
|
327 av_free(state);
|
|
328 av_free(zero);
|
|
329 return -1;
|
|
330 }
|
|
331
|
|
332 if(shift){ /* we need to do point transform or normalize samples */
|
|
333 int x, w;
|
|
334
|
|
335 w = s->width * s->nb_components;
|
|
336
|
|
337 if(s->bits <= 8){
|
|
338 uint8_t *src = s->picture.data[0];
|
|
339
|
|
340 for(i = 0; i < s->height; i++){
|
|
341 for(x = off; x < w; x+= stride){
|
|
342 src[x] <<= shift;
|
|
343 }
|
|
344 src += s->picture.linesize[0];
|
|
345 }
|
|
346 }else{
|
|
347 uint16_t *src = (uint16_t*) s->picture.data[0];
|
|
348
|
|
349 for(i = 0; i < s->height; i++){
|
|
350 for(x = 0; x < w; x++){
|
|
351 src[x] <<= shift;
|
|
352 }
|
|
353 src += s->picture.linesize[0]/2;
|
|
354 }
|
|
355 }
|
|
356 }
|
|
357 av_free(state);
|
|
358 av_free(zero);
|
|
359
|
|
360 return 0;
|
|
361 }
|