2 * JPEG2000 image decoder
3 * Copyright (c) 2007 Kamil Nowosad
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * JPEG2000 image decoder
25 * @author Kamil Nowosad
31 #include "bytestream.h"
35 #include "libavutil/common.h"
37 #define JP2_SIG_TYPE 0x6A502020
38 #define JP2_SIG_VALUE 0x0D0A870A
39 #define JP2_CODESTREAM 0x6A703263
45 Jpeg2000Component *comp;
46 uint8_t properties[4];
47 Jpeg2000CodingStyle codsty[4];
48 Jpeg2000QuantStyle qntsty[4];
51 typedef struct Jpeg2000DecoderContext {
52 AVCodecContext *avctx;
57 int image_offset_x, image_offset_y;
58 int tile_offset_x, tile_offset_y;
59 uint8_t cbps[4]; // bits per sample in particular components
60 uint8_t sgnd[4]; // if a component is signed
61 uint8_t properties[4];
65 int tile_width, tile_height;
66 int numXtiles, numYtiles;
69 Jpeg2000CodingStyle codsty[4];
70 Jpeg2000QuantStyle qntsty[4];
77 } Jpeg2000DecoderContext;
79 static int get_bits(Jpeg2000DecoderContext *s, int n)
85 if (s->bit_index == 0) {
86 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
89 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
94 static void j2k_flush(Jpeg2000DecoderContext *s)
96 if (bytestream2_get_byte(&s->g) == 0xff)
97 bytestream2_skip(&s->g, 1);
101 /** decode the value stored in node */
102 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
104 Jpeg2000TgtNode *stack[30];
105 int sp = -1, curval = 0;
108 return AVERROR(EINVAL);
110 while (node && !node->vis) {
118 curval = stack[sp]->val;
120 while (curval < threshold && sp >= 0) {
121 if (curval < stack[sp]->val)
122 curval = stack[sp]->val;
123 while (curval < threshold) {
125 if ((ret = get_bits(s, 1)) > 0) {
133 stack[sp]->val = curval;
139 /* marker segments */
140 /* get sizes and offsets of image, tiles; number of components */
141 static int get_siz(Jpeg2000DecoderContext *s)
144 ThreadFrame frame = { .f = s->picture };
146 if (bytestream2_get_bytes_left(&s->g) < 36)
147 return AVERROR(EINVAL);
149 bytestream2_get_be16u(&s->g); // Rsiz
150 s->width = bytestream2_get_be32u(&s->g); // Width
151 s->height = bytestream2_get_be32u(&s->g); // Height
152 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
153 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
154 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
155 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
156 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
157 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
158 s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
160 if (s->ncomponents <= 0 || s->ncomponents > 4) {
161 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
162 return AVERROR(EINVAL);
164 if (s->tile_width<=0 || s->tile_height<=0)
165 return AVERROR(EINVAL);
167 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
168 return AVERROR(EINVAL);
170 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
171 uint8_t x = bytestream2_get_byteu(&s->g);
172 s->cbps[i] = (x & 0x7f) + 1;
173 s->precision = FFMAX(s->cbps[i], s->precision);
174 s->sgnd[i] = !!(x & 0x80);
175 s->cdx[i] = bytestream2_get_byteu(&s->g);
176 s->cdy[i] = bytestream2_get_byteu(&s->g);
179 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
180 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
182 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
183 return AVERROR(EINVAL);
185 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(Jpeg2000Tile));
187 return AVERROR(ENOMEM);
189 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
190 Jpeg2000Tile *tile = s->tile + i;
192 tile->comp = av_mallocz(s->ncomponents * sizeof(Jpeg2000Component));
194 return AVERROR(ENOMEM);
197 s->avctx->width = s->width - s->image_offset_x;
198 s->avctx->height = s->height - s->image_offset_y;
200 switch(s->ncomponents) {
202 if (s->precision > 8) {
203 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
205 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
209 if (s->precision > 8) {
210 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
212 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
216 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
221 if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
224 s->picture->pict_type = AV_PICTURE_TYPE_I;
225 s->picture->key_frame = 1;
230 /** get common part for COD and COC segments */
231 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
233 if (bytestream2_get_bytes_left(&s->g) < 5)
234 return AVERROR(EINVAL);
235 c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
236 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
237 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
239 c->cblk_style = bytestream2_get_byteu(&s->g);
240 if (c->cblk_style != 0) { // cblk style
241 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
243 c->transform = bytestream2_get_byteu(&s->g); // transformation
244 if (c->csty & JPEG2000_CSTY_PREC) {
247 for (i = 0; i < c->nreslevels; i++)
248 bytestream2_get_byte(&s->g);
253 /** get coding parameters for a particular tile or whole image*/
254 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
256 Jpeg2000CodingStyle tmp;
259 if (bytestream2_get_bytes_left(&s->g) < 5)
260 return AVERROR(EINVAL);
262 tmp.log2_prec_width =
263 tmp.log2_prec_height = 15;
265 tmp.csty = bytestream2_get_byteu(&s->g);
267 // get progression order
268 tmp.prog_order = bytestream2_get_byteu(&s->g);
269 if (tmp.prog_order) {
270 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
273 tmp.nlayers = bytestream2_get_be16u(&s->g);
274 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
277 for (compno = 0; compno < s->ncomponents; compno++) {
278 if (!(properties[compno] & HAD_COC))
279 memcpy(c + compno, &tmp, sizeof(Jpeg2000CodingStyle));
284 /** get coding parameters for a component in the whole image on a particular tile */
285 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
289 if (bytestream2_get_bytes_left(&s->g) < 2)
290 return AVERROR(EINVAL);
292 compno = bytestream2_get_byteu(&s->g);
295 c->csty = bytestream2_get_byte(&s->g);
298 properties[compno] |= HAD_COC;
302 /** get common part for QCD and QCC segments */
303 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
307 if (bytestream2_get_bytes_left(&s->g) < 1)
308 return AVERROR(EINVAL);
310 x = bytestream2_get_byteu(&s->g); // Sqcd
312 q->nguardbits = x >> 5;
313 q->quantsty = x & 0x1f;
315 if (q->quantsty == JPEG2000_QSTY_NONE) {
317 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
318 return AVERROR(EINVAL);
319 for (i = 0; i < n; i++)
320 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
321 } else if (q->quantsty == JPEG2000_QSTY_SI) {
322 if (bytestream2_get_bytes_left(&s->g) < 2)
323 return AVERROR(EINVAL);
324 x = bytestream2_get_be16u(&s->g);
325 q->expn[0] = x >> 11;
326 q->mant[0] = x & 0x7ff;
327 for (i = 1; i < 32 * 3; i++) {
328 int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
329 q->expn[i] = curexpn;
330 q->mant[i] = q->mant[0];
334 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
335 return AVERROR(EINVAL);
336 for (i = 0; i < n; i++) {
337 x = bytestream2_get_be16u(&s->g);
338 q->expn[i] = x >> 11;
339 q->mant[i] = x & 0x7ff;
345 /** get quantization parameters for a particular tile or a whole image */
346 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
348 Jpeg2000QuantStyle tmp;
351 if (get_qcx(s, n, &tmp))
353 for (compno = 0; compno < s->ncomponents; compno++)
354 if (!(properties[compno] & HAD_QCC))
355 memcpy(q + compno, &tmp, sizeof(Jpeg2000QuantStyle));
359 /** get quantization parameters for a component in the whole image on in a particular tile */
360 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
364 if (bytestream2_get_bytes_left(&s->g) < 1)
365 return AVERROR(EINVAL);
367 compno = bytestream2_get_byteu(&s->g);
368 properties[compno] |= HAD_QCC;
369 return get_qcx(s, n-1, q+compno);
372 /** get start of tile segment */
373 static int get_sot(Jpeg2000DecoderContext *s)
375 if (bytestream2_get_bytes_left(&s->g) < 8)
376 return AVERROR(EINVAL);
378 s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
379 if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
381 return AVERROR(EINVAL);
384 bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
386 if (!bytestream2_get_byteu(&s->g)) { ///< TPsot
387 Jpeg2000Tile *tile = s->tile + s->curtileno;
390 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
391 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
393 bytestream2_get_byteu(&s->g); ///< TNsot
398 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
401 tilex = tileno % s->numXtiles,
402 tiley = tileno / s->numXtiles;
403 Jpeg2000Tile *tile = s->tile + tileno;
406 return AVERROR(ENOMEM);
407 for (compno = 0; compno < s->ncomponents; compno++) {
408 Jpeg2000Component *comp = tile->comp + compno;
409 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
410 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
411 int ret; // global bandno
413 comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
414 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
415 comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
416 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
418 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
424 /** read the number of coding passes */
425 static int getnpasses(Jpeg2000DecoderContext *s)
432 if ((num = get_bits(s, 2)) != 3)
433 return num < 0 ? num : 3 + num;
434 if ((num = get_bits(s, 5)) != 31)
435 return num < 0 ? num : 6 + num;
436 num = get_bits(s, 7);
437 return num < 0 ? num : 37 + num;
440 static int getlblockinc(Jpeg2000DecoderContext *s)
443 while (ret = get_bits(s, 1)) {
451 static int decode_packet(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno,
452 int layno, uint8_t *expn, int numgbits)
454 int bandno, cblkny, cblknx, cblkno, ret;
456 if (!(ret = get_bits(s, 1))) {
462 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
463 Jpeg2000Band *band = rlevel->band + bandno;
464 Jpeg2000Prec *prec = band->prec + precno;
467 if (band->coord[0][0] == band->coord[0][1]
468 || band->coord[1][0] == band->coord[1][1])
471 for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
472 for (cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++) {
473 Jpeg2000Cblk *cblk = band->cblk + cblkno;
474 int incl, newpasses, llen;
477 incl = get_bits(s, 1);
479 incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
486 cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
487 if ((newpasses = getnpasses(s)) < 0)
489 if ((llen = getlblockinc(s)) < 0)
491 cblk->lblock += llen;
492 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
494 cblk->lengthinc = ret;
495 cblk->npasses += newpasses;
500 if (codsty->csty & JPEG2000_CSTY_EPH) {
501 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH) {
502 bytestream2_skip(&s->g, 2);
504 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
508 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
509 Jpeg2000Band *band = rlevel->band + bandno;
510 int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
511 for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++) {
513 for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++) {
514 Jpeg2000Cblk *cblk = band->cblk + yi * cblknw + xi;
515 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
516 || sizeof(cblk->data) < cblk->lengthinc
518 return AVERROR(EINVAL);
519 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
520 cblk->length += cblk->lengthinc;
528 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
530 int layno, reslevelno, compno, precno, ok_reslevel;
532 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
534 for (reslevelno = 0; ok_reslevel; reslevelno++) {
536 for (compno = 0; compno < s->ncomponents; compno++) {
537 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
538 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
539 if (reslevelno < codsty->nreslevels) {
540 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
542 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
543 if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
544 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
554 /* TIER-1 routines */
555 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
556 int vert_causal_ctx_csty_symbol)
558 int mask = 3 << (bpno - 1), y0, x, y;
560 for (y0 = 0; y0 < height; y0 += 4)
561 for (x = 0; x < width; x++)
562 for (y = y0; y < height && y < y0+4; y++) {
563 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
564 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
566 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
567 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
568 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
569 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
570 if (bpass_csty_symbol)
571 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
573 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
576 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
578 t1->flags[y+1][x+1] |= JPEG2000_T1_VIS;
583 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpno)
588 phalf = 1 << (bpno - 1);
591 for (y0 = 0; y0 < height; y0 += 4)
592 for (x = 0; x < width; x++)
593 for (y = y0; y < height && y < y0+4; y++) {
594 if ((t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
595 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y+1][x+1]);
596 int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
597 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
598 t1->flags[y+1][x+1] |= JPEG2000_T1_REF;
603 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height,
604 int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
606 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
608 for (y0 = 0; y0 < height; y0 += 4) {
609 for (x = 0; x < width; x++) {
610 if (y0 + 3 < height && !(
611 (t1->flags[y0+1][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
612 (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
613 (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
614 (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
615 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
617 runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
618 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
625 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
627 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
629 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
630 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
631 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
636 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
637 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
638 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
641 t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
647 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
648 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
649 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
650 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
652 av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
657 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
658 int width, int height, int bandpos)
660 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
661 int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
662 int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
664 for (y = 0; y < height+2; y++)
665 memset(t1->flags[y], 0, (width+2)*sizeof(int));
667 for (y = 0; y < height; y++)
668 memset(t1->data[y], 0, width*sizeof(int));
670 cblk->data[cblk->length] = 0xff;
671 cblk->data[cblk->length+1] = 0xff;
672 ff_mqc_initdec(&t1->mqc, cblk->data);
676 case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
677 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
679 case 1: decode_refpass(t1, width, height, bpno+1);
680 if (bpass_csty_symbol && clnpass_cnt >= 4)
681 ff_mqc_initdec(&t1->mqc, cblk->data);
683 case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
684 codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
685 clnpass_cnt = clnpass_cnt + 1;
686 if (bpass_csty_symbol && clnpass_cnt >= 4)
687 ff_mqc_initdec(&t1->mqc, cblk->data);
700 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
702 int i, *src[3], i0, i1, i2, csize = 1;
704 for (i = 0; i < 3; i++)
705 src[i] = tile->comp[i].data;
707 for (i = 0; i < 2; i++)
708 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
710 if (tile->codsty[0].transform == FF_DWT97) {
711 for (i = 0; i < csize; i++) {
712 i0 = *src[0] + (*src[2] * 46802 >> 16);
713 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
714 i2 = *src[0] + (116130 * *src[1] >> 16);
720 for (i = 0; i < csize; i++) {
721 i1 = *src[0] - (*src[2] + *src[1] >> 2);
731 static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
733 int compno, reslevelno, bandno;
736 Jpeg2000T1Context t1;
738 for (compno = 0; compno < s->ncomponents; compno++) {
739 Jpeg2000Component *comp = tile->comp + compno;
740 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
742 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
743 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
744 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
745 Jpeg2000Band *band = rlevel->band + bandno;
746 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
748 bandpos = bandno + (reslevelno > 0);
750 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
752 yy1 = FFMIN(ff_jpeg2000_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
753 band->coord[1][1]) - band->coord[1][0] + yy0;
755 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
758 for (cblky = 0; cblky < band->cblkny; cblky++) {
759 if (reslevelno == 0 || bandno == 1)
762 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
764 xx1 = FFMIN(ff_jpeg2000_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
765 band->coord[0][1]) - band->coord[0][0] + xx0;
767 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++) {
769 decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
770 if (codsty->transform == FF_DWT53) {
771 for (y = yy0; y < yy1; y+=s->cdy[compno]) {
772 int *ptr = t1.data[y-yy0];
773 for (x = xx0; x < xx1; x+=s->cdx[compno]) {
774 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
778 for (y = yy0; y < yy1; y+=s->cdy[compno]) {
779 int *ptr = t1.data[y-yy0];
780 for (x = xx0; x < xx1; x+=s->cdx[compno]) {
781 int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
782 tmp2 = FFABS(tmp>>1) + (tmp&1);
783 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
788 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
791 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
795 ff_j2k_dwt_decode(&comp->dwt, comp->data);
796 src[compno] = comp->data;
798 if (tile->codsty[0].mct)
801 if (s->precision <= 8) {
802 for (compno = 0; compno < s->ncomponents; compno++) {
803 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
804 line = s->picture->data[0] + y * s->picture->linesize[0];
805 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
808 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
809 dst = line + x * s->ncomponents + compno;
811 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
812 *src[compno] += 1 << (s->cbps[compno]-1);
813 if (*src[compno] < 0)
815 else if (*src[compno] >= (1 << s->cbps[compno]))
816 *src[compno] = (1 << s->cbps[compno]) - 1;
817 *dst = *src[compno]++;
818 dst += s->ncomponents;
820 line += s->picture->linesize[0];
824 for (compno = 0; compno < s->ncomponents; compno++) {
825 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
826 line = s->picture->data[0] + y * s->picture->linesize[0];
827 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
830 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
831 dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
832 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
835 val = *src[compno]++ << (16 - s->cbps[compno]);
837 val = av_clip(val, 0, (1 << 16) - 1);
839 dst += s->ncomponents;
841 line += s->picture->linesize[0];
848 static void cleanup(Jpeg2000DecoderContext *s)
851 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
852 for (compno = 0; compno < s->ncomponents; compno++) {
853 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
854 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
856 ff_j2k_cleanup(comp, codsty);
858 av_freep(&s->tile[tileno].comp);
863 static int decode_codestream(Jpeg2000DecoderContext *s)
865 Jpeg2000CodingStyle *codsty = s->codsty;
866 Jpeg2000QuantStyle *qntsty = s->qntsty;
867 uint8_t *properties = s->properties;
870 int oldpos, marker, len, ret = 0;
872 if (bytestream2_get_bytes_left(&s->g) < 2) {
873 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
877 marker = bytestream2_get_be16u(&s->g);
878 av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
879 oldpos = bytestream2_tell(&s->g);
881 if (marker == JPEG2000_SOD) {
882 Jpeg2000Tile *tile = s->tile + s->curtileno;
883 if (ret = init_tile(s, s->curtileno)) {
884 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
887 if (ret = jpeg2000_decode_packets(s, tile)) {
888 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
893 if (marker == JPEG2000_EOC)
896 if (bytestream2_get_bytes_left(&s->g) < 2)
897 return AVERROR(EINVAL);
898 len = bytestream2_get_be16u(&s->g);
903 s->numXtiles = s->numYtiles = 0;
906 ret = get_coc(s, codsty, properties);
909 ret = get_cod(s, codsty, properties);
912 ret = get_qcc(s, len, qntsty, properties);
915 ret = get_qcd(s, len, qntsty, properties);
918 if (!(ret = get_sot(s))) {
919 codsty = s->tile[s->curtileno].codsty;
920 qntsty = s->tile[s->curtileno].qntsty;
921 properties = s->tile[s->curtileno].properties;
925 // the comment is ignored
926 bytestream2_skip(&s->g, len - 2);
929 av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
930 bytestream2_skip(&s->g, len - 2);
933 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
934 av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
935 return ret ? ret : -1;
941 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
943 uint32_t atom_size, atom;
944 int found_codestream = 0, search_range = 10;
946 while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
947 atom_size = bytestream2_get_be32u(&s->g);
948 atom = bytestream2_get_be32u(&s->g);
949 if (atom == JP2_CODESTREAM) {
950 found_codestream = 1;
952 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
954 bytestream2_skipu(&s->g, atom_size - 8);
959 if (found_codestream)
964 static int decode_frame(AVCodecContext *avctx,
965 void *data, int *got_frame,
968 Jpeg2000DecoderContext *s = avctx->priv_data;
969 AVFrame *picture = data;
972 s->picture = picture;
975 bytestream2_init(&s->g, avpkt->data, avpkt->size);
978 if (bytestream2_get_bytes_left(&s->g) < 2) {
979 ret = AVERROR(EINVAL);
983 // check if the image is in jp2 format
984 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
985 (bytestream2_get_be32u(&s->g) == 12) &&
986 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
987 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
988 if (!jp2_find_codestream(s)) {
989 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
994 bytestream2_seek(&s->g, 0, SEEK_SET);
997 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
998 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1002 if (ret = decode_codestream(s))
1005 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1006 if (ret = decode_tile(s, s->tile + tileno))
1013 return bytestream2_tell(&s->g);
1020 static void jpeg2000_init_static_data(AVCodec *codec)
1022 ff_jpeg2000_init_tier1_luts();
1025 AVCodec ff_j2k_decoder = {
1027 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1028 .type = AVMEDIA_TYPE_VIDEO,
1029 .id = AV_CODEC_ID_JPEG2000,
1030 .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
1031 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1032 .init_static_data = jpeg2000_init_static_data,
1033 .decode = decode_frame,