FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeglsdec.c
Date: 2026-03-07 22:03:05
Exec Total Coverage
Lines: 217 321 67.6%
Functions: 5 5 100.0%
Branches: 145 242 59.9%

Line Branch Exec Source
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
28 #include "libavutil/mem.h"
29 #include "avcodec.h"
30 #include "codec_internal.h"
31 #include "get_bits.h"
32 #include "golomb.h"
33 #include "mathops.h"
34 #include "mjpegdec.h"
35 #include "jpegls.h"
36 #include "jpeglsdec.h"
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 */
51 16 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
52 {
53 int id;
54 int tid, wt, maxtab, i, j;
55
56 16 int len = bytestream2_get_be16(&s->gB);
57 16 id = bytestream2_get_byte(&s->gB);
58
59
1/5
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
16 switch (id) {
60 case 1:
61 if (len < 13)
62 return AVERROR_INVALIDDATA;
63
64 s->maxval = bytestream2_get_be16u(&s->gB);
65 s->t1 = bytestream2_get_be16u(&s->gB);
66 s->t2 = bytestream2_get_be16u(&s->gB);
67 s->t3 = bytestream2_get_be16u(&s->gB);
68 s->reset = bytestream2_get_be16u(&s->gB);
69
70 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
71 av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n",
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 16 case 2:
79 16 s->palette_index = 0;
80 16 case 3:
81 16 tid= bytestream2_get_byte(&s->gB);
82 16 wt = bytestream2_get_byte(&s->gB);
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (len < 5)
85 return AVERROR_INVALIDDATA;
86
87
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (wt < 1 || wt > MAX_COMPONENTS) {
88 avpriv_request_sample(s->avctx, "wt %d", wt);
89 return AVERROR_PATCHWELCOME;
90 }
91
92
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!s->maxval)
93 16 maxtab = 255;
94 else if ((5 + wt*(s->maxval+1)) < 65535)
95 maxtab = s->maxval;
96 else
97 maxtab = 65530/wt - 1;
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
100 av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab);
101 }
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (maxtab >= 256) {
103 avpriv_request_sample(s->avctx, ">8bit palette");
104 return AVERROR_PATCHWELCOME;
105 }
106 16 maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->palette_index > maxtab)
109 return AVERROR_INVALIDDATA;
110
111
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) &&
112
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 (s->picture_ptr->format == AV_PIX_FMT_GRAY8 || s->picture_ptr->format == AV_PIX_FMT_PAL8)) {
113 16 uint32_t *pal = (uint32_t *)s->picture_ptr->data[1];
114 16 int shift = 0;
115
116
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (s->avctx->bits_per_raw_sample > 0 && s->avctx->bits_per_raw_sample < 8) {
117
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 maxtab = FFMIN(maxtab, (1<<s->avctx->bits_per_raw_sample)-1);
118 16 shift = 8 - s->avctx->bits_per_raw_sample;
119 }
120
121 16 s->force_pal8++;
122
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (!pal) {
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (s->force_pal8 > 1)
124 return AVERROR_INVALIDDATA;
125 8 return 1;
126 }
127
128
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 8 times.
352 for (i=s->palette_index; i<=maxtab; i++) {
129 344 uint8_t k = i << shift;
130
1/2
✓ Branch 0 taken 344 times.
✗ Branch 1 not taken.
344 pal[k] = wt < 4 ? 0xFF000000 : 0;
131
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 344 times.
1376 for (j=0; j<wt; j++) {
132 1032 pal[k] |= bytestream2_get_byte(&s->gB) << (8*(wt-j-1));
133 }
134 }
135 8 s->palette_index = i;
136 }
137 8 break;
138 case 4:
139 avpriv_request_sample(s->avctx, "oversize image");
140 return AVERROR(ENOSYS);
141 default:
142 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
143 return AVERROR_INVALIDDATA;
144 }
145 ff_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
146
147 8 return 0;
148 }
149
150 /**
151 * Get context-dependent Golomb code, decode it and update context
152 */
153 46641676 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
154 {
155 int k, ret;
156
157
2/2
✓ Branch 0 taken 120956970 times.
✓ Branch 1 taken 46641676 times.
167598646 for (k = 0; ((unsigned)state->N[Q] << k) < state->A[Q]; k++)
158 ;
159
160 #ifdef JLS_BROKEN
161 if (!show_bits_long(gb, 32))
162 return -1;
163 #endif
164 46641676 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
165
166 /* decode mapped error */
167
2/2
✓ Branch 0 taken 23896660 times.
✓ Branch 1 taken 22745016 times.
46641676 if (ret & 1)
168 23896660 ret = -(ret + 1 >> 1);
169 else
170 22745016 ret >>= 1;
171
172 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
173
5/6
✓ Branch 0 taken 46641676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2699415 times.
✓ Branch 3 taken 43942261 times.
✓ Branch 4 taken 840758 times.
✓ Branch 5 taken 1858657 times.
46641676 if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
174 840758 ret = -(ret + 1);
175
176 46641676 ret = ff_jpegls_update_state_regular(state, Q, ret);
177
178 46641676 return ret;
179 }
180
181 /**
182 * Get Golomb code, decode it and update state for run termination
183 */
184 784769 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
185 int RItype, int limit_add)
186 {
187 int k, ret, temp, map;
188 784769 int Q = 365 + RItype;
189
190 784769 temp = state->A[Q];
191
2/2
✓ Branch 0 taken 510340 times.
✓ Branch 1 taken 274429 times.
784769 if (RItype)
192 510340 temp += state->N[Q] >> 1;
193
194
2/2
✓ Branch 0 taken 1887871 times.
✓ Branch 1 taken 784769 times.
2672640 for (k = 0; ((unsigned)state->N[Q] << k) < temp; k++)
195 ;
196
197 #ifdef JLS_BROKEN
198 if (!show_bits_long(gb, 32))
199 return -1;
200 #endif
201 784769 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
202 state->qbpp);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784769 times.
784769 if (ret < 0)
204 return -0x10000;
205
206 /* decode mapped error */
207 784769 map = 0;
208
8/8
✓ Branch 0 taken 153938 times.
✓ Branch 1 taken 630831 times.
✓ Branch 2 taken 26894 times.
✓ Branch 3 taken 127044 times.
✓ Branch 4 taken 2304 times.
✓ Branch 5 taken 24590 times.
✓ Branch 6 taken 60548 times.
✓ Branch 7 taken 68800 times.
784769 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
209 60548 map = 1;
210 784769 ret += RItype + map;
211
212
2/2
✓ Branch 0 taken 352900 times.
✓ Branch 1 taken 431869 times.
784769 if (ret & 1) {
213 352900 ret = map - (ret + 1 >> 1);
214 352900 state->B[Q]++;
215 } else {
216 431869 ret = ret >> 1;
217 }
218
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784769 times.
784769 if (FFABS(ret) > 0xFFFF)
220 return -0x10000;
221 /* update state */
222 784769 state->A[Q] += FFABS(ret) - RItype;
223 784769 ret *= state->twonear;
224 784769 ff_jpegls_downscale_state(state, Q);
225
226 784769 return ret;
227 }
228
229 /**
230 * Decode one line of image
231 */
232 145002 static inline int ls_decode_line(JLSState *state, MJpegDecodeContext *s,
233 void *last, void *dst, int last2, int w,
234 int stride, int comp, int bits)
235 {
236 145002 int i, x = 0;
237 int Ra, Rb, Rc, Rd;
238 int D0, D1, D2;
239
240
2/2
✓ Branch 0 taken 47434440 times.
✓ Branch 1 taken 137007 times.
47571447 while (x < w) {
241 int err, pred;
242
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47434440 times.
47434440 if (get_bits_left(&s->gb) <= 0)
244 return AVERROR_INVALIDDATA;
245
246 /* compute gradients */
247
4/6
✓ Branch 0 taken 47289438 times.
✓ Branch 1 taken 145002 times.
✓ Branch 2 taken 47289438 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 145002 times.
✗ Branch 5 not taken.
47434440 Ra = x ? R(dst, x - stride) : R(last, x);
248
1/2
✓ Branch 0 taken 47434440 times.
✗ Branch 1 not taken.
47434440 Rb = R(last, x);
249
3/4
✓ Branch 0 taken 47289438 times.
✓ Branch 1 taken 145002 times.
✓ Branch 2 taken 47289438 times.
✗ Branch 3 not taken.
47434440 Rc = x ? R(last, x - stride) : last2;
250
4/6
✓ Branch 0 taken 138055 times.
✓ Branch 1 taken 47296385 times.
✓ Branch 2 taken 138055 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 47296385 times.
✗ Branch 5 not taken.
47434440 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
251 47434440 D0 = Rd - Rb;
252 47434440 D1 = Rb - Rc;
253 47434440 D2 = Rc - Ra;
254 /* run mode */
255
2/2
✓ Branch 0 taken 5935360 times.
✓ Branch 1 taken 41499080 times.
47434440 if ((FFABS(D0) <= state->near) &&
256
2/2
✓ Branch 0 taken 2323348 times.
✓ Branch 1 taken 3612012 times.
5935360 (FFABS(D1) <= state->near) &&
257
2/2
✓ Branch 0 taken 792764 times.
✓ Branch 1 taken 1530584 times.
3108117 (FFABS(D2) <= state->near)) {
258 int r;
259 int RItype;
260
261 /* decode full runs while available */
262
2/2
✓ Branch 1 taken 737915 times.
✓ Branch 2 taken 784769 times.
1522684 while (get_bits1(&s->gb)) {
263 int r;
264 737915 r = 1 << ff_log2_run[state->run_index[comp]];
265
2/2
✓ Branch 0 taken 6338 times.
✓ Branch 1 taken 731577 times.
737915 if (x + r * stride > w)
266 6338 r = (w - x) / stride;
267
2/2
✓ Branch 0 taken 2755018 times.
✓ Branch 1 taken 737915 times.
3492933 for (i = 0; i < r; i++) {
268
1/2
✓ Branch 0 taken 2755018 times.
✗ Branch 1 not taken.
2755018 W(dst, x, Ra);
269 2755018 x += stride;
270 }
271 /* if EOL reached, we stop decoding */
272
2/2
✓ Branch 0 taken 6338 times.
✓ Branch 1 taken 731577 times.
737915 if (r != 1 << ff_log2_run[state->run_index[comp]])
273 6338 return 0;
274
1/2
✓ Branch 0 taken 731577 times.
✗ Branch 1 not taken.
731577 if (state->run_index[comp] < 31)
275 731577 state->run_index[comp]++;
276
2/2
✓ Branch 0 taken 1657 times.
✓ Branch 1 taken 729920 times.
731577 if (x + stride > w)
277 1657 return 0;
278 }
279 /* decode aborted run */
280 784769 r = ff_log2_run[state->run_index[comp]];
281
2/2
✓ Branch 0 taken 647235 times.
✓ Branch 1 taken 137534 times.
784769 if (r)
282 647235 r = get_bits(&s->gb, r);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784769 times.
784769 if (x + r * stride > w) {
284 r = (w - x) / stride;
285 }
286
2/2
✓ Branch 0 taken 1010453 times.
✓ Branch 1 taken 784769 times.
1795222 for (i = 0; i < r; i++) {
287
1/2
✓ Branch 0 taken 1010453 times.
✗ Branch 1 not taken.
1010453 W(dst, x, Ra);
288 1010453 x += stride;
289 }
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784769 times.
784769 if (x >= w) {
292 av_log(NULL, AV_LOG_ERROR, "run overflow\n");
293 av_assert0(x <= w);
294 return AVERROR_INVALIDDATA;
295 }
296
297 /* decode run termination value */
298
1/2
✓ Branch 0 taken 784769 times.
✗ Branch 1 not taken.
784769 Rb = R(last, x);
299 784769 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
300 784769 err = ls_get_code_runterm(&s->gb, state, RItype,
301 784769 ff_log2_run[state->run_index[comp]]);
302
2/2
✓ Branch 0 taken 729428 times.
✓ Branch 1 taken 55341 times.
784769 if (state->run_index[comp])
303 729428 state->run_index[comp]--;
304
305
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 784769 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
784769 if (state->near && RItype) {
306 pred = Ra + err;
307 } else {
308
2/2
✓ Branch 0 taken 170007 times.
✓ Branch 1 taken 614762 times.
784769 if (Rb < Ra)
309 170007 pred = Rb - err;
310 else
311 614762 pred = Rb + err;
312 }
313 } else { /* regular mode */
314 int context, sign;
315
316 46641676 context = ff_jpegls_quantize(state, D0) * 81 +
317 46641676 ff_jpegls_quantize(state, D1) * 9 +
318 46641676 ff_jpegls_quantize(state, D2);
319 46641676 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
320
321
2/2
✓ Branch 0 taken 19168277 times.
✓ Branch 1 taken 27473399 times.
46641676 if (context < 0) {
322 19168277 context = -context;
323 19168277 sign = 1;
324 } else {
325 27473399 sign = 0;
326 }
327
328
2/2
✓ Branch 0 taken 19168277 times.
✓ Branch 1 taken 27473399 times.
46641676 if (sign) {
329 19168277 pred = av_clip(pred - state->C[context], 0, state->maxval);
330 19168277 err = -ls_get_code_regular(&s->gb, state, context);
331 } else {
332 27473399 pred = av_clip(pred + state->C[context], 0, state->maxval);
333 27473399 err = ls_get_code_regular(&s->gb, state, context);
334 }
335
336 /* we have to do something more for near-lossless coding */
337 46641676 pred += err;
338 }
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47426445 times.
47426445 if (state->near) {
340 if (pred < -state->near)
341 pred += state->range * state->twonear;
342 else if (pred > state->maxval + state->near)
343 pred -= state->range * state->twonear;
344 pred = av_clip(pred, 0, state->maxval);
345 }
346
347 47426445 pred &= state->maxval;
348
1/2
✓ Branch 0 taken 47426445 times.
✗ Branch 1 not taken.
47426445 W(dst, x, pred);
349 47426445 x += stride;
350 }
351
352 137007 return 0;
353 }
354
355 221 int ff_jpegls_decode_picture(MJpegDecodeContext *s)
356 {
357 221 int near = s->Ss;
358 221 int point_transform = s->Al;
359 221 int ilv = s->Se;
360 221 int i, t = 0;
361 uint8_t *zero, *last, *cur;
362 221 JLSState *state = s->jls_state;
363 221 int off = 0, stride = 1, width, shift, ret = 0;
364 221 int decoded_height = 0;
365
366
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 200 times.
221 if (!state) {
367 21 state = av_malloc(sizeof(*state));
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!state)
369 return AVERROR(ENOMEM);
370 21 s->jls_state = state;
371 }
372 221 zero = av_mallocz(s->picture_ptr->linesize[0]);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (!zero)
374 return AVERROR(ENOMEM);
375 221 last = zero;
376 221 cur = s->picture_ptr->data[0];
377
378 /* initialize JPEG-LS state from JPEG parameters */
379 221 state->near = near;
380 221 state->bpp = (s->bits < 2) ? 2 : s->bits;
381 221 state->maxval = s->maxval;
382 221 state->T1 = s->t1;
383 221 state->T2 = s->t2;
384 221 state->T3 = s->t3;
385 221 state->reset = s->reset;
386 221 ff_jpegls_reset_coding_parameters(state, 0);
387
388 /* Testing parameters here, we cannot test in LSE or SOF because
389 * these interdepend and are allowed in either order
390 */
391
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 if (state->maxval >= (1<<state->bpp) ||
392
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 state->T1 > state->T2 ||
393
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 state->T2 > state->T3 ||
394
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 state->T3 > state->maxval ||
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 state->reset > FFMAX(255, state->maxval)) {
396 ret = AVERROR_INVALIDDATA;
397 goto end;
398 }
399
400
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 if (s->bits <= 8)
401 221 shift = point_transform + (8 - s->bits);
402 else
403 shift = point_transform + (16 - s->bits);
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (shift >= 16) {
406 ret = AVERROR_INVALIDDATA;
407 goto end;
408 }
409
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
411 av_log(s->avctx, AV_LOG_DEBUG,
412 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
413 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
414 s->width, s->height, state->near, state->maxval,
415 state->T1, state->T2, state->T3,
416 state->reset, state->limit, state->qbpp, state->range);
417 av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
418 ilv, point_transform, s->bits, s->cur_scan);
419 }
420
421 221 s->restart_count = -1;
422
423
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 207 times.
221 if (ilv == 0) { /* separate planes */
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (s->cur_scan > s->nb_components) {
425 ret = AVERROR_INVALIDDATA;
426 goto end;
427 }
428
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 stride = (s->nb_components > 1) ? 3 : 1;
429 14 off = av_clip(s->cur_scan - 1, 0, stride - 1);
430 14 width = s->width * stride;
431 14 cur += off;
432
2/2
✓ Branch 0 taken 6216 times.
✓ Branch 1 taken 14 times.
6230 for (i = 0; i < s->height; i++) {
433 int restart;
434 6216 ret = ff_mjpeg_handle_restart(s, &restart);
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6216 times.
6216 if (ret < 0)
436 goto end;
437
2/2
✓ Branch 0 taken 536 times.
✓ Branch 1 taken 5680 times.
6216 if (restart) {
438 536 ff_jpegls_init_state(state);
439 536 t = 0;
440 536 last = zero;
441 }
442
1/2
✓ Branch 0 taken 6216 times.
✗ Branch 1 not taken.
6216 if (s->bits <= 8) {
443 6216 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
444 6216 t = last[0];
445 } else {
446 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
447 t = *((uint16_t *)last);
448 }
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6216 times.
6216 if (ret < 0)
450 break;
451 6216 last = cur;
452 6216 cur += s->picture_ptr->linesize[0];
453 }
454 14 decoded_height = i;
455
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 } else if (ilv == 1) { /* line interleaving */
456 int j;
457 207 int Rc[3] = { 0, 0, 0 };
458
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 stride = (s->nb_components > 1) ? 3 : 1;
459 207 memset(cur, 0, s->picture_ptr->linesize[0]);
460 207 width = s->width * stride;
461
2/2
✓ Branch 0 taken 46262 times.
✓ Branch 1 taken 207 times.
46469 for (i = 0; i < s->height; i++) {
462 int restart;
463 46262 ret = ff_mjpeg_handle_restart(s, &restart);
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46262 times.
46262 if (ret < 0)
465 goto end;
466
2/2
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 45881 times.
46262 if (restart) {
467 381 ff_jpegls_init_state(state);
468 381 memset(Rc, 0, sizeof(Rc));
469 381 last = zero;
470 }
471
2/2
✓ Branch 0 taken 138786 times.
✓ Branch 1 taken 46262 times.
185048 for (j = 0; j < stride; j++) {
472 138786 ret = ls_decode_line(state, s, last + j, cur + j,
473 Rc[j], width, stride, j, 8);
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138786 times.
138786 if (ret < 0)
475 break;
476 138786 Rc[j] = last[j];
477 }
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46262 times.
46262 if (ret < 0)
479 break;
480 46262 last = cur;
481 46262 cur += s->picture_ptr->linesize[0];
482 }
483 207 decoded_height = i;
484 } else if (ilv == 2) { /* sample interleaving */
485 avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
486 ret = AVERROR_PATCHWELCOME;
487 goto end;
488 } else { /* unknown interleaving */
489 avpriv_report_missing_feature(s->avctx, "Unknown interleaved images");
490 ret = AVERROR_PATCHWELCOME;
491 goto end;
492 }
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
221 if (s->xfrm && s->nb_components == 3) {
495 int x, w;
496
497 w = s->width * s->nb_components;
498
499 if (s->bits <= 8) {
500 uint8_t *src = s->picture_ptr->data[0];
501
502 for (i = 0; i < s->height; i++) {
503 switch(s->xfrm) {
504 case 1:
505 for (x = off; x + 2 < w; x += 3) {
506 src[x ] += src[x+1] + 128;
507 src[x+2] += src[x+1] + 128;
508 }
509 break;
510 case 2:
511 for (x = off; x + 2 < w; x += 3) {
512 src[x ] += src[x+1] + 128;
513 src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
514 }
515 break;
516 case 3:
517 for (x = off; x + 2 < w; x += 3) {
518 int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
519 src[x+0] = src[x+2] + g + 128;
520 src[x+2] = src[x+1] + g + 128;
521 src[x+1] = g;
522 }
523 break;
524 case 4:
525 for (x = off; x + 2 < w; x += 3) {
526 int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
527 int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
528 int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
529 src[x+0] = av_clip_uint8(r);
530 src[x+1] = av_clip_uint8(g);
531 src[x+2] = av_clip_uint8(b);
532 }
533 break;
534 }
535 src += s->picture_ptr->linesize[0];
536 }
537 }else
538 avpriv_report_missing_feature(s->avctx, "16bit xfrm");
539 }
540
541
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 8 times.
221 if (shift) { /* we need to do point transform or normalize samples */
542 int x, w;
543
544 8 w = s->width * s->nb_components;
545
546
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->bits <= 8) {
547 8 uint8_t *src = s->picture_ptr->data[0];
548
549
2/2
✓ Branch 0 taken 5688 times.
✓ Branch 1 taken 8 times.
5696 for (i = 0; i < decoded_height; i++) {
550
2/2
✓ Branch 0 taken 4044168 times.
✓ Branch 1 taken 5688 times.
4049856 for (x = off; x < w; x += stride)
551 4044168 src[x] <<= shift;
552 5688 src += s->picture_ptr->linesize[0];
553 }
554 } else {
555 uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
556
557 for (i = 0; i < decoded_height; i++) {
558 for (x = 0; x < w; x++)
559 src[x] <<= shift;
560 src += s->picture_ptr->linesize[0] / 2;
561 }
562 }
563 }
564
565 213 end:
566 221 av_free(zero);
567
568 221 return ret;
569 }
570
571 const FFCodec ff_jpegls_decoder = {
572 .p.name = "jpegls",
573 CODEC_LONG_NAME("JPEG-LS"),
574 .p.type = AVMEDIA_TYPE_VIDEO,
575 .p.id = AV_CODEC_ID_JPEGLS,
576 .priv_data_size = sizeof(MJpegDecodeContext),
577 .init = ff_mjpeg_decode_init,
578 .close = ff_mjpeg_decode_end,
579 FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame),
580 .p.capabilities = AV_CODEC_CAP_DR1,
581 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
582 };
583