2 * JPEG 2000 image decoder
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * JPEG 2000 image decoder
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/thread.h"
39 #include "bytestream.h"
43 #include "jpeg2000dsp.h"
46 #define JP2_SIG_TYPE 0x6A502020
47 #define JP2_SIG_VALUE 0x0D0A870A
48 #define JP2_CODESTREAM 0x6A703263
49 #define JP2_HEADER 0x6A703268
56 typedef struct Jpeg2000POCEntry {
65 typedef struct Jpeg2000POC {
66 Jpeg2000POCEntry poc[MAX_POCS];
71 typedef struct Jpeg2000TilePart {
72 uint8_t tile_index; // Tile index who refers the tile-part
73 const uint8_t *tp_end;
74 GetByteContext tpg; // bit stream in tile-part
77 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
78 * one per component, so tile_part elements have a size of 3 */
79 typedef struct Jpeg2000Tile {
80 Jpeg2000Component *comp;
81 uint8_t properties[4];
82 Jpeg2000CodingStyle codsty[4];
83 Jpeg2000QuantStyle qntsty[4];
85 Jpeg2000TilePart tile_part[32];
86 uint8_t has_ppt; // whether this tile has a ppt marker
87 uint8_t *packed_headers; // contains packed headers. Used only along with PPT marker
88 int packed_headers_size; // size in bytes of the packed headers
89 GetByteContext packed_headers_stream; // byte context corresponding to packed headers
90 uint16_t tp_idx; // Tile-part index
91 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
94 typedef struct Jpeg2000DecoderContext {
96 AVCodecContext *avctx;
100 int image_offset_x, image_offset_y;
101 int tile_offset_x, tile_offset_y;
102 uint8_t cbps[4]; // bits per sample in particular components
103 uint8_t sgnd[4]; // if a component is signed
104 uint8_t properties[4];
109 uint32_t palette[256];
112 int tile_width, tile_height;
113 unsigned numXtiles, numYtiles;
117 Jpeg2000CodingStyle codsty[4];
118 Jpeg2000QuantStyle qntsty[4];
120 uint8_t roi_shift[4];
127 Jpeg2000DSPContext dsp;
129 /*options parameters*/
130 int reduction_factor;
131 } Jpeg2000DecoderContext;
133 /* get_bits functions for JPEG2000 packet bitstream
134 * It is a get_bit function with a bit-stuffing routine. If the value of the
135 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
136 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
137 static int get_bits(Jpeg2000DecoderContext *s, int n)
143 if (s->bit_index == 0) {
144 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
147 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
152 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
154 if (bytestream2_get_byte(&s->g) == 0xff)
155 bytestream2_skip(&s->g, 1);
159 /* decode the value stored in node */
160 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
163 Jpeg2000TgtNode *stack[30];
164 int sp = -1, curval = 0;
167 av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
168 return AVERROR_INVALIDDATA;
171 while (node && !node->vis) {
179 curval = stack[sp]->val;
181 while (curval < threshold && sp >= 0) {
182 if (curval < stack[sp]->val)
183 curval = stack[sp]->val;
184 while (curval < threshold) {
186 if ((ret = get_bits(s, 1)) > 0) {
194 stack[sp]->val = curval;
200 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
201 int bpc, uint32_t log2_chroma_wh, int pal8)
204 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
208 if (desc->nb_components != components) {
212 switch (components) {
214 match = match && desc->comp[3].depth >= bpc &&
215 (log2_chroma_wh >> 14 & 3) == 0 &&
216 (log2_chroma_wh >> 12 & 3) == 0;
218 match = match && desc->comp[2].depth >= bpc &&
219 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
220 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
222 match = match && desc->comp[1].depth >= bpc &&
223 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
224 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
227 match = match && desc->comp[0].depth >= bpc &&
228 (log2_chroma_wh >> 2 & 3) == 0 &&
229 (log2_chroma_wh & 3) == 0 &&
230 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
235 // pix_fmts with lower bpp have to be listed before
236 // similar pix_fmts with higher bpp.
237 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
238 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
239 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
240 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
241 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
242 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
243 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
244 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
245 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
246 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
247 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
248 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
249 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
250 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
252 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
253 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
254 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
255 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS,
257 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
262 /* marker segments */
263 /* get sizes and offsets of image, tiles; number of components */
264 static int get_siz(Jpeg2000DecoderContext *s)
268 uint32_t log2_chroma_wh = 0;
269 const enum AVPixelFormat *possible_fmts = NULL;
270 int possible_fmts_nb = 0;
273 if (bytestream2_get_bytes_left(&s->g) < 36) {
274 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
275 return AVERROR_INVALIDDATA;
278 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
279 s->width = bytestream2_get_be32u(&s->g); // Width
280 s->height = bytestream2_get_be32u(&s->g); // Height
281 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
282 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
283 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
284 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
285 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
286 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
287 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
289 if (s->image_offset_x || s->image_offset_y) {
290 avpriv_request_sample(s->avctx, "Support for image offsets");
291 return AVERROR_PATCHWELCOME;
293 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
294 avpriv_request_sample(s->avctx, "Large Dimensions");
295 return AVERROR_PATCHWELCOME;
298 if (ncomponents <= 0) {
299 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
301 return AVERROR_INVALIDDATA;
304 if (ncomponents > 4) {
305 avpriv_request_sample(s->avctx, "Support for %d components",
307 return AVERROR_PATCHWELCOME;
310 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
311 s->image_offset_x < s->tile_offset_x ||
312 s->image_offset_y < s->tile_offset_y ||
313 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
314 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
316 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
317 return AVERROR_INVALIDDATA;
320 s->ncomponents = ncomponents;
322 if (s->tile_width <= 0 || s->tile_height <= 0) {
323 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
324 s->tile_width, s->tile_height);
325 return AVERROR_INVALIDDATA;
328 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
329 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
330 return AVERROR_INVALIDDATA;
333 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
334 uint8_t x = bytestream2_get_byteu(&s->g);
335 s->cbps[i] = (x & 0x7f) + 1;
336 s->precision = FFMAX(s->cbps[i], s->precision);
337 s->sgnd[i] = !!(x & 0x80);
338 s->cdx[i] = bytestream2_get_byteu(&s->g);
339 s->cdy[i] = bytestream2_get_byteu(&s->g);
340 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
341 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
342 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
343 return AVERROR_INVALIDDATA;
345 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
348 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
349 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
351 // There must be at least a SOT and SOD per tile, their minimum size is 14
352 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
353 s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
355 s->numXtiles = s->numYtiles = 0;
356 return AVERROR(EINVAL);
359 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
361 s->numXtiles = s->numYtiles = 0;
362 return AVERROR(ENOMEM);
365 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
366 Jpeg2000Tile *tile = s->tile + i;
368 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
370 return AVERROR(ENOMEM);
373 /* compute image size with reduction factor */
374 ret = ff_set_dimensions(s->avctx,
375 ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
376 s->reduction_factor),
377 ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
378 s->reduction_factor));
382 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
383 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
384 possible_fmts = xyz_pix_fmts;
385 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
387 switch (s->colour_space) {
389 possible_fmts = rgb_pix_fmts;
390 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
393 possible_fmts = gray_pix_fmts;
394 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
397 possible_fmts = yuv_pix_fmts;
398 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
401 possible_fmts = all_pix_fmts;
402 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
406 if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
407 && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
408 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
409 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
410 for (i = 0; i < possible_fmts_nb; ++i) {
411 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
412 s->avctx->pix_fmt = possible_fmts[i];
417 if (i == possible_fmts_nb) {
418 if (ncomponents == 4 &&
419 s->cdy[0] == 1 && s->cdx[0] == 1 &&
420 s->cdy[1] == 1 && s->cdx[1] == 1 &&
421 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
422 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
423 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
434 if (i == possible_fmts_nb) {
435 av_log(s->avctx, AV_LOG_ERROR,
436 "Unknown pix_fmt, profile: %d, colour_space: %d, "
437 "components: %d, precision: %d\n"
438 "cdx[0]: %d, cdy[0]: %d\n"
439 "cdx[1]: %d, cdy[1]: %d\n"
440 "cdx[2]: %d, cdy[2]: %d\n"
441 "cdx[3]: %d, cdy[3]: %d\n",
442 s->avctx->profile, s->colour_space, ncomponents, s->precision,
445 ncomponents > 1 ? s->cdx[1] : 0,
446 ncomponents > 1 ? s->cdy[1] : 0,
447 ncomponents > 2 ? s->cdx[2] : 0,
448 ncomponents > 2 ? s->cdy[2] : 0,
449 ncomponents > 3 ? s->cdx[3] : 0,
450 ncomponents > 3 ? s->cdy[3] : 0);
451 return AVERROR_PATCHWELCOME;
453 s->avctx->bits_per_raw_sample = s->precision;
457 /* get common part for COD and COC segments */
458 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
462 if (bytestream2_get_bytes_left(&s->g) < 5) {
463 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
464 return AVERROR_INVALIDDATA;
467 /* nreslevels = number of resolution levels
468 = number of decomposition level +1 */
469 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
470 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
471 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
472 return AVERROR_INVALIDDATA;
475 if (c->nreslevels <= s->reduction_factor) {
476 /* we are forced to update reduction_factor as its requested value is
477 not compatible with this bitstream, and as we might have used it
478 already in setup earlier we have to fail this frame until
479 reinitialization is implemented */
480 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
481 s->reduction_factor = c->nreslevels - 1;
482 return AVERROR(EINVAL);
485 /* compute number of resolution levels to decode */
486 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
488 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
489 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
491 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
492 c->log2_cblk_width + c->log2_cblk_height > 12) {
493 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
494 return AVERROR_INVALIDDATA;
497 c->cblk_style = bytestream2_get_byteu(&s->g);
498 if (c->cblk_style != 0) { // cblk style
499 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
500 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
501 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
503 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
504 /* set integer 9/7 DWT in case of BITEXACT flag */
505 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
506 c->transform = FF_DWT97_INT;
507 else if (c->transform == FF_DWT53) {
508 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
511 if (c->csty & JPEG2000_CSTY_PREC) {
513 for (i = 0; i < c->nreslevels; i++) {
514 byte = bytestream2_get_byte(&s->g);
515 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
516 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
518 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
519 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
520 c->log2_prec_widths[i], c->log2_prec_heights[i]);
521 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
522 return AVERROR_INVALIDDATA;
526 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
527 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
532 /* get coding parameters for a particular tile or whole image*/
533 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
536 Jpeg2000CodingStyle tmp;
539 if (bytestream2_get_bytes_left(&s->g) < 5) {
540 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
541 return AVERROR_INVALIDDATA;
544 tmp.csty = bytestream2_get_byteu(&s->g);
546 // get progression order
547 tmp.prog_order = bytestream2_get_byteu(&s->g);
549 tmp.nlayers = bytestream2_get_be16u(&s->g);
550 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
552 if (tmp.mct && s->ncomponents < 3) {
553 av_log(s->avctx, AV_LOG_ERROR,
554 "MCT %"PRIu8" with too few components (%d)\n",
555 tmp.mct, s->ncomponents);
556 return AVERROR_INVALIDDATA;
559 if ((ret = get_cox(s, &tmp)) < 0)
562 for (compno = 0; compno < s->ncomponents; compno++)
563 if (!(properties[compno] & HAD_COC))
564 memcpy(c + compno, &tmp, sizeof(tmp));
568 /* Get coding parameters for a component in the whole image or a
569 * particular tile. */
570 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
576 if (bytestream2_get_bytes_left(&s->g) < 2) {
577 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
578 return AVERROR_INVALIDDATA;
581 compno = bytestream2_get_byteu(&s->g);
583 if (compno >= s->ncomponents) {
584 av_log(s->avctx, AV_LOG_ERROR,
585 "Invalid compno %d. There are %d components in the image.\n",
586 compno, s->ncomponents);
587 return AVERROR_INVALIDDATA;
591 has_eph = c->csty & JPEG2000_CSTY_EPH;
592 c->csty = bytestream2_get_byteu(&s->g);
593 c->csty |= has_eph; //do not override eph present bits from COD
595 if ((ret = get_cox(s, c)) < 0)
598 properties[compno] |= HAD_COC;
602 static int get_rgn(Jpeg2000DecoderContext *s, int n)
605 compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
606 bytestream2_get_be16u(&s->g);
607 if (bytestream2_get_byte(&s->g)) {
608 av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
609 return AVERROR_INVALIDDATA; // SRgn field value is 0
612 // Currently compno cannot be greater than 4.
613 // However, future implementation should support compno up to 65536
614 if (compno < s->ncomponents) {
615 if (s->curtileno == -1)
616 s->roi_shift[compno] = bytestream2_get_byte(&s->g);
618 if (s->tile[s->curtileno].tp_idx != 0)
619 return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
620 s->tile[s->curtileno].comp[compno].roi_shift = bytestream2_get_byte(&s->g);
624 return AVERROR_INVALIDDATA;
627 /* Get common part for QCD and QCC segments. */
628 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
632 if (bytestream2_get_bytes_left(&s->g) < 1)
633 return AVERROR_INVALIDDATA;
635 x = bytestream2_get_byteu(&s->g); // Sqcd
637 q->nguardbits = x >> 5;
638 q->quantsty = x & 0x1f;
640 if (q->quantsty == JPEG2000_QSTY_NONE) {
642 if (bytestream2_get_bytes_left(&s->g) < n ||
643 n > JPEG2000_MAX_DECLEVELS*3)
644 return AVERROR_INVALIDDATA;
645 for (i = 0; i < n; i++)
646 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
647 } else if (q->quantsty == JPEG2000_QSTY_SI) {
648 if (bytestream2_get_bytes_left(&s->g) < 2)
649 return AVERROR_INVALIDDATA;
650 x = bytestream2_get_be16u(&s->g);
651 q->expn[0] = x >> 11;
652 q->mant[0] = x & 0x7ff;
653 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
654 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
655 q->expn[i] = curexpn;
656 q->mant[i] = q->mant[0];
660 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
661 n > JPEG2000_MAX_DECLEVELS*3)
662 return AVERROR_INVALIDDATA;
663 for (i = 0; i < n; i++) {
664 x = bytestream2_get_be16u(&s->g);
665 q->expn[i] = x >> 11;
666 q->mant[i] = x & 0x7ff;
672 /* Get quantization parameters for a particular tile or a whole image. */
673 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
676 Jpeg2000QuantStyle tmp;
679 memset(&tmp, 0, sizeof(tmp));
681 if ((ret = get_qcx(s, n, &tmp)) < 0)
683 for (compno = 0; compno < s->ncomponents; compno++)
684 if (!(properties[compno] & HAD_QCC))
685 memcpy(q + compno, &tmp, sizeof(tmp));
689 /* Get quantization parameters for a component in the whole image
690 * on in a particular tile. */
691 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
696 if (bytestream2_get_bytes_left(&s->g) < 1)
697 return AVERROR_INVALIDDATA;
699 compno = bytestream2_get_byteu(&s->g);
701 if (compno >= s->ncomponents) {
702 av_log(s->avctx, AV_LOG_ERROR,
703 "Invalid compno %d. There are %d components in the image.\n",
704 compno, s->ncomponents);
705 return AVERROR_INVALIDDATA;
708 properties[compno] |= HAD_QCC;
709 return get_qcx(s, n - 1, q + compno);
712 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
715 int elem_size = s->ncomponents <= 257 ? 7 : 9;
716 Jpeg2000POC tmp = {{{0}}};
718 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
719 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
720 return AVERROR_INVALIDDATA;
724 avpriv_request_sample(s->avctx, "Fat POC not supported");
725 return AVERROR_PATCHWELCOME;
728 tmp.nb_poc = (size - 2) / elem_size;
729 if (tmp.nb_poc > MAX_POCS) {
730 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
731 return AVERROR_PATCHWELCOME;
734 for (i = 0; i<tmp.nb_poc; i++) {
735 Jpeg2000POCEntry *e = &tmp.poc[i];
736 e->RSpoc = bytestream2_get_byteu(&s->g);
737 e->CSpoc = bytestream2_get_byteu(&s->g);
738 e->LYEpoc = bytestream2_get_be16u(&s->g);
739 e->REpoc = bytestream2_get_byteu(&s->g);
740 e->CEpoc = bytestream2_get_byteu(&s->g);
741 e->Ppoc = bytestream2_get_byteu(&s->g);
744 if (e->CEpoc > s->ncomponents)
745 e->CEpoc = s->ncomponents;
746 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
747 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
749 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
750 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
752 return AVERROR_INVALIDDATA;
756 if (!p->nb_poc || p->is_default) {
759 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
760 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
761 return AVERROR_INVALIDDATA;
763 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
764 p->nb_poc += tmp.nb_poc;
773 /* Get start of tile segment. */
774 static int get_sot(Jpeg2000DecoderContext *s, int n)
776 Jpeg2000TilePart *tp;
781 if (bytestream2_get_bytes_left(&s->g) < 8)
782 return AVERROR_INVALIDDATA;
785 Isot = bytestream2_get_be16u(&s->g); // Isot
786 if (Isot >= s->numXtiles * s->numYtiles)
787 return AVERROR_INVALIDDATA;
790 Psot = bytestream2_get_be32u(&s->g); // Psot
791 TPsot = bytestream2_get_byteu(&s->g); // TPsot
793 /* Read TNSot but not used */
794 bytestream2_get_byteu(&s->g); // TNsot
797 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
799 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
800 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
801 return AVERROR_INVALIDDATA;
804 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
805 avpriv_request_sample(s->avctx, "Too many tile parts");
806 return AVERROR_PATCHWELCOME;
809 s->tile[Isot].tp_idx = TPsot;
810 tp = s->tile[Isot].tile_part + TPsot;
811 tp->tile_index = Isot;
812 tp->tp_end = s->g.buffer + Psot - n - 2;
815 Jpeg2000Tile *tile = s->tile + s->curtileno;
818 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
819 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
820 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
821 tile->poc.is_default = 1;
827 static int read_crg(Jpeg2000DecoderContext *s, int n)
829 if (s->ncomponents*4 != n - 2) {
830 av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
831 return AVERROR_INVALIDDATA;
833 bytestream2_skip(&s->g, n - 2);
836 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
837 * Used to know the number of tile parts and lengths.
838 * There may be multiple TLMs in the header.
839 * TODO: The function is not used for tile-parts management, nor anywhere else.
840 * It can be useful to allocate memory for tile parts, before managing the SOT
841 * markers. Parsing the TLM header is needed to increment the input header
843 * This marker is mandatory for DCI. */
844 static int get_tlm(Jpeg2000DecoderContext *s, int n)
846 uint8_t Stlm, ST, SP, tile_tlm, i;
847 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
848 Stlm = bytestream2_get_byte(&s->g);
850 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
851 ST = (Stlm >> 4) & 0x03;
853 av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
854 return AVERROR_INVALIDDATA;
857 SP = (Stlm >> 6) & 0x01;
858 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
859 for (i = 0; i < tile_tlm; i++) {
864 bytestream2_get_byte(&s->g);
867 bytestream2_get_be16(&s->g);
870 bytestream2_get_be32(&s->g);
874 bytestream2_get_be16(&s->g);
876 bytestream2_get_be32(&s->g);
882 static int get_plt(Jpeg2000DecoderContext *s, int n)
887 av_log(s->avctx, AV_LOG_DEBUG,
888 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
891 return AVERROR_INVALIDDATA;
893 /*Zplt =*/ bytestream2_get_byte(&s->g);
895 for (i = 0; i < n - 3; i++) {
896 v = bytestream2_get_byte(&s->g);
899 return AVERROR_INVALIDDATA;
904 static int get_ppt(Jpeg2000DecoderContext *s, int n)
910 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
911 return AVERROR_INVALIDDATA;
913 if (s->curtileno < 0)
914 return AVERROR_INVALIDDATA;
916 tile = &s->tile[s->curtileno];
917 if (tile->tp_idx != 0) {
918 av_log(s->avctx, AV_LOG_ERROR,
919 "PPT marker can occur only on first tile part of a tile.\n");
920 return AVERROR_INVALIDDATA;
923 tile->has_ppt = 1; // this tile has a ppt marker
924 bytestream2_get_byte(&s->g); // Zppt is skipped and not used
925 new = av_realloc(tile->packed_headers,
926 tile->packed_headers_size + n - 3);
928 tile->packed_headers = new;
930 return AVERROR(ENOMEM);
931 memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
932 memcpy(tile->packed_headers + tile->packed_headers_size,
934 tile->packed_headers_size += n - 3;
935 bytestream2_skip(&s->g, n - 3);
940 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
943 int tilex = tileno % s->numXtiles;
944 int tiley = tileno / s->numXtiles;
945 Jpeg2000Tile *tile = s->tile + tileno;
948 return AVERROR(ENOMEM);
950 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
951 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
952 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
953 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
955 for (compno = 0; compno < s->ncomponents; compno++) {
956 Jpeg2000Component *comp = tile->comp + compno;
957 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
958 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
959 int ret; // global bandno
961 comp->coord_o[0][0] = tile->coord[0][0];
962 comp->coord_o[0][1] = tile->coord[0][1];
963 comp->coord_o[1][0] = tile->coord[1][0];
964 comp->coord_o[1][1] = tile->coord[1][1];
966 comp->coord_o[0][0] /= s->cdx[compno];
967 comp->coord_o[0][1] /= s->cdx[compno];
968 comp->coord_o[1][0] /= s->cdy[compno];
969 comp->coord_o[1][1] /= s->cdy[compno];
972 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
973 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
974 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
975 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
977 if (!comp->roi_shift)
978 comp->roi_shift = s->roi_shift[compno];
980 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
981 s->cbps[compno], s->cdx[compno],
982 s->cdy[compno], s->avctx))
988 /* Read the number of coding passes. */
989 static int getnpasses(Jpeg2000DecoderContext *s)
996 if ((num = get_bits(s, 2)) != 3)
997 return num < 0 ? num : 3 + num;
998 if ((num = get_bits(s, 5)) != 31)
999 return num < 0 ? num : 6 + num;
1000 num = get_bits(s, 7);
1001 return num < 0 ? num : 37 + num;
1004 static int getlblockinc(Jpeg2000DecoderContext *s)
1007 while (ret = get_bits(s, 1)) {
1015 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1018 s->g = tile->tile_part[*tp_index].tpg;
1019 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1020 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1021 s->g = tile->tile_part[++(*tp_index)].tpg;
1024 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1025 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1028 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1029 Jpeg2000CodingStyle *codsty,
1030 Jpeg2000ResLevel *rlevel, int precno,
1031 int layno, uint8_t *expn, int numgbits)
1033 int bandno, cblkno, ret, nb_code_blocks;
1036 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1038 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1039 // Select stream to read from
1041 s->g = tile->packed_headers_stream;
1043 select_stream(s, tile, tp_index);
1045 if (!(ret = get_bits(s, 1))) {
1051 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1052 Jpeg2000Band *band = rlevel->band + bandno;
1053 Jpeg2000Prec *prec = band->prec + precno;
1055 if (band->coord[0][0] == band->coord[0][1] ||
1056 band->coord[1][0] == band->coord[1][1])
1058 nb_code_blocks = prec->nb_codeblocks_height *
1059 prec->nb_codeblocks_width;
1060 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1061 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1062 int incl, newpasses, llen;
1066 incl = get_bits(s, 1);
1068 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1074 if (!cblk->npasses) {
1075 int v = expn[bandno] + numgbits - 1 -
1076 tag_tree_decode(s, prec->zerobits + cblkno, 100);
1077 if (v < 0 || v > 30) {
1078 av_log(s->avctx, AV_LOG_ERROR,
1079 "nonzerobits %d invalid or unsupported\n", v);
1080 return AVERROR_INVALIDDATA;
1082 cblk->nonzerobits = v;
1084 if ((newpasses = getnpasses(s)) < 0)
1086 av_assert2(newpasses > 0);
1087 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1088 avpriv_request_sample(s->avctx, "Too many passes");
1089 return AVERROR_PATCHWELCOME;
1091 if ((llen = getlblockinc(s)) < 0)
1093 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1094 avpriv_request_sample(s->avctx,
1095 "Block with length beyond 16 bits");
1096 return AVERROR_PATCHWELCOME;
1099 cblk->lblock += llen;
1101 cblk->nb_lengthinc = 0;
1102 cblk->nb_terminationsinc = 0;
1103 av_free(cblk->lengthinc);
1104 cblk->lengthinc = av_mallocz_array(newpasses , sizeof(*cblk->lengthinc));
1105 if (!cblk->lengthinc)
1106 return AVERROR(ENOMEM);
1107 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1109 return AVERROR(ENOMEM);
1110 cblk->data_start = tmp;
1114 while (newpasses1 < newpasses) {
1116 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1117 cblk->nb_terminationsinc ++;
1122 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1124 if (ret > cblk->data_allocated) {
1125 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1126 void *new = av_realloc(cblk->data, new_size);
1129 cblk->data_allocated = new_size;
1132 if (ret > cblk->data_allocated) {
1133 avpriv_request_sample(s->avctx,
1134 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1135 cblk->data_allocated);
1136 return AVERROR_PATCHWELCOME;
1138 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1139 cblk->npasses += newpasses1;
1140 newpasses -= newpasses1;
1146 if (codsty->csty & JPEG2000_CSTY_EPH) {
1147 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1148 bytestream2_skip(&s->g, 2);
1150 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1153 // Save state of stream
1154 if (tile->has_ppt) {
1155 tile->packed_headers_stream = s->g;
1156 select_stream(s, tile, tp_index);
1158 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1159 Jpeg2000Band *band = rlevel->band + bandno;
1160 Jpeg2000Prec *prec = band->prec + precno;
1162 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1163 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1164 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1165 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1167 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1168 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1169 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1170 void *new = av_realloc(cblk->data, new_size);
1173 cblk->data_allocated = new_size;
1176 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1177 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1179 av_log(s->avctx, AV_LOG_ERROR,
1180 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1181 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1182 return AVERROR_INVALIDDATA;
1185 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1186 cblk->length += cblk->lengthinc[cwsno];
1187 cblk->lengthinc[cwsno] = 0;
1188 if (cblk->nb_terminationsinc) {
1189 cblk->nb_terminationsinc--;
1190 cblk->nb_terminations++;
1191 cblk->data[cblk->length++] = 0xFF;
1192 cblk->data[cblk->length++] = 0xFF;
1193 cblk->data_start[cblk->nb_terminations] = cblk->length;
1196 av_freep(&cblk->lengthinc);
1199 // Save state of stream
1200 tile->tile_part[*tp_index].tpg = s->g;
1205 tile->packed_headers_stream = s->g;
1207 tile->tile_part[*tp_index].tpg = s->g;
1211 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1212 int RSpoc, int CSpoc,
1213 int LYEpoc, int REpoc, int CEpoc,
1214 int Ppoc, int *tp_index)
1217 int layno, reslevelno, compno, precno, ok_reslevel;
1222 case JPEG2000_PGOD_RLCP:
1223 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1225 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1227 for (layno = 0; layno < LYEpoc; layno++) {
1228 for (compno = CSpoc; compno < CEpoc; compno++) {
1229 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1230 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1231 if (reslevelno < codsty->nreslevels) {
1232 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1235 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1236 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1239 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1240 qntsty->nguardbits)) < 0)
1248 case JPEG2000_PGOD_LRCP:
1249 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1250 for (layno = 0; layno < LYEpoc; layno++) {
1252 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1254 for (compno = CSpoc; compno < CEpoc; compno++) {
1255 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1256 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1257 if (reslevelno < codsty->nreslevels) {
1258 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1261 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1262 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1265 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1266 qntsty->nguardbits)) < 0)
1274 case JPEG2000_PGOD_CPRL:
1275 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1276 for (compno = CSpoc; compno < CEpoc; compno++) {
1277 Jpeg2000Component *comp = tile->comp + compno;
1278 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1279 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1283 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1286 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1287 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1288 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1289 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1290 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1292 if (step_x >= 31 || step_y >= 31){
1293 avpriv_request_sample(s->avctx, "CPRL with large step");
1294 return AVERROR_PATCHWELCOME;
1299 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1300 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1301 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1302 unsigned prcx, prcy;
1303 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1304 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1305 int xc = x / s->cdx[compno];
1306 int yc = y / s->cdy[compno];
1308 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1311 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1314 // check if a precinct exists
1315 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1316 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1317 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1318 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1320 precno = prcx + rlevel->num_precincts_x * prcy;
1322 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1323 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1324 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1328 for (layno = 0; layno < LYEpoc; layno++) {
1329 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1331 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1332 qntsty->nguardbits)) < 0)
1341 case JPEG2000_PGOD_RPCL:
1342 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1344 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1348 for (compno = CSpoc; compno < CEpoc; compno++) {
1349 Jpeg2000Component *comp = tile->comp + compno;
1350 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1352 if (reslevelno < codsty->nreslevels) {
1353 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1354 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1355 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1356 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1362 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1363 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1364 for (compno = CSpoc; compno < CEpoc; compno++) {
1365 Jpeg2000Component *comp = tile->comp + compno;
1366 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1367 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1368 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1369 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1370 unsigned prcx, prcy;
1372 int xc = x / s->cdx[compno];
1373 int yc = y / s->cdy[compno];
1375 if (reslevelno >= codsty->nreslevels)
1378 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1381 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1384 // check if a precinct exists
1385 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1386 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1387 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1388 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1390 precno = prcx + rlevel->num_precincts_x * prcy;
1393 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1394 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1395 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1399 for (layno = 0; layno < LYEpoc; layno++) {
1400 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1403 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1404 qntsty->nguardbits)) < 0)
1413 case JPEG2000_PGOD_PCRL:
1414 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1417 for (compno = CSpoc; compno < CEpoc; compno++) {
1418 Jpeg2000Component *comp = tile->comp + compno;
1419 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1421 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1422 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1423 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1424 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1425 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1428 if (step_x >= 31 || step_y >= 31){
1429 avpriv_request_sample(s->avctx, "PCRL with large step");
1430 return AVERROR_PATCHWELCOME;
1435 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1436 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1437 for (compno = CSpoc; compno < CEpoc; compno++) {
1438 Jpeg2000Component *comp = tile->comp + compno;
1439 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1440 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1441 int xc = x / s->cdx[compno];
1442 int yc = y / s->cdy[compno];
1444 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1445 unsigned prcx, prcy;
1446 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1447 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1449 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1452 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1455 // check if a precinct exists
1456 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1457 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1458 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1459 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1461 precno = prcx + rlevel->num_precincts_x * prcy;
1463 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1464 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1465 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1469 for (layno = 0; layno < LYEpoc; layno++) {
1470 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1472 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1473 qntsty->nguardbits)) < 0)
1489 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1491 int ret = AVERROR_BUG;
1496 if (tile->poc.nb_poc) {
1497 for (i=0; i<tile->poc.nb_poc; i++) {
1498 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1499 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1501 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1503 FFMIN(e->CEpoc, s->ncomponents),
1510 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1512 tile->codsty[0].nlayers,
1515 tile->codsty[0].prog_order,
1519 /* EOC marker reached */
1520 bytestream2_skip(&s->g, 2);
1525 /* TIER-1 routines */
1526 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1527 int bpno, int bandno,
1528 int vert_causal_ctx_csty_symbol)
1530 int mask = 3 << (bpno - 1), y0, x, y;
1532 for (y0 = 0; y0 < height; y0 += 4)
1533 for (x = 0; x < width; x++)
1534 for (y = y0; y < height && y < y0 + 4; y++) {
1535 int flags_mask = -1;
1536 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1537 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1538 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1539 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1540 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1541 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1543 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1545 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1548 ff_jpeg2000_set_significance(t1, x, y,
1549 t1->data[(y) * t1->stride + x] < 0);
1551 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1556 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1557 int bpno, int vert_causal_ctx_csty_symbol)
1562 phalf = 1 << (bpno - 1);
1565 for (y0 = 0; y0 < height; y0 += 4)
1566 for (x = 0; x < width; x++)
1567 for (y = y0; y < height && y < y0 + 4; y++)
1568 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1569 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1570 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1571 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1572 int r = ff_mqc_decode(&t1->mqc,
1573 t1->mqc.cx_states + ctxno)
1575 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1576 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1580 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1581 int width, int height, int bpno, int bandno,
1582 int seg_symbols, int vert_causal_ctx_csty_symbol)
1584 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1586 for (y0 = 0; y0 < height; y0 += 4) {
1587 for (x = 0; x < width; x++) {
1588 int flags_mask = -1;
1589 if (vert_causal_ctx_csty_symbol)
1590 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1591 if (y0 + 3 < height &&
1592 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1593 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1594 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1595 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1596 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1598 runlen = ff_mqc_decode(&t1->mqc,
1599 t1->mqc.cx_states + MQC_CX_UNI);
1600 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1609 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1610 int flags_mask = -1;
1611 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1612 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1614 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1615 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1621 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1623 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1624 t1->mqc.cx_states + ctxno) ^
1627 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1630 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1636 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1637 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1638 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1639 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1641 av_log(s->avctx, AV_LOG_ERROR,
1642 "Segmentation symbol value incorrect\n");
1646 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1647 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1648 int width, int height, int bandpos, uint8_t roi_shift)
1650 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1652 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1656 av_assert0(width <= 1024U && height <= 1024U);
1657 av_assert0(width*height <= 4096);
1659 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1661 /* If code-block contains no compressed data: nothing to do. */
1665 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1667 cblk->data[cblk->length] = 0xff;
1668 cblk->data[cblk->length+1] = 0xff;
1669 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1673 av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1674 return AVERROR_INVALIDDATA;
1678 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1679 vert_causal_ctx_csty_symbol);
1682 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1685 av_assert2(!t1->mqc.raw);
1686 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1687 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1688 vert_causal_ctx_csty_symbol);
1691 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1692 ff_mqc_init_contexts(&t1->mqc);
1694 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1695 if (term_cnt >= cblk->nb_terminations) {
1696 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1697 return AVERROR_INVALIDDATA;
1699 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1700 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1701 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1702 pass_cnt, cblk->npasses);
1705 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1716 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1717 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1718 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1724 static inline int roi_shift_param(Jpeg2000Component *comp,
1729 roi_shift = comp->roi_shift;
1730 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1732 if (val > (1 << roi_shift))
1733 return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1734 return quan_parameter;
1737 /* TODO: Verify dequantization for lossless case
1738 * comp->data can be float or int
1739 * band->stepsize can be float or int
1740 * depending on the type of DWT transformation.
1741 * see ISO/IEC 15444-1:2002 A.6.1 */
1743 /* Float dequantization of a codeblock.*/
1744 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1745 Jpeg2000Component *comp,
1746 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1749 int w = cblk->coord[0][1] - cblk->coord[0][0];
1750 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1751 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1752 int *src = t1->data + j*t1->stride;
1753 for (i = 0; i < w; ++i)
1754 datap[i] = src[i] * band->f_stepsize;
1758 /* Integer dequantization of a codeblock.*/
1759 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1760 Jpeg2000Component *comp,
1761 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1764 int w = cblk->coord[0][1] - cblk->coord[0][0];
1765 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1766 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1767 int *src = t1->data + j*t1->stride;
1768 if (band->i_stepsize == 32768) {
1769 for (i = 0; i < w; ++i)
1770 datap[i] = src[i] / 2;
1772 // This should be VERY uncommon
1773 for (i = 0; i < w; ++i)
1774 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1779 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1780 Jpeg2000Component *comp,
1781 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1784 int w = cblk->coord[0][1] - cblk->coord[0][0];
1785 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1786 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1787 int *src = t1->data + j*t1->stride;
1788 for (i = 0; i < w; ++i)
1789 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1793 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1798 for (i = 1; i < 3; i++) {
1799 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1800 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1803 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1804 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1809 for (i = 0; i < 3; i++)
1810 if (tile->codsty[0].transform == FF_DWT97)
1811 src[i] = tile->comp[i].f_data;
1813 src[i] = tile->comp[i].i_data;
1815 for (i = 0; i < 2; i++)
1816 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1818 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1821 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1822 Jpeg2000Component *comp,
1823 Jpeg2000T1Context *t1)
1826 int w = cblk->coord[0][1] - cblk->coord[0][0];
1827 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1828 int *src = t1->data + j*t1->stride;
1829 for (i = 0; i < w; ++i)
1830 src[i] = roi_shift_param(comp, src[i]);
1834 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1836 Jpeg2000T1Context t1;
1838 int compno, reslevelno, bandno;
1840 /* Loop on tile components */
1841 for (compno = 0; compno < s->ncomponents; compno++) {
1842 Jpeg2000Component *comp = tile->comp + compno;
1843 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1846 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1848 /* Loop on resolution levels */
1849 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1850 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1852 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1853 int nb_precincts, precno;
1854 Jpeg2000Band *band = rlevel->band + bandno;
1855 int cblkno = 0, bandpos;
1857 bandpos = bandno + (reslevelno > 0);
1859 if (band->coord[0][0] == band->coord[0][1] ||
1860 band->coord[1][0] == band->coord[1][1])
1863 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1864 /* Loop on precincts */
1865 for (precno = 0; precno < nb_precincts; precno++) {
1866 Jpeg2000Prec *prec = band->prec + precno;
1868 /* Loop on codeblocks */
1870 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1873 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1874 int ret = decode_cblk(s, codsty, &t1, cblk,
1875 cblk->coord[0][1] - cblk->coord[0][0],
1876 cblk->coord[1][1] - cblk->coord[1][0],
1877 bandpos, comp->roi_shift);
1882 x = cblk->coord[0][0] - band->coord[0][0];
1883 y = cblk->coord[1][0] - band->coord[1][0];
1885 if (comp->roi_shift)
1886 roi_scale_cblk(cblk, comp, &t1);
1887 if (codsty->transform == FF_DWT97)
1888 dequantization_float(x, y, cblk, comp, &t1, band);
1889 else if (codsty->transform == FF_DWT97_INT)
1890 dequantization_int_97(x, y, cblk, comp, &t1, band);
1892 dequantization_int(x, y, cblk, comp, &t1, band);
1896 } /* end reslevel */
1900 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1905 #define WRITE_FRAME(D, PIXEL) \
1906 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1907 AVFrame * picture, int precision) \
1909 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1910 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1911 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1916 for (compno = 0; compno < s->ncomponents; compno++) { \
1917 Jpeg2000Component *comp = tile->comp + compno; \
1918 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1920 float *datap = comp->f_data; \
1921 int32_t *i_datap = comp->i_data; \
1922 int cbps = s->cbps[compno]; \
1923 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1927 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1929 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno]; \
1930 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1931 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) { \
1934 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno]; \
1935 dst = line + x * pixelsize + compno*!planar; \
1937 if (codsty->transform == FF_DWT97) { \
1938 for (; x < w; x++) { \
1939 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1940 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1941 val = av_clip(val, 0, (1 << cbps) - 1); \
1942 *dst = val << (precision - cbps); \
1947 for (; x < w; x++) { \
1948 int val = *i_datap + (1 << (cbps - 1)); \
1949 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1950 val = av_clip(val, 0, (1 << cbps) - 1); \
1951 *dst = val << (precision - cbps); \
1956 line += picture->linesize[plane] / sizeof(PIXEL); \
1962 WRITE_FRAME(8, uint8_t)
1963 WRITE_FRAME(16, uint16_t)
1967 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1968 int jobnr, int threadnr)
1970 Jpeg2000DecoderContext *s = avctx->priv_data;
1971 AVFrame *picture = td;
1972 Jpeg2000Tile *tile = s->tile + jobnr;
1975 tile_codeblocks(s, tile);
1977 /* inverse MCT transformation */
1978 if (tile->codsty[0].mct)
1979 mct_decode(s, tile);
1981 for (x = 0; x < s->ncomponents; x++) {
1982 if (s->cdef[x] < 0) {
1983 for (x = 0; x < s->ncomponents; x++) {
1986 if ((s->ncomponents & 1) == 0)
1987 s->cdef[s->ncomponents-1] = 0;
1992 if (s->precision <= 8) {
1993 write_frame_8(s, tile, picture, 8);
1995 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1996 picture->format == AV_PIX_FMT_RGB48 ||
1997 picture->format == AV_PIX_FMT_RGBA64 ||
1998 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2000 write_frame_16(s, tile, picture, precision);
2006 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2009 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2010 if (s->tile[tileno].comp) {
2011 for (compno = 0; compno < s->ncomponents; compno++) {
2012 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2013 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2015 ff_jpeg2000_cleanup(comp, codsty);
2017 av_freep(&s->tile[tileno].comp);
2018 av_freep(&s->tile[tileno].packed_headers);
2019 s->tile[tileno].packed_headers_size = 0;
2023 memset(s->codsty, 0, sizeof(s->codsty));
2024 memset(s->qntsty, 0, sizeof(s->qntsty));
2025 memset(s->properties, 0, sizeof(s->properties));
2026 memset(&s->poc , 0, sizeof(s->poc));
2027 s->numXtiles = s->numYtiles = 0;
2031 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2033 Jpeg2000CodingStyle *codsty = s->codsty;
2034 Jpeg2000QuantStyle *qntsty = s->qntsty;
2035 Jpeg2000POC *poc = &s->poc;
2036 uint8_t *properties = s->properties;
2043 if (bytestream2_get_bytes_left(&s->g) < 2) {
2044 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2048 marker = bytestream2_get_be16u(&s->g);
2049 oldpos = bytestream2_tell(&s->g);
2051 if (marker == JPEG2000_SOD) {
2053 Jpeg2000TilePart *tp;
2056 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2057 return AVERROR_INVALIDDATA;
2059 if (s->curtileno < 0) {
2060 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2061 return AVERROR_INVALIDDATA;
2064 tile = s->tile + s->curtileno;
2065 tp = tile->tile_part + tile->tp_idx;
2066 if (tp->tp_end < s->g.buffer) {
2067 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2068 return AVERROR_INVALIDDATA;
2071 if (tile->has_ppt && tile->tp_idx == 0) {
2072 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2075 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2076 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2080 if (marker == JPEG2000_EOC)
2083 len = bytestream2_get_be16(&s->g);
2084 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2085 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2086 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2087 return AVERROR_INVALIDDATA;
2089 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2095 if (s->ncomponents) {
2096 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2097 return AVERROR_INVALIDDATA;
2101 s->numXtiles = s->numYtiles = 0;
2104 ret = get_coc(s, codsty, properties);
2107 ret = get_cod(s, codsty, properties);
2110 ret = get_rgn(s, len);
2113 ret = get_qcc(s, len, qntsty, properties);
2116 ret = get_qcd(s, len, qntsty, properties);
2119 ret = get_poc(s, len, poc);
2122 if (!(ret = get_sot(s, len))) {
2123 av_assert1(s->curtileno >= 0);
2124 codsty = s->tile[s->curtileno].codsty;
2125 qntsty = s->tile[s->curtileno].qntsty;
2126 poc = &s->tile[s->curtileno].poc;
2127 properties = s->tile[s->curtileno].properties;
2131 // the PLM marker is ignored
2133 // the comment is ignored
2134 bytestream2_skip(&s->g, len - 2);
2137 ret = read_crg(s, len);
2140 // Tile-part lengths
2141 ret = get_tlm(s, len);
2144 // Packet length, tile-part header
2145 ret = get_plt(s, len);
2148 // Packed headers, tile-part header
2149 ret = get_ppt(s, len);
2152 av_log(s->avctx, AV_LOG_ERROR,
2153 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2154 marker, bytestream2_tell(&s->g) - 4);
2155 bytestream2_skip(&s->g, len - 2);
2158 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2159 av_log(s->avctx, AV_LOG_ERROR,
2160 "error during processing marker segment %.4"PRIx16"\n",
2162 return ret ? ret : -1;
2168 /* Read bit stream packets --> T2 operation. */
2169 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2174 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2175 Jpeg2000Tile *tile = s->tile + tileno;
2177 if ((ret = init_tile(s, tileno)) < 0)
2180 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2187 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2189 uint32_t atom_size, atom, atom_end;
2190 int search_range = 10;
2194 bytestream2_get_bytes_left(&s->g) >= 8) {
2195 atom_size = bytestream2_get_be32u(&s->g);
2196 atom = bytestream2_get_be32u(&s->g);
2197 if (atom_size == 1) {
2198 if (bytestream2_get_be32u(&s->g)) {
2199 avpriv_request_sample(s->avctx, "Huge atom");
2202 atom_size = bytestream2_get_be32u(&s->g);
2203 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2205 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2208 if (atom == JP2_CODESTREAM)
2211 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2214 if (atom == JP2_HEADER &&
2216 uint32_t atom2_size, atom2, atom2_end;
2218 atom2_size = bytestream2_get_be32u(&s->g);
2219 atom2 = bytestream2_get_be32u(&s->g);
2220 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2221 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2224 if (atom2 == JP2_CODESTREAM) {
2226 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2227 int method = bytestream2_get_byteu(&s->g);
2228 bytestream2_skipu(&s->g, 2);
2230 s->colour_space = bytestream2_get_be32u(&s->g);
2232 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2233 int i, size, colour_count, colour_channels, colour_depth[3];
2234 colour_count = bytestream2_get_be16u(&s->g);
2235 colour_channels = bytestream2_get_byteu(&s->g);
2236 // FIXME: Do not ignore channel_sign
2237 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2238 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2239 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2240 size = (colour_depth[0] + 7 >> 3) * colour_count +
2241 (colour_depth[1] + 7 >> 3) * colour_count +
2242 (colour_depth[2] + 7 >> 3) * colour_count;
2243 if (colour_count > AVPALETTE_COUNT ||
2244 colour_channels != 3 ||
2245 colour_depth[0] > 16 ||
2246 colour_depth[1] > 16 ||
2247 colour_depth[2] > 16 ||
2248 atom2_size < size) {
2249 avpriv_request_sample(s->avctx, "Unknown palette");
2250 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2254 for (i = 0; i < colour_count; i++) {
2256 if (colour_depth[0] <= 8) {
2257 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2258 r |= r >> colour_depth[0];
2260 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2262 if (colour_depth[1] <= 8) {
2263 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2264 g |= g >> colour_depth[1];
2266 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2268 if (colour_depth[2] <= 8) {
2269 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2270 b |= b >> colour_depth[2];
2272 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2274 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2276 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2277 int n = bytestream2_get_be16u(&s->g);
2279 int cn = bytestream2_get_be16(&s->g);
2280 int av_unused typ = bytestream2_get_be16(&s->g);
2281 int asoc = bytestream2_get_be16(&s->g);
2282 if (cn < 4 && asoc < 4)
2285 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2286 int64_t vnum, vden, hnum, hden, vexp, hexp;
2288 bytestream2_skip(&s->g, 4);
2289 resx = bytestream2_get_be32u(&s->g);
2290 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2291 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2294 vnum = bytestream2_get_be16u(&s->g);
2295 vden = bytestream2_get_be16u(&s->g);
2296 hnum = bytestream2_get_be16u(&s->g);
2297 hden = bytestream2_get_be16u(&s->g);
2298 vexp = bytestream2_get_byteu(&s->g);
2299 hexp = bytestream2_get_byteu(&s->g);
2300 if (!vnum || !vden || !hnum || !hden) {
2301 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2302 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2312 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2313 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2314 av_reduce(&s->sar.den, &s->sar.num,
2315 hnum * vden * pow(10, hexp),
2316 vnum * hden * pow(10, vexp),
2319 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2320 } while (atom_end - atom2_end >= 8);
2324 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2330 static av_cold void jpeg2000_init_static_data(void)
2332 ff_jpeg2000_init_tier1_luts();
2333 ff_mqc_init_context_tables();
2336 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2338 static AVOnce init_static_once = AV_ONCE_INIT;
2339 Jpeg2000DecoderContext *s = avctx->priv_data;
2341 ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2342 ff_jpeg2000dsp_init(&s->dsp);
2347 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2348 int *got_frame, AVPacket *avpkt)
2350 Jpeg2000DecoderContext *s = avctx->priv_data;
2351 ThreadFrame frame = { .f = data };
2352 AVFrame *picture = data;
2356 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2358 memset(s->cdef, -1, sizeof(s->cdef));
2360 if (bytestream2_get_bytes_left(&s->g) < 2) {
2361 ret = AVERROR_INVALIDDATA;
2365 // check if the image is in jp2 format
2366 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2367 (bytestream2_get_be32u(&s->g) == 12) &&
2368 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2369 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2370 if (!jp2_find_codestream(s)) {
2371 av_log(avctx, AV_LOG_ERROR,
2372 "Could not find Jpeg2000 codestream atom.\n");
2373 ret = AVERROR_INVALIDDATA;
2377 bytestream2_seek(&s->g, 0, SEEK_SET);
2380 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2381 bytestream2_skip(&s->g, 1);
2383 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2384 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2385 ret = AVERROR_INVALIDDATA;
2388 if (ret = jpeg2000_read_main_headers(s))
2391 /* get picture buffer */
2392 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2394 picture->pict_type = AV_PICTURE_TYPE_I;
2395 picture->key_frame = 1;
2397 if (ret = jpeg2000_read_bitstream_packets(s))
2400 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2402 jpeg2000_dec_cleanup(s);
2406 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2407 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2408 if (s->sar.num && s->sar.den)
2409 avctx->sample_aspect_ratio = s->sar;
2410 s->sar.num = s->sar.den = 0;
2412 return bytestream2_tell(&s->g);
2415 jpeg2000_dec_cleanup(s);
2419 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2420 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2422 static const AVOption options[] = {
2423 { "lowres", "Lower the decoding resolution by a power of two",
2424 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2428 static const AVClass jpeg2000_class = {
2429 .class_name = "jpeg2000",
2430 .item_name = av_default_item_name,
2432 .version = LIBAVUTIL_VERSION_INT,
2435 AVCodec ff_jpeg2000_decoder = {
2437 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2438 .type = AVMEDIA_TYPE_VIDEO,
2439 .id = AV_CODEC_ID_JPEG2000,
2440 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2441 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2442 .init = jpeg2000_decode_init,
2443 .decode = jpeg2000_decode_frame,
2444 .priv_class = &jpeg2000_class,
2446 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)