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 int tid, wt, maxtab,
i, j;
54
57
58 switch (id) {
59 case 1:
62
68
71 s->maxval,
s->t1,
s->t2,
s->t3,
s->reset);
72 }
73
74 // ff_jpegls_reset_coding_parameters(s, 0);
75 //FIXME quant table?
76 break;
77 case 2:
79 case 3:
82
85
89 }
90
92 maxtab = 255;
93 else if ((5 + wt*(
s->maxval+1)) < 65535)
95 else
96 maxtab = 65530/wt - 1;
97
99 av_log(
s->avctx,
AV_LOG_DEBUG,
"LSE palette %d tid:%d wt:%d maxtab:%d\n",
id, tid, wt, maxtab);
100 }
101 if (maxtab >= 256) {
104 }
105 maxtab =
FFMIN(maxtab, (
len - 5) / wt +
s->palette_index);
106
107 if (
s->palette_index > maxtab)
109
112 uint32_t *pal = (uint32_t *)
s->picture_ptr->data[1];
114
115 if (
s->avctx->bits_per_raw_sample > 0 &&
s->avctx->bits_per_raw_sample < 8) {
116 maxtab =
FFMIN(maxtab, (1<<
s->avctx->bits_per_raw_sample)-1);
117 shift = 8 -
s->avctx->bits_per_raw_sample;
118 }
119
121 if (!pal) {
122 if (
s->force_pal8 > 1)
124 return 1;
125 }
126
127 for (
i=
s->palette_index;
i<=maxtab;
i++) {
129 pal[k] = wt < 4 ? 0xFF000000 : 0;
130 for (j=0; j<wt; j++) {
131 pal[k] |=
get_bits(&
s->gb, 8) << (8*(wt-j-1));
132 }
133 }
134 s->palette_index =
i;
135 }
136 break;
137 case 4:
140 default:
143 }
144 ff_dlog(
s->avctx,
"ID=%i, T=%i,%i,%i\n",
id,
s->t1,
s->t2,
s->t3);
145
146 return 0;
147 }
148
149 /**
150 * Get context-dependent Golomb code, decode it and update context
151 */
153 {
155
156 for (k = 0; ((unsigned)
state->N[Q] << k) <
state->A[Q]; k++)
157 ;
158
159 #ifdef JLS_BROKEN
161 return -1;
162 #endif
164
165 /* decode mapped error */
168 else
170
171 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
174
176
178 }
179
180 /**
181 * Get Golomb code, decode it and update state for run termination
182 */
184 int RItype, int limit_add)
185 {
187 int Q = 365 + RItype;
188
190 if (RItype)
192
193 for (k = 0; ((unsigned)
state->N[Q] << k) <
temp; k++)
194 ;
195
196 #ifdef JLS_BROKEN
198 return -1;
199 #endif
203 return -0x10000;
204
205 /* decode mapped error */
207 if (!k && (RItype ||
ret) && (2 *
state->B[Q] <
state->N[Q]))
210
214 } else {
216 }
217
219 return -0x10000;
220 /* update state */
224
226 }
227
228 /**
229 * Decode one line of image
230 */
232 void *last,
void *dst,
int last2,
int w,
234 {
236 int Ra, Rb, Rc, Rd;
237 int D0, D1, D2;
238
241
244
245 /* compute gradients */
246 Ra = x ?
R(dst, x -
stride) :
R(last, x);
248 Rc = x ?
R(last, x -
stride) : last2;
250 D0 = Rd - Rb;
251 D1 = Rb - Rc;
252 D2 = Rc - Ra;
253 /* run mode */
258 int RItype;
259
260 /* decode full runs while available */
266 for (
i = 0;
i <
r;
i++) {
269 }
270 /* if EOL reached, we stop decoding */
272 return 0;
276 return 0;
277 }
278 /* decode aborted run */
284 }
285 for (
i = 0;
i <
r;
i++) {
288 }
289
294 }
295
296 /* decode run termination value */
298 RItype = (
FFABS(Ra - Rb) <=
state->near) ? 1 : 0;
303
304 if (
state->near && RItype) {
306 } else {
307 if (Rb < Ra)
309 else
311 }
312 } else { /* regular mode */
314
319
322 sign = 1;
323 } else {
324 sign = 0;
325 }
326
327 if (sign) {
330 } else {
333 }
334
335 /* we have to do something more for near-lossless coding */
337 }
339 if (pred < -state->near)
344 }
345
349 }
350
351 return 0;
352 }
353
355 int point_transform, int ilv)
356 {
358 uint8_t *
zero, *last, *cur;
361 int decoded_height = 0;
362
368 }
373 cur =
s->picture_ptr->data[0];
374
375 /* initialize JPEG-LS state from JPEG parameters */
378 state->bpp = (
s->bits < 2) ? 2 :
s->bits;
379 state->maxval =
s->maxval;
386
388 shift = point_transform + (8 -
s->bits);
389 else
390 shift = point_transform + (16 -
s->bits);
391
394 goto end;
395 }
396
399 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
400 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
405 ilv, point_transform,
s->bits,
s->cur_scan);
406 }
409 goto end;
410 }
411 if (ilv == 0) { /* separate planes */
412 if (
s->cur_scan >
s->nb_components) {
414 goto end;
415 }
416 stride = (
s->nb_components > 1) ? 3 : 1;
419 cur += off;
420 for (
i = 0;
i <
s->height;
i++) {
424 t = last[0];
425 } else {
427 t = *((uint16_t *)last);
428 }
430 break;
431 last = cur;
432 cur +=
s->picture_ptr->linesize[0];
433
434 if (
s->restart_interval && !--
s->restart_count) {
437 }
438 }
440 } else if (ilv == 1) { /* line interleaving */
441 int j;
442 int Rc[3] = { 0, 0, 0 };
443 stride = (
s->nb_components > 1) ? 3 : 1;
444 memset(cur, 0,
s->picture_ptr->linesize[0]);
446 for (
i = 0;
i <
s->height;
i++) {
448 for (j = 0; j <
stride; j++) {
452 break;
453 Rc[j] = last[j];
454
455 if (
s->restart_interval && !--
s->restart_count) {
458 }
459 }
461 break;
462 last = cur;
463 cur +=
s->picture_ptr->linesize[0];
464 }
466 } else if (ilv == 2) { /* sample interleaving */
469 goto end;
470 } else { /* unknown interleaving */
473 goto end;
474 }
475
476 if (
s->xfrm &&
s->nb_components == 3) {
478
479 w =
s->width *
s->nb_components;
480
482 uint8_t *
src =
s->picture_ptr->data[0];
483
484 for (
i = 0;
i <
s->height;
i++) {
486 case 1:
487 for (x = off; x + 2 <
w; x += 3) {
488 src[x ] +=
src[x+1] + 128;
489 src[x+2] +=
src[x+1] + 128;
490 }
491 break;
492 case 2:
493 for (x = off; x + 2 <
w; x += 3) {
494 src[x ] +=
src[x+1] + 128;
495 src[x+2] += ((
src[x ] +
src[x+1])>>1) + 128;
496 }
497 break;
498 case 3:
499 for (x = off; x + 2 <
w; x += 3) {
500 int g =
src[x+0] - ((
src[x+2]+
src[x+1])>>2) + 64;
504 }
505 break;
506 case 4:
507 for (x = off; x + 2 <
w; x += 3) {
508 int r =
src[x+0] - (( 359 * (
src[x+2]-128) + 490) >> 8);
509 int g =
src[x+0] - (( 88 * (
src[x+1]-128) - 183 * (
src[x+2]-128) + 30) >> 8);
510 int b =
src[x+0] + ((454 * (
src[x+1]-128) + 574) >> 8);
514 }
515 break;
516 }
517 src +=
s->picture_ptr->linesize[0];
518 }
519 }else
521 }
522
523 if (
shift) {
/* we need to do point transform or normalize samples */
525
526 w =
s->width *
s->nb_components;
527
529 uint8_t *
src =
s->picture_ptr->data[0];
530
531 for (
i = 0;
i < decoded_height;
i++) {
532 for (x = off; x <
w; x +=
stride)
534 src +=
s->picture_ptr->linesize[0];
535 }
536 } else {
537 uint16_t *
src = (uint16_t *)
s->picture_ptr->data[0];
538
539 for (
i = 0;
i < decoded_height;
i++) {
540 for (x = 0; x <
w; x++)
542 src +=
s->picture_ptr->linesize[0] / 2;
543 }
544 }
545 }
546
547 end:
549
551 }
552
564 };