FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeglsdec.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 200 305 65.6%
Branches: 135 232 58.2%

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 "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "internal.h"
32 #include "mathops.h"
33 #include "mjpeg.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 = get_bits(&s->gb, 16);
57 16 id = get_bits(&s->gb, 8);
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 = get_bits(&s->gb, 16);
65 s->t1 = get_bits(&s->gb, 16);
66 s->t2 = get_bits(&s->gb, 16);
67 s->t3 = get_bits(&s->gb, 16);
68 s->reset = get_bits(&s->gb, 16);
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= get_bits(&s->gb, 8);
82 16 wt = get_bits(&s->gb, 8);
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] |= get_bits(&s->gb, 8) << (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 46507616 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 120390394 times.
✓ Branch 1 taken 46507616 times.
166898010 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 46507616 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
165
166 /* decode mapped error */
167
2/2
✓ Branch 0 taken 23829574 times.
✓ Branch 1 taken 22678042 times.
46507616 if (ret & 1)
168 23829574 ret = -(ret + 1 >> 1);
169 else
170 22678042 ret >>= 1;
171
172 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
173
5/6
✓ Branch 0 taken 46507616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2699029 times.
✓ Branch 3 taken 43808587 times.
✓ Branch 4 taken 840496 times.
✓ Branch 5 taken 1858533 times.
46507616 if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
174 840496 ret = -(ret + 1);
175
176 46507616 ret = ff_jpegls_update_state_regular(state, Q, ret);
177
178 46507616 return ret;
179 }
180
181 /**
182 * Get Golomb code, decode it and update state for run termination
183 */
184 783661 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 783661 int Q = 365 + RItype;
189
190 783661 temp = state->A[Q];
191
2/2
✓ Branch 0 taken 509232 times.
✓ Branch 1 taken 274429 times.
783661 if (RItype)
192 509232 temp += state->N[Q] >> 1;
193
194
2/2
✓ Branch 0 taken 1884419 times.
✓ Branch 1 taken 783661 times.
2668080 for (k = 0; (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 783661 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
202 state->qbpp);
203
204 /* decode mapped error */
205 783661 map = 0;
206
8/8
✓ Branch 0 taken 153938 times.
✓ Branch 1 taken 629723 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.
783661 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
207 60548 map = 1;
208 783661 ret += RItype + map;
209
210
2/2
✓ Branch 0 taken 351896 times.
✓ Branch 1 taken 431765 times.
783661 if (ret & 1) {
211 351896 ret = map - (ret + 1 >> 1);
212 351896 state->B[Q]++;
213 } else {
214 431765 ret = ret >> 1;
215 }
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
783661 if(FFABS(ret) > 0xFFFF)
218 return -0x10000;
219 /* update state */
220 783661 state->A[Q] += FFABS(ret) - RItype;
221 783661 ret *= state->twonear;
222 783661 ff_jpegls_downscale_state(state, Q);
223
224 783661 return ret;
225 }
226
227 /**
228 * Decode one line of image
229 */
230 143946 static inline int ls_decode_line(JLSState *state, MJpegDecodeContext *s,
231 void *last, void *dst, int last2, int w,
232 int stride, int comp, int bits)
233 {
234 143946 int i, x = 0;
235 int Ra, Rb, Rc, Rd;
236 int D0, D1, D2;
237
238
2/2
✓ Branch 0 taken 47299272 times.
✓ Branch 1 taken 135951 times.
47435223 while (x < w) {
239 int err, pred;
240
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47299272 times.
47299272 if (get_bits_left(&s->gb) <= 0)
242 return AVERROR_INVALIDDATA;
243
244 /* compute gradients */
245
4/6
✓ Branch 0 taken 47155326 times.
✓ Branch 1 taken 143946 times.
✓ Branch 2 taken 47155326 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 143946 times.
✗ Branch 5 not taken.
47299272 Ra = x ? R(dst, x - stride) : R(last, x);
246
1/2
✓ Branch 0 taken 47299272 times.
✗ Branch 1 not taken.
47299272 Rb = R(last, x);
247
3/4
✓ Branch 0 taken 47155326 times.
✓ Branch 1 taken 143946 times.
✓ Branch 2 taken 47155326 times.
✗ Branch 3 not taken.
47299272 Rc = x ? R(last, x - stride) : last2;
248
4/6
✓ Branch 0 taken 136999 times.
✓ Branch 1 taken 47162273 times.
✓ Branch 2 taken 136999 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 47162273 times.
✗ Branch 5 not taken.
47299272 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
249 47299272 D0 = Rd - Rb;
250 47299272 D1 = Rb - Rc;
251 47299272 D2 = Rc - Ra;
252 /* run mode */
253
2/2
✓ Branch 0 taken 5800192 times.
✓ Branch 1 taken 41499080 times.
47299272 if ((FFABS(D0) <= state->near) &&
254
2/2
✓ Branch 0 taken 2188180 times.
✓ Branch 1 taken 3612012 times.
5800192 (FFABS(D1) <= state->near) &&
255
2/2
✓ Branch 0 taken 791656 times.
✓ Branch 1 taken 1396524 times.
2971841 (FFABS(D2) <= state->near)) {
256 int r;
257 int RItype;
258
259 /* decode full runs while available */
260
2/2
✓ Branch 1 taken 737915 times.
✓ Branch 2 taken 783661 times.
1521576 while (get_bits1(&s->gb)) {
261 int r;
262 737915 r = 1 << ff_log2_run[state->run_index[comp]];
263
2/2
✓ Branch 0 taken 6338 times.
✓ Branch 1 taken 731577 times.
737915 if (x + r * stride > w)
264 6338 r = (w - x) / stride;
265
2/2
✓ Branch 0 taken 2755018 times.
✓ Branch 1 taken 737915 times.
3492933 for (i = 0; i < r; i++) {
266
1/2
✓ Branch 0 taken 2755018 times.
✗ Branch 1 not taken.
2755018 W(dst, x, Ra);
267 2755018 x += stride;
268 }
269 /* if EOL reached, we stop decoding */
270
2/2
✓ Branch 0 taken 6338 times.
✓ Branch 1 taken 731577 times.
737915 if (r != 1 << ff_log2_run[state->run_index[comp]])
271 6338 return 0;
272
1/2
✓ Branch 0 taken 731577 times.
✗ Branch 1 not taken.
731577 if (state->run_index[comp] < 31)
273 731577 state->run_index[comp]++;
274
2/2
✓ Branch 0 taken 1657 times.
✓ Branch 1 taken 729920 times.
731577 if (x + stride > w)
275 1657 return 0;
276 }
277 /* decode aborted run */
278 783661 r = ff_log2_run[state->run_index[comp]];
279
2/2
✓ Branch 0 taken 647235 times.
✓ Branch 1 taken 136426 times.
783661 if (r)
280 647235 r = get_bits_long(&s->gb, r);
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
783661 if (x + r * stride > w) {
282 r = (w - x) / stride;
283 }
284
2/2
✓ Branch 0 taken 1010453 times.
✓ Branch 1 taken 783661 times.
1794114 for (i = 0; i < r; i++) {
285
1/2
✓ Branch 0 taken 1010453 times.
✗ Branch 1 not taken.
1010453 W(dst, x, Ra);
286 1010453 x += stride;
287 }
288
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
783661 if (x >= w) {
290 av_log(NULL, AV_LOG_ERROR, "run overflow\n");
291 av_assert0(x <= w);
292 return AVERROR_INVALIDDATA;
293 }
294
295 /* decode run termination value */
296
1/2
✓ Branch 0 taken 783661 times.
✗ Branch 1 not taken.
783661 Rb = R(last, x);
297 783661 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
298 783661 err = ls_get_code_runterm(&s->gb, state, RItype,
299 783661 ff_log2_run[state->run_index[comp]]);
300
2/2
✓ Branch 0 taken 729428 times.
✓ Branch 1 taken 54233 times.
783661 if (state->run_index[comp])
301 729428 state->run_index[comp]--;
302
303
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
783661 if (state->near && RItype) {
304 pred = Ra + err;
305 } else {
306
2/2
✓ Branch 0 taken 170007 times.
✓ Branch 1 taken 613654 times.
783661 if (Rb < Ra)
307 170007 pred = Rb - err;
308 else
309 613654 pred = Rb + err;
310 }
311 } else { /* regular mode */
312 int context, sign;
313
314 46507616 context = ff_jpegls_quantize(state, D0) * 81 +
315 46507616 ff_jpegls_quantize(state, D1) * 9 +
316 46507616 ff_jpegls_quantize(state, D2);
317 46507616 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
318
319
2/2
✓ Branch 0 taken 19034217 times.
✓ Branch 1 taken 27473399 times.
46507616 if (context < 0) {
320 19034217 context = -context;
321 19034217 sign = 1;
322 } else {
323 27473399 sign = 0;
324 }
325
326
2/2
✓ Branch 0 taken 19034217 times.
✓ Branch 1 taken 27473399 times.
46507616 if (sign) {
327 19034217 pred = av_clip(pred - state->C[context], 0, state->maxval);
328 19034217 err = -ls_get_code_regular(&s->gb, state, context);
329 } else {
330 27473399 pred = av_clip(pred + state->C[context], 0, state->maxval);
331 27473399 err = ls_get_code_regular(&s->gb, state, context);
332 }
333
334 /* we have to do something more for near-lossless coding */
335 46507616 pred += err;
336 }
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47291277 times.
47291277 if (state->near) {
338 if (pred < -state->near)
339 pred += state->range * state->twonear;
340 else if (pred > state->maxval + state->near)
341 pred -= state->range * state->twonear;
342 pred = av_clip(pred, 0, state->maxval);
343 }
344
345 47291277 pred &= state->maxval;
346
1/2
✓ Branch 0 taken 47291277 times.
✗ Branch 1 not taken.
47291277 W(dst, x, pred);
347 47291277 x += stride;
348 }
349
350 135951 return 0;
351 }
352
353 213 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
354 int point_transform, int ilv)
355 {
356 213 int i, t = 0;
357 uint8_t *zero, *last, *cur;
358 213 JLSState *state = s->jls_state;
359 213 int off = 0, stride = 1, width, shift, ret = 0;
360 213 int decoded_height = 0;
361
362
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 196 times.
213 if (!state) {
363 17 state = av_malloc(sizeof(*state));
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!state)
365 return AVERROR(ENOMEM);
366 17 s->jls_state = state;
367 }
368 213 zero = av_mallocz(s->picture_ptr->linesize[0]);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (!zero)
370 return AVERROR(ENOMEM);
371 213 last = zero;
372 213 cur = s->picture_ptr->data[0];
373
374 /* initialize JPEG-LS state from JPEG parameters */
375 213 memset(state, 0, sizeof(*state));
376 213 state->near = near;
377 213 state->bpp = (s->bits < 2) ? 2 : s->bits;
378 213 state->maxval = s->maxval;
379 213 state->T1 = s->t1;
380 213 state->T2 = s->t2;
381 213 state->T3 = s->t3;
382 213 state->reset = s->reset;
383 213 ff_jpegls_reset_coding_parameters(state, 0);
384 213 ff_jpegls_init_state(state);
385
386
1/2
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
213 if (s->bits <= 8)
387 213 shift = point_transform + (8 - s->bits);
388 else
389 shift = point_transform + (16 - s->bits);
390
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (shift >= 16) {
392 ret = AVERROR_INVALIDDATA;
393 goto end;
394 }
395
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
397 av_log(s->avctx, AV_LOG_DEBUG,
398 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
399 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
400 s->width, s->height, state->near, state->maxval,
401 state->T1, state->T2, state->T3,
402 state->reset, state->limit, state->qbpp, state->range);
403 av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
404 ilv, point_transform, s->bits, s->cur_scan);
405 }
406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
213 if (get_bits_left(&s->gb) < s->height) {
407 ret = AVERROR_INVALIDDATA;
408 goto end;
409 }
410
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 205 times.
213 if (ilv == 0) { /* separate planes */
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (s->cur_scan > s->nb_components) {
412 ret = AVERROR_INVALIDDATA;
413 goto end;
414 }
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 stride = (s->nb_components > 1) ? 3 : 1;
416 8 off = av_clip(s->cur_scan - 1, 0, stride - 1);
417 8 width = s->width * stride;
418 8 cur += off;
419
2/2
✓ Branch 0 taken 5688 times.
✓ Branch 1 taken 8 times.
5696 for (i = 0; i < s->height; i++) {
420 int ret;
421
1/2
✓ Branch 0 taken 5688 times.
✗ Branch 1 not taken.
5688 if (s->bits <= 8) {
422 5688 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
423 5688 t = last[0];
424 } else {
425 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
426 t = *((uint16_t *)last);
427 }
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5688 times.
5688 if (ret < 0)
429 break;
430 5688 last = cur;
431 5688 cur += s->picture_ptr->linesize[0];
432
433
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5688 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5688 if (s->restart_interval && !--s->restart_count) {
434 align_get_bits(&s->gb);
435 skip_bits(&s->gb, 16); /* skip RSTn */
436 }
437 }
438 8 decoded_height = i;
439
1/2
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
205 } else if (ilv == 1) { /* line interleaving */
440 int j;
441 205 int Rc[3] = { 0, 0, 0 };
442
1/2
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
205 stride = (s->nb_components > 1) ? 3 : 1;
443 205 memset(cur, 0, s->picture_ptr->linesize[0]);
444 205 width = s->width * stride;
445
2/2
✓ Branch 0 taken 46086 times.
✓ Branch 1 taken 205 times.
46291 for (i = 0; i < s->height; i++) {
446 int ret;
447
2/2
✓ Branch 0 taken 138258 times.
✓ Branch 1 taken 46086 times.
184344 for (j = 0; j < stride; j++) {
448 138258 ret = ls_decode_line(state, s, last + j, cur + j,
449 Rc[j], width, stride, j, 8);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138258 times.
138258 if (ret < 0)
451 break;
452 138258 Rc[j] = last[j];
453
454
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 138258 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
138258 if (s->restart_interval && !--s->restart_count) {
455 align_get_bits(&s->gb);
456 skip_bits(&s->gb, 16); /* skip RSTn */
457 }
458 }
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46086 times.
46086 if (ret < 0)
460 break;
461 46086 last = cur;
462 46086 cur += s->picture_ptr->linesize[0];
463 }
464 205 decoded_height = i;
465 } else if (ilv == 2) { /* sample interleaving */
466 avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
467 ret = AVERROR_PATCHWELCOME;
468 goto end;
469 } else { /* unknown interleaving */
470 avpriv_report_missing_feature(s->avctx, "Unknown interleaved images");
471 ret = AVERROR_PATCHWELCOME;
472 goto end;
473 }
474
475
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
213 if (s->xfrm && s->nb_components == 3) {
476 int x, w;
477
478 w = s->width * s->nb_components;
479
480 if (s->bits <= 8) {
481 uint8_t *src = s->picture_ptr->data[0];
482
483 for (i = 0; i < s->height; i++) {
484 switch(s->xfrm) {
485 case 1:
486 for (x = off; x < w; x += 3) {
487 src[x ] += src[x+1] + 128;
488 src[x+2] += src[x+1] + 128;
489 }
490 break;
491 case 2:
492 for (x = off; x < w; x += 3) {
493 src[x ] += src[x+1] + 128;
494 src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
495 }
496 break;
497 case 3:
498 for (x = off; x < w; x += 3) {
499 int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
500 src[x+0] = src[x+2] + g + 128;
501 src[x+2] = src[x+1] + g + 128;
502 src[x+1] = g;
503 }
504 break;
505 case 4:
506 for (x = off; x < w; x += 3) {
507 int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
508 int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
509 int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
510 src[x+0] = av_clip_uint8(r);
511 src[x+1] = av_clip_uint8(g);
512 src[x+2] = av_clip_uint8(b);
513 }
514 break;
515 }
516 src += s->picture_ptr->linesize[0];
517 }
518 }else
519 avpriv_report_missing_feature(s->avctx, "16bit xfrm");
520 }
521
522
2/2
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 8 times.
213 if (shift) { /* we need to do point transform or normalize samples */
523 int x, w;
524
525 8 w = s->width * s->nb_components;
526
527
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->bits <= 8) {
528 8 uint8_t *src = s->picture_ptr->data[0];
529
530
2/2
✓ Branch 0 taken 5688 times.
✓ Branch 1 taken 8 times.
5696 for (i = 0; i < decoded_height; i++) {
531
2/2
✓ Branch 0 taken 4044168 times.
✓ Branch 1 taken 5688 times.
4049856 for (x = off; x < w; x += stride)
532 4044168 src[x] <<= shift;
533 5688 src += s->picture_ptr->linesize[0];
534 }
535 } else {
536 uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
537
538 for (i = 0; i < decoded_height; i++) {
539 for (x = 0; x < w; x++)
540 src[x] <<= shift;
541 src += s->picture_ptr->linesize[0] / 2;
542 }
543 }
544 }
545
546 205 end:
547 213 av_free(zero);
548
549 213 return ret;
550 }
551
552 const AVCodec ff_jpegls_decoder = {
553 .name = "jpegls",
554 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
555 .type = AVMEDIA_TYPE_VIDEO,
556 .id = AV_CODEC_ID_JPEGLS,
557 .priv_data_size = sizeof(MJpegDecodeContext),
558 .init = ff_mjpeg_decode_init,
559 .close = ff_mjpeg_decode_end,
560 .receive_frame = ff_mjpeg_receive_frame,
561 .capabilities = AV_CODEC_CAP_DR1,
562 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
563 FF_CODEC_CAP_SETS_PKT_DTS,
564 };
565