/src/ffmpeg/libavcodec/jpeglsdec.c
Line | Count | 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 | | int ff_jpegls_decode_lse(MJpegDecodeContext *s) |
53 | 126k | { |
54 | 126k | int id; |
55 | 126k | int tid, wt, maxtab, i, j; |
56 | | |
57 | 126k | int len = bytestream2_get_be16(&s->gB); |
58 | 126k | id = bytestream2_get_byte(&s->gB); |
59 | | |
60 | 126k | switch (id) { |
61 | 34.4k | case 1: |
62 | 34.4k | if (len < 13) |
63 | 9.74k | return AVERROR_INVALIDDATA; |
64 | | |
65 | 24.6k | s->maxval = bytestream2_get_be16u(&s->gB); |
66 | 24.6k | s->t1 = bytestream2_get_be16u(&s->gB); |
67 | 24.6k | s->t2 = bytestream2_get_be16u(&s->gB); |
68 | 24.6k | s->t3 = bytestream2_get_be16u(&s->gB); |
69 | 24.6k | s->reset = bytestream2_get_be16u(&s->gB); |
70 | | |
71 | 24.6k | if (s->avctx->debug & FF_DEBUG_PICT_INFO) { |
72 | 0 | av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n", |
73 | 0 | s->maxval, s->t1, s->t2, s->t3, s->reset); |
74 | 0 | } |
75 | | |
76 | | // ff_jpegls_reset_coding_parameters(s, 0); |
77 | | //FIXME quant table? |
78 | 24.6k | break; |
79 | 30.8k | case 2: |
80 | 30.8k | s->palette_index = 0; |
81 | 30.8k | av_fallthrough; |
82 | 81.0k | case 3: |
83 | 81.0k | tid= bytestream2_get_byte(&s->gB); |
84 | 81.0k | wt = bytestream2_get_byte(&s->gB); |
85 | | |
86 | 81.0k | if (len < 5) |
87 | 2.30k | return AVERROR_INVALIDDATA; |
88 | | |
89 | 78.7k | if (wt < 1 || wt > MAX_COMPONENTS) { |
90 | 9.64k | avpriv_request_sample(s->avctx, "wt %d", wt); |
91 | 9.64k | return AVERROR_PATCHWELCOME; |
92 | 9.64k | } |
93 | | |
94 | 69.1k | if (!s->maxval) |
95 | 58.4k | maxtab = 255; |
96 | 10.6k | else if ((5 + wt*(s->maxval+1)) < 65535) |
97 | 8.94k | maxtab = s->maxval; |
98 | 1.73k | else |
99 | 1.73k | maxtab = 65530/wt - 1; |
100 | | |
101 | 69.1k | if (s->avctx->debug & FF_DEBUG_PICT_INFO) { |
102 | 0 | av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab); |
103 | 0 | } |
104 | 69.1k | if (maxtab >= 256) { |
105 | 3.62k | avpriv_request_sample(s->avctx, ">8bit palette"); |
106 | 3.62k | return AVERROR_PATCHWELCOME; |
107 | 3.62k | } |
108 | 65.5k | maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index); |
109 | | |
110 | 65.5k | if (s->palette_index > maxtab) |
111 | 1.86k | return AVERROR_INVALIDDATA; |
112 | | |
113 | 63.6k | if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) && |
114 | 40.8k | (s->picture_ptr->format == AV_PIX_FMT_GRAY8 || s->picture_ptr->format == AV_PIX_FMT_PAL8)) { |
115 | 39.9k | uint32_t *pal = (uint32_t *)s->picture_ptr->data[1]; |
116 | 39.9k | int shift = 0; |
117 | | |
118 | 39.9k | if (s->avctx->bits_per_raw_sample > 0 && s->avctx->bits_per_raw_sample < 8) { |
119 | 31.6k | maxtab = FFMIN(maxtab, (1<<s->avctx->bits_per_raw_sample)-1); |
120 | 31.6k | shift = 8 - s->avctx->bits_per_raw_sample; |
121 | 31.6k | } |
122 | | |
123 | 39.9k | s->force_pal8++; |
124 | 39.9k | if (!pal) { |
125 | 23.4k | if (s->force_pal8 > 1) |
126 | 3.25k | return AVERROR_INVALIDDATA; |
127 | 20.2k | return 1; |
128 | 23.4k | } |
129 | | |
130 | 700k | for (i=s->palette_index; i<=maxtab; i++) { |
131 | 684k | uint8_t k = i << shift; |
132 | 684k | pal[k] = wt < 4 ? 0xFF000000 : 0; |
133 | 1.73M | for (j=0; j<wt; j++) { |
134 | 1.05M | pal[k] |= bytestream2_get_byte(&s->gB) << (8*(wt-j-1)); |
135 | 1.05M | } |
136 | 684k | } |
137 | 16.4k | s->palette_index = i; |
138 | 16.4k | } |
139 | 40.2k | break; |
140 | 40.2k | case 4: |
141 | 2.02k | avpriv_request_sample(s->avctx, "oversize image"); |
142 | 2.02k | return AVERROR(ENOSYS); |
143 | 8.92k | default: |
144 | 8.92k | av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id); |
145 | 8.92k | return AVERROR_INVALIDDATA; |
146 | 126k | } |
147 | 64.8k | ff_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3); |
148 | | |
149 | 64.8k | return 0; |
150 | 126k | } |
151 | | |
152 | | /** |
153 | | * Get context-dependent Golomb code, decode it and update context |
154 | | */ |
155 | | static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q) |
156 | 31.7M | { |
157 | 31.7M | int k, ret; |
158 | | |
159 | 169M | for (k = 0; ((unsigned)state->N[Q] << k) < state->A[Q]; k++) |
160 | 137M | ; |
161 | | |
162 | | #ifdef JLS_BROKEN |
163 | | if (!show_bits_long(gb, 32)) |
164 | | return -1; |
165 | | #endif |
166 | 31.7M | ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp); |
167 | | |
168 | | /* decode mapped error */ |
169 | 31.7M | if (ret & 1) |
170 | 15.8M | ret = -(ret + 1 >> 1); |
171 | 15.8M | else |
172 | 15.8M | ret >>= 1; |
173 | | |
174 | | /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */ |
175 | 31.7M | if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q])) |
176 | 541k | ret = -(ret + 1); |
177 | | |
178 | 31.7M | ret = ff_jpegls_update_state_regular(state, Q, ret); |
179 | | |
180 | 31.7M | return ret; |
181 | 31.7M | } |
182 | | |
183 | | /** |
184 | | * Get Golomb code, decode it and update state for run termination |
185 | | */ |
186 | | static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, |
187 | | int RItype, int limit_add) |
188 | 13.9M | { |
189 | 13.9M | int k, ret, temp, map; |
190 | 13.9M | int Q = 365 + RItype; |
191 | | |
192 | 13.9M | temp = state->A[Q]; |
193 | 13.9M | if (RItype) |
194 | 13.9M | temp += state->N[Q] >> 1; |
195 | | |
196 | 158M | for (k = 0; ((unsigned)state->N[Q] << k) < temp; k++) |
197 | 144M | ; |
198 | | |
199 | | #ifdef JLS_BROKEN |
200 | | if (!show_bits_long(gb, 32)) |
201 | | return -1; |
202 | | #endif |
203 | 13.9M | ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, |
204 | 13.9M | state->qbpp); |
205 | 13.9M | if (ret < 0) |
206 | 2.79M | return -0x10000; |
207 | | |
208 | | /* decode mapped error */ |
209 | 11.1M | map = 0; |
210 | 11.1M | if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q])) |
211 | 189k | map = 1; |
212 | 11.1M | ret += RItype + map; |
213 | | |
214 | 11.1M | if (ret & 1) { |
215 | 9.03M | ret = map - (ret + 1 >> 1); |
216 | 9.03M | state->B[Q]++; |
217 | 9.03M | } else { |
218 | 2.16M | ret = ret >> 1; |
219 | 2.16M | } |
220 | | |
221 | 11.1M | if (FFABS(ret) > 0xFFFF) |
222 | 1.49M | return -0x10000; |
223 | | /* update state */ |
224 | 9.69M | state->A[Q] += FFABS(ret) - RItype; |
225 | 9.69M | ret *= state->twonear; |
226 | 9.69M | ff_jpegls_downscale_state(state, Q); |
227 | | |
228 | 9.69M | return ret; |
229 | 11.1M | } |
230 | | |
231 | | /** |
232 | | * Decode one line of image |
233 | | */ |
234 | | 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 | 2.32M | { |
238 | 2.32M | int i, x = 0; |
239 | 2.32M | int Ra, Rb, Rc, Rd; |
240 | 2.32M | int D0, D1, D2; |
241 | | |
242 | 48.0M | while (x < w) { |
243 | 46.6M | int err, pred; |
244 | | |
245 | 46.6M | if (get_bits_left(&s->gb) <= 0) |
246 | 232k | return AVERROR_INVALIDDATA; |
247 | | |
248 | | /* compute gradients */ |
249 | 46.3M | Ra = x ? R(dst, x - stride) : R(last, x); |
250 | 46.3M | Rb = R(last, x); |
251 | 46.3M | Rc = x ? R(last, x - stride) : last2; |
252 | 46.3M | Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride); |
253 | 46.3M | D0 = Rd - Rb; |
254 | 46.3M | D1 = Rb - Rc; |
255 | 46.3M | D2 = Rc - Ra; |
256 | | /* run mode */ |
257 | 46.3M | if ((FFABS(D0) <= state->near) && |
258 | 36.6M | (FFABS(D1) <= state->near) && |
259 | 34.3M | (FFABS(D2) <= state->near)) { |
260 | 14.6M | int r; |
261 | 14.6M | int RItype; |
262 | | |
263 | | /* decode full runs while available */ |
264 | 20.2M | while (get_bits1(&s->gb)) { |
265 | 6.20M | int r; |
266 | 6.20M | r = 1 << ff_log2_run[state->run_index[comp]]; |
267 | 6.20M | if (x + r * stride > w) |
268 | 280k | r = (w - x) / stride; |
269 | 735M | for (i = 0; i < r; i++) { |
270 | 729M | W(dst, x, Ra); |
271 | 729M | x += stride; |
272 | 729M | } |
273 | | /* if EOL reached, we stop decoding */ |
274 | 6.20M | if (r != 1 << ff_log2_run[state->run_index[comp]]) |
275 | 280k | return 0; |
276 | 5.91M | if (state->run_index[comp] < 31) |
277 | 5.91M | state->run_index[comp]++; |
278 | 5.91M | if (x + stride > w) |
279 | 336k | return 0; |
280 | 5.91M | } |
281 | | /* decode aborted run */ |
282 | 14.0M | r = ff_log2_run[state->run_index[comp]]; |
283 | 14.0M | if (r) |
284 | 1.75M | r = get_bits(&s->gb, r); |
285 | 14.0M | if (x + r * stride > w) { |
286 | 8.24k | r = (w - x) / stride; |
287 | 8.24k | } |
288 | 108M | for (i = 0; i < r; i++) { |
289 | 94.6M | W(dst, x, Ra); |
290 | 94.6M | x += stride; |
291 | 94.6M | } |
292 | | |
293 | 14.0M | if (x >= w) { |
294 | 11.9k | av_log(NULL, AV_LOG_ERROR, "run overflow\n"); |
295 | 11.9k | av_assert0(x <= w); |
296 | 11.9k | return AVERROR_INVALIDDATA; |
297 | 11.9k | } |
298 | | |
299 | | /* decode run termination value */ |
300 | 13.9M | Rb = R(last, x); |
301 | 13.9M | RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0; |
302 | 13.9M | err = ls_get_code_runterm(&s->gb, state, RItype, |
303 | 13.9M | ff_log2_run[state->run_index[comp]]); |
304 | 13.9M | if (state->run_index[comp]) |
305 | 3.61M | state->run_index[comp]--; |
306 | | |
307 | 13.9M | if (state->near && RItype) { |
308 | 12.1M | pred = Ra + err; |
309 | 12.1M | } else { |
310 | 1.79M | if (Rb < Ra) |
311 | 35.5k | pred = Rb - err; |
312 | 1.76M | else |
313 | 1.76M | pred = Rb + err; |
314 | 1.79M | } |
315 | 31.7M | } else { /* regular mode */ |
316 | 31.7M | int context, sign; |
317 | | |
318 | 31.7M | context = ff_jpegls_quantize(state, D0) * 81 + |
319 | 31.7M | ff_jpegls_quantize(state, D1) * 9 + |
320 | 31.7M | ff_jpegls_quantize(state, D2); |
321 | 31.7M | pred = mid_pred(Ra, Ra + Rb - Rc, Rb); |
322 | | |
323 | 31.7M | if (context < 0) { |
324 | 25.6M | context = -context; |
325 | 25.6M | sign = 1; |
326 | 25.6M | } else { |
327 | 6.16M | sign = 0; |
328 | 6.16M | } |
329 | | |
330 | 31.7M | if (sign) { |
331 | 25.6M | pred = av_clip(pred - state->C[context], 0, state->maxval); |
332 | 25.6M | err = -ls_get_code_regular(&s->gb, state, context); |
333 | 25.6M | } else { |
334 | 6.16M | pred = av_clip(pred + state->C[context], 0, state->maxval); |
335 | 6.16M | err = ls_get_code_regular(&s->gb, state, context); |
336 | 6.16M | } |
337 | | |
338 | | /* we have to do something more for near-lossless coding */ |
339 | 31.7M | pred += err; |
340 | 31.7M | } |
341 | 45.7M | if (state->near) { |
342 | 26.6M | if (pred < -state->near) |
343 | 13.6M | pred += state->range * state->twonear; |
344 | 13.0M | else if (pred > state->maxval + state->near) |
345 | 4.27M | pred -= state->range * state->twonear; |
346 | 26.6M | pred = av_clip(pred, 0, state->maxval); |
347 | 26.6M | } |
348 | | |
349 | 45.7M | pred &= state->maxval; |
350 | 45.7M | W(dst, x, pred); |
351 | 45.7M | x += stride; |
352 | 45.7M | } |
353 | | |
354 | 1.46M | return 0; |
355 | 2.32M | } |
356 | | |
357 | | int ff_jpegls_decode_picture(MJpegDecodeContext *s) |
358 | 368k | { |
359 | 368k | int near = s->Ss; |
360 | 368k | int point_transform = s->Al; |
361 | 368k | int ilv = s->Se; |
362 | 368k | int i, t = 0; |
363 | 368k | uint8_t *zero, *last, *cur; |
364 | 368k | JLSState *state = s->jls_state; |
365 | 368k | int off = 0, stride = 1, width, shift, ret = 0; |
366 | 368k | 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 | 368k | if (s->total_ls_decoded_height > s->height * 2LL * MAX_COMPONENTS) |
375 | 25.5k | return AVERROR_INVALIDDATA; |
376 | | |
377 | 342k | if (!state) { |
378 | 12.1k | state = av_malloc(sizeof(*state)); |
379 | 12.1k | if (!state) |
380 | 0 | return AVERROR(ENOMEM); |
381 | 12.1k | s->jls_state = state; |
382 | 12.1k | } |
383 | 342k | zero = av_mallocz(s->picture_ptr->linesize[0]); |
384 | 342k | if (!zero) |
385 | 0 | return AVERROR(ENOMEM); |
386 | 342k | last = zero; |
387 | 342k | cur = s->picture_ptr->data[0]; |
388 | | |
389 | | /* initialize JPEG-LS state from JPEG parameters */ |
390 | 342k | state->near = near; |
391 | 342k | state->bpp = (s->bits < 2) ? 2 : s->bits; |
392 | 342k | state->maxval = s->maxval; |
393 | 342k | state->T1 = s->t1; |
394 | 342k | state->T2 = s->t2; |
395 | 342k | state->T3 = s->t3; |
396 | 342k | state->reset = s->reset; |
397 | 342k | 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 | 342k | if (state->maxval >= (1<<state->bpp) || |
403 | 330k | state->T1 > state->T2 || |
404 | 326k | state->T2 > state->T3 || |
405 | 321k | state->T3 > state->maxval || |
406 | 309k | state->reset > FFMAX(255, state->maxval)) { |
407 | 35.9k | ret = AVERROR_INVALIDDATA; |
408 | 35.9k | goto end; |
409 | 35.9k | } |
410 | | |
411 | 306k | if (s->bits <= 8) |
412 | 94.4k | shift = point_transform + (8 - s->bits); |
413 | 212k | else |
414 | 212k | shift = point_transform + (16 - s->bits); |
415 | | |
416 | 306k | if (shift >= 16) { |
417 | 11.2k | ret = AVERROR_INVALIDDATA; |
418 | 11.2k | goto end; |
419 | 11.2k | } |
420 | | |
421 | 295k | if (s->avctx->debug & FF_DEBUG_PICT_INFO) { |
422 | 0 | av_log(s->avctx, AV_LOG_DEBUG, |
423 | 0 | "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) " |
424 | 0 | "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n", |
425 | 0 | s->width, s->height, state->near, state->maxval, |
426 | 0 | state->T1, state->T2, state->T3, |
427 | 0 | state->reset, state->limit, state->qbpp, state->range); |
428 | 0 | av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", |
429 | 0 | ilv, point_transform, s->bits, s->cur_scan); |
430 | 0 | } |
431 | | |
432 | 295k | s->restart_count = -1; |
433 | | |
434 | 295k | if (ilv == 0) { /* separate planes */ |
435 | 77.8k | if (s->cur_scan > s->nb_components) { |
436 | 17.0k | ret = AVERROR_INVALIDDATA; |
437 | 17.0k | goto end; |
438 | 17.0k | } |
439 | 60.8k | stride = (s->nb_components > 1) ? 3 : 1; |
440 | 60.8k | off = av_clip(s->cur_scan - 1, 0, stride - 1); |
441 | 60.8k | width = s->width * stride; |
442 | 60.8k | cur += off; |
443 | 878k | for (i = 0; i < s->height; i++) { |
444 | 874k | int restart; |
445 | 874k | ret = ff_mjpeg_handle_restart(s, &restart); |
446 | 874k | if (ret < 0) |
447 | 0 | goto end; |
448 | 874k | if (restart) { |
449 | 303k | ff_jpegls_init_state(state); |
450 | 303k | t = 0; |
451 | 303k | last = zero; |
452 | 303k | } |
453 | 874k | if (s->bits <= 8) { |
454 | 436k | ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8); |
455 | 436k | t = last[0]; |
456 | 437k | } else { |
457 | 437k | ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16); |
458 | 437k | t = *((uint16_t *)last); |
459 | 437k | } |
460 | 874k | if (ret < 0) |
461 | 57.4k | break; |
462 | 817k | last = cur; |
463 | 817k | cur += s->picture_ptr->linesize[0]; |
464 | 817k | } |
465 | 60.8k | decoded_height = i; |
466 | 217k | } else if (ilv == 1) { /* line interleaving */ |
467 | 191k | int j; |
468 | 191k | int Rc[3] = { 0, 0, 0 }; |
469 | 191k | stride = (s->nb_components > 1) ? 3 : 1; |
470 | 191k | memset(cur, 0, s->picture_ptr->linesize[0]); |
471 | 191k | width = s->width * stride; |
472 | 1.35M | for (i = 0; i < s->height; i++) { |
473 | 1.35M | int restart; |
474 | 1.35M | ret = ff_mjpeg_handle_restart(s, &restart); |
475 | 1.35M | if (ret < 0) |
476 | 0 | goto end; |
477 | 1.35M | if (restart) { |
478 | 630k | ff_jpegls_init_state(state); |
479 | 630k | memset(Rc, 0, sizeof(Rc)); |
480 | 630k | last = zero; |
481 | 630k | } |
482 | 2.61M | for (j = 0; j < stride; j++) { |
483 | 1.45M | ret = ls_decode_line(state, s, last + j, cur + j, |
484 | 1.45M | Rc[j], width, stride, j, 8); |
485 | 1.45M | if (ret < 0) |
486 | 186k | break; |
487 | 1.26M | Rc[j] = last[j]; |
488 | 1.26M | } |
489 | 1.35M | if (ret < 0) |
490 | 186k | break; |
491 | 1.16M | last = cur; |
492 | 1.16M | cur += s->picture_ptr->linesize[0]; |
493 | 1.16M | } |
494 | 191k | decoded_height = i; |
495 | 191k | } else if (ilv == 2) { /* sample interleaving */ |
496 | 2.79k | avpriv_report_missing_feature(s->avctx, "Sample interleaved images"); |
497 | 2.79k | ret = AVERROR_PATCHWELCOME; |
498 | 2.79k | goto end; |
499 | 23.1k | } else { /* unknown interleaving */ |
500 | 23.1k | avpriv_report_missing_feature(s->avctx, "Unknown interleaved images"); |
501 | 23.1k | ret = AVERROR_PATCHWELCOME; |
502 | 23.1k | goto end; |
503 | 23.1k | } |
504 | | |
505 | 252k | s->total_ls_decoded_height += decoded_height; |
506 | | |
507 | 252k | if (s->xfrm && s->nb_components == 3) { |
508 | 11.0k | int x, w; |
509 | | |
510 | 11.0k | w = s->width * s->nb_components; |
511 | | |
512 | 11.0k | if (s->bits <= 8) { |
513 | 11.0k | uint8_t *src = s->picture_ptr->data[0]; |
514 | | |
515 | 296k | for (i = 0; i < decoded_height; i++) { |
516 | 285k | switch(s->xfrm) { |
517 | 23.9k | case 1: |
518 | 3.48M | for (x = off; x + 2 < w; x += 3) { |
519 | 3.45M | src[x ] += src[x+1] + 128; |
520 | 3.45M | src[x+2] += src[x+1] + 128; |
521 | 3.45M | } |
522 | 23.9k | break; |
523 | 39.9k | case 2: |
524 | 22.7M | for (x = off; x + 2 < w; x += 3) { |
525 | 22.7M | src[x ] += src[x+1] + 128; |
526 | 22.7M | src[x+2] += ((src[x ] + src[x+1])>>1) + 128; |
527 | 22.7M | } |
528 | 39.9k | break; |
529 | 194k | case 3: |
530 | 28.6M | for (x = off; x + 2 < w; x += 3) { |
531 | 28.4M | int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64; |
532 | 28.4M | src[x+0] = src[x+2] + g + 128; |
533 | 28.4M | src[x+2] = src[x+1] + g + 128; |
534 | 28.4M | src[x+1] = g; |
535 | 28.4M | } |
536 | 194k | break; |
537 | 24.6k | case 4: |
538 | 2.20M | for (x = off; x + 2 < w; x += 3) { |
539 | 2.18M | int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8); |
540 | 2.18M | int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8); |
541 | 2.18M | int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8); |
542 | 2.18M | src[x+0] = av_clip_uint8(r); |
543 | 2.18M | src[x+1] = av_clip_uint8(g); |
544 | 2.18M | src[x+2] = av_clip_uint8(b); |
545 | 2.18M | } |
546 | 24.6k | break; |
547 | 285k | } |
548 | 285k | src += s->picture_ptr->linesize[0]; |
549 | 285k | } |
550 | 11.0k | }else |
551 | 0 | avpriv_report_missing_feature(s->avctx, "16bit xfrm"); |
552 | 11.0k | } |
553 | | |
554 | 252k | if (shift) { /* we need to do point transform or normalize samples */ |
555 | 230k | int x, w; |
556 | | |
557 | 230k | w = s->width * s->nb_components; |
558 | | |
559 | 230k | if (s->bits <= 8) { |
560 | 59.4k | uint8_t *src = s->picture_ptr->data[0]; |
561 | | |
562 | 585k | for (i = 0; i < decoded_height; i++) { |
563 | 351M | for (x = off; x < w; x += stride) |
564 | 350M | src[x] <<= shift; |
565 | 525k | src += s->picture_ptr->linesize[0]; |
566 | 525k | } |
567 | 170k | } else { |
568 | 170k | uint16_t *src = (uint16_t *)s->picture_ptr->data[0]; |
569 | | |
570 | 1.57M | for (i = 0; i < decoded_height; i++) { |
571 | 308M | for (x = 0; x < w; x++) |
572 | 306M | src[x] <<= shift; |
573 | 1.40M | src += s->picture_ptr->linesize[0] / 2; |
574 | 1.40M | } |
575 | 170k | } |
576 | 230k | } |
577 | | |
578 | 342k | end: |
579 | 342k | av_free(zero); |
580 | | |
581 | 342k | return ret; |
582 | 252k | } |
583 | | |
584 | | const FFCodec ff_jpegls_decoder = { |
585 | | .p.name = "jpegls", |
586 | | CODEC_LONG_NAME("JPEG-LS"), |
587 | | .p.type = AVMEDIA_TYPE_VIDEO, |
588 | | .p.id = AV_CODEC_ID_JPEGLS, |
589 | | .priv_data_size = sizeof(MJpegDecodeContext), |
590 | | .init = ff_mjpeg_decode_init, |
591 | | .close = ff_mjpeg_decode_end, |
592 | | FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame), |
593 | | .p.capabilities = AV_CODEC_CAP_DR1, |
594 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
595 | | }; |