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
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 * Decode LSE block with initialization parameters
50 */
52 {
54 int tid, wt, maxtab,
i, j;
55
58
59 switch (id) {
60 case 1:
63
69
72 s->maxval,
s->t1,
s->t2,
s->t3,
s->reset);
73 }
74
75 // ff_jpegls_reset_coding_parameters(s, 0);
76 //FIXME quant table?
77 break;
78 case 2:
80 case 3:
83
86
90 }
91
93 maxtab = 255;
94 else if ((5 + wt*(
s->maxval+1)) < 65535)
96 else
97 maxtab = 65530/wt - 1;
98
100 av_log(
s->avctx,
AV_LOG_DEBUG,
"LSE palette %d tid:%d wt:%d maxtab:%d\n",
id, tid, wt, maxtab);
101 }
102 if (maxtab >= 256) {
105 }
106 maxtab =
FFMIN(maxtab, (
len - 5) / wt +
s->palette_index);
107
108 if (
s->palette_index > maxtab)
110
113 uint32_t *pal = (uint32_t *)
s->picture_ptr->data[1];
115
116 if (
s->avctx->bits_per_raw_sample > 0 &&
s->avctx->bits_per_raw_sample < 8) {
117 maxtab =
FFMIN(maxtab, (1<<
s->avctx->bits_per_raw_sample)-1);
118 shift = 8 -
s->avctx->bits_per_raw_sample;
119 }
120
122 if (!pal) {
123 if (
s->force_pal8 > 1)
125 return 1;
126 }
127
128 for (
i=
s->palette_index;
i<=maxtab;
i++) {
130 pal[k] = wt < 4 ? 0xFF000000 : 0;
131 for (j=0; j<wt; j++) {
132 pal[k] |=
get_bits(&
s->gb, 8) << (8*(wt-j-1));
133 }
134 }
135 s->palette_index =
i;
136 }
137 break;
138 case 4:
141 default:
144 }
145 ff_dlog(
s->avctx,
"ID=%i, T=%i,%i,%i\n",
id,
s->t1,
s->t2,
s->t3);
146
147 return 0;
148 }
149
150 /**
151 * Get context-dependent Golomb code, decode it and update context
152 */
154 {
156
157 for (k = 0; ((unsigned)
state->N[
Q] << k) <
state->A[
Q]; k++)
158 ;
159
160 #ifdef JLS_BROKEN
162 return -1;
163 #endif
165
166 /* decode mapped error */
169 else
171
172 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
175
177
179 }
180
181 /**
182 * Get Golomb code, decode it and update state for run termination
183 */
185 int RItype, int limit_add)
186 {
188 int Q = 365 + RItype;
189
191 if (RItype)
193
194 for (k = 0; ((unsigned)
state->N[
Q] << k) <
temp; k++)
195 ;
196
197 #ifdef JLS_BROKEN
199 return -1;
200 #endif
204 return -0x10000;
205
206 /* decode mapped error */
211
215 } else {
217 }
218
220 return -0x10000;
221 /* update state */
225
227 }
228
229 /**
230 * Decode one line of image
231 */
233 void *last,
void *
dst,
int last2,
int w,
235 {
237 int Ra, Rb, Rc, Rd;
238 int D0, D1, D2;
239
242
245
246 /* compute gradients */
249 Rc = x ?
R(last, x -
stride) : last2;
251 D0 = Rd - Rb;
252 D1 = Rb - Rc;
253 D2 = Rc - Ra;
254 /* run mode */
259 int RItype;
260
261 /* decode full runs while available */
267 for (
i = 0;
i <
r;
i++) {
270 }
271 /* if EOL reached, we stop decoding */
273 return 0;
277 return 0;
278 }
279 /* decode aborted run */
285 }
286 for (
i = 0;
i <
r;
i++) {
289 }
290
295 }
296
297 /* decode run termination value */
299 RItype = (
FFABS(Ra - Rb) <=
state->near) ? 1 : 0;
304
305 if (
state->near && RItype) {
307 } else {
308 if (Rb < Ra)
310 else
312 }
313 } else { /* regular mode */
315
320
323 sign = 1;
324 } else {
325 sign = 0;
326 }
327
328 if (sign) {
331 } else {
334 }
335
336 /* we have to do something more for near-lossless coding */
338 }
340 if (pred < -state->near)
345 }
346
350 }
351
352 return 0;
353 }
354
356 int point_transform, int ilv)
357 {
359 uint8_t *
zero, *last, *cur;
362 int decoded_height = 0;
363
369 }
374 cur =
s->picture_ptr->data[0];
375
376 /* initialize JPEG-LS state from JPEG parameters */
379 state->bpp = (
s->bits < 2) ? 2 :
s->bits;
380 state->maxval =
s->maxval;
386
387 /* Testing parameters here, we cannot test in LSE or SOF because
388 * these interdepend and are allowed in either order
389 */
396 goto end;
397 }
398
400
402 shift = point_transform + (8 -
s->bits);
403 else
404 shift = point_transform + (16 -
s->bits);
405
408 goto end;
409 }
410
413 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
414 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
419 ilv, point_transform,
s->bits,
s->cur_scan);
420 }
423 goto end;
424 }
425 if (ilv == 0) { /* separate planes */
426 if (
s->cur_scan >
s->nb_components) {
428 goto end;
429 }
430 stride = (
s->nb_components > 1) ? 3 : 1;
433 cur += off;
434 for (
i = 0;
i <
s->height;
i++) {
438 t = last[0];
439 } else {
441 t = *((uint16_t *)last);
442 }
444 break;
445 last = cur;
446 cur +=
s->picture_ptr->linesize[0];
447
448 if (
s->restart_interval && !--
s->restart_count) {
451 }
452 }
454 } else if (ilv == 1) { /* line interleaving */
455 int j;
456 int Rc[3] = { 0, 0, 0 };
457 stride = (
s->nb_components > 1) ? 3 : 1;
458 memset(cur, 0,
s->picture_ptr->linesize[0]);
460 for (
i = 0;
i <
s->height;
i++) {
462 for (j = 0; j <
stride; j++) {
466 break;
467 Rc[j] = last[j];
468
469 if (
s->restart_interval && !--
s->restart_count) {
472 }
473 }
475 break;
476 last = cur;
477 cur +=
s->picture_ptr->linesize[0];
478 }
480 } else if (ilv == 2) { /* sample interleaving */
483 goto end;
484 } else { /* unknown interleaving */
487 goto end;
488 }
489
490 if (
s->xfrm &&
s->nb_components == 3) {
492
493 w =
s->width *
s->nb_components;
494
496 uint8_t *
src =
s->picture_ptr->data[0];
497
498 for (
i = 0;
i <
s->height;
i++) {
500 case 1:
501 for (x = off; x + 2 <
w; x += 3) {
502 src[x ] +=
src[x+1] + 128;
503 src[x+2] +=
src[x+1] + 128;
504 }
505 break;
506 case 2:
507 for (x = off; x + 2 <
w; x += 3) {
508 src[x ] +=
src[x+1] + 128;
509 src[x+2] += ((
src[x ] +
src[x+1])>>1) + 128;
510 }
511 break;
512 case 3:
513 for (x = off; x + 2 <
w; x += 3) {
514 int g =
src[x+0] - ((
src[x+2]+
src[x+1])>>2) + 64;
518 }
519 break;
520 case 4:
521 for (x = off; x + 2 <
w; x += 3) {
522 int r =
src[x+0] - (( 359 * (
src[x+2]-128) + 490) >> 8);
523 int g =
src[x+0] - (( 88 * (
src[x+1]-128) - 183 * (
src[x+2]-128) + 30) >> 8);
524 int b =
src[x+0] + ((454 * (
src[x+1]-128) + 574) >> 8);
528 }
529 break;
530 }
531 src +=
s->picture_ptr->linesize[0];
532 }
533 }else
535 }
536
537 if (
shift) {
/* we need to do point transform or normalize samples */
539
540 w =
s->width *
s->nb_components;
541
543 uint8_t *
src =
s->picture_ptr->data[0];
544
545 for (
i = 0;
i < decoded_height;
i++) {
546 for (x = off; x <
w; x +=
stride)
548 src +=
s->picture_ptr->linesize[0];
549 }
550 } else {
551 uint16_t *
src = (uint16_t *)
s->picture_ptr->data[0];
552
553 for (
i = 0;
i < decoded_height;
i++) {
554 for (x = 0; x <
w; x++)
556 src +=
s->picture_ptr->linesize[0] / 2;
557 }
558 }
559 }
560
561 end:
563
565 }
566
578 };