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
24 * JPEG2000 image decoder
26 * @author Kamil Nowosad
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
33 #include "bytestream.h"
38 #define JP2_SIG_TYPE 0x6A502020
39 #define JP2_SIG_VALUE 0x0D0A870A
40 #define JP2_CODESTREAM 0x6A703263
45 typedef struct Jpeg2000Tile {
46 Jpeg2000Component *comp;
47 uint8_t properties[4];
48 Jpeg2000CodingStyle codsty[4];
49 Jpeg2000QuantStyle qntsty[4];
52 typedef struct Jpeg2000DecoderContext {
54 AVCodecContext *avctx;
59 int image_offset_x, image_offset_y;
60 int tile_offset_x, tile_offset_y;
61 uint8_t cbps[4]; // bits per sample in particular components
62 uint8_t sgnd[4]; // if a component is signed
63 uint8_t properties[4];
67 int tile_width, tile_height;
68 int numXtiles, numYtiles;
71 Jpeg2000CodingStyle codsty[4];
72 Jpeg2000QuantStyle qntsty[4];
80 /*options parameters*/
83 } Jpeg2000DecoderContext;
85 /* get_bits functions for JPEG2000 packet bitstream
86 * It is a get_bit function with a bit-stuffing routine. If the value of the
87 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
88 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
89 static int get_bits(Jpeg2000DecoderContext *s, int n)
95 if (s->bit_index == 0) {
96 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
99 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
104 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
106 if (bytestream2_get_byte(&s->g) == 0xff)
107 bytestream2_skip(&s->g, 1);
111 /* decode the value stored in node */
112 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
115 Jpeg2000TgtNode *stack[30];
116 int sp = -1, curval = 0;
119 return AVERROR(EINVAL);
121 while (node && !node->vis) {
129 curval = stack[sp]->val;
131 while (curval < threshold && sp >= 0) {
132 if (curval < stack[sp]->val)
133 curval = stack[sp]->val;
134 while (curval < threshold) {
136 if ((ret = get_bits(s, 1)) > 0) {
144 stack[sp]->val = curval;
150 /* marker segments */
151 /* get sizes and offsets of image, tiles; number of components */
152 static int get_siz(Jpeg2000DecoderContext *s)
155 ThreadFrame frame = { .f = s->picture };
157 if (bytestream2_get_bytes_left(&s->g) < 36)
158 return AVERROR(EINVAL);
160 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
161 s->width = bytestream2_get_be32u(&s->g); // Width
162 s->height = bytestream2_get_be32u(&s->g); // Height
163 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
164 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
165 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
166 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
167 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
168 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
169 s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
171 if (s->ncomponents <= 0 || s->ncomponents > 4) {
172 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
173 return AVERROR(EINVAL);
175 if (s->tile_width<=0 || s->tile_height<=0)
176 return AVERROR(EINVAL);
178 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
179 return AVERROR(EINVAL);
181 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
182 uint8_t x = bytestream2_get_byteu(&s->g);
183 s->cbps[i] = (x & 0x7f) + 1;
184 s->precision = FFMAX(s->cbps[i], s->precision);
185 s->sgnd[i] = !!(x & 0x80);
186 s->cdx[i] = bytestream2_get_byteu(&s->g);
187 s->cdy[i] = bytestream2_get_byteu(&s->g);
188 if (s->cdx[i] != 1 || s->cdy[i] != 1) {
189 av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values\n");
193 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
194 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
196 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
197 return AVERROR(EINVAL);
199 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
201 return AVERROR(ENOMEM);
203 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
204 Jpeg2000Tile *tile = s->tile + i;
206 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
208 return AVERROR(ENOMEM);
211 /* compute image size with reduction factor */
212 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
213 s->reduction_factor);
214 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
215 s->reduction_factor);
217 switch(s->ncomponents) {
219 if (s->precision > 8)
220 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
222 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
225 switch (s->avctx->profile) {
226 case FF_PROFILE_JPEG2000_DCINEMA_2K:
227 case FF_PROFILE_JPEG2000_DCINEMA_4K:
228 /* XYZ color-space for digital cinema profiles */
229 s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
232 if (s->precision > 8)
233 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
235 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
240 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
243 /* pixel format can not be identified */
244 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
249 if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
252 s->picture->pict_type = AV_PICTURE_TYPE_I;
253 s->picture->key_frame = 1;
258 /* get common part for COD and COC segments */
259 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
263 if (bytestream2_get_bytes_left(&s->g) < 5)
264 return AVERROR(EINVAL);
265 c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
266 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
267 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
268 return AVERROR_INVALIDDATA;
271 /* compute number of resolution levels to decode */
272 if (c->nreslevels < s->reduction_factor)
273 c->nreslevels2decode = 1;
275 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
277 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
278 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
280 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
281 c->log2_cblk_width + c->log2_cblk_height > 14) {
282 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
283 return AVERROR_INVALIDDATA;
286 c->cblk_style = bytestream2_get_byteu(&s->g);
287 if (c->cblk_style != 0) { // cblk style
288 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
290 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
291 /* set integer 9/7 DWT in case of BITEXACT flag */
292 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
293 c->transform = FF_DWT97_INT;
295 if (c->csty & JPEG2000_CSTY_PREC) {
297 for (i = 0; i < c->nreslevels; i++) {
298 byte = bytestream2_get_byte(&s->g);
299 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
300 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
303 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
304 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
309 /* get coding parameters for a particular tile or whole image*/
310 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
313 Jpeg2000CodingStyle tmp;
316 if (bytestream2_get_bytes_left(&s->g) < 5)
317 return AVERROR(EINVAL);
319 tmp.csty = bytestream2_get_byteu(&s->g);
321 // get progression order
322 tmp.prog_order = bytestream2_get_byteu(&s->g);
323 if (tmp.prog_order) {
324 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
327 tmp.nlayers = bytestream2_get_be16u(&s->g);
328 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
331 for (compno = 0; compno < s->ncomponents; compno++)
332 if (!(properties[compno] & HAD_COC))
333 memcpy(c + compno, &tmp, sizeof(tmp));
337 /* Get coding parameters for a component in the whole image or a
338 * particular tile. */
339 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
344 if (bytestream2_get_bytes_left(&s->g) < 2)
345 return AVERROR(EINVAL);
347 compno = bytestream2_get_byteu(&s->g);
350 c->csty = bytestream2_get_byteu(&s->g);
353 properties[compno] |= HAD_COC;
357 /* Get common part for QCD and QCC segments. */
358 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
362 if (bytestream2_get_bytes_left(&s->g) < 1)
363 return AVERROR(EINVAL);
365 x = bytestream2_get_byteu(&s->g); // Sqcd
367 q->nguardbits = x >> 5;
368 q->quantsty = x & 0x1f;
370 if (q->quantsty == JPEG2000_QSTY_NONE) {
372 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
373 return AVERROR(EINVAL);
374 for (i = 0; i < n; i++)
375 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
376 } else if (q->quantsty == JPEG2000_QSTY_SI) {
377 if (bytestream2_get_bytes_left(&s->g) < 2)
378 return AVERROR(EINVAL);
379 x = bytestream2_get_be16u(&s->g);
380 q->expn[0] = x >> 11;
381 q->mant[0] = x & 0x7ff;
382 for (i = 1; i < 32 * 3; i++) {
383 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
384 q->expn[i] = curexpn;
385 q->mant[i] = q->mant[0];
389 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
390 return AVERROR(EINVAL);
391 for (i = 0; i < n; i++) {
392 x = bytestream2_get_be16u(&s->g);
393 q->expn[i] = x >> 11;
394 q->mant[i] = x & 0x7ff;
400 /* Get quantization parameters for a particular tile or a whole image. */
401 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
404 Jpeg2000QuantStyle tmp;
407 if (get_qcx(s, n, &tmp))
409 for (compno = 0; compno < s->ncomponents; compno++)
410 if (!(properties[compno] & HAD_QCC))
411 memcpy(q + compno, &tmp, sizeof(tmp));
415 /* Get quantization parameters for a component in the whole image
416 * on in a particular tile. */
417 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
422 if (bytestream2_get_bytes_left(&s->g) < 1)
423 return AVERROR(EINVAL);
425 compno = bytestream2_get_byteu(&s->g);
426 properties[compno] |= HAD_QCC;
427 return get_qcx(s, n - 1, q + compno);
430 /* get start of tile segment */
431 static int get_sot(Jpeg2000DecoderContext *s)
433 if (bytestream2_get_bytes_left(&s->g) < 8)
434 return AVERROR(EINVAL);
436 s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
437 if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
439 return AVERROR(EINVAL);
442 bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
444 if (!bytestream2_get_byteu(&s->g)) { ///< TPsot
445 Jpeg2000Tile *tile = s->tile + s->curtileno;
448 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
449 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
451 bytestream2_get_byteu(&s->g); ///< TNsot
456 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
457 * Used to know the number of tile parts and lengths.
458 * There may be multiple TLMs in the header.
459 * TODO: The function is not used for tile-parts management, nor anywhere else.
460 * It can be useful to allocate memory for tile parts, before managing the SOT
461 * markers. Parsing the TLM header is needed to increment the input header
463 * This marker is mandatory for DCI. */
464 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
466 uint8_t Stlm, ST, SP, tile_tlm, i;
467 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
468 Stlm = bytestream2_get_byte(&s->g);
470 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
471 ST = (Stlm >> 4) & 0x03;
472 // TODO: Manage case of ST = 0b11 --> raise error
473 SP = (Stlm >> 6) & 0x01;
474 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
475 for (i = 0; i < tile_tlm; i++) {
480 bytestream2_get_byte(&s->g);
483 bytestream2_get_be16(&s->g);
486 bytestream2_get_be32(&s->g);
490 bytestream2_get_be16(&s->g);
492 bytestream2_get_be32(&s->g);
498 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
501 int tilex = tileno % s->numXtiles;
502 int tiley = tileno / s->numXtiles;
503 Jpeg2000Tile *tile = s->tile + tileno;
506 return AVERROR(ENOMEM);
507 for (compno = 0; compno < s->ncomponents; compno++) {
508 Jpeg2000Component *comp = tile->comp + compno;
509 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
510 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
511 int ret; // global bandno
513 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
514 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
515 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
516 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
518 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
519 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
520 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
521 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
523 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno], s->avctx))
529 /* read the number of coding passes */
530 static int getnpasses(Jpeg2000DecoderContext *s)
537 if ((num = get_bits(s, 2)) != 3)
538 return num < 0 ? num : 3 + num;
539 if ((num = get_bits(s, 5)) != 31)
540 return num < 0 ? num : 6 + num;
541 num = get_bits(s, 7);
542 return num < 0 ? num : 37 + num;
545 static int getlblockinc(Jpeg2000DecoderContext *s)
548 while (ret = get_bits(s, 1)) {
556 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
557 Jpeg2000CodingStyle *codsty,
558 Jpeg2000ResLevel *rlevel, int precno,
559 int layno, uint8_t *expn, int numgbits)
561 int bandno, cblkno, ret, nb_code_blocks;
563 if (!(ret = get_bits(s, 1))) {
569 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
570 Jpeg2000Band *band = rlevel->band + bandno;
571 Jpeg2000Prec *prec = band->prec + precno;
573 if (band->coord[0][0] == band->coord[0][1] ||
574 band->coord[1][0] == band->coord[1][1])
577 nb_code_blocks = prec->nb_codeblocks_height *
578 prec->nb_codeblocks_width;
579 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
580 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
581 int incl, newpasses, llen;
584 incl = get_bits(s, 1);
586 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
593 cblk->nonzerobits = expn[bandno] + numgbits - 1 -
594 tag_tree_decode(s, prec->zerobits + cblkno,
596 if ((newpasses = getnpasses(s)) < 0)
598 if ((llen = getlblockinc(s)) < 0)
600 cblk->lblock += llen;
601 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
603 cblk->lengthinc = ret;
604 cblk->npasses += newpasses;
609 if (codsty->csty & JPEG2000_CSTY_EPH) {
610 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
611 bytestream2_skip(&s->g, 2);
613 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
616 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
617 Jpeg2000Band *band = rlevel->band + bandno;
618 Jpeg2000Prec *prec = band->prec + precno;
620 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
621 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
622 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
623 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
624 || sizeof(cblk->data) < cblk->lengthinc
626 return AVERROR(EINVAL);
627 /* Code-block data can be empty. In that case initialize data
629 if (cblk->lengthinc > 0) {
630 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
632 cblk->data[0] = 0xFF;
633 cblk->data[1] = 0xFF;
635 cblk->length += cblk->lengthinc;
642 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
644 int layno, reslevelno, compno, precno, ok_reslevel;
646 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
648 for (reslevelno = 0; ok_reslevel; reslevelno++) {
650 for (compno = 0; compno < s->ncomponents; compno++) {
651 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
652 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
653 if (reslevelno < codsty->nreslevels) {
654 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
657 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
658 if (jpeg2000_decode_packet(s,
661 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
671 /* TIER-1 routines */
672 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
673 int bpno, int bandno, int bpass_csty_symbol,
674 int vert_causal_ctx_csty_symbol)
676 int mask = 3 << (bpno - 1), y0, x, y;
678 for (y0 = 0; y0 < height; y0 += 4)
679 for (x = 0; x < width; x++)
680 for (y = y0; y < height && y < y0 + 4; y++) {
681 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
682 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
684 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
685 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
686 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
687 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
688 if (bpass_csty_symbol)
689 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
691 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
694 ff_jpeg2000_set_significance(t1, x, y,
697 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
702 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
708 phalf = 1 << (bpno - 1);
711 for (y0 = 0; y0 < height; y0 += 4)
712 for (x = 0; x < width; x++)
713 for (y = y0; y < height && y < y0 + 4; y++)
714 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
715 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
716 int r = ff_mqc_decode(&t1->mqc,
717 t1->mqc.cx_states + ctxno)
719 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
720 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
724 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
725 int width, int height, int bpno, int bandno,
726 int seg_symbols, int vert_causal_ctx_csty_symbol)
728 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
730 for (y0 = 0; y0 < height; y0 += 4) {
731 for (x = 0; x < width; x++) {
732 if (y0 + 3 < height &&
733 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
734 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
735 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
736 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
737 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
739 runlen = ff_mqc_decode(&t1->mqc,
740 t1->mqc.cx_states + MQC_CX_UNI);
741 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
750 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
752 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
754 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
755 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
756 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
762 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
764 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
765 t1->mqc.cx_states + ctxno) ^
768 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
771 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
777 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
778 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
779 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
780 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
782 av_log(s->avctx, AV_LOG_ERROR,
783 "Segmentation symbol value incorrect\n");
787 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
788 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
789 int width, int height, int bandpos)
791 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
792 int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
793 int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
795 for (y = 0; y < height; y++)
796 memset(t1->data[y], 0, width * sizeof(**t1->data));
798 /* If code-block contains no compressed data: nothing to do. */
802 for (y = 0; y < height+2; y++)
803 memset(t1->flags[y], 0, (width + 2)*sizeof(**t1->flags));
805 cblk->data[cblk->length] = 0xff;
806 cblk->data[cblk->length+1] = 0xff;
807 ff_mqc_initdec(&t1->mqc, cblk->data);
812 decode_sigpass(t1, width, height, bpno + 1, bandpos,
813 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
816 decode_refpass(t1, width, height, bpno + 1);
817 if (bpass_csty_symbol && clnpass_cnt >= 4)
818 ff_mqc_initdec(&t1->mqc, cblk->data);
821 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
822 codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
823 clnpass_cnt = clnpass_cnt + 1;
824 if (bpass_csty_symbol && clnpass_cnt >= 4)
825 ff_mqc_initdec(&t1->mqc, cblk->data);
838 /* Float dequantization of a codeblock.*/
839 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
840 Jpeg2000Component *comp,
841 Jpeg2000T1Context *t1, Jpeg2000Band *band)
844 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
845 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
846 for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
847 idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
848 datap[idx] = (float)(t1->data[j][i]) * band->f_stepsize;
852 /* Integer dequantization of a codeblock.*/
853 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
854 Jpeg2000Component *comp,
855 Jpeg2000T1Context *t1, Jpeg2000Band *band)
858 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
859 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
860 for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
861 idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
863 ((int32_t)(t1->data[j][i]) * band->i_stepsize + (1 << 15)) >> 16;
867 /* Inverse ICT parameters in float and integer.
868 * int value = (float value) * (1<<16) */
869 static const float f_ict_params[4] = {
875 static const int i_ict_params[4] = {
882 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
885 int32_t *src[3], i0, i1, i2;
886 float *srcf[3], i0f, i1f, i2f;
888 for (i = 0; i < 3; i++)
889 if (tile->codsty[0].transform == FF_DWT97)
890 srcf[i] = tile->comp[i].f_data;
892 src [i] = tile->comp[i].i_data;
894 for (i = 0; i < 2; i++)
895 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
897 switch (tile->codsty[0].transform) {
899 for (i = 0; i < csize; i++) {
900 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
901 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
902 - (f_ict_params[2] * *srcf[2]);
903 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
910 for (i = 0; i < csize; i++) {
911 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
912 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
913 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
914 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
921 for (i = 0; i < csize; i++) {
922 i1 = *src[0] - (*src[2] + *src[1] >> 2);
933 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
936 int compno, reslevelno, bandno;
940 Jpeg2000T1Context t1;
942 /* Loop on tile components */
943 for (compno = 0; compno < s->ncomponents; compno++) {
944 Jpeg2000Component *comp = tile->comp + compno;
945 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
947 /* Loop on resolution levels */
948 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
949 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
951 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
952 int nb_precincts, precno;
953 Jpeg2000Band *band = rlevel->band + bandno;
954 int cblkno=0, bandpos;
956 bandpos = bandno + (reslevelno > 0);
958 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
961 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
962 /* Loop on precincts */
963 for (precno = 0; precno < nb_precincts; precno++) {
964 Jpeg2000Prec *prec = band->prec + precno;
966 /* Loop on codeblocks */
967 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
969 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
970 decode_cblk(s, codsty, &t1, cblk,
971 cblk->coord[0][1] - cblk->coord[0][0],
972 cblk->coord[1][1] - cblk->coord[1][0],
975 /* Manage band offsets */
976 x = cblk->coord[0][0];
977 y = cblk->coord[1][0];
979 if (codsty->transform == FF_DWT97)
980 dequantization_float(x, y, cblk, comp, &t1, band);
982 dequantization_int(x, y, cblk, comp, &t1, band);
988 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
991 /* inverse MCT transformation */
992 if (tile->codsty[0].mct)
995 if (s->precision <= 8) {
996 for (compno = 0; compno < s->ncomponents; compno++) {
997 Jpeg2000Component *comp = tile->comp + compno;
998 float *datap = comp->f_data;
999 int32_t *i_datap = comp->i_data;
1001 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1002 line = picture->data[0] + y * picture->linesize[0];
1003 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1006 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1007 dst = line + x * s->ncomponents + compno;
1009 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
1011 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1012 if (tile->codsty->transform == FF_DWT97)
1013 val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1015 val = *i_datap + (1 << (s->cbps[compno] - 1));
1016 val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1017 *dst = val << (8 - s->cbps[compno]);
1020 dst += s->ncomponents;
1022 line += picture->linesize[0];
1026 for (compno = 0; compno < s->ncomponents; compno++) {
1027 Jpeg2000Component *comp = tile->comp + compno;
1028 float *datap = comp->f_data;
1029 int32_t *i_datap = comp->i_data;
1032 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1033 linel = (uint16_t*)picture->data[0] + y * (picture->linesize[0] >> 1);
1034 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1037 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1038 dst = linel + (x * s->ncomponents + compno);
1039 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
1041 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1042 if (tile->codsty->transform == FF_DWT97)
1043 val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1045 val = *i_datap + (1 << (s->cbps[compno] - 1));
1046 val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1047 /* align 12 bit values in little-endian mode */
1048 *dst = val << (16 - s->cbps[compno]);
1051 dst += s->ncomponents;
1053 linel += picture->linesize[0]>>1;
1060 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1063 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1064 for (compno = 0; compno < s->ncomponents; compno++) {
1065 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1066 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1068 ff_jpeg2000_cleanup(comp, codsty);
1070 av_freep(&s->tile[tileno].comp);
1075 static int jpeg2000_decode_codestream(Jpeg2000DecoderContext *s)
1077 Jpeg2000CodingStyle *codsty = s->codsty;
1078 Jpeg2000QuantStyle *qntsty = s->qntsty;
1079 uint8_t *properties = s->properties;
1086 if (bytestream2_get_bytes_left(&s->g) < 2) {
1087 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1091 marker = bytestream2_get_be16u(&s->g);
1092 oldpos = bytestream2_tell(&s->g);
1094 if (marker == JPEG2000_SOD) {
1095 Jpeg2000Tile *tile = s->tile + s->curtileno;
1096 if (ret = init_tile(s, s->curtileno)) {
1097 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
1100 if (ret = jpeg2000_decode_packets(s, tile)) {
1101 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
1106 if (marker == JPEG2000_EOC)
1109 if (bytestream2_get_bytes_left(&s->g) < 2)
1110 return AVERROR(EINVAL);
1111 len = bytestream2_get_be16u(&s->g);
1116 s->numXtiles = s->numYtiles = 0;
1119 ret = get_coc(s, codsty, properties);
1122 ret = get_cod(s, codsty, properties);
1125 ret = get_qcc(s, len, qntsty, properties);
1128 ret = get_qcd(s, len, qntsty, properties);
1131 if (!(ret = get_sot(s))) {
1132 codsty = s->tile[s->curtileno].codsty;
1133 qntsty = s->tile[s->curtileno].qntsty;
1134 properties = s->tile[s->curtileno].properties;
1138 // the comment is ignored
1139 bytestream2_skip(&s->g, len - 2);
1142 // Tile-part lengths
1143 ret = get_tlm(s, len);
1146 av_log(s->avctx, AV_LOG_ERROR,
1147 "unsupported marker 0x%.4X at pos 0x%X\n",
1148 marker, bytestream2_tell(&s->g) - 4);
1149 bytestream2_skip(&s->g, len - 2);
1152 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1153 av_log(s->avctx, AV_LOG_ERROR,
1154 "error during processing marker segment %.4x\n", marker);
1155 return ret ? ret : -1;
1161 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1163 uint32_t atom_size, atom;
1164 int found_codestream = 0, search_range = 10;
1166 while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1167 atom_size = bytestream2_get_be32u(&s->g);
1168 atom = bytestream2_get_be32u(&s->g);
1169 if (atom == JP2_CODESTREAM) {
1170 found_codestream = 1;
1172 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1174 bytestream2_skipu(&s->g, atom_size - 8);
1179 if (found_codestream)
1184 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1185 int *got_frame, AVPacket *avpkt)
1187 Jpeg2000DecoderContext *s = avctx->priv_data;
1188 AVFrame *picture = data;
1191 s->picture = picture;
1194 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1197 // reduction factor, i.e number of resolution levels to skip
1198 s->reduction_factor = avctx->lowres;
1200 if (bytestream2_get_bytes_left(&s->g) < 2) {
1201 ret = AVERROR(EINVAL);
1205 // check if the image is in jp2 format
1206 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1207 (bytestream2_get_be32u(&s->g) == 12) &&
1208 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1209 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1210 if (!jp2_find_codestream(s)) {
1211 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1216 bytestream2_seek(&s->g, 0, SEEK_SET);
1219 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1220 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1224 if (ret = jpeg2000_decode_codestream(s))
1227 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1228 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, s->picture))
1231 jpeg2000_dec_cleanup(s);
1235 return bytestream2_tell(&s->g);
1238 jpeg2000_dec_cleanup(s);
1242 static void jpeg2000_init_static_data(AVCodec *codec)
1244 ff_jpeg2000_init_tier1_luts();
1247 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1248 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1250 static const AVOption options[] = {
1251 { "lowres", "Lower the decoding resolution by a power of two",
1252 OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1256 static const AVProfile profiles[] = {
1257 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1258 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1259 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1260 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1261 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1262 { FF_PROFILE_UNKNOWN },
1265 static const AVClass class = {
1266 .class_name = "j2k",
1267 .item_name = av_default_item_name,
1269 .version = LIBAVUTIL_VERSION_INT,
1272 AVCodec ff_j2k_decoder = {
1274 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1275 .type = AVMEDIA_TYPE_VIDEO,
1276 .id = AV_CODEC_ID_JPEG2000,
1277 .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
1278 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1279 .init_static_data = jpeg2000_init_static_data,
1280 .decode = jpeg2000_decode_frame,
1281 .priv_class = &class,
1283 .profiles = NULL_IF_CONFIG_SMALL(profiles)