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 memcpy(tile->packed_headers + tile->packed_headers_size,
933 tile->packed_headers_size += n - 3;
934 bytestream2_skip(&s->g, n - 3);
939 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
942 int tilex = tileno % s->numXtiles;
943 int tiley = tileno / s->numXtiles;
944 Jpeg2000Tile *tile = s->tile + tileno;
947 return AVERROR(ENOMEM);
949 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
950 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
951 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
952 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
954 for (compno = 0; compno < s->ncomponents; compno++) {
955 Jpeg2000Component *comp = tile->comp + compno;
956 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
957 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
958 int ret; // global bandno
960 comp->coord_o[0][0] = tile->coord[0][0];
961 comp->coord_o[0][1] = tile->coord[0][1];
962 comp->coord_o[1][0] = tile->coord[1][0];
963 comp->coord_o[1][1] = tile->coord[1][1];
965 comp->coord_o[0][0] /= s->cdx[compno];
966 comp->coord_o[0][1] /= s->cdx[compno];
967 comp->coord_o[1][0] /= s->cdy[compno];
968 comp->coord_o[1][1] /= s->cdy[compno];
971 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
972 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
973 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
974 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
976 if (!comp->roi_shift)
977 comp->roi_shift = s->roi_shift[compno];
979 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
980 s->cbps[compno], s->cdx[compno],
981 s->cdy[compno], s->avctx))
987 /* Read the number of coding passes. */
988 static int getnpasses(Jpeg2000DecoderContext *s)
995 if ((num = get_bits(s, 2)) != 3)
996 return num < 0 ? num : 3 + num;
997 if ((num = get_bits(s, 5)) != 31)
998 return num < 0 ? num : 6 + num;
999 num = get_bits(s, 7);
1000 return num < 0 ? num : 37 + num;
1003 static int getlblockinc(Jpeg2000DecoderContext *s)
1006 while (ret = get_bits(s, 1)) {
1014 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1017 s->g = tile->tile_part[*tp_index].tpg;
1018 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1019 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1020 s->g = tile->tile_part[++(*tp_index)].tpg;
1023 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1024 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1027 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1028 Jpeg2000CodingStyle *codsty,
1029 Jpeg2000ResLevel *rlevel, int precno,
1030 int layno, uint8_t *expn, int numgbits)
1032 int bandno, cblkno, ret, nb_code_blocks;
1035 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1037 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1038 // Select stream to read from
1040 s->g = tile->packed_headers_stream;
1042 select_stream(s, tile, tp_index);
1044 if (!(ret = get_bits(s, 1))) {
1050 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1051 Jpeg2000Band *band = rlevel->band + bandno;
1052 Jpeg2000Prec *prec = band->prec + precno;
1054 if (band->coord[0][0] == band->coord[0][1] ||
1055 band->coord[1][0] == band->coord[1][1])
1057 nb_code_blocks = prec->nb_codeblocks_height *
1058 prec->nb_codeblocks_width;
1059 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1060 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1061 int incl, newpasses, llen;
1065 incl = get_bits(s, 1);
1067 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1073 if (!cblk->npasses) {
1074 int v = expn[bandno] + numgbits - 1 -
1075 tag_tree_decode(s, prec->zerobits + cblkno, 100);
1076 if (v < 0 || v > 30) {
1077 av_log(s->avctx, AV_LOG_ERROR,
1078 "nonzerobits %d invalid or unsupported\n", v);
1079 return AVERROR_INVALIDDATA;
1081 cblk->nonzerobits = v;
1083 if ((newpasses = getnpasses(s)) < 0)
1085 av_assert2(newpasses > 0);
1086 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1087 avpriv_request_sample(s->avctx, "Too many passes");
1088 return AVERROR_PATCHWELCOME;
1090 if ((llen = getlblockinc(s)) < 0)
1092 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1093 avpriv_request_sample(s->avctx,
1094 "Block with length beyond 16 bits");
1095 return AVERROR_PATCHWELCOME;
1098 cblk->lblock += llen;
1100 cblk->nb_lengthinc = 0;
1101 cblk->nb_terminationsinc = 0;
1102 av_free(cblk->lengthinc);
1103 cblk->lengthinc = av_mallocz_array(newpasses , sizeof(*cblk->lengthinc));
1104 if (!cblk->lengthinc)
1105 return AVERROR(ENOMEM);
1106 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1108 return AVERROR(ENOMEM);
1109 cblk->data_start = tmp;
1113 while (newpasses1 < newpasses) {
1115 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1116 cblk->nb_terminationsinc ++;
1121 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1123 if (ret > cblk->data_allocated) {
1124 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1125 void *new = av_realloc(cblk->data, new_size);
1128 cblk->data_allocated = new_size;
1131 if (ret > cblk->data_allocated) {
1132 avpriv_request_sample(s->avctx,
1133 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1134 cblk->data_allocated);
1135 return AVERROR_PATCHWELCOME;
1137 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1138 cblk->npasses += newpasses1;
1139 newpasses -= newpasses1;
1145 if (codsty->csty & JPEG2000_CSTY_EPH) {
1146 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1147 bytestream2_skip(&s->g, 2);
1149 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1152 // Save state of stream
1153 if (tile->has_ppt) {
1154 tile->packed_headers_stream = s->g;
1155 select_stream(s, tile, tp_index);
1157 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1158 Jpeg2000Band *band = rlevel->band + bandno;
1159 Jpeg2000Prec *prec = band->prec + precno;
1161 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1162 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1163 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1164 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1166 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1167 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1168 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1169 void *new = av_realloc(cblk->data, new_size);
1172 cblk->data_allocated = new_size;
1175 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1176 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1178 av_log(s->avctx, AV_LOG_ERROR,
1179 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1180 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1181 return AVERROR_INVALIDDATA;
1184 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1185 cblk->length += cblk->lengthinc[cwsno];
1186 cblk->lengthinc[cwsno] = 0;
1187 if (cblk->nb_terminationsinc) {
1188 cblk->nb_terminationsinc--;
1189 cblk->nb_terminations++;
1190 cblk->data[cblk->length++] = 0xFF;
1191 cblk->data[cblk->length++] = 0xFF;
1192 cblk->data_start[cblk->nb_terminations] = cblk->length;
1195 av_freep(&cblk->lengthinc);
1198 // Save state of stream
1199 tile->tile_part[*tp_index].tpg = s->g;
1204 tile->packed_headers_stream = s->g;
1206 tile->tile_part[*tp_index].tpg = s->g;
1210 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1211 int RSpoc, int CSpoc,
1212 int LYEpoc, int REpoc, int CEpoc,
1213 int Ppoc, int *tp_index)
1216 int layno, reslevelno, compno, precno, ok_reslevel;
1221 case JPEG2000_PGOD_RLCP:
1222 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1224 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1226 for (layno = 0; layno < LYEpoc; layno++) {
1227 for (compno = CSpoc; compno < CEpoc; compno++) {
1228 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1229 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1230 if (reslevelno < codsty->nreslevels) {
1231 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1234 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1235 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1238 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1239 qntsty->nguardbits)) < 0)
1247 case JPEG2000_PGOD_LRCP:
1248 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1249 for (layno = 0; layno < LYEpoc; layno++) {
1251 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1253 for (compno = CSpoc; compno < CEpoc; compno++) {
1254 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1255 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1256 if (reslevelno < codsty->nreslevels) {
1257 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1260 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1261 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1264 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1265 qntsty->nguardbits)) < 0)
1273 case JPEG2000_PGOD_CPRL:
1274 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1275 for (compno = CSpoc; compno < CEpoc; compno++) {
1276 Jpeg2000Component *comp = tile->comp + compno;
1277 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1278 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1282 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1285 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1286 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1287 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1288 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1289 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1291 if (step_x >= 31 || step_y >= 31){
1292 avpriv_request_sample(s->avctx, "CPRL with large step");
1293 return AVERROR_PATCHWELCOME;
1298 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1299 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1300 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1301 unsigned prcx, prcy;
1302 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1303 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1304 int xc = x / s->cdx[compno];
1305 int yc = y / s->cdy[compno];
1307 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1310 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1313 // check if a precinct exists
1314 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1315 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1316 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1317 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1319 precno = prcx + rlevel->num_precincts_x * prcy;
1321 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1322 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1323 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1327 for (layno = 0; layno < LYEpoc; layno++) {
1328 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1330 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1331 qntsty->nguardbits)) < 0)
1340 case JPEG2000_PGOD_RPCL:
1341 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1343 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1347 for (compno = CSpoc; compno < CEpoc; compno++) {
1348 Jpeg2000Component *comp = tile->comp + compno;
1349 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1351 if (reslevelno < codsty->nreslevels) {
1352 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1353 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1354 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1355 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1361 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1362 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1363 for (compno = CSpoc; compno < CEpoc; compno++) {
1364 Jpeg2000Component *comp = tile->comp + compno;
1365 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1366 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1367 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1368 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1369 unsigned prcx, prcy;
1371 int xc = x / s->cdx[compno];
1372 int yc = y / s->cdy[compno];
1374 if (reslevelno >= codsty->nreslevels)
1377 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1380 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1383 // check if a precinct exists
1384 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1385 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1386 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1387 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1389 precno = prcx + rlevel->num_precincts_x * prcy;
1392 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1393 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1394 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1398 for (layno = 0; layno < LYEpoc; layno++) {
1399 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1402 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1403 qntsty->nguardbits)) < 0)
1412 case JPEG2000_PGOD_PCRL:
1413 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1416 for (compno = CSpoc; compno < CEpoc; compno++) {
1417 Jpeg2000Component *comp = tile->comp + compno;
1418 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1420 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1421 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1422 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1423 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1424 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1427 if (step_x >= 31 || step_y >= 31){
1428 avpriv_request_sample(s->avctx, "PCRL with large step");
1429 return AVERROR_PATCHWELCOME;
1434 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1435 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1436 for (compno = CSpoc; compno < CEpoc; compno++) {
1437 Jpeg2000Component *comp = tile->comp + compno;
1438 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1439 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1440 int xc = x / s->cdx[compno];
1441 int yc = y / s->cdy[compno];
1443 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1444 unsigned prcx, prcy;
1445 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1446 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1448 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1451 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1454 // check if a precinct exists
1455 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1456 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1457 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1458 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1460 precno = prcx + rlevel->num_precincts_x * prcy;
1462 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1463 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1464 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1468 for (layno = 0; layno < LYEpoc; layno++) {
1469 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1471 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1472 qntsty->nguardbits)) < 0)
1488 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1490 int ret = AVERROR_BUG;
1495 if (tile->poc.nb_poc) {
1496 for (i=0; i<tile->poc.nb_poc; i++) {
1497 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1498 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1500 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1502 FFMIN(e->CEpoc, s->ncomponents),
1509 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1511 tile->codsty[0].nlayers,
1514 tile->codsty[0].prog_order,
1518 /* EOC marker reached */
1519 bytestream2_skip(&s->g, 2);
1524 /* TIER-1 routines */
1525 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1526 int bpno, int bandno,
1527 int vert_causal_ctx_csty_symbol)
1529 int mask = 3 << (bpno - 1), y0, x, y;
1531 for (y0 = 0; y0 < height; y0 += 4)
1532 for (x = 0; x < width; x++)
1533 for (y = y0; y < height && y < y0 + 4; y++) {
1534 int flags_mask = -1;
1535 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1536 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1537 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1538 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1539 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1540 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1542 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1544 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1547 ff_jpeg2000_set_significance(t1, x, y,
1548 t1->data[(y) * t1->stride + x] < 0);
1550 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1555 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1556 int bpno, int vert_causal_ctx_csty_symbol)
1561 phalf = 1 << (bpno - 1);
1564 for (y0 = 0; y0 < height; y0 += 4)
1565 for (x = 0; x < width; x++)
1566 for (y = y0; y < height && y < y0 + 4; y++)
1567 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1568 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1569 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1570 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1571 int r = ff_mqc_decode(&t1->mqc,
1572 t1->mqc.cx_states + ctxno)
1574 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1575 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1579 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1580 int width, int height, int bpno, int bandno,
1581 int seg_symbols, int vert_causal_ctx_csty_symbol)
1583 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1585 for (y0 = 0; y0 < height; y0 += 4) {
1586 for (x = 0; x < width; x++) {
1587 int flags_mask = -1;
1588 if (vert_causal_ctx_csty_symbol)
1589 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1590 if (y0 + 3 < height &&
1591 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1592 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1593 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1594 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1595 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1597 runlen = ff_mqc_decode(&t1->mqc,
1598 t1->mqc.cx_states + MQC_CX_UNI);
1599 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1608 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1609 int flags_mask = -1;
1610 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1611 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1613 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1614 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1620 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1622 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1623 t1->mqc.cx_states + ctxno) ^
1626 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1629 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1635 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1636 val = (val << 1) + 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);
1640 av_log(s->avctx, AV_LOG_ERROR,
1641 "Segmentation symbol value incorrect\n");
1645 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1646 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1647 int width, int height, int bandpos, uint8_t roi_shift)
1649 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1651 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1655 av_assert0(width <= 1024U && height <= 1024U);
1656 av_assert0(width*height <= 4096);
1658 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1660 /* If code-block contains no compressed data: nothing to do. */
1664 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1666 cblk->data[cblk->length] = 0xff;
1667 cblk->data[cblk->length+1] = 0xff;
1668 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1672 av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1673 return AVERROR_INVALIDDATA;
1677 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1678 vert_causal_ctx_csty_symbol);
1681 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1684 av_assert2(!t1->mqc.raw);
1685 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1686 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1687 vert_causal_ctx_csty_symbol);
1690 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1691 ff_mqc_init_contexts(&t1->mqc);
1693 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1694 if (term_cnt >= cblk->nb_terminations) {
1695 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1696 return AVERROR_INVALIDDATA;
1698 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1699 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1700 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1701 pass_cnt, cblk->npasses);
1704 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1715 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1716 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1717 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1723 static inline int roi_shift_param(Jpeg2000Component *comp,
1728 roi_shift = comp->roi_shift;
1729 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1731 if (val > (1 << roi_shift))
1732 return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1733 return quan_parameter;
1736 /* TODO: Verify dequantization for lossless case
1737 * comp->data can be float or int
1738 * band->stepsize can be float or int
1739 * depending on the type of DWT transformation.
1740 * see ISO/IEC 15444-1:2002 A.6.1 */
1742 /* Float dequantization of a codeblock.*/
1743 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1744 Jpeg2000Component *comp,
1745 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1748 int w = cblk->coord[0][1] - cblk->coord[0][0];
1749 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1750 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1751 int *src = t1->data + j*t1->stride;
1752 for (i = 0; i < w; ++i)
1753 datap[i] = src[i] * band->f_stepsize;
1757 /* Integer dequantization of a codeblock.*/
1758 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1759 Jpeg2000Component *comp,
1760 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1763 int w = cblk->coord[0][1] - cblk->coord[0][0];
1764 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1765 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1766 int *src = t1->data + j*t1->stride;
1767 if (band->i_stepsize == 32768) {
1768 for (i = 0; i < w; ++i)
1769 datap[i] = src[i] / 2;
1771 // This should be VERY uncommon
1772 for (i = 0; i < w; ++i)
1773 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1778 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1779 Jpeg2000Component *comp,
1780 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1783 int w = cblk->coord[0][1] - cblk->coord[0][0];
1784 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1785 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1786 int *src = t1->data + j*t1->stride;
1787 for (i = 0; i < w; ++i)
1788 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1792 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1797 for (i = 1; i < 3; i++) {
1798 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1799 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1802 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1803 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1808 for (i = 0; i < 3; i++)
1809 if (tile->codsty[0].transform == FF_DWT97)
1810 src[i] = tile->comp[i].f_data;
1812 src[i] = tile->comp[i].i_data;
1814 for (i = 0; i < 2; i++)
1815 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1817 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1820 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1821 Jpeg2000Component *comp,
1822 Jpeg2000T1Context *t1)
1825 int w = cblk->coord[0][1] - cblk->coord[0][0];
1826 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1827 int *src = t1->data + j*t1->stride;
1828 for (i = 0; i < w; ++i)
1829 src[i] = roi_shift_param(comp, src[i]);
1833 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1835 Jpeg2000T1Context t1;
1837 int compno, reslevelno, bandno;
1839 /* Loop on tile components */
1840 for (compno = 0; compno < s->ncomponents; compno++) {
1841 Jpeg2000Component *comp = tile->comp + compno;
1842 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1845 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1847 /* Loop on resolution levels */
1848 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1849 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1851 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1852 int nb_precincts, precno;
1853 Jpeg2000Band *band = rlevel->band + bandno;
1854 int cblkno = 0, bandpos;
1856 bandpos = bandno + (reslevelno > 0);
1858 if (band->coord[0][0] == band->coord[0][1] ||
1859 band->coord[1][0] == band->coord[1][1])
1862 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1863 /* Loop on precincts */
1864 for (precno = 0; precno < nb_precincts; precno++) {
1865 Jpeg2000Prec *prec = band->prec + precno;
1867 /* Loop on codeblocks */
1869 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1872 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1873 int ret = decode_cblk(s, codsty, &t1, cblk,
1874 cblk->coord[0][1] - cblk->coord[0][0],
1875 cblk->coord[1][1] - cblk->coord[1][0],
1876 bandpos, comp->roi_shift);
1881 x = cblk->coord[0][0] - band->coord[0][0];
1882 y = cblk->coord[1][0] - band->coord[1][0];
1884 if (comp->roi_shift)
1885 roi_scale_cblk(cblk, comp, &t1);
1886 if (codsty->transform == FF_DWT97)
1887 dequantization_float(x, y, cblk, comp, &t1, band);
1888 else if (codsty->transform == FF_DWT97_INT)
1889 dequantization_int_97(x, y, cblk, comp, &t1, band);
1891 dequantization_int(x, y, cblk, comp, &t1, band);
1895 } /* end reslevel */
1899 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1904 #define WRITE_FRAME(D, PIXEL) \
1905 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1906 AVFrame * picture, int precision) \
1908 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1909 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1910 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1915 for (compno = 0; compno < s->ncomponents; compno++) { \
1916 Jpeg2000Component *comp = tile->comp + compno; \
1917 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1919 float *datap = comp->f_data; \
1920 int32_t *i_datap = comp->i_data; \
1921 int cbps = s->cbps[compno]; \
1922 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1926 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1928 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno]; \
1929 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1930 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) { \
1933 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno]; \
1934 dst = line + x * pixelsize + compno*!planar; \
1936 if (codsty->transform == FF_DWT97) { \
1937 for (; x < w; x++) { \
1938 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1939 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1940 val = av_clip(val, 0, (1 << cbps) - 1); \
1941 *dst = val << (precision - cbps); \
1946 for (; x < w; x++) { \
1947 int val = *i_datap + (1 << (cbps - 1)); \
1948 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1949 val = av_clip(val, 0, (1 << cbps) - 1); \
1950 *dst = val << (precision - cbps); \
1955 line += picture->linesize[plane] / sizeof(PIXEL); \
1961 WRITE_FRAME(8, uint8_t)
1962 WRITE_FRAME(16, uint16_t)
1966 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1967 int jobnr, int threadnr)
1969 Jpeg2000DecoderContext *s = avctx->priv_data;
1970 AVFrame *picture = td;
1971 Jpeg2000Tile *tile = s->tile + jobnr;
1974 tile_codeblocks(s, tile);
1976 /* inverse MCT transformation */
1977 if (tile->codsty[0].mct)
1978 mct_decode(s, tile);
1980 for (x = 0; x < s->ncomponents; x++) {
1981 if (s->cdef[x] < 0) {
1982 for (x = 0; x < s->ncomponents; x++) {
1985 if ((s->ncomponents & 1) == 0)
1986 s->cdef[s->ncomponents-1] = 0;
1991 if (s->precision <= 8) {
1992 write_frame_8(s, tile, picture, 8);
1994 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1995 picture->format == AV_PIX_FMT_RGB48 ||
1996 picture->format == AV_PIX_FMT_RGBA64 ||
1997 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1999 write_frame_16(s, tile, picture, precision);
2005 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2008 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2009 if (s->tile[tileno].comp) {
2010 for (compno = 0; compno < s->ncomponents; compno++) {
2011 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2012 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2014 ff_jpeg2000_cleanup(comp, codsty);
2016 av_freep(&s->tile[tileno].comp);
2017 av_freep(&s->tile[tileno].packed_headers);
2018 s->tile[tileno].packed_headers_size = 0;
2022 memset(s->codsty, 0, sizeof(s->codsty));
2023 memset(s->qntsty, 0, sizeof(s->qntsty));
2024 memset(s->properties, 0, sizeof(s->properties));
2025 memset(&s->poc , 0, sizeof(s->poc));
2026 s->numXtiles = s->numYtiles = 0;
2030 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2032 Jpeg2000CodingStyle *codsty = s->codsty;
2033 Jpeg2000QuantStyle *qntsty = s->qntsty;
2034 Jpeg2000POC *poc = &s->poc;
2035 uint8_t *properties = s->properties;
2042 if (bytestream2_get_bytes_left(&s->g) < 2) {
2043 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2047 marker = bytestream2_get_be16u(&s->g);
2048 oldpos = bytestream2_tell(&s->g);
2050 if (marker == JPEG2000_SOD) {
2052 Jpeg2000TilePart *tp;
2055 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2056 return AVERROR_INVALIDDATA;
2058 if (s->curtileno < 0) {
2059 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2060 return AVERROR_INVALIDDATA;
2063 tile = s->tile + s->curtileno;
2064 tp = tile->tile_part + tile->tp_idx;
2065 if (tp->tp_end < s->g.buffer) {
2066 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2067 return AVERROR_INVALIDDATA;
2070 if (tile->has_ppt && tile->tp_idx == 0) {
2071 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2074 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2075 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2079 if (marker == JPEG2000_EOC)
2082 len = bytestream2_get_be16(&s->g);
2083 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2084 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2085 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2086 return AVERROR_INVALIDDATA;
2088 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2094 if (s->ncomponents) {
2095 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2096 return AVERROR_INVALIDDATA;
2100 s->numXtiles = s->numYtiles = 0;
2103 ret = get_coc(s, codsty, properties);
2106 ret = get_cod(s, codsty, properties);
2109 ret = get_rgn(s, len);
2112 ret = get_qcc(s, len, qntsty, properties);
2115 ret = get_qcd(s, len, qntsty, properties);
2118 ret = get_poc(s, len, poc);
2121 if (!(ret = get_sot(s, len))) {
2122 av_assert1(s->curtileno >= 0);
2123 codsty = s->tile[s->curtileno].codsty;
2124 qntsty = s->tile[s->curtileno].qntsty;
2125 poc = &s->tile[s->curtileno].poc;
2126 properties = s->tile[s->curtileno].properties;
2130 // the PLM marker is ignored
2132 // the comment is ignored
2133 bytestream2_skip(&s->g, len - 2);
2136 ret = read_crg(s, len);
2139 // Tile-part lengths
2140 ret = get_tlm(s, len);
2143 // Packet length, tile-part header
2144 ret = get_plt(s, len);
2147 // Packed headers, tile-part header
2148 ret = get_ppt(s, len);
2151 av_log(s->avctx, AV_LOG_ERROR,
2152 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2153 marker, bytestream2_tell(&s->g) - 4);
2154 bytestream2_skip(&s->g, len - 2);
2157 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2158 av_log(s->avctx, AV_LOG_ERROR,
2159 "error during processing marker segment %.4"PRIx16"\n",
2161 return ret ? ret : -1;
2167 /* Read bit stream packets --> T2 operation. */
2168 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2173 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2174 Jpeg2000Tile *tile = s->tile + tileno;
2176 if ((ret = init_tile(s, tileno)) < 0)
2179 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2186 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2188 uint32_t atom_size, atom, atom_end;
2189 int search_range = 10;
2193 bytestream2_get_bytes_left(&s->g) >= 8) {
2194 atom_size = bytestream2_get_be32u(&s->g);
2195 atom = bytestream2_get_be32u(&s->g);
2196 if (atom_size == 1) {
2197 if (bytestream2_get_be32u(&s->g)) {
2198 avpriv_request_sample(s->avctx, "Huge atom");
2201 atom_size = bytestream2_get_be32u(&s->g);
2202 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2204 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2207 if (atom == JP2_CODESTREAM)
2210 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2213 if (atom == JP2_HEADER &&
2215 uint32_t atom2_size, atom2, atom2_end;
2217 atom2_size = bytestream2_get_be32u(&s->g);
2218 atom2 = bytestream2_get_be32u(&s->g);
2219 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2220 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2223 if (atom2 == JP2_CODESTREAM) {
2225 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2226 int method = bytestream2_get_byteu(&s->g);
2227 bytestream2_skipu(&s->g, 2);
2229 s->colour_space = bytestream2_get_be32u(&s->g);
2231 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2232 int i, size, colour_count, colour_channels, colour_depth[3];
2233 colour_count = bytestream2_get_be16u(&s->g);
2234 colour_channels = bytestream2_get_byteu(&s->g);
2235 // FIXME: Do not ignore channel_sign
2236 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2237 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2238 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2239 size = (colour_depth[0] + 7 >> 3) * colour_count +
2240 (colour_depth[1] + 7 >> 3) * colour_count +
2241 (colour_depth[2] + 7 >> 3) * colour_count;
2242 if (colour_count > AVPALETTE_COUNT ||
2243 colour_channels != 3 ||
2244 colour_depth[0] > 16 ||
2245 colour_depth[1] > 16 ||
2246 colour_depth[2] > 16 ||
2247 atom2_size < size) {
2248 avpriv_request_sample(s->avctx, "Unknown palette");
2249 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2253 for (i = 0; i < colour_count; i++) {
2255 if (colour_depth[0] <= 8) {
2256 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2257 r |= r >> colour_depth[0];
2259 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2261 if (colour_depth[1] <= 8) {
2262 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2263 g |= g >> colour_depth[1];
2265 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2267 if (colour_depth[2] <= 8) {
2268 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2269 b |= b >> colour_depth[2];
2271 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2273 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2275 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2276 int n = bytestream2_get_be16u(&s->g);
2278 int cn = bytestream2_get_be16(&s->g);
2279 int av_unused typ = bytestream2_get_be16(&s->g);
2280 int asoc = bytestream2_get_be16(&s->g);
2281 if (cn < 4 && asoc < 4)
2284 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2285 int64_t vnum, vden, hnum, hden, vexp, hexp;
2287 bytestream2_skip(&s->g, 4);
2288 resx = bytestream2_get_be32u(&s->g);
2289 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2290 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2293 vnum = bytestream2_get_be16u(&s->g);
2294 vden = bytestream2_get_be16u(&s->g);
2295 hnum = bytestream2_get_be16u(&s->g);
2296 hden = bytestream2_get_be16u(&s->g);
2297 vexp = bytestream2_get_byteu(&s->g);
2298 hexp = bytestream2_get_byteu(&s->g);
2299 if (!vnum || !vden || !hnum || !hden) {
2300 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2301 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2311 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2312 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2313 av_reduce(&s->sar.den, &s->sar.num,
2314 hnum * vden * pow(10, hexp),
2315 vnum * hden * pow(10, vexp),
2318 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2319 } while (atom_end - atom2_end >= 8);
2323 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2329 static av_cold void jpeg2000_init_static_data(void)
2331 ff_jpeg2000_init_tier1_luts();
2332 ff_mqc_init_context_tables();
2335 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2337 static AVOnce init_static_once = AV_ONCE_INIT;
2338 Jpeg2000DecoderContext *s = avctx->priv_data;
2340 ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2341 ff_jpeg2000dsp_init(&s->dsp);
2346 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2347 int *got_frame, AVPacket *avpkt)
2349 Jpeg2000DecoderContext *s = avctx->priv_data;
2350 ThreadFrame frame = { .f = data };
2351 AVFrame *picture = data;
2355 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2357 memset(s->cdef, -1, sizeof(s->cdef));
2359 if (bytestream2_get_bytes_left(&s->g) < 2) {
2360 ret = AVERROR_INVALIDDATA;
2364 // check if the image is in jp2 format
2365 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2366 (bytestream2_get_be32u(&s->g) == 12) &&
2367 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2368 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2369 if (!jp2_find_codestream(s)) {
2370 av_log(avctx, AV_LOG_ERROR,
2371 "Could not find Jpeg2000 codestream atom.\n");
2372 ret = AVERROR_INVALIDDATA;
2376 bytestream2_seek(&s->g, 0, SEEK_SET);
2379 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2380 bytestream2_skip(&s->g, 1);
2382 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2383 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2384 ret = AVERROR_INVALIDDATA;
2387 if (ret = jpeg2000_read_main_headers(s))
2390 /* get picture buffer */
2391 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2393 picture->pict_type = AV_PICTURE_TYPE_I;
2394 picture->key_frame = 1;
2396 if (ret = jpeg2000_read_bitstream_packets(s))
2399 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2401 jpeg2000_dec_cleanup(s);
2405 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2406 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2407 if (s->sar.num && s->sar.den)
2408 avctx->sample_aspect_ratio = s->sar;
2409 s->sar.num = s->sar.den = 0;
2411 return bytestream2_tell(&s->g);
2414 jpeg2000_dec_cleanup(s);
2418 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2419 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2421 static const AVOption options[] = {
2422 { "lowres", "Lower the decoding resolution by a power of two",
2423 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2427 static const AVClass jpeg2000_class = {
2428 .class_name = "jpeg2000",
2429 .item_name = av_default_item_name,
2431 .version = LIBAVUTIL_VERSION_INT,
2434 AVCodec ff_jpeg2000_decoder = {
2436 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2437 .type = AVMEDIA_TYPE_VIDEO,
2438 .id = AV_CODEC_ID_JPEG2000,
2439 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2440 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2441 .init = jpeg2000_decode_init,
2442 .decode = jpeg2000_decode_frame,
2443 .priv_class = &jpeg2000_class,
2445 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)