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
29 #include "bytestream.h"
31 #include "libavutil/common.h"
33 #define JP2_SIG_TYPE 0x6A502020
34 #define JP2_SIG_VALUE 0x0D0A870A
35 #define JP2_CODESTREAM 0x6A703263
42 uint8_t properties[4];
43 J2kCodingStyle codsty[4];
44 J2kQuantStyle qntsty[4];
48 AVCodecContext *avctx;
51 int width, height; ///< image width and height
52 int image_offset_x, image_offset_y;
53 int tile_offset_x, tile_offset_y;
54 uint8_t cbps[4]; ///< bits per sample in particular components
55 uint8_t sgnd[4]; ///< if a component is signed
56 uint8_t properties[4];
60 int tile_width, tile_height; ///< tile size
61 int numXtiles, numYtiles;
64 J2kCodingStyle codsty[4];
65 J2kQuantStyle qntsty[4];
77 static int get_bits(J2kDecoderContext *s, int n)
80 if (s->buf_end - s->buf < ((n - s->bit_index) >> 8))
81 return AVERROR(EINVAL);
84 if (s->bit_index == 0){
85 s->bit_index = 7 + (*s->buf != 0xff);
89 res |= (*s->buf >> s->bit_index) & 1;
94 static void j2k_flush(J2kDecoderContext *s)
102 void printcomp(J2kComponent *comp)
105 for (i = 0; i < comp->y1 - comp->y0; i++)
106 ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
109 static void nspaces(FILE *fd, int n)
111 while(n--) putc(' ', fd);
114 static void dump(J2kDecoderContext *s, FILE *fd)
116 int tileno, compno, reslevelno, bandno, precno;
117 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
118 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
120 s->width, s->height, s->tile_width, s->tile_height,
121 s->numXtiles, s->numYtiles, s->ncomponents);
122 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
123 J2kTile *tile = s->tile + tileno;
125 fprintf(fd, "tile %d:\n", tileno);
126 for(compno = 0; compno < s->ncomponents; compno++){
127 J2kComponent *comp = tile->comp + compno;
129 fprintf(fd, "component %d:\n", compno);
131 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
132 comp->x0, comp->x1, comp->y0, comp->y1);
133 for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
134 J2kResLevel *reslevel = comp->reslevel + reslevelno;
136 fprintf(fd, "reslevel %d:\n", reslevelno);
138 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
139 reslevel->x0, reslevel->x1, reslevel->y0,
140 reslevel->y1, reslevel->nbands);
141 for(bandno = 0; bandno < reslevel->nbands; bandno++){
142 J2kBand *band = reslevel->band + bandno;
144 fprintf(fd, "band %d:\n", bandno);
146 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
147 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
150 band->codeblock_width, band->codeblock_height,
151 band->cblknx, band->cblkny);
152 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
153 J2kPrec *prec = band->prec + precno;
155 fprintf(fd, "prec %d:\n", precno);
157 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
158 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
167 /** decode the value stored in node */
168 static int tag_tree_decode(J2kDecoderContext *s, J2kTgtNode *node, int threshold)
170 J2kTgtNode *stack[30];
171 int sp = -1, curval = 0;
173 while(node && !node->vis){
181 curval = stack[sp]->val;
183 while(curval < threshold && sp >= 0){
184 if (curval < stack[sp]->val)
185 curval = stack[sp]->val;
186 while (curval < threshold){
188 if ((ret = get_bits(s, 1)) > 0){
196 stack[sp]->val = curval;
202 /* marker segments */
203 /** get sizes and offsets of image, tiles; number of components */
204 static int get_siz(J2kDecoderContext *s)
208 if (s->buf_end - s->buf < 36)
209 return AVERROR(EINVAL);
211 bytestream_get_be16(&s->buf); // Rsiz (skipped)
212 s->width = bytestream_get_be32(&s->buf); // width
213 s->height = bytestream_get_be32(&s->buf); // height
214 s->image_offset_x = bytestream_get_be32(&s->buf); // X0Siz
215 s->image_offset_y = bytestream_get_be32(&s->buf); // Y0Siz
217 s->tile_width = bytestream_get_be32(&s->buf); // XTSiz
218 s->tile_height = bytestream_get_be32(&s->buf); // YTSiz
219 s->tile_offset_x = bytestream_get_be32(&s->buf); // XT0Siz
220 s->tile_offset_y = bytestream_get_be32(&s->buf); // YT0Siz
221 s->ncomponents = bytestream_get_be16(&s->buf); // CSiz
223 if (s->buf_end - s->buf < 2 * s->ncomponents)
224 return AVERROR(EINVAL);
226 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
227 uint8_t x = bytestream_get_byte(&s->buf);
228 s->cbps[i] = (x & 0x7f) + 1;
229 s->precision = FFMAX(s->cbps[i], s->precision);
230 s->sgnd[i] = (x & 0x80) == 1;
231 s->cdx[i] = bytestream_get_byte(&s->buf);
232 s->cdy[i] = bytestream_get_byte(&s->buf);
235 s->numXtiles = ff_j2k_ceildiv(s->width - s->tile_offset_x, s->tile_width);
236 s->numYtiles = ff_j2k_ceildiv(s->height - s->tile_offset_y, s->tile_height);
238 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(J2kTile));
240 return AVERROR(ENOMEM);
242 for (i = 0; i < s->numXtiles * s->numYtiles; i++){
243 J2kTile *tile = s->tile + i;
245 tile->comp = av_mallocz(s->ncomponents * sizeof(J2kComponent));
247 return AVERROR(ENOMEM);
250 s->avctx->width = s->width - s->image_offset_x;
251 s->avctx->height = s->height - s->image_offset_y;
253 switch(s->ncomponents){
254 case 1: if (s->precision > 8) {
255 s->avctx->pix_fmt = PIX_FMT_GRAY16;
256 } else s->avctx->pix_fmt = PIX_FMT_GRAY8;
258 case 3: if (s->precision > 8) {
259 s->avctx->pix_fmt = PIX_FMT_RGB48;
260 } else s->avctx->pix_fmt = PIX_FMT_RGB24;
262 case 4: s->avctx->pix_fmt = PIX_FMT_BGRA; break;
265 if (s->picture.data[0])
266 s->avctx->release_buffer(s->avctx, &s->picture);
268 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0)
271 s->picture.pict_type = FF_I_TYPE;
272 s->picture.key_frame = 1;
277 /** get common part for COD and COC segments */
278 static int get_cox(J2kDecoderContext *s, J2kCodingStyle *c)
280 if (s->buf_end - s->buf < 5)
281 return AVERROR(EINVAL);
282 c->nreslevels = bytestream_get_byte(&s->buf) + 1; // num of resolution levels - 1
283 c->log2_cblk_width = bytestream_get_byte(&s->buf) + 2; // cblk width
284 c->log2_cblk_height = bytestream_get_byte(&s->buf) + 2; // cblk height
286 c->cblk_style = bytestream_get_byte(&s->buf);
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 = bytestream_get_byte(&s->buf); // transformation
291 if (c->csty & J2K_CSTY_PREC) {
293 for (i = 0; i < c->nreslevels; i++)
294 bytestream_get_byte(&s->buf);
299 /** get coding parameters for a particular tile or whole image*/
300 static int get_cod(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
305 if (s->buf_end - s->buf < 5)
306 return AVERROR(EINVAL);
308 tmp.log2_prec_width =
309 tmp.log2_prec_height = 15;
311 tmp.csty = bytestream_get_byte(&s->buf);
313 if (bytestream_get_byte(&s->buf)){ // progression level
314 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
318 tmp.nlayers = bytestream_get_be16(&s->buf);
319 tmp.mct = bytestream_get_byte(&s->buf); // multiple component transformation
322 for (compno = 0; compno < s->ncomponents; compno++){
323 if (!(properties[compno] & HAD_COC))
324 memcpy(c + compno, &tmp, sizeof(J2kCodingStyle));
329 /** get coding parameters for a component in the whole image on a particular tile */
330 static int get_coc(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
334 if (s->buf_end - s->buf < 2)
335 return AVERROR(EINVAL);
337 compno = bytestream_get_byte(&s->buf);
340 c->csty = bytestream_get_byte(&s->buf);
343 properties[compno] |= HAD_COC;
347 /** get common part for QCD and QCC segments */
348 static int get_qcx(J2kDecoderContext *s, int n, J2kQuantStyle *q)
352 if (s->buf_end - s->buf < 1)
353 return AVERROR(EINVAL);
355 x = bytestream_get_byte(&s->buf); // Sqcd
357 q->nguardbits = x >> 5;
358 q->quantsty = x & 0x1f;
360 if (q->quantsty == J2K_QSTY_NONE){
362 if (s->buf_end - s->buf < n)
363 return AVERROR(EINVAL);
364 for (i = 0; i < n; i++)
365 q->expn[i] = bytestream_get_byte(&s->buf) >> 3;
366 } else if (q->quantsty == J2K_QSTY_SI){
367 if (s->buf_end - s->buf < 2)
368 return AVERROR(EINVAL);
369 x = bytestream_get_be16(&s->buf);
370 q->expn[0] = x >> 11;
371 q->mant[0] = x & 0x7ff;
372 for (i = 1; i < 32 * 3; i++){
373 int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
374 q->expn[i] = curexpn;
375 q->mant[i] = q->mant[0];
379 if (s->buf_end - s->buf < n)
380 return AVERROR(EINVAL);
381 for (i = 0; i < n; i++){
382 x = bytestream_get_be16(&s->buf);
383 q->expn[i] = x >> 11;
384 q->mant[i] = x & 0x7ff;
390 /** get quantization parameters for a particular tile or a whole image */
391 static int get_qcd(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
396 if (get_qcx(s, n, &tmp))
398 for (compno = 0; compno < s->ncomponents; compno++)
399 if (!(properties[compno] & HAD_QCC))
400 memcpy(q + compno, &tmp, sizeof(J2kQuantStyle));
404 /** get quantization parameters for a component in the whole image on in a particular tile */
405 static int get_qcc(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
409 if (s->buf_end - s->buf < 1)
410 return AVERROR(EINVAL);
412 compno = bytestream_get_byte(&s->buf);
413 properties[compno] |= HAD_QCC;
414 return get_qcx(s, n-1, q+compno);
417 /** get start of tile segment */
418 static uint8_t get_sot(J2kDecoderContext *s)
420 if (s->buf_end - s->buf < 4)
421 return AVERROR(EINVAL);
423 s->curtileno = bytestream_get_be16(&s->buf); ///< Isot
425 s->buf += 4; ///< Psot (ignored)
427 if (!bytestream_get_byte(&s->buf)){ ///< TPsot
428 J2kTile *tile = s->tile + s->curtileno;
431 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(J2kCodingStyle));
432 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(J2kQuantStyle));
434 bytestream_get_byte(&s->buf); ///< TNsot
439 static int init_tile(J2kDecoderContext *s, int tileno)
442 tilex = tileno % s->numXtiles,
443 tiley = tileno / s->numXtiles;
444 J2kTile *tile = s->tile + tileno;
447 return AVERROR(ENOMEM);
448 for (compno = 0; compno < s->ncomponents; compno++){
449 J2kComponent *comp = tile->comp + compno;
450 J2kCodingStyle *codsty = tile->codsty + compno;
451 J2kQuantStyle *qntsty = tile->qntsty + compno;
452 int ret; // global bandno
454 comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
455 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
456 comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
457 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
459 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
465 /** read the number of coding passes */
466 static int getnpasses(J2kDecoderContext *s)
473 if ((num = get_bits(s, 2)) != 3)
474 return num < 0 ? num : 3 + num;
475 if ((num = get_bits(s, 5)) != 31)
476 return num < 0 ? num : 6 + num;
477 num = get_bits(s, 7);
478 return num < 0 ? num : 37 + num;
481 static int getlblockinc(J2kDecoderContext *s)
484 while (ret = get_bits(s, 1)){
492 static int decode_packet(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kResLevel *rlevel, int precno,
493 int layno, uint8_t *expn, int numgbits)
495 int bandno, cblkny, cblknx, cblkno, ret;
497 if (!(ret = get_bits(s, 1))){
503 for (bandno = 0; bandno < rlevel->nbands; bandno++){
504 J2kBand *band = rlevel->band + bandno;
505 J2kPrec *prec = band->prec + precno;
508 if (band->coord[0][0] == band->coord[0][1]
509 || band->coord[1][0] == band->coord[1][1])
512 for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
513 for(cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
514 J2kCblk *cblk = band->cblk + cblkno;
515 int incl, newpasses, llen;
518 incl = get_bits(s, 1);
520 incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
527 cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
528 if ((newpasses = getnpasses(s)) < 0)
530 if ((llen = getlblockinc(s)) < 0)
532 cblk->lblock += llen;
533 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
535 cblk->lengthinc = ret;
536 cblk->npasses += newpasses;
541 if (codsty->csty & J2K_CSTY_EPH) {
542 if (AV_RB16(s->buf) == J2K_EPH) {
545 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
549 for (bandno = 0; bandno < rlevel->nbands; bandno++){
550 J2kBand *band = rlevel->band + bandno;
551 int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
552 for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++){
554 for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++){
555 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
556 if (s->buf_end - s->buf < cblk->lengthinc)
557 return AVERROR(EINVAL);
558 bytestream_get_buffer(&s->buf, cblk->data, cblk->lengthinc);
559 cblk->length += cblk->lengthinc;
567 static int decode_packets(J2kDecoderContext *s, J2kTile *tile)
569 int layno, reslevelno, compno, precno, ok_reslevel;
571 for (layno = 0; layno < tile->codsty[0].nlayers; layno++){
573 for (reslevelno = 0; ok_reslevel; reslevelno++){
575 for (compno = 0; compno < s->ncomponents; compno++){
576 J2kCodingStyle *codsty = tile->codsty + compno;
577 J2kQuantStyle *qntsty = tile->qntsty + compno;
578 if (reslevelno < codsty->nreslevels){
579 J2kResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
581 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
582 if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
583 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
593 /* TIER-1 routines */
594 static void decode_sigpass(J2kT1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
595 int vert_causal_ctx_csty_symbol)
597 int mask = 3 << (bpno - 1), y0, x, y;
599 for (y0 = 0; y0 < height; y0 += 4)
600 for (x = 0; x < width; x++)
601 for (y = y0; y < height && y < y0+4; y++){
602 if ((t1->flags[y+1][x+1] & J2K_T1_SIG_NB)
603 && !(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
604 int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 && y == 3);
605 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno,
606 vert_causal_ctx_csty_loc_symbol))){
607 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
608 if (bpass_csty_symbol)
609 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
611 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
614 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
616 t1->flags[y+1][x+1] |= J2K_T1_VIS;
621 static void decode_refpass(J2kT1Context *t1, int width, int height, int bpno)
626 phalf = 1 << (bpno - 1);
629 for (y0 = 0; y0 < height; y0 += 4)
630 for (x = 0; x < width; x++)
631 for (y = y0; y < height && y < y0+4; y++){
632 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
633 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
634 int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
635 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
636 t1->flags[y+1][x+1] |= J2K_T1_REF;
641 static void decode_clnpass(J2kDecoderContext *s, J2kT1Context *t1, int width, int height,
642 int bpno, int bandno, int seg_symbols)
644 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
646 for (y0 = 0; y0 < height; y0 += 4) {
647 for (x = 0; x < width; x++){
648 if (y0 + 3 < height && !(
649 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
650 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
651 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
652 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)))){
653 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
655 runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
656 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
663 for (y = y0 + runlen; y < y0 + 4 && y < height; y++){
665 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)))
666 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1],
670 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
671 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
672 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
675 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
681 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
682 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
683 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
684 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
686 av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
691 static int decode_cblk(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kT1Context *t1, J2kCblk *cblk,
692 int width, int height, int bandpos)
694 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
696 for (y = 0; y < height+2; y++)
697 memset(t1->flags[y], 0, (width+2)*sizeof(int));
699 for (y = 0; y < height; y++)
700 memset(t1->data[y], 0, width*sizeof(int));
702 ff_mqc_initdec(&t1->mqc, cblk->data);
703 cblk->data[cblk->length] = 0xff;
704 cblk->data[cblk->length+1] = 0xff;
706 int bpass_csty_symbol = J2K_CBLK_BYPASS & codsty->cblk_style;
707 int vert_causal_ctx_csty_symbol = J2K_CBLK_VSC & codsty->cblk_style;
711 case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
712 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
714 case 1: decode_refpass(t1, width, height, bpno+1);
715 if (bpass_csty_symbol && clnpass_cnt >= 4)
716 ff_mqc_initdec(&t1->mqc, cblk->data);
718 case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
719 codsty->cblk_style & J2K_CBLK_SEGSYM);
720 clnpass_cnt = clnpass_cnt + 1;
721 if (bpass_csty_symbol && clnpass_cnt >= 4)
722 ff_mqc_initdec(&t1->mqc, cblk->data);
735 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
737 int i, *src[3], i0, i1, i2, csize = 1;
739 for (i = 0; i < 3; i++)
740 src[i] = tile->comp[i].data;
742 for (i = 0; i < 2; i++)
743 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
745 if (tile->codsty[0].transform == FF_DWT97){
746 for (i = 0; i < csize; i++){
747 i0 = *src[0] + (*src[2] * 46802 >> 16);
748 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
749 i2 = *src[0] + (116130 * *src[1] >> 16);
755 for (i = 0; i < csize; i++){
756 i1 = *src[0] - (*src[2] + *src[1] >> 2);
766 static int decode_tile(J2kDecoderContext *s, J2kTile *tile)
768 int compno, reslevelno, bandno;
773 for (compno = 0; compno < s->ncomponents; compno++){
774 J2kComponent *comp = tile->comp + compno;
775 J2kCodingStyle *codsty = tile->codsty + compno;
777 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
778 J2kResLevel *rlevel = comp->reslevel + reslevelno;
779 for (bandno = 0; bandno < rlevel->nbands; bandno++){
780 J2kBand *band = rlevel->band + bandno;
781 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
783 bandpos = bandno + (reslevelno > 0);
785 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
787 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
788 band->coord[1][1]) - band->coord[1][0] + yy0;
790 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
793 for (cblky = 0; cblky < band->cblkny; cblky++){
794 if (reslevelno == 0 || bandno == 1)
797 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
799 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
800 band->coord[0][1]) - band->coord[0][0] + xx0;
802 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
804 decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
805 if (codsty->transform == FF_DWT53){
806 for (y = yy0; y < yy1; y+=s->cdy[compno]){
807 int *ptr = t1.data[y-yy0];
808 for (x = xx0; x < xx1; x+=s->cdx[compno]){
809 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
813 for (y = yy0; y < yy1; y+=s->cdy[compno]){
814 int *ptr = t1.data[y-yy0];
815 for (x = xx0; x < xx1; x+=s->cdx[compno]){
816 int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
817 tmp2 = FFABS(tmp>>1) + FFABS(tmp&1);
818 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
823 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
826 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
830 ff_j2k_dwt_decode(&comp->dwt, comp->data);
831 src[compno] = comp->data;
833 if (tile->codsty[0].mct)
836 if (s->avctx->pix_fmt == PIX_FMT_BGRA) // RGBA -> BGRA
837 FFSWAP(int *, src[0], src[2]);
839 if (s->precision <= 8) {
840 for (compno = 0; compno < s->ncomponents; compno++){
841 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
842 line = s->picture.data[0] + y * s->picture.linesize[0];
843 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]){
846 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
847 dst = line + x * s->ncomponents + compno;
849 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
850 *src[compno] += 1 << (s->cbps[compno]-1);
851 if (*src[compno] < 0)
853 else if (*src[compno] >= (1 << s->cbps[compno]))
854 *src[compno] = (1 << s->cbps[compno]) - 1;
855 *dst = *src[compno]++;
856 dst += s->ncomponents;
858 line += s->picture.linesize[0];
862 for (compno = 0; compno < s->ncomponents; compno++) {
863 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
864 line = s->picture.data[0] + y * s->picture.linesize[0];
865 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
867 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
868 dst = line + (x * s->ncomponents + compno) * 2;
869 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
871 val = *src[compno]++ << (16 - s->cbps[compno]);
873 val = av_clip(val, 0, (1 << 16) - 1);
875 dst += s->ncomponents;
877 line += s->picture.linesize[0];
884 static void cleanup(J2kDecoderContext *s)
887 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
888 for (compno = 0; compno < s->ncomponents; compno++){
889 J2kComponent *comp = s->tile[tileno].comp + compno;
890 J2kCodingStyle *codsty = s->tile[tileno].codsty + compno;
892 ff_j2k_cleanup(comp, codsty);
894 av_freep(&s->tile[tileno].comp);
899 static int decode_codestream(J2kDecoderContext *s)
901 J2kCodingStyle *codsty = s->codsty;
902 J2kQuantStyle *qntsty = s->qntsty;
903 uint8_t *properties = s->properties;
906 int marker, len, ret = 0;
908 if (s->buf_end - s->buf < 2){
909 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
913 marker = bytestream_get_be16(&s->buf);
916 if (marker == J2K_SOD){
917 J2kTile *tile = s->tile + s->curtileno;
918 if (ret = init_tile(s, s->curtileno))
920 if (ret = decode_packets(s, tile))
924 if (marker == J2K_EOC)
927 if (s->buf_end - s->buf < 2)
928 return AVERROR(EINVAL);
929 len = bytestream_get_be16(&s->buf);
932 ret = get_siz(s); break;
934 ret = get_coc(s, codsty, properties); break;
936 ret = get_cod(s, codsty, properties); break;
938 ret = get_qcc(s, len, qntsty, properties); break;
940 ret = get_qcd(s, len, qntsty, properties); break;
942 if (!(ret = get_sot(s))){
943 codsty = s->tile[s->curtileno].codsty;
944 qntsty = s->tile[s->curtileno].qntsty;
945 properties = s->tile[s->curtileno].properties;
949 // the comment is ignored
950 s->buf += len - 2; break;
952 av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, s->buf - s->buf_start - 4);
953 s->buf += len - 2; break;
955 if (s->buf - oldbuf != len || ret){
956 av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
957 return ret ? ret : -1;
963 static int jp2_find_codestream(J2kDecoderContext *s)
966 int found_codestream = 0, search_range = 10;
968 // skip jpeg2k signature atom
971 while(!found_codestream && search_range) {
972 atom_size = AV_RB32(s->buf);
973 if(AV_RB32(s->buf + 4) == JP2_CODESTREAM) {
974 found_codestream = 1;
987 static int decode_frame(AVCodecContext *avctx,
988 void *data, int *data_size,
991 J2kDecoderContext *s = avctx->priv_data;
992 AVFrame *picture = data;
996 av_log(s->avctx, AV_LOG_DEBUG, "start\n");
999 s->buf = s->buf_start = avpkt->data;
1000 s->buf_end = s->buf_start + avpkt->size;
1003 ff_j2k_init_tier1_luts();
1005 if (s->buf_end - s->buf < 2)
1006 return AVERROR(EINVAL);
1008 // check if the image is in jp2 format
1009 if((AV_RB32(s->buf) == 12) && (AV_RB32(s->buf + 4) == JP2_SIG_TYPE) &&
1010 (AV_RB32(s->buf + 8) == JP2_SIG_VALUE)) {
1011 if(!jp2_find_codestream(s)) {
1012 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1017 if (bytestream_get_be16(&s->buf) != J2K_SOC){
1018 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1021 if (ret = decode_codestream(s))
1024 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1025 if (ret = decode_tile(s, s->tile + tileno))
1029 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1031 *data_size = sizeof(AVPicture);
1032 *picture = s->picture;
1034 return s->buf - s->buf_start;
1037 static av_cold int j2kdec_init(AVCodecContext *avctx)
1039 J2kDecoderContext *s = avctx->priv_data;
1041 avcodec_get_frame_defaults((AVFrame*)&s->picture);
1042 avctx->coded_frame = (AVFrame*)&s->picture;
1046 static av_cold int decode_end(AVCodecContext *avctx)
1048 J2kDecoderContext *s = avctx->priv_data;
1050 if (s->picture.data[0])
1051 avctx->release_buffer(avctx, &s->picture);
1056 AVCodec ff_jpeg2000_decoder = {
1060 sizeof(J2kDecoderContext),
1065 .capabilities = CODEC_CAP_EXPERIMENTAL,
1067 (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_RGB24, -1}