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