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
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/common.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
36 #include "bytestream.h"
40 #include "jpeg2000dsp.h"
42 #define JP2_SIG_TYPE 0x6A502020
43 #define JP2_SIG_VALUE 0x0D0A870A
44 #define JP2_CODESTREAM 0x6A703263
45 #define JP2_HEADER 0x6A703268
52 typedef struct Jpeg2000POCEntry {
61 typedef struct Jpeg2000POC {
62 Jpeg2000POCEntry poc[MAX_POCS];
67 typedef struct Jpeg2000TilePart {
68 uint8_t tile_index; // Tile index who refers the tile-part
69 const uint8_t *tp_end;
70 GetByteContext tpg; // bit stream in tile-part
73 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
74 * one per component, so tile_part elements have a size of 3 */
75 typedef struct Jpeg2000Tile {
76 Jpeg2000Component *comp;
77 uint8_t properties[4];
78 Jpeg2000CodingStyle codsty[4];
79 Jpeg2000QuantStyle qntsty[4];
81 Jpeg2000TilePart tile_part[256];
82 uint16_t tp_idx; // Tile-part index
83 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
86 typedef struct Jpeg2000DecoderContext {
88 AVCodecContext *avctx;
92 int image_offset_x, image_offset_y;
93 int tile_offset_x, tile_offset_y;
94 uint8_t cbps[4]; // bits per sample in particular components
95 uint8_t sgnd[4]; // if a component is signed
96 uint8_t properties[4];
101 uint32_t palette[256];
104 int tile_width, tile_height;
105 unsigned numXtiles, numYtiles;
108 Jpeg2000CodingStyle codsty[4];
109 Jpeg2000QuantStyle qntsty[4];
117 Jpeg2000DSPContext dsp;
119 /*options parameters*/
120 int reduction_factor;
121 } Jpeg2000DecoderContext;
123 /* get_bits functions for JPEG2000 packet bitstream
124 * It is a get_bit function with a bit-stuffing routine. If the value of the
125 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
126 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
127 static int get_bits(Jpeg2000DecoderContext *s, int n)
133 if (s->bit_index == 0) {
134 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
137 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
142 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
144 if (bytestream2_get_byte(&s->g) == 0xff)
145 bytestream2_skip(&s->g, 1);
149 /* decode the value stored in node */
150 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
153 Jpeg2000TgtNode *stack[30];
154 int sp = -1, curval = 0;
157 av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
158 return AVERROR_INVALIDDATA;
161 while (node && !node->vis) {
169 curval = stack[sp]->val;
171 while (curval < threshold && sp >= 0) {
172 if (curval < stack[sp]->val)
173 curval = stack[sp]->val;
174 while (curval < threshold) {
176 if ((ret = get_bits(s, 1)) > 0) {
184 stack[sp]->val = curval;
190 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
191 int bpc, uint32_t log2_chroma_wh, int pal8)
194 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
198 if (desc->nb_components != components) {
202 switch (components) {
204 match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
205 (log2_chroma_wh >> 14 & 3) == 0 &&
206 (log2_chroma_wh >> 12 & 3) == 0;
208 match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
209 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
210 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
212 match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
213 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
214 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
217 match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
218 (log2_chroma_wh >> 2 & 3) == 0 &&
219 (log2_chroma_wh & 3) == 0 &&
220 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
225 // pix_fmts with lower bpp have to be listed before
226 // similar pix_fmts with higher bpp.
227 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
228 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
229 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
230 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
231 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
232 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
233 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
234 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
235 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
236 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
237 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
238 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
239 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
240 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
242 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
243 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
244 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
245 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS,
247 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
252 /* marker segments */
253 /* get sizes and offsets of image, tiles; number of components */
254 static int get_siz(Jpeg2000DecoderContext *s)
258 uint32_t log2_chroma_wh = 0;
259 const enum AVPixelFormat *possible_fmts = NULL;
260 int possible_fmts_nb = 0;
262 if (bytestream2_get_bytes_left(&s->g) < 36) {
263 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
264 return AVERROR_INVALIDDATA;
267 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
268 s->width = bytestream2_get_be32u(&s->g); // Width
269 s->height = bytestream2_get_be32u(&s->g); // Height
270 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
271 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
272 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
273 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
274 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
275 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
276 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
278 if (s->image_offset_x || s->image_offset_y) {
279 avpriv_request_sample(s->avctx, "Support for image offsets");
280 return AVERROR_PATCHWELCOME;
283 if (ncomponents <= 0) {
284 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
286 return AVERROR_INVALIDDATA;
289 if (ncomponents > 4) {
290 avpriv_request_sample(s->avctx, "Support for %d components",
292 return AVERROR_PATCHWELCOME;
295 s->ncomponents = ncomponents;
297 if (s->tile_width <= 0 || s->tile_height <= 0) {
298 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
299 s->tile_width, s->tile_height);
300 return AVERROR_INVALIDDATA;
303 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
304 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
305 return AVERROR_INVALIDDATA;
308 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
309 uint8_t x = bytestream2_get_byteu(&s->g);
310 s->cbps[i] = (x & 0x7f) + 1;
311 s->precision = FFMAX(s->cbps[i], s->precision);
312 s->sgnd[i] = !!(x & 0x80);
313 s->cdx[i] = bytestream2_get_byteu(&s->g);
314 s->cdy[i] = bytestream2_get_byteu(&s->g);
315 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
316 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
317 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
318 return AVERROR_INVALIDDATA;
320 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
323 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
324 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
326 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
327 s->numXtiles = s->numYtiles = 0;
328 return AVERROR(EINVAL);
331 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
333 s->numXtiles = s->numYtiles = 0;
334 return AVERROR(ENOMEM);
337 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
338 Jpeg2000Tile *tile = s->tile + i;
340 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
342 return AVERROR(ENOMEM);
345 /* compute image size with reduction factor */
346 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
347 s->reduction_factor);
348 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
349 s->reduction_factor);
351 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
352 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
353 possible_fmts = xyz_pix_fmts;
354 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
356 switch (s->colour_space) {
358 possible_fmts = rgb_pix_fmts;
359 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
362 possible_fmts = gray_pix_fmts;
363 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
366 possible_fmts = yuv_pix_fmts;
367 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
370 possible_fmts = all_pix_fmts;
371 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
375 for (i = 0; i < possible_fmts_nb; ++i) {
376 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
377 s->avctx->pix_fmt = possible_fmts[i];
382 if (i == possible_fmts_nb) {
383 if (ncomponents == 4 &&
384 s->cdy[0] == 1 && s->cdx[0] == 1 &&
385 s->cdy[1] == 1 && s->cdx[1] == 1 &&
386 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
387 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
388 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
399 if (i == possible_fmts_nb) {
400 av_log(s->avctx, AV_LOG_ERROR,
401 "Unknown pix_fmt, profile: %d, colour_space: %d, "
402 "components: %d, precision: %d\n"
403 "cdx[0]: %d, cdy[0]: %d\n"
404 "cdx[1]: %d, cdy[1]: %d\n"
405 "cdx[2]: %d, cdy[2]: %d\n"
406 "cdx[3]: %d, cdy[3]: %d\n",
407 s->avctx->profile, s->colour_space, ncomponents, s->precision,
410 ncomponents > 1 ? s->cdx[1] : 0,
411 ncomponents > 1 ? s->cdy[1] : 0,
412 ncomponents > 2 ? s->cdx[2] : 0,
413 ncomponents > 2 ? s->cdy[2] : 0,
414 ncomponents > 3 ? s->cdx[3] : 0,
415 ncomponents > 3 ? s->cdy[3] : 0);
416 return AVERROR_PATCHWELCOME;
418 s->avctx->bits_per_raw_sample = s->precision;
422 /* get common part for COD and COC segments */
423 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
427 if (bytestream2_get_bytes_left(&s->g) < 5) {
428 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
429 return AVERROR_INVALIDDATA;
432 /* nreslevels = number of resolution levels
433 = number of decomposition level +1 */
434 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
435 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
436 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
437 return AVERROR_INVALIDDATA;
440 if (c->nreslevels <= s->reduction_factor) {
441 /* we are forced to update reduction_factor as its requested value is
442 not compatible with this bitstream, and as we might have used it
443 already in setup earlier we have to fail this frame until
444 reinitialization is implemented */
445 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
446 s->reduction_factor = c->nreslevels - 1;
447 return AVERROR(EINVAL);
450 /* compute number of resolution levels to decode */
451 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
453 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
454 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
456 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
457 c->log2_cblk_width + c->log2_cblk_height > 12) {
458 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
459 return AVERROR_INVALIDDATA;
462 c->cblk_style = bytestream2_get_byteu(&s->g);
463 if (c->cblk_style != 0) { // cblk style
464 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
465 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
466 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
468 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
469 /* set integer 9/7 DWT in case of BITEXACT flag */
470 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
471 c->transform = FF_DWT97_INT;
472 else if (c->transform == FF_DWT53) {
473 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
476 if (c->csty & JPEG2000_CSTY_PREC) {
478 for (i = 0; i < c->nreslevels; i++) {
479 byte = bytestream2_get_byte(&s->g);
480 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
481 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
483 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
484 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
485 c->log2_prec_widths[i], c->log2_prec_heights[i]);
486 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
487 return AVERROR_INVALIDDATA;
491 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
492 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
497 /* get coding parameters for a particular tile or whole image*/
498 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
501 Jpeg2000CodingStyle tmp;
504 if (bytestream2_get_bytes_left(&s->g) < 5) {
505 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
506 return AVERROR_INVALIDDATA;
509 tmp.csty = bytestream2_get_byteu(&s->g);
511 // get progression order
512 tmp.prog_order = bytestream2_get_byteu(&s->g);
514 tmp.nlayers = bytestream2_get_be16u(&s->g);
515 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
517 if (tmp.mct && s->ncomponents < 3) {
518 av_log(s->avctx, AV_LOG_ERROR,
519 "MCT %"PRIu8" with too few components (%d)\n",
520 tmp.mct, s->ncomponents);
521 return AVERROR_INVALIDDATA;
524 if ((ret = get_cox(s, &tmp)) < 0)
527 for (compno = 0; compno < s->ncomponents; compno++)
528 if (!(properties[compno] & HAD_COC))
529 memcpy(c + compno, &tmp, sizeof(tmp));
533 /* Get coding parameters for a component in the whole image or a
534 * particular tile. */
535 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
540 if (bytestream2_get_bytes_left(&s->g) < 2) {
541 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
542 return AVERROR_INVALIDDATA;
545 compno = bytestream2_get_byteu(&s->g);
547 if (compno >= s->ncomponents) {
548 av_log(s->avctx, AV_LOG_ERROR,
549 "Invalid compno %d. There are %d components in the image.\n",
550 compno, s->ncomponents);
551 return AVERROR_INVALIDDATA;
555 c->csty = bytestream2_get_byteu(&s->g);
557 if ((ret = get_cox(s, c)) < 0)
560 properties[compno] |= HAD_COC;
564 /* Get common part for QCD and QCC segments. */
565 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
569 if (bytestream2_get_bytes_left(&s->g) < 1)
570 return AVERROR_INVALIDDATA;
572 x = bytestream2_get_byteu(&s->g); // Sqcd
574 q->nguardbits = x >> 5;
575 q->quantsty = x & 0x1f;
577 if (q->quantsty == JPEG2000_QSTY_NONE) {
579 if (bytestream2_get_bytes_left(&s->g) < n ||
580 n > JPEG2000_MAX_DECLEVELS*3)
581 return AVERROR_INVALIDDATA;
582 for (i = 0; i < n; i++)
583 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
584 } else if (q->quantsty == JPEG2000_QSTY_SI) {
585 if (bytestream2_get_bytes_left(&s->g) < 2)
586 return AVERROR_INVALIDDATA;
587 x = bytestream2_get_be16u(&s->g);
588 q->expn[0] = x >> 11;
589 q->mant[0] = x & 0x7ff;
590 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
591 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
592 q->expn[i] = curexpn;
593 q->mant[i] = q->mant[0];
597 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
598 n > JPEG2000_MAX_DECLEVELS*3)
599 return AVERROR_INVALIDDATA;
600 for (i = 0; i < n; i++) {
601 x = bytestream2_get_be16u(&s->g);
602 q->expn[i] = x >> 11;
603 q->mant[i] = x & 0x7ff;
609 /* Get quantization parameters for a particular tile or a whole image. */
610 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
613 Jpeg2000QuantStyle tmp;
616 memset(&tmp, 0, sizeof(tmp));
618 if ((ret = get_qcx(s, n, &tmp)) < 0)
620 for (compno = 0; compno < s->ncomponents; compno++)
621 if (!(properties[compno] & HAD_QCC))
622 memcpy(q + compno, &tmp, sizeof(tmp));
626 /* Get quantization parameters for a component in the whole image
627 * on in a particular tile. */
628 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
633 if (bytestream2_get_bytes_left(&s->g) < 1)
634 return AVERROR_INVALIDDATA;
636 compno = bytestream2_get_byteu(&s->g);
638 if (compno >= s->ncomponents) {
639 av_log(s->avctx, AV_LOG_ERROR,
640 "Invalid compno %d. There are %d components in the image.\n",
641 compno, s->ncomponents);
642 return AVERROR_INVALIDDATA;
645 properties[compno] |= HAD_QCC;
646 return get_qcx(s, n - 1, q + compno);
649 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
652 int elem_size = s->ncomponents <= 257 ? 7 : 9;
653 Jpeg2000POC tmp = {{{0}}};
655 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
656 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
657 return AVERROR_INVALIDDATA;
661 avpriv_request_sample(s->avctx, "Fat POC not supported\n");
662 return AVERROR_PATCHWELCOME;
665 tmp.nb_poc = (size - 2) / elem_size;
666 if (tmp.nb_poc > MAX_POCS) {
667 avpriv_request_sample(s->avctx, "Too many POCs (%d)\n", tmp.nb_poc);
668 return AVERROR_PATCHWELCOME;
671 for (i = 0; i<tmp.nb_poc; i++) {
672 Jpeg2000POCEntry *e = &tmp.poc[i];
673 e->RSpoc = bytestream2_get_byteu(&s->g);
674 e->CSpoc = bytestream2_get_byteu(&s->g);
675 e->LYEpoc = bytestream2_get_be16u(&s->g);
676 e->REpoc = bytestream2_get_byteu(&s->g);
677 e->CEpoc = bytestream2_get_byteu(&s->g);
678 e->Ppoc = bytestream2_get_byteu(&s->g);
681 if (e->CEpoc > s->ncomponents)
682 e->CEpoc = s->ncomponents;
683 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
684 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
686 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
687 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
689 return AVERROR_INVALIDDATA;
693 if (!p->nb_poc || p->is_default) {
696 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
697 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
698 return AVERROR_INVALIDDATA;
700 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
701 p->nb_poc += tmp.nb_poc;
710 /* Get start of tile segment. */
711 static int get_sot(Jpeg2000DecoderContext *s, int n)
713 Jpeg2000TilePart *tp;
718 if (bytestream2_get_bytes_left(&s->g) < 8)
719 return AVERROR_INVALIDDATA;
722 Isot = bytestream2_get_be16u(&s->g); // Isot
723 if (Isot >= s->numXtiles * s->numYtiles)
724 return AVERROR_INVALIDDATA;
727 Psot = bytestream2_get_be32u(&s->g); // Psot
728 TPsot = bytestream2_get_byteu(&s->g); // TPsot
730 /* Read TNSot but not used */
731 bytestream2_get_byteu(&s->g); // TNsot
734 Psot = bytestream2_get_bytes_left(&s->g) + n + 2;
736 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
737 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
738 return AVERROR_INVALIDDATA;
741 av_assert0(TPsot < FF_ARRAY_ELEMS(s->tile[Isot].tile_part));
743 s->tile[Isot].tp_idx = TPsot;
744 tp = s->tile[Isot].tile_part + TPsot;
745 tp->tile_index = Isot;
746 tp->tp_end = s->g.buffer + Psot - n - 2;
749 Jpeg2000Tile *tile = s->tile + s->curtileno;
752 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
753 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
754 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
755 tile->poc.is_default = 1;
761 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
762 * Used to know the number of tile parts and lengths.
763 * There may be multiple TLMs in the header.
764 * TODO: The function is not used for tile-parts management, nor anywhere else.
765 * It can be useful to allocate memory for tile parts, before managing the SOT
766 * markers. Parsing the TLM header is needed to increment the input header
768 * This marker is mandatory for DCI. */
769 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
771 uint8_t Stlm, ST, SP, tile_tlm, i;
772 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
773 Stlm = bytestream2_get_byte(&s->g);
775 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
776 ST = (Stlm >> 4) & 0x03;
777 // TODO: Manage case of ST = 0b11 --> raise error
778 SP = (Stlm >> 6) & 0x01;
779 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
780 for (i = 0; i < tile_tlm; i++) {
785 bytestream2_get_byte(&s->g);
788 bytestream2_get_be16(&s->g);
791 bytestream2_get_be32(&s->g);
795 bytestream2_get_be16(&s->g);
797 bytestream2_get_be32(&s->g);
803 static uint8_t get_plt(Jpeg2000DecoderContext *s, int n)
807 av_log(s->avctx, AV_LOG_DEBUG,
808 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
810 /*Zplt =*/ bytestream2_get_byte(&s->g);
812 for (i = 0; i < n - 3; i++) {
813 bytestream2_get_byte(&s->g);
819 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
822 int tilex = tileno % s->numXtiles;
823 int tiley = tileno / s->numXtiles;
824 Jpeg2000Tile *tile = s->tile + tileno;
827 return AVERROR(ENOMEM);
829 tile->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
830 tile->coord[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
831 tile->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
832 tile->coord[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
834 for (compno = 0; compno < s->ncomponents; compno++) {
835 Jpeg2000Component *comp = tile->comp + compno;
836 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
837 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
838 int ret; // global bandno
840 comp->coord_o[0][0] = tile->coord[0][0];
841 comp->coord_o[0][1] = tile->coord[0][1];
842 comp->coord_o[1][0] = tile->coord[1][0];
843 comp->coord_o[1][1] = tile->coord[1][1];
845 comp->coord_o[0][0] /= s->cdx[compno];
846 comp->coord_o[0][1] /= s->cdx[compno];
847 comp->coord_o[1][0] /= s->cdy[compno];
848 comp->coord_o[1][1] /= s->cdy[compno];
851 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
852 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
853 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
854 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
856 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
857 s->cbps[compno], s->cdx[compno],
858 s->cdy[compno], s->avctx))
864 /* Read the number of coding passes. */
865 static int getnpasses(Jpeg2000DecoderContext *s)
872 if ((num = get_bits(s, 2)) != 3)
873 return num < 0 ? num : 3 + num;
874 if ((num = get_bits(s, 5)) != 31)
875 return num < 0 ? num : 6 + num;
876 num = get_bits(s, 7);
877 return num < 0 ? num : 37 + num;
880 static int getlblockinc(Jpeg2000DecoderContext *s)
883 while (ret = get_bits(s, 1)) {
891 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
892 Jpeg2000CodingStyle *codsty,
893 Jpeg2000ResLevel *rlevel, int precno,
894 int layno, uint8_t *expn, int numgbits)
896 int bandno, cblkno, ret, nb_code_blocks;
899 if (layno < rlevel->band[0].prec[precno].decoded_layers)
901 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
903 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
904 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
905 s->g = tile->tile_part[++(*tp_index)].tpg;
909 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
910 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
912 if (!(ret = get_bits(s, 1))) {
918 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
919 Jpeg2000Band *band = rlevel->band + bandno;
920 Jpeg2000Prec *prec = band->prec + precno;
922 if (band->coord[0][0] == band->coord[0][1] ||
923 band->coord[1][0] == band->coord[1][1])
925 nb_code_blocks = prec->nb_codeblocks_height *
926 prec->nb_codeblocks_width;
927 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
928 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
929 int incl, newpasses, llen;
932 incl = get_bits(s, 1);
934 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
940 if (!cblk->npasses) {
941 int v = expn[bandno] + numgbits - 1 -
942 tag_tree_decode(s, prec->zerobits + cblkno, 100);
944 av_log(s->avctx, AV_LOG_ERROR,
945 "nonzerobits %d invalid\n", v);
946 return AVERROR_INVALIDDATA;
948 cblk->nonzerobits = v;
950 if ((newpasses = getnpasses(s)) < 0)
952 av_assert2(newpasses > 0);
953 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
954 avpriv_request_sample(s->avctx, "Too many passes\n");
955 return AVERROR_PATCHWELCOME;
957 if ((llen = getlblockinc(s)) < 0)
959 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
960 avpriv_request_sample(s->avctx,
961 "Block with length beyond 16 bits\n");
962 return AVERROR_PATCHWELCOME;
965 cblk->lblock += llen;
967 cblk->nb_lengthinc = 0;
968 cblk->nb_terminationsinc = 0;
972 while (newpasses1 < newpasses) {
974 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
975 cblk->nb_terminationsinc ++;
980 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
982 if (ret > sizeof(cblk->data)) {
983 avpriv_request_sample(s->avctx,
984 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
986 return AVERROR_PATCHWELCOME;
988 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
989 cblk->npasses += newpasses1;
990 newpasses -= newpasses1;
996 if (codsty->csty & JPEG2000_CSTY_EPH) {
997 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
998 bytestream2_skip(&s->g, 2);
1000 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1003 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1004 Jpeg2000Band *band = rlevel->band + bandno;
1005 Jpeg2000Prec *prec = band->prec + precno;
1007 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1008 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1009 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1010 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1011 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1012 || sizeof(cblk->data) < cblk->length + cblk->lengthinc[cwsno] + 4
1014 av_log(s->avctx, AV_LOG_ERROR,
1015 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1016 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1017 return AVERROR_INVALIDDATA;
1020 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1021 cblk->length += cblk->lengthinc[cwsno];
1022 cblk->lengthinc[cwsno] = 0;
1023 if (cblk->nb_terminationsinc) {
1024 cblk->nb_terminationsinc--;
1025 cblk->nb_terminations++;
1026 cblk->data[cblk->length++] = 0xFF;
1027 cblk->data[cblk->length++] = 0xFF;
1028 cblk->data_start[cblk->nb_terminations] = cblk->length;
1036 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1037 int RSpoc, int CSpoc,
1038 int LYEpoc, int REpoc, int CEpoc,
1039 int Ppoc, int *tp_index)
1042 int layno, reslevelno, compno, precno, ok_reslevel;
1047 case JPEG2000_PGOD_RLCP:
1048 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1050 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1052 for (layno = 0; layno < LYEpoc; layno++) {
1053 for (compno = CSpoc; compno < CEpoc; compno++) {
1054 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1055 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1056 if (reslevelno < codsty->nreslevels) {
1057 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1060 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1061 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1064 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1065 qntsty->nguardbits)) < 0)
1073 case JPEG2000_PGOD_LRCP:
1074 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1075 for (layno = 0; layno < LYEpoc; layno++) {
1077 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1079 for (compno = CSpoc; compno < CEpoc; compno++) {
1080 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1081 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1082 if (reslevelno < codsty->nreslevels) {
1083 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1086 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1087 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1090 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1091 qntsty->nguardbits)) < 0)
1099 case JPEG2000_PGOD_CPRL:
1100 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1101 for (compno = CSpoc; compno < CEpoc; compno++) {
1102 Jpeg2000Component *comp = tile->comp + compno;
1103 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1104 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1108 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1109 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1110 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1111 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1112 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1117 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1118 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1119 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1120 unsigned prcx, prcy;
1121 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1122 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1123 int xc = x / s->cdx[compno];
1124 int yc = y / s->cdy[compno];
1126 if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1129 if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1132 // check if a precinct exists
1133 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1134 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1135 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1136 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1138 precno = prcx + rlevel->num_precincts_x * prcy;
1140 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1141 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1142 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1146 for (layno = 0; layno < LYEpoc; layno++) {
1147 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1149 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1150 qntsty->nguardbits)) < 0)
1159 case JPEG2000_PGOD_RPCL:
1160 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1162 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1166 for (compno = CSpoc; compno < CEpoc; compno++) {
1167 Jpeg2000Component *comp = tile->comp + compno;
1168 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1170 if (reslevelno < codsty->nreslevels) {
1171 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1172 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1173 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1174 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1180 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1181 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1182 for (compno = CSpoc; compno < CEpoc; compno++) {
1183 Jpeg2000Component *comp = tile->comp + compno;
1184 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1185 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1186 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1187 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1188 unsigned prcx, prcy;
1190 int xc = x / s->cdx[compno];
1191 int yc = y / s->cdy[compno];
1193 if (reslevelno >= codsty->nreslevels)
1196 if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1199 if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1202 // check if a precinct exists
1203 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1204 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1205 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1206 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1208 precno = prcx + rlevel->num_precincts_x * prcy;
1211 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1212 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1213 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1217 for (layno = 0; layno < LYEpoc; layno++) {
1218 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1221 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1222 qntsty->nguardbits)) < 0)
1231 case JPEG2000_PGOD_PCRL:
1232 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1235 for (compno = CSpoc; compno < CEpoc; compno++) {
1236 Jpeg2000Component *comp = tile->comp + compno;
1237 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1239 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1240 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1241 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1242 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1243 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1249 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1250 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1251 for (compno = CSpoc; compno < CEpoc; compno++) {
1252 Jpeg2000Component *comp = tile->comp + compno;
1253 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1254 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1255 int xc = x / s->cdx[compno];
1256 int yc = y / s->cdy[compno];
1258 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1259 unsigned prcx, prcy;
1260 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1261 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1263 if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1266 if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1269 // check if a precinct exists
1270 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1271 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1272 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1273 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1275 precno = prcx + rlevel->num_precincts_x * prcy;
1277 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1278 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1279 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1283 for (layno = 0; layno < LYEpoc; layno++) {
1284 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1286 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1287 qntsty->nguardbits)) < 0)
1303 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1309 if (tile->poc.nb_poc) {
1310 for (i=0; i<tile->poc.nb_poc; i++) {
1311 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1312 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1314 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1316 FFMIN(e->CEpoc, s->ncomponents),
1323 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1325 tile->codsty[0].nlayers,
1328 tile->codsty[0].prog_order,
1332 /* EOC marker reached */
1333 bytestream2_skip(&s->g, 2);
1338 /* TIER-1 routines */
1339 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1340 int bpno, int bandno,
1341 int vert_causal_ctx_csty_symbol)
1343 int mask = 3 << (bpno - 1), y0, x, y;
1345 for (y0 = 0; y0 < height; y0 += 4)
1346 for (x = 0; x < width; x++)
1347 for (y = y0; y < height && y < y0 + 4; y++) {
1348 int flags_mask = -1;
1349 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1350 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1351 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1352 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1353 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1354 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1356 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1358 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1361 ff_jpeg2000_set_significance(t1, x, y,
1362 t1->data[(y) * t1->stride + x] < 0);
1364 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1369 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1370 int bpno, int vert_causal_ctx_csty_symbol)
1375 phalf = 1 << (bpno - 1);
1378 for (y0 = 0; y0 < height; y0 += 4)
1379 for (x = 0; x < width; x++)
1380 for (y = y0; y < height && y < y0 + 4; y++)
1381 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1382 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1383 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1384 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1385 int r = ff_mqc_decode(&t1->mqc,
1386 t1->mqc.cx_states + ctxno)
1388 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1389 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1393 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1394 int width, int height, int bpno, int bandno,
1395 int seg_symbols, int vert_causal_ctx_csty_symbol)
1397 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1399 for (y0 = 0; y0 < height; y0 += 4) {
1400 for (x = 0; x < width; x++) {
1401 int flags_mask = -1;
1402 if (vert_causal_ctx_csty_symbol)
1403 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1404 if (y0 + 3 < height &&
1405 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1406 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1407 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1408 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1409 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1411 runlen = ff_mqc_decode(&t1->mqc,
1412 t1->mqc.cx_states + MQC_CX_UNI);
1413 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1422 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1423 int flags_mask = -1;
1424 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1425 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1427 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1428 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1434 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1436 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1437 t1->mqc.cx_states + ctxno) ^
1440 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1443 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1449 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1450 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1451 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1452 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1454 av_log(s->avctx, AV_LOG_ERROR,
1455 "Segmentation symbol value incorrect\n");
1459 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1460 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1461 int width, int height, int bandpos)
1463 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
1465 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1469 av_assert0(width <= 1024U && height <= 1024U);
1470 av_assert0(width*height <= 4096);
1472 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1474 /* If code-block contains no compressed data: nothing to do. */
1478 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1480 cblk->data[cblk->length] = 0xff;
1481 cblk->data[cblk->length+1] = 0xff;
1482 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1487 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1488 vert_causal_ctx_csty_symbol);
1491 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1494 av_assert2(!t1->mqc.raw);
1495 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1496 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1497 vert_causal_ctx_csty_symbol);
1500 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1501 ff_mqc_init_contexts(&t1->mqc);
1503 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1504 if (term_cnt >= cblk->nb_terminations) {
1505 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1506 return AVERROR_INVALIDDATA;
1508 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1509 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1510 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1511 pass_cnt, cblk->npasses);
1514 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1525 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1526 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1527 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1533 /* TODO: Verify dequantization for lossless case
1534 * comp->data can be float or int
1535 * band->stepsize can be float or int
1536 * depending on the type of DWT transformation.
1537 * see ISO/IEC 15444-1:2002 A.6.1 */
1539 /* Float dequantization of a codeblock.*/
1540 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1541 Jpeg2000Component *comp,
1542 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1545 int w = cblk->coord[0][1] - cblk->coord[0][0];
1546 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1547 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1548 int *src = t1->data + j*t1->stride;
1549 for (i = 0; i < w; ++i)
1550 datap[i] = src[i] * band->f_stepsize;
1554 /* Integer dequantization of a codeblock.*/
1555 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1556 Jpeg2000Component *comp,
1557 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1560 int w = cblk->coord[0][1] - cblk->coord[0][0];
1561 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1562 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1563 int *src = t1->data + j*t1->stride;
1564 if (band->i_stepsize == 32768) {
1565 for (i = 0; i < w; ++i)
1566 datap[i] = src[i] / 2;
1568 // This should be VERY uncommon
1569 for (i = 0; i < w; ++i)
1570 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1575 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1576 Jpeg2000Component *comp,
1577 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1580 int w = cblk->coord[0][1] - cblk->coord[0][0];
1581 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1582 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1583 int *src = t1->data + j*t1->stride;
1584 for (i = 0; i < w; ++i)
1585 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1589 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1594 for (i = 1; i < 3; i++) {
1595 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1596 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1599 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1600 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1605 for (i = 0; i < 3; i++)
1606 if (tile->codsty[0].transform == FF_DWT97)
1607 src[i] = tile->comp[i].f_data;
1609 src[i] = tile->comp[i].i_data;
1611 for (i = 0; i < 2; i++)
1612 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1614 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1617 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1620 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1621 int compno, reslevelno, bandno;
1623 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
1624 int pixelsize = planar ? 1 : pixdesc->nb_components;
1627 Jpeg2000T1Context t1;
1629 /* Loop on tile components */
1630 for (compno = 0; compno < s->ncomponents; compno++) {
1631 Jpeg2000Component *comp = tile->comp + compno;
1632 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1634 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1636 /* Loop on resolution levels */
1637 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1638 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1640 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1641 int nb_precincts, precno;
1642 Jpeg2000Band *band = rlevel->band + bandno;
1643 int cblkno = 0, bandpos;
1645 bandpos = bandno + (reslevelno > 0);
1647 if (band->coord[0][0] == band->coord[0][1] ||
1648 band->coord[1][0] == band->coord[1][1])
1651 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1652 /* Loop on precincts */
1653 for (precno = 0; precno < nb_precincts; precno++) {
1654 Jpeg2000Prec *prec = band->prec + precno;
1656 /* Loop on codeblocks */
1657 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1659 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1660 decode_cblk(s, codsty, &t1, cblk,
1661 cblk->coord[0][1] - cblk->coord[0][0],
1662 cblk->coord[1][1] - cblk->coord[1][0],
1665 x = cblk->coord[0][0] - band->coord[0][0];
1666 y = cblk->coord[1][0] - band->coord[1][0];
1668 if (codsty->transform == FF_DWT97)
1669 dequantization_float(x, y, cblk, comp, &t1, band);
1670 else if (codsty->transform == FF_DWT97_INT)
1671 dequantization_int_97(x, y, cblk, comp, &t1, band);
1673 dequantization_int(x, y, cblk, comp, &t1, band);
1677 } /* end reslevel */
1680 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1683 /* inverse MCT transformation */
1684 if (tile->codsty[0].mct)
1685 mct_decode(s, tile);
1687 if (s->cdef[0] < 0) {
1688 for (x = 0; x < s->ncomponents; x++)
1690 if ((s->ncomponents & 1) == 0)
1691 s->cdef[s->ncomponents-1] = 0;
1694 if (s->precision <= 8) {
1695 for (compno = 0; compno < s->ncomponents; compno++) {
1696 Jpeg2000Component *comp = tile->comp + compno;
1697 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1698 float *datap = comp->f_data;
1699 int32_t *i_datap = comp->i_data;
1700 int cbps = s->cbps[compno];
1701 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1705 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1708 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];
1709 line = picture->data[plane] + y * picture->linesize[plane];
1710 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
1713 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];
1714 dst = line + x * pixelsize + compno*!planar;
1716 if (codsty->transform == FF_DWT97) {
1717 for (; x < w; x ++) {
1718 int val = lrintf(*datap) + (1 << (cbps - 1));
1719 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1720 val = av_clip(val, 0, (1 << cbps) - 1);
1721 *dst = val << (8 - cbps);
1726 for (; x < w; x ++) {
1727 int val = *i_datap + (1 << (cbps - 1));
1728 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1729 val = av_clip(val, 0, (1 << cbps) - 1);
1730 *dst = val << (8 - cbps);
1735 line += picture->linesize[plane];
1739 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1740 picture->format == AV_PIX_FMT_RGB48 ||
1741 picture->format == AV_PIX_FMT_RGBA64 ||
1742 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1744 for (compno = 0; compno < s->ncomponents; compno++) {
1745 Jpeg2000Component *comp = tile->comp + compno;
1746 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1747 float *datap = comp->f_data;
1748 int32_t *i_datap = comp->i_data;
1750 int cbps = s->cbps[compno];
1751 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1755 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1757 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];
1758 linel = (uint16_t *)picture->data[plane] + y * (picture->linesize[plane] >> 1);
1759 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
1762 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];
1763 dst = linel + (x * pixelsize + compno*!planar);
1764 if (codsty->transform == FF_DWT97) {
1765 for (; x < w; x ++) {
1766 int val = lrintf(*datap) + (1 << (cbps - 1));
1767 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1768 val = av_clip(val, 0, (1 << cbps) - 1);
1769 /* align 12 bit values in little-endian mode */
1770 *dst = val << (precision - cbps);
1775 for (; x < w; x ++) {
1776 int val = *i_datap + (1 << (cbps - 1));
1777 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1778 val = av_clip(val, 0, (1 << cbps) - 1);
1779 /* align 12 bit values in little-endian mode */
1780 *dst = val << (precision - cbps);
1785 linel += picture->linesize[plane] >> 1;
1793 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1796 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1797 if (s->tile[tileno].comp) {
1798 for (compno = 0; compno < s->ncomponents; compno++) {
1799 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1800 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1802 ff_jpeg2000_cleanup(comp, codsty);
1804 av_freep(&s->tile[tileno].comp);
1808 memset(s->codsty, 0, sizeof(s->codsty));
1809 memset(s->qntsty, 0, sizeof(s->qntsty));
1810 memset(&s->poc , 0, sizeof(s->poc));
1811 s->numXtiles = s->numYtiles = 0;
1814 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1816 Jpeg2000CodingStyle *codsty = s->codsty;
1817 Jpeg2000QuantStyle *qntsty = s->qntsty;
1818 Jpeg2000POC *poc = &s->poc;
1819 uint8_t *properties = s->properties;
1826 if (bytestream2_get_bytes_left(&s->g) < 2) {
1827 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1831 marker = bytestream2_get_be16u(&s->g);
1832 oldpos = bytestream2_tell(&s->g);
1834 if (marker == JPEG2000_SOD) {
1836 Jpeg2000TilePart *tp;
1839 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1840 return AVERROR_INVALIDDATA;
1842 if (s->curtileno < 0) {
1843 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1844 return AVERROR_INVALIDDATA;
1847 tile = s->tile + s->curtileno;
1848 tp = tile->tile_part + tile->tp_idx;
1849 if (tp->tp_end < s->g.buffer) {
1850 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1851 return AVERROR_INVALIDDATA;
1853 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1854 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1858 if (marker == JPEG2000_EOC)
1861 len = bytestream2_get_be16(&s->g);
1862 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
1863 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
1864 return AVERROR_INVALIDDATA;
1871 s->numXtiles = s->numYtiles = 0;
1874 ret = get_coc(s, codsty, properties);
1877 ret = get_cod(s, codsty, properties);
1880 ret = get_qcc(s, len, qntsty, properties);
1883 ret = get_qcd(s, len, qntsty, properties);
1886 ret = get_poc(s, len, poc);
1889 if (!(ret = get_sot(s, len))) {
1890 av_assert1(s->curtileno >= 0);
1891 codsty = s->tile[s->curtileno].codsty;
1892 qntsty = s->tile[s->curtileno].qntsty;
1893 poc = &s->tile[s->curtileno].poc;
1894 properties = s->tile[s->curtileno].properties;
1898 // the comment is ignored
1899 bytestream2_skip(&s->g, len - 2);
1902 // Tile-part lengths
1903 ret = get_tlm(s, len);
1906 // Packet length, tile-part header
1907 ret = get_plt(s, len);
1910 av_log(s->avctx, AV_LOG_ERROR,
1911 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1912 marker, bytestream2_tell(&s->g) - 4);
1913 bytestream2_skip(&s->g, len - 2);
1916 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1917 av_log(s->avctx, AV_LOG_ERROR,
1918 "error during processing marker segment %.4"PRIx16"\n",
1920 return ret ? ret : -1;
1926 /* Read bit stream packets --> T2 operation. */
1927 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1932 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1933 Jpeg2000Tile *tile = s->tile + tileno;
1935 if ((ret = init_tile(s, tileno)) < 0)
1938 s->g = tile->tile_part[0].tpg;
1939 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
1946 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1948 uint32_t atom_size, atom, atom_end;
1949 int search_range = 10;
1953 bytestream2_get_bytes_left(&s->g) >= 8) {
1954 atom_size = bytestream2_get_be32u(&s->g);
1955 atom = bytestream2_get_be32u(&s->g);
1956 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
1958 if (atom == JP2_CODESTREAM)
1961 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1964 if (atom == JP2_HEADER &&
1966 uint32_t atom2_size, atom2, atom2_end;
1968 atom2_size = bytestream2_get_be32u(&s->g);
1969 atom2 = bytestream2_get_be32u(&s->g);
1970 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
1971 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1973 if (atom2 == JP2_CODESTREAM) {
1975 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1976 int method = bytestream2_get_byteu(&s->g);
1977 bytestream2_skipu(&s->g, 2);
1979 s->colour_space = bytestream2_get_be32u(&s->g);
1981 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1982 int i, size, colour_count, colour_channels, colour_depth[3];
1984 colour_count = bytestream2_get_be16u(&s->g);
1985 colour_channels = bytestream2_get_byteu(&s->g);
1986 // FIXME: Do not ignore channel_sign
1987 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1988 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1989 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1990 size = (colour_depth[0] + 7 >> 3) * colour_count +
1991 (colour_depth[1] + 7 >> 3) * colour_count +
1992 (colour_depth[2] + 7 >> 3) * colour_count;
1993 if (colour_count > 256 ||
1994 colour_channels != 3 ||
1995 colour_depth[0] > 16 ||
1996 colour_depth[1] > 16 ||
1997 colour_depth[2] > 16 ||
1998 atom2_size < size) {
1999 avpriv_request_sample(s->avctx, "Unknown palette");
2000 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2004 for (i = 0; i < colour_count; i++) {
2005 if (colour_depth[0] <= 8) {
2006 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2007 r |= r >> colour_depth[0];
2009 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2011 if (colour_depth[1] <= 8) {
2012 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2013 r |= r >> colour_depth[1];
2015 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2017 if (colour_depth[2] <= 8) {
2018 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2019 r |= r >> colour_depth[2];
2021 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2023 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2025 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2026 int n = bytestream2_get_be16u(&s->g);
2028 int cn = bytestream2_get_be16(&s->g);
2029 int av_unused typ = bytestream2_get_be16(&s->g);
2030 int asoc = bytestream2_get_be16(&s->g);
2031 if (cn < 4 && asoc < 4)
2035 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2036 } while (atom_end - atom2_end >= 8);
2040 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2046 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2048 Jpeg2000DecoderContext *s = avctx->priv_data;
2050 ff_jpeg2000dsp_init(&s->dsp);
2055 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2056 int *got_frame, AVPacket *avpkt)
2058 Jpeg2000DecoderContext *s = avctx->priv_data;
2059 ThreadFrame frame = { .f = data };
2060 AVFrame *picture = data;
2064 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2066 memset(s->cdef, -1, sizeof(s->cdef));
2068 if (bytestream2_get_bytes_left(&s->g) < 2) {
2069 ret = AVERROR_INVALIDDATA;
2073 // check if the image is in jp2 format
2074 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2075 (bytestream2_get_be32u(&s->g) == 12) &&
2076 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2077 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2078 if (!jp2_find_codestream(s)) {
2079 av_log(avctx, AV_LOG_ERROR,
2080 "Could not find Jpeg2000 codestream atom.\n");
2081 ret = AVERROR_INVALIDDATA;
2085 bytestream2_seek(&s->g, 0, SEEK_SET);
2088 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2089 bytestream2_skip(&s->g, 1);
2091 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2092 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2093 ret = AVERROR_INVALIDDATA;
2096 if (ret = jpeg2000_read_main_headers(s))
2099 /* get picture buffer */
2100 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2102 picture->pict_type = AV_PICTURE_TYPE_I;
2103 picture->key_frame = 1;
2105 if (ret = jpeg2000_read_bitstream_packets(s))
2108 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
2109 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
2112 jpeg2000_dec_cleanup(s);
2116 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2117 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2119 return bytestream2_tell(&s->g);
2122 jpeg2000_dec_cleanup(s);
2126 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
2128 ff_jpeg2000_init_tier1_luts();
2129 ff_mqc_init_context_tables();
2132 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2133 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2135 static const AVOption options[] = {
2136 { "lowres", "Lower the decoding resolution by a power of two",
2137 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2141 static const AVProfile profiles[] = {
2142 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
2143 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
2144 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
2145 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
2146 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
2147 { FF_PROFILE_UNKNOWN },
2150 static const AVClass jpeg2000_class = {
2151 .class_name = "jpeg2000",
2152 .item_name = av_default_item_name,
2154 .version = LIBAVUTIL_VERSION_INT,
2157 AVCodec ff_jpeg2000_decoder = {
2159 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2160 .type = AVMEDIA_TYPE_VIDEO,
2161 .id = AV_CODEC_ID_JPEG2000,
2162 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_DR1,
2163 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2164 .init_static_data = jpeg2000_init_static_data,
2165 .init = jpeg2000_decode_init,
2166 .decode = jpeg2000_decode_frame,
2167 .priv_class = &jpeg2000_class,
2169 .profiles = NULL_IF_CONFIG_SMALL(profiles)