Commit | Line | Data |
---|---|---|
c81a7063 NB |
1 | /* |
2 | * JPEG 2000 image decoder | |
3 | * Copyright (c) 2007 Kamil Nowosad | |
4 | * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com> | |
5 | * | |
6 | * This file is part of Libav. | |
7 | * | |
8 | * Libav 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 | * Libav 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 Libav; 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 2000 image decoder | |
26 | */ | |
27 | ||
28 | #include "libavutil/common.h" | |
29 | #include "libavutil/opt.h" | |
30 | #include "avcodec.h" | |
31 | #include "bytestream.h" | |
32 | #include "internal.h" | |
2e2d2466 | 33 | #include "thread.h" |
c81a7063 NB |
34 | #include "jpeg2000.h" |
35 | ||
36 | #define JP2_SIG_TYPE 0x6A502020 | |
37 | #define JP2_SIG_VALUE 0x0D0A870A | |
38 | #define JP2_CODESTREAM 0x6A703263 | |
39 | ||
40 | #define HAD_COC 0x01 | |
41 | #define HAD_QCC 0x02 | |
42 | ||
43 | typedef struct Jpeg2000TilePart { | |
44 | uint16_t tp_idx; // Tile-part index | |
45 | uint8_t tile_index; // Tile index who refers the tile-part | |
46 | uint32_t tp_len; // Length of tile-part | |
1a3598aa | 47 | GetByteContext tpg; // bit stream in tile-part |
c81a7063 NB |
48 | } Jpeg2000TilePart; |
49 | ||
50 | /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile | |
51 | * one per component, so tile_part elements have a size of 3 */ | |
52 | typedef struct Jpeg2000Tile { | |
53 | Jpeg2000Component *comp; | |
54 | uint8_t properties[4]; | |
55 | Jpeg2000CodingStyle codsty[4]; | |
56 | Jpeg2000QuantStyle qntsty[4]; | |
57 | Jpeg2000TilePart tile_part[3]; | |
58 | } Jpeg2000Tile; | |
59 | ||
60 | typedef struct Jpeg2000DecoderContext { | |
61 | AVClass *class; | |
62 | AVCodecContext *avctx; | |
1a3598aa | 63 | GetByteContext g; |
c81a7063 NB |
64 | |
65 | int width, height; | |
66 | int image_offset_x, image_offset_y; | |
67 | int tile_offset_x, tile_offset_y; | |
68 | uint8_t cbps[4]; // bits per sample in particular components | |
69 | uint8_t sgnd[4]; // if a component is signed | |
70 | uint8_t properties[4]; | |
71 | int cdx[4], cdy[4]; | |
72 | int precision; | |
73 | int ncomponents; | |
74 | int tile_width, tile_height; | |
75 | int numXtiles, numYtiles; | |
76 | int maxtilelen; | |
77 | ||
78 | Jpeg2000CodingStyle codsty[4]; | |
79 | Jpeg2000QuantStyle qntsty[4]; | |
80 | ||
c81a7063 NB |
81 | int bit_index; |
82 | ||
83 | int16_t curtileno; | |
84 | Jpeg2000Tile *tile; | |
85 | ||
86 | /*options parameters*/ | |
87 | int16_t lowres; | |
88 | int16_t reduction_factor; | |
89 | } Jpeg2000DecoderContext; | |
90 | ||
91 | /* get_bits functions for JPEG2000 packet bitstream | |
92 | * It is a get_bit function with a bit-stuffing routine. If the value of the | |
93 | * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB. | |
94 | * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */ | |
95 | static int get_bits(Jpeg2000DecoderContext *s, int n) | |
96 | { | |
97 | int res = 0; | |
c81a7063 NB |
98 | while (--n >= 0) { |
99 | res <<= 1; | |
100 | if (s->bit_index == 0) { | |
1a3598aa | 101 | s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu); |
c81a7063 NB |
102 | } |
103 | s->bit_index--; | |
1a3598aa | 104 | res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1; |
c81a7063 NB |
105 | } |
106 | return res; | |
107 | } | |
108 | ||
109 | static void jpeg2000_flush(Jpeg2000DecoderContext *s) | |
110 | { | |
1a3598aa MN |
111 | if (bytestream2_get_byte(&s->g) == 0xff) |
112 | bytestream2_skip(&s->g, 1); | |
c81a7063 | 113 | s->bit_index = 8; |
c81a7063 NB |
114 | } |
115 | ||
116 | /* decode the value stored in node */ | |
117 | static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, | |
118 | int threshold) | |
119 | { | |
120 | Jpeg2000TgtNode *stack[30]; | |
121 | int sp = -1, curval = 0; | |
122 | ||
123 | while (node && !node->vis) { | |
124 | stack[++sp] = node; | |
125 | node = node->parent; | |
126 | } | |
127 | ||
128 | if (node) | |
129 | curval = node->val; | |
130 | else | |
131 | curval = stack[sp]->val; | |
132 | ||
133 | while (curval < threshold && sp >= 0) { | |
134 | if (curval < stack[sp]->val) | |
135 | curval = stack[sp]->val; | |
136 | while (curval < threshold) { | |
137 | int ret; | |
138 | if ((ret = get_bits(s, 1)) > 0) { | |
139 | stack[sp]->vis++; | |
140 | break; | |
141 | } else if (!ret) | |
142 | curval++; | |
143 | else | |
144 | return ret; | |
145 | } | |
146 | stack[sp]->val = curval; | |
147 | sp--; | |
148 | } | |
149 | return curval; | |
150 | } | |
151 | ||
152 | /* marker segments */ | |
153 | /* get sizes and offsets of image, tiles; number of components */ | |
154 | static int get_siz(Jpeg2000DecoderContext *s) | |
155 | { | |
156 | int i; | |
157 | ||
1a3598aa | 158 | if (bytestream2_get_bytes_left(&s->g) < 36) |
5efadcb8 | 159 | return AVERROR_INVALIDDATA; |
c81a7063 | 160 | |
1a3598aa MN |
161 | s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz |
162 | s->width = bytestream2_get_be32u(&s->g); // Width | |
163 | s->height = bytestream2_get_be32u(&s->g); // Height | |
164 | s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz | |
165 | s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz | |
166 | s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz | |
167 | s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz | |
168 | s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz | |
169 | s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz | |
170 | s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz | |
171 | ||
172 | if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) | |
5efadcb8 | 173 | return AVERROR_INVALIDDATA; |
c81a7063 NB |
174 | |
175 | for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i | |
1a3598aa | 176 | uint8_t x = bytestream2_get_byteu(&s->g); |
c81a7063 NB |
177 | s->cbps[i] = (x & 0x7f) + 1; |
178 | s->precision = FFMAX(s->cbps[i], s->precision); | |
179 | s->sgnd[i] = (x & 0x80) == 1; | |
1a3598aa MN |
180 | s->cdx[i] = bytestream2_get_byteu(&s->g); |
181 | s->cdy[i] = bytestream2_get_byteu(&s->g); | |
c81a7063 NB |
182 | } |
183 | ||
184 | s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width); | |
185 | s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height); | |
186 | ||
187 | s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile)); | |
188 | if (!s->tile) | |
189 | return AVERROR(ENOMEM); | |
190 | ||
191 | for (i = 0; i < s->numXtiles * s->numYtiles; i++) { | |
192 | Jpeg2000Tile *tile = s->tile + i; | |
193 | ||
194 | tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp)); | |
195 | if (!tile->comp) | |
196 | return AVERROR(ENOMEM); | |
197 | } | |
198 | ||
199 | /* compute image size with reduction factor */ | |
200 | s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x, | |
201 | s->reduction_factor); | |
202 | s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y, | |
203 | s->reduction_factor); | |
204 | ||
205 | switch (s->avctx->profile) { | |
206 | case FF_PROFILE_JPEG2000_DCINEMA_2K: | |
207 | case FF_PROFILE_JPEG2000_DCINEMA_4K: | |
208 | /* XYZ color-space for digital cinema profiles */ | |
10f1a4d9 | 209 | s->avctx->pix_fmt = AV_PIX_FMT_XYZ12; |
c81a7063 NB |
210 | break; |
211 | default: | |
212 | /* For other profiles selects color-space according number of | |
213 | * components and bit depth precision. */ | |
214 | switch (s->ncomponents) { | |
215 | case 1: | |
216 | if (s->precision > 8) | |
217 | s->avctx->pix_fmt = AV_PIX_FMT_GRAY16; | |
218 | else | |
219 | s->avctx->pix_fmt = AV_PIX_FMT_GRAY8; | |
220 | break; | |
221 | case 3: | |
222 | if (s->precision > 8) | |
223 | s->avctx->pix_fmt = AV_PIX_FMT_RGB48; | |
224 | else | |
225 | s->avctx->pix_fmt = AV_PIX_FMT_RGB24; | |
226 | break; | |
227 | case 4: | |
228 | s->avctx->pix_fmt = AV_PIX_FMT_BGRA; | |
229 | break; | |
230 | default: | |
231 | /* pixel format can not be identified */ | |
232 | s->avctx->pix_fmt = AV_PIX_FMT_NONE; | |
233 | break; | |
234 | } | |
235 | break; | |
236 | } | |
237 | return 0; | |
238 | } | |
239 | ||
240 | /* get common part for COD and COC segments */ | |
241 | static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c) | |
242 | { | |
243 | uint8_t byte; | |
244 | ||
1a3598aa MN |
245 | if (bytestream2_get_bytes_left(&s->g) < 5) |
246 | return AVERROR_INVALIDDATA; | |
247 | ||
5650e331 LB |
248 | /* nreslevels = number of resolution levels |
249 | = number of decomposition level +1 */ | |
1a3598aa | 250 | c->nreslevels = bytestream2_get_byteu(&s->g) + 1; |
5650e331 LB |
251 | |
252 | if (c->nreslevels > JPEG2000_MAX_RESLEVELS) | |
253 | return AVERROR_INVALIDDATA; | |
c81a7063 NB |
254 | |
255 | /* compute number of resolution levels to decode */ | |
256 | if (c->nreslevels < s->reduction_factor) | |
257 | c->nreslevels2decode = 1; | |
258 | else | |
259 | c->nreslevels2decode = c->nreslevels - s->reduction_factor; | |
260 | ||
1a3598aa MN |
261 | c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width |
262 | c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height | |
c81a7063 | 263 | |
fbcc03db MN |
264 | if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 || |
265 | c->log2_cblk_width + c->log2_cblk_height > 12) { | |
266 | av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n"); | |
267 | return AVERROR_INVALIDDATA; | |
268 | } | |
269 | ||
1a3598aa | 270 | c->cblk_style = bytestream2_get_byteu(&s->g); |
c81a7063 | 271 | if (c->cblk_style != 0) { // cblk style |
5efadcb8 LB |
272 | avpriv_request_sample(s->avctx, "Support for extra cblk styles"); |
273 | return AVERROR_PATCHWELCOME; | |
c81a7063 | 274 | } |
1a3598aa | 275 | c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type |
c81a7063 NB |
276 | /* set integer 9/7 DWT in case of BITEXACT flag */ |
277 | if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97)) | |
278 | c->transform = FF_DWT97_INT; | |
279 | ||
280 | if (c->csty & JPEG2000_CSTY_PREC) { | |
281 | int i; | |
282 | for (i = 0; i < c->nreslevels; i++) { | |
1a3598aa | 283 | byte = bytestream2_get_byte(&s->g); |
c81a7063 NB |
284 | c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx |
285 | c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy | |
286 | } | |
287 | } | |
288 | return 0; | |
289 | } | |
290 | ||
291 | /* get coding parameters for a particular tile or whole image*/ | |
292 | static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, | |
293 | uint8_t *properties) | |
294 | { | |
295 | Jpeg2000CodingStyle tmp; | |
296 | int compno; | |
297 | ||
1a3598aa | 298 | if (bytestream2_get_bytes_left(&s->g) < 5) |
5efadcb8 | 299 | return AVERROR_INVALIDDATA; |
c81a7063 NB |
300 | |
301 | tmp.log2_prec_width = | |
302 | tmp.log2_prec_height = 15; | |
303 | ||
1a3598aa | 304 | tmp.csty = bytestream2_get_byteu(&s->g); |
c81a7063 NB |
305 | |
306 | // get progression order | |
1a3598aa | 307 | tmp.prog_order = bytestream2_get_byteu(&s->g); |
c81a7063 | 308 | |
1a3598aa MN |
309 | tmp.nlayers = bytestream2_get_be16u(&s->g); |
310 | tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation | |
c81a7063 NB |
311 | |
312 | get_cox(s, &tmp); | |
313 | for (compno = 0; compno < s->ncomponents; compno++) | |
314 | if (!(properties[compno] & HAD_COC)) | |
315 | memcpy(c + compno, &tmp, sizeof(tmp)); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | /* Get coding parameters for a component in the whole image or a | |
320 | * particular tile. */ | |
321 | static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, | |
322 | uint8_t *properties) | |
323 | { | |
324 | int compno; | |
325 | ||
1a3598aa | 326 | if (bytestream2_get_bytes_left(&s->g) < 2) |
5efadcb8 | 327 | return AVERROR_INVALIDDATA; |
c81a7063 | 328 | |
1a3598aa | 329 | compno = bytestream2_get_byteu(&s->g); |
c81a7063 NB |
330 | |
331 | c += compno; | |
1a3598aa | 332 | c->csty = bytestream2_get_byteu(&s->g); |
c81a7063 NB |
333 | get_cox(s, c); |
334 | ||
335 | properties[compno] |= HAD_COC; | |
336 | return 0; | |
337 | } | |
338 | ||
339 | /* Get common part for QCD and QCC segments. */ | |
340 | static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q) | |
341 | { | |
342 | int i, x; | |
343 | ||
1a3598aa | 344 | if (bytestream2_get_bytes_left(&s->g) < 1) |
5efadcb8 | 345 | return AVERROR_INVALIDDATA; |
c81a7063 | 346 | |
1a3598aa | 347 | x = bytestream2_get_byteu(&s->g); // Sqcd |
c81a7063 NB |
348 | |
349 | q->nguardbits = x >> 5; | |
350 | q->quantsty = x & 0x1f; | |
351 | ||
352 | if (q->quantsty == JPEG2000_QSTY_NONE) { | |
353 | n -= 3; | |
1a3598aa MN |
354 | if (bytestream2_get_bytes_left(&s->g) < n || |
355 | n > JPEG2000_MAX_DECLEVELS) | |
5efadcb8 | 356 | return AVERROR_INVALIDDATA; |
c81a7063 | 357 | for (i = 0; i < n; i++) |
1a3598aa | 358 | q->expn[i] = bytestream2_get_byteu(&s->g) >> 3; |
c81a7063 | 359 | } else if (q->quantsty == JPEG2000_QSTY_SI) { |
1a3598aa | 360 | if (bytestream2_get_bytes_left(&s->g) < 2) |
5efadcb8 | 361 | return AVERROR_INVALIDDATA; |
1a3598aa | 362 | x = bytestream2_get_be16u(&s->g); |
c81a7063 NB |
363 | q->expn[0] = x >> 11; |
364 | q->mant[0] = x & 0x7ff; | |
be327100 | 365 | for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) { |
c81a7063 NB |
366 | int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3); |
367 | q->expn[i] = curexpn; | |
368 | q->mant[i] = q->mant[0]; | |
369 | } | |
370 | } else { | |
371 | n = (n - 3) >> 1; | |
1a3598aa MN |
372 | if (bytestream2_get_bytes_left(&s->g) < 2 * n || |
373 | n > JPEG2000_MAX_DECLEVELS) | |
5efadcb8 | 374 | return AVERROR_INVALIDDATA; |
c81a7063 | 375 | for (i = 0; i < n; i++) { |
1a3598aa | 376 | x = bytestream2_get_be16u(&s->g); |
c81a7063 NB |
377 | q->expn[i] = x >> 11; |
378 | q->mant[i] = x & 0x7ff; | |
379 | } | |
380 | } | |
381 | return 0; | |
382 | } | |
383 | ||
384 | /* Get quantization parameters for a particular tile or a whole image. */ | |
385 | static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, | |
386 | uint8_t *properties) | |
387 | { | |
388 | Jpeg2000QuantStyle tmp; | |
5efadcb8 | 389 | int compno, ret; |
c81a7063 | 390 | |
5efadcb8 LB |
391 | if ((ret = get_qcx(s, n, &tmp)) < 0) |
392 | return ret; | |
c81a7063 NB |
393 | for (compno = 0; compno < s->ncomponents; compno++) |
394 | if (!(properties[compno] & HAD_QCC)) | |
395 | memcpy(q + compno, &tmp, sizeof(tmp)); | |
396 | return 0; | |
397 | } | |
398 | ||
399 | /* Get quantization parameters for a component in the whole image | |
400 | * on in a particular tile. */ | |
401 | static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, | |
402 | uint8_t *properties) | |
403 | { | |
404 | int compno; | |
405 | ||
1a3598aa | 406 | if (bytestream2_get_bytes_left(&s->g) < 1) |
5efadcb8 | 407 | return AVERROR_INVALIDDATA; |
c81a7063 | 408 | |
1a3598aa | 409 | compno = bytestream2_get_byteu(&s->g); |
c81a7063 NB |
410 | properties[compno] |= HAD_QCC; |
411 | return get_qcx(s, n - 1, q + compno); | |
412 | } | |
413 | ||
414 | /* Get start of tile segment. */ | |
5efadcb8 | 415 | static int get_sot(Jpeg2000DecoderContext *s, int n) |
c81a7063 NB |
416 | { |
417 | Jpeg2000TilePart *tp; | |
418 | uint16_t Isot; | |
419 | uint32_t Psot; | |
420 | uint8_t TPsot; | |
421 | ||
1a3598aa | 422 | if (bytestream2_get_bytes_left(&s->g) < 8) |
5efadcb8 | 423 | return AVERROR_INVALIDDATA; |
c81a7063 | 424 | |
1a3598aa | 425 | Isot = bytestream2_get_be16u(&s->g); // Isot |
c81a7063 | 426 | if (Isot) { |
5efadcb8 LB |
427 | avpriv_request_sample(s->avctx, "Support for more than one tile"); |
428 | return AVERROR_PATCHWELCOME; | |
c81a7063 | 429 | } |
1a3598aa MN |
430 | Psot = bytestream2_get_be32u(&s->g); // Psot |
431 | TPsot = bytestream2_get_byteu(&s->g); // TPsot | |
c81a7063 NB |
432 | |
433 | /* Read TNSot but not used */ | |
1a3598aa | 434 | bytestream2_get_byteu(&s->g); // TNsot |
c81a7063 NB |
435 | |
436 | tp = s->tile[s->curtileno].tile_part + TPsot; | |
437 | tp->tile_index = Isot; | |
438 | tp->tp_len = Psot; | |
439 | tp->tp_idx = TPsot; | |
440 | ||
441 | /* Start of bit stream. Pointer to SOD marker | |
442 | * Check SOD marker is present. */ | |
1a3598aa MN |
443 | if (JPEG2000_SOD == bytestream2_get_be16(&s->g)) { |
444 | bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_len - n - 4); | |
445 | bytestream2_skip(&s->g, tp->tp_len - n - 4); | |
446 | } else { | |
c81a7063 | 447 | av_log(s->avctx, AV_LOG_ERROR, "SOD marker not found \n"); |
5efadcb8 | 448 | return AVERROR_INVALIDDATA; |
c81a7063 NB |
449 | } |
450 | ||
451 | /* End address of bit stream = | |
452 | * start address + (Psot - size of SOT HEADER(n) | |
453 | * - size of SOT MARKER(2) - size of SOD marker(2) */ | |
c81a7063 NB |
454 | |
455 | return 0; | |
456 | } | |
457 | ||
458 | /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1 | |
459 | * Used to know the number of tile parts and lengths. | |
460 | * There may be multiple TLMs in the header. | |
461 | * TODO: The function is not used for tile-parts management, nor anywhere else. | |
462 | * It can be useful to allocate memory for tile parts, before managing the SOT | |
463 | * markers. Parsing the TLM header is needed to increment the input header | |
464 | * buffer. | |
465 | * This marker is mandatory for DCI. */ | |
466 | static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n) | |
467 | { | |
468 | uint8_t Stlm, ST, SP, tile_tlm, i; | |
1a3598aa MN |
469 | bytestream2_get_byte(&s->g); /* Ztlm: skipped */ |
470 | Stlm = bytestream2_get_byte(&s->g); | |
c81a7063 NB |
471 | |
472 | // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); | |
473 | ST = (Stlm >> 4) & 0x03; | |
474 | // TODO: Manage case of ST = 0b11 --> raise error | |
475 | SP = (Stlm >> 6) & 0x01; | |
476 | tile_tlm = (n - 4) / ((SP + 1) * 2 + ST); | |
477 | for (i = 0; i < tile_tlm; i++) { | |
478 | switch (ST) { | |
479 | case 0: | |
480 | break; | |
481 | case 1: | |
1a3598aa | 482 | bytestream2_get_byte(&s->g); |
c81a7063 NB |
483 | break; |
484 | case 2: | |
1a3598aa | 485 | bytestream2_get_be16(&s->g); |
c81a7063 NB |
486 | break; |
487 | case 3: | |
1a3598aa | 488 | bytestream2_get_be32(&s->g); |
c81a7063 NB |
489 | break; |
490 | } | |
491 | if (SP == 0) { | |
1a3598aa | 492 | bytestream2_get_be16(&s->g); |
c81a7063 | 493 | } else { |
1a3598aa | 494 | bytestream2_get_be32(&s->g); |
c81a7063 NB |
495 | } |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
500 | static int init_tile(Jpeg2000DecoderContext *s, int tileno) | |
501 | { | |
502 | int compno; | |
503 | int tilex = tileno % s->numXtiles; | |
504 | int tiley = tileno / s->numXtiles; | |
505 | Jpeg2000Tile *tile = s->tile + tileno; | |
506 | Jpeg2000CodingStyle *codsty; | |
507 | Jpeg2000QuantStyle *qntsty; | |
508 | ||
509 | if (!tile->comp) | |
510 | return AVERROR(ENOMEM); | |
511 | ||
512 | /* copy codsty, qnsty to tile. TODO: Is it the best way? | |
513 | * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle | |
514 | * and Jpeg2000QuantStyle */ | |
515 | memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(*codsty)); | |
516 | memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(*qntsty)); | |
517 | ||
518 | for (compno = 0; compno < s->ncomponents; compno++) { | |
519 | Jpeg2000Component *comp = tile->comp + compno; | |
520 | int ret; // global bandno | |
521 | codsty = tile->codsty + compno; | |
522 | qntsty = tile->qntsty + compno; | |
523 | ||
524 | comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x); | |
525 | comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width); | |
526 | comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y); | |
527 | comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height); | |
528 | ||
529 | // FIXME: add a dcinema profile check ? | |
530 | // value is guaranteed by profile (orig=0, 1 tile) | |
531 | comp->coord[0][0] = 0; | |
532 | comp->coord[0][1] = s->avctx->width; | |
533 | comp->coord[1][0] = 0; | |
534 | comp->coord[1][1] = s->avctx->height; | |
535 | ||
536 | if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty, | |
537 | s->cbps[compno], s->cdx[compno], | |
538 | s->cdy[compno], s->avctx)) | |
539 | return ret; | |
540 | } | |
541 | return 0; | |
542 | } | |
543 | ||
544 | /* Read the number of coding passes. */ | |
545 | static int getnpasses(Jpeg2000DecoderContext *s) | |
546 | { | |
547 | int num; | |
548 | if (!get_bits(s, 1)) | |
549 | return 1; | |
550 | if (!get_bits(s, 1)) | |
551 | return 2; | |
552 | if ((num = get_bits(s, 2)) != 3) | |
553 | return num < 0 ? num : 3 + num; | |
554 | if ((num = get_bits(s, 5)) != 31) | |
555 | return num < 0 ? num : 6 + num; | |
556 | num = get_bits(s, 7); | |
557 | return num < 0 ? num : 37 + num; | |
558 | } | |
559 | ||
560 | static int getlblockinc(Jpeg2000DecoderContext *s) | |
561 | { | |
562 | int res = 0, ret; | |
563 | while (ret = get_bits(s, 1)) { | |
564 | if (ret < 0) | |
565 | return ret; | |
566 | res++; | |
567 | } | |
568 | return res; | |
569 | } | |
570 | ||
571 | static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, | |
572 | Jpeg2000CodingStyle *codsty, | |
573 | Jpeg2000ResLevel *rlevel, int precno, | |
574 | int layno, uint8_t *expn, int numgbits) | |
575 | { | |
576 | int bandno, cblkno, ret, nb_code_blocks; | |
577 | ||
578 | if (!(ret = get_bits(s, 1))) { | |
579 | jpeg2000_flush(s); | |
580 | return 0; | |
581 | } else if (ret < 0) | |
582 | return ret; | |
583 | ||
584 | for (bandno = 0; bandno < rlevel->nbands; bandno++) { | |
585 | Jpeg2000Band *band = rlevel->band + bandno; | |
586 | Jpeg2000Prec *prec = band->prec + precno; | |
587 | ||
588 | if (band->coord[0][0] == band->coord[0][1] || | |
589 | band->coord[1][0] == band->coord[1][1]) | |
590 | continue; | |
591 | prec->yi0 = 0; | |
592 | prec->xi0 = 0; | |
593 | nb_code_blocks = prec->nb_codeblocks_height * | |
594 | prec->nb_codeblocks_width; | |
595 | for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { | |
596 | Jpeg2000Cblk *cblk = prec->cblk + cblkno; | |
597 | int incl, newpasses, llen; | |
598 | ||
599 | if (cblk->npasses) | |
600 | incl = get_bits(s, 1); | |
601 | else | |
602 | incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno; | |
603 | if (!incl) | |
604 | continue; | |
605 | else if (incl < 0) | |
606 | return incl; | |
607 | ||
608 | if (!cblk->npasses) | |
609 | cblk->nonzerobits = expn[bandno] + numgbits - 1 - | |
610 | tag_tree_decode(s, prec->zerobits + cblkno, | |
611 | 100); | |
612 | if ((newpasses = getnpasses(s)) < 0) | |
613 | return newpasses; | |
614 | if ((llen = getlblockinc(s)) < 0) | |
615 | return llen; | |
616 | cblk->lblock += llen; | |
617 | if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0) | |
618 | return ret; | |
619 | cblk->lengthinc = ret; | |
620 | cblk->npasses += newpasses; | |
621 | } | |
622 | } | |
623 | jpeg2000_flush(s); | |
624 | ||
625 | if (codsty->csty & JPEG2000_CSTY_EPH) { | |
1a3598aa MN |
626 | if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH) |
627 | bytestream2_skip(&s->g, 2); | |
c81a7063 NB |
628 | else |
629 | av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n"); | |
630 | } | |
631 | ||
632 | for (bandno = 0; bandno < rlevel->nbands; bandno++) { | |
633 | Jpeg2000Band *band = rlevel->band + bandno; | |
634 | Jpeg2000Prec *prec = band->prec + precno; | |
635 | ||
636 | nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width; | |
637 | for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { | |
638 | Jpeg2000Cblk *cblk = prec->cblk + cblkno; | |
1a3598aa | 639 | if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc) |
5efadcb8 | 640 | return AVERROR_INVALIDDATA; |
28816f9d NB |
641 | /* Code-block data can be empty. In that case initialize data |
642 | * with 0xFFFF. */ | |
643 | if (cblk->lengthinc > 0) { | |
1a3598aa | 644 | bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc); |
28816f9d NB |
645 | } else { |
646 | cblk->data[0] = 0xFF; | |
647 | cblk->data[1] = 0xFF; | |
648 | } | |
c81a7063 NB |
649 | cblk->length += cblk->lengthinc; |
650 | cblk->lengthinc = 0; | |
651 | } | |
652 | } | |
653 | return 0; | |
654 | } | |
655 | ||
656 | static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile) | |
657 | { | |
5efadcb8 | 658 | int layno, reslevelno, compno, precno, ok_reslevel, ret; |
c81a7063 NB |
659 | uint8_t prog_order = tile->codsty[0].prog_order; |
660 | uint16_t x; | |
661 | uint16_t y; | |
662 | ||
663 | s->bit_index = 8; | |
664 | switch (prog_order) { | |
665 | case JPEG2000_PGOD_LRCP: | |
666 | for (layno = 0; layno < tile->codsty[0].nlayers; layno++) { | |
667 | ok_reslevel = 1; | |
668 | for (reslevelno = 0; ok_reslevel; reslevelno++) { | |
669 | ok_reslevel = 0; | |
670 | for (compno = 0; compno < s->ncomponents; compno++) { | |
671 | Jpeg2000CodingStyle *codsty = tile->codsty + compno; | |
672 | Jpeg2000QuantStyle *qntsty = tile->qntsty + compno; | |
673 | if (reslevelno < codsty->nreslevels) { | |
674 | Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + | |
675 | reslevelno; | |
676 | ok_reslevel = 1; | |
677 | for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) | |
5efadcb8 LB |
678 | if ((ret = jpeg2000_decode_packet(s, |
679 | codsty, rlevel, | |
680 | precno, layno, | |
681 | qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), | |
682 | qntsty->nguardbits)) < 0) | |
683 | return ret; | |
c81a7063 NB |
684 | } |
685 | } | |
686 | } | |
687 | } | |
688 | break; | |
689 | ||
690 | case JPEG2000_PGOD_CPRL: | |
691 | for (compno = 0; compno < s->ncomponents; compno++) { | |
692 | Jpeg2000CodingStyle *codsty = tile->codsty + compno; | |
693 | Jpeg2000QuantStyle *qntsty = tile->qntsty + compno; | |
694 | ||
695 | /* Set bit stream buffer address according to tile-part. | |
696 | * For DCinema one tile-part per component, so can be | |
697 | * indexed by component. */ | |
1a3598aa | 698 | s->g = tile->tile_part[compno].tpg; |
c81a7063 NB |
699 | |
700 | /* Position loop (y axis) | |
701 | * TODO: Automate computing of step 256. | |
702 | * Fixed here, but to be computed before entering here. */ | |
703 | for (y = 0; y < s->height; y += 256) { | |
704 | /* Position loop (y axis) | |
705 | * TODO: automate computing of step 256. | |
706 | * Fixed here, but to be computed before entering here. */ | |
707 | for (x = 0; x < s->width; x += 256) { | |
708 | for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) { | |
709 | uint16_t prcx, prcy; | |
710 | uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r | |
711 | Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno; | |
712 | ||
713 | if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) || | |
714 | (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema | |
715 | continue; | |
716 | ||
717 | if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) || | |
718 | (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema | |
719 | continue; | |
720 | ||
721 | // check if a precinct exists | |
722 | prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width; | |
723 | prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height; | |
724 | precno = prcx + rlevel->num_precincts_x * prcy; | |
725 | for (layno = 0; layno < tile->codsty[0].nlayers; layno++) { | |
5efadcb8 LB |
726 | if ((ret = jpeg2000_decode_packet(s, codsty, rlevel, |
727 | precno, layno, | |
728 | qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0), | |
729 | qntsty->nguardbits)) < 0) | |
730 | return ret; | |
c81a7063 NB |
731 | } |
732 | } | |
733 | } | |
734 | } | |
735 | } | |
736 | break; | |
737 | ||
738 | default: | |
739 | break; | |
740 | } | |
741 | ||
742 | /* EOC marker reached */ | |
1a3598aa | 743 | bytestream2_skip(&s->g, 2); |
c81a7063 NB |
744 | |
745 | return 0; | |
746 | } | |
747 | ||
748 | /* TIER-1 routines */ | |
749 | static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, | |
750 | int bpno, int bandno) | |
751 | { | |
752 | int mask = 3 << (bpno - 1), y0, x, y; | |
753 | ||
754 | for (y0 = 0; y0 < height; y0 += 4) | |
755 | for (x = 0; x < width; x++) | |
756 | for (y = y0; y < height && y < y0 + 4; y++) | |
757 | if ((t1->flags[y + 1][x + 1] & JPEG2000_T1_SIG_NB) | |
758 | && !(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) { | |
759 | if (ff_mqc_decode(&t1->mqc, | |
760 | t1->mqc.cx_states + | |
761 | ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1], | |
762 | bandno))) { | |
763 | int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1], | |
764 | &xorbit); | |
765 | ||
766 | t1->data[y][x] = | |
767 | (ff_mqc_decode(&t1->mqc, | |
768 | t1->mqc.cx_states + ctxno) ^ xorbit) | |
769 | ? -mask : mask; | |
770 | ||
771 | ff_jpeg2000_set_significance(t1, x, y, | |
772 | t1->data[y][x] < 0); | |
773 | } | |
774 | t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS; | |
775 | } | |
776 | } | |
777 | ||
778 | static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, | |
779 | int bpno) | |
780 | { | |
781 | int phalf, nhalf; | |
782 | int y0, x, y; | |
783 | ||
784 | phalf = 1 << (bpno - 1); | |
785 | nhalf = -phalf; | |
786 | ||
787 | for (y0 = 0; y0 < height; y0 += 4) | |
788 | for (x = 0; x < width; x++) | |
789 | for (y = y0; y < height && y < y0 + 4; y++) | |
790 | if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) { | |
791 | int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]); | |
792 | int r = ff_mqc_decode(&t1->mqc, | |
793 | t1->mqc.cx_states + ctxno) | |
794 | ? phalf : nhalf; | |
795 | t1->data[y][x] += t1->data[y][x] < 0 ? -r : r; | |
796 | t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF; | |
797 | } | |
798 | } | |
799 | ||
800 | static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, | |
801 | int width, int height, int bpno, int bandno, | |
802 | int seg_symbols) | |
803 | { | |
804 | int mask = 3 << (bpno - 1), y0, x, y, runlen, dec; | |
805 | ||
806 | for (y0 = 0; y0 < height; y0 += 4) | |
807 | for (x = 0; x < width; x++) { | |
808 | if (y0 + 3 < height && | |
809 | !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) || | |
810 | (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) || | |
811 | (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) || | |
812 | (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) { | |
813 | if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL)) | |
814 | continue; | |
815 | runlen = ff_mqc_decode(&t1->mqc, | |
816 | t1->mqc.cx_states + MQC_CX_UNI); | |
817 | runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, | |
818 | t1->mqc.cx_states + | |
819 | MQC_CX_UNI); | |
820 | dec = 1; | |
821 | } else { | |
822 | runlen = 0; | |
823 | dec = 0; | |
824 | } | |
825 | ||
826 | for (y = y0 + runlen; y < y0 + 4 && y < height; y++) { | |
827 | if (!dec) { | |
828 | if (!(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) | |
829 | dec = ff_mqc_decode(&t1->mqc, | |
830 | t1->mqc.cx_states + | |
831 | ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1], | |
832 | bandno)); | |
833 | } | |
834 | if (dec) { | |
835 | int xorbit; | |
836 | int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1], | |
837 | &xorbit); | |
838 | t1->data[y][x] = (ff_mqc_decode(&t1->mqc, | |
839 | t1->mqc.cx_states + ctxno) ^ | |
840 | xorbit) | |
841 | ? -mask : mask; | |
842 | ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0); | |
843 | } | |
844 | dec = 0; | |
845 | t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS; | |
846 | } | |
847 | } | |
848 | if (seg_symbols) { | |
849 | int val; | |
850 | val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI); | |
851 | val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI); | |
852 | val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI); | |
853 | val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI); | |
854 | if (val != 0xa) | |
855 | av_log(s->avctx, AV_LOG_ERROR, | |
856 | "Segmentation symbol value incorrect\n"); | |
857 | } | |
858 | } | |
859 | ||
860 | static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, | |
861 | Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, | |
862 | int width, int height, int bandpos) | |
863 | { | |
864 | int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y; | |
865 | ||
c81a7063 NB |
866 | for (y = 0; y < height; y++) |
867 | memset(t1->data[y], 0, width * sizeof(width)); | |
28816f9d NB |
868 | /* If code-block contains no compressed data: nothing to do. */ |
869 | if (!cblk->length) | |
870 | return 0; | |
871 | for (y = 0; y < height + 2; y++) | |
872 | memset(t1->flags[y], 0, (width + 2) * sizeof(width)); | |
c81a7063 NB |
873 | |
874 | ff_mqc_initdec(&t1->mqc, cblk->data); | |
875 | cblk->data[cblk->length] = 0xff; | |
876 | cblk->data[cblk->length + 1] = 0xff; | |
877 | ||
878 | while (passno--) { | |
879 | switch (pass_t) { | |
880 | case 0: | |
881 | decode_sigpass(t1, width, height, bpno + 1, bandpos); | |
882 | break; | |
883 | case 1: | |
884 | decode_refpass(t1, width, height, bpno + 1); | |
885 | break; | |
886 | case 2: | |
887 | decode_clnpass(s, t1, width, height, bpno + 1, bandpos, | |
888 | codsty->cblk_style & JPEG2000_CBLK_SEGSYM); | |
889 | break; | |
890 | } | |
891 | ||
892 | pass_t++; | |
893 | if (pass_t == 3) { | |
894 | bpno--; | |
895 | pass_t = 0; | |
896 | } | |
897 | } | |
898 | return 0; | |
899 | } | |
900 | ||
901 | /* TODO: Verify dequantization for lossless case | |
902 | * comp->data can be float or int | |
903 | * band->stepsize can be float or int | |
904 | * depending on the type of DWT transformation. | |
905 | * see ISO/IEC 15444-1:2002 A.6.1 */ | |
906 | ||
907 | /* Float dequantization of a codeblock.*/ | |
908 | static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk, | |
909 | Jpeg2000Component *comp, | |
910 | Jpeg2000T1Context *t1, Jpeg2000Band *band) | |
911 | { | |
912 | int i, j, idx; | |
913 | float *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]; | |
914 | for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) | |
915 | for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) { | |
916 | idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i; | |
917 | datap[idx] = (float)(t1->data[j][i]) * ((float)band->stepsize); | |
918 | } | |
919 | return; | |
920 | } | |
921 | ||
922 | /* Integer dequantization of a codeblock.*/ | |
923 | static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, | |
924 | Jpeg2000Component *comp, | |
925 | Jpeg2000T1Context *t1, Jpeg2000Band *band) | |
926 | { | |
927 | int i, j, idx; | |
928 | int32_t *datap = | |
929 | (int32_t *) &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]; | |
930 | for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) | |
931 | for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) { | |
932 | idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i; | |
933 | datap[idx] = | |
934 | ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16; | |
935 | } | |
936 | return; | |
937 | } | |
938 | ||
939 | /* Inverse ICT parameters in float and integer. | |
940 | * int value = (float value) * (1<<16) */ | |
941 | static const float f_ict_params[4] = { | |
942 | 1.402f, | |
943 | 0.34413f, | |
944 | 0.71414f, | |
945 | 1.772f | |
946 | }; | |
947 | static const int i_ict_params[4] = { | |
948 | 91881, | |
949 | 22553, | |
950 | 46802, | |
951 | 116130 | |
952 | }; | |
953 | ||
954 | static int mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile) | |
955 | { | |
956 | int i, csize = 1; | |
957 | int ret = 0; | |
958 | int32_t *src[3], i0, i1, i2; | |
959 | float *srcf[3], i0f, i1f, i2f; | |
960 | ||
961 | for (i = 0; i < 3; i++) | |
962 | if (tile->codsty[0].transform == FF_DWT97) | |
963 | srcf[i] = tile->comp[i].data; | |
964 | else | |
965 | src[i] = (int32_t *)tile->comp[i].data; | |
966 | ||
967 | for (i = 0; i < 2; i++) | |
968 | csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0]; | |
969 | switch (tile->codsty[0].transform) { | |
970 | case FF_DWT97: | |
971 | for (i = 0; i < csize; i++) { | |
972 | i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]); | |
973 | i1f = *srcf[0] - (f_ict_params[1] * *srcf[1]) | |
974 | - (f_ict_params[2] * *srcf[2]); | |
975 | i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]); | |
976 | *srcf[0]++ = i0f; | |
977 | *srcf[1]++ = i1f; | |
978 | *srcf[2]++ = i2f; | |
979 | } | |
980 | break; | |
981 | case FF_DWT97_INT: | |
982 | for (i = 0; i < csize; i++) { | |
983 | i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16); | |
984 | i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16) | |
985 | - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16); | |
986 | i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16); | |
987 | *src[0]++ = i0; | |
988 | *src[1]++ = i1; | |
989 | *src[2]++ = i2; | |
990 | } | |
991 | break; | |
992 | case FF_DWT53: | |
993 | for (i = 0; i < csize; i++) { | |
994 | i1 = *src[0] - (*src[2] + *src[1] >> 2); | |
995 | i0 = i1 + *src[2]; | |
996 | i2 = i1 + *src[1]; | |
997 | *src[0]++ = i0; | |
998 | *src[1]++ = i1; | |
999 | *src[2]++ = i2; | |
1000 | } | |
1001 | break; | |
1002 | } | |
1003 | return ret; | |
1004 | } | |
1005 | ||
1006 | static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, | |
1007 | AVFrame *picture) | |
1008 | { | |
1009 | int compno, reslevelno, bandno; | |
1010 | int x, y; | |
1011 | ||
1012 | uint8_t *line; | |
1013 | Jpeg2000T1Context t1; | |
1014 | /* Loop on tile components */ | |
1015 | ||
1016 | for (compno = 0; compno < s->ncomponents; compno++) { | |
1017 | Jpeg2000Component *comp = tile->comp + compno; | |
1018 | Jpeg2000CodingStyle *codsty = tile->codsty + compno; | |
1019 | /* Loop on resolution levels */ | |
1020 | for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) { | |
1021 | Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno; | |
1022 | /* Loop on bands */ | |
1023 | for (bandno = 0; bandno < rlevel->nbands; bandno++) { | |
1024 | uint16_t nb_precincts, precno; | |
1025 | Jpeg2000Band *band = rlevel->band + bandno; | |
1026 | int cblkno = 0, bandpos; | |
1027 | bandpos = bandno + (reslevelno > 0); | |
1028 | ||
1029 | nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y; | |
1030 | /* Loop on precincts */ | |
1031 | for (precno = 0; precno < nb_precincts; precno++) { | |
1032 | Jpeg2000Prec *prec = band->prec + precno; | |
1033 | ||
1034 | /* Loop on codeblocks */ | |
1035 | for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) { | |
1036 | int x, y; | |
1037 | Jpeg2000Cblk *cblk = prec->cblk + cblkno; | |
1038 | decode_cblk(s, codsty, &t1, cblk, | |
1039 | cblk->coord[0][1] - cblk->coord[0][0], | |
1040 | cblk->coord[1][1] - cblk->coord[1][0], | |
1041 | bandpos); | |
1042 | ||
1043 | /* Manage band offsets */ | |
1044 | x = cblk->coord[0][0]; | |
1045 | y = cblk->coord[1][0]; | |
1046 | if ((reslevelno > 0) && ((bandno + 1) & 1)) { | |
1047 | Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1); | |
1048 | x += pres->coord[0][1] - pres->coord[0][0]; | |
1049 | } | |
1050 | if ((reslevelno > 0) && ((bandno + 1) & 2)) { | |
1051 | Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1); | |
1052 | y += pres->coord[1][1] - pres->coord[1][0]; | |
1053 | } | |
1054 | ||
1055 | if (s->avctx->flags & CODEC_FLAG_BITEXACT) | |
1056 | dequantization_int(x, y, cblk, comp, &t1, band); | |
1057 | else | |
1058 | dequantization_float(x, y, cblk, comp, &t1, band); | |
1059 | } /* end cblk */ | |
1060 | } /*end prec */ | |
1061 | } /* end band */ | |
1062 | } /* end reslevel */ | |
1063 | ||
1064 | /* inverse DWT */ | |
1065 | ff_dwt_decode(&comp->dwt, comp->data); | |
1066 | } /*end comp */ | |
1067 | ||
1068 | /* inverse MCT transformation */ | |
1069 | if (tile->codsty[0].mct) | |
1070 | mct_decode(s, tile); | |
1071 | ||
7c57a582 | 1072 | if (s->avctx->pix_fmt == AV_PIX_FMT_BGRA) // RGBA -> BGRA |
c81a7063 NB |
1073 | FFSWAP(float *, tile->comp[0].data, tile->comp[2].data); |
1074 | ||
1075 | if (s->precision <= 8) { | |
1076 | for (compno = 0; compno < s->ncomponents; compno++) { | |
1077 | Jpeg2000Component *comp = tile->comp + compno; | |
1078 | int32_t *datap = (int32_t *)comp->data; | |
1079 | y = tile->comp[compno].coord[1][0] - s->image_offset_y; | |
1080 | line = picture->data[0] + y * picture->linesize[0]; | |
1081 | for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { | |
1082 | uint8_t *dst; | |
1083 | ||
1084 | x = tile->comp[compno].coord[0][0] - s->image_offset_x; | |
1085 | dst = line + x * s->ncomponents + compno; | |
1086 | ||
1087 | for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) { | |
1088 | *datap += 1 << (s->cbps[compno] - 1); | |
1089 | if (*datap < 0) | |
1090 | *datap = 0; | |
1091 | else if (*datap >= (1 << s->cbps[compno])) | |
1092 | *datap = (1 << s->cbps[compno]) - 1; | |
1093 | *dst = *datap++; | |
1094 | dst += s->ncomponents; | |
1095 | } | |
1096 | line += picture->linesize[0]; | |
1097 | } | |
1098 | } | |
1099 | } else { | |
1100 | for (compno = 0; compno < s->ncomponents; compno++) { | |
1101 | Jpeg2000Component *comp = tile->comp + compno; | |
1102 | float *datap = comp->data; | |
1103 | int32_t *i_datap = (int32_t *) comp->data; | |
1104 | uint16_t *linel; | |
1105 | ||
1106 | y = tile->comp[compno].coord[1][0] - s->image_offset_y; | |
1107 | linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1); | |
1108 | for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { | |
1109 | uint16_t *dst; | |
1110 | x = tile->comp[compno].coord[0][0] - s->image_offset_x; | |
1111 | dst = linel + (x * s->ncomponents + compno); | |
1112 | for (; x < s->avctx->width; x += s->cdx[compno]) { | |
1113 | int16_t val; | |
1114 | /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ | |
1115 | if (s->avctx->flags & CODEC_FLAG_BITEXACT) | |
1116 | val = *i_datap + (1 << (s->cbps[compno] - 1)); | |
1117 | else | |
1118 | val = lrintf(*datap) + (1 << (s->cbps[compno] - 1)); | |
1119 | val = av_clip(val, 0, (1 << s->cbps[compno]) - 1); | |
1120 | /* align 12 bit values in little-endian mode */ | |
1121 | *dst = val << 4; | |
1122 | datap++; | |
1123 | i_datap++; | |
1124 | dst += s->ncomponents; | |
1125 | } | |
1126 | linel += picture->linesize[0] >> 1; | |
1127 | } | |
1128 | } | |
1129 | } | |
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s) | |
1134 | { | |
1135 | int tileno, compno; | |
1136 | for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) { | |
1137 | for (compno = 0; compno < s->ncomponents; compno++) { | |
1138 | Jpeg2000Component *comp = s->tile[tileno].comp + compno; | |
1139 | Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno; | |
1140 | ||
1141 | ff_jpeg2000_cleanup(comp, codsty); | |
1142 | } | |
1143 | av_freep(&s->tile[tileno].comp); | |
1144 | } | |
1145 | av_freep(&s->tile); | |
1146 | } | |
1147 | ||
1148 | static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s) | |
1149 | { | |
1150 | Jpeg2000CodingStyle *codsty = s->codsty; | |
1151 | Jpeg2000QuantStyle *qntsty = s->qntsty; | |
1152 | uint8_t *properties = s->properties; | |
1153 | ||
1154 | for (;;) { | |
1155 | int len, ret = 0; | |
1156 | uint16_t marker; | |
1a3598aa | 1157 | int oldpos; |
c81a7063 | 1158 | |
1a3598aa | 1159 | if (bytestream2_get_bytes_left(&s->g) < 2) { |
c81a7063 NB |
1160 | av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n"); |
1161 | break; | |
1162 | } | |
1163 | ||
1a3598aa MN |
1164 | marker = bytestream2_get_be16u(&s->g); |
1165 | oldpos = bytestream2_tell(&s->g); | |
c81a7063 NB |
1166 | |
1167 | if (marker == JPEG2000_EOC) | |
1168 | break; | |
1169 | ||
1a3598aa | 1170 | if (bytestream2_get_bytes_left(&s->g) < 2) |
5efadcb8 | 1171 | return AVERROR_INVALIDDATA; |
1a3598aa | 1172 | len = bytestream2_get_be16u(&s->g); |
c81a7063 NB |
1173 | switch (marker) { |
1174 | case JPEG2000_SIZ: | |
1175 | ret = get_siz(s); | |
1176 | break; | |
1177 | case JPEG2000_COC: | |
1178 | ret = get_coc(s, codsty, properties); | |
1179 | break; | |
1180 | case JPEG2000_COD: | |
1181 | ret = get_cod(s, codsty, properties); | |
1182 | break; | |
1183 | case JPEG2000_QCC: | |
1184 | ret = get_qcc(s, len, qntsty, properties); | |
1185 | break; | |
1186 | case JPEG2000_QCD: | |
1187 | ret = get_qcd(s, len, qntsty, properties); | |
1188 | break; | |
1189 | case JPEG2000_SOT: | |
1190 | ret = get_sot(s, len); | |
1191 | break; | |
1192 | case JPEG2000_COM: | |
1193 | // the comment is ignored | |
1a3598aa | 1194 | bytestream2_skip(&s->g, len - 2); |
c81a7063 NB |
1195 | break; |
1196 | case JPEG2000_TLM: | |
1197 | // Tile-part lengths | |
1198 | ret = get_tlm(s, len); | |
1199 | break; | |
1200 | default: | |
1201 | av_log(s->avctx, AV_LOG_ERROR, | |
1a3598aa MN |
1202 | "unsupported marker 0x%.4X at pos 0x%X\n", |
1203 | marker, bytestream2_tell(&s->g) - 4); | |
1204 | bytestream2_skip(&s->g, len - 2); | |
c81a7063 NB |
1205 | break; |
1206 | } | |
1a3598aa | 1207 | if (((bytestream2_tell(&s->g) - oldpos != len) && (marker != JPEG2000_SOT)) || ret) { |
c81a7063 NB |
1208 | av_log(s->avctx, AV_LOG_ERROR, |
1209 | "error during processing marker segment %.4x\n", marker); | |
1210 | return ret ? ret : -1; | |
1211 | } | |
1212 | } | |
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | /* Read bit stream packets --> T2 operation. */ | |
1217 | static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s) | |
1218 | { | |
1219 | int ret = 0; | |
1220 | Jpeg2000Tile *tile = s->tile + s->curtileno; | |
1221 | ||
1222 | if (ret = init_tile(s, s->curtileno)) | |
1223 | return ret; | |
1224 | if (ret = jpeg2000_decode_packets(s, tile)) | |
1225 | return ret; | |
1226 | ||
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | static int jp2_find_codestream(Jpeg2000DecoderContext *s) | |
1231 | { | |
1a3598aa | 1232 | uint32_t atom_size, atom; |
c81a7063 NB |
1233 | int found_codestream = 0, search_range = 10; |
1234 | ||
1a3598aa MN |
1235 | while(!found_codestream && search_range |
1236 | && | |
1237 | bytestream2_get_bytes_left(&s->g) >= 8) { | |
1238 | atom_size = bytestream2_get_be32u(&s->g); | |
1239 | atom = bytestream2_get_be32u(&s->g); | |
1240 | if (atom == JP2_CODESTREAM) { | |
c81a7063 | 1241 | found_codestream = 1; |
c81a7063 | 1242 | } else { |
1a3598aa MN |
1243 | if (bytestream2_get_bytes_left(&s->g) < atom_size - 8) |
1244 | return 0; | |
1245 | bytestream2_skipu(&s->g, atom_size - 8); | |
c81a7063 NB |
1246 | search_range--; |
1247 | } | |
1248 | } | |
1249 | ||
1250 | if (found_codestream) | |
1251 | return 1; | |
1252 | return 0; | |
1253 | } | |
1254 | ||
1255 | static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data, | |
1256 | int *got_frame, AVPacket *avpkt) | |
1257 | { | |
1258 | Jpeg2000DecoderContext *s = avctx->priv_data; | |
2e2d2466 | 1259 | ThreadFrame frame = { .f = data }; |
c81a7063 NB |
1260 | AVFrame *picture = data; |
1261 | int tileno, ret; | |
1262 | ||
1263 | s->avctx = avctx; | |
1a3598aa | 1264 | bytestream2_init(&s->g, avpkt->data, avpkt->size); |
c81a7063 NB |
1265 | s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles |
1266 | ||
1267 | // reduction factor, i.e number of resolution levels to skip | |
1268 | s->reduction_factor = s->lowres; | |
1269 | ||
1a3598aa | 1270 | if (bytestream2_get_bytes_left(&s->g) < 2) |
5efadcb8 | 1271 | return AVERROR_INVALIDDATA; |
c81a7063 NB |
1272 | |
1273 | // check if the image is in jp2 format | |
1a3598aa MN |
1274 | if (bytestream2_get_bytes_left(&s->g) >= 12 && |
1275 | (bytestream2_get_be32u(&s->g) == 12) && | |
1276 | (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) && | |
1277 | (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) { | |
c81a7063 NB |
1278 | if (!jp2_find_codestream(s)) { |
1279 | av_log(avctx, AV_LOG_ERROR, | |
5efadcb8 LB |
1280 | "Could not find Jpeg2000 codestream atom.\n"); |
1281 | return AVERROR_INVALIDDATA; | |
c81a7063 | 1282 | } |
1a3598aa MN |
1283 | } else { |
1284 | bytestream2_seek(&s->g, 0, SEEK_SET); | |
1285 | if (bytestream2_peek_be16(&s->g) != JPEG2000_SOC) | |
1286 | bytestream2_skip(&s->g, 8); | |
c81a7063 NB |
1287 | } |
1288 | ||
1a3598aa | 1289 | if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) { |
c81a7063 | 1290 | av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n"); |
5efadcb8 | 1291 | return AVERROR_INVALIDDATA; |
c81a7063 NB |
1292 | } |
1293 | if (ret = jpeg2000_read_main_headers(s)) | |
45a1694f | 1294 | goto end; |
c81a7063 NB |
1295 | |
1296 | /* get picture buffer */ | |
2e2d2466 NB |
1297 | if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) { |
1298 | av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n"); | |
45a1694f | 1299 | goto end; |
c81a7063 NB |
1300 | } |
1301 | picture->pict_type = AV_PICTURE_TYPE_I; | |
1302 | picture->key_frame = 1; | |
1303 | ||
1304 | if (ret = jpeg2000_read_bitstream_packets(s)) | |
45a1694f | 1305 | goto end; |
c81a7063 NB |
1306 | for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) |
1307 | if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture)) | |
45a1694f | 1308 | goto end; |
c81a7063 NB |
1309 | |
1310 | *got_frame = 1; | |
1311 | ||
1a3598aa MN |
1312 | return bytestream2_tell(&s->g); |
1313 | ||
45a1694f JG |
1314 | end: |
1315 | jpeg2000_dec_cleanup(s); | |
1a3598aa | 1316 | return ret; |
c81a7063 NB |
1317 | } |
1318 | ||
70f96615 JG |
1319 | static void jpeg2000_init_static_data(AVCodec *codec) |
1320 | { | |
1321 | ff_jpeg2000_init_tier1_luts(); | |
1322 | } | |
1323 | ||
c81a7063 NB |
1324 | #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x) |
1325 | #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | |
1326 | ||
1327 | static const AVOption options[] = { | |
1328 | { "lowres", "Lower the decoding resolution by a power of two", | |
1329 | OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD }, | |
1330 | { NULL }, | |
1331 | }; | |
1332 | ||
1333 | static const AVProfile profiles[] = { | |
1334 | { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" }, | |
1335 | { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" }, | |
1336 | { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" }, | |
1337 | { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" }, | |
1338 | { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" }, | |
1339 | { FF_PROFILE_UNKNOWN }, | |
1340 | }; | |
1341 | ||
1342 | static const AVClass class = { | |
1343 | .class_name = "jpeg2000", | |
1344 | .item_name = av_default_item_name, | |
1345 | .option = options, | |
1346 | .version = LIBAVUTIL_VERSION_INT, | |
1347 | }; | |
1348 | ||
1349 | AVCodec ff_jpeg2000_decoder = { | |
70f96615 JG |
1350 | .name = "jpeg2000", |
1351 | .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"), | |
1352 | .type = AVMEDIA_TYPE_VIDEO, | |
1353 | .id = AV_CODEC_ID_JPEG2000, | |
1354 | .capabilities = CODEC_CAP_FRAME_THREADS, | |
1355 | .priv_data_size = sizeof(Jpeg2000DecoderContext), | |
1356 | .init_static_data = jpeg2000_init_static_data, | |
1357 | .decode = jpeg2000_decode_frame, | |
1358 | .priv_class = &class, | |
7c57a582 AK |
1359 | .pix_fmts = (enum AVPixelFormat[]) { AV_PIX_FMT_XYZ12, |
1360 | AV_PIX_FMT_GRAY8, | |
1361 | -1 }, | |
70f96615 | 1362 | .profiles = NULL_IF_CONFIG_SMALL(profiles) |
c81a7063 | 1363 | }; |