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"
38 #include "bytestream.h"
42 #include "jpeg2000dsp.h"
45 #define JP2_SIG_TYPE 0x6A502020
46 #define JP2_SIG_VALUE 0x0D0A870A
47 #define JP2_CODESTREAM 0x6A703263
48 #define JP2_HEADER 0x6A703268
55 typedef struct Jpeg2000POCEntry {
64 typedef struct Jpeg2000POC {
65 Jpeg2000POCEntry poc[MAX_POCS];
70 typedef struct Jpeg2000TilePart {
71 uint8_t tile_index; // Tile index who refers the tile-part
72 const uint8_t *tp_end;
73 GetByteContext tpg; // bit stream in tile-part
76 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
77 * one per component, so tile_part elements have a size of 3 */
78 typedef struct Jpeg2000Tile {
79 Jpeg2000Component *comp;
80 uint8_t properties[4];
81 Jpeg2000CodingStyle codsty[4];
82 Jpeg2000QuantStyle qntsty[4];
84 Jpeg2000TilePart tile_part[256];
85 uint16_t tp_idx; // Tile-part index
86 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
89 typedef struct Jpeg2000DecoderContext {
91 AVCodecContext *avctx;
95 int image_offset_x, image_offset_y;
96 int tile_offset_x, tile_offset_y;
97 uint8_t cbps[4]; // bits per sample in particular components
98 uint8_t sgnd[4]; // if a component is signed
99 uint8_t properties[4];
104 uint32_t palette[256];
107 int tile_width, tile_height;
108 unsigned numXtiles, numYtiles;
112 Jpeg2000CodingStyle codsty[4];
113 Jpeg2000QuantStyle qntsty[4];
121 Jpeg2000DSPContext dsp;
123 /*options parameters*/
124 int reduction_factor;
125 } Jpeg2000DecoderContext;
127 /* get_bits functions for JPEG2000 packet bitstream
128 * It is a get_bit function with a bit-stuffing routine. If the value of the
129 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
130 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
131 static int get_bits(Jpeg2000DecoderContext *s, int n)
137 if (s->bit_index == 0) {
138 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
141 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
146 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
148 if (bytestream2_get_byte(&s->g) == 0xff)
149 bytestream2_skip(&s->g, 1);
153 /* decode the value stored in node */
154 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
157 Jpeg2000TgtNode *stack[30];
158 int sp = -1, curval = 0;
161 av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
162 return AVERROR_INVALIDDATA;
165 while (node && !node->vis) {
173 curval = stack[sp]->val;
175 while (curval < threshold && sp >= 0) {
176 if (curval < stack[sp]->val)
177 curval = stack[sp]->val;
178 while (curval < threshold) {
180 if ((ret = get_bits(s, 1)) > 0) {
188 stack[sp]->val = curval;
194 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
195 int bpc, uint32_t log2_chroma_wh, int pal8)
198 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
202 if (desc->nb_components != components) {
206 switch (components) {
208 match = match && desc->comp[3].depth >= bpc &&
209 (log2_chroma_wh >> 14 & 3) == 0 &&
210 (log2_chroma_wh >> 12 & 3) == 0;
212 match = match && desc->comp[2].depth >= bpc &&
213 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
214 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
216 match = match && desc->comp[1].depth >= bpc &&
217 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
218 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
221 match = match && desc->comp[0].depth >= bpc &&
222 (log2_chroma_wh >> 2 & 3) == 0 &&
223 (log2_chroma_wh & 3) == 0 &&
224 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
229 // pix_fmts with lower bpp have to be listed before
230 // similar pix_fmts with higher bpp.
231 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
232 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
233 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
234 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
235 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
236 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
237 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
238 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
239 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
240 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
241 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
242 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
243 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
244 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
246 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
247 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
248 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
249 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS,
251 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
256 /* marker segments */
257 /* get sizes and offsets of image, tiles; number of components */
258 static int get_siz(Jpeg2000DecoderContext *s)
262 uint32_t log2_chroma_wh = 0;
263 const enum AVPixelFormat *possible_fmts = NULL;
264 int possible_fmts_nb = 0;
267 if (bytestream2_get_bytes_left(&s->g) < 36) {
268 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
269 return AVERROR_INVALIDDATA;
272 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
273 s->width = bytestream2_get_be32u(&s->g); // Width
274 s->height = bytestream2_get_be32u(&s->g); // Height
275 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
276 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
277 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
278 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
279 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
280 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
281 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
283 if (s->image_offset_x || s->image_offset_y) {
284 avpriv_request_sample(s->avctx, "Support for image offsets");
285 return AVERROR_PATCHWELCOME;
287 if (av_image_check_size(s->width, s->height, 0, s->avctx)) {
288 avpriv_request_sample(s->avctx, "Large Dimensions");
289 return AVERROR_PATCHWELCOME;
292 if (ncomponents <= 0) {
293 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
295 return AVERROR_INVALIDDATA;
298 if (ncomponents > 4) {
299 avpriv_request_sample(s->avctx, "Support for %d components",
301 return AVERROR_PATCHWELCOME;
304 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
305 s->image_offset_x < s->tile_offset_x ||
306 s->image_offset_y < s->tile_offset_y ||
307 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
308 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
310 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
311 return AVERROR_INVALIDDATA;
314 s->ncomponents = ncomponents;
316 if (s->tile_width <= 0 || s->tile_height <= 0) {
317 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
318 s->tile_width, s->tile_height);
319 return AVERROR_INVALIDDATA;
322 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
323 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
324 return AVERROR_INVALIDDATA;
327 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
328 uint8_t x = bytestream2_get_byteu(&s->g);
329 s->cbps[i] = (x & 0x7f) + 1;
330 s->precision = FFMAX(s->cbps[i], s->precision);
331 s->sgnd[i] = !!(x & 0x80);
332 s->cdx[i] = bytestream2_get_byteu(&s->g);
333 s->cdy[i] = bytestream2_get_byteu(&s->g);
334 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
335 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
336 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
337 return AVERROR_INVALIDDATA;
339 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
342 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
343 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
345 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
346 s->numXtiles = s->numYtiles = 0;
347 return AVERROR(EINVAL);
350 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
352 s->numXtiles = s->numYtiles = 0;
353 return AVERROR(ENOMEM);
356 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
357 Jpeg2000Tile *tile = s->tile + i;
359 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
361 return AVERROR(ENOMEM);
364 /* compute image size with reduction factor */
365 ret = ff_set_dimensions(s->avctx,
366 ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
367 s->reduction_factor),
368 ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
369 s->reduction_factor));
373 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
374 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
375 possible_fmts = xyz_pix_fmts;
376 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
378 switch (s->colour_space) {
380 possible_fmts = rgb_pix_fmts;
381 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
384 possible_fmts = gray_pix_fmts;
385 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
388 possible_fmts = yuv_pix_fmts;
389 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
392 possible_fmts = all_pix_fmts;
393 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
397 for (i = 0; i < possible_fmts_nb; ++i) {
398 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
399 s->avctx->pix_fmt = possible_fmts[i];
404 if (i == possible_fmts_nb) {
405 if (ncomponents == 4 &&
406 s->cdy[0] == 1 && s->cdx[0] == 1 &&
407 s->cdy[1] == 1 && s->cdx[1] == 1 &&
408 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
409 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
410 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
421 if (i == possible_fmts_nb) {
422 av_log(s->avctx, AV_LOG_ERROR,
423 "Unknown pix_fmt, profile: %d, colour_space: %d, "
424 "components: %d, precision: %d\n"
425 "cdx[0]: %d, cdy[0]: %d\n"
426 "cdx[1]: %d, cdy[1]: %d\n"
427 "cdx[2]: %d, cdy[2]: %d\n"
428 "cdx[3]: %d, cdy[3]: %d\n",
429 s->avctx->profile, s->colour_space, ncomponents, s->precision,
432 ncomponents > 1 ? s->cdx[1] : 0,
433 ncomponents > 1 ? s->cdy[1] : 0,
434 ncomponents > 2 ? s->cdx[2] : 0,
435 ncomponents > 2 ? s->cdy[2] : 0,
436 ncomponents > 3 ? s->cdx[3] : 0,
437 ncomponents > 3 ? s->cdy[3] : 0);
438 return AVERROR_PATCHWELCOME;
440 s->avctx->bits_per_raw_sample = s->precision;
444 /* get common part for COD and COC segments */
445 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
449 if (bytestream2_get_bytes_left(&s->g) < 5) {
450 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
451 return AVERROR_INVALIDDATA;
454 /* nreslevels = number of resolution levels
455 = number of decomposition level +1 */
456 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
457 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
458 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
459 return AVERROR_INVALIDDATA;
462 if (c->nreslevels <= s->reduction_factor) {
463 /* we are forced to update reduction_factor as its requested value is
464 not compatible with this bitstream, and as we might have used it
465 already in setup earlier we have to fail this frame until
466 reinitialization is implemented */
467 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
468 s->reduction_factor = c->nreslevels - 1;
469 return AVERROR(EINVAL);
472 /* compute number of resolution levels to decode */
473 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
475 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
476 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
478 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
479 c->log2_cblk_width + c->log2_cblk_height > 12) {
480 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
481 return AVERROR_INVALIDDATA;
484 c->cblk_style = bytestream2_get_byteu(&s->g);
485 if (c->cblk_style != 0) { // cblk style
486 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
487 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
488 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
490 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
491 /* set integer 9/7 DWT in case of BITEXACT flag */
492 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
493 c->transform = FF_DWT97_INT;
494 else if (c->transform == FF_DWT53) {
495 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
498 if (c->csty & JPEG2000_CSTY_PREC) {
500 for (i = 0; i < c->nreslevels; i++) {
501 byte = bytestream2_get_byte(&s->g);
502 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
503 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
505 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
506 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
507 c->log2_prec_widths[i], c->log2_prec_heights[i]);
508 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
509 return AVERROR_INVALIDDATA;
513 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
514 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
519 /* get coding parameters for a particular tile or whole image*/
520 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
523 Jpeg2000CodingStyle tmp;
526 if (bytestream2_get_bytes_left(&s->g) < 5) {
527 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
528 return AVERROR_INVALIDDATA;
531 tmp.csty = bytestream2_get_byteu(&s->g);
533 // get progression order
534 tmp.prog_order = bytestream2_get_byteu(&s->g);
536 tmp.nlayers = bytestream2_get_be16u(&s->g);
537 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
539 if (tmp.mct && s->ncomponents < 3) {
540 av_log(s->avctx, AV_LOG_ERROR,
541 "MCT %"PRIu8" with too few components (%d)\n",
542 tmp.mct, s->ncomponents);
543 return AVERROR_INVALIDDATA;
546 if ((ret = get_cox(s, &tmp)) < 0)
549 for (compno = 0; compno < s->ncomponents; compno++)
550 if (!(properties[compno] & HAD_COC))
551 memcpy(c + compno, &tmp, sizeof(tmp));
555 /* Get coding parameters for a component in the whole image or a
556 * particular tile. */
557 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
562 if (bytestream2_get_bytes_left(&s->g) < 2) {
563 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
564 return AVERROR_INVALIDDATA;
567 compno = bytestream2_get_byteu(&s->g);
569 if (compno >= s->ncomponents) {
570 av_log(s->avctx, AV_LOG_ERROR,
571 "Invalid compno %d. There are %d components in the image.\n",
572 compno, s->ncomponents);
573 return AVERROR_INVALIDDATA;
577 c->csty = bytestream2_get_byteu(&s->g);
579 if ((ret = get_cox(s, c)) < 0)
582 properties[compno] |= HAD_COC;
586 /* Get common part for QCD and QCC segments. */
587 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
591 if (bytestream2_get_bytes_left(&s->g) < 1)
592 return AVERROR_INVALIDDATA;
594 x = bytestream2_get_byteu(&s->g); // Sqcd
596 q->nguardbits = x >> 5;
597 q->quantsty = x & 0x1f;
599 if (q->quantsty == JPEG2000_QSTY_NONE) {
601 if (bytestream2_get_bytes_left(&s->g) < n ||
602 n > JPEG2000_MAX_DECLEVELS*3)
603 return AVERROR_INVALIDDATA;
604 for (i = 0; i < n; i++)
605 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
606 } else if (q->quantsty == JPEG2000_QSTY_SI) {
607 if (bytestream2_get_bytes_left(&s->g) < 2)
608 return AVERROR_INVALIDDATA;
609 x = bytestream2_get_be16u(&s->g);
610 q->expn[0] = x >> 11;
611 q->mant[0] = x & 0x7ff;
612 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
613 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
614 q->expn[i] = curexpn;
615 q->mant[i] = q->mant[0];
619 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
620 n > JPEG2000_MAX_DECLEVELS*3)
621 return AVERROR_INVALIDDATA;
622 for (i = 0; i < n; i++) {
623 x = bytestream2_get_be16u(&s->g);
624 q->expn[i] = x >> 11;
625 q->mant[i] = x & 0x7ff;
631 /* Get quantization parameters for a particular tile or a whole image. */
632 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
635 Jpeg2000QuantStyle tmp;
638 memset(&tmp, 0, sizeof(tmp));
640 if ((ret = get_qcx(s, n, &tmp)) < 0)
642 for (compno = 0; compno < s->ncomponents; compno++)
643 if (!(properties[compno] & HAD_QCC))
644 memcpy(q + compno, &tmp, sizeof(tmp));
648 /* Get quantization parameters for a component in the whole image
649 * on in a particular tile. */
650 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
655 if (bytestream2_get_bytes_left(&s->g) < 1)
656 return AVERROR_INVALIDDATA;
658 compno = bytestream2_get_byteu(&s->g);
660 if (compno >= s->ncomponents) {
661 av_log(s->avctx, AV_LOG_ERROR,
662 "Invalid compno %d. There are %d components in the image.\n",
663 compno, s->ncomponents);
664 return AVERROR_INVALIDDATA;
667 properties[compno] |= HAD_QCC;
668 return get_qcx(s, n - 1, q + compno);
671 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
674 int elem_size = s->ncomponents <= 257 ? 7 : 9;
675 Jpeg2000POC tmp = {{{0}}};
677 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
678 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
679 return AVERROR_INVALIDDATA;
683 avpriv_request_sample(s->avctx, "Fat POC not supported");
684 return AVERROR_PATCHWELCOME;
687 tmp.nb_poc = (size - 2) / elem_size;
688 if (tmp.nb_poc > MAX_POCS) {
689 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
690 return AVERROR_PATCHWELCOME;
693 for (i = 0; i<tmp.nb_poc; i++) {
694 Jpeg2000POCEntry *e = &tmp.poc[i];
695 e->RSpoc = bytestream2_get_byteu(&s->g);
696 e->CSpoc = bytestream2_get_byteu(&s->g);
697 e->LYEpoc = bytestream2_get_be16u(&s->g);
698 e->REpoc = bytestream2_get_byteu(&s->g);
699 e->CEpoc = bytestream2_get_byteu(&s->g);
700 e->Ppoc = bytestream2_get_byteu(&s->g);
703 if (e->CEpoc > s->ncomponents)
704 e->CEpoc = s->ncomponents;
705 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
706 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
708 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
709 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
711 return AVERROR_INVALIDDATA;
715 if (!p->nb_poc || p->is_default) {
718 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
719 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
720 return AVERROR_INVALIDDATA;
722 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
723 p->nb_poc += tmp.nb_poc;
732 /* Get start of tile segment. */
733 static int get_sot(Jpeg2000DecoderContext *s, int n)
735 Jpeg2000TilePart *tp;
740 if (bytestream2_get_bytes_left(&s->g) < 8)
741 return AVERROR_INVALIDDATA;
744 Isot = bytestream2_get_be16u(&s->g); // Isot
745 if (Isot >= s->numXtiles * s->numYtiles)
746 return AVERROR_INVALIDDATA;
749 Psot = bytestream2_get_be32u(&s->g); // Psot
750 TPsot = bytestream2_get_byteu(&s->g); // TPsot
752 /* Read TNSot but not used */
753 bytestream2_get_byteu(&s->g); // TNsot
756 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
758 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
759 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
760 return AVERROR_INVALIDDATA;
763 av_assert0(TPsot < FF_ARRAY_ELEMS(s->tile[Isot].tile_part));
765 s->tile[Isot].tp_idx = TPsot;
766 tp = s->tile[Isot].tile_part + TPsot;
767 tp->tile_index = Isot;
768 tp->tp_end = s->g.buffer + Psot - n - 2;
771 Jpeg2000Tile *tile = s->tile + s->curtileno;
774 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
775 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
776 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
777 tile->poc.is_default = 1;
783 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
784 * Used to know the number of tile parts and lengths.
785 * There may be multiple TLMs in the header.
786 * TODO: The function is not used for tile-parts management, nor anywhere else.
787 * It can be useful to allocate memory for tile parts, before managing the SOT
788 * markers. Parsing the TLM header is needed to increment the input header
790 * This marker is mandatory for DCI. */
791 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
793 uint8_t Stlm, ST, SP, tile_tlm, i;
794 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
795 Stlm = bytestream2_get_byte(&s->g);
797 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
798 ST = (Stlm >> 4) & 0x03;
799 // TODO: Manage case of ST = 0b11 --> raise error
800 SP = (Stlm >> 6) & 0x01;
801 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
802 for (i = 0; i < tile_tlm; i++) {
807 bytestream2_get_byte(&s->g);
810 bytestream2_get_be16(&s->g);
813 bytestream2_get_be32(&s->g);
817 bytestream2_get_be16(&s->g);
819 bytestream2_get_be32(&s->g);
825 static uint8_t get_plt(Jpeg2000DecoderContext *s, int n)
829 av_log(s->avctx, AV_LOG_DEBUG,
830 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
832 /*Zplt =*/ bytestream2_get_byte(&s->g);
834 for (i = 0; i < n - 3; i++) {
835 bytestream2_get_byte(&s->g);
841 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
844 int tilex = tileno % s->numXtiles;
845 int tiley = tileno / s->numXtiles;
846 Jpeg2000Tile *tile = s->tile + tileno;
849 return AVERROR(ENOMEM);
851 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
852 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
853 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
854 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
856 for (compno = 0; compno < s->ncomponents; compno++) {
857 Jpeg2000Component *comp = tile->comp + compno;
858 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
859 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
860 int ret; // global bandno
862 comp->coord_o[0][0] = tile->coord[0][0];
863 comp->coord_o[0][1] = tile->coord[0][1];
864 comp->coord_o[1][0] = tile->coord[1][0];
865 comp->coord_o[1][1] = tile->coord[1][1];
867 comp->coord_o[0][0] /= s->cdx[compno];
868 comp->coord_o[0][1] /= s->cdx[compno];
869 comp->coord_o[1][0] /= s->cdy[compno];
870 comp->coord_o[1][1] /= s->cdy[compno];
873 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
874 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
875 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
876 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
878 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
879 s->cbps[compno], s->cdx[compno],
880 s->cdy[compno], s->avctx))
886 /* Read the number of coding passes. */
887 static int getnpasses(Jpeg2000DecoderContext *s)
894 if ((num = get_bits(s, 2)) != 3)
895 return num < 0 ? num : 3 + num;
896 if ((num = get_bits(s, 5)) != 31)
897 return num < 0 ? num : 6 + num;
898 num = get_bits(s, 7);
899 return num < 0 ? num : 37 + num;
902 static int getlblockinc(Jpeg2000DecoderContext *s)
905 while (ret = get_bits(s, 1)) {
913 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
914 Jpeg2000CodingStyle *codsty,
915 Jpeg2000ResLevel *rlevel, int precno,
916 int layno, uint8_t *expn, int numgbits)
918 int bandno, cblkno, ret, nb_code_blocks;
921 if (layno < rlevel->band[0].prec[precno].decoded_layers)
923 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
925 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
926 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
927 s->g = tile->tile_part[++(*tp_index)].tpg;
931 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
932 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
934 if (!(ret = get_bits(s, 1))) {
940 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
941 Jpeg2000Band *band = rlevel->band + bandno;
942 Jpeg2000Prec *prec = band->prec + precno;
944 if (band->coord[0][0] == band->coord[0][1] ||
945 band->coord[1][0] == band->coord[1][1])
947 nb_code_blocks = prec->nb_codeblocks_height *
948 prec->nb_codeblocks_width;
949 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
950 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
951 int incl, newpasses, llen;
954 incl = get_bits(s, 1);
956 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
962 if (!cblk->npasses) {
963 int v = expn[bandno] + numgbits - 1 -
964 tag_tree_decode(s, prec->zerobits + cblkno, 100);
965 if (v < 0 || v > 30) {
966 av_log(s->avctx, AV_LOG_ERROR,
967 "nonzerobits %d invalid or unsupported\n", v);
968 return AVERROR_INVALIDDATA;
970 cblk->nonzerobits = v;
972 if ((newpasses = getnpasses(s)) < 0)
974 av_assert2(newpasses > 0);
975 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
976 avpriv_request_sample(s->avctx, "Too many passes");
977 return AVERROR_PATCHWELCOME;
979 if ((llen = getlblockinc(s)) < 0)
981 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
982 avpriv_request_sample(s->avctx,
983 "Block with length beyond 16 bits");
984 return AVERROR_PATCHWELCOME;
987 cblk->lblock += llen;
989 cblk->nb_lengthinc = 0;
990 cblk->nb_terminationsinc = 0;
994 while (newpasses1 < newpasses) {
996 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
997 cblk->nb_terminationsinc ++;
1002 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1004 if (ret > cblk->data_allocated) {
1005 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1006 void *new = av_realloc(cblk->data, new_size);
1009 cblk->data_allocated = new_size;
1012 if (ret > cblk->data_allocated) {
1013 avpriv_request_sample(s->avctx,
1014 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1015 cblk->data_allocated);
1016 return AVERROR_PATCHWELCOME;
1018 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1019 cblk->npasses += newpasses1;
1020 newpasses -= newpasses1;
1026 if (codsty->csty & JPEG2000_CSTY_EPH) {
1027 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1028 bytestream2_skip(&s->g, 2);
1030 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1033 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1034 Jpeg2000Band *band = rlevel->band + bandno;
1035 Jpeg2000Prec *prec = band->prec + precno;
1037 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1038 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1039 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1040 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1041 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1042 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1043 void *new = av_realloc(cblk->data, new_size);
1046 cblk->data_allocated = new_size;
1049 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1050 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1052 av_log(s->avctx, AV_LOG_ERROR,
1053 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1054 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1055 return AVERROR_INVALIDDATA;
1058 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1059 cblk->length += cblk->lengthinc[cwsno];
1060 cblk->lengthinc[cwsno] = 0;
1061 if (cblk->nb_terminationsinc) {
1062 cblk->nb_terminationsinc--;
1063 cblk->nb_terminations++;
1064 cblk->data[cblk->length++] = 0xFF;
1065 cblk->data[cblk->length++] = 0xFF;
1066 cblk->data_start[cblk->nb_terminations] = cblk->length;
1074 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1075 int RSpoc, int CSpoc,
1076 int LYEpoc, int REpoc, int CEpoc,
1077 int Ppoc, int *tp_index)
1080 int layno, reslevelno, compno, precno, ok_reslevel;
1085 case JPEG2000_PGOD_RLCP:
1086 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1088 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1090 for (layno = 0; layno < LYEpoc; layno++) {
1091 for (compno = CSpoc; compno < CEpoc; compno++) {
1092 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1093 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1094 if (reslevelno < codsty->nreslevels) {
1095 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1098 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1099 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1102 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1103 qntsty->nguardbits)) < 0)
1111 case JPEG2000_PGOD_LRCP:
1112 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1113 for (layno = 0; layno < LYEpoc; layno++) {
1115 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1117 for (compno = CSpoc; compno < CEpoc; compno++) {
1118 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1119 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1120 if (reslevelno < codsty->nreslevels) {
1121 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1124 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1125 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1128 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1129 qntsty->nguardbits)) < 0)
1137 case JPEG2000_PGOD_CPRL:
1138 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1139 for (compno = CSpoc; compno < CEpoc; compno++) {
1140 Jpeg2000Component *comp = tile->comp + compno;
1141 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1142 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1146 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1147 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1148 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1149 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1150 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1152 av_assert0(step_x < 32 && step_y < 32);
1156 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1157 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1158 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1159 unsigned prcx, prcy;
1160 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1161 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1162 int xc = x / s->cdx[compno];
1163 int yc = y / s->cdy[compno];
1165 if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1168 if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1171 // check if a precinct exists
1172 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1173 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1174 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1175 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1177 precno = prcx + rlevel->num_precincts_x * prcy;
1179 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1180 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1181 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1185 for (layno = 0; layno < LYEpoc; layno++) {
1186 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1188 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1189 qntsty->nguardbits)) < 0)
1198 case JPEG2000_PGOD_RPCL:
1199 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1201 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1205 for (compno = CSpoc; compno < CEpoc; compno++) {
1206 Jpeg2000Component *comp = tile->comp + compno;
1207 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1209 if (reslevelno < codsty->nreslevels) {
1210 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1211 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1212 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1213 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1219 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1220 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1221 for (compno = CSpoc; compno < CEpoc; compno++) {
1222 Jpeg2000Component *comp = tile->comp + compno;
1223 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1224 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1225 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1226 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1227 unsigned prcx, prcy;
1229 int xc = x / s->cdx[compno];
1230 int yc = y / s->cdy[compno];
1232 if (reslevelno >= codsty->nreslevels)
1235 if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1238 if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1241 // check if a precinct exists
1242 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1243 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1244 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1245 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1247 precno = prcx + rlevel->num_precincts_x * prcy;
1250 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1251 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1252 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1256 for (layno = 0; layno < LYEpoc; layno++) {
1257 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1260 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1261 qntsty->nguardbits)) < 0)
1270 case JPEG2000_PGOD_PCRL:
1271 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1274 for (compno = CSpoc; compno < CEpoc; compno++) {
1275 Jpeg2000Component *comp = tile->comp + compno;
1276 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1278 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1279 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1280 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1281 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1282 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1285 if (step_x >= 31 || step_y >= 31){
1286 avpriv_request_sample(s->avctx, "PCRL with large step");
1287 return AVERROR_PATCHWELCOME;
1292 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1293 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1294 for (compno = CSpoc; compno < CEpoc; compno++) {
1295 Jpeg2000Component *comp = tile->comp + compno;
1296 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1297 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1298 int xc = x / s->cdx[compno];
1299 int yc = y / s->cdy[compno];
1301 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1302 unsigned prcx, prcy;
1303 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1304 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1306 if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1309 if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1312 // check if a precinct exists
1313 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1314 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1315 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1316 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1318 precno = prcx + rlevel->num_precincts_x * prcy;
1320 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1321 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1322 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1326 for (layno = 0; layno < LYEpoc; layno++) {
1327 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1329 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1330 qntsty->nguardbits)) < 0)
1346 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1348 int ret = AVERROR_BUG;
1353 if (tile->poc.nb_poc) {
1354 for (i=0; i<tile->poc.nb_poc; i++) {
1355 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1356 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1358 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1360 FFMIN(e->CEpoc, s->ncomponents),
1367 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1369 tile->codsty[0].nlayers,
1372 tile->codsty[0].prog_order,
1376 /* EOC marker reached */
1377 bytestream2_skip(&s->g, 2);
1382 /* TIER-1 routines */
1383 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1384 int bpno, int bandno,
1385 int vert_causal_ctx_csty_symbol)
1387 int mask = 3 << (bpno - 1), y0, x, y;
1389 for (y0 = 0; y0 < height; y0 += 4)
1390 for (x = 0; x < width; x++)
1391 for (y = y0; y < height && y < y0 + 4; y++) {
1392 int flags_mask = -1;
1393 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1394 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1395 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1396 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1397 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1398 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1400 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1402 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1405 ff_jpeg2000_set_significance(t1, x, y,
1406 t1->data[(y) * t1->stride + x] < 0);
1408 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1413 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1414 int bpno, int vert_causal_ctx_csty_symbol)
1419 phalf = 1 << (bpno - 1);
1422 for (y0 = 0; y0 < height; y0 += 4)
1423 for (x = 0; x < width; x++)
1424 for (y = y0; y < height && y < y0 + 4; y++)
1425 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1426 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1427 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1428 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1429 int r = ff_mqc_decode(&t1->mqc,
1430 t1->mqc.cx_states + ctxno)
1432 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1433 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1437 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1438 int width, int height, int bpno, int bandno,
1439 int seg_symbols, int vert_causal_ctx_csty_symbol)
1441 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1443 for (y0 = 0; y0 < height; y0 += 4) {
1444 for (x = 0; x < width; x++) {
1445 int flags_mask = -1;
1446 if (vert_causal_ctx_csty_symbol)
1447 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1448 if (y0 + 3 < height &&
1449 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1450 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1451 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1452 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1453 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1455 runlen = ff_mqc_decode(&t1->mqc,
1456 t1->mqc.cx_states + MQC_CX_UNI);
1457 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1466 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1467 int flags_mask = -1;
1468 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1469 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1471 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1472 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1478 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1480 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1481 t1->mqc.cx_states + ctxno) ^
1484 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1487 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1493 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1494 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1495 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1496 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1498 av_log(s->avctx, AV_LOG_ERROR,
1499 "Segmentation symbol value incorrect\n");
1503 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1504 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1505 int width, int height, int bandpos)
1507 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
1509 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1513 av_assert0(width <= 1024U && height <= 1024U);
1514 av_assert0(width*height <= 4096);
1516 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1518 /* If code-block contains no compressed data: nothing to do. */
1522 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1524 cblk->data[cblk->length] = 0xff;
1525 cblk->data[cblk->length+1] = 0xff;
1526 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1530 av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1531 return AVERROR_INVALIDDATA;
1535 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1536 vert_causal_ctx_csty_symbol);
1539 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1542 av_assert2(!t1->mqc.raw);
1543 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1544 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1545 vert_causal_ctx_csty_symbol);
1548 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1549 ff_mqc_init_contexts(&t1->mqc);
1551 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1552 if (term_cnt >= cblk->nb_terminations) {
1553 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1554 return AVERROR_INVALIDDATA;
1556 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1557 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1558 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1559 pass_cnt, cblk->npasses);
1562 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1573 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1574 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1575 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1581 /* TODO: Verify dequantization for lossless case
1582 * comp->data can be float or int
1583 * band->stepsize can be float or int
1584 * depending on the type of DWT transformation.
1585 * see ISO/IEC 15444-1:2002 A.6.1 */
1587 /* Float dequantization of a codeblock.*/
1588 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1589 Jpeg2000Component *comp,
1590 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1593 int w = cblk->coord[0][1] - cblk->coord[0][0];
1594 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1595 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1596 int *src = t1->data + j*t1->stride;
1597 for (i = 0; i < w; ++i)
1598 datap[i] = src[i] * band->f_stepsize;
1602 /* Integer dequantization of a codeblock.*/
1603 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1604 Jpeg2000Component *comp,
1605 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1608 int w = cblk->coord[0][1] - cblk->coord[0][0];
1609 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1610 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1611 int *src = t1->data + j*t1->stride;
1612 if (band->i_stepsize == 32768) {
1613 for (i = 0; i < w; ++i)
1614 datap[i] = src[i] / 2;
1616 // This should be VERY uncommon
1617 for (i = 0; i < w; ++i)
1618 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1623 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1624 Jpeg2000Component *comp,
1625 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1628 int w = cblk->coord[0][1] - cblk->coord[0][0];
1629 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1630 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1631 int *src = t1->data + j*t1->stride;
1632 for (i = 0; i < w; ++i)
1633 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1637 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1642 for (i = 1; i < 3; i++) {
1643 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1644 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1647 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1648 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1653 for (i = 0; i < 3; i++)
1654 if (tile->codsty[0].transform == FF_DWT97)
1655 src[i] = tile->comp[i].f_data;
1657 src[i] = tile->comp[i].i_data;
1659 for (i = 0; i < 2; i++)
1660 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1662 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1665 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1667 Jpeg2000T1Context t1;
1669 int compno, reslevelno, bandno;
1671 /* Loop on tile components */
1672 for (compno = 0; compno < s->ncomponents; compno++) {
1673 Jpeg2000Component *comp = tile->comp + compno;
1674 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1676 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1678 /* Loop on resolution levels */
1679 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1680 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1682 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1683 int nb_precincts, precno;
1684 Jpeg2000Band *band = rlevel->band + bandno;
1685 int cblkno = 0, bandpos;
1687 bandpos = bandno + (reslevelno > 0);
1689 if (band->coord[0][0] == band->coord[0][1] ||
1690 band->coord[1][0] == band->coord[1][1])
1693 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1694 /* Loop on precincts */
1695 for (precno = 0; precno < nb_precincts; precno++) {
1696 Jpeg2000Prec *prec = band->prec + precno;
1698 /* Loop on codeblocks */
1700 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1703 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1704 decode_cblk(s, codsty, &t1, cblk,
1705 cblk->coord[0][1] - cblk->coord[0][0],
1706 cblk->coord[1][1] - cblk->coord[1][0],
1709 x = cblk->coord[0][0] - band->coord[0][0];
1710 y = cblk->coord[1][0] - band->coord[1][0];
1712 if (codsty->transform == FF_DWT97)
1713 dequantization_float(x, y, cblk, comp, &t1, band);
1714 else if (codsty->transform == FF_DWT97_INT)
1715 dequantization_int_97(x, y, cblk, comp, &t1, band);
1717 dequantization_int(x, y, cblk, comp, &t1, band);
1721 } /* end reslevel */
1724 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1728 #define WRITE_FRAME(D, PIXEL) \
1729 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1730 AVFrame * picture, int precision) \
1732 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1733 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1734 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1739 for (compno = 0; compno < s->ncomponents; compno++) { \
1740 Jpeg2000Component *comp = tile->comp + compno; \
1741 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1743 float *datap = comp->f_data; \
1744 int32_t *i_datap = comp->i_data; \
1745 int cbps = s->cbps[compno]; \
1746 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1750 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1752 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno]; \
1753 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1754 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) { \
1757 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno]; \
1758 dst = line + x * pixelsize + compno*!planar; \
1760 if (codsty->transform == FF_DWT97) { \
1761 for (; x < w; x++) { \
1762 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1763 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1764 val = av_clip(val, 0, (1 << cbps) - 1); \
1765 *dst = val << (precision - cbps); \
1770 for (; x < w; x++) { \
1771 int val = *i_datap + (1 << (cbps - 1)); \
1772 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1773 val = av_clip(val, 0, (1 << cbps) - 1); \
1774 *dst = val << (precision - cbps); \
1779 line += picture->linesize[plane] / sizeof(PIXEL); \
1785 WRITE_FRAME(8, uint8_t)
1786 WRITE_FRAME(16, uint16_t)
1790 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1791 int jobnr, int threadnr)
1793 Jpeg2000DecoderContext *s = avctx->priv_data;
1794 AVFrame *picture = td;
1795 Jpeg2000Tile *tile = s->tile + jobnr;
1798 tile_codeblocks(s, tile);
1800 /* inverse MCT transformation */
1801 if (tile->codsty[0].mct)
1802 mct_decode(s, tile);
1804 for (x = 0; x < s->ncomponents; x++) {
1805 if (s->cdef[x] < 0) {
1806 for (x = 0; x < s->ncomponents; x++) {
1809 if ((s->ncomponents & 1) == 0)
1810 s->cdef[s->ncomponents-1] = 0;
1815 if (s->precision <= 8) {
1816 write_frame_8(s, tile, picture, 8);
1818 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1819 picture->format == AV_PIX_FMT_RGB48 ||
1820 picture->format == AV_PIX_FMT_RGBA64 ||
1821 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1823 write_frame_16(s, tile, picture, precision);
1829 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1832 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1833 if (s->tile[tileno].comp) {
1834 for (compno = 0; compno < s->ncomponents; compno++) {
1835 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1836 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1838 ff_jpeg2000_cleanup(comp, codsty);
1840 av_freep(&s->tile[tileno].comp);
1844 memset(s->codsty, 0, sizeof(s->codsty));
1845 memset(s->qntsty, 0, sizeof(s->qntsty));
1846 memset(s->properties, 0, sizeof(s->properties));
1847 memset(&s->poc , 0, sizeof(s->poc));
1848 s->numXtiles = s->numYtiles = 0;
1852 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1854 Jpeg2000CodingStyle *codsty = s->codsty;
1855 Jpeg2000QuantStyle *qntsty = s->qntsty;
1856 Jpeg2000POC *poc = &s->poc;
1857 uint8_t *properties = s->properties;
1864 if (bytestream2_get_bytes_left(&s->g) < 2) {
1865 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1869 marker = bytestream2_get_be16u(&s->g);
1870 oldpos = bytestream2_tell(&s->g);
1872 if (marker == JPEG2000_SOD) {
1874 Jpeg2000TilePart *tp;
1877 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1878 return AVERROR_INVALIDDATA;
1880 if (s->curtileno < 0) {
1881 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1882 return AVERROR_INVALIDDATA;
1885 tile = s->tile + s->curtileno;
1886 tp = tile->tile_part + tile->tp_idx;
1887 if (tp->tp_end < s->g.buffer) {
1888 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1889 return AVERROR_INVALIDDATA;
1891 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1892 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1896 if (marker == JPEG2000_EOC)
1899 len = bytestream2_get_be16(&s->g);
1900 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
1901 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
1902 return AVERROR_INVALIDDATA;
1907 if (s->ncomponents) {
1908 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
1909 return AVERROR_INVALIDDATA;
1913 s->numXtiles = s->numYtiles = 0;
1916 ret = get_coc(s, codsty, properties);
1919 ret = get_cod(s, codsty, properties);
1922 ret = get_qcc(s, len, qntsty, properties);
1925 ret = get_qcd(s, len, qntsty, properties);
1928 ret = get_poc(s, len, poc);
1931 if (!(ret = get_sot(s, len))) {
1932 av_assert1(s->curtileno >= 0);
1933 codsty = s->tile[s->curtileno].codsty;
1934 qntsty = s->tile[s->curtileno].qntsty;
1935 poc = &s->tile[s->curtileno].poc;
1936 properties = s->tile[s->curtileno].properties;
1940 // the PLM marker is ignored
1942 // the comment is ignored
1943 bytestream2_skip(&s->g, len - 2);
1946 // Tile-part lengths
1947 ret = get_tlm(s, len);
1950 // Packet length, tile-part header
1951 ret = get_plt(s, len);
1954 av_log(s->avctx, AV_LOG_ERROR,
1955 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1956 marker, bytestream2_tell(&s->g) - 4);
1957 bytestream2_skip(&s->g, len - 2);
1960 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1961 av_log(s->avctx, AV_LOG_ERROR,
1962 "error during processing marker segment %.4"PRIx16"\n",
1964 return ret ? ret : -1;
1970 /* Read bit stream packets --> T2 operation. */
1971 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1976 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1977 Jpeg2000Tile *tile = s->tile + tileno;
1979 if ((ret = init_tile(s, tileno)) < 0)
1982 s->g = tile->tile_part[0].tpg;
1983 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
1990 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1992 uint32_t atom_size, atom, atom_end;
1993 int search_range = 10;
1997 bytestream2_get_bytes_left(&s->g) >= 8) {
1998 atom_size = bytestream2_get_be32u(&s->g);
1999 atom = bytestream2_get_be32u(&s->g);
2000 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2002 if (atom == JP2_CODESTREAM)
2005 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2008 if (atom == JP2_HEADER &&
2010 uint32_t atom2_size, atom2, atom2_end;
2012 atom2_size = bytestream2_get_be32u(&s->g);
2013 atom2 = bytestream2_get_be32u(&s->g);
2014 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2015 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2018 if (atom2 == JP2_CODESTREAM) {
2020 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2021 int method = bytestream2_get_byteu(&s->g);
2022 bytestream2_skipu(&s->g, 2);
2024 s->colour_space = bytestream2_get_be32u(&s->g);
2026 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2027 int i, size, colour_count, colour_channels, colour_depth[3];
2029 colour_count = bytestream2_get_be16u(&s->g);
2030 colour_channels = bytestream2_get_byteu(&s->g);
2031 // FIXME: Do not ignore channel_sign
2032 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2033 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2034 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2035 size = (colour_depth[0] + 7 >> 3) * colour_count +
2036 (colour_depth[1] + 7 >> 3) * colour_count +
2037 (colour_depth[2] + 7 >> 3) * colour_count;
2038 if (colour_count > 256 ||
2039 colour_channels != 3 ||
2040 colour_depth[0] > 16 ||
2041 colour_depth[1] > 16 ||
2042 colour_depth[2] > 16 ||
2043 atom2_size < size) {
2044 avpriv_request_sample(s->avctx, "Unknown palette");
2045 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2049 for (i = 0; i < colour_count; i++) {
2050 if (colour_depth[0] <= 8) {
2051 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2052 r |= r >> colour_depth[0];
2054 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2056 if (colour_depth[1] <= 8) {
2057 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2058 g |= g >> colour_depth[1];
2060 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2062 if (colour_depth[2] <= 8) {
2063 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2064 b |= b >> colour_depth[2];
2066 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2068 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2070 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2071 int n = bytestream2_get_be16u(&s->g);
2073 int cn = bytestream2_get_be16(&s->g);
2074 int av_unused typ = bytestream2_get_be16(&s->g);
2075 int asoc = bytestream2_get_be16(&s->g);
2076 if (cn < 4 && asoc < 4)
2079 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2080 int64_t vnum, vden, hnum, hden, vexp, hexp;
2082 bytestream2_skip(&s->g, 4);
2083 resx = bytestream2_get_be32u(&s->g);
2084 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2085 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2088 vnum = bytestream2_get_be16u(&s->g);
2089 vden = bytestream2_get_be16u(&s->g);
2090 hnum = bytestream2_get_be16u(&s->g);
2091 hden = bytestream2_get_be16u(&s->g);
2092 vexp = bytestream2_get_byteu(&s->g);
2093 hexp = bytestream2_get_byteu(&s->g);
2094 if (!vnum || !vden || !hnum || !hden) {
2095 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2096 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2106 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2107 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2108 av_reduce(&s->sar.den, &s->sar.num,
2109 hnum * vden * pow(10, hexp),
2110 vnum * hden * pow(10, vexp),
2113 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2114 } while (atom_end - atom2_end >= 8);
2118 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2124 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2126 Jpeg2000DecoderContext *s = avctx->priv_data;
2128 ff_jpeg2000dsp_init(&s->dsp);
2133 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2134 int *got_frame, AVPacket *avpkt)
2136 Jpeg2000DecoderContext *s = avctx->priv_data;
2137 ThreadFrame frame = { .f = data };
2138 AVFrame *picture = data;
2142 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2144 memset(s->cdef, -1, sizeof(s->cdef));
2146 if (bytestream2_get_bytes_left(&s->g) < 2) {
2147 ret = AVERROR_INVALIDDATA;
2151 // check if the image is in jp2 format
2152 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2153 (bytestream2_get_be32u(&s->g) == 12) &&
2154 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2155 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2156 if (!jp2_find_codestream(s)) {
2157 av_log(avctx, AV_LOG_ERROR,
2158 "Could not find Jpeg2000 codestream atom.\n");
2159 ret = AVERROR_INVALIDDATA;
2163 bytestream2_seek(&s->g, 0, SEEK_SET);
2166 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2167 bytestream2_skip(&s->g, 1);
2169 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2170 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2171 ret = AVERROR_INVALIDDATA;
2174 if (ret = jpeg2000_read_main_headers(s))
2177 /* get picture buffer */
2178 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2180 picture->pict_type = AV_PICTURE_TYPE_I;
2181 picture->key_frame = 1;
2183 if (ret = jpeg2000_read_bitstream_packets(s))
2186 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2188 jpeg2000_dec_cleanup(s);
2192 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2193 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2194 if (s->sar.num && s->sar.den)
2195 avctx->sample_aspect_ratio = s->sar;
2196 s->sar.num = s->sar.den = 0;
2198 return bytestream2_tell(&s->g);
2201 jpeg2000_dec_cleanup(s);
2205 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
2207 ff_jpeg2000_init_tier1_luts();
2208 ff_mqc_init_context_tables();
2211 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2212 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2214 static const AVOption options[] = {
2215 { "lowres", "Lower the decoding resolution by a power of two",
2216 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2220 static const AVClass jpeg2000_class = {
2221 .class_name = "jpeg2000",
2222 .item_name = av_default_item_name,
2224 .version = LIBAVUTIL_VERSION_INT,
2227 AVCodec ff_jpeg2000_decoder = {
2229 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2230 .type = AVMEDIA_TYPE_VIDEO,
2231 .id = AV_CODEC_ID_JPEG2000,
2232 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2233 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2234 .init_static_data = jpeg2000_init_static_data,
2235 .init = jpeg2000_decode_init,
2236 .decode = jpeg2000_decode_frame,
2237 .priv_class = &jpeg2000_class,
2239 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)