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/common.h"
29 #include "libavutil/opt.h"
31 #include "bytestream.h"
36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
43 typedef struct Jpeg2000TilePart {
44 uint16_t tp_idx; // Tile-part index
45 uint8_t tile_index; // Tile index who refers the tile-part
46 uint32_t tp_len; // Length of tile-part
47 GetByteContext tpg; // bit stream in tile-part
50 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
51 * one per component, so tile_part elements have a size of 3 */
52 typedef struct Jpeg2000Tile {
53 Jpeg2000Component *comp;
54 uint8_t properties[4];
55 Jpeg2000CodingStyle codsty[4];
56 Jpeg2000QuantStyle qntsty[4];
57 Jpeg2000TilePart tile_part[3];
60 typedef struct Jpeg2000DecoderContext {
62 AVCodecContext *avctx;
66 int image_offset_x, image_offset_y;
67 int tile_offset_x, tile_offset_y;
68 uint8_t cbps[4]; // bits per sample in particular components
69 uint8_t sgnd[4]; // if a component is signed
70 uint8_t properties[4];
74 int tile_width, tile_height;
75 int numXtiles, numYtiles;
78 Jpeg2000CodingStyle codsty[4];
79 Jpeg2000QuantStyle qntsty[4];
86 /*options parameters*/
88 int16_t reduction_factor;
89 } Jpeg2000DecoderContext;
91 /* get_bits functions for JPEG2000 packet bitstream
92 * It is a get_bit function with a bit-stuffing routine. If the value of the
93 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
94 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
95 static int get_bits(Jpeg2000DecoderContext *s, int n)
101 if (s->bit_index == 0) {
102 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
105 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
110 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
112 if (bytestream2_get_byte(&s->g) == 0xff)
113 bytestream2_skip(&s->g, 1);
117 /* decode the value stored in node */
118 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
121 Jpeg2000TgtNode *stack[30];
122 int sp = -1, curval = 0;
124 while (node && !node->vis) {
132 curval = stack[sp]->val;
134 while (curval < threshold && sp >= 0) {
135 if (curval < stack[sp]->val)
136 curval = stack[sp]->val;
137 while (curval < threshold) {
139 if ((ret = get_bits(s, 1)) > 0) {
147 stack[sp]->val = curval;
153 /* marker segments */
154 /* get sizes and offsets of image, tiles; number of components */
155 static int get_siz(Jpeg2000DecoderContext *s)
159 if (bytestream2_get_bytes_left(&s->g) < 36)
160 return AVERROR(EINVAL);
162 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
163 s->width = bytestream2_get_be32u(&s->g); // Width
164 s->height = bytestream2_get_be32u(&s->g); // Height
165 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
166 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
167 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
168 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
169 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
170 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
171 s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
173 if (s->ncomponents <= 0 || s->ncomponents > 4) {
174 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
175 return AVERROR(EINVAL);
177 if (s->tile_width<=0 || s->tile_height<=0)
178 return AVERROR(EINVAL);
180 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
181 return AVERROR(EINVAL);
183 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
184 uint8_t x = bytestream2_get_byteu(&s->g);
185 s->cbps[i] = (x & 0x7f) + 1;
186 s->precision = FFMAX(s->cbps[i], s->precision);
187 s->sgnd[i] = (x & 0x80) == 1;
188 s->cdx[i] = bytestream2_get_byteu(&s->g);
189 s->cdy[i] = bytestream2_get_byteu(&s->g);
192 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
193 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
195 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
196 return AVERROR(EINVAL);
198 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
200 return AVERROR(ENOMEM);
202 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
203 Jpeg2000Tile *tile = s->tile + i;
205 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
207 return AVERROR(ENOMEM);
210 /* compute image size with reduction factor */
211 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
212 s->reduction_factor);
213 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
214 s->reduction_factor);
216 switch (s->avctx->profile) {
217 case FF_PROFILE_JPEG2000_DCINEMA_2K:
218 case FF_PROFILE_JPEG2000_DCINEMA_4K:
219 /* XYZ color-space for digital cinema profiles */
220 s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
223 /* For other profiles selects color-space according number of
224 * components and bit depth precision. */
225 switch (s->ncomponents) {
227 if (s->precision > 8)
228 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
230 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
233 if (s->precision > 8)
234 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
236 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
239 s->avctx->pix_fmt = AV_PIX_FMT_BGRA;
242 /* pixel format can not be identified */
243 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
251 /* get common part for COD and COC segments */
252 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
256 if (bytestream2_get_bytes_left(&s->g) < 5)
257 return AVERROR(EINVAL);
258 c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
259 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
260 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
261 return AVERROR_INVALIDDATA;
264 /* compute number of resolution levels to decode */
265 if (c->nreslevels < s->reduction_factor)
266 c->nreslevels2decode = 1;
268 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
270 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
271 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
273 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
274 c->log2_cblk_width + c->log2_cblk_height > 14) {
275 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
276 return AVERROR_INVALIDDATA;
279 c->cblk_style = bytestream2_get_byteu(&s->g);
280 if (c->cblk_style != 0) { // cblk style
281 av_log(s->avctx, AV_LOG_ERROR, "no extra cblk styles supported\n");
284 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
285 /* set integer 9/7 DWT in case of BITEXACT flag */
286 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
287 c->transform = FF_DWT97_INT;
289 if (c->csty & JPEG2000_CSTY_PREC) {
291 for (i = 0; i < c->nreslevels; i++) {
292 byte = bytestream2_get_byte(&s->g);
293 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
294 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
297 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
298 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
303 /* get coding parameters for a particular tile or whole image*/
304 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
307 Jpeg2000CodingStyle tmp;
310 if (bytestream2_get_bytes_left(&s->g) < 5)
311 return AVERROR(EINVAL);
313 tmp.csty = bytestream2_get_byteu(&s->g);
315 // get progression order
316 tmp.prog_order = bytestream2_get_byteu(&s->g);
318 tmp.nlayers = bytestream2_get_be16u(&s->g);
319 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
322 for (compno = 0; compno < s->ncomponents; compno++)
323 if (!(properties[compno] & HAD_COC))
324 memcpy(c + compno, &tmp, sizeof(tmp));
328 /* Get coding parameters for a component in the whole image or a
329 * particular tile. */
330 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
335 if (bytestream2_get_bytes_left(&s->g) < 2)
336 return AVERROR(EINVAL);
338 compno = bytestream2_get_byteu(&s->g);
341 c->csty = bytestream2_get_byteu(&s->g);
344 properties[compno] |= HAD_COC;
348 /* Get common part for QCD and QCC segments. */
349 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
353 if (bytestream2_get_bytes_left(&s->g) < 1)
354 return AVERROR(EINVAL);
356 x = bytestream2_get_byteu(&s->g); // Sqcd
358 q->nguardbits = x >> 5;
359 q->quantsty = x & 0x1f;
361 if (q->quantsty == JPEG2000_QSTY_NONE) {
363 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
364 return AVERROR(EINVAL);
365 for (i = 0; i < n; i++)
366 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
367 } else if (q->quantsty == JPEG2000_QSTY_SI) {
368 if (bytestream2_get_bytes_left(&s->g) < 2)
369 return AVERROR(EINVAL);
370 x = bytestream2_get_be16u(&s->g);
371 q->expn[0] = x >> 11;
372 q->mant[0] = x & 0x7ff;
373 for (i = 1; i < 32 * 3; i++) {
374 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
375 q->expn[i] = curexpn;
376 q->mant[i] = q->mant[0];
380 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
381 return AVERROR(EINVAL);
382 for (i = 0; i < n; i++) {
383 x = bytestream2_get_be16u(&s->g);
384 q->expn[i] = x >> 11;
385 q->mant[i] = x & 0x7ff;
391 /* Get quantization parameters for a particular tile or a whole image. */
392 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
395 Jpeg2000QuantStyle tmp;
398 if (get_qcx(s, n, &tmp))
400 for (compno = 0; compno < s->ncomponents; compno++)
401 if (!(properties[compno] & HAD_QCC))
402 memcpy(q + compno, &tmp, sizeof(tmp));
406 /* Get quantization parameters for a component in the whole image
407 * on in a particular tile. */
408 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
413 if (bytestream2_get_bytes_left(&s->g) < 1)
414 return AVERROR(EINVAL);
416 compno = bytestream2_get_byteu(&s->g);
417 properties[compno] |= HAD_QCC;
418 return get_qcx(s, n - 1, q + compno);
421 /* Get start of tile segment. */
422 static int get_sot(Jpeg2000DecoderContext *s, int n)
424 Jpeg2000TilePart *tp;
429 if (bytestream2_get_bytes_left(&s->g) < 8)
430 return AVERROR(EINVAL);
432 s->curtileno = Isot = bytestream2_get_be16u(&s->g); // Isot
433 if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
435 return AVERROR(EINVAL);
438 av_log(s->avctx, AV_LOG_ERROR,
439 "Not a DCINEMA JP2K file: more than one tile\n");
442 Psot = bytestream2_get_be32u(&s->g); // Psot
443 TPsot = bytestream2_get_byteu(&s->g); // TPsot
445 /* Read TNSot but not used */
446 bytestream2_get_byteu(&s->g); // TNsot
448 if (TPsot >= FF_ARRAY_ELEMS(s->tile[s->curtileno].tile_part)) {
449 av_log(s->avctx, AV_LOG_ERROR, "TPsot %d too big\n", TPsot);
450 return AVERROR_PATCHWELCOME;
453 tp = s->tile[s->curtileno].tile_part + TPsot;
454 tp->tile_index = Isot;
458 /* Start of bit stream. Pointer to SOD marker
459 * Check SOD marker is present. */
460 if (JPEG2000_SOD == bytestream2_get_be16(&s->g)) {
461 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_len - n - 4);
462 bytestream2_skip(&s->g, tp->tp_len - n - 4);
464 av_log(s->avctx, AV_LOG_ERROR, "SOD marker not found \n");
468 /* End address of bit stream =
469 * start address + (Psot - size of SOT HEADER(n)
470 * - size of SOT MARKER(2) - size of SOD marker(2) */
475 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
476 * Used to know the number of tile parts and lengths.
477 * There may be multiple TLMs in the header.
478 * TODO: The function is not used for tile-parts management, nor anywhere else.
479 * It can be useful to allocate memory for tile parts, before managing the SOT
480 * markers. Parsing the TLM header is needed to increment the input header
482 * This marker is mandatory for DCI. */
483 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
485 uint8_t Stlm, ST, SP, tile_tlm, i;
486 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
487 Stlm = bytestream2_get_byte(&s->g);
489 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
490 ST = (Stlm >> 4) & 0x03;
491 // TODO: Manage case of ST = 0b11 --> raise error
492 SP = (Stlm >> 6) & 0x01;
493 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
494 for (i = 0; i < tile_tlm; i++) {
499 bytestream2_get_byte(&s->g);
502 bytestream2_get_be16(&s->g);
505 bytestream2_get_be32(&s->g);
509 bytestream2_get_be16(&s->g);
511 bytestream2_get_be32(&s->g);
517 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
520 int tilex = tileno % s->numXtiles;
521 int tiley = tileno / s->numXtiles;
522 Jpeg2000Tile *tile = s->tile + tileno;
523 Jpeg2000CodingStyle *codsty;
524 Jpeg2000QuantStyle *qntsty;
527 return AVERROR(ENOMEM);
529 /* copy codsty, qnsty to tile. TODO: Is it the best way?
530 * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle
531 * and Jpeg2000QuantStyle */
532 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(*codsty));
533 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(*qntsty));
535 for (compno = 0; compno < s->ncomponents; compno++) {
536 Jpeg2000Component *comp = tile->comp + compno;
537 int ret; // global bandno
538 codsty = tile->codsty + compno;
539 qntsty = tile->qntsty + compno;
541 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
542 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
543 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
544 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
546 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
547 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
548 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
549 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
551 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
552 s->cbps[compno], s->cdx[compno],
553 s->cdy[compno], s->avctx))
559 /* Read the number of coding passes. */
560 static int getnpasses(Jpeg2000DecoderContext *s)
567 if ((num = get_bits(s, 2)) != 3)
568 return num < 0 ? num : 3 + num;
569 if ((num = get_bits(s, 5)) != 31)
570 return num < 0 ? num : 6 + num;
571 num = get_bits(s, 7);
572 return num < 0 ? num : 37 + num;
575 static int getlblockinc(Jpeg2000DecoderContext *s)
578 while (ret = get_bits(s, 1)) {
586 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
587 Jpeg2000CodingStyle *codsty,
588 Jpeg2000ResLevel *rlevel, int precno,
589 int layno, uint8_t *expn, int numgbits)
591 int bandno, cblkno, ret, nb_code_blocks;
593 if (!(ret = get_bits(s, 1))) {
599 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
600 Jpeg2000Band *band = rlevel->band + bandno;
601 Jpeg2000Prec *prec = band->prec + precno;
603 if (band->coord[0][0] == band->coord[0][1] ||
604 band->coord[1][0] == band->coord[1][1])
606 nb_code_blocks = prec->nb_codeblocks_height *
607 prec->nb_codeblocks_width;
608 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
609 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
610 int incl, newpasses, llen;
613 incl = get_bits(s, 1);
615 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
622 cblk->nonzerobits = expn[bandno] + numgbits - 1 -
623 tag_tree_decode(s, prec->zerobits + cblkno,
625 if ((newpasses = getnpasses(s)) < 0)
627 if ((llen = getlblockinc(s)) < 0)
629 cblk->lblock += llen;
630 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
632 cblk->lengthinc = ret;
633 cblk->npasses += newpasses;
638 if (codsty->csty & JPEG2000_CSTY_EPH) {
639 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
640 bytestream2_skip(&s->g, 2);
642 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
645 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
646 Jpeg2000Band *band = rlevel->band + bandno;
647 Jpeg2000Prec *prec = band->prec + precno;
649 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
650 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
651 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
652 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
653 || sizeof(cblk->data) < cblk->lengthinc
655 return AVERROR(EINVAL);
656 /* Code-block data can be empty. In that case initialize data
658 if (cblk->lengthinc > 0) {
659 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
661 cblk->data[0] = 0xFF;
662 cblk->data[1] = 0xFF;
664 cblk->length += cblk->lengthinc;
671 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
673 int layno, reslevelno, compno, precno, ok_reslevel;
674 uint8_t prog_order = tile->codsty[0].prog_order;
679 switch (prog_order) {
680 case JPEG2000_PGOD_LRCP:
681 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
683 for (reslevelno = 0; ok_reslevel; reslevelno++) {
685 for (compno = 0; compno < s->ncomponents; compno++) {
686 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
687 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
688 if (reslevelno < codsty->nreslevels) {
689 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
692 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
693 if (jpeg2000_decode_packet(s,
696 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
705 case JPEG2000_PGOD_CPRL:
706 for (compno = 0; compno < s->ncomponents; compno++) {
707 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
708 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
710 /* Set bit stream buffer address according to tile-part.
711 * For DCinema one tile-part per component, so can be
712 * indexed by component. */
713 s->g = tile->tile_part[compno].tpg;
715 /* Position loop (y axis)
716 * TODO: Automate computing of step 256.
717 * Fixed here, but to be computed before entering here. */
718 for (y = 0; y < s->height; y += 256) {
719 /* Position loop (y axis)
720 * TODO: automate computing of step 256.
721 * Fixed here, but to be computed before entering here. */
722 for (x = 0; x < s->width; x += 256) {
723 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
725 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
726 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
728 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
729 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
732 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
733 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
736 // check if a precinct exists
737 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
738 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
739 precno = prcx + rlevel->num_precincts_x * prcy;
740 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
741 if (jpeg2000_decode_packet(s, codsty, rlevel,
743 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
757 /* EOC marker reached */
758 bytestream2_skip(&s->g, 2);
763 /* TIER-1 routines */
764 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
765 int bpno, int bandno)
767 int mask = 3 << (bpno - 1), y0, x, y;
769 for (y0 = 0; y0 < height; y0 += 4)
770 for (x = 0; x < width; x++)
771 for (y = y0; y < height && y < y0 + 4; y++)
772 if ((t1->flags[y + 1][x + 1] & JPEG2000_T1_SIG_NB)
773 && !(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
774 if (ff_mqc_decode(&t1->mqc,
776 ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
778 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
782 (ff_mqc_decode(&t1->mqc,
783 t1->mqc.cx_states + ctxno) ^ xorbit)
786 ff_jpeg2000_set_significance(t1, x, y,
789 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
793 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
799 phalf = 1 << (bpno - 1);
802 for (y0 = 0; y0 < height; y0 += 4)
803 for (x = 0; x < width; x++)
804 for (y = y0; y < height && y < y0 + 4; y++)
805 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
806 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
807 int r = ff_mqc_decode(&t1->mqc,
808 t1->mqc.cx_states + ctxno)
810 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
811 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
815 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
816 int width, int height, int bpno, int bandno,
819 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
821 for (y0 = 0; y0 < height; y0 += 4)
822 for (x = 0; x < width; x++) {
823 if (y0 + 3 < height &&
824 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
825 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
826 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
827 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
828 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
830 runlen = ff_mqc_decode(&t1->mqc,
831 t1->mqc.cx_states + MQC_CX_UNI);
832 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
841 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
843 if (!(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)))
844 dec = ff_mqc_decode(&t1->mqc,
846 ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
851 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
853 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
854 t1->mqc.cx_states + ctxno) ^
857 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
860 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
865 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
866 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
867 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
868 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
870 av_log(s->avctx, AV_LOG_ERROR,
871 "Segmentation symbol value incorrect\n");
875 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
876 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
877 int width, int height, int bandpos)
879 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
881 for (y = 0; y < height; y++)
882 memset(t1->data[y], 0, width * sizeof(width));
884 /* If code-block contains no compressed data: nothing to do. */
887 for (y = 0; y < height + 2; y++)
888 memset(t1->flags[y], 0, (width + 2) * sizeof(width));
890 ff_mqc_initdec(&t1->mqc, cblk->data);
891 cblk->data[cblk->length] = 0xff;
892 cblk->data[cblk->length + 1] = 0xff;
897 decode_sigpass(t1, width, height, bpno + 1, bandpos);
900 decode_refpass(t1, width, height, bpno + 1);
903 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
904 codsty->cblk_style & JPEG2000_CBLK_SEGSYM);
917 /* TODO: Verify dequantization for lossless case
918 * comp->data can be float or int
919 * band->stepsize can be float or int
920 * depending on the type of DWT transformation.
921 * see ISO/IEC 15444-1:2002 A.6.1 */
923 /* Float dequantization of a codeblock.*/
924 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
925 Jpeg2000Component *comp,
926 Jpeg2000T1Context *t1, Jpeg2000Band *band)
929 float *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
930 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
931 for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
932 idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
933 datap[idx] = (float)(t1->data[j][i]) * ((float)band->stepsize);
938 /* Integer dequantization of a codeblock.*/
939 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
940 Jpeg2000Component *comp,
941 Jpeg2000T1Context *t1, Jpeg2000Band *band)
945 (int32_t *) &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
946 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
947 for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
948 idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
950 ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16;
955 /* Inverse ICT parameters in float and integer.
956 * int value = (float value) * (1<<16) */
957 static const float f_ict_params[4] = {
963 static const int i_ict_params[4] = {
970 static int mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
974 int32_t *src[3], i0, i1, i2;
975 float *srcf[3], i0f, i1f, i2f;
977 for (i = 0; i < 3; i++)
978 if (tile->codsty[0].transform == FF_DWT97)
979 srcf[i] = tile->comp[i].data;
981 src[i] = (int32_t *)tile->comp[i].data;
983 for (i = 0; i < 2; i++)
984 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
985 switch (tile->codsty[0].transform) {
987 for (i = 0; i < csize; i++) {
988 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
989 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
990 - (f_ict_params[2] * *srcf[2]);
991 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
998 for (i = 0; i < csize; i++) {
999 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1000 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1001 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1002 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1009 for (i = 0; i < csize; i++) {
1010 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1022 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1025 int compno, reslevelno, bandno;
1029 Jpeg2000T1Context t1;
1030 /* Loop on tile components */
1032 for (compno = 0; compno < s->ncomponents; compno++) {
1033 Jpeg2000Component *comp = tile->comp + compno;
1034 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1035 /* Loop on resolution levels */
1036 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1037 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1039 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1040 uint16_t nb_precincts, precno;
1041 Jpeg2000Band *band = rlevel->band + bandno;
1042 int cblkno = 0, bandpos;
1043 bandpos = bandno + (reslevelno > 0);
1045 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1046 /* Loop on precincts */
1047 for (precno = 0; precno < nb_precincts; precno++) {
1048 Jpeg2000Prec *prec = band->prec + precno;
1050 /* Loop on codeblocks */
1051 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1053 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1054 decode_cblk(s, codsty, &t1, cblk,
1055 cblk->coord[0][1] - cblk->coord[0][0],
1056 cblk->coord[1][1] - cblk->coord[1][0],
1059 /* Manage band offsets */
1060 x = cblk->coord[0][0];
1061 y = cblk->coord[1][0];
1063 if (s->avctx->flags & CODEC_FLAG_BITEXACT)
1064 dequantization_int(x, y, cblk, comp, &t1, band);
1066 dequantization_float(x, y, cblk, comp, &t1, band);
1070 } /* end reslevel */
1073 ff_dwt_decode(&comp->dwt, comp->data);
1076 /* inverse MCT transformation */
1077 if (tile->codsty[0].mct)
1078 mct_decode(s, tile);
1080 if (s->avctx->pix_fmt == AV_PIX_FMT_BGRA) // RGBA -> BGRA
1081 FFSWAP(float *, tile->comp[0].data, tile->comp[2].data);
1083 if (s->precision <= 8) {
1084 for (compno = 0; compno < s->ncomponents; compno++) {
1085 Jpeg2000Component *comp = tile->comp + compno;
1086 int32_t *datap = (int32_t *)comp->data;
1087 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1088 line = picture->data[0] + y * picture->linesize[0];
1089 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1092 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1093 dst = line + x * s->ncomponents + compno;
1095 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
1096 *datap += 1 << (s->cbps[compno] - 1);
1099 else if (*datap >= (1 << s->cbps[compno]))
1100 *datap = (1 << s->cbps[compno]) - 1;
1102 dst += s->ncomponents;
1104 line += picture->linesize[0];
1108 for (compno = 0; compno < s->ncomponents; compno++) {
1109 Jpeg2000Component *comp = tile->comp + compno;
1110 float *datap = comp->data;
1111 int32_t *i_datap = (int32_t *) comp->data;
1114 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1115 linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
1116 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1118 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1119 dst = linel + (x * s->ncomponents + compno);
1120 for (; x < s->avctx->width; x += s->cdx[compno]) {
1122 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1123 if (s->avctx->flags & CODEC_FLAG_BITEXACT)
1124 val = *i_datap + (1 << (s->cbps[compno] - 1));
1126 val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1127 val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1128 /* align 12 bit values in little-endian mode */
1132 dst += s->ncomponents;
1134 linel += picture->linesize[0] >> 1;
1141 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1144 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1145 for (compno = 0; compno < s->ncomponents; compno++) {
1146 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1147 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1149 ff_jpeg2000_cleanup(comp, codsty);
1151 av_freep(&s->tile[tileno].comp);
1156 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1158 Jpeg2000CodingStyle *codsty = s->codsty;
1159 Jpeg2000QuantStyle *qntsty = s->qntsty;
1160 uint8_t *properties = s->properties;
1167 if (bytestream2_get_bytes_left(&s->g) < 2) {
1168 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1172 marker = bytestream2_get_be16u(&s->g);
1173 oldpos = bytestream2_tell(&s->g);
1175 if (marker == JPEG2000_EOC)
1178 if (bytestream2_get_bytes_left(&s->g) < 2)
1179 return AVERROR(EINVAL);
1180 len = bytestream2_get_be16u(&s->g);
1185 s->numXtiles = s->numYtiles = 0;
1188 ret = get_coc(s, codsty, properties);
1191 ret = get_cod(s, codsty, properties);
1194 ret = get_qcc(s, len, qntsty, properties);
1197 ret = get_qcd(s, len, qntsty, properties);
1200 ret = get_sot(s, len);
1203 // the comment is ignored
1204 bytestream2_skip(&s->g, len - 2);
1207 // Tile-part lengths
1208 ret = get_tlm(s, len);
1211 av_log(s->avctx, AV_LOG_ERROR,
1212 "unsupported marker 0x%.4X at pos 0x%X\n",
1213 marker, bytestream2_tell(&s->g) - 4);
1214 bytestream2_skip(&s->g, len - 2);
1217 if (((bytestream2_tell(&s->g) - oldpos != len) && (marker != JPEG2000_SOT)) || ret) {
1218 av_log(s->avctx, AV_LOG_ERROR,
1219 "error during processing marker segment %.4x\n", marker);
1220 return ret ? ret : -1;
1226 /* Read bit stream packets --> T2 operation. */
1227 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1230 Jpeg2000Tile *tile = s->tile + s->curtileno;
1232 if (ret = init_tile(s, s->curtileno))
1234 if (ret = jpeg2000_decode_packets(s, tile))
1240 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1242 uint32_t atom_size, atom;
1243 int found_codestream = 0, search_range = 10;
1245 while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1246 atom_size = bytestream2_get_be32u(&s->g);
1247 atom = bytestream2_get_be32u(&s->g);
1248 if (atom == JP2_CODESTREAM) {
1249 found_codestream = 1;
1251 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1253 bytestream2_skipu(&s->g, atom_size - 8);
1258 if (found_codestream)
1263 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1264 int *got_frame, AVPacket *avpkt)
1266 Jpeg2000DecoderContext *s = avctx->priv_data;
1267 ThreadFrame frame = { .f = data };
1268 AVFrame *picture = data;
1272 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1273 s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1275 // reduction factor, i.e number of resolution levels to skip
1276 s->reduction_factor = s->lowres;
1278 if (bytestream2_get_bytes_left(&s->g) < 2)
1279 return AVERROR(EINVAL);
1281 // check if the image is in jp2 format
1282 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1283 (bytestream2_get_be32u(&s->g) == 12) &&
1284 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1285 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1286 if (!jp2_find_codestream(s)) {
1287 av_log(avctx, AV_LOG_ERROR,
1288 "couldn't find jpeg2k codestream atom\n");
1292 bytestream2_seek(&s->g, 0, SEEK_SET);
1293 if (bytestream2_peek_be16(&s->g) != JPEG2000_SOC /*&& AV_RB32(s->buf + 4) == JP2_CODESTREAM*/)
1294 bytestream2_skip(&s->g, 8);
1297 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1298 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1301 if (ret = jpeg2000_read_main_headers(s))
1304 /* get picture buffer */
1305 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
1306 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
1309 picture->pict_type = AV_PICTURE_TYPE_I;
1310 picture->key_frame = 1;
1312 if (ret = jpeg2000_read_bitstream_packets(s))
1314 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1315 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1317 jpeg2000_dec_cleanup(s);
1321 return bytestream2_tell(&s->g);
1323 jpeg2000_dec_cleanup(s);
1327 static void jpeg2000_init_static_data(AVCodec *codec)
1329 ff_jpeg2000_init_tier1_luts();
1332 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1333 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1335 static const AVOption options[] = {
1336 { "lowres", "Lower the decoding resolution by a power of two",
1337 OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1341 static const AVProfile profiles[] = {
1342 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1343 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1344 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1345 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1346 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1347 { FF_PROFILE_UNKNOWN },
1350 static const AVClass class = {
1351 .class_name = "jpeg2000",
1352 .item_name = av_default_item_name,
1354 .version = LIBAVUTIL_VERSION_INT,
1357 AVCodec ff_jpeg2000_decoder = {
1359 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1360 .type = AVMEDIA_TYPE_VIDEO,
1361 .id = AV_CODEC_ID_JPEG2000,
1362 .capabilities = CODEC_CAP_FRAME_THREADS,
1363 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1364 .init_static_data = jpeg2000_init_static_data,
1365 .decode = jpeg2000_decode_frame,
1366 .priv_class = &class,
1367 .pix_fmts = (enum AVPixelFormat[]) { AV_PIX_FMT_XYZ12,
1371 .profiles = NULL_IF_CONFIG_SMALL(profiles)