2 * JPEG 2000 image decoder
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * JPEG 2000 image decoder
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/thread.h"
39 #include "bytestream.h"
43 #include "jpeg2000dsp.h"
46 #define JP2_SIG_TYPE 0x6A502020
47 #define JP2_SIG_VALUE 0x0D0A870A
48 #define JP2_CODESTREAM 0x6A703263
49 #define JP2_HEADER 0x6A703268
56 typedef struct Jpeg2000POCEntry {
65 typedef struct Jpeg2000POC {
66 Jpeg2000POCEntry poc[MAX_POCS];
71 typedef struct Jpeg2000TilePart {
72 uint8_t tile_index; // Tile index who refers the tile-part
73 const uint8_t *tp_end;
74 GetByteContext tpg; // bit stream in tile-part
77 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
78 * one per component, so tile_part elements have a size of 3 */
79 typedef struct Jpeg2000Tile {
80 Jpeg2000Component *comp;
81 uint8_t properties[4];
82 Jpeg2000CodingStyle codsty[4];
83 Jpeg2000QuantStyle qntsty[4];
85 Jpeg2000TilePart tile_part[32];
86 uint8_t has_ppt; // whether this tile has a ppt marker
87 uint8_t *packed_headers; // contains packed headers. Used only along with PPT marker
88 int packed_headers_size; // size in bytes of the packed headers
89 GetByteContext packed_headers_stream; // byte context corresponding to packed headers
90 uint16_t tp_idx; // Tile-part index
91 int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
94 typedef struct Jpeg2000DecoderContext {
96 AVCodecContext *avctx;
100 int image_offset_x, image_offset_y;
101 int tile_offset_x, tile_offset_y;
102 uint8_t cbps[4]; // bits per sample in particular components
103 uint8_t sgnd[4]; // if a component is signed
104 uint8_t properties[4];
109 uint32_t palette[256];
112 int tile_width, tile_height;
113 unsigned numXtiles, numYtiles;
117 Jpeg2000CodingStyle codsty[4];
118 Jpeg2000QuantStyle qntsty[4];
126 Jpeg2000DSPContext dsp;
128 /*options parameters*/
129 int reduction_factor;
130 } Jpeg2000DecoderContext;
132 /* get_bits functions for JPEG2000 packet bitstream
133 * It is a get_bit function with a bit-stuffing routine. If the value of the
134 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
135 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
136 static int get_bits(Jpeg2000DecoderContext *s, int n)
142 if (s->bit_index == 0) {
143 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
146 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
151 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
153 if (bytestream2_get_byte(&s->g) == 0xff)
154 bytestream2_skip(&s->g, 1);
158 /* decode the value stored in node */
159 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
162 Jpeg2000TgtNode *stack[30];
163 int sp = -1, curval = 0;
166 av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
167 return AVERROR_INVALIDDATA;
170 while (node && !node->vis) {
178 curval = stack[sp]->val;
180 while (curval < threshold && sp >= 0) {
181 if (curval < stack[sp]->val)
182 curval = stack[sp]->val;
183 while (curval < threshold) {
185 if ((ret = get_bits(s, 1)) > 0) {
193 stack[sp]->val = curval;
199 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
200 int bpc, uint32_t log2_chroma_wh, int pal8)
203 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
207 if (desc->nb_components != components) {
211 switch (components) {
213 match = match && desc->comp[3].depth >= bpc &&
214 (log2_chroma_wh >> 14 & 3) == 0 &&
215 (log2_chroma_wh >> 12 & 3) == 0;
217 match = match && desc->comp[2].depth >= bpc &&
218 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
219 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
221 match = match && desc->comp[1].depth >= bpc &&
222 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
223 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
226 match = match && desc->comp[0].depth >= bpc &&
227 (log2_chroma_wh >> 2 & 3) == 0 &&
228 (log2_chroma_wh & 3) == 0 &&
229 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
234 // pix_fmts with lower bpp have to be listed before
235 // similar pix_fmts with higher bpp.
236 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
237 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
238 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
239 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
240 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
241 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
242 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
243 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
244 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
245 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
246 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
247 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
248 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
249 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
251 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
252 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
253 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
254 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS,
256 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
261 /* marker segments */
262 /* get sizes and offsets of image, tiles; number of components */
263 static int get_siz(Jpeg2000DecoderContext *s)
267 uint32_t log2_chroma_wh = 0;
268 const enum AVPixelFormat *possible_fmts = NULL;
269 int possible_fmts_nb = 0;
272 if (bytestream2_get_bytes_left(&s->g) < 36) {
273 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
274 return AVERROR_INVALIDDATA;
277 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
278 s->width = bytestream2_get_be32u(&s->g); // Width
279 s->height = bytestream2_get_be32u(&s->g); // Height
280 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
281 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
282 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
283 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
284 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
285 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
286 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
288 if (s->image_offset_x || s->image_offset_y) {
289 avpriv_request_sample(s->avctx, "Support for image offsets");
290 return AVERROR_PATCHWELCOME;
292 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
293 avpriv_request_sample(s->avctx, "Large Dimensions");
294 return AVERROR_PATCHWELCOME;
297 if (ncomponents <= 0) {
298 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
300 return AVERROR_INVALIDDATA;
303 if (ncomponents > 4) {
304 avpriv_request_sample(s->avctx, "Support for %d components",
306 return AVERROR_PATCHWELCOME;
309 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
310 s->image_offset_x < s->tile_offset_x ||
311 s->image_offset_y < s->tile_offset_y ||
312 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
313 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
315 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
316 return AVERROR_INVALIDDATA;
319 s->ncomponents = ncomponents;
321 if (s->tile_width <= 0 || s->tile_height <= 0) {
322 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
323 s->tile_width, s->tile_height);
324 return AVERROR_INVALIDDATA;
327 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
328 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
329 return AVERROR_INVALIDDATA;
332 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
333 uint8_t x = bytestream2_get_byteu(&s->g);
334 s->cbps[i] = (x & 0x7f) + 1;
335 s->precision = FFMAX(s->cbps[i], s->precision);
336 s->sgnd[i] = !!(x & 0x80);
337 s->cdx[i] = bytestream2_get_byteu(&s->g);
338 s->cdy[i] = bytestream2_get_byteu(&s->g);
339 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
340 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
341 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
342 return AVERROR_INVALIDDATA;
344 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
347 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
348 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
350 // There must be at least a SOT and SOD per tile, their minimum size is 14
351 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
352 s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
354 s->numXtiles = s->numYtiles = 0;
355 return AVERROR(EINVAL);
358 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
360 s->numXtiles = s->numYtiles = 0;
361 return AVERROR(ENOMEM);
364 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
365 Jpeg2000Tile *tile = s->tile + i;
367 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
369 return AVERROR(ENOMEM);
372 /* compute image size with reduction factor */
373 ret = ff_set_dimensions(s->avctx,
374 ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
375 s->reduction_factor),
376 ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
377 s->reduction_factor));
381 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
382 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
383 possible_fmts = xyz_pix_fmts;
384 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
386 switch (s->colour_space) {
388 possible_fmts = rgb_pix_fmts;
389 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
392 possible_fmts = gray_pix_fmts;
393 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
396 possible_fmts = yuv_pix_fmts;
397 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
400 possible_fmts = all_pix_fmts;
401 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
405 if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
406 && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
407 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
408 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
409 for (i = 0; i < possible_fmts_nb; ++i) {
410 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
411 s->avctx->pix_fmt = possible_fmts[i];
416 if (i == possible_fmts_nb) {
417 if (ncomponents == 4 &&
418 s->cdy[0] == 1 && s->cdx[0] == 1 &&
419 s->cdy[1] == 1 && s->cdx[1] == 1 &&
420 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
421 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
422 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
433 if (i == possible_fmts_nb) {
434 av_log(s->avctx, AV_LOG_ERROR,
435 "Unknown pix_fmt, profile: %d, colour_space: %d, "
436 "components: %d, precision: %d\n"
437 "cdx[0]: %d, cdy[0]: %d\n"
438 "cdx[1]: %d, cdy[1]: %d\n"
439 "cdx[2]: %d, cdy[2]: %d\n"
440 "cdx[3]: %d, cdy[3]: %d\n",
441 s->avctx->profile, s->colour_space, ncomponents, s->precision,
444 ncomponents > 1 ? s->cdx[1] : 0,
445 ncomponents > 1 ? s->cdy[1] : 0,
446 ncomponents > 2 ? s->cdx[2] : 0,
447 ncomponents > 2 ? s->cdy[2] : 0,
448 ncomponents > 3 ? s->cdx[3] : 0,
449 ncomponents > 3 ? s->cdy[3] : 0);
450 return AVERROR_PATCHWELCOME;
452 s->avctx->bits_per_raw_sample = s->precision;
456 /* get common part for COD and COC segments */
457 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
461 if (bytestream2_get_bytes_left(&s->g) < 5) {
462 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
463 return AVERROR_INVALIDDATA;
466 /* nreslevels = number of resolution levels
467 = number of decomposition level +1 */
468 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
469 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
470 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
471 return AVERROR_INVALIDDATA;
474 if (c->nreslevels <= s->reduction_factor) {
475 /* we are forced to update reduction_factor as its requested value is
476 not compatible with this bitstream, and as we might have used it
477 already in setup earlier we have to fail this frame until
478 reinitialization is implemented */
479 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
480 s->reduction_factor = c->nreslevels - 1;
481 return AVERROR(EINVAL);
484 /* compute number of resolution levels to decode */
485 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
487 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
488 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
490 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
491 c->log2_cblk_width + c->log2_cblk_height > 12) {
492 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
493 return AVERROR_INVALIDDATA;
496 c->cblk_style = bytestream2_get_byteu(&s->g);
497 if (c->cblk_style != 0) { // cblk style
498 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
499 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
500 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
502 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
503 /* set integer 9/7 DWT in case of BITEXACT flag */
504 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
505 c->transform = FF_DWT97_INT;
506 else if (c->transform == FF_DWT53) {
507 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
510 if (c->csty & JPEG2000_CSTY_PREC) {
512 for (i = 0; i < c->nreslevels; i++) {
513 byte = bytestream2_get_byte(&s->g);
514 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
515 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
517 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
518 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
519 c->log2_prec_widths[i], c->log2_prec_heights[i]);
520 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
521 return AVERROR_INVALIDDATA;
525 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
526 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
531 /* get coding parameters for a particular tile or whole image*/
532 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
535 Jpeg2000CodingStyle tmp;
538 if (bytestream2_get_bytes_left(&s->g) < 5) {
539 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
540 return AVERROR_INVALIDDATA;
543 tmp.csty = bytestream2_get_byteu(&s->g);
545 // get progression order
546 tmp.prog_order = bytestream2_get_byteu(&s->g);
548 tmp.nlayers = bytestream2_get_be16u(&s->g);
549 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
551 if (tmp.mct && s->ncomponents < 3) {
552 av_log(s->avctx, AV_LOG_ERROR,
553 "MCT %"PRIu8" with too few components (%d)\n",
554 tmp.mct, s->ncomponents);
555 return AVERROR_INVALIDDATA;
558 if ((ret = get_cox(s, &tmp)) < 0)
561 for (compno = 0; compno < s->ncomponents; compno++)
562 if (!(properties[compno] & HAD_COC))
563 memcpy(c + compno, &tmp, sizeof(tmp));
567 /* Get coding parameters for a component in the whole image or a
568 * particular tile. */
569 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
575 if (bytestream2_get_bytes_left(&s->g) < 2) {
576 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
577 return AVERROR_INVALIDDATA;
580 compno = bytestream2_get_byteu(&s->g);
582 if (compno >= s->ncomponents) {
583 av_log(s->avctx, AV_LOG_ERROR,
584 "Invalid compno %d. There are %d components in the image.\n",
585 compno, s->ncomponents);
586 return AVERROR_INVALIDDATA;
590 has_eph = c->csty & JPEG2000_CSTY_EPH;
591 c->csty = bytestream2_get_byteu(&s->g);
592 c->csty |= has_eph; //do not override eph present bits from COD
594 if ((ret = get_cox(s, c)) < 0)
597 properties[compno] |= HAD_COC;
601 /* Get common part for QCD and QCC segments. */
602 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
606 if (bytestream2_get_bytes_left(&s->g) < 1)
607 return AVERROR_INVALIDDATA;
609 x = bytestream2_get_byteu(&s->g); // Sqcd
611 q->nguardbits = x >> 5;
612 q->quantsty = x & 0x1f;
614 if (q->quantsty == JPEG2000_QSTY_NONE) {
616 if (bytestream2_get_bytes_left(&s->g) < n ||
617 n > JPEG2000_MAX_DECLEVELS*3)
618 return AVERROR_INVALIDDATA;
619 for (i = 0; i < n; i++)
620 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
621 } else if (q->quantsty == JPEG2000_QSTY_SI) {
622 if (bytestream2_get_bytes_left(&s->g) < 2)
623 return AVERROR_INVALIDDATA;
624 x = bytestream2_get_be16u(&s->g);
625 q->expn[0] = x >> 11;
626 q->mant[0] = x & 0x7ff;
627 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
628 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
629 q->expn[i] = curexpn;
630 q->mant[i] = q->mant[0];
634 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
635 n > JPEG2000_MAX_DECLEVELS*3)
636 return AVERROR_INVALIDDATA;
637 for (i = 0; i < n; i++) {
638 x = bytestream2_get_be16u(&s->g);
639 q->expn[i] = x >> 11;
640 q->mant[i] = x & 0x7ff;
646 /* Get quantization parameters for a particular tile or a whole image. */
647 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
650 Jpeg2000QuantStyle tmp;
653 memset(&tmp, 0, sizeof(tmp));
655 if ((ret = get_qcx(s, n, &tmp)) < 0)
657 for (compno = 0; compno < s->ncomponents; compno++)
658 if (!(properties[compno] & HAD_QCC))
659 memcpy(q + compno, &tmp, sizeof(tmp));
663 /* Get quantization parameters for a component in the whole image
664 * on in a particular tile. */
665 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
670 if (bytestream2_get_bytes_left(&s->g) < 1)
671 return AVERROR_INVALIDDATA;
673 compno = bytestream2_get_byteu(&s->g);
675 if (compno >= s->ncomponents) {
676 av_log(s->avctx, AV_LOG_ERROR,
677 "Invalid compno %d. There are %d components in the image.\n",
678 compno, s->ncomponents);
679 return AVERROR_INVALIDDATA;
682 properties[compno] |= HAD_QCC;
683 return get_qcx(s, n - 1, q + compno);
686 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
689 int elem_size = s->ncomponents <= 257 ? 7 : 9;
690 Jpeg2000POC tmp = {{{0}}};
692 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
693 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
694 return AVERROR_INVALIDDATA;
698 avpriv_request_sample(s->avctx, "Fat POC not supported");
699 return AVERROR_PATCHWELCOME;
702 tmp.nb_poc = (size - 2) / elem_size;
703 if (tmp.nb_poc > MAX_POCS) {
704 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
705 return AVERROR_PATCHWELCOME;
708 for (i = 0; i<tmp.nb_poc; i++) {
709 Jpeg2000POCEntry *e = &tmp.poc[i];
710 e->RSpoc = bytestream2_get_byteu(&s->g);
711 e->CSpoc = bytestream2_get_byteu(&s->g);
712 e->LYEpoc = bytestream2_get_be16u(&s->g);
713 e->REpoc = bytestream2_get_byteu(&s->g);
714 e->CEpoc = bytestream2_get_byteu(&s->g);
715 e->Ppoc = bytestream2_get_byteu(&s->g);
718 if (e->CEpoc > s->ncomponents)
719 e->CEpoc = s->ncomponents;
720 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
721 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
723 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
724 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
726 return AVERROR_INVALIDDATA;
730 if (!p->nb_poc || p->is_default) {
733 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
734 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
735 return AVERROR_INVALIDDATA;
737 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
738 p->nb_poc += tmp.nb_poc;
747 /* Get start of tile segment. */
748 static int get_sot(Jpeg2000DecoderContext *s, int n)
750 Jpeg2000TilePart *tp;
755 if (bytestream2_get_bytes_left(&s->g) < 8)
756 return AVERROR_INVALIDDATA;
759 Isot = bytestream2_get_be16u(&s->g); // Isot
760 if (Isot >= s->numXtiles * s->numYtiles)
761 return AVERROR_INVALIDDATA;
764 Psot = bytestream2_get_be32u(&s->g); // Psot
765 TPsot = bytestream2_get_byteu(&s->g); // TPsot
767 /* Read TNSot but not used */
768 bytestream2_get_byteu(&s->g); // TNsot
771 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
773 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
774 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
775 return AVERROR_INVALIDDATA;
778 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
779 avpriv_request_sample(s->avctx, "Too many tile parts");
780 return AVERROR_PATCHWELCOME;
783 s->tile[Isot].tp_idx = TPsot;
784 tp = s->tile[Isot].tile_part + TPsot;
785 tp->tile_index = Isot;
786 tp->tp_end = s->g.buffer + Psot - n - 2;
789 Jpeg2000Tile *tile = s->tile + s->curtileno;
792 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
793 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
794 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
795 tile->poc.is_default = 1;
801 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
802 * Used to know the number of tile parts and lengths.
803 * There may be multiple TLMs in the header.
804 * TODO: The function is not used for tile-parts management, nor anywhere else.
805 * It can be useful to allocate memory for tile parts, before managing the SOT
806 * markers. Parsing the TLM header is needed to increment the input header
808 * This marker is mandatory for DCI. */
809 static int get_tlm(Jpeg2000DecoderContext *s, int n)
811 uint8_t Stlm, ST, SP, tile_tlm, i;
812 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
813 Stlm = bytestream2_get_byte(&s->g);
815 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
816 ST = (Stlm >> 4) & 0x03;
818 av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
819 return AVERROR_INVALIDDATA;
822 SP = (Stlm >> 6) & 0x01;
823 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
824 for (i = 0; i < tile_tlm; i++) {
829 bytestream2_get_byte(&s->g);
832 bytestream2_get_be16(&s->g);
835 bytestream2_get_be32(&s->g);
839 bytestream2_get_be16(&s->g);
841 bytestream2_get_be32(&s->g);
847 static int get_plt(Jpeg2000DecoderContext *s, int n)
852 av_log(s->avctx, AV_LOG_DEBUG,
853 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
856 return AVERROR_INVALIDDATA;
858 /*Zplt =*/ bytestream2_get_byte(&s->g);
860 for (i = 0; i < n - 3; i++) {
861 v = bytestream2_get_byte(&s->g);
864 return AVERROR_INVALIDDATA;
869 static int get_ppt(Jpeg2000DecoderContext *s, int n)
875 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
876 return AVERROR_INVALIDDATA;
878 if (s->curtileno < 0)
879 return AVERROR_INVALIDDATA;
881 tile = &s->tile[s->curtileno];
882 if (tile->tp_idx != 0) {
883 av_log(s->avctx, AV_LOG_ERROR,
884 "PPT marker can occur only on first tile part of a tile.\n");
885 return AVERROR_INVALIDDATA;
888 tile->has_ppt = 1; // this tile has a ppt marker
889 bytestream2_get_byte(&s->g); // Zppt is skipped and not used
890 new = av_realloc(tile->packed_headers,
891 tile->packed_headers_size + n - 3);
893 tile->packed_headers = new;
895 return AVERROR(ENOMEM);
896 memcpy(tile->packed_headers + tile->packed_headers_size,
898 tile->packed_headers_size += n - 3;
899 bytestream2_skip(&s->g, n - 3);
904 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
907 int tilex = tileno % s->numXtiles;
908 int tiley = tileno / s->numXtiles;
909 Jpeg2000Tile *tile = s->tile + tileno;
912 return AVERROR(ENOMEM);
914 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
915 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
916 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
917 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
919 for (compno = 0; compno < s->ncomponents; compno++) {
920 Jpeg2000Component *comp = tile->comp + compno;
921 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
922 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
923 int ret; // global bandno
925 comp->coord_o[0][0] = tile->coord[0][0];
926 comp->coord_o[0][1] = tile->coord[0][1];
927 comp->coord_o[1][0] = tile->coord[1][0];
928 comp->coord_o[1][1] = tile->coord[1][1];
930 comp->coord_o[0][0] /= s->cdx[compno];
931 comp->coord_o[0][1] /= s->cdx[compno];
932 comp->coord_o[1][0] /= s->cdy[compno];
933 comp->coord_o[1][1] /= s->cdy[compno];
936 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
937 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
938 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
939 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
941 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
942 s->cbps[compno], s->cdx[compno],
943 s->cdy[compno], s->avctx))
949 /* Read the number of coding passes. */
950 static int getnpasses(Jpeg2000DecoderContext *s)
957 if ((num = get_bits(s, 2)) != 3)
958 return num < 0 ? num : 3 + num;
959 if ((num = get_bits(s, 5)) != 31)
960 return num < 0 ? num : 6 + num;
961 num = get_bits(s, 7);
962 return num < 0 ? num : 37 + num;
965 static int getlblockinc(Jpeg2000DecoderContext *s)
968 while (ret = get_bits(s, 1)) {
976 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
979 s->g = tile->tile_part[*tp_index].tpg;
980 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
981 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
982 s->g = tile->tile_part[++(*tp_index)].tpg;
985 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
986 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
989 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
990 Jpeg2000CodingStyle *codsty,
991 Jpeg2000ResLevel *rlevel, int precno,
992 int layno, uint8_t *expn, int numgbits)
994 int bandno, cblkno, ret, nb_code_blocks;
997 if (layno < rlevel->band[0].prec[precno].decoded_layers)
999 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1000 // Select stream to read from
1002 s->g = tile->packed_headers_stream;
1004 select_stream(s, tile, tp_index);
1006 if (!(ret = get_bits(s, 1))) {
1012 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1013 Jpeg2000Band *band = rlevel->band + bandno;
1014 Jpeg2000Prec *prec = band->prec + precno;
1016 if (band->coord[0][0] == band->coord[0][1] ||
1017 band->coord[1][0] == band->coord[1][1])
1019 nb_code_blocks = prec->nb_codeblocks_height *
1020 prec->nb_codeblocks_width;
1021 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1022 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1023 int incl, newpasses, llen;
1027 incl = get_bits(s, 1);
1029 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1035 if (!cblk->npasses) {
1036 int v = expn[bandno] + numgbits - 1 -
1037 tag_tree_decode(s, prec->zerobits + cblkno, 100);
1038 if (v < 0 || v > 30) {
1039 av_log(s->avctx, AV_LOG_ERROR,
1040 "nonzerobits %d invalid or unsupported\n", v);
1041 return AVERROR_INVALIDDATA;
1043 cblk->nonzerobits = v;
1045 if ((newpasses = getnpasses(s)) < 0)
1047 av_assert2(newpasses > 0);
1048 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1049 avpriv_request_sample(s->avctx, "Too many passes");
1050 return AVERROR_PATCHWELCOME;
1052 if ((llen = getlblockinc(s)) < 0)
1054 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1055 avpriv_request_sample(s->avctx,
1056 "Block with length beyond 16 bits");
1057 return AVERROR_PATCHWELCOME;
1060 cblk->lblock += llen;
1062 cblk->nb_lengthinc = 0;
1063 cblk->nb_terminationsinc = 0;
1064 av_free(cblk->lengthinc);
1065 cblk->lengthinc = av_mallocz_array(newpasses , sizeof(*cblk->lengthinc));
1066 if (!cblk->lengthinc)
1067 return AVERROR(ENOMEM);
1068 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1070 return AVERROR(ENOMEM);
1071 cblk->data_start = tmp;
1075 while (newpasses1 < newpasses) {
1077 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1078 cblk->nb_terminationsinc ++;
1083 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1085 if (ret > cblk->data_allocated) {
1086 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1087 void *new = av_realloc(cblk->data, new_size);
1090 cblk->data_allocated = new_size;
1093 if (ret > cblk->data_allocated) {
1094 avpriv_request_sample(s->avctx,
1095 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1096 cblk->data_allocated);
1097 return AVERROR_PATCHWELCOME;
1099 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1100 cblk->npasses += newpasses1;
1101 newpasses -= newpasses1;
1107 if (codsty->csty & JPEG2000_CSTY_EPH) {
1108 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1109 bytestream2_skip(&s->g, 2);
1111 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1114 // Save state of stream
1115 if (tile->has_ppt) {
1116 tile->packed_headers_stream = s->g;
1117 select_stream(s, tile, tp_index);
1119 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1120 Jpeg2000Band *band = rlevel->band + bandno;
1121 Jpeg2000Prec *prec = band->prec + precno;
1123 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1124 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1125 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1126 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1128 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1129 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1130 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1131 void *new = av_realloc(cblk->data, new_size);
1134 cblk->data_allocated = new_size;
1137 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1138 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1140 av_log(s->avctx, AV_LOG_ERROR,
1141 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1142 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1143 return AVERROR_INVALIDDATA;
1146 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1147 cblk->length += cblk->lengthinc[cwsno];
1148 cblk->lengthinc[cwsno] = 0;
1149 if (cblk->nb_terminationsinc) {
1150 cblk->nb_terminationsinc--;
1151 cblk->nb_terminations++;
1152 cblk->data[cblk->length++] = 0xFF;
1153 cblk->data[cblk->length++] = 0xFF;
1154 cblk->data_start[cblk->nb_terminations] = cblk->length;
1157 av_freep(&cblk->lengthinc);
1160 // Save state of stream
1161 tile->tile_part[*tp_index].tpg = s->g;
1166 tile->packed_headers_stream = s->g;
1168 tile->tile_part[*tp_index].tpg = s->g;
1172 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1173 int RSpoc, int CSpoc,
1174 int LYEpoc, int REpoc, int CEpoc,
1175 int Ppoc, int *tp_index)
1178 int layno, reslevelno, compno, precno, ok_reslevel;
1183 case JPEG2000_PGOD_RLCP:
1184 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1186 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1188 for (layno = 0; layno < LYEpoc; layno++) {
1189 for (compno = CSpoc; compno < CEpoc; compno++) {
1190 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1191 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1192 if (reslevelno < codsty->nreslevels) {
1193 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1196 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1197 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1200 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1201 qntsty->nguardbits)) < 0)
1209 case JPEG2000_PGOD_LRCP:
1210 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1211 for (layno = 0; layno < LYEpoc; layno++) {
1213 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1215 for (compno = CSpoc; compno < CEpoc; compno++) {
1216 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1217 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1218 if (reslevelno < codsty->nreslevels) {
1219 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1222 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1223 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1226 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1227 qntsty->nguardbits)) < 0)
1235 case JPEG2000_PGOD_CPRL:
1236 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1237 for (compno = CSpoc; compno < CEpoc; compno++) {
1238 Jpeg2000Component *comp = tile->comp + compno;
1239 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1240 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1244 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1247 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1248 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1249 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1250 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1251 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1253 if (step_x >= 31 || step_y >= 31){
1254 avpriv_request_sample(s->avctx, "CPRL with large step");
1255 return AVERROR_PATCHWELCOME;
1260 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1261 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1262 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1263 unsigned prcx, prcy;
1264 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1265 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1266 int xc = x / s->cdx[compno];
1267 int yc = y / s->cdy[compno];
1269 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1272 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1275 // check if a precinct exists
1276 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1277 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1278 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1279 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1281 precno = prcx + rlevel->num_precincts_x * prcy;
1283 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1284 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1285 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1289 for (layno = 0; layno < LYEpoc; layno++) {
1290 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1292 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1293 qntsty->nguardbits)) < 0)
1302 case JPEG2000_PGOD_RPCL:
1303 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1305 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1309 for (compno = CSpoc; compno < CEpoc; compno++) {
1310 Jpeg2000Component *comp = tile->comp + compno;
1311 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1313 if (reslevelno < codsty->nreslevels) {
1314 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1315 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1316 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1317 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1323 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1324 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1325 for (compno = CSpoc; compno < CEpoc; compno++) {
1326 Jpeg2000Component *comp = tile->comp + compno;
1327 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1328 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1329 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1330 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1331 unsigned prcx, prcy;
1333 int xc = x / s->cdx[compno];
1334 int yc = y / s->cdy[compno];
1336 if (reslevelno >= codsty->nreslevels)
1339 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1342 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1345 // check if a precinct exists
1346 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1347 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1348 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1349 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1351 precno = prcx + rlevel->num_precincts_x * prcy;
1354 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1355 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1356 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1360 for (layno = 0; layno < LYEpoc; layno++) {
1361 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1364 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1365 qntsty->nguardbits)) < 0)
1374 case JPEG2000_PGOD_PCRL:
1375 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1378 for (compno = CSpoc; compno < CEpoc; compno++) {
1379 Jpeg2000Component *comp = tile->comp + compno;
1380 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1382 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1383 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1384 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1385 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1386 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1389 if (step_x >= 31 || step_y >= 31){
1390 avpriv_request_sample(s->avctx, "PCRL with large step");
1391 return AVERROR_PATCHWELCOME;
1396 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1397 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1398 for (compno = CSpoc; compno < CEpoc; compno++) {
1399 Jpeg2000Component *comp = tile->comp + compno;
1400 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1401 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1402 int xc = x / s->cdx[compno];
1403 int yc = y / s->cdy[compno];
1405 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1406 unsigned prcx, prcy;
1407 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1408 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1410 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1413 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1416 // check if a precinct exists
1417 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1418 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1419 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1420 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1422 precno = prcx + rlevel->num_precincts_x * prcy;
1424 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1425 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1426 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1430 for (layno = 0; layno < LYEpoc; layno++) {
1431 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1433 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1434 qntsty->nguardbits)) < 0)
1450 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1452 int ret = AVERROR_BUG;
1457 if (tile->poc.nb_poc) {
1458 for (i=0; i<tile->poc.nb_poc; i++) {
1459 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1460 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1462 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1464 FFMIN(e->CEpoc, s->ncomponents),
1471 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1473 tile->codsty[0].nlayers,
1476 tile->codsty[0].prog_order,
1480 /* EOC marker reached */
1481 bytestream2_skip(&s->g, 2);
1486 /* TIER-1 routines */
1487 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1488 int bpno, int bandno,
1489 int vert_causal_ctx_csty_symbol)
1491 int mask = 3 << (bpno - 1), y0, x, y;
1493 for (y0 = 0; y0 < height; y0 += 4)
1494 for (x = 0; x < width; x++)
1495 for (y = y0; y < height && y < y0 + 4; y++) {
1496 int flags_mask = -1;
1497 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1498 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1499 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1500 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1501 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1502 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1504 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1506 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1509 ff_jpeg2000_set_significance(t1, x, y,
1510 t1->data[(y) * t1->stride + x] < 0);
1512 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1517 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1518 int bpno, int vert_causal_ctx_csty_symbol)
1523 phalf = 1 << (bpno - 1);
1526 for (y0 = 0; y0 < height; y0 += 4)
1527 for (x = 0; x < width; x++)
1528 for (y = y0; y < height && y < y0 + 4; y++)
1529 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1530 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1531 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1532 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1533 int r = ff_mqc_decode(&t1->mqc,
1534 t1->mqc.cx_states + ctxno)
1536 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1537 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1541 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1542 int width, int height, int bpno, int bandno,
1543 int seg_symbols, int vert_causal_ctx_csty_symbol)
1545 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1547 for (y0 = 0; y0 < height; y0 += 4) {
1548 for (x = 0; x < width; x++) {
1549 int flags_mask = -1;
1550 if (vert_causal_ctx_csty_symbol)
1551 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1552 if (y0 + 3 < height &&
1553 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1554 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1555 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1556 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1557 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1559 runlen = ff_mqc_decode(&t1->mqc,
1560 t1->mqc.cx_states + MQC_CX_UNI);
1561 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1570 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1571 int flags_mask = -1;
1572 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1573 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1575 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1576 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1582 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1584 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1585 t1->mqc.cx_states + ctxno) ^
1588 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1591 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1597 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1598 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1599 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1600 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1602 av_log(s->avctx, AV_LOG_ERROR,
1603 "Segmentation symbol value incorrect\n");
1607 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1608 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1609 int width, int height, int bandpos)
1611 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
1613 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1617 av_assert0(width <= 1024U && height <= 1024U);
1618 av_assert0(width*height <= 4096);
1620 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1622 /* If code-block contains no compressed data: nothing to do. */
1626 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1628 cblk->data[cblk->length] = 0xff;
1629 cblk->data[cblk->length+1] = 0xff;
1630 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1634 av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1635 return AVERROR_INVALIDDATA;
1639 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1640 vert_causal_ctx_csty_symbol);
1643 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1646 av_assert2(!t1->mqc.raw);
1647 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1648 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1649 vert_causal_ctx_csty_symbol);
1652 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1653 ff_mqc_init_contexts(&t1->mqc);
1655 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1656 if (term_cnt >= cblk->nb_terminations) {
1657 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1658 return AVERROR_INVALIDDATA;
1660 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1661 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1662 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1663 pass_cnt, cblk->npasses);
1666 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1677 if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1678 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1679 cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1685 /* TODO: Verify dequantization for lossless case
1686 * comp->data can be float or int
1687 * band->stepsize can be float or int
1688 * depending on the type of DWT transformation.
1689 * see ISO/IEC 15444-1:2002 A.6.1 */
1691 /* Float dequantization of a codeblock.*/
1692 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1693 Jpeg2000Component *comp,
1694 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1697 int w = cblk->coord[0][1] - cblk->coord[0][0];
1698 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1699 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1700 int *src = t1->data + j*t1->stride;
1701 for (i = 0; i < w; ++i)
1702 datap[i] = src[i] * band->f_stepsize;
1706 /* Integer dequantization of a codeblock.*/
1707 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1708 Jpeg2000Component *comp,
1709 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1712 int w = cblk->coord[0][1] - cblk->coord[0][0];
1713 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1714 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1715 int *src = t1->data + j*t1->stride;
1716 if (band->i_stepsize == 32768) {
1717 for (i = 0; i < w; ++i)
1718 datap[i] = src[i] / 2;
1720 // This should be VERY uncommon
1721 for (i = 0; i < w; ++i)
1722 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1727 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1728 Jpeg2000Component *comp,
1729 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1732 int w = cblk->coord[0][1] - cblk->coord[0][0];
1733 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1734 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1735 int *src = t1->data + j*t1->stride;
1736 for (i = 0; i < w; ++i)
1737 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1741 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1746 for (i = 1; i < 3; i++) {
1747 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1748 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1751 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1752 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1757 for (i = 0; i < 3; i++)
1758 if (tile->codsty[0].transform == FF_DWT97)
1759 src[i] = tile->comp[i].f_data;
1761 src[i] = tile->comp[i].i_data;
1763 for (i = 0; i < 2; i++)
1764 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1766 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1769 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1771 Jpeg2000T1Context t1;
1773 int compno, reslevelno, bandno;
1775 /* Loop on tile components */
1776 for (compno = 0; compno < s->ncomponents; compno++) {
1777 Jpeg2000Component *comp = tile->comp + compno;
1778 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1781 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1783 /* Loop on resolution levels */
1784 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1785 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1787 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1788 int nb_precincts, precno;
1789 Jpeg2000Band *band = rlevel->band + bandno;
1790 int cblkno = 0, bandpos;
1792 bandpos = bandno + (reslevelno > 0);
1794 if (band->coord[0][0] == band->coord[0][1] ||
1795 band->coord[1][0] == band->coord[1][1])
1798 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1799 /* Loop on precincts */
1800 for (precno = 0; precno < nb_precincts; precno++) {
1801 Jpeg2000Prec *prec = band->prec + precno;
1803 /* Loop on codeblocks */
1805 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1808 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1809 int ret = decode_cblk(s, codsty, &t1, cblk,
1810 cblk->coord[0][1] - cblk->coord[0][0],
1811 cblk->coord[1][1] - cblk->coord[1][0],
1817 x = cblk->coord[0][0] - band->coord[0][0];
1818 y = cblk->coord[1][0] - band->coord[1][0];
1820 if (codsty->transform == FF_DWT97)
1821 dequantization_float(x, y, cblk, comp, &t1, band);
1822 else if (codsty->transform == FF_DWT97_INT)
1823 dequantization_int_97(x, y, cblk, comp, &t1, band);
1825 dequantization_int(x, y, cblk, comp, &t1, band);
1829 } /* end reslevel */
1833 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1838 #define WRITE_FRAME(D, PIXEL) \
1839 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1840 AVFrame * picture, int precision) \
1842 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1843 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1844 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1849 for (compno = 0; compno < s->ncomponents; compno++) { \
1850 Jpeg2000Component *comp = tile->comp + compno; \
1851 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1853 float *datap = comp->f_data; \
1854 int32_t *i_datap = comp->i_data; \
1855 int cbps = s->cbps[compno]; \
1856 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1860 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1862 y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno]; \
1863 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1864 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) { \
1867 x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno]; \
1868 dst = line + x * pixelsize + compno*!planar; \
1870 if (codsty->transform == FF_DWT97) { \
1871 for (; x < w; x++) { \
1872 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1873 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1874 val = av_clip(val, 0, (1 << cbps) - 1); \
1875 *dst = val << (precision - cbps); \
1880 for (; x < w; x++) { \
1881 int val = *i_datap + (1 << (cbps - 1)); \
1882 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1883 val = av_clip(val, 0, (1 << cbps) - 1); \
1884 *dst = val << (precision - cbps); \
1889 line += picture->linesize[plane] / sizeof(PIXEL); \
1895 WRITE_FRAME(8, uint8_t)
1896 WRITE_FRAME(16, uint16_t)
1900 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1901 int jobnr, int threadnr)
1903 Jpeg2000DecoderContext *s = avctx->priv_data;
1904 AVFrame *picture = td;
1905 Jpeg2000Tile *tile = s->tile + jobnr;
1908 tile_codeblocks(s, tile);
1910 /* inverse MCT transformation */
1911 if (tile->codsty[0].mct)
1912 mct_decode(s, tile);
1914 for (x = 0; x < s->ncomponents; x++) {
1915 if (s->cdef[x] < 0) {
1916 for (x = 0; x < s->ncomponents; x++) {
1919 if ((s->ncomponents & 1) == 0)
1920 s->cdef[s->ncomponents-1] = 0;
1925 if (s->precision <= 8) {
1926 write_frame_8(s, tile, picture, 8);
1928 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1929 picture->format == AV_PIX_FMT_RGB48 ||
1930 picture->format == AV_PIX_FMT_RGBA64 ||
1931 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1933 write_frame_16(s, tile, picture, precision);
1939 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1942 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1943 if (s->tile[tileno].comp) {
1944 for (compno = 0; compno < s->ncomponents; compno++) {
1945 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1946 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1948 ff_jpeg2000_cleanup(comp, codsty);
1950 av_freep(&s->tile[tileno].comp);
1954 memset(s->codsty, 0, sizeof(s->codsty));
1955 memset(s->qntsty, 0, sizeof(s->qntsty));
1956 memset(s->properties, 0, sizeof(s->properties));
1957 memset(&s->poc , 0, sizeof(s->poc));
1958 s->numXtiles = s->numYtiles = 0;
1962 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1964 Jpeg2000CodingStyle *codsty = s->codsty;
1965 Jpeg2000QuantStyle *qntsty = s->qntsty;
1966 Jpeg2000POC *poc = &s->poc;
1967 uint8_t *properties = s->properties;
1974 if (bytestream2_get_bytes_left(&s->g) < 2) {
1975 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1979 marker = bytestream2_get_be16u(&s->g);
1980 oldpos = bytestream2_tell(&s->g);
1982 if (marker == JPEG2000_SOD) {
1984 Jpeg2000TilePart *tp;
1987 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1988 return AVERROR_INVALIDDATA;
1990 if (s->curtileno < 0) {
1991 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1992 return AVERROR_INVALIDDATA;
1995 tile = s->tile + s->curtileno;
1996 tp = tile->tile_part + tile->tp_idx;
1997 if (tp->tp_end < s->g.buffer) {
1998 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1999 return AVERROR_INVALIDDATA;
2002 if (tile->has_ppt && tile->tp_idx == 0) {
2003 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2006 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2007 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2011 if (marker == JPEG2000_EOC)
2014 len = bytestream2_get_be16(&s->g);
2015 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2016 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2017 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2018 return AVERROR_INVALIDDATA;
2020 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2026 if (s->ncomponents) {
2027 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2028 return AVERROR_INVALIDDATA;
2032 s->numXtiles = s->numYtiles = 0;
2035 ret = get_coc(s, codsty, properties);
2038 ret = get_cod(s, codsty, properties);
2041 ret = get_qcc(s, len, qntsty, properties);
2044 ret = get_qcd(s, len, qntsty, properties);
2047 ret = get_poc(s, len, poc);
2050 if (!(ret = get_sot(s, len))) {
2051 av_assert1(s->curtileno >= 0);
2052 codsty = s->tile[s->curtileno].codsty;
2053 qntsty = s->tile[s->curtileno].qntsty;
2054 poc = &s->tile[s->curtileno].poc;
2055 properties = s->tile[s->curtileno].properties;
2059 // the PLM marker is ignored
2061 // the comment is ignored
2062 bytestream2_skip(&s->g, len - 2);
2065 // Tile-part lengths
2066 ret = get_tlm(s, len);
2069 // Packet length, tile-part header
2070 ret = get_plt(s, len);
2073 // Packed headers, tile-part header
2074 ret = get_ppt(s, len);
2077 av_log(s->avctx, AV_LOG_ERROR,
2078 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2079 marker, bytestream2_tell(&s->g) - 4);
2080 bytestream2_skip(&s->g, len - 2);
2083 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2084 av_log(s->avctx, AV_LOG_ERROR,
2085 "error during processing marker segment %.4"PRIx16"\n",
2087 return ret ? ret : -1;
2093 /* Read bit stream packets --> T2 operation. */
2094 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2099 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2100 Jpeg2000Tile *tile = s->tile + tileno;
2102 if ((ret = init_tile(s, tileno)) < 0)
2105 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2112 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2114 uint32_t atom_size, atom, atom_end;
2115 int search_range = 10;
2119 bytestream2_get_bytes_left(&s->g) >= 8) {
2120 atom_size = bytestream2_get_be32u(&s->g);
2121 atom = bytestream2_get_be32u(&s->g);
2122 if (atom_size == 1) {
2123 if (bytestream2_get_be32u(&s->g)) {
2124 avpriv_request_sample(s->avctx, "Huge atom");
2127 atom_size = bytestream2_get_be32u(&s->g);
2128 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2130 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2133 if (atom == JP2_CODESTREAM)
2136 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2139 if (atom == JP2_HEADER &&
2141 uint32_t atom2_size, atom2, atom2_end;
2143 atom2_size = bytestream2_get_be32u(&s->g);
2144 atom2 = bytestream2_get_be32u(&s->g);
2145 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2146 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2149 if (atom2 == JP2_CODESTREAM) {
2151 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2152 int method = bytestream2_get_byteu(&s->g);
2153 bytestream2_skipu(&s->g, 2);
2155 s->colour_space = bytestream2_get_be32u(&s->g);
2157 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2158 int i, size, colour_count, colour_channels, colour_depth[3];
2159 colour_count = bytestream2_get_be16u(&s->g);
2160 colour_channels = bytestream2_get_byteu(&s->g);
2161 // FIXME: Do not ignore channel_sign
2162 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2163 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2164 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2165 size = (colour_depth[0] + 7 >> 3) * colour_count +
2166 (colour_depth[1] + 7 >> 3) * colour_count +
2167 (colour_depth[2] + 7 >> 3) * colour_count;
2168 if (colour_count > AVPALETTE_COUNT ||
2169 colour_channels != 3 ||
2170 colour_depth[0] > 16 ||
2171 colour_depth[1] > 16 ||
2172 colour_depth[2] > 16 ||
2173 atom2_size < size) {
2174 avpriv_request_sample(s->avctx, "Unknown palette");
2175 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2179 for (i = 0; i < colour_count; i++) {
2181 if (colour_depth[0] <= 8) {
2182 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2183 r |= r >> colour_depth[0];
2185 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2187 if (colour_depth[1] <= 8) {
2188 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2189 g |= g >> colour_depth[1];
2191 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2193 if (colour_depth[2] <= 8) {
2194 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2195 b |= b >> colour_depth[2];
2197 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2199 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2201 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2202 int n = bytestream2_get_be16u(&s->g);
2204 int cn = bytestream2_get_be16(&s->g);
2205 int av_unused typ = bytestream2_get_be16(&s->g);
2206 int asoc = bytestream2_get_be16(&s->g);
2207 if (cn < 4 && asoc < 4)
2210 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2211 int64_t vnum, vden, hnum, hden, vexp, hexp;
2213 bytestream2_skip(&s->g, 4);
2214 resx = bytestream2_get_be32u(&s->g);
2215 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2216 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2219 vnum = bytestream2_get_be16u(&s->g);
2220 vden = bytestream2_get_be16u(&s->g);
2221 hnum = bytestream2_get_be16u(&s->g);
2222 hden = bytestream2_get_be16u(&s->g);
2223 vexp = bytestream2_get_byteu(&s->g);
2224 hexp = bytestream2_get_byteu(&s->g);
2225 if (!vnum || !vden || !hnum || !hden) {
2226 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2227 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2237 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2238 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2239 av_reduce(&s->sar.den, &s->sar.num,
2240 hnum * vden * pow(10, hexp),
2241 vnum * hden * pow(10, vexp),
2244 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2245 } while (atom_end - atom2_end >= 8);
2249 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2255 static av_cold void jpeg2000_init_static_data(void)
2257 ff_jpeg2000_init_tier1_luts();
2258 ff_mqc_init_context_tables();
2261 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2263 static AVOnce init_static_once = AV_ONCE_INIT;
2264 Jpeg2000DecoderContext *s = avctx->priv_data;
2266 ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2267 ff_jpeg2000dsp_init(&s->dsp);
2272 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2273 int *got_frame, AVPacket *avpkt)
2275 Jpeg2000DecoderContext *s = avctx->priv_data;
2276 ThreadFrame frame = { .f = data };
2277 AVFrame *picture = data;
2281 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2283 memset(s->cdef, -1, sizeof(s->cdef));
2285 if (bytestream2_get_bytes_left(&s->g) < 2) {
2286 ret = AVERROR_INVALIDDATA;
2290 // check if the image is in jp2 format
2291 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2292 (bytestream2_get_be32u(&s->g) == 12) &&
2293 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2294 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2295 if (!jp2_find_codestream(s)) {
2296 av_log(avctx, AV_LOG_ERROR,
2297 "Could not find Jpeg2000 codestream atom.\n");
2298 ret = AVERROR_INVALIDDATA;
2302 bytestream2_seek(&s->g, 0, SEEK_SET);
2305 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2306 bytestream2_skip(&s->g, 1);
2308 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2309 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2310 ret = AVERROR_INVALIDDATA;
2313 if (ret = jpeg2000_read_main_headers(s))
2316 /* get picture buffer */
2317 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2319 picture->pict_type = AV_PICTURE_TYPE_I;
2320 picture->key_frame = 1;
2322 if (ret = jpeg2000_read_bitstream_packets(s))
2325 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2327 jpeg2000_dec_cleanup(s);
2331 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2332 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2333 if (s->sar.num && s->sar.den)
2334 avctx->sample_aspect_ratio = s->sar;
2335 s->sar.num = s->sar.den = 0;
2337 return bytestream2_tell(&s->g);
2340 jpeg2000_dec_cleanup(s);
2344 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2345 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2347 static const AVOption options[] = {
2348 { "lowres", "Lower the decoding resolution by a power of two",
2349 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2353 static const AVClass jpeg2000_class = {
2354 .class_name = "jpeg2000",
2355 .item_name = av_default_item_name,
2357 .version = LIBAVUTIL_VERSION_INT,
2360 AVCodec ff_jpeg2000_decoder = {
2362 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2363 .type = AVMEDIA_TYPE_VIDEO,
2364 .id = AV_CODEC_ID_JPEG2000,
2365 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2366 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2367 .init = jpeg2000_decode_init,
2368 .decode = jpeg2000_decode_frame,
2369 .priv_class = &jpeg2000_class,
2371 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)