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
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
33 #include "bytestream.h"
38 #define JP2_SIG_TYPE 0x6A502020
39 #define JP2_SIG_VALUE 0x0D0A870A
40 #define JP2_CODESTREAM 0x6A703263
41 #define JP2_HEADER 0x6A703268
46 typedef struct Jpeg2000TilePart {
47 uint8_t tile_index; // Tile index who refers the tile-part
48 const uint8_t *tp_end;
49 GetByteContext tpg; // bit stream in tile-part
52 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
53 * one per component, so tile_part elements have a size of 3 */
54 typedef struct Jpeg2000Tile {
55 Jpeg2000Component *comp;
56 uint8_t properties[4];
57 Jpeg2000CodingStyle codsty[4];
58 Jpeg2000QuantStyle qntsty[4];
59 Jpeg2000TilePart tile_part[4];
60 uint16_t tp_idx; // Tile-part index
63 typedef struct Jpeg2000DecoderContext {
65 AVCodecContext *avctx;
69 int image_offset_x, image_offset_y;
70 int tile_offset_x, tile_offset_y;
71 uint8_t cbps[4]; // bits per sample in particular components
72 uint8_t sgnd[4]; // if a component is signed
73 uint8_t properties[4];
78 uint32_t palette[256];
81 int tile_width, tile_height;
82 unsigned numXtiles, numYtiles;
85 Jpeg2000CodingStyle codsty[4];
86 Jpeg2000QuantStyle qntsty[4];
94 /*options parameters*/
96 } Jpeg2000DecoderContext;
98 /* get_bits functions for JPEG2000 packet bitstream
99 * It is a get_bit function with a bit-stuffing routine. If the value of the
100 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
101 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
102 static int get_bits(Jpeg2000DecoderContext *s, int n)
108 if (s->bit_index == 0) {
109 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
112 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
117 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
119 if (bytestream2_get_byte(&s->g) == 0xff)
120 bytestream2_skip(&s->g, 1);
124 /* decode the value stored in node */
125 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
128 Jpeg2000TgtNode *stack[30];
129 int sp = -1, curval = 0;
132 return AVERROR_INVALIDDATA;
134 while (node && !node->vis) {
142 curval = stack[sp]->val;
144 while (curval < threshold && sp >= 0) {
145 if (curval < stack[sp]->val)
146 curval = stack[sp]->val;
147 while (curval < threshold) {
149 if ((ret = get_bits(s, 1)) > 0) {
157 stack[sp]->val = curval;
163 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
164 int bpc, uint32_t log2_chroma_wh, int pal8)
167 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
169 if (desc->nb_components != components) {
173 switch (components) {
175 match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
176 (log2_chroma_wh >> 14 & 3) == 0 &&
177 (log2_chroma_wh >> 12 & 3) == 0;
179 match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
180 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
181 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
183 match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
184 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
185 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
188 match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
189 (log2_chroma_wh >> 2 & 3) == 0 &&
190 (log2_chroma_wh & 3) == 0 &&
191 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
196 // pix_fmts with lower bpp have to be listed before
197 // similar pix_fmts with higher bpp.
198 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
199 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
200 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
201 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
202 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
203 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
204 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
205 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
206 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
207 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
208 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
209 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
210 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
211 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
213 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
214 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
215 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
216 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS};
217 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
222 /* marker segments */
223 /* get sizes and offsets of image, tiles; number of components */
224 static int get_siz(Jpeg2000DecoderContext *s)
228 uint32_t log2_chroma_wh = 0;
229 const enum AVPixelFormat *possible_fmts = NULL;
230 int possible_fmts_nb = 0;
232 if (bytestream2_get_bytes_left(&s->g) < 36)
233 return AVERROR_INVALIDDATA;
235 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
236 s->width = bytestream2_get_be32u(&s->g); // Width
237 s->height = bytestream2_get_be32u(&s->g); // Height
238 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
239 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
240 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
241 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
242 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
243 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
244 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
246 if (s->image_offset_x || s->image_offset_y) {
247 avpriv_request_sample(s->avctx, "Support for image offsets");
248 return AVERROR_PATCHWELCOME;
251 if (ncomponents <= 0) {
252 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
254 return AVERROR_INVALIDDATA;
257 if (ncomponents > 4) {
258 avpriv_request_sample(s->avctx, "Support for %d components",
260 return AVERROR_PATCHWELCOME;
263 s->ncomponents = ncomponents;
265 if (s->tile_width <= 0 || s->tile_height <= 0) {
266 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
267 s->tile_width, s->tile_height);
268 return AVERROR_INVALIDDATA;
271 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
272 return AVERROR_INVALIDDATA;
274 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
275 uint8_t x = bytestream2_get_byteu(&s->g);
276 s->cbps[i] = (x & 0x7f) + 1;
277 s->precision = FFMAX(s->cbps[i], s->precision);
278 s->sgnd[i] = !!(x & 0x80);
279 s->cdx[i] = bytestream2_get_byteu(&s->g);
280 s->cdy[i] = bytestream2_get_byteu(&s->g);
281 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
282 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
283 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
284 return AVERROR_INVALIDDATA;
286 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
289 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
290 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
292 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
293 s->numXtiles = s->numYtiles = 0;
294 return AVERROR(EINVAL);
297 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
299 s->numXtiles = s->numYtiles = 0;
300 return AVERROR(ENOMEM);
303 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
304 Jpeg2000Tile *tile = s->tile + i;
306 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
308 return AVERROR(ENOMEM);
311 /* compute image size with reduction factor */
312 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
313 s->reduction_factor);
314 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
315 s->reduction_factor);
317 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
318 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
319 possible_fmts = xyz_pix_fmts;
320 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
322 switch (s->colour_space) {
324 possible_fmts = rgb_pix_fmts;
325 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
328 possible_fmts = gray_pix_fmts;
329 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
332 possible_fmts = yuv_pix_fmts;
333 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
336 possible_fmts = all_pix_fmts;
337 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
341 for (i = 0; i < possible_fmts_nb; ++i) {
342 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
343 s->avctx->pix_fmt = possible_fmts[i];
347 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
348 av_log(s->avctx, AV_LOG_ERROR,
349 "Unknown pix_fmt, profile: %d, colour_space: %d, "
350 "components: %d, precision: %d, "
351 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
352 s->avctx->profile, s->colour_space, ncomponents, s->precision,
353 ncomponents > 2 ? s->cdx[1] : 0,
354 ncomponents > 2 ? s->cdy[1] : 0,
355 ncomponents > 2 ? s->cdx[2] : 0,
356 ncomponents > 2 ? s->cdy[2] : 0);
358 s->avctx->bits_per_raw_sample = s->precision;
362 /* get common part for COD and COC segments */
363 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
367 if (bytestream2_get_bytes_left(&s->g) < 5)
368 return AVERROR_INVALIDDATA;
370 /* nreslevels = number of resolution levels
371 = number of decomposition level +1 */
372 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
373 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
374 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
375 return AVERROR_INVALIDDATA;
378 if (c->nreslevels <= s->reduction_factor) {
379 /* we are forced to update reduction_factor as its requested value is
380 not compatible with this bitstream, and as we might have used it
381 already in setup earlier we have to fail this frame until
382 reinitialization is implemented */
383 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
384 s->reduction_factor = c->nreslevels - 1;
385 return AVERROR(EINVAL);
388 /* compute number of resolution levels to decode */
389 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
391 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
392 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
394 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
395 c->log2_cblk_width + c->log2_cblk_height > 12) {
396 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
397 return AVERROR_INVALIDDATA;
400 if (c->log2_cblk_width > 6 || c->log2_cblk_height > 6) {
401 avpriv_request_sample(s->avctx, "cblk size > 64");
402 return AVERROR_PATCHWELCOME;
405 c->cblk_style = bytestream2_get_byteu(&s->g);
406 if (c->cblk_style != 0) { // cblk style
407 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
409 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
410 /* set integer 9/7 DWT in case of BITEXACT flag */
411 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
412 c->transform = FF_DWT97_INT;
414 if (c->csty & JPEG2000_CSTY_PREC) {
416 for (i = 0; i < c->nreslevels; i++) {
417 byte = bytestream2_get_byte(&s->g);
418 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
419 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
422 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
423 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
428 /* get coding parameters for a particular tile or whole image*/
429 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
432 Jpeg2000CodingStyle tmp;
435 if (bytestream2_get_bytes_left(&s->g) < 5)
436 return AVERROR_INVALIDDATA;
438 tmp.csty = bytestream2_get_byteu(&s->g);
440 // get progression order
441 tmp.prog_order = bytestream2_get_byteu(&s->g);
443 tmp.nlayers = bytestream2_get_be16u(&s->g);
444 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
446 if (tmp.mct && s->ncomponents < 3) {
447 av_log(s->avctx, AV_LOG_ERROR,
448 "MCT %d with too few components (%d)\n",
449 tmp.mct, s->ncomponents);
450 return AVERROR_INVALIDDATA;
453 if ((ret = get_cox(s, &tmp)) < 0)
456 for (compno = 0; compno < s->ncomponents; compno++)
457 if (!(properties[compno] & HAD_COC))
458 memcpy(c + compno, &tmp, sizeof(tmp));
462 /* Get coding parameters for a component in the whole image or a
463 * particular tile. */
464 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
469 if (bytestream2_get_bytes_left(&s->g) < 2)
470 return AVERROR_INVALIDDATA;
472 compno = bytestream2_get_byteu(&s->g);
474 if (compno >= s->ncomponents) {
475 av_log(s->avctx, AV_LOG_ERROR,
476 "Invalid compno %d. There are %d components in the image.\n",
477 compno, s->ncomponents);
478 return AVERROR_INVALIDDATA;
482 c->csty = bytestream2_get_byteu(&s->g);
484 if ((ret = get_cox(s, c)) < 0)
487 properties[compno] |= HAD_COC;
491 /* Get common part for QCD and QCC segments. */
492 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
496 if (bytestream2_get_bytes_left(&s->g) < 1)
497 return AVERROR_INVALIDDATA;
499 x = bytestream2_get_byteu(&s->g); // Sqcd
501 q->nguardbits = x >> 5;
502 q->quantsty = x & 0x1f;
504 if (q->quantsty == JPEG2000_QSTY_NONE) {
506 if (bytestream2_get_bytes_left(&s->g) < n ||
507 n > JPEG2000_MAX_DECLEVELS*3)
508 return AVERROR_INVALIDDATA;
509 for (i = 0; i < n; i++)
510 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
511 } else if (q->quantsty == JPEG2000_QSTY_SI) {
512 if (bytestream2_get_bytes_left(&s->g) < 2)
513 return AVERROR_INVALIDDATA;
514 x = bytestream2_get_be16u(&s->g);
515 q->expn[0] = x >> 11;
516 q->mant[0] = x & 0x7ff;
517 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
518 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
519 q->expn[i] = curexpn;
520 q->mant[i] = q->mant[0];
524 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
525 n > JPEG2000_MAX_DECLEVELS*3)
526 return AVERROR_INVALIDDATA;
527 for (i = 0; i < n; i++) {
528 x = bytestream2_get_be16u(&s->g);
529 q->expn[i] = x >> 11;
530 q->mant[i] = x & 0x7ff;
536 /* Get quantization parameters for a particular tile or a whole image. */
537 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
540 Jpeg2000QuantStyle tmp;
543 memset(&tmp, 0, sizeof(tmp));
545 if ((ret = get_qcx(s, n, &tmp)) < 0)
547 for (compno = 0; compno < s->ncomponents; compno++)
548 if (!(properties[compno] & HAD_QCC))
549 memcpy(q + compno, &tmp, sizeof(tmp));
553 /* Get quantization parameters for a component in the whole image
554 * on in a particular tile. */
555 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
560 if (bytestream2_get_bytes_left(&s->g) < 1)
561 return AVERROR_INVALIDDATA;
563 compno = bytestream2_get_byteu(&s->g);
565 if (compno >= s->ncomponents) {
566 av_log(s->avctx, AV_LOG_ERROR,
567 "Invalid compno %d. There are %d components in the image.\n",
568 compno, s->ncomponents);
569 return AVERROR_INVALIDDATA;
572 properties[compno] |= HAD_QCC;
573 return get_qcx(s, n - 1, q + compno);
576 /* Get start of tile segment. */
577 static int get_sot(Jpeg2000DecoderContext *s, int n)
579 Jpeg2000TilePart *tp;
584 if (bytestream2_get_bytes_left(&s->g) < 8)
585 return AVERROR_INVALIDDATA;
588 Isot = bytestream2_get_be16u(&s->g); // Isot
589 if (Isot >= s->numXtiles * s->numYtiles)
590 return AVERROR_INVALIDDATA;
593 Psot = bytestream2_get_be32u(&s->g); // Psot
594 TPsot = bytestream2_get_byteu(&s->g); // TPsot
596 /* Read TNSot but not used */
597 bytestream2_get_byteu(&s->g); // TNsot
599 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
600 av_log(s->avctx, AV_LOG_ERROR, "Psot %d too big\n", Psot);
601 return AVERROR_INVALIDDATA;
604 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
605 avpriv_request_sample(s->avctx, "Support for %d components", TPsot);
606 return AVERROR_PATCHWELCOME;
609 s->tile[Isot].tp_idx = TPsot;
610 tp = s->tile[Isot].tile_part + TPsot;
611 tp->tile_index = Isot;
612 tp->tp_end = s->g.buffer + Psot - n - 2;
615 Jpeg2000Tile *tile = s->tile + s->curtileno;
618 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
619 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
625 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
626 * Used to know the number of tile parts and lengths.
627 * There may be multiple TLMs in the header.
628 * TODO: The function is not used for tile-parts management, nor anywhere else.
629 * It can be useful to allocate memory for tile parts, before managing the SOT
630 * markers. Parsing the TLM header is needed to increment the input header
632 * This marker is mandatory for DCI. */
633 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
635 uint8_t Stlm, ST, SP, tile_tlm, i;
636 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
637 Stlm = bytestream2_get_byte(&s->g);
639 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
640 ST = (Stlm >> 4) & 0x03;
641 // TODO: Manage case of ST = 0b11 --> raise error
642 SP = (Stlm >> 6) & 0x01;
643 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
644 for (i = 0; i < tile_tlm; i++) {
649 bytestream2_get_byte(&s->g);
652 bytestream2_get_be16(&s->g);
655 bytestream2_get_be32(&s->g);
659 bytestream2_get_be16(&s->g);
661 bytestream2_get_be32(&s->g);
667 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
670 int tilex = tileno % s->numXtiles;
671 int tiley = tileno / s->numXtiles;
672 Jpeg2000Tile *tile = s->tile + tileno;
675 return AVERROR(ENOMEM);
677 for (compno = 0; compno < s->ncomponents; compno++) {
678 Jpeg2000Component *comp = tile->comp + compno;
679 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
680 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
681 int ret; // global bandno
683 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
684 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
685 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
686 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
688 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
689 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
690 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
691 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
693 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
694 s->cbps[compno], s->cdx[compno],
695 s->cdy[compno], s->avctx))
701 /* Read the number of coding passes. */
702 static int getnpasses(Jpeg2000DecoderContext *s)
709 if ((num = get_bits(s, 2)) != 3)
710 return num < 0 ? num : 3 + num;
711 if ((num = get_bits(s, 5)) != 31)
712 return num < 0 ? num : 6 + num;
713 num = get_bits(s, 7);
714 return num < 0 ? num : 37 + num;
717 static int getlblockinc(Jpeg2000DecoderContext *s)
720 while (ret = get_bits(s, 1)) {
728 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
729 Jpeg2000CodingStyle *codsty,
730 Jpeg2000ResLevel *rlevel, int precno,
731 int layno, uint8_t *expn, int numgbits)
733 int bandno, cblkno, ret, nb_code_blocks;
735 if (!(ret = get_bits(s, 1))) {
741 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
742 Jpeg2000Band *band = rlevel->band + bandno;
743 Jpeg2000Prec *prec = band->prec + precno;
745 if (band->coord[0][0] == band->coord[0][1] ||
746 band->coord[1][0] == band->coord[1][1])
748 nb_code_blocks = prec->nb_codeblocks_height *
749 prec->nb_codeblocks_width;
750 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
751 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
752 int incl, newpasses, llen;
755 incl = get_bits(s, 1);
757 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
763 if (!cblk->npasses) {
764 int v = expn[bandno] + numgbits - 1 -
765 tag_tree_decode(s, prec->zerobits + cblkno, 100);
767 av_log(s->avctx, AV_LOG_ERROR,
768 "nonzerobits %d invalid\n", v);
769 return AVERROR_INVALIDDATA;
771 cblk->nonzerobits = v;
773 if ((newpasses = getnpasses(s)) < 0)
775 if ((llen = getlblockinc(s)) < 0)
777 cblk->lblock += llen;
778 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
780 if (ret > sizeof(cblk->data)) {
781 avpriv_request_sample(s->avctx,
782 "Block with lengthinc greater than %zu",
784 return AVERROR_PATCHWELCOME;
786 cblk->lengthinc = ret;
787 cblk->npasses += newpasses;
792 if (codsty->csty & JPEG2000_CSTY_EPH) {
793 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
794 bytestream2_skip(&s->g, 2);
796 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
799 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
800 Jpeg2000Band *band = rlevel->band + bandno;
801 Jpeg2000Prec *prec = band->prec + precno;
803 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
804 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
805 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
806 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
807 || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
809 av_log(s->avctx, AV_LOG_ERROR,
810 "Block length %d or lengthinc %d is too large\n",
811 cblk->length, cblk->lengthinc);
812 return AVERROR_INVALIDDATA;
815 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
816 cblk->length += cblk->lengthinc;
823 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
826 int layno, reslevelno, compno, precno, ok_reslevel;
830 switch (tile->codsty[0].prog_order) {
831 case JPEG2000_PGOD_RLCP:
832 avpriv_request_sample(s->avctx, "Progression order RLCP");
834 case JPEG2000_PGOD_LRCP:
835 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
837 for (reslevelno = 0; ok_reslevel; reslevelno++) {
839 for (compno = 0; compno < s->ncomponents; compno++) {
840 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
841 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
842 if (reslevelno < codsty->nreslevels) {
843 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
846 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
847 if ((ret = jpeg2000_decode_packet(s,
850 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
851 qntsty->nguardbits)) < 0)
859 case JPEG2000_PGOD_CPRL:
860 for (compno = 0; compno < s->ncomponents; compno++) {
861 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
862 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
864 /* Set bit stream buffer address according to tile-part.
865 * For DCinema one tile-part per component, so can be
866 * indexed by component. */
867 s->g = tile->tile_part[compno].tpg;
869 /* Position loop (y axis)
870 * TODO: Automate computing of step 256.
871 * Fixed here, but to be computed before entering here. */
872 for (y = 0; y < s->height; y += 256) {
873 /* Position loop (y axis)
874 * TODO: automate computing of step 256.
875 * Fixed here, but to be computed before entering here. */
876 for (x = 0; x < s->width; x += 256) {
877 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
879 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
880 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
882 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
883 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
886 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
887 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
890 // check if a precinct exists
891 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
892 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
893 precno = prcx + rlevel->num_precincts_x * prcy;
895 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y)
896 return AVERROR_PATCHWELCOME;
898 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
899 if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
901 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
902 qntsty->nguardbits)) < 0)
911 case JPEG2000_PGOD_RPCL:
912 avpriv_request_sample(s->avctx, "Progression order RPCL");
913 ret = AVERROR_PATCHWELCOME;
916 case JPEG2000_PGOD_PCRL:
917 avpriv_request_sample(s->avctx, "Progression order PCRL");
918 ret = AVERROR_PATCHWELCOME;
925 /* EOC marker reached */
926 bytestream2_skip(&s->g, 2);
931 /* TIER-1 routines */
932 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
933 int bpno, int bandno, int bpass_csty_symbol,
934 int vert_causal_ctx_csty_symbol)
936 int mask = 3 << (bpno - 1), y0, x, y;
938 for (y0 = 0; y0 < height; y0 += 4)
939 for (x = 0; x < width; x++)
940 for (y = y0; y < height && y < y0 + 4; y++) {
941 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
942 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
944 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
945 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
946 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
947 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
948 if (bpass_csty_symbol)
949 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
951 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
954 ff_jpeg2000_set_significance(t1, x, y,
957 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
962 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
968 phalf = 1 << (bpno - 1);
971 for (y0 = 0; y0 < height; y0 += 4)
972 for (x = 0; x < width; x++)
973 for (y = y0; y < height && y < y0 + 4; y++)
974 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
975 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
976 int r = ff_mqc_decode(&t1->mqc,
977 t1->mqc.cx_states + ctxno)
979 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
980 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
984 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
985 int width, int height, int bpno, int bandno,
986 int seg_symbols, int vert_causal_ctx_csty_symbol)
988 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
990 for (y0 = 0; y0 < height; y0 += 4) {
991 for (x = 0; x < width; x++) {
992 if (y0 + 3 < height &&
993 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
994 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
995 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
996 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
997 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
999 runlen = ff_mqc_decode(&t1->mqc,
1000 t1->mqc.cx_states + MQC_CX_UNI);
1001 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1010 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1012 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1013 int flags_mask = -1;
1014 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1015 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
1016 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
1022 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
1024 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
1025 t1->mqc.cx_states + ctxno) ^
1028 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
1031 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
1037 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1038 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1039 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1040 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1042 av_log(s->avctx, AV_LOG_ERROR,
1043 "Segmentation symbol value incorrect\n");
1047 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1048 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1049 int width, int height, int bandpos)
1051 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
1052 int clnpass_cnt = 0;
1053 int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
1054 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1056 av_assert0(width <= JPEG2000_MAX_CBLKW);
1057 av_assert0(height <= JPEG2000_MAX_CBLKH);
1059 for (y = 0; y < height; y++)
1060 memset(t1->data[y], 0, width * sizeof(**t1->data));
1062 /* If code-block contains no compressed data: nothing to do. */
1066 for (y = 0; y < height + 2; y++)
1067 memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
1069 cblk->data[cblk->length] = 0xff;
1070 cblk->data[cblk->length+1] = 0xff;
1071 ff_mqc_initdec(&t1->mqc, cblk->data);
1076 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1077 bpass_csty_symbol && (clnpass_cnt >= 4),
1078 vert_causal_ctx_csty_symbol);
1081 decode_refpass(t1, width, height, bpno + 1);
1082 if (bpass_csty_symbol && clnpass_cnt >= 4)
1083 ff_mqc_initdec(&t1->mqc, cblk->data);
1086 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1087 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1088 vert_causal_ctx_csty_symbol);
1089 clnpass_cnt = clnpass_cnt + 1;
1090 if (bpass_csty_symbol && clnpass_cnt >= 4)
1091 ff_mqc_initdec(&t1->mqc, cblk->data);
1104 /* TODO: Verify dequantization for lossless case
1105 * comp->data can be float or int
1106 * band->stepsize can be float or int
1107 * depending on the type of DWT transformation.
1108 * see ISO/IEC 15444-1:2002 A.6.1 */
1110 /* Float dequantization of a codeblock.*/
1111 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1112 Jpeg2000Component *comp,
1113 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1116 int w = cblk->coord[0][1] - cblk->coord[0][0];
1117 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1118 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1119 int *src = t1->data[j];
1120 for (i = 0; i < w; ++i)
1121 datap[i] = src[i] * band->f_stepsize;
1125 /* Integer dequantization of a codeblock.*/
1126 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1127 Jpeg2000Component *comp,
1128 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1131 int w = cblk->coord[0][1] - cblk->coord[0][0];
1132 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1133 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1134 int *src = t1->data[j];
1135 for (i = 0; i < w; ++i)
1136 datap[i] = (src[i] * band->i_stepsize + (1 << 14)) >> 15;
1140 /* Inverse ICT parameters in float and integer.
1141 * int value = (float value) * (1<<16) */
1142 static const float f_ict_params[4] = {
1148 static const int i_ict_params[4] = {
1155 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1158 int32_t *src[3], i0, i1, i2;
1159 float *srcf[3], i0f, i1f, i2f;
1161 for (i = 1; i < 3; i++)
1162 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1163 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1167 for (i = 0; i < 3; i++)
1168 if (tile->codsty[0].transform == FF_DWT97)
1169 srcf[i] = tile->comp[i].f_data;
1171 src [i] = tile->comp[i].i_data;
1173 for (i = 0; i < 2; i++)
1174 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1176 switch (tile->codsty[0].transform) {
1178 for (i = 0; i < csize; i++) {
1179 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
1180 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
1181 - (f_ict_params[2] * *srcf[2]);
1182 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
1189 for (i = 0; i < csize; i++) {
1190 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1191 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1192 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1193 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1200 for (i = 0; i < csize; i++) {
1201 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1212 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1215 int compno, reslevelno, bandno;
1219 Jpeg2000T1Context t1;
1221 /* Loop on tile components */
1222 for (compno = 0; compno < s->ncomponents; compno++) {
1223 Jpeg2000Component *comp = tile->comp + compno;
1224 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1226 /* Loop on resolution levels */
1227 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1228 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1230 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1231 int nb_precincts, precno;
1232 Jpeg2000Band *band = rlevel->band + bandno;
1233 int cblkno = 0, bandpos;
1235 bandpos = bandno + (reslevelno > 0);
1237 if (band->coord[0][0] == band->coord[0][1] ||
1238 band->coord[1][0] == band->coord[1][1])
1241 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1242 /* Loop on precincts */
1243 for (precno = 0; precno < nb_precincts; precno++) {
1244 Jpeg2000Prec *prec = band->prec + precno;
1246 /* Loop on codeblocks */
1247 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1249 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1250 decode_cblk(s, codsty, &t1, cblk,
1251 cblk->coord[0][1] - cblk->coord[0][0],
1252 cblk->coord[1][1] - cblk->coord[1][0],
1255 x = cblk->coord[0][0];
1256 y = cblk->coord[1][0];
1258 if (codsty->transform == FF_DWT97)
1259 dequantization_float(x, y, cblk, comp, &t1, band);
1261 dequantization_int(x, y, cblk, comp, &t1, band);
1265 } /* end reslevel */
1268 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1271 /* inverse MCT transformation */
1272 if (tile->codsty[0].mct)
1273 mct_decode(s, tile);
1275 if (s->cdef[0] < 0) {
1276 for (x = 0; x < s->ncomponents; x++)
1278 if ((s->ncomponents & 1) == 0)
1279 s->cdef[s->ncomponents-1] = 0;
1282 if (s->precision <= 8) {
1283 for (compno = 0; compno < s->ncomponents; compno++) {
1284 Jpeg2000Component *comp = tile->comp + compno;
1285 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1286 float *datap = comp->f_data;
1287 int32_t *i_datap = comp->i_data;
1288 int cbps = s->cbps[compno];
1289 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1290 int planar = !!picture->data[2];
1291 int pixelsize = planar ? 1 : s->ncomponents;
1295 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1298 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1299 line = picture->data[plane] + y / s->cdy[compno] * picture->linesize[plane];
1300 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1303 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1304 dst = line + x / s->cdx[compno] * pixelsize + compno*!planar;
1306 if (codsty->transform == FF_DWT97) {
1307 for (; x < w; x += s->cdx[compno]) {
1308 int val = lrintf(*datap) + (1 << (cbps - 1));
1309 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1310 val = av_clip(val, 0, (1 << cbps) - 1);
1311 *dst = val << (8 - cbps);
1316 for (; x < w; x += s->cdx[compno]) {
1317 int val = *i_datap + (1 << (cbps - 1));
1318 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1319 val = av_clip(val, 0, (1 << cbps) - 1);
1320 *dst = val << (8 - cbps);
1325 line += picture->linesize[plane];
1329 for (compno = 0; compno < s->ncomponents; compno++) {
1330 Jpeg2000Component *comp = tile->comp + compno;
1331 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1332 float *datap = comp->f_data;
1333 int32_t *i_datap = comp->i_data;
1335 int cbps = s->cbps[compno];
1336 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1337 int planar = !!picture->data[2];
1338 int pixelsize = planar ? 1 : s->ncomponents;
1342 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1344 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1345 linel = (uint16_t *)picture->data[plane] + y / s->cdy[compno] * (picture->linesize[plane] >> 1);
1346 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1349 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1350 dst = linel + (x / s->cdx[compno] * pixelsize + compno*!planar);
1351 if (codsty->transform == FF_DWT97) {
1352 for (; x < w; x += s-> cdx[compno]) {
1353 int val = lrintf(*datap) + (1 << (cbps - 1));
1354 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1355 val = av_clip(val, 0, (1 << cbps) - 1);
1356 /* align 12 bit values in little-endian mode */
1357 *dst = val << (16 - cbps);
1362 for (; x < w; x += s-> cdx[compno]) {
1363 int val = *i_datap + (1 << (cbps - 1));
1364 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1365 val = av_clip(val, 0, (1 << cbps) - 1);
1366 /* align 12 bit values in little-endian mode */
1367 *dst = val << (16 - cbps);
1372 linel += picture->linesize[plane] >> 1;
1380 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1383 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1384 if (s->tile[tileno].comp) {
1385 for (compno = 0; compno < s->ncomponents; compno++) {
1386 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1387 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1389 ff_jpeg2000_cleanup(comp, codsty);
1391 av_freep(&s->tile[tileno].comp);
1395 memset(s->codsty, 0, sizeof(s->codsty));
1396 memset(s->qntsty, 0, sizeof(s->qntsty));
1397 s->numXtiles = s->numYtiles = 0;
1400 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1402 Jpeg2000CodingStyle *codsty = s->codsty;
1403 Jpeg2000QuantStyle *qntsty = s->qntsty;
1404 uint8_t *properties = s->properties;
1411 if (bytestream2_get_bytes_left(&s->g) < 2) {
1412 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1416 marker = bytestream2_get_be16u(&s->g);
1417 oldpos = bytestream2_tell(&s->g);
1419 if (marker == JPEG2000_SOD) {
1421 Jpeg2000TilePart *tp;
1424 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1425 return AVERROR_INVALIDDATA;
1427 if (s->curtileno < 0) {
1428 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1429 return AVERROR_INVALIDDATA;
1432 tile = s->tile + s->curtileno;
1433 tp = tile->tile_part + tile->tp_idx;
1434 if (tp->tp_end < s->g.buffer) {
1435 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1436 return AVERROR_INVALIDDATA;
1438 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1439 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1443 if (marker == JPEG2000_EOC)
1446 len = bytestream2_get_be16(&s->g);
1447 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1448 return AVERROR_INVALIDDATA;
1454 s->numXtiles = s->numYtiles = 0;
1457 ret = get_coc(s, codsty, properties);
1460 ret = get_cod(s, codsty, properties);
1463 ret = get_qcc(s, len, qntsty, properties);
1466 ret = get_qcd(s, len, qntsty, properties);
1469 if (!(ret = get_sot(s, len))) {
1470 av_assert1(s->curtileno >= 0);
1471 codsty = s->tile[s->curtileno].codsty;
1472 qntsty = s->tile[s->curtileno].qntsty;
1473 properties = s->tile[s->curtileno].properties;
1477 // the comment is ignored
1478 bytestream2_skip(&s->g, len - 2);
1481 // Tile-part lengths
1482 ret = get_tlm(s, len);
1485 av_log(s->avctx, AV_LOG_ERROR,
1486 "unsupported marker 0x%.4X at pos 0x%X\n",
1487 marker, bytestream2_tell(&s->g) - 4);
1488 bytestream2_skip(&s->g, len - 2);
1491 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1492 av_log(s->avctx, AV_LOG_ERROR,
1493 "error during processing marker segment %.4x\n", marker);
1494 return ret ? ret : -1;
1500 /* Read bit stream packets --> T2 operation. */
1501 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1506 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1507 Jpeg2000Tile *tile = s->tile + tileno;
1509 if (ret = init_tile(s, tileno))
1512 s->g = tile->tile_part[0].tpg;
1513 if (ret = jpeg2000_decode_packets(s, tile))
1520 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1522 uint32_t atom_size, atom, atom_end;
1523 int search_range = 10;
1527 bytestream2_get_bytes_left(&s->g) >= 8) {
1528 atom_size = bytestream2_get_be32u(&s->g);
1529 atom = bytestream2_get_be32u(&s->g);
1530 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
1532 if (atom == JP2_CODESTREAM)
1535 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1538 if (atom == JP2_HEADER &&
1540 uint32_t atom2_size, atom2, atom2_end;
1542 atom2_size = bytestream2_get_be32u(&s->g);
1543 atom2 = bytestream2_get_be32u(&s->g);
1544 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
1545 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1547 if (atom2 == JP2_CODESTREAM) {
1549 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1550 int method = bytestream2_get_byteu(&s->g);
1551 bytestream2_skipu(&s->g, 2);
1553 s->colour_space = bytestream2_get_be32u(&s->g);
1555 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1556 int i, size, colour_count, colour_channels, colour_depth[3];
1558 colour_count = bytestream2_get_be16u(&s->g);
1559 colour_channels = bytestream2_get_byteu(&s->g);
1560 // FIXME: Do not ignore channel_sign
1561 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1562 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1563 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1564 size = (colour_depth[0] + 7 >> 3) * colour_count +
1565 (colour_depth[1] + 7 >> 3) * colour_count +
1566 (colour_depth[2] + 7 >> 3) * colour_count;
1567 if (colour_count > 256 ||
1568 colour_channels != 3 ||
1569 colour_depth[0] > 16 ||
1570 colour_depth[1] > 16 ||
1571 colour_depth[2] > 16 ||
1572 atom2_size < size) {
1573 avpriv_request_sample(s->avctx, "Unknown palette");
1574 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1578 for (i = 0; i < colour_count; i++) {
1579 if (colour_depth[0] <= 8) {
1580 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
1581 r |= r >> colour_depth[0];
1583 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
1585 if (colour_depth[1] <= 8) {
1586 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
1587 r |= r >> colour_depth[1];
1589 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
1591 if (colour_depth[2] <= 8) {
1592 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
1593 r |= r >> colour_depth[2];
1595 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
1597 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
1599 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
1600 int n = bytestream2_get_be16u(&s->g);
1602 int cn = bytestream2_get_be16(&s->g);
1603 int av_unused typ = bytestream2_get_be16(&s->g);
1604 int asoc = bytestream2_get_be16(&s->g);
1605 if (cn < 4 || asoc < 4)
1609 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1610 } while (atom_end - atom2_end >= 8);
1614 bytestream2_seek(&s->g, atom_end, SEEK_SET);
1620 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1621 int *got_frame, AVPacket *avpkt)
1623 Jpeg2000DecoderContext *s = avctx->priv_data;
1624 ThreadFrame frame = { .f = data };
1625 AVFrame *picture = data;
1629 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1631 memset(s->cdef, -1, sizeof(s->cdef));
1633 if (bytestream2_get_bytes_left(&s->g) < 2) {
1634 ret = AVERROR_INVALIDDATA;
1638 // check if the image is in jp2 format
1639 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1640 (bytestream2_get_be32u(&s->g) == 12) &&
1641 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1642 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1643 if (!jp2_find_codestream(s)) {
1644 av_log(avctx, AV_LOG_ERROR,
1645 "Could not find Jpeg2000 codestream atom.\n");
1646 ret = AVERROR_INVALIDDATA;
1650 bytestream2_seek(&s->g, 0, SEEK_SET);
1653 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
1654 bytestream2_skip(&s->g, 1);
1656 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1657 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1658 ret = AVERROR_INVALIDDATA;
1661 if (ret = jpeg2000_read_main_headers(s))
1664 /* get picture buffer */
1665 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1667 picture->pict_type = AV_PICTURE_TYPE_I;
1668 picture->key_frame = 1;
1670 if (ret = jpeg2000_read_bitstream_packets(s))
1673 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1674 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1677 jpeg2000_dec_cleanup(s);
1681 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
1682 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
1684 return bytestream2_tell(&s->g);
1687 jpeg2000_dec_cleanup(s);
1691 static void jpeg2000_init_static_data(AVCodec *codec)
1693 ff_jpeg2000_init_tier1_luts();
1694 ff_mqc_init_context_tables();
1697 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1698 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1700 static const AVOption options[] = {
1701 { "lowres", "Lower the decoding resolution by a power of two",
1702 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1706 static const AVProfile profiles[] = {
1707 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1708 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1709 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1710 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1711 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1712 { FF_PROFILE_UNKNOWN },
1715 static const AVClass jpeg2000_class = {
1716 .class_name = "jpeg2000",
1717 .item_name = av_default_item_name,
1719 .version = LIBAVUTIL_VERSION_INT,
1722 AVCodec ff_jpeg2000_decoder = {
1724 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1725 .type = AVMEDIA_TYPE_VIDEO,
1726 .id = AV_CODEC_ID_JPEG2000,
1727 .capabilities = CODEC_CAP_FRAME_THREADS,
1728 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1729 .init_static_data = jpeg2000_init_static_data,
1730 .decode = jpeg2000_decode_frame,
1731 .priv_class = &jpeg2000_class,
1733 .profiles = NULL_IF_CONFIG_SMALL(profiles)