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