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;
272 int o_dimx, o_dimy; //original image dimensions.
275 if (bytestream2_get_bytes_left(&s->g) < 36) {
276 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
277 return AVERROR_INVALIDDATA;
280 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
281 s->width = bytestream2_get_be32u(&s->g); // Width
282 s->height = bytestream2_get_be32u(&s->g); // Height
283 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
284 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
285 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
286 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
287 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
288 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
289 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
291 if (s->image_offset_x || s->image_offset_y) {
292 avpriv_request_sample(s->avctx, "Support for image offsets");
293 return AVERROR_PATCHWELCOME;
295 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
296 avpriv_request_sample(s->avctx, "Large Dimensions");
297 return AVERROR_PATCHWELCOME;
300 if (ncomponents <= 0) {
301 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
303 return AVERROR_INVALIDDATA;
306 if (ncomponents > 4) {
307 avpriv_request_sample(s->avctx, "Support for %d components",
309 return AVERROR_PATCHWELCOME;
312 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
313 s->image_offset_x < s->tile_offset_x ||
314 s->image_offset_y < s->tile_offset_y ||
315 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
316 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
318 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
319 return AVERROR_INVALIDDATA;
322 s->ncomponents = ncomponents;
324 if (s->tile_width <= 0 || s->tile_height <= 0) {
325 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
326 s->tile_width, s->tile_height);
327 return AVERROR_INVALIDDATA;
330 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
331 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
332 return AVERROR_INVALIDDATA;
335 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
336 uint8_t x = bytestream2_get_byteu(&s->g);
337 s->cbps[i] = (x & 0x7f) + 1;
338 s->precision = FFMAX(s->cbps[i], s->precision);
339 s->sgnd[i] = !!(x & 0x80);
340 s->cdx[i] = bytestream2_get_byteu(&s->g);
341 s->cdy[i] = bytestream2_get_byteu(&s->g);
342 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
343 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
344 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
345 return AVERROR_INVALIDDATA;
347 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
350 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
351 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
353 // There must be at least a SOT and SOD per tile, their minimum size is 14
354 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
355 s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
357 s->numXtiles = s->numYtiles = 0;
358 return AVERROR(EINVAL);
361 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
363 s->numXtiles = s->numYtiles = 0;
364 return AVERROR(ENOMEM);
367 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
368 Jpeg2000Tile *tile = s->tile + i;
370 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
372 return AVERROR(ENOMEM);
375 /* compute image size with reduction factor */
376 o_dimx = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
377 s->reduction_factor);
378 o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
379 s->reduction_factor);
380 dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
381 dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
382 for (i = 1; i < s->ncomponents; i++) {
383 dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
384 dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
387 ret = ff_set_dimensions(s->avctx, dimx, dimy);
391 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
392 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
393 possible_fmts = xyz_pix_fmts;
394 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
396 switch (s->colour_space) {
398 possible_fmts = rgb_pix_fmts;
399 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
402 possible_fmts = gray_pix_fmts;
403 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
406 possible_fmts = yuv_pix_fmts;
407 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
410 possible_fmts = all_pix_fmts;
411 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
415 if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
416 && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
417 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
418 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
419 for (i = 0; i < possible_fmts_nb; ++i) {
420 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
421 s->avctx->pix_fmt = possible_fmts[i];
426 if (i == possible_fmts_nb) {
427 if (ncomponents == 4 &&
428 s->cdy[0] == 1 && s->cdx[0] == 1 &&
429 s->cdy[1] == 1 && s->cdx[1] == 1 &&
430 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
431 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
432 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
443 if (i == possible_fmts_nb) {
444 av_log(s->avctx, AV_LOG_ERROR,
445 "Unknown pix_fmt, profile: %d, colour_space: %d, "
446 "components: %d, precision: %d\n"
447 "cdx[0]: %d, cdy[0]: %d\n"
448 "cdx[1]: %d, cdy[1]: %d\n"
449 "cdx[2]: %d, cdy[2]: %d\n"
450 "cdx[3]: %d, cdy[3]: %d\n",
451 s->avctx->profile, s->colour_space, ncomponents, s->precision,
454 ncomponents > 1 ? s->cdx[1] : 0,
455 ncomponents > 1 ? s->cdy[1] : 0,
456 ncomponents > 2 ? s->cdx[2] : 0,
457 ncomponents > 2 ? s->cdy[2] : 0,
458 ncomponents > 3 ? s->cdx[3] : 0,
459 ncomponents > 3 ? s->cdy[3] : 0);
460 return AVERROR_PATCHWELCOME;
462 s->avctx->bits_per_raw_sample = s->precision;
466 /* get common part for COD and COC segments */
467 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
471 if (bytestream2_get_bytes_left(&s->g) < 5) {
472 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
473 return AVERROR_INVALIDDATA;
476 /* nreslevels = number of resolution levels
477 = number of decomposition level +1 */
478 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
479 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
480 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
481 return AVERROR_INVALIDDATA;
484 if (c->nreslevels <= s->reduction_factor) {
485 /* we are forced to update reduction_factor as its requested value is
486 not compatible with this bitstream, and as we might have used it
487 already in setup earlier we have to fail this frame until
488 reinitialization is implemented */
489 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
490 s->reduction_factor = c->nreslevels - 1;
491 return AVERROR(EINVAL);
494 /* compute number of resolution levels to decode */
495 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
497 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
498 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
500 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
501 c->log2_cblk_width + c->log2_cblk_height > 12) {
502 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
503 return AVERROR_INVALIDDATA;
506 c->cblk_style = bytestream2_get_byteu(&s->g);
507 if (c->cblk_style != 0) { // cblk style
508 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
509 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
510 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
512 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
513 /* set integer 9/7 DWT in case of BITEXACT flag */
514 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
515 c->transform = FF_DWT97_INT;
516 else if (c->transform == FF_DWT53) {
517 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
520 if (c->csty & JPEG2000_CSTY_PREC) {
522 for (i = 0; i < c->nreslevels; i++) {
523 byte = bytestream2_get_byte(&s->g);
524 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
525 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
527 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
528 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
529 c->log2_prec_widths[i], c->log2_prec_heights[i]);
530 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
531 return AVERROR_INVALIDDATA;
535 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
536 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
541 /* get coding parameters for a particular tile or whole image*/
542 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
545 Jpeg2000CodingStyle tmp;
548 if (bytestream2_get_bytes_left(&s->g) < 5) {
549 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
550 return AVERROR_INVALIDDATA;
553 tmp.csty = bytestream2_get_byteu(&s->g);
555 // get progression order
556 tmp.prog_order = bytestream2_get_byteu(&s->g);
558 tmp.nlayers = bytestream2_get_be16u(&s->g);
559 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
561 if (tmp.mct && s->ncomponents < 3) {
562 av_log(s->avctx, AV_LOG_ERROR,
563 "MCT %"PRIu8" with too few components (%d)\n",
564 tmp.mct, s->ncomponents);
565 return AVERROR_INVALIDDATA;
568 if ((ret = get_cox(s, &tmp)) < 0)
571 for (compno = 0; compno < s->ncomponents; compno++)
572 if (!(properties[compno] & HAD_COC))
573 memcpy(c + compno, &tmp, sizeof(tmp));
577 /* Get coding parameters for a component in the whole image or a
578 * particular tile. */
579 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
585 if (bytestream2_get_bytes_left(&s->g) < 2) {
586 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
587 return AVERROR_INVALIDDATA;
590 compno = bytestream2_get_byteu(&s->g);
592 if (compno >= s->ncomponents) {
593 av_log(s->avctx, AV_LOG_ERROR,
594 "Invalid compno %d. There are %d components in the image.\n",
595 compno, s->ncomponents);
596 return AVERROR_INVALIDDATA;
600 has_eph = c->csty & JPEG2000_CSTY_EPH;
601 c->csty = bytestream2_get_byteu(&s->g);
602 c->csty |= has_eph; //do not override eph present bits from COD
604 if ((ret = get_cox(s, c)) < 0)
607 properties[compno] |= HAD_COC;
611 static int get_rgn(Jpeg2000DecoderContext *s, int n)
614 compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
615 bytestream2_get_be16u(&s->g);
616 if (bytestream2_get_byte(&s->g)) {
617 av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
618 return AVERROR_INVALIDDATA; // SRgn field value is 0
621 // Currently compno cannot be greater than 4.
622 // However, future implementation should support compno up to 65536
623 if (compno < s->ncomponents) {
625 if (s->curtileno == -1) {
626 v = bytestream2_get_byte(&s->g);
628 return AVERROR_PATCHWELCOME;
629 s->roi_shift[compno] = v;
631 if (s->tile[s->curtileno].tp_idx != 0)
632 return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
633 v = bytestream2_get_byte(&s->g);
635 return AVERROR_PATCHWELCOME;
636 s->tile[s->curtileno].comp[compno].roi_shift = v;
640 return AVERROR_INVALIDDATA;
643 /* Get common part for QCD and QCC segments. */
644 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
648 if (bytestream2_get_bytes_left(&s->g) < 1)
649 return AVERROR_INVALIDDATA;
651 x = bytestream2_get_byteu(&s->g); // Sqcd
653 q->nguardbits = x >> 5;
654 q->quantsty = x & 0x1f;
656 if (q->quantsty == JPEG2000_QSTY_NONE) {
658 if (bytestream2_get_bytes_left(&s->g) < n ||
659 n > JPEG2000_MAX_DECLEVELS*3)
660 return AVERROR_INVALIDDATA;
661 for (i = 0; i < n; i++)
662 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
663 } else if (q->quantsty == JPEG2000_QSTY_SI) {
664 if (bytestream2_get_bytes_left(&s->g) < 2)
665 return AVERROR_INVALIDDATA;
666 x = bytestream2_get_be16u(&s->g);
667 q->expn[0] = x >> 11;
668 q->mant[0] = x & 0x7ff;
669 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
670 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
671 q->expn[i] = curexpn;
672 q->mant[i] = q->mant[0];
676 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
677 n > JPEG2000_MAX_DECLEVELS*3)
678 return AVERROR_INVALIDDATA;
679 for (i = 0; i < n; i++) {
680 x = bytestream2_get_be16u(&s->g);
681 q->expn[i] = x >> 11;
682 q->mant[i] = x & 0x7ff;
688 /* Get quantization parameters for a particular tile or a whole image. */
689 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
692 Jpeg2000QuantStyle tmp;
695 memset(&tmp, 0, sizeof(tmp));
697 if ((ret = get_qcx(s, n, &tmp)) < 0)
699 for (compno = 0; compno < s->ncomponents; compno++)
700 if (!(properties[compno] & HAD_QCC))
701 memcpy(q + compno, &tmp, sizeof(tmp));
705 /* Get quantization parameters for a component in the whole image
706 * on in a particular tile. */
707 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
712 if (bytestream2_get_bytes_left(&s->g) < 1)
713 return AVERROR_INVALIDDATA;
715 compno = bytestream2_get_byteu(&s->g);
717 if (compno >= s->ncomponents) {
718 av_log(s->avctx, AV_LOG_ERROR,
719 "Invalid compno %d. There are %d components in the image.\n",
720 compno, s->ncomponents);
721 return AVERROR_INVALIDDATA;
724 properties[compno] |= HAD_QCC;
725 return get_qcx(s, n - 1, q + compno);
728 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
731 int elem_size = s->ncomponents <= 257 ? 7 : 9;
732 Jpeg2000POC tmp = {{{0}}};
734 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
735 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
736 return AVERROR_INVALIDDATA;
740 avpriv_request_sample(s->avctx, "Fat POC not supported");
741 return AVERROR_PATCHWELCOME;
744 tmp.nb_poc = (size - 2) / elem_size;
745 if (tmp.nb_poc > MAX_POCS) {
746 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
747 return AVERROR_PATCHWELCOME;
750 for (i = 0; i<tmp.nb_poc; i++) {
751 Jpeg2000POCEntry *e = &tmp.poc[i];
752 e->RSpoc = bytestream2_get_byteu(&s->g);
753 e->CSpoc = bytestream2_get_byteu(&s->g);
754 e->LYEpoc = bytestream2_get_be16u(&s->g);
755 e->REpoc = bytestream2_get_byteu(&s->g);
756 e->CEpoc = bytestream2_get_byteu(&s->g);
757 e->Ppoc = bytestream2_get_byteu(&s->g);
760 if (e->CEpoc > s->ncomponents)
761 e->CEpoc = s->ncomponents;
762 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
763 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
765 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
766 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
768 return AVERROR_INVALIDDATA;
772 if (!p->nb_poc || p->is_default) {
775 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
776 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
777 return AVERROR_INVALIDDATA;
779 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
780 p->nb_poc += tmp.nb_poc;
789 /* Get start of tile segment. */
790 static int get_sot(Jpeg2000DecoderContext *s, int n)
792 Jpeg2000TilePart *tp;
797 if (bytestream2_get_bytes_left(&s->g) < 8)
798 return AVERROR_INVALIDDATA;
801 Isot = bytestream2_get_be16u(&s->g); // Isot
802 if (Isot >= s->numXtiles * s->numYtiles)
803 return AVERROR_INVALIDDATA;
806 Psot = bytestream2_get_be32u(&s->g); // Psot
807 TPsot = bytestream2_get_byteu(&s->g); // TPsot
809 /* Read TNSot but not used */
810 bytestream2_get_byteu(&s->g); // TNsot
813 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
815 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
816 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
817 return AVERROR_INVALIDDATA;
820 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
821 avpriv_request_sample(s->avctx, "Too many tile parts");
822 return AVERROR_PATCHWELCOME;
825 s->tile[Isot].tp_idx = TPsot;
826 tp = s->tile[Isot].tile_part + TPsot;
827 tp->tile_index = Isot;
828 tp->tp_end = s->g.buffer + Psot - n - 2;
831 Jpeg2000Tile *tile = s->tile + s->curtileno;
834 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
835 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
836 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
837 tile->poc.is_default = 1;
843 static int read_crg(Jpeg2000DecoderContext *s, int n)
845 if (s->ncomponents*4 != n - 2) {
846 av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
847 return AVERROR_INVALIDDATA;
849 bytestream2_skip(&s->g, n - 2);
852 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
853 * Used to know the number of tile parts and lengths.
854 * There may be multiple TLMs in the header.
855 * TODO: The function is not used for tile-parts management, nor anywhere else.
856 * It can be useful to allocate memory for tile parts, before managing the SOT
857 * markers. Parsing the TLM header is needed to increment the input header
859 * This marker is mandatory for DCI. */
860 static int get_tlm(Jpeg2000DecoderContext *s, int n)
862 uint8_t Stlm, ST, SP, tile_tlm, i;
863 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
864 Stlm = bytestream2_get_byte(&s->g);
866 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
867 ST = (Stlm >> 4) & 0x03;
869 av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
870 return AVERROR_INVALIDDATA;
873 SP = (Stlm >> 6) & 0x01;
874 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
875 for (i = 0; i < tile_tlm; i++) {
880 bytestream2_get_byte(&s->g);
883 bytestream2_get_be16(&s->g);
886 bytestream2_get_be32(&s->g);
890 bytestream2_get_be16(&s->g);
892 bytestream2_get_be32(&s->g);
898 static int get_plt(Jpeg2000DecoderContext *s, int n)
903 av_log(s->avctx, AV_LOG_DEBUG,
904 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
907 return AVERROR_INVALIDDATA;
909 /*Zplt =*/ bytestream2_get_byte(&s->g);
911 for (i = 0; i < n - 3; i++) {
912 v = bytestream2_get_byte(&s->g);
915 return AVERROR_INVALIDDATA;
920 static int get_ppt(Jpeg2000DecoderContext *s, int n)
926 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
927 return AVERROR_INVALIDDATA;
929 if (s->curtileno < 0)
930 return AVERROR_INVALIDDATA;
932 tile = &s->tile[s->curtileno];
933 if (tile->tp_idx != 0) {
934 av_log(s->avctx, AV_LOG_ERROR,
935 "PPT marker can occur only on first tile part of a tile.\n");
936 return AVERROR_INVALIDDATA;
939 tile->has_ppt = 1; // this tile has a ppt marker
940 bytestream2_get_byte(&s->g); // Zppt is skipped and not used
941 new = av_realloc(tile->packed_headers,
942 tile->packed_headers_size + n - 3);
944 tile->packed_headers = new;
946 return AVERROR(ENOMEM);
947 memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
948 memcpy(tile->packed_headers + tile->packed_headers_size,
950 tile->packed_headers_size += n - 3;
951 bytestream2_skip(&s->g, n - 3);
956 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
959 int tilex = tileno % s->numXtiles;
960 int tiley = tileno / s->numXtiles;
961 Jpeg2000Tile *tile = s->tile + tileno;
964 return AVERROR(ENOMEM);
966 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
967 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
968 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
969 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
971 for (compno = 0; compno < s->ncomponents; compno++) {
972 Jpeg2000Component *comp = tile->comp + compno;
973 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
974 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
975 int ret; // global bandno
977 comp->coord_o[0][0] = tile->coord[0][0];
978 comp->coord_o[0][1] = tile->coord[0][1];
979 comp->coord_o[1][0] = tile->coord[1][0];
980 comp->coord_o[1][1] = tile->coord[1][1];
982 comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
983 comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
984 comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
985 comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
987 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
988 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
989 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
990 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
992 if (!comp->roi_shift)
993 comp->roi_shift = s->roi_shift[compno];
995 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
996 s->cbps[compno], s->cdx[compno],
997 s->cdy[compno], s->avctx))
1003 /* Read the number of coding passes. */
1004 static int getnpasses(Jpeg2000DecoderContext *s)
1007 if (!get_bits(s, 1))
1009 if (!get_bits(s, 1))
1011 if ((num = get_bits(s, 2)) != 3)
1012 return num < 0 ? num : 3 + num;
1013 if ((num = get_bits(s, 5)) != 31)
1014 return num < 0 ? num : 6 + num;
1015 num = get_bits(s, 7);
1016 return num < 0 ? num : 37 + num;
1019 static int getlblockinc(Jpeg2000DecoderContext *s)
1022 while (ret = get_bits(s, 1)) {
1030 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1033 s->g = tile->tile_part[*tp_index].tpg;
1034 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1035 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1036 s->g = tile->tile_part[++(*tp_index)].tpg;
1039 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1040 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1043 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1044 Jpeg2000CodingStyle *codsty,
1045 Jpeg2000ResLevel *rlevel, int precno,
1046 int layno, uint8_t *expn, int numgbits)
1048 int bandno, cblkno, ret, nb_code_blocks;
1051 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1053 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1054 // Select stream to read from
1056 s->g = tile->packed_headers_stream;
1058 select_stream(s, tile, tp_index);
1060 if (!(ret = get_bits(s, 1))) {
1066 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1067 Jpeg2000Band *band = rlevel->band + bandno;
1068 Jpeg2000Prec *prec = band->prec + precno;
1070 if (band->coord[0][0] == band->coord[0][1] ||
1071 band->coord[1][0] == band->coord[1][1])
1073 nb_code_blocks = prec->nb_codeblocks_height *
1074 prec->nb_codeblocks_width;
1075 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1076 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1077 int incl, newpasses, llen;
1081 incl = get_bits(s, 1);
1083 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1089 if (!cblk->npasses) {
1090 int v = expn[bandno] + numgbits - 1 -
1091 tag_tree_decode(s, prec->zerobits + cblkno, 100);
1092 if (v < 0 || v > 30) {
1093 av_log(s->avctx, AV_LOG_ERROR,
1094 "nonzerobits %d invalid or unsupported\n", v);
1095 return AVERROR_INVALIDDATA;
1097 cblk->nonzerobits = v;
1099 if ((newpasses = getnpasses(s)) < 0)
1101 av_assert2(newpasses > 0);
1102 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1103 avpriv_request_sample(s->avctx, "Too many passes");
1104 return AVERROR_PATCHWELCOME;
1106 if ((llen = getlblockinc(s)) < 0)
1108 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1109 avpriv_request_sample(s->avctx,
1110 "Block with length beyond 16 bits");
1111 return AVERROR_PATCHWELCOME;
1114 cblk->lblock += llen;
1116 cblk->nb_lengthinc = 0;
1117 cblk->nb_terminationsinc = 0;
1118 av_free(cblk->lengthinc);
1119 cblk->lengthinc = av_mallocz_array(newpasses , sizeof(*cblk->lengthinc));
1120 if (!cblk->lengthinc)
1121 return AVERROR(ENOMEM);
1122 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1124 return AVERROR(ENOMEM);
1125 cblk->data_start = tmp;
1129 while (newpasses1 < newpasses) {
1131 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1132 cblk->nb_terminationsinc ++;
1137 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1139 if (ret > cblk->data_allocated) {
1140 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1141 void *new = av_realloc(cblk->data, new_size);
1144 cblk->data_allocated = new_size;
1147 if (ret > cblk->data_allocated) {
1148 avpriv_request_sample(s->avctx,
1149 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1150 cblk->data_allocated);
1151 return AVERROR_PATCHWELCOME;
1153 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1154 cblk->npasses += newpasses1;
1155 newpasses -= newpasses1;
1161 if (codsty->csty & JPEG2000_CSTY_EPH) {
1162 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1163 bytestream2_skip(&s->g, 2);
1165 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1168 // Save state of stream
1169 if (tile->has_ppt) {
1170 tile->packed_headers_stream = s->g;
1171 select_stream(s, tile, tp_index);
1173 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1174 Jpeg2000Band *band = rlevel->band + bandno;
1175 Jpeg2000Prec *prec = band->prec + precno;
1177 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1178 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1179 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1180 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1182 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1183 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1184 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1185 void *new = av_realloc(cblk->data, new_size);
1188 cblk->data_allocated = new_size;
1191 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1192 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1194 av_log(s->avctx, AV_LOG_ERROR,
1195 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1196 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1197 return AVERROR_INVALIDDATA;
1200 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1201 cblk->length += cblk->lengthinc[cwsno];
1202 cblk->lengthinc[cwsno] = 0;
1203 if (cblk->nb_terminationsinc) {
1204 cblk->nb_terminationsinc--;
1205 cblk->nb_terminations++;
1206 cblk->data[cblk->length++] = 0xFF;
1207 cblk->data[cblk->length++] = 0xFF;
1208 cblk->data_start[cblk->nb_terminations] = cblk->length;
1211 av_freep(&cblk->lengthinc);
1214 // Save state of stream
1215 tile->tile_part[*tp_index].tpg = s->g;
1220 tile->packed_headers_stream = s->g;
1222 tile->tile_part[*tp_index].tpg = s->g;
1226 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1227 int RSpoc, int CSpoc,
1228 int LYEpoc, int REpoc, int CEpoc,
1229 int Ppoc, int *tp_index)
1232 int layno, reslevelno, compno, precno, ok_reslevel;
1237 case JPEG2000_PGOD_RLCP:
1238 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1240 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1242 for (layno = 0; layno < LYEpoc; layno++) {
1243 for (compno = CSpoc; compno < CEpoc; compno++) {
1244 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1245 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1246 if (reslevelno < codsty->nreslevels) {
1247 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1250 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1251 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1254 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1255 qntsty->nguardbits)) < 0)
1263 case JPEG2000_PGOD_LRCP:
1264 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1265 for (layno = 0; layno < LYEpoc; layno++) {
1267 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1269 for (compno = CSpoc; compno < CEpoc; compno++) {
1270 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1271 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1272 if (reslevelno < codsty->nreslevels) {
1273 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1276 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1277 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1280 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1281 qntsty->nguardbits)) < 0)
1289 case JPEG2000_PGOD_CPRL:
1290 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1291 for (compno = CSpoc; compno < CEpoc; compno++) {
1292 Jpeg2000Component *comp = tile->comp + compno;
1293 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1294 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1298 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1301 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1302 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1303 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1304 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1305 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1307 if (step_x >= 31 || step_y >= 31){
1308 avpriv_request_sample(s->avctx, "CPRL with large step");
1309 return AVERROR_PATCHWELCOME;
1314 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1315 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1316 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1317 unsigned prcx, prcy;
1318 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1319 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1320 int xc = x / s->cdx[compno];
1321 int yc = y / s->cdy[compno];
1323 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1326 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1329 // check if a precinct exists
1330 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1331 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1332 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1333 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1335 precno = prcx + rlevel->num_precincts_x * prcy;
1337 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1338 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1339 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1343 for (layno = 0; layno < LYEpoc; layno++) {
1344 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1346 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1347 qntsty->nguardbits)) < 0)
1356 case JPEG2000_PGOD_RPCL:
1357 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1359 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1363 for (compno = CSpoc; compno < CEpoc; compno++) {
1364 Jpeg2000Component *comp = tile->comp + compno;
1365 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1367 if (reslevelno < codsty->nreslevels) {
1368 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1369 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1370 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1371 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1377 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1378 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1379 for (compno = CSpoc; compno < CEpoc; compno++) {
1380 Jpeg2000Component *comp = tile->comp + compno;
1381 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1382 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1383 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1384 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1385 unsigned prcx, prcy;
1387 int xc = x / s->cdx[compno];
1388 int yc = y / s->cdy[compno];
1390 if (reslevelno >= codsty->nreslevels)
1393 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1396 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1399 // check if a precinct exists
1400 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1401 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1402 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1403 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1405 precno = prcx + rlevel->num_precincts_x * prcy;
1408 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1409 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1410 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1414 for (layno = 0; layno < LYEpoc; layno++) {
1415 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1418 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1419 qntsty->nguardbits)) < 0)
1428 case JPEG2000_PGOD_PCRL:
1429 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1432 for (compno = CSpoc; compno < CEpoc; compno++) {
1433 Jpeg2000Component *comp = tile->comp + compno;
1434 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1436 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1437 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1438 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1439 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1440 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1443 if (step_x >= 31 || step_y >= 31){
1444 avpriv_request_sample(s->avctx, "PCRL with large step");
1445 return AVERROR_PATCHWELCOME;
1450 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1451 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1452 for (compno = CSpoc; compno < CEpoc; compno++) {
1453 Jpeg2000Component *comp = tile->comp + compno;
1454 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1455 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1456 int xc = x / s->cdx[compno];
1457 int yc = y / s->cdy[compno];
1459 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1460 unsigned prcx, prcy;
1461 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1462 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1464 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1467 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1470 // check if a precinct exists
1471 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1472 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1473 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1474 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1476 precno = prcx + rlevel->num_precincts_x * prcy;
1478 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1479 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1480 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1484 for (layno = 0; layno < LYEpoc; layno++) {
1485 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1487 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1488 qntsty->nguardbits)) < 0)
1504 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1506 int ret = AVERROR_BUG;
1511 if (tile->poc.nb_poc) {
1512 for (i=0; i<tile->poc.nb_poc; i++) {
1513 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1514 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1516 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1518 FFMIN(e->CEpoc, s->ncomponents),
1525 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1527 tile->codsty[0].nlayers,
1530 tile->codsty[0].prog_order,
1534 /* EOC marker reached */
1535 bytestream2_skip(&s->g, 2);
1540 /* TIER-1 routines */
1541 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1542 int bpno, int bandno,
1543 int vert_causal_ctx_csty_symbol)
1545 int mask = 3 << (bpno - 1), y0, x, y;
1547 for (y0 = 0; y0 < height; y0 += 4)
1548 for (x = 0; x < width; x++)
1549 for (y = y0; y < height && y < y0 + 4; y++) {
1550 int flags_mask = -1;
1551 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1552 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1553 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1554 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1555 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1556 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1558 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1560 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1563 ff_jpeg2000_set_significance(t1, x, y,
1564 t1->data[(y) * t1->stride + x] < 0);
1566 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1571 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1572 int bpno, int vert_causal_ctx_csty_symbol)
1577 phalf = 1 << (bpno - 1);
1580 for (y0 = 0; y0 < height; y0 += 4)
1581 for (x = 0; x < width; x++)
1582 for (y = y0; y < height && y < y0 + 4; y++)
1583 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1584 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1585 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1586 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1587 int r = ff_mqc_decode(&t1->mqc,
1588 t1->mqc.cx_states + ctxno)
1590 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1591 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1595 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1596 int width, int height, int bpno, int bandno,
1597 int seg_symbols, int vert_causal_ctx_csty_symbol)
1599 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1601 for (y0 = 0; y0 < height; y0 += 4) {
1602 for (x = 0; x < width; x++) {
1603 int flags_mask = -1;
1604 if (vert_causal_ctx_csty_symbol)
1605 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1606 if (y0 + 3 < height &&
1607 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1608 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1609 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1610 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1611 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1613 runlen = ff_mqc_decode(&t1->mqc,
1614 t1->mqc.cx_states + MQC_CX_UNI);
1615 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1624 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1625 int flags_mask = -1;
1626 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1627 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1629 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1630 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1636 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1638 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1639 t1->mqc.cx_states + ctxno) ^
1642 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1645 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1651 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1652 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1653 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1654 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1656 av_log(s->avctx, AV_LOG_ERROR,
1657 "Segmentation symbol value incorrect\n");
1661 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1662 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1663 int width, int height, int bandpos, uint8_t roi_shift)
1665 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1667 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1671 av_assert0(width <= 1024U && height <= 1024U);
1672 av_assert0(width*height <= 4096);
1674 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1676 /* If code-block contains no compressed data: nothing to do. */
1680 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1682 cblk->data[cblk->length] = 0xff;
1683 cblk->data[cblk->length+1] = 0xff;
1684 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1687 if (bpno < 0 || bpno > 29) {
1688 av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
1689 return AVERROR_INVALIDDATA;
1693 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1694 vert_causal_ctx_csty_symbol);
1697 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1700 av_assert2(!t1->mqc.raw);
1701 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1702 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1703 vert_causal_ctx_csty_symbol);
1706 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1707 ff_mqc_init_contexts(&t1->mqc);
1709 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1710 if (term_cnt >= cblk->nb_terminations) {
1711 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1712 return AVERROR_INVALIDDATA;
1714 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1715 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1716 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1717 pass_cnt, cblk->npasses);
1720 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1731 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1732 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1733 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1739 static inline int roi_shift_param(Jpeg2000Component *comp,
1744 roi_shift = comp->roi_shift;
1745 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1747 if (val > (1 << roi_shift))
1748 return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1749 return quan_parameter;
1752 /* TODO: Verify dequantization for lossless case
1753 * comp->data can be float or int
1754 * band->stepsize can be float or int
1755 * depending on the type of DWT transformation.
1756 * see ISO/IEC 15444-1:2002 A.6.1 */
1758 /* Float dequantization of a codeblock.*/
1759 static void dequantization_float(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 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1767 int *src = t1->data + j*t1->stride;
1768 for (i = 0; i < w; ++i)
1769 datap[i] = src[i] * band->f_stepsize;
1773 /* Integer dequantization of a codeblock.*/
1774 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1775 Jpeg2000Component *comp,
1776 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1779 int w = cblk->coord[0][1] - cblk->coord[0][0];
1780 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1781 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1782 int *src = t1->data + j*t1->stride;
1783 if (band->i_stepsize == 32768) {
1784 for (i = 0; i < w; ++i)
1785 datap[i] = src[i] / 2;
1787 // This should be VERY uncommon
1788 for (i = 0; i < w; ++i)
1789 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1794 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1795 Jpeg2000Component *comp,
1796 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1799 int w = cblk->coord[0][1] - cblk->coord[0][0];
1800 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1801 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1802 int *src = t1->data + j*t1->stride;
1803 for (i = 0; i < w; ++i)
1804 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1808 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1813 for (i = 1; i < 3; i++) {
1814 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1815 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1818 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1819 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1824 for (i = 0; i < 3; i++)
1825 if (tile->codsty[0].transform == FF_DWT97)
1826 src[i] = tile->comp[i].f_data;
1828 src[i] = tile->comp[i].i_data;
1830 for (i = 0; i < 2; i++)
1831 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1833 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1836 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1837 Jpeg2000Component *comp,
1838 Jpeg2000T1Context *t1)
1841 int w = cblk->coord[0][1] - cblk->coord[0][0];
1842 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1843 int *src = t1->data + j*t1->stride;
1844 for (i = 0; i < w; ++i)
1845 src[i] = roi_shift_param(comp, src[i]);
1849 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1851 Jpeg2000T1Context t1;
1853 int compno, reslevelno, bandno;
1855 /* Loop on tile components */
1856 for (compno = 0; compno < s->ncomponents; compno++) {
1857 Jpeg2000Component *comp = tile->comp + compno;
1858 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1861 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1863 /* Loop on resolution levels */
1864 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1865 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1867 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1868 int nb_precincts, precno;
1869 Jpeg2000Band *band = rlevel->band + bandno;
1870 int cblkno = 0, bandpos;
1872 bandpos = bandno + (reslevelno > 0);
1874 if (band->coord[0][0] == band->coord[0][1] ||
1875 band->coord[1][0] == band->coord[1][1])
1878 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1879 /* Loop on precincts */
1880 for (precno = 0; precno < nb_precincts; precno++) {
1881 Jpeg2000Prec *prec = band->prec + precno;
1883 /* Loop on codeblocks */
1885 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1888 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1889 int ret = decode_cblk(s, codsty, &t1, cblk,
1890 cblk->coord[0][1] - cblk->coord[0][0],
1891 cblk->coord[1][1] - cblk->coord[1][0],
1892 bandpos, comp->roi_shift);
1897 x = cblk->coord[0][0] - band->coord[0][0];
1898 y = cblk->coord[1][0] - band->coord[1][0];
1900 if (comp->roi_shift)
1901 roi_scale_cblk(cblk, comp, &t1);
1902 if (codsty->transform == FF_DWT97)
1903 dequantization_float(x, y, cblk, comp, &t1, band);
1904 else if (codsty->transform == FF_DWT97_INT)
1905 dequantization_int_97(x, y, cblk, comp, &t1, band);
1907 dequantization_int(x, y, cblk, comp, &t1, band);
1911 } /* end reslevel */
1915 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1920 #define WRITE_FRAME(D, PIXEL) \
1921 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1922 AVFrame * picture, int precision) \
1924 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1925 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1926 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1931 for (compno = 0; compno < s->ncomponents; compno++) { \
1932 Jpeg2000Component *comp = tile->comp + compno; \
1933 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1935 float *datap = comp->f_data; \
1936 int32_t *i_datap = comp->i_data; \
1937 int cbps = s->cbps[compno]; \
1938 int w = tile->comp[compno].coord[0][1] - \
1939 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1940 int h = tile->comp[compno].coord[1][1] - \
1941 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1945 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1947 y = tile->comp[compno].coord[1][0] - \
1948 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1949 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1950 for (; y < h; y++) { \
1953 x = tile->comp[compno].coord[0][0] - \
1954 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1955 dst = line + x * pixelsize + compno*!planar; \
1957 if (codsty->transform == FF_DWT97) { \
1958 for (; x < w; x++) { \
1959 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1960 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1961 val = av_clip(val, 0, (1 << cbps) - 1); \
1962 *dst = val << (precision - cbps); \
1967 for (; x < w; x++) { \
1968 int val = *i_datap + (1 << (cbps - 1)); \
1969 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1970 val = av_clip(val, 0, (1 << cbps) - 1); \
1971 *dst = val << (precision - cbps); \
1976 line += picture->linesize[plane] / sizeof(PIXEL); \
1982 WRITE_FRAME(8, uint8_t)
1983 WRITE_FRAME(16, uint16_t)
1987 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1988 int jobnr, int threadnr)
1990 Jpeg2000DecoderContext *s = avctx->priv_data;
1991 AVFrame *picture = td;
1992 Jpeg2000Tile *tile = s->tile + jobnr;
1995 tile_codeblocks(s, tile);
1997 /* inverse MCT transformation */
1998 if (tile->codsty[0].mct)
1999 mct_decode(s, tile);
2001 for (x = 0; x < s->ncomponents; x++) {
2002 if (s->cdef[x] < 0) {
2003 for (x = 0; x < s->ncomponents; x++) {
2006 if ((s->ncomponents & 1) == 0)
2007 s->cdef[s->ncomponents-1] = 0;
2012 if (s->precision <= 8) {
2013 write_frame_8(s, tile, picture, 8);
2015 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2016 picture->format == AV_PIX_FMT_RGB48 ||
2017 picture->format == AV_PIX_FMT_RGBA64 ||
2018 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2020 write_frame_16(s, tile, picture, precision);
2026 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2029 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2030 if (s->tile[tileno].comp) {
2031 for (compno = 0; compno < s->ncomponents; compno++) {
2032 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2033 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2035 ff_jpeg2000_cleanup(comp, codsty);
2037 av_freep(&s->tile[tileno].comp);
2038 av_freep(&s->tile[tileno].packed_headers);
2039 s->tile[tileno].packed_headers_size = 0;
2043 memset(s->codsty, 0, sizeof(s->codsty));
2044 memset(s->qntsty, 0, sizeof(s->qntsty));
2045 memset(s->properties, 0, sizeof(s->properties));
2046 memset(&s->poc , 0, sizeof(s->poc));
2047 s->numXtiles = s->numYtiles = 0;
2051 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2053 Jpeg2000CodingStyle *codsty = s->codsty;
2054 Jpeg2000QuantStyle *qntsty = s->qntsty;
2055 Jpeg2000POC *poc = &s->poc;
2056 uint8_t *properties = s->properties;
2063 if (bytestream2_get_bytes_left(&s->g) < 2) {
2064 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2068 marker = bytestream2_get_be16u(&s->g);
2069 oldpos = bytestream2_tell(&s->g);
2071 if (marker == JPEG2000_SOD) {
2073 Jpeg2000TilePart *tp;
2076 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2077 return AVERROR_INVALIDDATA;
2079 if (s->curtileno < 0) {
2080 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2081 return AVERROR_INVALIDDATA;
2084 tile = s->tile + s->curtileno;
2085 tp = tile->tile_part + tile->tp_idx;
2086 if (tp->tp_end < s->g.buffer) {
2087 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2088 return AVERROR_INVALIDDATA;
2091 if (tile->has_ppt && tile->tp_idx == 0) {
2092 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2095 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2096 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2100 if (marker == JPEG2000_EOC)
2103 len = bytestream2_get_be16(&s->g);
2104 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2105 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2106 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2107 return AVERROR_INVALIDDATA;
2109 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2115 if (s->ncomponents) {
2116 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2117 return AVERROR_INVALIDDATA;
2121 s->numXtiles = s->numYtiles = 0;
2124 ret = get_coc(s, codsty, properties);
2127 ret = get_cod(s, codsty, properties);
2130 ret = get_rgn(s, len);
2133 ret = get_qcc(s, len, qntsty, properties);
2136 ret = get_qcd(s, len, qntsty, properties);
2139 ret = get_poc(s, len, poc);
2142 if (!(ret = get_sot(s, len))) {
2143 av_assert1(s->curtileno >= 0);
2144 codsty = s->tile[s->curtileno].codsty;
2145 qntsty = s->tile[s->curtileno].qntsty;
2146 poc = &s->tile[s->curtileno].poc;
2147 properties = s->tile[s->curtileno].properties;
2151 // the PLM marker is ignored
2153 // the comment is ignored
2154 bytestream2_skip(&s->g, len - 2);
2157 ret = read_crg(s, len);
2160 // Tile-part lengths
2161 ret = get_tlm(s, len);
2164 // Packet length, tile-part header
2165 ret = get_plt(s, len);
2168 // Packed headers, tile-part header
2169 ret = get_ppt(s, len);
2172 av_log(s->avctx, AV_LOG_ERROR,
2173 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2174 marker, bytestream2_tell(&s->g) - 4);
2175 bytestream2_skip(&s->g, len - 2);
2178 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2179 av_log(s->avctx, AV_LOG_ERROR,
2180 "error during processing marker segment %.4"PRIx16"\n",
2182 return ret ? ret : -1;
2188 /* Read bit stream packets --> T2 operation. */
2189 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2194 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2195 Jpeg2000Tile *tile = s->tile + tileno;
2197 if ((ret = init_tile(s, tileno)) < 0)
2200 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2207 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2209 uint32_t atom_size, atom, atom_end;
2210 int search_range = 10;
2214 bytestream2_get_bytes_left(&s->g) >= 8) {
2215 atom_size = bytestream2_get_be32u(&s->g);
2216 atom = bytestream2_get_be32u(&s->g);
2217 if (atom_size == 1) {
2218 if (bytestream2_get_be32u(&s->g)) {
2219 avpriv_request_sample(s->avctx, "Huge atom");
2222 atom_size = bytestream2_get_be32u(&s->g);
2223 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2225 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2228 if (atom == JP2_CODESTREAM)
2231 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2234 if (atom == JP2_HEADER &&
2236 uint32_t atom2_size, atom2, atom2_end;
2238 atom2_size = bytestream2_get_be32u(&s->g);
2239 atom2 = bytestream2_get_be32u(&s->g);
2240 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2241 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2244 if (atom2 == JP2_CODESTREAM) {
2246 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2247 int method = bytestream2_get_byteu(&s->g);
2248 bytestream2_skipu(&s->g, 2);
2250 s->colour_space = bytestream2_get_be32u(&s->g);
2252 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2253 int i, size, colour_count, colour_channels, colour_depth[3];
2254 colour_count = bytestream2_get_be16u(&s->g);
2255 colour_channels = bytestream2_get_byteu(&s->g);
2256 // FIXME: Do not ignore channel_sign
2257 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2258 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2259 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2260 size = (colour_depth[0] + 7 >> 3) * colour_count +
2261 (colour_depth[1] + 7 >> 3) * colour_count +
2262 (colour_depth[2] + 7 >> 3) * colour_count;
2263 if (colour_count > AVPALETTE_COUNT ||
2264 colour_channels != 3 ||
2265 colour_depth[0] > 16 ||
2266 colour_depth[1] > 16 ||
2267 colour_depth[2] > 16 ||
2268 atom2_size < size) {
2269 avpriv_request_sample(s->avctx, "Unknown palette");
2270 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2274 for (i = 0; i < colour_count; i++) {
2276 if (colour_depth[0] <= 8) {
2277 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2278 r |= r >> colour_depth[0];
2280 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2282 if (colour_depth[1] <= 8) {
2283 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2284 g |= g >> colour_depth[1];
2286 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2288 if (colour_depth[2] <= 8) {
2289 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2290 b |= b >> colour_depth[2];
2292 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2294 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2296 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2297 int n = bytestream2_get_be16u(&s->g);
2299 int cn = bytestream2_get_be16(&s->g);
2300 int av_unused typ = bytestream2_get_be16(&s->g);
2301 int asoc = bytestream2_get_be16(&s->g);
2302 if (cn < 4 && asoc < 4)
2305 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2306 int64_t vnum, vden, hnum, hden, vexp, hexp;
2308 bytestream2_skip(&s->g, 4);
2309 resx = bytestream2_get_be32u(&s->g);
2310 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2311 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2314 vnum = bytestream2_get_be16u(&s->g);
2315 vden = bytestream2_get_be16u(&s->g);
2316 hnum = bytestream2_get_be16u(&s->g);
2317 hden = bytestream2_get_be16u(&s->g);
2318 vexp = bytestream2_get_byteu(&s->g);
2319 hexp = bytestream2_get_byteu(&s->g);
2320 if (!vnum || !vden || !hnum || !hden) {
2321 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2322 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2332 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2333 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2334 av_reduce(&s->sar.den, &s->sar.num,
2335 hnum * vden * pow(10, hexp),
2336 vnum * hden * pow(10, vexp),
2339 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2340 } while (atom_end - atom2_end >= 8);
2344 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2350 static av_cold void jpeg2000_init_static_data(void)
2352 ff_jpeg2000_init_tier1_luts();
2353 ff_mqc_init_context_tables();
2356 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2358 static AVOnce init_static_once = AV_ONCE_INIT;
2359 Jpeg2000DecoderContext *s = avctx->priv_data;
2361 ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2362 ff_jpeg2000dsp_init(&s->dsp);
2367 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2368 int *got_frame, AVPacket *avpkt)
2370 Jpeg2000DecoderContext *s = avctx->priv_data;
2371 ThreadFrame frame = { .f = data };
2372 AVFrame *picture = data;
2376 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2378 memset(s->cdef, -1, sizeof(s->cdef));
2380 if (bytestream2_get_bytes_left(&s->g) < 2) {
2381 ret = AVERROR_INVALIDDATA;
2385 // check if the image is in jp2 format
2386 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2387 (bytestream2_get_be32u(&s->g) == 12) &&
2388 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2389 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2390 if (!jp2_find_codestream(s)) {
2391 av_log(avctx, AV_LOG_ERROR,
2392 "Could not find Jpeg2000 codestream atom.\n");
2393 ret = AVERROR_INVALIDDATA;
2397 bytestream2_seek(&s->g, 0, SEEK_SET);
2400 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2401 bytestream2_skip(&s->g, 1);
2403 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2404 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2405 ret = AVERROR_INVALIDDATA;
2408 if (ret = jpeg2000_read_main_headers(s))
2411 /* get picture buffer */
2412 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2414 picture->pict_type = AV_PICTURE_TYPE_I;
2415 picture->key_frame = 1;
2417 if (ret = jpeg2000_read_bitstream_packets(s))
2420 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2422 jpeg2000_dec_cleanup(s);
2426 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2427 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2428 if (s->sar.num && s->sar.den)
2429 avctx->sample_aspect_ratio = s->sar;
2430 s->sar.num = s->sar.den = 0;
2432 return bytestream2_tell(&s->g);
2435 jpeg2000_dec_cleanup(s);
2439 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2440 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2442 static const AVOption options[] = {
2443 { "lowres", "Lower the decoding resolution by a power of two",
2444 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2448 static const AVClass jpeg2000_class = {
2449 .class_name = "jpeg2000",
2450 .item_name = av_default_item_name,
2452 .version = LIBAVUTIL_VERSION_INT,
2455 AVCodec ff_jpeg2000_decoder = {
2457 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2458 .type = AVMEDIA_TYPE_VIDEO,
2459 .id = AV_CODEC_ID_JPEG2000,
2460 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2461 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2462 .init = jpeg2000_decode_init,
2463 .decode = jpeg2000_decode_frame,
2464 .priv_class = &jpeg2000_class,
2466 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)