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 /*
39 * Uncomment this to significantly speed up decoding of broken JPEG-LS
40 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
41 *
42 * There is no Golomb code with length >= 32 bits possible, so check and
43 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
44 * on this errors.
45 */
46 //#define JLS_BROKEN
47
48
49 /**
50 * Decode LSE block with initialization parameters
51 */
53 {
55
56 skip_bits(&s->
gb, 16);
/* length: FIXME: verify field validity */
58
59 switch(id){
60 case 1:
66
67 // ff_jpegls_reset_coding_parameters(s, 0);
68 //FIXME quant table?
69 break;
70 case 2:
71 case 3:
73 return -1;
74 case 4:
76 return -1;
77 default:
79 return -1;
80 }
82
83 return 0;
84 }
85
86 /**
87 * Get context-dependent Golomb code, decode it and update context
88 */
90 int k, ret;
91
92 for(k = 0; (state->
N[Q] << k) < state->
A[Q]; k++);
93
94 #ifdef JLS_BROKEN
96 #endif
98
99 /* decode mapped error */
100 if(ret & 1)
101 ret = -((ret + 1) >> 1);
102 else
103 ret >>= 1;
104
105 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
106 if(!state->
near && !k && (2 * state->
B[Q] <= -state->
N[Q]))
107 ret = -(ret + 1);
108
110
111 return ret;
112 }
113
114 /**
115 * Get Golomb code, decode it and update state for run termination
116 */
118 int k, ret,
temp, map;
119 int Q = 365 + RItype;
120
122 if(RItype)
123 temp += state->
N[Q] >> 1;
124
125 for(k = 0; (state->
N[Q] << k) < temp; k++);
126
127 #ifdef JLS_BROKEN
129 #endif
131
132 /* decode mapped error */
133 map = 0;
134 if(!k && (RItype || ret) && (2 * state->
B[Q] < state->
N[Q]))
135 map = 1;
136 ret += RItype + map;
137
138 if(ret & 1){
139 ret = map - ((ret + 1) >> 1);
141 } else {
142 ret = ret >> 1;
143 }
144
145 /* update state */
146 state->
A[Q] +=
FFABS(ret) - RItype;
149
150 return ret;
151 }
152
153 /**
154 * Decode one line of image
155 */
157 int i, x = 0;
158 int Ra, Rb, Rc, Rd;
159 int D0, D1, D2;
160
161 while(x < w) {
163
164 /* compute gradients */
165 Ra = x ?
R(dst, x - stride) :
R(last, x);
167 Rc = x ?
R(last, x - stride) : last2;
168 Rd = (x >= w -
stride) ?
R(last, x) :
R(last, x + stride);
169 D0 = Rd - Rb;
170 D1 = Rb - Rc;
171 D2 = Rc - Ra;
172 /* run mode */
175 int RItype;
176
177 /* decode full runs while available */
181 if(x + r * stride > w) {
182 r = (w - x) / stride;
183 }
184 for(i = 0; i <
r; i++) {
187 }
188 /* if EOL reached, we stop decoding */
190 return;
193 if(x + stride > w)
194 return;
195 }
196 /* decode aborted run */
198 if(r)
200 if(x + r * stride > w) {
201 r = (w - x) / stride;
202 }
203 for(i = 0; i <
r; i++) {
206 }
207
208 /* decode run termination value */
210 RItype = (
FFABS(Ra - Rb) <= state->
near) ? 1 : 0;
214
215 if(state->
near && RItype){
216 pred = Ra + err;
217 } else {
218 if(Rb < Ra)
219 pred = Rb - err;
220 else
221 pred = Rb + err;
222 }
223 } else { /* regular mode */
224 int context, sign;
225
227 pred =
mid_pred(Ra, Ra + Rb - Rc, Rb);
228
229 if(context < 0){
230 context = -context;
231 sign = 1;
232 }else{
233 sign = 0;
234 }
235
236 if(sign){
237 pred = av_clip(pred - state->
C[context], 0, state->
maxval);
239 } else {
240 pred = av_clip(pred + state->
C[context], 0, state->
maxval);
242 }
243
244 /* we have to do something more for near-lossless coding */
245 pred += err;
246 }
248 if(pred < -state->near)
252 pred = av_clip(pred, 0, state->
maxval);
253 }
254
258 }
259 }
260
266
270
272 /* initialize JPEG-LS state from JPEG parameters */
282
284 shift = point_transform + (8 - s->
bits);
285 else
286 shift = point_transform + (16 - s->
bits);
287
289 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",
291 state->
T1, state->
T2, state->
T3,
295 }
296 if(ilv == 0) { /* separate planes */
301 for(i = 0; i < s->
height; i++) {
304 t = last[0];
305 }else{
307 t = *((uint16_t*)last);
308 }
309 last = cur;
311
315 }
316 }
317 } else if(ilv == 1) { /* line interleaving */
318 int j;
319 int Rc[3] = {0, 0, 0};
323 for(i = 0; i < s->
height; i++) {
324 for(j = 0; j <
stride; j++) {
326 Rc[j] = last[j];
327
331 }
332 }
333 last = cur;
335 }
336 } else if(ilv == 2) { /* sample interleaving */
340 return -1;
341 }
342
343 if(shift){ /* we need to do point transform or normalize samples */
344 int x, w;
345
347
350
351 for(i = 0; i < s->
height; i++){
352 for(x = off; x < w; x+=
stride){
354 }
356 }
357 }else{
359
360 for(i = 0; i < s->
height; i++){
361 for(x = 0; x < w; x++){
363 }
365 }
366 }
367 }
370
371 return 0;
372 }
373
374
385 };