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 "codec_internal.h" | ||
30 | #include "get_bits.h" | ||
31 | #include "golomb.h" | ||
32 | #include "mathops.h" | ||
33 | #include "mjpegdec.h" | ||
34 | #include "jpegls.h" | ||
35 | #include "jpeglsdec.h" | ||
36 | |||
37 | /* | ||
38 | * Uncomment this to significantly speed up decoding of broken JPEG-LS | ||
39 | * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit. | ||
40 | * | ||
41 | * There is no Golomb code with length >= 32 bits possible, so check and | ||
42 | * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow | ||
43 | * on this errors. | ||
44 | */ | ||
45 | //#define JLS_BROKEN | ||
46 | |||
47 | /** | ||
48 | * Decode LSE block with initialization parameters | ||
49 | */ | ||
50 | 16 | int ff_jpegls_decode_lse(MJpegDecodeContext *s) | |
51 | { | ||
52 | int id; | ||
53 | int tid, wt, maxtab, i, j; | ||
54 | |||
55 | 16 | int len = get_bits(&s->gb, 16); | |
56 | 16 | id = get_bits(&s->gb, 8); | |
57 | |||
58 |
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) { |
59 | ✗ | case 1: | |
60 | ✗ | if (len < 13) | |
61 | ✗ | return AVERROR_INVALIDDATA; | |
62 | |||
63 | ✗ | s->maxval = get_bits(&s->gb, 16); | |
64 | ✗ | s->t1 = get_bits(&s->gb, 16); | |
65 | ✗ | s->t2 = get_bits(&s->gb, 16); | |
66 | ✗ | s->t3 = get_bits(&s->gb, 16); | |
67 | ✗ | s->reset = get_bits(&s->gb, 16); | |
68 | |||
69 | ✗ | if (s->avctx->debug & FF_DEBUG_PICT_INFO) { | |
70 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n", | |
71 | s->maxval, s->t1, s->t2, s->t3, s->reset); | ||
72 | } | ||
73 | |||
74 | // ff_jpegls_reset_coding_parameters(s, 0); | ||
75 | //FIXME quant table? | ||
76 | ✗ | break; | |
77 | 16 | case 2: | |
78 | 16 | s->palette_index = 0; | |
79 | 16 | case 3: | |
80 | 16 | tid= get_bits(&s->gb, 8); | |
81 | 16 | wt = get_bits(&s->gb, 8); | |
82 | |||
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (len < 5) |
84 | ✗ | return AVERROR_INVALIDDATA; | |
85 | |||
86 |
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) { |
87 | ✗ | avpriv_request_sample(s->avctx, "wt %d", wt); | |
88 | ✗ | return AVERROR_PATCHWELCOME; | |
89 | } | ||
90 | |||
91 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if (!s->maxval) |
92 | 16 | maxtab = 255; | |
93 | ✗ | else if ((5 + wt*(s->maxval+1)) < 65535) | |
94 | ✗ | maxtab = s->maxval; | |
95 | else | ||
96 | ✗ | maxtab = 65530/wt - 1; | |
97 | |||
98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) { |
99 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab); | |
100 | } | ||
101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (maxtab >= 256) { |
102 | ✗ | avpriv_request_sample(s->avctx, ">8bit palette"); | |
103 | ✗ | return AVERROR_PATCHWELCOME; | |
104 | } | ||
105 | 16 | maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index); | |
106 | |||
107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (s->palette_index > maxtab) |
108 | ✗ | return AVERROR_INVALIDDATA; | |
109 | |||
110 |
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) && |
111 |
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)) { |
112 | 16 | uint32_t *pal = (uint32_t *)s->picture_ptr->data[1]; | |
113 | 16 | int shift = 0; | |
114 | |||
115 |
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) { |
116 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | maxtab = FFMIN(maxtab, (1<<s->avctx->bits_per_raw_sample)-1); |
117 | 16 | shift = 8 - s->avctx->bits_per_raw_sample; | |
118 | } | ||
119 | |||
120 | 16 | s->force_pal8++; | |
121 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (!pal) { |
122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->force_pal8 > 1) |
123 | ✗ | return AVERROR_INVALIDDATA; | |
124 | 8 | return 1; | |
125 | } | ||
126 | |||
127 |
2/2✓ Branch 0 taken 344 times.
✓ Branch 1 taken 8 times.
|
352 | for (i=s->palette_index; i<=maxtab; i++) { |
128 | 344 | uint8_t k = i << shift; | |
129 |
1/2✓ Branch 0 taken 344 times.
✗ Branch 1 not taken.
|
344 | pal[k] = wt < 4 ? 0xFF000000 : 0; |
130 |
2/2✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 344 times.
|
1376 | for (j=0; j<wt; j++) { |
131 | 1032 | pal[k] |= get_bits(&s->gb, 8) << (8*(wt-j-1)); | |
132 | } | ||
133 | } | ||
134 | 8 | s->palette_index = i; | |
135 | } | ||
136 | 8 | break; | |
137 | ✗ | case 4: | |
138 | ✗ | avpriv_request_sample(s->avctx, "oversize image"); | |
139 | ✗ | return AVERROR(ENOSYS); | |
140 | ✗ | default: | |
141 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id); | |
142 | ✗ | return AVERROR_INVALIDDATA; | |
143 | } | ||
144 | ff_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3); | ||
145 | |||
146 | 8 | return 0; | |
147 | } | ||
148 | |||
149 | /** | ||
150 | * Get context-dependent Golomb code, decode it and update context | ||
151 | */ | ||
152 | 46507616 | static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q) | |
153 | { | ||
154 | int k, ret; | ||
155 | |||
156 |
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++) |
157 | ; | ||
158 | |||
159 | #ifdef JLS_BROKEN | ||
160 | if (!show_bits_long(gb, 32)) | ||
161 | return -1; | ||
162 | #endif | ||
163 | 46507616 | ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp); | |
164 | |||
165 | /* decode mapped error */ | ||
166 |
2/2✓ Branch 0 taken 23829574 times.
✓ Branch 1 taken 22678042 times.
|
46507616 | if (ret & 1) |
167 | 23829574 | ret = -(ret + 1 >> 1); | |
168 | else | ||
169 | 22678042 | ret >>= 1; | |
170 | |||
171 | /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */ | ||
172 |
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])) |
173 | 840496 | ret = -(ret + 1); | |
174 | |||
175 | 46507616 | ret = ff_jpegls_update_state_regular(state, Q, ret); | |
176 | |||
177 | 46507616 | return ret; | |
178 | } | ||
179 | |||
180 | /** | ||
181 | * Get Golomb code, decode it and update state for run termination | ||
182 | */ | ||
183 | 783661 | static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, | |
184 | int RItype, int limit_add) | ||
185 | { | ||
186 | int k, ret, temp, map; | ||
187 | 783661 | int Q = 365 + RItype; | |
188 | |||
189 | 783661 | temp = state->A[Q]; | |
190 |
2/2✓ Branch 0 taken 509232 times.
✓ Branch 1 taken 274429 times.
|
783661 | if (RItype) |
191 | 509232 | temp += state->N[Q] >> 1; | |
192 | |||
193 |
2/2✓ Branch 0 taken 1884419 times.
✓ Branch 1 taken 783661 times.
|
2668080 | for (k = 0; ((unsigned)state->N[Q] << k) < temp; k++) |
194 | ; | ||
195 | |||
196 | #ifdef JLS_BROKEN | ||
197 | if (!show_bits_long(gb, 32)) | ||
198 | return -1; | ||
199 | #endif | ||
200 | 783661 | ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, | |
201 | state->qbpp); | ||
202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
|
783661 | if (ret < 0) |
203 | ✗ | return -0x10000; | |
204 | |||
205 | /* decode mapped error */ | ||
206 | 783661 | map = 0; | |
207 |
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])) |
208 | 60548 | map = 1; | |
209 | 783661 | ret += RItype + map; | |
210 | |||
211 |
2/2✓ Branch 0 taken 351896 times.
✓ Branch 1 taken 431765 times.
|
783661 | if (ret & 1) { |
212 | 351896 | ret = map - (ret + 1 >> 1); | |
213 | 351896 | state->B[Q]++; | |
214 | } else { | ||
215 | 431765 | ret = ret >> 1; | |
216 | } | ||
217 | |||
218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
|
783661 | if (FFABS(ret) > 0xFFFF) |
219 | ✗ | return -0x10000; | |
220 | /* update state */ | ||
221 | 783661 | state->A[Q] += FFABS(ret) - RItype; | |
222 | 783661 | ret *= state->twonear; | |
223 | 783661 | ff_jpegls_downscale_state(state, Q); | |
224 | |||
225 | 783661 | return ret; | |
226 | } | ||
227 | |||
228 | /** | ||
229 | * Decode one line of image | ||
230 | */ | ||
231 | 143946 | static inline int ls_decode_line(JLSState *state, MJpegDecodeContext *s, | |
232 | void *last, void *dst, int last2, int w, | ||
233 | int stride, int comp, int bits) | ||
234 | { | ||
235 | 143946 | int i, x = 0; | |
236 | int Ra, Rb, Rc, Rd; | ||
237 | int D0, D1, D2; | ||
238 | |||
239 |
2/2✓ Branch 0 taken 47299272 times.
✓ Branch 1 taken 135951 times.
|
47435223 | while (x < w) { |
240 | int err, pred; | ||
241 | |||
242 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 47299272 times.
|
47299272 | if (get_bits_left(&s->gb) <= 0) |
243 | ✗ | return AVERROR_INVALIDDATA; | |
244 | |||
245 | /* compute gradients */ | ||
246 |
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); |
247 |
1/2✓ Branch 0 taken 47299272 times.
✗ Branch 1 not taken.
|
47299272 | Rb = R(last, x); |
248 |
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; |
249 |
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); |
250 | 47299272 | D0 = Rd - Rb; | |
251 | 47299272 | D1 = Rb - Rc; | |
252 | 47299272 | D2 = Rc - Ra; | |
253 | /* run mode */ | ||
254 |
2/2✓ Branch 0 taken 5800192 times.
✓ Branch 1 taken 41499080 times.
|
47299272 | if ((FFABS(D0) <= state->near) && |
255 |
2/2✓ Branch 0 taken 2188180 times.
✓ Branch 1 taken 3612012 times.
|
5800192 | (FFABS(D1) <= state->near) && |
256 |
2/2✓ Branch 0 taken 791656 times.
✓ Branch 1 taken 1396524 times.
|
2971841 | (FFABS(D2) <= state->near)) { |
257 | int r; | ||
258 | int RItype; | ||
259 | |||
260 | /* decode full runs while available */ | ||
261 |
2/2✓ Branch 1 taken 737915 times.
✓ Branch 2 taken 783661 times.
|
1521576 | while (get_bits1(&s->gb)) { |
262 | int r; | ||
263 | 737915 | r = 1 << ff_log2_run[state->run_index[comp]]; | |
264 |
2/2✓ Branch 0 taken 6338 times.
✓ Branch 1 taken 731577 times.
|
737915 | if (x + r * stride > w) |
265 | 6338 | r = (w - x) / stride; | |
266 |
2/2✓ Branch 0 taken 2755018 times.
✓ Branch 1 taken 737915 times.
|
3492933 | for (i = 0; i < r; i++) { |
267 |
1/2✓ Branch 0 taken 2755018 times.
✗ Branch 1 not taken.
|
2755018 | W(dst, x, Ra); |
268 | 2755018 | x += stride; | |
269 | } | ||
270 | /* if EOL reached, we stop decoding */ | ||
271 |
2/2✓ Branch 0 taken 6338 times.
✓ Branch 1 taken 731577 times.
|
737915 | if (r != 1 << ff_log2_run[state->run_index[comp]]) |
272 | 6338 | return 0; | |
273 |
1/2✓ Branch 0 taken 731577 times.
✗ Branch 1 not taken.
|
731577 | if (state->run_index[comp] < 31) |
274 | 731577 | state->run_index[comp]++; | |
275 |
2/2✓ Branch 0 taken 1657 times.
✓ Branch 1 taken 729920 times.
|
731577 | if (x + stride > w) |
276 | 1657 | return 0; | |
277 | } | ||
278 | /* decode aborted run */ | ||
279 | 783661 | r = ff_log2_run[state->run_index[comp]]; | |
280 |
2/2✓ Branch 0 taken 647235 times.
✓ Branch 1 taken 136426 times.
|
783661 | if (r) |
281 | 647235 | r = get_bits(&s->gb, r); | |
282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
|
783661 | if (x + r * stride > w) { |
283 | ✗ | r = (w - x) / stride; | |
284 | } | ||
285 |
2/2✓ Branch 0 taken 1010453 times.
✓ Branch 1 taken 783661 times.
|
1794114 | for (i = 0; i < r; i++) { |
286 |
1/2✓ Branch 0 taken 1010453 times.
✗ Branch 1 not taken.
|
1010453 | W(dst, x, Ra); |
287 | 1010453 | x += stride; | |
288 | } | ||
289 | |||
290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
|
783661 | if (x >= w) { |
291 | ✗ | av_log(NULL, AV_LOG_ERROR, "run overflow\n"); | |
292 | ✗ | av_assert0(x <= w); | |
293 | ✗ | return AVERROR_INVALIDDATA; | |
294 | } | ||
295 | |||
296 | /* decode run termination value */ | ||
297 |
1/2✓ Branch 0 taken 783661 times.
✗ Branch 1 not taken.
|
783661 | Rb = R(last, x); |
298 | 783661 | RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0; | |
299 | 783661 | err = ls_get_code_runterm(&s->gb, state, RItype, | |
300 | 783661 | ff_log2_run[state->run_index[comp]]); | |
301 |
2/2✓ Branch 0 taken 729428 times.
✓ Branch 1 taken 54233 times.
|
783661 | if (state->run_index[comp]) |
302 | 729428 | state->run_index[comp]--; | |
303 | |||
304 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 783661 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
783661 | if (state->near && RItype) { |
305 | ✗ | pred = Ra + err; | |
306 | } else { | ||
307 |
2/2✓ Branch 0 taken 170007 times.
✓ Branch 1 taken 613654 times.
|
783661 | if (Rb < Ra) |
308 | 170007 | pred = Rb - err; | |
309 | else | ||
310 | 613654 | pred = Rb + err; | |
311 | } | ||
312 | } else { /* regular mode */ | ||
313 | int context, sign; | ||
314 | |||
315 | 46507616 | context = ff_jpegls_quantize(state, D0) * 81 + | |
316 | 46507616 | ff_jpegls_quantize(state, D1) * 9 + | |
317 | 46507616 | ff_jpegls_quantize(state, D2); | |
318 | 46507616 | pred = mid_pred(Ra, Ra + Rb - Rc, Rb); | |
319 | |||
320 |
2/2✓ Branch 0 taken 19034217 times.
✓ Branch 1 taken 27473399 times.
|
46507616 | if (context < 0) { |
321 | 19034217 | context = -context; | |
322 | 19034217 | sign = 1; | |
323 | } else { | ||
324 | 27473399 | sign = 0; | |
325 | } | ||
326 | |||
327 |
2/2✓ Branch 0 taken 19034217 times.
✓ Branch 1 taken 27473399 times.
|
46507616 | if (sign) { |
328 | 19034217 | pred = av_clip(pred - state->C[context], 0, state->maxval); | |
329 | 19034217 | err = -ls_get_code_regular(&s->gb, state, context); | |
330 | } else { | ||
331 | 27473399 | pred = av_clip(pred + state->C[context], 0, state->maxval); | |
332 | 27473399 | err = ls_get_code_regular(&s->gb, state, context); | |
333 | } | ||
334 | |||
335 | /* we have to do something more for near-lossless coding */ | ||
336 | 46507616 | pred += err; | |
337 | } | ||
338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47291277 times.
|
47291277 | if (state->near) { |
339 | ✗ | if (pred < -state->near) | |
340 | ✗ | pred += state->range * state->twonear; | |
341 | ✗ | else if (pred > state->maxval + state->near) | |
342 | ✗ | pred -= state->range * state->twonear; | |
343 | ✗ | pred = av_clip(pred, 0, state->maxval); | |
344 | } | ||
345 | |||
346 | 47291277 | pred &= state->maxval; | |
347 |
1/2✓ Branch 0 taken 47291277 times.
✗ Branch 1 not taken.
|
47291277 | W(dst, x, pred); |
348 | 47291277 | x += stride; | |
349 | } | ||
350 | |||
351 | 135951 | return 0; | |
352 | } | ||
353 | |||
354 | 213 | int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, | |
355 | int point_transform, int ilv) | ||
356 | { | ||
357 | 213 | int i, t = 0; | |
358 | uint8_t *zero, *last, *cur; | ||
359 | 213 | JLSState *state = s->jls_state; | |
360 | 213 | int off = 0, stride = 1, width, shift, ret = 0; | |
361 | 213 | int decoded_height = 0; | |
362 | |||
363 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 196 times.
|
213 | if (!state) { |
364 | 17 | state = av_malloc(sizeof(*state)); | |
365 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (!state) |
366 | ✗ | return AVERROR(ENOMEM); | |
367 | 17 | s->jls_state = state; | |
368 | } | ||
369 | 213 | zero = av_mallocz(s->picture_ptr->linesize[0]); | |
370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
|
213 | if (!zero) |
371 | ✗ | return AVERROR(ENOMEM); | |
372 | 213 | last = zero; | |
373 | 213 | cur = s->picture_ptr->data[0]; | |
374 | |||
375 | /* initialize JPEG-LS state from JPEG parameters */ | ||
376 | 213 | memset(state, 0, sizeof(*state)); | |
377 | 213 | state->near = near; | |
378 | 213 | state->bpp = (s->bits < 2) ? 2 : s->bits; | |
379 | 213 | state->maxval = s->maxval; | |
380 | 213 | state->T1 = s->t1; | |
381 | 213 | state->T2 = s->t2; | |
382 | 213 | state->T3 = s->t3; | |
383 | 213 | state->reset = s->reset; | |
384 | 213 | ff_jpegls_reset_coding_parameters(state, 0); | |
385 | 213 | ff_jpegls_init_state(state); | |
386 | |||
387 |
1/2✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
|
213 | if (s->bits <= 8) |
388 | 213 | shift = point_transform + (8 - s->bits); | |
389 | else | ||
390 | ✗ | shift = point_transform + (16 - s->bits); | |
391 | |||
392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
|
213 | if (shift >= 16) { |
393 | ✗ | ret = AVERROR_INVALIDDATA; | |
394 | ✗ | goto end; | |
395 | } | ||
396 | |||
397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
|
213 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) { |
398 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, | |
399 | "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) " | ||
400 | "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n", | ||
401 | s->width, s->height, state->near, state->maxval, | ||
402 | state->T1, state->T2, state->T3, | ||
403 | state->reset, state->limit, state->qbpp, state->range); | ||
404 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", | |
405 | ilv, point_transform, s->bits, s->cur_scan); | ||
406 | } | ||
407 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
|
213 | if (get_bits_left(&s->gb) < s->height) { |
408 | ✗ | ret = AVERROR_INVALIDDATA; | |
409 | ✗ | goto end; | |
410 | } | ||
411 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 205 times.
|
213 | if (ilv == 0) { /* separate planes */ |
412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->cur_scan > s->nb_components) { |
413 | ✗ | ret = AVERROR_INVALIDDATA; | |
414 | ✗ | goto end; | |
415 | } | ||
416 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | stride = (s->nb_components > 1) ? 3 : 1; |
417 | 8 | off = av_clip(s->cur_scan - 1, 0, stride - 1); | |
418 | 8 | width = s->width * stride; | |
419 | 8 | cur += off; | |
420 |
2/2✓ Branch 0 taken 5688 times.
✓ Branch 1 taken 8 times.
|
5696 | for (i = 0; i < s->height; i++) { |
421 | int ret; | ||
422 |
1/2✓ Branch 0 taken 5688 times.
✗ Branch 1 not taken.
|
5688 | if (s->bits <= 8) { |
423 | 5688 | ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8); | |
424 | 5688 | t = last[0]; | |
425 | } else { | ||
426 | ✗ | ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16); | |
427 | ✗ | t = *((uint16_t *)last); | |
428 | } | ||
429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5688 times.
|
5688 | if (ret < 0) |
430 | ✗ | break; | |
431 | 5688 | last = cur; | |
432 | 5688 | cur += s->picture_ptr->linesize[0]; | |
433 | |||
434 |
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) { |
435 | ✗ | align_get_bits(&s->gb); | |
436 | ✗ | skip_bits(&s->gb, 16); /* skip RSTn */ | |
437 | } | ||
438 | } | ||
439 | 8 | decoded_height = i; | |
440 |
1/2✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
|
205 | } else if (ilv == 1) { /* line interleaving */ |
441 | int j; | ||
442 | 205 | int Rc[3] = { 0, 0, 0 }; | |
443 |
1/2✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
|
205 | stride = (s->nb_components > 1) ? 3 : 1; |
444 | 205 | memset(cur, 0, s->picture_ptr->linesize[0]); | |
445 | 205 | width = s->width * stride; | |
446 |
2/2✓ Branch 0 taken 46086 times.
✓ Branch 1 taken 205 times.
|
46291 | for (i = 0; i < s->height; i++) { |
447 | int ret; | ||
448 |
2/2✓ Branch 0 taken 138258 times.
✓ Branch 1 taken 46086 times.
|
184344 | for (j = 0; j < stride; j++) { |
449 | 138258 | ret = ls_decode_line(state, s, last + j, cur + j, | |
450 | Rc[j], width, stride, j, 8); | ||
451 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 138258 times.
|
138258 | if (ret < 0) |
452 | ✗ | break; | |
453 | 138258 | Rc[j] = last[j]; | |
454 | |||
455 |
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) { |
456 | ✗ | align_get_bits(&s->gb); | |
457 | ✗ | skip_bits(&s->gb, 16); /* skip RSTn */ | |
458 | } | ||
459 | } | ||
460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46086 times.
|
46086 | if (ret < 0) |
461 | ✗ | break; | |
462 | 46086 | last = cur; | |
463 | 46086 | cur += s->picture_ptr->linesize[0]; | |
464 | } | ||
465 | 205 | decoded_height = i; | |
466 | ✗ | } else if (ilv == 2) { /* sample interleaving */ | |
467 | ✗ | avpriv_report_missing_feature(s->avctx, "Sample interleaved images"); | |
468 | ✗ | ret = AVERROR_PATCHWELCOME; | |
469 | ✗ | goto end; | |
470 | } else { /* unknown interleaving */ | ||
471 | ✗ | avpriv_report_missing_feature(s->avctx, "Unknown interleaved images"); | |
472 | ✗ | ret = AVERROR_PATCHWELCOME; | |
473 | ✗ | goto end; | |
474 | } | ||
475 | |||
476 |
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) { |
477 | int x, w; | ||
478 | |||
479 | ✗ | w = s->width * s->nb_components; | |
480 | |||
481 | ✗ | if (s->bits <= 8) { | |
482 | ✗ | uint8_t *src = s->picture_ptr->data[0]; | |
483 | |||
484 | ✗ | for (i = 0; i < s->height; i++) { | |
485 | ✗ | switch(s->xfrm) { | |
486 | ✗ | case 1: | |
487 | ✗ | for (x = off; x + 2 < w; x += 3) { | |
488 | ✗ | src[x ] += src[x+1] + 128; | |
489 | ✗ | src[x+2] += src[x+1] + 128; | |
490 | } | ||
491 | ✗ | break; | |
492 | ✗ | case 2: | |
493 | ✗ | for (x = off; x + 2 < w; x += 3) { | |
494 | ✗ | src[x ] += src[x+1] + 128; | |
495 | ✗ | src[x+2] += ((src[x ] + src[x+1])>>1) + 128; | |
496 | } | ||
497 | ✗ | break; | |
498 | ✗ | case 3: | |
499 | ✗ | for (x = off; x + 2 < w; x += 3) { | |
500 | ✗ | int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64; | |
501 | ✗ | src[x+0] = src[x+2] + g + 128; | |
502 | ✗ | src[x+2] = src[x+1] + g + 128; | |
503 | ✗ | src[x+1] = g; | |
504 | } | ||
505 | ✗ | break; | |
506 | ✗ | case 4: | |
507 | ✗ | for (x = off; x + 2 < w; x += 3) { | |
508 | ✗ | int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8); | |
509 | ✗ | int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8); | |
510 | ✗ | int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8); | |
511 | ✗ | src[x+0] = av_clip_uint8(r); | |
512 | ✗ | src[x+1] = av_clip_uint8(g); | |
513 | ✗ | src[x+2] = av_clip_uint8(b); | |
514 | } | ||
515 | ✗ | break; | |
516 | } | ||
517 | ✗ | src += s->picture_ptr->linesize[0]; | |
518 | } | ||
519 | }else | ||
520 | ✗ | avpriv_report_missing_feature(s->avctx, "16bit xfrm"); | |
521 | } | ||
522 | |||
523 |
2/2✓ Branch 0 taken 205 times.
✓ Branch 1 taken 8 times.
|
213 | if (shift) { /* we need to do point transform or normalize samples */ |
524 | int x, w; | ||
525 | |||
526 | 8 | w = s->width * s->nb_components; | |
527 | |||
528 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (s->bits <= 8) { |
529 | 8 | uint8_t *src = s->picture_ptr->data[0]; | |
530 | |||
531 |
2/2✓ Branch 0 taken 5688 times.
✓ Branch 1 taken 8 times.
|
5696 | for (i = 0; i < decoded_height; i++) { |
532 |
2/2✓ Branch 0 taken 4044168 times.
✓ Branch 1 taken 5688 times.
|
4049856 | for (x = off; x < w; x += stride) |
533 | 4044168 | src[x] <<= shift; | |
534 | 5688 | src += s->picture_ptr->linesize[0]; | |
535 | } | ||
536 | } else { | ||
537 | ✗ | uint16_t *src = (uint16_t *)s->picture_ptr->data[0]; | |
538 | |||
539 | ✗ | for (i = 0; i < decoded_height; i++) { | |
540 | ✗ | for (x = 0; x < w; x++) | |
541 | ✗ | src[x] <<= shift; | |
542 | ✗ | src += s->picture_ptr->linesize[0] / 2; | |
543 | } | ||
544 | } | ||
545 | } | ||
546 | |||
547 | 205 | end: | |
548 | 213 | av_free(zero); | |
549 | |||
550 | 213 | return ret; | |
551 | } | ||
552 | |||
553 | const FFCodec ff_jpegls_decoder = { | ||
554 | .p.name = "jpegls", | ||
555 | CODEC_LONG_NAME("JPEG-LS"), | ||
556 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
557 | .p.id = AV_CODEC_ID_JPEGLS, | ||
558 | .priv_data_size = sizeof(MJpegDecodeContext), | ||
559 | .init = ff_mjpeg_decode_init, | ||
560 | .close = ff_mjpeg_decode_end, | ||
561 | FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame), | ||
562 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
563 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
564 | }; | ||
565 |