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 | 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 | 16 | if (len < 5) | |
| 87 | ✗ | return AVERROR_INVALIDDATA; | |
| 88 | |||
| 89 | 16 | if (wt < 1 || wt > MAX_COMPONENTS) { | |
| 90 | ✗ | avpriv_request_sample(s->avctx, "wt %d", wt); | |
| 91 | ✗ | return AVERROR_PATCHWELCOME; | |
| 92 | } | ||
| 93 | |||
| 94 | 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 | 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 | 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 | 16 | if (s->palette_index > maxtab) | |
| 111 | ✗ | return AVERROR_INVALIDDATA; | |
| 112 | |||
| 113 | 16 | if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) && | |
| 114 | 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 | 16 | if (s->avctx->bits_per_raw_sample > 0 && s->avctx->bits_per_raw_sample < 8) { | |
| 119 | 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 | 16 | if (!pal) { | |
| 125 | 8 | if (s->force_pal8 > 1) | |
| 126 | ✗ | return AVERROR_INVALIDDATA; | |
| 127 | 8 | return 1; | |
| 128 | } | ||
| 129 | |||
| 130 | 352 | for (i=s->palette_index; i<=maxtab; i++) { | |
| 131 | 344 | uint8_t k = i << shift; | |
| 132 | 344 | pal[k] = wt < 4 ? 0xFF000000 : 0; | |
| 133 | 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 | 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 | 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 | 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 | 785305 | if (RItype) | |
| 194 | 510394 | temp += state->N[Q] >> 1; | |
| 195 | |||
| 196 | 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 | 785305 | if (ret < 0) | |
| 206 | ✗ | return -0x10000; | |
| 207 | |||
| 208 | /* decode mapped error */ | ||
| 209 | 785305 | map = 0; | |
| 210 | 785305 | if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q])) | |
| 211 | 60548 | map = 1; | |
| 212 | 785305 | ret += RItype + map; | |
| 213 | |||
| 214 | 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 | 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 | 47575543 | while (x < w) { | |
| 243 | int err, pred; | ||
| 244 | |||
| 245 | 47438482 | if (get_bits_left(&s->gb) <= 0) | |
| 246 | ✗ | return AVERROR_INVALIDDATA; | |
| 247 | |||
| 248 | /* compute gradients */ | ||
| 249 | 47438482 | Ra = x ? R(dst, x - stride) : R(last, x); | |
| 250 | 47438482 | Rb = R(last, x); | |
| 251 | 47438482 | Rc = x ? R(last, x - stride) : last2; | |
| 252 | 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 | 47438482 | if ((FFABS(D0) <= state->near) && | |
| 258 | 5936844 | (FFABS(D1) <= state->near) && | |
| 259 | 3109733 | (FFABS(D2) <= state->near)) { | |
| 260 | int r; | ||
| 261 | int RItype; | ||
| 262 | |||
| 263 | /* decode full runs while available */ | ||
| 264 | 1523858 | while (get_bits1(&s->gb)) { | |
| 265 | int r; | ||
| 266 | 738553 | r = 1 << ff_log2_run[state->run_index[comp]]; | |
| 267 | 738553 | if (x + r * stride > w) | |
| 268 | 6410 | r = (w - x) / stride; | |
| 269 | 3496245 | for (i = 0; i < r; i++) { | |
| 270 | 2757692 | W(dst, x, Ra); | |
| 271 | 2757692 | x += stride; | |
| 272 | } | ||
| 273 | /* if EOL reached, we stop decoding */ | ||
| 274 | 738553 | if (r != 1 << ff_log2_run[state->run_index[comp]]) | |
| 275 | 6410 | return 0; | |
| 276 | 732143 | if (state->run_index[comp] < 31) | |
| 277 | 732143 | state->run_index[comp]++; | |
| 278 | 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 | 785305 | if (r) | |
| 284 | 647769 | r = get_bits(&s->gb, r); | |
| 285 | 785305 | if (x + r * stride > w) { | |
| 286 | ✗ | r = (w - x) / stride; | |
| 287 | } | ||
| 288 | 1797308 | for (i = 0; i < r; i++) { | |
| 289 | 1012003 | W(dst, x, Ra); | |
| 290 | 1012003 | x += stride; | |
| 291 | } | ||
| 292 | |||
| 293 | 785305 | 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 | 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 | 785305 | if (state->run_index[comp]) | |
| 305 | 729962 | state->run_index[comp]--; | |
| 306 | |||
| 307 | 785305 | if (state->near && RItype) { | |
| 308 | ✗ | pred = Ra + err; | |
| 309 | } else { | ||
| 310 | 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 | 46645108 | if (context < 0) { | |
| 324 | 19170011 | context = -context; | |
| 325 | 19170011 | sign = 1; | |
| 326 | } else { | ||
| 327 | 27475097 | sign = 0; | |
| 328 | } | ||
| 329 | |||
| 330 | 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 | 47430413 | 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 | 47430413 | pred &= state->maxval; | |
| 350 | 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 | 223 | if (s->total_ls_decoded_height > s->height * 2LL * MAX_COMPONENTS) | |
| 375 | ✗ | return AVERROR_INVALIDDATA; | |
| 376 | |||
| 377 | 223 | if (!state) { | |
| 378 | 23 | state = av_malloc(sizeof(*state)); | |
| 379 | 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 | 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 | 223 | if (state->maxval >= (1<<state->bpp) || | |
| 403 | 223 | state->T1 > state->T2 || | |
| 404 | 223 | state->T2 > state->T3 || | |
| 405 | 223 | state->T3 > state->maxval || | |
| 406 | 223 | state->reset > FFMAX(255, state->maxval)) { | |
| 407 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 408 | ✗ | goto end; | |
| 409 | } | ||
| 410 | |||
| 411 | 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 | 223 | if (shift >= 16) { | |
| 417 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 418 | ✗ | goto end; | |
| 419 | } | ||
| 420 | |||
| 421 | 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 | 223 | if (ilv == 0) { /* separate planes */ | |
| 435 | 14 | if (s->cur_scan > s->nb_components) { | |
| 436 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 437 | ✗ | goto end; | |
| 438 | } | ||
| 439 | 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 | 6230 | for (i = 0; i < s->height; i++) { | |
| 444 | int restart; | ||
| 445 | 6216 | ret = ff_mjpeg_handle_restart(s, &restart); | |
| 446 | 6216 | if (ret < 0) | |
| 447 | ✗ | goto end; | |
| 448 | 6216 | if (restart) { | |
| 449 | 536 | ff_jpegls_init_state(state); | |
| 450 | 536 | t = 0; | |
| 451 | 536 | last = zero; | |
| 452 | } | ||
| 453 | 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 | 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 | 209 | } else if (ilv == 1) { /* line interleaving */ | |
| 467 | int j; | ||
| 468 | 209 | int Rc[3] = { 0, 0, 0 }; | |
| 469 | 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 | 46599 | for (i = 0; i < s->height; i++) { | |
| 473 | int restart; | ||
| 474 | 46390 | ret = ff_mjpeg_handle_restart(s, &restart); | |
| 475 | 46390 | if (ret < 0) | |
| 476 | ✗ | goto end; | |
| 477 | 46390 | if (restart) { | |
| 478 | 383 | ff_jpegls_init_state(state); | |
| 479 | 383 | memset(Rc, 0, sizeof(Rc)); | |
| 480 | 383 | last = zero; | |
| 481 | } | ||
| 482 | 185304 | for (j = 0; j < stride; j++) { | |
| 483 | 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 | 138914 | if (ret < 0) | |
| 494 | ✗ | break; | |
| 495 | } | ||
| 496 | 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 | ✗ | } 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 | 223 | if (s->xfrm && s->nb_components == 3) { | |
| 515 | int x, w; | ||
| 516 | |||
| 517 | ✗ | w = s->width * s->nb_components; | |
| 518 | |||
| 519 | ✗ | if (s->bits <= 8) { | |
| 520 | ✗ | uint8_t *src = s->picture_ptr->data[0]; | |
| 521 | |||
| 522 | ✗ | for (i = 0; i < decoded_height; i++) { | |
| 523 | ✗ | switch(s->xfrm) { | |
| 524 | ✗ | case 1: | |
| 525 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | 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 | 8 | if (s->bits <= 8) { | |
| 567 | 8 | uint8_t *src = s->picture_ptr->data[0]; | |
| 568 | |||
| 569 | 5696 | for (i = 0; i < decoded_height; i++) { | |
| 570 | 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 | ✗ | for (i = 0; i < decoded_height; i++) { | |
| 578 | ✗ | 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 |