../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
223 / 327
68.2%
Branches:
160 / 246
65.0%
Functions:
5 / 5
100.0%

libavcodec/jpeglsdec.c
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/attributes.h"
29 #include "libavutil/mem.h"
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "get_bits.h"
33 #include "golomb.h"
34 #include "mathops.h"
35 #include "mjpegdec.h"
36 #include "jpegls.h"
37 #include "jpeglsdec.h"
38
39 /*
40 * Uncomment this to significantly speed up decoding of broken JPEG-LS
41 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
42 *
43 * There is no Golomb code with length >= 32 bits possible, so check and
44 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
45 * on this errors.
46 */
47 //#define JLS_BROKEN
48
49 /**
50 * Decode LSE block with initialization parameters
51 */
52 16 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
53 {
54 int id;
55 int tid, wt, maxtab, i, j;
56
57 16 int len = bytestream2_get_be16(&s->gB);
58 16 id = bytestream2_get_byte(&s->gB);
59
60
1/5
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 15 taken 16 times.
✗ Branch 4 → 16 not taken.
✗ Branch 4 → 62 not taken.
✗ Branch 4 → 64 not taken.
16 switch (id) {
61 ✗ case 1:
62
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (len < 13)
63 ✗ return AVERROR_INVALIDDATA;
64
65 ✗ s->maxval = bytestream2_get_be16u(&s->gB);
66 ✗ s->t1 = bytestream2_get_be16u(&s->gB);
67 ✗ s->t2 = bytestream2_get_be16u(&s->gB);
68 ✗ s->t3 = bytestream2_get_be16u(&s->gB);
69 ✗ s->reset = bytestream2_get_be16u(&s->gB);
70
71
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
72 ✗ av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n",
73 s->maxval, s->t1, s->t2, s->t3, s->reset);
74 }
75
76 // ff_jpegls_reset_coding_parameters(s, 0);
77 //FIXME quant table?
78 ✗ break;
79 16 case 2:
80 16 s->palette_index = 0;
81 av_fallthrough;
82 16 case 3:
83 16 tid= bytestream2_get_byte(&s->gB);
84 16 wt = bytestream2_get_byte(&s->gB);
85
86
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 16 times.
16 if (len < 5)
87 ✗ return AVERROR_INVALIDDATA;
88
89
2/4
✓ Branch 20 → 21 taken 16 times.
✗ Branch 20 → 22 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 16 times.
16 if (wt < 1 || wt > MAX_COMPONENTS) {
90 ✗ avpriv_request_sample(s->avctx, "wt %d", wt);
91 ✗ return AVERROR_PATCHWELCOME;
92 }
93
94
1/2
✓ Branch 24 → 25 taken 16 times.
✗ Branch 24 → 26 not taken.
16 if (!s->maxval)
95 16 maxtab = 255;
96
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ else if ((5 + wt*(s->maxval+1)) < 65535)
97 ✗ maxtab = s->maxval;
98 else
99 ✗ maxtab = 65530/wt - 1;
100
101
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 16 times.
16 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
102 ✗ av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab);
103 }
104
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 34 taken 16 times.
16 if (maxtab >= 256) {
105 ✗ avpriv_request_sample(s->avctx, ">8bit palette");
106 ✗ return AVERROR_PATCHWELCOME;
107 }
108 16 maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
109
110
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 16 times.
16 if (s->palette_index > maxtab)
111 ✗ return AVERROR_INVALIDDATA;
112
113
3/4
✓ Branch 36 → 37 taken 8 times.
✓ Branch 36 → 38 taken 8 times.
✓ Branch 37 → 38 taken 8 times.
✗ Branch 37 → 61 not taken.
16 if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) &&
114
3/4
✓ Branch 38 → 39 taken 8 times.
✓ Branch 38 → 40 taken 8 times.
✓ Branch 39 → 40 taken 8 times.
✗ Branch 39 → 61 not taken.
16 (s->picture_ptr->format == AV_PIX_FMT_GRAY8 || s->picture_ptr->format == AV_PIX_FMT_PAL8)) {
115 16 uint32_t *pal = (uint32_t *)s->picture_ptr->data[1];
116 16 int shift = 0;
117
118
2/4
✓ Branch 40 → 41 taken 16 times.
✗ Branch 40 → 46 not taken.
✓ Branch 41 → 42 taken 16 times.
✗ Branch 41 → 46 not taken.
16 if (s->avctx->bits_per_raw_sample > 0 && s->avctx->bits_per_raw_sample < 8) {
119
1/2
✓ Branch 42 → 43 taken 16 times.
✗ Branch 42 → 44 not taken.
16 maxtab = FFMIN(maxtab, (1<<s->avctx->bits_per_raw_sample)-1);
120 16 shift = 8 - s->avctx->bits_per_raw_sample;
121 }
122
123 16 s->force_pal8++;
124
2/2
✓ Branch 46 → 47 taken 8 times.
✓ Branch 46 → 50 taken 8 times.
16 if (!pal) {
125
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 8 times.
8 if (s->force_pal8 > 1)
126 ✗ return AVERROR_INVALIDDATA;
127 8 return 1;
128 }
129
130
2/2
✓ Branch 59 → 51 taken 344 times.
✓ Branch 59 → 60 taken 8 times.
352 for (i=s->palette_index; i<=maxtab; i++) {
131 344 uint8_t k = i << shift;
132
1/2
✓ Branch 51 → 52 taken 344 times.
✗ Branch 51 → 53 not taken.
344 pal[k] = wt < 4 ? 0xFF000000 : 0;
133
2/2
✓ Branch 57 → 55 taken 1032 times.
✓ Branch 57 → 58 taken 344 times.
1376 for (j=0; j<wt; j++) {
134 1032 pal[k] |= bytestream2_get_byte(&s->gB) << (8*(wt-j-1));
135 }
136 }
137 8 s->palette_index = i;
138 }
139 8 break;
140 ✗ case 4:
141 ✗ avpriv_request_sample(s->avctx, "oversize image");
142 ✗ return AVERROR(ENOSYS);
143 ✗ default:
144 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
145 ✗ return AVERROR_INVALIDDATA;
146 }
147 ff_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
148
149 8 return 0;
150 }
151
152 /**
153 * Get context-dependent Golomb code, decode it and update context
154 */
155 46645108 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
156 {
157 int k, ret;
158
159
2/2
✓ Branch 4 → 3 taken 120992758 times.
✓ Branch 4 → 5 taken 46645108 times.
167637866 for (k = 0; ((unsigned)state->N[Q] << k) < state->A[Q]; k++)
160 ;
161
162 #ifdef JLS_BROKEN
163 if (!show_bits_long(gb, 32))
164 return -1;
165 #endif
166 46645108 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
167
168 /* decode mapped error */
169
2/2
✓ Branch 6 → 7 taken 23897846 times.
✓ Branch 6 → 8 taken 22747262 times.
46645108 if (ret & 1)
170 23897846 ret = -(ret + 1 >> 1);
171 else
172 22747262 ret >>= 1;
173
174 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
175
5/6
✓ Branch 9 → 10 taken 46645108 times.
✗ Branch 9 → 13 not taken.
✓ Branch 10 → 11 taken 2699415 times.
✓ Branch 10 → 13 taken 43945693 times.
✓ Branch 11 → 12 taken 840758 times.
✓ Branch 11 → 13 taken 1858657 times.
46645108 if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
176 840758 ret = -(ret + 1);
177
178 46645108 ret = ff_jpegls_update_state_regular(state, Q, ret);
179
180 46645108 return ret;
181 }
182
183 /**
184 * Get Golomb code, decode it and update state for run termination
185 */
186 785305 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
187 int RItype, int limit_add)
188 {
189 int k, ret, temp, map;
190 785305 int Q = 365 + RItype;
191
192 785305 temp = state->A[Q];
193
2/2
✓ Branch 2 → 3 taken 510394 times.
✓ Branch 2 → 4 taken 274911 times.
785305 if (RItype)
194 510394 temp += state->N[Q] >> 1;
195
196
2/2
✓ Branch 6 → 5 taken 1893571 times.
✓ Branch 6 → 7 taken 785305 times.
2678876 for (k = 0; ((unsigned)state->N[Q] << k) < temp; k++)
197 ;
198
199 #ifdef JLS_BROKEN
200 if (!show_bits_long(gb, 32))
201 return -1;
202 #endif
203 785305 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
204 state->qbpp);
205
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 785305 times.
785305 if (ret < 0)
206 ✗ return -0x10000;
207
208 /* decode mapped error */
209 785305 map = 0;
210
8/8
✓ Branch 10 → 11 taken 153938 times.
✓ Branch 10 → 15 taken 631367 times.
✓ Branch 11 → 12 taken 26894 times.
✓ Branch 11 → 13 taken 127044 times.
✓ Branch 12 → 13 taken 2304 times.
✓ Branch 12 → 15 taken 24590 times.
✓ Branch 13 → 14 taken 60548 times.
✓ Branch 13 → 15 taken 68800 times.
785305 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
211 60548 map = 1;
212 785305 ret += RItype + map;
213
214
2/2
✓ Branch 15 → 16 taken 352946 times.
✓ Branch 15 → 17 taken 432359 times.
785305 if (ret & 1) {
215 352946 ret = map - (ret + 1 >> 1);
216 352946 state->B[Q]++;
217 } else {
218 432359 ret = ret >> 1;
219 }
220
221
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 785305 times.
785305 if (FFABS(ret) > 0xFFFF)
222 ✗ return -0x10000;
223 /* update state */
224 785305 state->A[Q] += FFABS(ret) - RItype;
225 785305 ret *= state->twonear;
226 785305 ff_jpegls_downscale_state(state, Q);
227
228 785305 return ret;
229 }
230
231 /**
232 * Decode one line of image
233 */
234 145130 static inline int ls_decode_line(JLSState *state, MJpegDecodeContext *s,
235 void *last, void *dst, int last2, int w,
236 int stride, int comp, int bits)
237 {
238 145130 int i, x = 0;
239 int Ra, Rb, Rc, Rd;
240 int D0, D1, D2;
241
242
2/2
✓ Branch 99 → 3 taken 47438482 times.
✓ Branch 99 → 100 taken 137061 times.
47575543 while (x < w) {
243 int err, pred;
244
245
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 47438482 times.
47438482 if (get_bits_left(&s->gb) <= 0)
246 ✗ return AVERROR_INVALIDDATA;
247
248 /* compute gradients */
249
6/6
✓ Branch 6 → 7 taken 47293352 times.
✓ Branch 6 → 10 taken 145130 times.
✓ Branch 7 → 8 taken 47289438 times.
✓ Branch 7 → 9 taken 3914 times.
✓ Branch 10 → 11 taken 145002 times.
✓ Branch 10 → 12 taken 128 times.
47438482 Ra = x ? R(dst, x - stride) : R(last, x);
250
2/2
✓ Branch 13 → 14 taken 47434440 times.
✓ Branch 13 → 15 taken 4042 times.
47438482 Rb = R(last, x);
251
4/4
✓ Branch 16 → 17 taken 47293352 times.
✓ Branch 16 → 20 taken 145130 times.
✓ Branch 17 → 18 taken 47289438 times.
✓ Branch 17 → 19 taken 3914 times.
47438482 Rc = x ? R(last, x - stride) : last2;
252
6/6
✓ Branch 21 → 22 taken 138111 times.
✓ Branch 21 → 25 taken 47300371 times.
✓ Branch 22 → 23 taken 138055 times.
✓ Branch 22 → 24 taken 56 times.
✓ Branch 25 → 26 taken 47296385 times.
✓ Branch 25 → 27 taken 3986 times.
47438482 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
253 47438482 D0 = Rd - Rb;
254 47438482 D1 = Rb - Rc;
255 47438482 D2 = Rc - Ra;
256 /* run mode */
257
2/2
✓ Branch 28 → 29 taken 5936844 times.
✓ Branch 28 → 78 taken 41501638 times.
47438482 if ((FFABS(D0) <= state->near) &&
258
2/2
✓ Branch 29 → 30 taken 2324428 times.
✓ Branch 29 → 78 taken 3612416 times.
5936844 (FFABS(D1) <= state->near) &&
259
2/2
✓ Branch 30 → 31 taken 793374 times.
✓ Branch 30 → 78 taken 1531054 times.
3109733 (FFABS(D2) <= state->near)) {
260 int r;
261 int RItype;
262
263 /* decode full runs while available */
264
2/2
✓ Branch 47 → 32 taken 738553 times.
✓ Branch 47 → 48 taken 785305 times.
1523858 while (get_bits1(&s->gb)) {
265 int r;
266 738553 r = 1 << ff_log2_run[state->run_index[comp]];
267
2/2
✓ Branch 32 → 33 taken 6410 times.
✓ Branch 32 → 34 taken 732143 times.
738553 if (x + r * stride > w)
268 6410 r = (w - x) / stride;
269
2/2
✓ Branch 39 → 35 taken 2757692 times.
✓ Branch 39 → 40 taken 738553 times.
3496245 for (i = 0; i < r; i++) {
270
2/2
✓ Branch 35 → 36 taken 2755018 times.
✓ Branch 35 → 37 taken 2674 times.
2757692 W(dst, x, Ra);
271 2757692 x += stride;
272 }
273 /* if EOL reached, we stop decoding */
274
2/2
✓ Branch 40 → 41 taken 6410 times.
✓ Branch 40 → 42 taken 732143 times.
738553 if (r != 1 << ff_log2_run[state->run_index[comp]])
275 6410 return 0;
276
1/2
✓ Branch 42 → 43 taken 732143 times.
✗ Branch 42 → 44 not taken.
732143 if (state->run_index[comp] < 31)
277 732143 state->run_index[comp]++;
278
2/2
✓ Branch 44 → 45 taken 1659 times.
✓ Branch 44 → 46 taken 730484 times.
732143 if (x + stride > w)
279 1659 return 0;
280 }
281 /* decode aborted run */
282 785305 r = ff_log2_run[state->run_index[comp]];
283
2/2
✓ Branch 48 → 49 taken 647769 times.
✓ Branch 48 → 51 taken 137536 times.
785305 if (r)
284 647769 r = get_bits(&s->gb, r);
285
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 785305 times.
785305 if (x + r * stride > w) {
286 ✗ r = (w - x) / stride;
287 }
288
2/2
✓ Branch 58 → 54 taken 1012003 times.
✓ Branch 58 → 59 taken 785305 times.
1797308 for (i = 0; i < r; i++) {
289
2/2
✓ Branch 54 → 55 taken 1010453 times.
✓ Branch 54 → 56 taken 1550 times.
1012003 W(dst, x, Ra);
290 1012003 x += stride;
291 }
292
293
1/2
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 65 taken 785305 times.
785305 if (x >= w) {
294 ✗ av_log(NULL, AV_LOG_ERROR, "run overflow\n");
295
0/2
✗ Branch 61 → 62 not taken.
✗ Branch 61 → 64 not taken.
✗ av_assert0(x <= w);
296 ✗ return AVERROR_INVALIDDATA;
297 }
298
299 /* decode run termination value */
300
2/2
✓ Branch 65 → 66 taken 784769 times.
✓ Branch 65 → 67 taken 536 times.
785305 Rb = R(last, x);
301 785305 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
302 785305 err = ls_get_code_runterm(&s->gb, state, RItype,
303 785305 ff_log2_run[state->run_index[comp]]);
304
2/2
✓ Branch 69 → 70 taken 729962 times.
✓ Branch 69 → 71 taken 55343 times.
785305 if (state->run_index[comp])
305 729962 state->run_index[comp]--;
306
307
1/4
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 74 taken 785305 times.
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
785305 if (state->near && RItype) {
308 ✗ pred = Ra + err;
309 } else {
310
2/2
✓ Branch 74 → 75 taken 170163 times.
✓ Branch 74 → 76 taken 615142 times.
785305 if (Rb < Ra)
311 170163 pred = Rb - err;
312 else
313 615142 pred = Rb + err;
314 }
315 } else { /* regular mode */
316 int context, sign;
317
318 46645108 context = ff_jpegls_quantize(state, D0) * 81 +
319 46645108 ff_jpegls_quantize(state, D1) * 9 +
320 46645108 ff_jpegls_quantize(state, D2);
321 46645108 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
322
323
2/2
✓ Branch 81 → 82 taken 19170011 times.
✓ Branch 81 → 83 taken 27475097 times.
46645108 if (context < 0) {
324 19170011 context = -context;
325 19170011 sign = 1;
326 } else {
327 27475097 sign = 0;
328 }
329
330
2/2
✓ Branch 84 → 85 taken 19170011 times.
✓ Branch 84 → 87 taken 27475097 times.
46645108 if (sign) {
331 19170011 pred = av_clip(pred - state->C[context], 0, state->maxval);
332 19170011 err = -ls_get_code_regular(&s->gb, state, context);
333 } else {
334 27475097 pred = av_clip(pred + state->C[context], 0, state->maxval);
335 27475097 err = ls_get_code_regular(&s->gb, state, context);
336 }
337
338 /* we have to do something more for near-lossless coding */
339 46645108 pred += err;
340 }
341
1/2
✗ Branch 89 → 90 not taken.
✓ Branch 89 → 95 taken 47430413 times.
47430413 if (state->near) {
342
0/2
✗ Branch 90 → 91 not taken.
✗ Branch 90 → 92 not taken.
✗ if (pred < -state->near)
343 ✗ pred += state->range * state->twonear;
344
0/2
✗ Branch 92 → 93 not taken.
✗ Branch 92 → 94 not taken.
✗ else if (pred > state->maxval + state->near)
345 ✗ pred -= state->range * state->twonear;
346 ✗ pred = av_clip(pred, 0, state->maxval);
347 }
348
349 47430413 pred &= state->maxval;
350
2/2
✓ Branch 95 → 96 taken 47426445 times.
✓ Branch 95 → 97 taken 3968 times.
47430413 W(dst, x, pred);
351 47430413 x += stride;
352 }
353
354 137061 return 0;
355 }
356
357 223 int ff_jpegls_decode_picture(MJpegDecodeContext *s)
358 {
359 223 int near = s->Ss;
360 223 int point_transform = s->Al;
361 223 int ilv = s->Se;
362 223 int i, t = 0;
363 uint8_t *zero, *last, *cur;
364 223 JLSState *state = s->jls_state;
365 223 int off = 0, stride = 1, width, shift, ret = 0;
366 223 int decoded_height = 0;
367
368 /* Bound the total amount of JPEG-LS decoding work per packet:
369 * Per T.87, ILV=0 uses one scan per component while ILV=1/2 use a single
370 * interleaved scan, and ff_mjpeg_decode_sof() rejects subsampled JPEG-LS,
371 * so a valid image needs at most height * nb_components
372 * (<= height * MAX_COMPONENTS) rows of decoding. The extra factor of 2
373 * is slack so odd, damaged and weird files are not rejected. */
374
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 223 times.
223 if (s->total_ls_decoded_height > s->height * 2LL * MAX_COMPONENTS)
375 ✗ return AVERROR_INVALIDDATA;
376
377
2/2
✓ Branch 4 → 5 taken 23 times.
✓ Branch 4 → 9 taken 200 times.
223 if (!state) {
378 23 state = av_malloc(sizeof(*state));
379
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 23 times.
23 if (!state)
380 ✗ return AVERROR(ENOMEM);
381 23 s->jls_state = state;
382 }
383 223 zero = av_mallocz(s->picture_ptr->linesize[0]);
384
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 223 times.
223 if (!zero)
385 ✗ return AVERROR(ENOMEM);
386 223 last = zero;
387 223 cur = s->picture_ptr->data[0];
388
389 /* initialize JPEG-LS state from JPEG parameters */
390 223 state->near = near;
391 223 state->bpp = (s->bits < 2) ? 2 : s->bits;
392 223 state->maxval = s->maxval;
393 223 state->T1 = s->t1;
394 223 state->T2 = s->t2;
395 223 state->T3 = s->t3;
396 223 state->reset = s->reset;
397 223 ff_jpegls_reset_coding_parameters(state, 0);
398
399 /* Testing parameters here, we cannot test in LSE or SOF because
400 * these interdepend and are allowed in either order
401 */
402
1/2
✓ Branch 13 → 14 taken 223 times.
✗ Branch 13 → 18 not taken.
223 if (state->maxval >= (1<<state->bpp) ||
403
1/2
✓ Branch 14 → 15 taken 223 times.
✗ Branch 14 → 18 not taken.
223 state->T1 > state->T2 ||
404
1/2
✓ Branch 15 → 16 taken 223 times.
✗ Branch 15 → 18 not taken.
223 state->T2 > state->T3 ||
405
1/2
✓ Branch 16 → 17 taken 223 times.
✗ Branch 16 → 18 not taken.
223 state->T3 > state->maxval ||
406
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 223 times.
223 state->reset > FFMAX(255, state->maxval)) {
407 ✗ ret = AVERROR_INVALIDDATA;
408 ✗ goto end;
409 }
410
411
2/2
✓ Branch 19 → 20 taken 221 times.
✓ Branch 19 → 21 taken 2 times.
223 if (s->bits <= 8)
412 221 shift = point_transform + (8 - s->bits);
413 else
414 2 shift = point_transform + (16 - s->bits);
415
416
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 223 times.
223 if (shift >= 16) {
417 ✗ ret = AVERROR_INVALIDDATA;
418 ✗ goto end;
419 }
420
421
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 27 taken 223 times.
223 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
422 ✗ av_log(s->avctx, AV_LOG_DEBUG,
423 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
424 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
425 s->width, s->height, state->near, state->maxval,
426 state->T1, state->T2, state->T3,
427 state->reset, state->limit, state->qbpp, state->range);
428 ✗ av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
429 ilv, point_transform, s->bits, s->cur_scan);
430 }
431
432 223 s->restart_count = -1;
433
434
2/2
✓ Branch 27 → 28 taken 14 times.
✓ Branch 27 → 51 taken 209 times.
223 if (ilv == 0) { /* separate planes */
435
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 14 times.
14 if (s->cur_scan > s->nb_components) {
436 ✗ ret = AVERROR_INVALIDDATA;
437 ✗ goto end;
438 }
439
2/2
✓ Branch 30 → 31 taken 6 times.
✓ Branch 30 → 32 taken 8 times.
14 stride = (s->nb_components > 1) ? 3 : 1;
440 14 off = av_clip(s->cur_scan - 1, 0, stride - 1);
441 14 width = s->width * stride;
442 14 cur += off;
443
2/2
✓ Branch 49 → 34 taken 6216 times.
✓ Branch 49 → 50 taken 14 times.
6230 for (i = 0; i < s->height; i++) {
444 int restart;
445 6216 ret = ff_mjpeg_handle_restart(s, &restart);
446
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 6216 times.
6216 if (ret < 0)
447 ✗ goto end;
448
2/2
✓ Branch 37 → 38 taken 536 times.
✓ Branch 37 → 40 taken 5680 times.
6216 if (restart) {
449 536 ff_jpegls_init_state(state);
450 536 t = 0;
451 536 last = zero;
452 }
453
1/2
✓ Branch 40 → 41 taken 6216 times.
✗ Branch 40 → 43 not taken.
6216 if (s->bits <= 8) {
454 6216 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
455 6216 t = last[0];
456 } else {
457 ✗ ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
458 ✗ t = *((uint16_t *)last);
459 }
460
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 6216 times.
6216 if (ret < 0)
461 ✗ break;
462 6216 last = cur;
463 6216 cur += s->picture_ptr->linesize[0];
464 }
465 14 decoded_height = i;
466
1/2
✓ Branch 51 → 52 taken 209 times.
✗ Branch 51 → 78 not taken.
209 } else if (ilv == 1) { /* line interleaving */
467 int j;
468 209 int Rc[3] = { 0, 0, 0 };
469
2/2
✓ Branch 52 → 53 taken 207 times.
✓ Branch 52 → 54 taken 2 times.
209 stride = (s->nb_components > 1) ? 3 : 1;
470 209 memset(cur, 0, s->picture_ptr->linesize[0]);
471 209 width = s->width * stride;
472
2/2
✓ Branch 76 → 56 taken 46390 times.
✓ Branch 76 → 77 taken 209 times.
46599 for (i = 0; i < s->height; i++) {
473 int restart;
474 46390 ret = ff_mjpeg_handle_restart(s, &restart);
475
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 46390 times.
46390 if (ret < 0)
476 ✗ goto end;
477
2/2
✓ Branch 59 → 60 taken 383 times.
✓ Branch 59 → 62 taken 46007 times.
46390 if (restart) {
478 383 ff_jpegls_init_state(state);
479 383 memset(Rc, 0, sizeof(Rc));
480 383 last = zero;
481 }
482
2/2
✓ Branch 71 → 63 taken 138914 times.
✓ Branch 71 → 72 taken 46390 times.
185304 for (j = 0; j < stride; j++) {
483
2/2
✓ Branch 63 → 64 taken 138786 times.
✓ Branch 63 → 66 taken 128 times.
138914 if (s->bits <= 8) {
484 138786 ret = ls_decode_line(state, s, last + j, cur + j,
485 Rc[j], width, stride, j, 8);
486 138786 Rc[j] = last[j];
487 } else {
488 /* only single component scans can have more than 8 bits */
489 128 ret = ls_decode_line(state, s, last, cur,
490 Rc[j], width, stride, j, 16);
491 128 Rc[j] = *((uint16_t *)last);
492 }
493
1/2
✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 138914 times.
138914 if (ret < 0)
494 ✗ break;
495 }
496
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 74 taken 46390 times.
46390 if (ret < 0)
497 ✗ break;
498 46390 last = cur;
499 46390 cur += s->picture_ptr->linesize[0];
500 }
501 209 decoded_height = i;
502
0/2
✗ Branch 78 → 79 not taken.
✗ Branch 78 → 81 not taken.
✗ } else if (ilv == 2) { /* sample interleaving */
503 ✗ avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
504 ✗ ret = AVERROR_PATCHWELCOME;
505 ✗ goto end;
506 } else { /* unknown interleaving */
507 ✗ avpriv_report_missing_feature(s->avctx, "Unknown interleaved images");
508 ✗ ret = AVERROR_PATCHWELCOME;
509 ✗ goto end;
510 }
511
512 223 s->total_ls_decoded_height += decoded_height;
513
514
1/4
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 107 taken 223 times.
✗ Branch 84 → 85 not taken.
✗ Branch 84 → 107 not taken.
223 if (s->xfrm && s->nb_components == 3) {
515 int x, w;
516
517 ✗ w = s->width * s->nb_components;
518
519
0/2
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 106 not taken.
✗ if (s->bits <= 8) {
520 ✗ uint8_t *src = s->picture_ptr->data[0];
521
522
0/2
✗ Branch 105 → 87 not taken.
✗ Branch 105 → 107 not taken.
✗ for (i = 0; i < decoded_height; i++) {
523
0/5
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 92 not taken.
✗ Branch 87 → 96 not taken.
✗ Branch 87 → 100 not taken.
✗ Branch 87 → 104 not taken.
✗ switch(s->xfrm) {
524 ✗ case 1:
525
0/2
✗ Branch 90 → 89 not taken.
✗ Branch 90 → 91 not taken.
✗ for (x = off; x + 2 < w; x += 3) {
526 ✗ src[x ] += src[x+1] + 128;
527 ✗ src[x+2] += src[x+1] + 128;
528 }
529 ✗ break;
530 ✗ case 2:
531
0/2
✗ Branch 94 → 93 not taken.
✗ Branch 94 → 95 not taken.
✗ for (x = off; x + 2 < w; x += 3) {
532 ✗ src[x ] += src[x+1] + 128;
533 ✗ src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
534 }
535 ✗ break;
536 ✗ case 3:
537
0/2
✗ Branch 98 → 97 not taken.
✗ Branch 98 → 99 not taken.
✗ for (x = off; x + 2 < w; x += 3) {
538 ✗ int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
539 ✗ src[x+0] = src[x+2] + g + 128;
540 ✗ src[x+2] = src[x+1] + g + 128;
541 ✗ src[x+1] = g;
542 }
543 ✗ break;
544 ✗ case 4:
545
0/2
✗ Branch 102 → 101 not taken.
✗ Branch 102 → 103 not taken.
✗ for (x = off; x + 2 < w; x += 3) {
546 ✗ int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
547 ✗ int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
548 ✗ int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
549 ✗ src[x+0] = av_clip_uint8(r);
550 ✗ src[x+1] = av_clip_uint8(g);
551 ✗ src[x+2] = av_clip_uint8(b);
552 }
553 ✗ break;
554 }
555 ✗ src += s->picture_ptr->linesize[0];
556 }
557 }else
558 ✗ avpriv_report_missing_feature(s->avctx, "16bit xfrm");
559 }
560
561
2/2
✓ Branch 107 → 108 taken 215 times.
✓ Branch 107 → 109 taken 8 times.
223 if (shift) { /* we need to do point transform or normalize samples */
562 int x, w;
563
564 8 w = s->width * s->nb_components;
565
566
1/2
✓ Branch 109 → 110 taken 8 times.
✗ Branch 109 → 116 not taken.
8 if (s->bits <= 8) {
567 8 uint8_t *src = s->picture_ptr->data[0];
568
569
2/2
✓ Branch 115 → 111 taken 5688 times.
✓ Branch 115 → 123 taken 8 times.
5696 for (i = 0; i < decoded_height; i++) {
570
2/2
✓ Branch 113 → 112 taken 4044168 times.
✓ Branch 113 → 114 taken 5688 times.
4049856 for (x = off; x < w; x += stride)
571 4044168 src[x] <<= shift;
572 5688 src += s->picture_ptr->linesize[0];
573 }
574 } else {
575 ✗ uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
576
577
0/2
✗ Branch 121 → 117 not taken.
✗ Branch 121 → 122 not taken.
✗ for (i = 0; i < decoded_height; i++) {
578
0/2
✗ Branch 119 → 118 not taken.
✗ Branch 119 → 120 not taken.
✗ for (x = 0; x < w; x++)
579 ✗ src[x] <<= shift;
580 ✗ src += s->picture_ptr->linesize[0] / 2;
581 }
582 }
583 }
584
585 215 end:
586 223 av_free(zero);
587
588 223 return ret;
589 }
590
591 const FFCodec ff_jpegls_decoder = {
592 .p.name = "jpegls",
593 CODEC_LONG_NAME("JPEG-LS"),
594 .p.type = AVMEDIA_TYPE_VIDEO,
595 .p.id = AV_CODEC_ID_JPEGLS,
596 .priv_data_size = sizeof(MJpegDecodeContext),
597 .init = ff_mjpeg_decode_init,
598 .close = ff_mjpeg_decode_end,
599 FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame),
600 .p.capabilities = AV_CODEC_CAP_DR1,
601 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
602 };
603