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
25 * JPEG-LS decoder.
26 */
27
36
37 /*
38 * Uncomment this to significantly speed up decoding of broken JPEG-LS
39 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
40 *
41 * There is no Golomb code with length >= 32 bits possible, so check and
42 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
43 * on this errors.
44 */
45 //#define JLS_BROKEN
46
47 /**
48 * Decode LSE block with initialization parameters
49 */
51 {
53
54 skip_bits(&s->
gb, 16);
/* length: FIXME: verify field validity */
56
57 switch (id) {
58 case 1:
64
65 // ff_jpegls_reset_coding_parameters(s, 0);
66 //FIXME quant table?
67 break;
68 case 2:
69 case 3:
72 case 4:
75 default:
78 }
80
81 return 0;
82 }
83
84 /**
85 * Get context-dependent Golomb code, decode it and update context
86 */
88 {
90
91 for (k = 0; (state->
N[Q] << k) < state->
A[Q]; k++)
92 ;
93
94 #ifdef JLS_BROKEN
96 return -1;
97 #endif
99
100 /* decode mapped error */
101 if (ret & 1)
102 ret = -(ret + 1 >> 1);
103 else
104 ret >>= 1;
105
106 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
107 if (!state->
near && !k && (2 * state->
B[Q] <= -state->
N[Q]))
108 ret = -(ret + 1);
109
111
113 }
114
115 /**
116 * Get Golomb code, decode it and update state for run termination
117 */
119 int RItype, int limit_add)
120 {
122 int Q = 365 + RItype;
123
125 if (RItype)
126 temp += state->
N[Q] >> 1;
127
128 for (k = 0; (state->
N[Q] << k) < temp; k++)
129 ;
130
131 #ifdef JLS_BROKEN
133 return -1;
134 #endif
137
138 /* decode mapped error */
139 map = 0;
140 if (!k && (RItype || ret) && (2 * state->
B[Q] < state->
N[Q]))
141 map = 1;
142 ret += RItype + map;
143
144 if (ret & 1) {
145 ret = map - (ret + 1 >> 1);
147 } else {
148 ret = ret >> 1;
149 }
150
151 /* update state */
152 state->
A[Q] +=
FFABS(ret) - RItype;
155
157 }
158
159 /**
160 * Decode one line of image
161 */
163 void *last, void *dst, int last2, int w,
165 {
166 int i, x = 0;
167 int Ra, Rb, Rc, Rd;
168 int D0, D1, D2;
169
170 while (x < w) {
172
173 /* compute gradients */
174 Ra = x ?
R(dst, x - stride) :
R(last, x);
176 Rc = x ?
R(last, x - stride) : last2;
177 Rd = (x >= w -
stride) ?
R(last, x) :
R(last, x + stride);
178 D0 = Rd - Rb;
179 D1 = Rb - Rc;
180 D2 = Rc - Ra;
181 /* run mode */
186 int RItype;
187
188 /* decode full runs while available */
192 if (x + r * stride > w)
193 r = (w - x) / stride;
194 for (i = 0; i <
r; i++) {
197 }
198 /* if EOL reached, we stop decoding */
200 return;
203 if (x + stride > w)
204 return;
205 }
206 /* decode aborted run */
208 if (r)
210 if (x + r * stride > w) {
211 r = (w - x) / stride;
212 }
213 for (i = 0; i <
r; i++) {
216 }
217
218 /* decode run termination value */
220 RItype = (
FFABS(Ra - Rb) <= state->
near) ? 1 : 0;
225
226 if (state->
near && RItype) {
227 pred = Ra + err;
228 } else {
229 if (Rb < Ra)
230 pred = Rb - err;
231 else
232 pred = Rb + err;
233 }
234 } else { /* regular mode */
235 int context, sign;
236
240 pred =
mid_pred(Ra, Ra + Rb - Rc, Rb);
241
242 if (context < 0) {
243 context = -context;
244 sign = 1;
245 } else {
246 sign = 0;
247 }
248
249 if (sign) {
250 pred = av_clip(pred - state->
C[context], 0, state->
maxval);
252 } else {
253 pred = av_clip(pred + state->
C[context], 0, state->
maxval);
255 }
256
257 /* we have to do something more for near-lossless coding */
258 pred += err;
259 }
261 if (pred < -state->near)
265 pred = av_clip(pred, 0, state->
maxval);
266 }
267
271 }
272 }
273
275 int point_transform, int ilv)
276 {
281
285
287 /* initialize JPEG-LS state from JPEG parameters */
297
299 shift = point_transform + (8 - s->
bits);
300 else
301 shift = point_transform + (16 - s->
bits);
302
305 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
306 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
308 state->
T1, state->
T2, state->
T3,
312 }
313 if (ilv == 0) { /* separate planes */
317 }
322 for (i = 0; i < s->
height; i++) {
325 t = last[0];
326 } else {
328 t = *((uint16_t *)last);
329 }
330 last = cur;
332
336 }
337 }
338 } else if (ilv == 1) { /* line interleaving */
339 int j;
340 int Rc[3] = { 0, 0, 0 };
344 for (i = 0; i < s->
height; i++) {
345 for (j = 0; j <
stride; j++) {
347 Rc[j],
width, stride, j, 8);
348 Rc[j] = last[j];
349
353 }
354 }
355 last = cur;
357 }
358 } else if (ilv == 2) { /* sample interleaving */
362 }
363
364 if (shift) { /* we need to do point transform or normalize samples */
365 int x, w;
366
368
371
372 for (i = 0; i < s->
height; i++) {
373 for (x = off; x < w; x +=
stride)
374 src[x] <<= shift;
376 }
377 } else {
379
381 for (x = 0; x < w; x++)
382 src[x] <<= shift;
384 }
385 }
386 }
387
391
393 }
394
405 };