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 (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
292 avpriv_request_sample(s->avctx, "Large Dimensions");
293 return AVERROR_PATCHWELCOME;
296 if (ncomponents <= 0) {
297 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
299 return AVERROR_INVALIDDATA;
302 if (ncomponents > 4) {
303 avpriv_request_sample(s->avctx, "Support for %d components",
305 return AVERROR_PATCHWELCOME;
308 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
309 s->image_offset_x < s->tile_offset_x ||
310 s->image_offset_y < s->tile_offset_y ||
311 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
312 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
314 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
315 return AVERROR_INVALIDDATA;
318 s->ncomponents = ncomponents;
320 if (s->tile_width <= 0 || s->tile_height <= 0) {
321 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
322 s->tile_width, s->tile_height);
323 return AVERROR_INVALIDDATA;
326 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
327 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
328 return AVERROR_INVALIDDATA;
331 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
332 uint8_t x = bytestream2_get_byteu(&s->g);
333 s->cbps[i] = (x & 0x7f) + 1;
334 s->precision = FFMAX(s->cbps[i], s->precision);
335 s->sgnd[i] = !!(x & 0x80);
336 s->cdx[i] = bytestream2_get_byteu(&s->g);
337 s->cdy[i] = bytestream2_get_byteu(&s->g);
338 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
339 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
340 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
341 return AVERROR_INVALIDDATA;
343 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
346 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
347 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
349 // There must be at least a SOT and SOD per tile, their minimum size is 14
350 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
351 s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
353 s->numXtiles = s->numYtiles = 0;
354 return AVERROR(EINVAL);
357 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
359 s->numXtiles = s->numYtiles = 0;
360 return AVERROR(ENOMEM);
363 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
364 Jpeg2000Tile *tile = s->tile + i;
366 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
368 return AVERROR(ENOMEM);
371 /* compute image size with reduction factor */
372 o_dimx = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
373 s->reduction_factor);
374 o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
375 s->reduction_factor);
376 dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
377 dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
378 for (i = 1; i < s->ncomponents; i++) {
379 dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
380 dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
383 ret = ff_set_dimensions(s->avctx, dimx, dimy);
387 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
388 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
389 possible_fmts = xyz_pix_fmts;
390 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
392 switch (s->colour_space) {
394 possible_fmts = rgb_pix_fmts;
395 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
398 possible_fmts = gray_pix_fmts;
399 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
402 possible_fmts = yuv_pix_fmts;
403 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
406 possible_fmts = all_pix_fmts;
407 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
411 if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
412 && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
413 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
414 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
415 for (i = 0; i < possible_fmts_nb; ++i) {
416 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
417 s->avctx->pix_fmt = possible_fmts[i];
422 if (i == possible_fmts_nb) {
423 if (ncomponents == 4 &&
424 s->cdy[0] == 1 && s->cdx[0] == 1 &&
425 s->cdy[1] == 1 && s->cdx[1] == 1 &&
426 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
427 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
428 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
435 } else if (ncomponents == 3 && s->precision == 8 &&
436 s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
437 s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
438 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
440 } else if (ncomponents == 2 && s->precision == 8 &&
441 s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
442 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
444 } else if (ncomponents == 1 && s->precision == 8) {
445 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
451 if (i == possible_fmts_nb) {
452 av_log(s->avctx, AV_LOG_ERROR,
453 "Unknown pix_fmt, profile: %d, colour_space: %d, "
454 "components: %d, precision: %d\n"
455 "cdx[0]: %d, cdy[0]: %d\n"
456 "cdx[1]: %d, cdy[1]: %d\n"
457 "cdx[2]: %d, cdy[2]: %d\n"
458 "cdx[3]: %d, cdy[3]: %d\n",
459 s->avctx->profile, s->colour_space, ncomponents, s->precision,
462 ncomponents > 1 ? s->cdx[1] : 0,
463 ncomponents > 1 ? s->cdy[1] : 0,
464 ncomponents > 2 ? s->cdx[2] : 0,
465 ncomponents > 2 ? s->cdy[2] : 0,
466 ncomponents > 3 ? s->cdx[3] : 0,
467 ncomponents > 3 ? s->cdy[3] : 0);
468 return AVERROR_PATCHWELCOME;
470 s->avctx->bits_per_raw_sample = s->precision;
474 /* get common part for COD and COC segments */
475 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
479 if (bytestream2_get_bytes_left(&s->g) < 5) {
480 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
481 return AVERROR_INVALIDDATA;
484 /* nreslevels = number of resolution levels
485 = number of decomposition level +1 */
486 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
487 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
488 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
489 return AVERROR_INVALIDDATA;
492 if (c->nreslevels <= s->reduction_factor) {
493 /* we are forced to update reduction_factor as its requested value is
494 not compatible with this bitstream, and as we might have used it
495 already in setup earlier we have to fail this frame until
496 reinitialization is implemented */
497 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
498 s->reduction_factor = c->nreslevels - 1;
499 return AVERROR(EINVAL);
502 /* compute number of resolution levels to decode */
503 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
505 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
506 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
508 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
509 c->log2_cblk_width + c->log2_cblk_height > 12) {
510 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
511 return AVERROR_INVALIDDATA;
514 c->cblk_style = bytestream2_get_byteu(&s->g);
515 if (c->cblk_style != 0) { // cblk style
516 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
517 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
518 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
520 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
521 /* set integer 9/7 DWT in case of BITEXACT flag */
522 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
523 c->transform = FF_DWT97_INT;
524 else if (c->transform == FF_DWT53) {
525 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
528 if (c->csty & JPEG2000_CSTY_PREC) {
530 for (i = 0; i < c->nreslevels; i++) {
531 byte = bytestream2_get_byte(&s->g);
532 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
533 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
535 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
536 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
537 c->log2_prec_widths[i], c->log2_prec_heights[i]);
538 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
539 return AVERROR_INVALIDDATA;
543 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
544 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
549 /* get coding parameters for a particular tile or whole image*/
550 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
553 Jpeg2000CodingStyle tmp;
556 if (bytestream2_get_bytes_left(&s->g) < 5) {
557 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
558 return AVERROR_INVALIDDATA;
561 tmp.csty = bytestream2_get_byteu(&s->g);
563 // get progression order
564 tmp.prog_order = bytestream2_get_byteu(&s->g);
566 tmp.nlayers = bytestream2_get_be16u(&s->g);
567 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
569 if (tmp.mct && s->ncomponents < 3) {
570 av_log(s->avctx, AV_LOG_ERROR,
571 "MCT %"PRIu8" with too few components (%d)\n",
572 tmp.mct, s->ncomponents);
573 return AVERROR_INVALIDDATA;
576 if ((ret = get_cox(s, &tmp)) < 0)
579 for (compno = 0; compno < s->ncomponents; compno++)
580 if (!(properties[compno] & HAD_COC))
581 memcpy(c + compno, &tmp, sizeof(tmp));
585 /* Get coding parameters for a component in the whole image or a
586 * particular tile. */
587 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
591 uint8_t has_eph, has_sop;
593 if (bytestream2_get_bytes_left(&s->g) < 2) {
594 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
595 return AVERROR_INVALIDDATA;
598 compno = bytestream2_get_byteu(&s->g);
600 if (compno >= s->ncomponents) {
601 av_log(s->avctx, AV_LOG_ERROR,
602 "Invalid compno %d. There are %d components in the image.\n",
603 compno, s->ncomponents);
604 return AVERROR_INVALIDDATA;
608 has_eph = c->csty & JPEG2000_CSTY_EPH;
609 has_sop = c->csty & JPEG2000_CSTY_SOP;
610 c->csty = bytestream2_get_byteu(&s->g);
611 c->csty |= has_eph; //do not override eph present bits from COD
612 c->csty |= has_sop; //do not override sop present bits from COD
614 if ((ret = get_cox(s, c)) < 0)
617 properties[compno] |= HAD_COC;
622 static int get_rgn(Jpeg2000DecoderContext *s, int n)
625 compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
626 bytestream2_get_be16u(&s->g);
627 if (bytestream2_get_byte(&s->g)) {
628 av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
629 return AVERROR_INVALIDDATA; // SRgn field value is 0
632 // Currently compno cannot be greater than 4.
633 // However, future implementation should support compno up to 65536
634 if (compno < s->ncomponents) {
636 if (s->curtileno == -1) {
637 v = bytestream2_get_byte(&s->g);
639 return AVERROR_PATCHWELCOME;
640 s->roi_shift[compno] = v;
642 if (s->tile[s->curtileno].tp_idx != 0)
643 return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
644 v = bytestream2_get_byte(&s->g);
646 return AVERROR_PATCHWELCOME;
647 s->tile[s->curtileno].comp[compno].roi_shift = v;
651 return AVERROR_INVALIDDATA;
654 /* Get common part for QCD and QCC segments. */
655 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
659 if (bytestream2_get_bytes_left(&s->g) < 1)
660 return AVERROR_INVALIDDATA;
662 x = bytestream2_get_byteu(&s->g); // Sqcd
664 q->nguardbits = x >> 5;
665 q->quantsty = x & 0x1f;
667 if (q->quantsty == JPEG2000_QSTY_NONE) {
669 if (bytestream2_get_bytes_left(&s->g) < n ||
670 n > JPEG2000_MAX_DECLEVELS*3)
671 return AVERROR_INVALIDDATA;
672 for (i = 0; i < n; i++)
673 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
674 } else if (q->quantsty == JPEG2000_QSTY_SI) {
675 if (bytestream2_get_bytes_left(&s->g) < 2)
676 return AVERROR_INVALIDDATA;
677 x = bytestream2_get_be16u(&s->g);
678 q->expn[0] = x >> 11;
679 q->mant[0] = x & 0x7ff;
680 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
681 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
682 q->expn[i] = curexpn;
683 q->mant[i] = q->mant[0];
687 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
688 n > JPEG2000_MAX_DECLEVELS*3)
689 return AVERROR_INVALIDDATA;
690 for (i = 0; i < n; i++) {
691 x = bytestream2_get_be16u(&s->g);
692 q->expn[i] = x >> 11;
693 q->mant[i] = x & 0x7ff;
699 /* Get quantization parameters for a particular tile or a whole image. */
700 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
703 Jpeg2000QuantStyle tmp;
706 memset(&tmp, 0, sizeof(tmp));
708 if ((ret = get_qcx(s, n, &tmp)) < 0)
710 for (compno = 0; compno < s->ncomponents; compno++)
711 if (!(properties[compno] & HAD_QCC))
712 memcpy(q + compno, &tmp, sizeof(tmp));
716 /* Get quantization parameters for a component in the whole image
717 * on in a particular tile. */
718 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
723 if (bytestream2_get_bytes_left(&s->g) < 1)
724 return AVERROR_INVALIDDATA;
726 compno = bytestream2_get_byteu(&s->g);
728 if (compno >= s->ncomponents) {
729 av_log(s->avctx, AV_LOG_ERROR,
730 "Invalid compno %d. There are %d components in the image.\n",
731 compno, s->ncomponents);
732 return AVERROR_INVALIDDATA;
735 properties[compno] |= HAD_QCC;
736 return get_qcx(s, n - 1, q + compno);
739 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
742 int elem_size = s->ncomponents <= 257 ? 7 : 9;
743 Jpeg2000POC tmp = {{{0}}};
745 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
746 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
747 return AVERROR_INVALIDDATA;
751 avpriv_request_sample(s->avctx, "Fat POC not supported");
752 return AVERROR_PATCHWELCOME;
755 tmp.nb_poc = (size - 2) / elem_size;
756 if (tmp.nb_poc > MAX_POCS) {
757 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
758 return AVERROR_PATCHWELCOME;
761 for (i = 0; i<tmp.nb_poc; i++) {
762 Jpeg2000POCEntry *e = &tmp.poc[i];
763 e->RSpoc = bytestream2_get_byteu(&s->g);
764 e->CSpoc = bytestream2_get_byteu(&s->g);
765 e->LYEpoc = bytestream2_get_be16u(&s->g);
766 e->REpoc = bytestream2_get_byteu(&s->g);
767 e->CEpoc = bytestream2_get_byteu(&s->g);
768 e->Ppoc = bytestream2_get_byteu(&s->g);
771 if (e->CEpoc > s->ncomponents)
772 e->CEpoc = s->ncomponents;
773 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
774 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
776 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
777 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
779 return AVERROR_INVALIDDATA;
783 if (!p->nb_poc || p->is_default) {
786 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
787 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
788 return AVERROR_INVALIDDATA;
790 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
791 p->nb_poc += tmp.nb_poc;
800 /* Get start of tile segment. */
801 static int get_sot(Jpeg2000DecoderContext *s, int n)
803 Jpeg2000TilePart *tp;
808 if (bytestream2_get_bytes_left(&s->g) < 8)
809 return AVERROR_INVALIDDATA;
812 Isot = bytestream2_get_be16u(&s->g); // Isot
813 if (Isot >= s->numXtiles * s->numYtiles)
814 return AVERROR_INVALIDDATA;
817 Psot = bytestream2_get_be32u(&s->g); // Psot
818 TPsot = bytestream2_get_byteu(&s->g); // TPsot
820 /* Read TNSot but not used */
821 bytestream2_get_byteu(&s->g); // TNsot
824 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
826 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
827 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
828 return AVERROR_INVALIDDATA;
831 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
832 avpriv_request_sample(s->avctx, "Too many tile parts");
833 return AVERROR_PATCHWELCOME;
836 s->tile[Isot].tp_idx = TPsot;
837 tp = s->tile[Isot].tile_part + TPsot;
838 tp->tile_index = Isot;
839 tp->tp_end = s->g.buffer + Psot - n - 2;
842 Jpeg2000Tile *tile = s->tile + s->curtileno;
845 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
846 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
847 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
848 tile->poc.is_default = 1;
854 static int read_crg(Jpeg2000DecoderContext *s, int n)
856 if (s->ncomponents*4 != n - 2) {
857 av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
858 return AVERROR_INVALIDDATA;
860 bytestream2_skip(&s->g, n - 2);
863 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
864 * Used to know the number of tile parts and lengths.
865 * There may be multiple TLMs in the header.
866 * TODO: The function is not used for tile-parts management, nor anywhere else.
867 * It can be useful to allocate memory for tile parts, before managing the SOT
868 * markers. Parsing the TLM header is needed to increment the input header
870 * This marker is mandatory for DCI. */
871 static int get_tlm(Jpeg2000DecoderContext *s, int n)
873 uint8_t Stlm, ST, SP, tile_tlm, i;
874 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
875 Stlm = bytestream2_get_byte(&s->g);
877 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
878 ST = (Stlm >> 4) & 0x03;
880 av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
881 return AVERROR_INVALIDDATA;
884 SP = (Stlm >> 6) & 0x01;
885 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
886 for (i = 0; i < tile_tlm; i++) {
891 bytestream2_get_byte(&s->g);
894 bytestream2_get_be16(&s->g);
897 bytestream2_get_be32(&s->g);
901 bytestream2_get_be16(&s->g);
903 bytestream2_get_be32(&s->g);
909 static int get_plt(Jpeg2000DecoderContext *s, int n)
914 av_log(s->avctx, AV_LOG_DEBUG,
915 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
918 return AVERROR_INVALIDDATA;
920 /*Zplt =*/ bytestream2_get_byte(&s->g);
922 for (i = 0; i < n - 3; i++) {
923 v = bytestream2_get_byte(&s->g);
926 return AVERROR_INVALIDDATA;
931 static int get_ppt(Jpeg2000DecoderContext *s, int n)
937 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
938 return AVERROR_INVALIDDATA;
940 if (s->curtileno < 0)
941 return AVERROR_INVALIDDATA;
943 tile = &s->tile[s->curtileno];
944 if (tile->tp_idx != 0) {
945 av_log(s->avctx, AV_LOG_ERROR,
946 "PPT marker can occur only on first tile part of a tile.\n");
947 return AVERROR_INVALIDDATA;
950 tile->has_ppt = 1; // this tile has a ppt marker
951 bytestream2_get_byte(&s->g); // Zppt is skipped and not used
952 new = av_realloc(tile->packed_headers,
953 tile->packed_headers_size + n - 3);
955 tile->packed_headers = new;
957 return AVERROR(ENOMEM);
958 memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
959 memcpy(tile->packed_headers + tile->packed_headers_size,
961 tile->packed_headers_size += n - 3;
962 bytestream2_skip(&s->g, n - 3);
967 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
970 int tilex = tileno % s->numXtiles;
971 int tiley = tileno / s->numXtiles;
972 Jpeg2000Tile *tile = s->tile + tileno;
975 return AVERROR(ENOMEM);
977 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
978 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
979 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
980 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
982 for (compno = 0; compno < s->ncomponents; compno++) {
983 Jpeg2000Component *comp = tile->comp + compno;
984 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
985 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
986 int ret; // global bandno
988 comp->coord_o[0][0] = tile->coord[0][0];
989 comp->coord_o[0][1] = tile->coord[0][1];
990 comp->coord_o[1][0] = tile->coord[1][0];
991 comp->coord_o[1][1] = tile->coord[1][1];
993 comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
994 comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
995 comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
996 comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
998 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
999 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
1000 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
1001 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
1003 if (!comp->roi_shift)
1004 comp->roi_shift = s->roi_shift[compno];
1006 return AVERROR_INVALIDDATA;
1007 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
1008 s->cbps[compno], s->cdx[compno],
1009 s->cdy[compno], s->avctx))
1015 /* Read the number of coding passes. */
1016 static int getnpasses(Jpeg2000DecoderContext *s)
1019 if (!get_bits(s, 1))
1021 if (!get_bits(s, 1))
1023 if ((num = get_bits(s, 2)) != 3)
1024 return num < 0 ? num : 3 + num;
1025 if ((num = get_bits(s, 5)) != 31)
1026 return num < 0 ? num : 6 + num;
1027 num = get_bits(s, 7);
1028 return num < 0 ? num : 37 + num;
1031 static int getlblockinc(Jpeg2000DecoderContext *s)
1034 while (ret = get_bits(s, 1)) {
1042 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1045 s->g = tile->tile_part[*tp_index].tpg;
1046 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1047 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1048 s->g = tile->tile_part[++(*tp_index)].tpg;
1051 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1052 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1055 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1056 Jpeg2000CodingStyle *codsty,
1057 Jpeg2000ResLevel *rlevel, int precno,
1058 int layno, uint8_t *expn, int numgbits)
1060 int bandno, cblkno, ret, nb_code_blocks;
1063 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1065 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1066 // Select stream to read from
1068 s->g = tile->packed_headers_stream;
1070 select_stream(s, tile, tp_index);
1072 if (!(ret = get_bits(s, 1))) {
1078 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1079 Jpeg2000Band *band = rlevel->band + bandno;
1080 Jpeg2000Prec *prec = band->prec + precno;
1082 if (band->coord[0][0] == band->coord[0][1] ||
1083 band->coord[1][0] == band->coord[1][1])
1085 nb_code_blocks = prec->nb_codeblocks_height *
1086 prec->nb_codeblocks_width;
1087 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1088 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1089 int incl, newpasses, llen;
1093 incl = get_bits(s, 1);
1095 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1101 if (!cblk->npasses) {
1102 int v = expn[bandno] + numgbits - 1 -
1103 tag_tree_decode(s, prec->zerobits + cblkno, 100);
1104 if (v < 0 || v > 30) {
1105 av_log(s->avctx, AV_LOG_ERROR,
1106 "nonzerobits %d invalid or unsupported\n", v);
1107 return AVERROR_INVALIDDATA;
1109 cblk->nonzerobits = v;
1111 if ((newpasses = getnpasses(s)) < 0)
1113 av_assert2(newpasses > 0);
1114 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1115 avpriv_request_sample(s->avctx, "Too many passes");
1116 return AVERROR_PATCHWELCOME;
1118 if ((llen = getlblockinc(s)) < 0)
1120 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1121 avpriv_request_sample(s->avctx,
1122 "Block with length beyond 16 bits");
1123 return AVERROR_PATCHWELCOME;
1126 cblk->lblock += llen;
1128 cblk->nb_lengthinc = 0;
1129 cblk->nb_terminationsinc = 0;
1130 av_free(cblk->lengthinc);
1131 cblk->lengthinc = av_mallocz_array(newpasses , sizeof(*cblk->lengthinc));
1132 if (!cblk->lengthinc)
1133 return AVERROR(ENOMEM);
1134 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1136 return AVERROR(ENOMEM);
1137 cblk->data_start = tmp;
1141 while (newpasses1 < newpasses) {
1143 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1144 cblk->nb_terminationsinc ++;
1149 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1151 if (ret > cblk->data_allocated) {
1152 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1153 void *new = av_realloc(cblk->data, new_size);
1156 cblk->data_allocated = new_size;
1159 if (ret > cblk->data_allocated) {
1160 avpriv_request_sample(s->avctx,
1161 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1162 cblk->data_allocated);
1163 return AVERROR_PATCHWELCOME;
1165 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1166 cblk->npasses += newpasses1;
1167 newpasses -= newpasses1;
1173 if (codsty->csty & JPEG2000_CSTY_EPH) {
1174 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1175 bytestream2_skip(&s->g, 2);
1177 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1180 // Save state of stream
1181 if (tile->has_ppt) {
1182 tile->packed_headers_stream = s->g;
1183 select_stream(s, tile, tp_index);
1185 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1186 Jpeg2000Band *band = rlevel->band + bandno;
1187 Jpeg2000Prec *prec = band->prec + precno;
1189 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1190 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1191 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1192 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1194 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1195 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1196 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1197 void *new = av_realloc(cblk->data, new_size);
1200 cblk->data_allocated = new_size;
1203 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1204 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1206 av_log(s->avctx, AV_LOG_ERROR,
1207 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1208 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1209 return AVERROR_INVALIDDATA;
1212 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1213 cblk->length += cblk->lengthinc[cwsno];
1214 cblk->lengthinc[cwsno] = 0;
1215 if (cblk->nb_terminationsinc) {
1216 cblk->nb_terminationsinc--;
1217 cblk->nb_terminations++;
1218 cblk->data[cblk->length++] = 0xFF;
1219 cblk->data[cblk->length++] = 0xFF;
1220 cblk->data_start[cblk->nb_terminations] = cblk->length;
1223 av_freep(&cblk->lengthinc);
1226 // Save state of stream
1227 tile->tile_part[*tp_index].tpg = s->g;
1232 tile->packed_headers_stream = s->g;
1234 tile->tile_part[*tp_index].tpg = s->g;
1238 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1239 int RSpoc, int CSpoc,
1240 int LYEpoc, int REpoc, int CEpoc,
1241 int Ppoc, int *tp_index)
1244 int layno, reslevelno, compno, precno, ok_reslevel;
1249 case JPEG2000_PGOD_RLCP:
1250 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1252 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1254 for (layno = 0; layno < LYEpoc; layno++) {
1255 for (compno = CSpoc; compno < CEpoc; compno++) {
1256 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1257 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1258 if (reslevelno < codsty->nreslevels) {
1259 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1262 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1263 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1266 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1267 qntsty->nguardbits)) < 0)
1275 case JPEG2000_PGOD_LRCP:
1276 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1277 for (layno = 0; layno < LYEpoc; layno++) {
1279 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1281 for (compno = CSpoc; compno < CEpoc; compno++) {
1282 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1283 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1284 if (reslevelno < codsty->nreslevels) {
1285 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1288 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1289 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1292 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1293 qntsty->nguardbits)) < 0)
1301 case JPEG2000_PGOD_CPRL:
1302 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1303 for (compno = CSpoc; compno < CEpoc; compno++) {
1304 Jpeg2000Component *comp = tile->comp + compno;
1305 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1306 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1310 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1313 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1314 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1315 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1316 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1317 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1319 if (step_x >= 31 || step_y >= 31){
1320 avpriv_request_sample(s->avctx, "CPRL with large step");
1321 return AVERROR_PATCHWELCOME;
1326 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1327 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1328 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1329 unsigned prcx, prcy;
1330 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1331 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1332 int xc = x / s->cdx[compno];
1333 int yc = y / s->cdy[compno];
1335 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1338 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1341 // check if a precinct exists
1342 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1343 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1344 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1345 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1347 precno = prcx + rlevel->num_precincts_x * prcy;
1349 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1350 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1351 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1355 for (layno = 0; layno < LYEpoc; layno++) {
1356 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1358 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1359 qntsty->nguardbits)) < 0)
1368 case JPEG2000_PGOD_RPCL:
1369 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1371 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1375 for (compno = CSpoc; compno < CEpoc; compno++) {
1376 Jpeg2000Component *comp = tile->comp + compno;
1377 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1379 if (reslevelno < codsty->nreslevels) {
1380 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1381 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1382 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1383 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1389 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1390 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1391 for (compno = CSpoc; compno < CEpoc; compno++) {
1392 Jpeg2000Component *comp = tile->comp + compno;
1393 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1394 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1395 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1396 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1397 unsigned prcx, prcy;
1399 int xc = x / s->cdx[compno];
1400 int yc = y / s->cdy[compno];
1402 if (reslevelno >= codsty->nreslevels)
1405 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1408 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1411 // check if a precinct exists
1412 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1413 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1414 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1415 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1417 precno = prcx + rlevel->num_precincts_x * prcy;
1420 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1421 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1422 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1426 for (layno = 0; layno < LYEpoc; layno++) {
1427 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1430 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1431 qntsty->nguardbits)) < 0)
1440 case JPEG2000_PGOD_PCRL:
1441 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1444 for (compno = CSpoc; compno < CEpoc; compno++) {
1445 Jpeg2000Component *comp = tile->comp + compno;
1446 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1448 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1449 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1450 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1451 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1452 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1455 if (step_x >= 31 || step_y >= 31){
1456 avpriv_request_sample(s->avctx, "PCRL with large step");
1457 return AVERROR_PATCHWELCOME;
1462 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1463 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1464 for (compno = CSpoc; compno < CEpoc; compno++) {
1465 Jpeg2000Component *comp = tile->comp + compno;
1466 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1467 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1468 int xc = x / s->cdx[compno];
1469 int yc = y / s->cdy[compno];
1471 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1472 unsigned prcx, prcy;
1473 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1474 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1476 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1479 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1482 // check if a precinct exists
1483 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1484 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1485 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1486 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1488 precno = prcx + rlevel->num_precincts_x * prcy;
1490 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1491 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1492 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1496 for (layno = 0; layno < LYEpoc; layno++) {
1497 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1499 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1500 qntsty->nguardbits)) < 0)
1516 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1518 int ret = AVERROR_BUG;
1523 if (tile->poc.nb_poc) {
1524 for (i=0; i<tile->poc.nb_poc; i++) {
1525 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1526 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1528 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1530 FFMIN(e->CEpoc, s->ncomponents),
1537 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1539 tile->codsty[0].nlayers,
1542 tile->codsty[0].prog_order,
1546 /* EOC marker reached */
1547 bytestream2_skip(&s->g, 2);
1552 /* TIER-1 routines */
1553 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1554 int bpno, int bandno,
1555 int vert_causal_ctx_csty_symbol)
1557 int mask = 3 << (bpno - 1), y0, x, y;
1559 for (y0 = 0; y0 < height; y0 += 4)
1560 for (x = 0; x < width; x++)
1561 for (y = y0; y < height && y < y0 + 4; y++) {
1562 int flags_mask = -1;
1563 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1564 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1565 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1566 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1567 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1568 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1570 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1572 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1575 ff_jpeg2000_set_significance(t1, x, y,
1576 t1->data[(y) * t1->stride + x] < 0);
1578 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1583 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1584 int bpno, int vert_causal_ctx_csty_symbol)
1589 phalf = 1 << (bpno - 1);
1592 for (y0 = 0; y0 < height; y0 += 4)
1593 for (x = 0; x < width; x++)
1594 for (y = y0; y < height && y < y0 + 4; y++)
1595 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1596 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1597 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1598 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1599 int r = ff_mqc_decode(&t1->mqc,
1600 t1->mqc.cx_states + ctxno)
1602 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1603 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1607 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1608 int width, int height, int bpno, int bandno,
1609 int seg_symbols, int vert_causal_ctx_csty_symbol)
1611 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1613 for (y0 = 0; y0 < height; y0 += 4) {
1614 for (x = 0; x < width; x++) {
1615 int flags_mask = -1;
1616 if (vert_causal_ctx_csty_symbol)
1617 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1618 if (y0 + 3 < height &&
1619 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1620 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1621 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1622 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1623 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1625 runlen = ff_mqc_decode(&t1->mqc,
1626 t1->mqc.cx_states + MQC_CX_UNI);
1627 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1636 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1637 int flags_mask = -1;
1638 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1639 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1641 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1642 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1648 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1650 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1651 t1->mqc.cx_states + ctxno) ^
1654 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1657 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1663 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1664 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1665 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1666 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1668 av_log(s->avctx, AV_LOG_ERROR,
1669 "Segmentation symbol value incorrect\n");
1673 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1674 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1675 int width, int height, int bandpos, uint8_t roi_shift)
1677 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1679 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1683 av_assert0(width <= 1024U && height <= 1024U);
1684 av_assert0(width*height <= 4096);
1686 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1688 /* If code-block contains no compressed data: nothing to do. */
1692 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1694 cblk->data[cblk->length] = 0xff;
1695 cblk->data[cblk->length+1] = 0xff;
1696 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1699 if (bpno < 0 || bpno > 29) {
1700 av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
1701 return AVERROR_INVALIDDATA;
1705 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1706 vert_causal_ctx_csty_symbol);
1709 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1712 av_assert2(!t1->mqc.raw);
1713 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1714 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1715 vert_causal_ctx_csty_symbol);
1718 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1719 ff_mqc_init_contexts(&t1->mqc);
1721 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1722 if (term_cnt >= cblk->nb_terminations) {
1723 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1724 return AVERROR_INVALIDDATA;
1726 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1727 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1728 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1729 pass_cnt, cblk->npasses);
1732 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1743 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1744 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1745 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1751 static inline int roi_shift_param(Jpeg2000Component *comp,
1756 roi_shift = comp->roi_shift;
1757 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1759 if (val > (1 << roi_shift))
1760 return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1761 return quan_parameter;
1764 /* TODO: Verify dequantization for lossless case
1765 * comp->data can be float or int
1766 * band->stepsize can be float or int
1767 * depending on the type of DWT transformation.
1768 * see ISO/IEC 15444-1:2002 A.6.1 */
1770 /* Float dequantization of a codeblock.*/
1771 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1772 Jpeg2000Component *comp,
1773 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1776 int w = cblk->coord[0][1] - cblk->coord[0][0];
1777 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1778 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1779 int *src = t1->data + j*t1->stride;
1780 for (i = 0; i < w; ++i)
1781 datap[i] = src[i] * band->f_stepsize;
1785 /* Integer dequantization of a codeblock.*/
1786 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1787 Jpeg2000Component *comp,
1788 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1791 int w = cblk->coord[0][1] - cblk->coord[0][0];
1792 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1793 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1794 int *src = t1->data + j*t1->stride;
1795 if (band->i_stepsize == 32768) {
1796 for (i = 0; i < w; ++i)
1797 datap[i] = src[i] / 2;
1799 // This should be VERY uncommon
1800 for (i = 0; i < w; ++i)
1801 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1806 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1807 Jpeg2000Component *comp,
1808 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1811 int w = cblk->coord[0][1] - cblk->coord[0][0];
1812 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1813 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1814 int *src = t1->data + j*t1->stride;
1815 for (i = 0; i < w; ++i)
1816 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1820 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1825 for (i = 1; i < 3; i++) {
1826 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1827 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1830 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1831 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1836 for (i = 0; i < 3; i++)
1837 if (tile->codsty[0].transform == FF_DWT97)
1838 src[i] = tile->comp[i].f_data;
1840 src[i] = tile->comp[i].i_data;
1842 for (i = 0; i < 2; i++)
1843 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1845 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1848 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1849 Jpeg2000Component *comp,
1850 Jpeg2000T1Context *t1)
1853 int w = cblk->coord[0][1] - cblk->coord[0][0];
1854 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1855 int *src = t1->data + j*t1->stride;
1856 for (i = 0; i < w; ++i)
1857 src[i] = roi_shift_param(comp, src[i]);
1861 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1863 Jpeg2000T1Context t1;
1865 int compno, reslevelno, bandno;
1867 /* Loop on tile components */
1868 for (compno = 0; compno < s->ncomponents; compno++) {
1869 Jpeg2000Component *comp = tile->comp + compno;
1870 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1873 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1875 /* Loop on resolution levels */
1876 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1877 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1879 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1880 int nb_precincts, precno;
1881 Jpeg2000Band *band = rlevel->band + bandno;
1882 int cblkno = 0, bandpos;
1884 bandpos = bandno + (reslevelno > 0);
1886 if (band->coord[0][0] == band->coord[0][1] ||
1887 band->coord[1][0] == band->coord[1][1])
1890 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1891 /* Loop on precincts */
1892 for (precno = 0; precno < nb_precincts; precno++) {
1893 Jpeg2000Prec *prec = band->prec + precno;
1895 /* Loop on codeblocks */
1897 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1900 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1901 int ret = decode_cblk(s, codsty, &t1, cblk,
1902 cblk->coord[0][1] - cblk->coord[0][0],
1903 cblk->coord[1][1] - cblk->coord[1][0],
1904 bandpos, comp->roi_shift);
1909 x = cblk->coord[0][0] - band->coord[0][0];
1910 y = cblk->coord[1][0] - band->coord[1][0];
1912 if (comp->roi_shift)
1913 roi_scale_cblk(cblk, comp, &t1);
1914 if (codsty->transform == FF_DWT97)
1915 dequantization_float(x, y, cblk, comp, &t1, band);
1916 else if (codsty->transform == FF_DWT97_INT)
1917 dequantization_int_97(x, y, cblk, comp, &t1, band);
1919 dequantization_int(x, y, cblk, comp, &t1, band);
1923 } /* end reslevel */
1927 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1932 #define WRITE_FRAME(D, PIXEL) \
1933 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1934 AVFrame * picture, int precision) \
1936 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1937 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1938 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1943 for (compno = 0; compno < s->ncomponents; compno++) { \
1944 Jpeg2000Component *comp = tile->comp + compno; \
1945 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1947 float *datap = comp->f_data; \
1948 int32_t *i_datap = comp->i_data; \
1949 int cbps = s->cbps[compno]; \
1950 int w = tile->comp[compno].coord[0][1] - \
1951 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1952 int h = tile->comp[compno].coord[1][1] - \
1953 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1957 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1959 y = tile->comp[compno].coord[1][0] - \
1960 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1961 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1962 for (; y < h; y++) { \
1965 x = tile->comp[compno].coord[0][0] - \
1966 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1967 dst = line + x * pixelsize + compno*!planar; \
1969 if (codsty->transform == FF_DWT97) { \
1970 for (; x < w; x++) { \
1971 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1972 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1973 val = av_clip(val, 0, (1 << cbps) - 1); \
1974 *dst = val << (precision - cbps); \
1979 for (; x < w; x++) { \
1980 int val = *i_datap + (1 << (cbps - 1)); \
1981 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1982 val = av_clip(val, 0, (1 << cbps) - 1); \
1983 *dst = val << (precision - cbps); \
1988 line += picture->linesize[plane] / sizeof(PIXEL); \
1994 WRITE_FRAME(8, uint8_t)
1995 WRITE_FRAME(16, uint16_t)
1999 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2000 int jobnr, int threadnr)
2002 Jpeg2000DecoderContext *s = avctx->priv_data;
2003 AVFrame *picture = td;
2004 Jpeg2000Tile *tile = s->tile + jobnr;
2007 tile_codeblocks(s, tile);
2009 /* inverse MCT transformation */
2010 if (tile->codsty[0].mct)
2011 mct_decode(s, tile);
2013 for (x = 0; x < s->ncomponents; x++) {
2014 if (s->cdef[x] < 0) {
2015 for (x = 0; x < s->ncomponents; x++) {
2018 if ((s->ncomponents & 1) == 0)
2019 s->cdef[s->ncomponents-1] = 0;
2024 if (s->precision <= 8) {
2025 write_frame_8(s, tile, picture, 8);
2027 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2028 picture->format == AV_PIX_FMT_RGB48 ||
2029 picture->format == AV_PIX_FMT_RGBA64 ||
2030 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2032 write_frame_16(s, tile, picture, precision);
2038 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2041 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2042 if (s->tile[tileno].comp) {
2043 for (compno = 0; compno < s->ncomponents; compno++) {
2044 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2045 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2047 ff_jpeg2000_cleanup(comp, codsty);
2049 av_freep(&s->tile[tileno].comp);
2050 av_freep(&s->tile[tileno].packed_headers);
2051 s->tile[tileno].packed_headers_size = 0;
2055 memset(s->codsty, 0, sizeof(s->codsty));
2056 memset(s->qntsty, 0, sizeof(s->qntsty));
2057 memset(s->properties, 0, sizeof(s->properties));
2058 memset(&s->poc , 0, sizeof(s->poc));
2059 s->numXtiles = s->numYtiles = 0;
2063 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2065 Jpeg2000CodingStyle *codsty = s->codsty;
2066 Jpeg2000QuantStyle *qntsty = s->qntsty;
2067 Jpeg2000POC *poc = &s->poc;
2068 uint8_t *properties = s->properties;
2075 if (bytestream2_get_bytes_left(&s->g) < 2) {
2076 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2080 marker = bytestream2_get_be16u(&s->g);
2081 oldpos = bytestream2_tell(&s->g);
2083 if (marker == JPEG2000_SOD) {
2085 Jpeg2000TilePart *tp;
2088 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2089 return AVERROR_INVALIDDATA;
2091 if (s->curtileno < 0) {
2092 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2093 return AVERROR_INVALIDDATA;
2096 tile = s->tile + s->curtileno;
2097 tp = tile->tile_part + tile->tp_idx;
2098 if (tp->tp_end < s->g.buffer) {
2099 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2100 return AVERROR_INVALIDDATA;
2103 if (tile->has_ppt && tile->tp_idx == 0) {
2104 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2107 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2108 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2112 if (marker == JPEG2000_EOC)
2115 len = bytestream2_get_be16(&s->g);
2116 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2117 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2118 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2119 return AVERROR_INVALIDDATA;
2121 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2127 if (s->ncomponents) {
2128 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2129 return AVERROR_INVALIDDATA;
2133 s->numXtiles = s->numYtiles = 0;
2136 ret = get_coc(s, codsty, properties);
2139 ret = get_cod(s, codsty, properties);
2142 ret = get_rgn(s, len);
2145 ret = get_qcc(s, len, qntsty, properties);
2148 ret = get_qcd(s, len, qntsty, properties);
2151 ret = get_poc(s, len, poc);
2154 if (!(ret = get_sot(s, len))) {
2155 av_assert1(s->curtileno >= 0);
2156 codsty = s->tile[s->curtileno].codsty;
2157 qntsty = s->tile[s->curtileno].qntsty;
2158 poc = &s->tile[s->curtileno].poc;
2159 properties = s->tile[s->curtileno].properties;
2163 // the PLM marker is ignored
2165 // the comment is ignored
2166 bytestream2_skip(&s->g, len - 2);
2169 ret = read_crg(s, len);
2172 // Tile-part lengths
2173 ret = get_tlm(s, len);
2176 // Packet length, tile-part header
2177 ret = get_plt(s, len);
2180 // Packed headers, tile-part header
2181 ret = get_ppt(s, len);
2184 av_log(s->avctx, AV_LOG_ERROR,
2185 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2186 marker, bytestream2_tell(&s->g) - 4);
2187 bytestream2_skip(&s->g, len - 2);
2190 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2191 av_log(s->avctx, AV_LOG_ERROR,
2192 "error during processing marker segment %.4"PRIx16"\n",
2194 return ret ? ret : -1;
2200 /* Read bit stream packets --> T2 operation. */
2201 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2206 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2207 Jpeg2000Tile *tile = s->tile + tileno;
2209 if ((ret = init_tile(s, tileno)) < 0)
2212 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2219 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2221 uint32_t atom_size, atom, atom_end;
2222 int search_range = 10;
2226 bytestream2_get_bytes_left(&s->g) >= 8) {
2227 atom_size = bytestream2_get_be32u(&s->g);
2228 atom = bytestream2_get_be32u(&s->g);
2229 if (atom_size == 1) {
2230 if (bytestream2_get_be32u(&s->g)) {
2231 avpriv_request_sample(s->avctx, "Huge atom");
2234 atom_size = bytestream2_get_be32u(&s->g);
2235 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2237 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2240 if (atom == JP2_CODESTREAM)
2243 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2246 if (atom == JP2_HEADER &&
2248 uint32_t atom2_size, atom2, atom2_end;
2250 atom2_size = bytestream2_get_be32u(&s->g);
2251 atom2 = bytestream2_get_be32u(&s->g);
2252 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2253 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2256 if (atom2 == JP2_CODESTREAM) {
2258 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2259 int method = bytestream2_get_byteu(&s->g);
2260 bytestream2_skipu(&s->g, 2);
2262 s->colour_space = bytestream2_get_be32u(&s->g);
2264 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2265 int i, size, colour_count, colour_channels, colour_depth[3];
2266 colour_count = bytestream2_get_be16u(&s->g);
2267 colour_channels = bytestream2_get_byteu(&s->g);
2268 // FIXME: Do not ignore channel_sign
2269 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2270 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2271 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2272 size = (colour_depth[0] + 7 >> 3) * colour_count +
2273 (colour_depth[1] + 7 >> 3) * colour_count +
2274 (colour_depth[2] + 7 >> 3) * colour_count;
2275 if (colour_count > AVPALETTE_COUNT ||
2276 colour_channels != 3 ||
2277 colour_depth[0] > 16 ||
2278 colour_depth[1] > 16 ||
2279 colour_depth[2] > 16 ||
2280 atom2_size < size) {
2281 avpriv_request_sample(s->avctx, "Unknown palette");
2282 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2286 for (i = 0; i < colour_count; i++) {
2288 if (colour_depth[0] <= 8) {
2289 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2290 r |= r >> colour_depth[0];
2292 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2294 if (colour_depth[1] <= 8) {
2295 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2296 g |= g >> colour_depth[1];
2298 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2300 if (colour_depth[2] <= 8) {
2301 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2302 b |= b >> colour_depth[2];
2304 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2306 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2308 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2309 int n = bytestream2_get_be16u(&s->g);
2311 int cn = bytestream2_get_be16(&s->g);
2312 int av_unused typ = bytestream2_get_be16(&s->g);
2313 int asoc = bytestream2_get_be16(&s->g);
2314 if (cn < 4 && asoc < 4)
2317 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2318 int64_t vnum, vden, hnum, hden, vexp, hexp;
2320 bytestream2_skip(&s->g, 4);
2321 resx = bytestream2_get_be32u(&s->g);
2322 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2323 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2326 vnum = bytestream2_get_be16u(&s->g);
2327 vden = bytestream2_get_be16u(&s->g);
2328 hnum = bytestream2_get_be16u(&s->g);
2329 hden = bytestream2_get_be16u(&s->g);
2330 vexp = bytestream2_get_byteu(&s->g);
2331 hexp = bytestream2_get_byteu(&s->g);
2332 if (!vnum || !vden || !hnum || !hden) {
2333 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2334 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2344 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2345 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2346 av_reduce(&s->sar.den, &s->sar.num,
2347 hnum * vden * pow(10, hexp),
2348 vnum * hden * pow(10, vexp),
2351 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2352 } while (atom_end - atom2_end >= 8);
2356 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2362 static av_cold void jpeg2000_init_static_data(void)
2364 ff_jpeg2000_init_tier1_luts();
2365 ff_mqc_init_context_tables();
2368 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2370 static AVOnce init_static_once = AV_ONCE_INIT;
2371 Jpeg2000DecoderContext *s = avctx->priv_data;
2373 ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2374 ff_jpeg2000dsp_init(&s->dsp);
2379 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2380 int *got_frame, AVPacket *avpkt)
2382 Jpeg2000DecoderContext *s = avctx->priv_data;
2383 ThreadFrame frame = { .f = data };
2384 AVFrame *picture = data;
2388 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2390 memset(s->cdef, -1, sizeof(s->cdef));
2392 if (bytestream2_get_bytes_left(&s->g) < 2) {
2393 ret = AVERROR_INVALIDDATA;
2397 // check if the image is in jp2 format
2398 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2399 (bytestream2_get_be32u(&s->g) == 12) &&
2400 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2401 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2402 if (!jp2_find_codestream(s)) {
2403 av_log(avctx, AV_LOG_ERROR,
2404 "Could not find Jpeg2000 codestream atom.\n");
2405 ret = AVERROR_INVALIDDATA;
2409 bytestream2_seek(&s->g, 0, SEEK_SET);
2412 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2413 bytestream2_skip(&s->g, 1);
2415 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2416 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2417 ret = AVERROR_INVALIDDATA;
2420 if (ret = jpeg2000_read_main_headers(s))
2423 /* get picture buffer */
2424 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2426 picture->pict_type = AV_PICTURE_TYPE_I;
2427 picture->key_frame = 1;
2429 if (ret = jpeg2000_read_bitstream_packets(s))
2432 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2434 jpeg2000_dec_cleanup(s);
2438 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2439 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2440 if (s->sar.num && s->sar.den)
2441 avctx->sample_aspect_ratio = s->sar;
2442 s->sar.num = s->sar.den = 0;
2444 return bytestream2_tell(&s->g);
2447 jpeg2000_dec_cleanup(s);
2451 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2452 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2454 static const AVOption options[] = {
2455 { "lowres", "Lower the decoding resolution by a power of two",
2456 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2460 static const AVClass jpeg2000_class = {
2461 .class_name = "jpeg2000",
2462 .item_name = av_default_item_name,
2464 .version = LIBAVUTIL_VERSION_INT,
2467 AVCodec ff_jpeg2000_decoder = {
2469 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2470 .type = AVMEDIA_TYPE_VIDEO,
2471 .id = AV_CODEC_ID_JPEG2000,
2472 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2473 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2474 .init = jpeg2000_decode_init,
2475 .decode = jpeg2000_decode_frame,
2476 .priv_class = &jpeg2000_class,
2478 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)