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/common.h"
30 #include "libavutil/opt.h"
32 #include "bytestream.h"
37 #define JP2_SIG_TYPE 0x6A502020
38 #define JP2_SIG_VALUE 0x0D0A870A
39 #define JP2_CODESTREAM 0x6A703263
44 typedef struct Jpeg2000Tile {
45 Jpeg2000Component *comp;
46 uint8_t properties[4];
47 Jpeg2000CodingStyle codsty[4];
48 Jpeg2000QuantStyle qntsty[4];
51 typedef struct Jpeg2000DecoderContext {
53 AVCodecContext *avctx;
58 int image_offset_x, image_offset_y;
59 int tile_offset_x, tile_offset_y;
60 uint8_t cbps[4]; // bits per sample in particular components
61 uint8_t sgnd[4]; // if a component is signed
62 uint8_t properties[4];
66 int tile_width, tile_height;
67 int numXtiles, numYtiles;
70 Jpeg2000CodingStyle codsty[4];
71 Jpeg2000QuantStyle qntsty[4];
79 /*options parameters*/
82 } Jpeg2000DecoderContext;
84 /* get_bits functions for JPEG2000 packet bitstream
85 * It is a get_bit function with a bit-stuffing routine. If the value of the
86 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
87 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
88 static int get_bits(Jpeg2000DecoderContext *s, int n)
94 if (s->bit_index == 0) {
95 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
98 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
103 static void j2k_flush(Jpeg2000DecoderContext *s)
105 if (bytestream2_get_byte(&s->g) == 0xff)
106 bytestream2_skip(&s->g, 1);
110 /* decode the value stored in node */
111 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
114 Jpeg2000TgtNode *stack[30];
115 int sp = -1, curval = 0;
118 return AVERROR(EINVAL);
120 while (node && !node->vis) {
128 curval = stack[sp]->val;
130 while (curval < threshold && sp >= 0) {
131 if (curval < stack[sp]->val)
132 curval = stack[sp]->val;
133 while (curval < threshold) {
135 if ((ret = get_bits(s, 1)) > 0) {
143 stack[sp]->val = curval;
149 /* marker segments */
150 /* get sizes and offsets of image, tiles; number of components */
151 static int get_siz(Jpeg2000DecoderContext *s)
154 ThreadFrame frame = { .f = s->picture };
156 if (bytestream2_get_bytes_left(&s->g) < 36)
157 return AVERROR(EINVAL);
159 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
160 s->width = bytestream2_get_be32u(&s->g); // Width
161 s->height = bytestream2_get_be32u(&s->g); // Height
162 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
163 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
164 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
165 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
166 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
167 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
168 s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
170 if (s->ncomponents <= 0 || s->ncomponents > 4) {
171 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
172 return AVERROR(EINVAL);
174 if (s->tile_width<=0 || s->tile_height<=0)
175 return AVERROR(EINVAL);
177 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
178 return AVERROR(EINVAL);
180 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
181 uint8_t x = bytestream2_get_byteu(&s->g);
182 s->cbps[i] = (x & 0x7f) + 1;
183 s->precision = FFMAX(s->cbps[i], s->precision);
184 s->sgnd[i] = !!(x & 0x80);
185 s->cdx[i] = bytestream2_get_byteu(&s->g);
186 s->cdy[i] = bytestream2_get_byteu(&s->g);
187 if (s->cdx[i] != 1 || s->cdy[i] != 1) {
188 av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values\n");
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);
215 switch(s->ncomponents) {
217 if (s->precision > 8) {
218 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
220 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
224 if (s->precision > 8) {
225 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
227 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
231 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
236 if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
239 s->picture->pict_type = AV_PICTURE_TYPE_I;
240 s->picture->key_frame = 1;
245 /* get common part for COD and COC segments */
246 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
250 if (bytestream2_get_bytes_left(&s->g) < 5)
251 return AVERROR(EINVAL);
252 c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
253 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
254 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
255 return AVERROR_INVALIDDATA;
258 /* compute number of resolution levels to decode */
259 if (c->nreslevels < s->reduction_factor)
260 c->nreslevels2decode = 1;
262 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
264 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
265 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
267 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
268 c->log2_cblk_width + c->log2_cblk_height > 14) {
269 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
270 return AVERROR_INVALIDDATA;
273 c->cblk_style = bytestream2_get_byteu(&s->g);
274 if (c->cblk_style != 0) { // cblk style
275 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
277 c->transform = bytestream2_get_byteu(&s->g); // transformation
278 if (c->csty & JPEG2000_CSTY_PREC) {
280 for (i = 0; i < c->nreslevels; i++) {
281 byte = bytestream2_get_byte(&s->g);
282 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
283 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
286 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
287 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
292 /* get coding parameters for a particular tile or whole image*/
293 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
296 Jpeg2000CodingStyle tmp;
299 if (bytestream2_get_bytes_left(&s->g) < 5)
300 return AVERROR(EINVAL);
302 tmp.csty = bytestream2_get_byteu(&s->g);
304 // get progression order
305 tmp.prog_order = bytestream2_get_byteu(&s->g);
306 if (tmp.prog_order) {
307 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
310 tmp.nlayers = bytestream2_get_be16u(&s->g);
311 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
314 for (compno = 0; compno < s->ncomponents; compno++)
315 if (!(properties[compno] & HAD_COC))
316 memcpy(c + compno, &tmp, sizeof(tmp));
320 /* Get coding parameters for a component in the whole image or a
321 * particular tile. */
322 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
327 if (bytestream2_get_bytes_left(&s->g) < 2)
328 return AVERROR(EINVAL);
330 compno = bytestream2_get_byteu(&s->g);
333 c->csty = bytestream2_get_byteu(&s->g);
336 properties[compno] |= HAD_COC;
340 /* Get common part for QCD and QCC segments. */
341 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
345 if (bytestream2_get_bytes_left(&s->g) < 1)
346 return AVERROR(EINVAL);
348 x = bytestream2_get_byteu(&s->g); // Sqcd
350 q->nguardbits = x >> 5;
351 q->quantsty = x & 0x1f;
353 if (q->quantsty == JPEG2000_QSTY_NONE) {
355 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
356 return AVERROR(EINVAL);
357 for (i = 0; i < n; i++)
358 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
359 } else if (q->quantsty == JPEG2000_QSTY_SI) {
360 if (bytestream2_get_bytes_left(&s->g) < 2)
361 return AVERROR(EINVAL);
362 x = bytestream2_get_be16u(&s->g);
363 q->expn[0] = x >> 11;
364 q->mant[0] = x & 0x7ff;
365 for (i = 1; i < 32 * 3; i++) {
366 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
367 q->expn[i] = curexpn;
368 q->mant[i] = q->mant[0];
372 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
373 return AVERROR(EINVAL);
374 for (i = 0; i < n; i++) {
375 x = bytestream2_get_be16u(&s->g);
376 q->expn[i] = x >> 11;
377 q->mant[i] = x & 0x7ff;
383 /* Get quantization parameters for a particular tile or a whole image. */
384 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
387 Jpeg2000QuantStyle tmp;
390 if (get_qcx(s, n, &tmp))
392 for (compno = 0; compno < s->ncomponents; compno++)
393 if (!(properties[compno] & HAD_QCC))
394 memcpy(q + compno, &tmp, sizeof(tmp));
398 /* Get quantization parameters for a component in the whole image
399 * on in a particular tile. */
400 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
405 if (bytestream2_get_bytes_left(&s->g) < 1)
406 return AVERROR(EINVAL);
408 compno = bytestream2_get_byteu(&s->g);
409 properties[compno] |= HAD_QCC;
410 return get_qcx(s, n - 1, q + compno);
413 /** get start of tile segment */
414 static int get_sot(Jpeg2000DecoderContext *s)
416 if (bytestream2_get_bytes_left(&s->g) < 8)
417 return AVERROR(EINVAL);
419 s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
420 if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
422 return AVERROR(EINVAL);
425 bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
427 if (!bytestream2_get_byteu(&s->g)) { ///< TPsot
428 Jpeg2000Tile *tile = s->tile + s->curtileno;
431 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
432 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
434 bytestream2_get_byteu(&s->g); ///< TNsot
439 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
440 * Used to know the number of tile parts and lengths.
441 * There may be multiple TLMs in the header.
442 * TODO: The function is not used for tile-parts management, nor anywhere else.
443 * It can be useful to allocate memory for tile parts, before managing the SOT
444 * markers. Parsing the TLM header is needed to increment the input header
446 * This marker is mandatory for DCI. */
447 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
449 uint8_t Stlm, ST, SP, tile_tlm, i;
450 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
451 Stlm = bytestream2_get_byte(&s->g);
453 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
454 ST = (Stlm >> 4) & 0x03;
455 // TODO: Manage case of ST = 0b11 --> raise error
456 SP = (Stlm >> 6) & 0x01;
457 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
458 for (i = 0; i < tile_tlm; i++) {
463 bytestream2_get_byte(&s->g);
466 bytestream2_get_be16(&s->g);
469 bytestream2_get_be32(&s->g);
473 bytestream2_get_be16(&s->g);
475 bytestream2_get_be32(&s->g);
481 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
484 int tilex = tileno % s->numXtiles;
485 int tiley = tileno / s->numXtiles;
486 Jpeg2000Tile *tile = s->tile + tileno;
489 return AVERROR(ENOMEM);
490 for (compno = 0; compno < s->ncomponents; compno++) {
491 Jpeg2000Component *comp = tile->comp + compno;
492 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
493 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
494 int ret; // global bandno
496 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
497 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
498 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
499 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
501 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
502 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
503 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
504 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
506 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno], s->avctx))
512 /** read the number of coding passes */
513 static int getnpasses(Jpeg2000DecoderContext *s)
520 if ((num = get_bits(s, 2)) != 3)
521 return num < 0 ? num : 3 + num;
522 if ((num = get_bits(s, 5)) != 31)
523 return num < 0 ? num : 6 + num;
524 num = get_bits(s, 7);
525 return num < 0 ? num : 37 + num;
528 static int getlblockinc(Jpeg2000DecoderContext *s)
531 while (ret = get_bits(s, 1)) {
539 static int decode_packet(Jpeg2000DecoderContext *s,
540 Jpeg2000CodingStyle *codsty,
541 Jpeg2000ResLevel *rlevel, int precno,
542 int layno, uint8_t *expn, int numgbits)
544 int bandno, cblkno, ret, nb_code_blocks;
546 if (!(ret = get_bits(s, 1))) {
552 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
553 Jpeg2000Band *band = rlevel->band + bandno;
554 Jpeg2000Prec *prec = band->prec + precno;
556 if (band->coord[0][0] == band->coord[0][1] ||
557 band->coord[1][0] == band->coord[1][1])
560 nb_code_blocks = prec->nb_codeblocks_height *
561 prec->nb_codeblocks_width;
562 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
563 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
564 int incl, newpasses, llen;
567 incl = get_bits(s, 1);
569 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
576 cblk->nonzerobits = expn[bandno] + numgbits - 1 -
577 tag_tree_decode(s, prec->zerobits + cblkno,
579 if ((newpasses = getnpasses(s)) < 0)
581 if ((llen = getlblockinc(s)) < 0)
583 cblk->lblock += llen;
584 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
586 cblk->lengthinc = ret;
587 cblk->npasses += newpasses;
592 if (codsty->csty & JPEG2000_CSTY_EPH) {
593 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
594 bytestream2_skip(&s->g, 2);
596 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
599 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
600 Jpeg2000Band *band = rlevel->band + bandno;
601 Jpeg2000Prec *prec = band->prec + precno;
603 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
604 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
605 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
606 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
607 || sizeof(cblk->data) < cblk->lengthinc
609 return AVERROR(EINVAL);
610 /* Code-block data can be empty. In that case initialize data
612 if (cblk->lengthinc > 0) {
613 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
615 cblk->data[0] = 0xFF;
616 cblk->data[1] = 0xFF;
618 cblk->length += cblk->lengthinc;
625 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
627 int layno, reslevelno, compno, precno, ok_reslevel;
629 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
631 for (reslevelno = 0; ok_reslevel; reslevelno++) {
633 for (compno = 0; compno < s->ncomponents; compno++) {
634 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
635 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
636 if (reslevelno < codsty->nreslevels) {
637 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
640 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
644 qntsty->expn + (reslevelno ? 3*(reslevelno-1)+1 : 0),
655 /* TIER-1 routines */
656 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
657 int bpno, int bandno, int bpass_csty_symbol,
658 int vert_causal_ctx_csty_symbol)
660 int mask = 3 << (bpno - 1), y0, x, y;
662 for (y0 = 0; y0 < height; y0 += 4)
663 for (x = 0; x < width; x++)
664 for (y = y0; y < height && y < y0 + 4; y++) {
665 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
666 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
668 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
669 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
670 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
671 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
672 if (bpass_csty_symbol)
673 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
675 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
678 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
680 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
685 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
691 phalf = 1 << (bpno - 1);
694 for (y0 = 0; y0 < height; y0 += 4)
695 for (x = 0; x < width; x++)
696 for (y = y0; y < height && y < y0 + 4; y++)
697 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
698 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
699 int r = ff_mqc_decode(&t1->mqc,
700 t1->mqc.cx_states + ctxno)
702 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
703 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
707 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
708 int width, int height, int bpno, int bandno,
709 int seg_symbols, int vert_causal_ctx_csty_symbol)
711 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
713 for (y0 = 0; y0 < height; y0 += 4) {
714 for (x = 0; x < width; x++) {
715 if (y0 + 3 < height &&
716 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
717 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
718 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
719 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
720 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
722 runlen = ff_mqc_decode(&t1->mqc,
723 t1->mqc.cx_states + MQC_CX_UNI);
724 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
733 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
735 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
737 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
738 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
739 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
745 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
747 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
748 t1->mqc.cx_states + ctxno) ^
751 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
754 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
760 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
761 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
762 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
763 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
765 av_log(s->avctx, AV_LOG_ERROR,
766 "Segmentation symbol value incorrect\n");
770 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
771 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
772 int width, int height, int bandpos)
774 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
775 int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
776 int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
778 for (y = 0; y < height+2; y++)
779 memset(t1->flags[y], 0, (width+2)*sizeof(int));
781 for (y = 0; y < height; y++)
782 memset(t1->data[y], 0, width*sizeof(int));
784 cblk->data[cblk->length] = 0xff;
785 cblk->data[cblk->length+1] = 0xff;
786 ff_mqc_initdec(&t1->mqc, cblk->data);
791 decode_sigpass(t1, width, height, bpno+1, bandpos,
792 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
795 decode_refpass(t1, width, height, bpno+1);
796 if (bpass_csty_symbol && clnpass_cnt >= 4)
797 ff_mqc_initdec(&t1->mqc, cblk->data);
800 decode_clnpass(s, t1, width, height, bpno+1, bandpos,
801 codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
802 clnpass_cnt = clnpass_cnt + 1;
803 if (bpass_csty_symbol && clnpass_cnt >= 4)
804 ff_mqc_initdec(&t1->mqc, cblk->data);
817 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
819 int i, *src[3], i0, i1, i2, csize = 1;
821 for (i = 0; i < 3; i++)
822 src[i] = tile->comp[i].data;
824 for (i = 0; i < 2; i++)
825 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
827 if (tile->codsty[0].transform == FF_DWT97) {
828 for (i = 0; i < csize; i++) {
829 i0 = *src[0] + (*src[2] * 46802 >> 16);
830 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
831 i2 = *src[0] + (116130 * *src[1] >> 16);
837 for (i = 0; i < csize; i++) {
838 i1 = *src[0] - (*src[2] + *src[1] >> 2);
848 static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
850 int compno, reslevelno, bandno;
853 Jpeg2000T1Context t1;
855 /* Loop on tile components */
856 for (compno = 0; compno < s->ncomponents; compno++) {
857 Jpeg2000Component *comp = tile->comp + compno;
858 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
860 /* Loop on resolution levels */
861 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
862 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
864 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
865 int nb_precincts, precno;
866 Jpeg2000Band *band = rlevel->band + bandno;
867 int cblkx, cblky, cblkno=0, bandpos;
869 bandpos = bandno + (reslevelno > 0);
871 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
874 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
875 /* Loop on precincts */
876 for (precno = 0; precno < nb_precincts; precno++) {
877 Jpeg2000Prec *prec = band->prec + precno;
879 /* Loop on codeblocks */
880 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
883 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
884 decode_cblk(s, codsty, &t1, cblk,
885 cblk->coord[0][1] - cblk->coord[0][0],
886 cblk->coord[1][1] - cblk->coord[1][0],
889 /* Manage band offsets */
890 x = cblk->coord[0][0];
891 y = cblk->coord[1][0];
893 if (codsty->transform == FF_DWT53) {
894 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
895 int *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * (y+j) + x];
896 int *ptr = t1.data[j];
897 for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
898 datap[i] = ptr[i] >> 1;
902 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
903 int *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * (y+j) + x];
904 int *ptr = t1.data[j];
905 for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
906 int tmp = ((int64_t)ptr[i]) * ((int64_t)band->stepsize) >> 16, tmp2;
907 tmp2 = FFABS(tmp>>1) + (tmp&1);
908 datap[i] = tmp < 0 ? -tmp2 : tmp2;
917 ff_dwt_decode(&comp->dwt, comp->data);
918 src[compno] = comp->data;
921 /* inverse MCT transformation */
922 if (tile->codsty[0].mct)
925 if (s->precision <= 8) {
926 for (compno = 0; compno < s->ncomponents; compno++) {
927 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
928 line = s->picture->data[0] + y * s->picture->linesize[0];
929 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
932 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
933 dst = line + x * s->ncomponents + compno;
935 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
936 *src[compno] += 1 << (s->cbps[compno]-1);
937 if (*src[compno] < 0)
939 else if (*src[compno] >= (1 << s->cbps[compno]))
940 *src[compno] = (1 << s->cbps[compno]) - 1;
941 *dst = *src[compno]++;
942 dst += s->ncomponents;
944 line += s->picture->linesize[0];
948 for (compno = 0; compno < s->ncomponents; compno++) {
949 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
950 line = s->picture->data[0] + y * s->picture->linesize[0];
951 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
954 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
955 dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
956 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
959 val = *src[compno]++ << (16 - s->cbps[compno]);
961 val = av_clip(val, 0, (1 << 16) - 1);
963 dst += s->ncomponents;
965 line += s->picture->linesize[0];
972 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
975 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
976 for (compno = 0; compno < s->ncomponents; compno++) {
977 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
978 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
980 ff_j2k_cleanup(comp, codsty);
982 av_freep(&s->tile[tileno].comp);
987 static int decode_codestream(Jpeg2000DecoderContext *s)
989 Jpeg2000CodingStyle *codsty = s->codsty;
990 Jpeg2000QuantStyle *qntsty = s->qntsty;
991 uint8_t *properties = s->properties;
994 int oldpos, marker, len, ret = 0;
996 if (bytestream2_get_bytes_left(&s->g) < 2) {
997 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1001 marker = bytestream2_get_be16u(&s->g);
1002 av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
1003 oldpos = bytestream2_tell(&s->g);
1005 if (marker == JPEG2000_SOD) {
1006 Jpeg2000Tile *tile = s->tile + s->curtileno;
1007 if (ret = init_tile(s, s->curtileno)) {
1008 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
1011 if (ret = jpeg2000_decode_packets(s, tile)) {
1012 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
1017 if (marker == JPEG2000_EOC)
1020 if (bytestream2_get_bytes_left(&s->g) < 2)
1021 return AVERROR(EINVAL);
1022 len = bytestream2_get_be16u(&s->g);
1027 s->numXtiles = s->numYtiles = 0;
1030 ret = get_coc(s, codsty, properties);
1033 ret = get_cod(s, codsty, properties);
1036 ret = get_qcc(s, len, qntsty, properties);
1039 ret = get_qcd(s, len, qntsty, properties);
1042 if (!(ret = get_sot(s))) {
1043 codsty = s->tile[s->curtileno].codsty;
1044 qntsty = s->tile[s->curtileno].qntsty;
1045 properties = s->tile[s->curtileno].properties;
1049 // the comment is ignored
1050 bytestream2_skip(&s->g, len - 2);
1053 // Tile-part lengths
1054 ret = get_tlm(s, len);
1057 av_log(s->avctx, AV_LOG_ERROR,
1058 "unsupported marker 0x%.4X at pos 0x%x\n",
1059 marker, bytestream2_tell(&s->g) - 4);
1060 bytestream2_skip(&s->g, len - 2);
1063 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1064 av_log(s->avctx, AV_LOG_ERROR,
1065 "error during processing marker segment %.4x\n", marker);
1066 return ret ? ret : -1;
1072 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1074 uint32_t atom_size, atom;
1075 int found_codestream = 0, search_range = 10;
1077 while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1078 atom_size = bytestream2_get_be32u(&s->g);
1079 atom = bytestream2_get_be32u(&s->g);
1080 if (atom == JP2_CODESTREAM) {
1081 found_codestream = 1;
1083 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1085 bytestream2_skipu(&s->g, atom_size - 8);
1090 if (found_codestream)
1095 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1096 int *got_frame, AVPacket *avpkt)
1098 Jpeg2000DecoderContext *s = avctx->priv_data;
1099 AVFrame *picture = data;
1102 s->picture = picture;
1105 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1108 // reduction factor, i.e number of resolution levels to skip
1109 s->reduction_factor = avctx->lowres;
1111 if (bytestream2_get_bytes_left(&s->g) < 2) {
1112 ret = AVERROR(EINVAL);
1116 // check if the image is in jp2 format
1117 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1118 (bytestream2_get_be32u(&s->g) == 12) &&
1119 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1120 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1121 if (!jp2_find_codestream(s)) {
1122 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1127 bytestream2_seek(&s->g, 0, SEEK_SET);
1130 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1131 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1135 if (ret = decode_codestream(s))
1138 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1139 if (ret = decode_tile(s, s->tile + tileno))
1142 jpeg2000_dec_cleanup(s);
1146 return bytestream2_tell(&s->g);
1149 jpeg2000_dec_cleanup(s);
1153 static void jpeg2000_init_static_data(AVCodec *codec)
1155 ff_jpeg2000_init_tier1_luts();
1158 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1159 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1161 static const AVOption options[] = {
1162 { "lowres", "Lower the decoding resolution by a power of two",
1163 OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1167 static const AVProfile profiles[] = {
1168 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1169 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1170 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1171 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1172 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1173 { FF_PROFILE_UNKNOWN },
1176 static const AVClass class = {
1177 .class_name = "j2k",
1178 .item_name = av_default_item_name,
1180 .version = LIBAVUTIL_VERSION_INT,
1183 AVCodec ff_j2k_decoder = {
1185 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1186 .type = AVMEDIA_TYPE_VIDEO,
1187 .id = AV_CODEC_ID_JPEG2000,
1188 .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
1189 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1190 .init_static_data = jpeg2000_init_static_data,
1191 .decode = jpeg2000_decode_frame,
1192 .priv_class = &class,
1194 .profiles = NULL_IF_CONFIG_SMALL(profiles)