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"
33 #include "libavutil/common.h"
35 #define JP2_SIG_TYPE 0x6A502020
36 #define JP2_SIG_VALUE 0x0D0A870A
37 #define JP2_CODESTREAM 0x6A703263
44 uint8_t properties[4];
45 J2kCodingStyle codsty[4];
46 J2kQuantStyle qntsty[4];
50 AVCodecContext *avctx;
54 int width, height; ///< image width and height
55 int image_offset_x, image_offset_y;
56 int tile_offset_x, tile_offset_y;
57 uint8_t cbps[4]; ///< bits per sample in particular components
58 uint8_t sgnd[4]; ///< if a component is signed
59 uint8_t properties[4];
63 int tile_width, tile_height; ///< tile size
64 int numXtiles, numYtiles;
67 J2kCodingStyle codsty[4];
68 J2kQuantStyle qntsty[4];
77 static int get_bits(J2kDecoderContext *s, int n)
83 if (s->bit_index == 0) {
84 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
87 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
92 static void j2k_flush(J2kDecoderContext *s)
94 if (bytestream2_get_byte(&s->g) == 0xff)
95 bytestream2_skip(&s->g, 1);
99 void printcomp(J2kComponent *comp)
102 for (i = 0; i < comp->y1 - comp->y0; i++)
103 ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
106 static void nspaces(FILE *fd, int n)
108 while(n--) putc(' ', fd);
111 static void dump(J2kDecoderContext *s, FILE *fd)
113 int tileno, compno, reslevelno, bandno, precno;
114 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
115 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
117 s->width, s->height, s->tile_width, s->tile_height,
118 s->numXtiles, s->numYtiles, s->ncomponents);
119 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
120 J2kTile *tile = s->tile + tileno;
122 fprintf(fd, "tile %d:\n", tileno);
123 for(compno = 0; compno < s->ncomponents; compno++){
124 J2kComponent *comp = tile->comp + compno;
126 fprintf(fd, "component %d:\n", compno);
128 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
129 comp->x0, comp->x1, comp->y0, comp->y1);
130 for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
131 J2kResLevel *reslevel = comp->reslevel + reslevelno;
133 fprintf(fd, "reslevel %d:\n", reslevelno);
135 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
136 reslevel->x0, reslevel->x1, reslevel->y0,
137 reslevel->y1, reslevel->nbands);
138 for(bandno = 0; bandno < reslevel->nbands; bandno++){
139 J2kBand *band = reslevel->band + bandno;
141 fprintf(fd, "band %d:\n", bandno);
143 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
144 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
147 band->codeblock_width, band->codeblock_height,
148 band->cblknx, band->cblkny);
149 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
150 J2kPrec *prec = band->prec + precno;
152 fprintf(fd, "prec %d:\n", precno);
154 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
155 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
164 /** decode the value stored in node */
165 static int tag_tree_decode(J2kDecoderContext *s, J2kTgtNode *node, int threshold)
167 J2kTgtNode *stack[30];
168 int sp = -1, curval = 0;
170 while(node && !node->vis){
178 curval = stack[sp]->val;
180 while(curval < threshold && sp >= 0){
181 if (curval < stack[sp]->val)
182 curval = stack[sp]->val;
183 while (curval < threshold){
185 if ((ret = get_bits(s, 1)) > 0){
193 stack[sp]->val = curval;
199 /* marker segments */
200 /** get sizes and offsets of image, tiles; number of components */
201 static int get_siz(J2kDecoderContext *s)
205 if (bytestream2_get_bytes_left(&s->g) < 36)
206 return AVERROR(EINVAL);
208 bytestream2_get_be16u(&s->g); // Rsiz (skipped)
209 s->width = bytestream2_get_be32u(&s->g); // width
210 s->height = bytestream2_get_be32u(&s->g); // height
211 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
212 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
214 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
215 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
216 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
217 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
218 s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
220 if(s->tile_width<=0 || s->tile_height<=0)
221 return AVERROR(EINVAL);
223 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
224 return AVERROR(EINVAL);
226 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
227 uint8_t x = bytestream2_get_byteu(&s->g);
228 s->cbps[i] = (x & 0x7f) + 1;
229 s->precision = FFMAX(s->cbps[i], s->precision);
230 s->sgnd[i] = !!(x & 0x80);
231 s->cdx[i] = bytestream2_get_byteu(&s->g);
232 s->cdy[i] = bytestream2_get_byteu(&s->g);
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 if(s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(J2kTile))
239 return AVERROR(EINVAL);
241 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(J2kTile));
243 return AVERROR(ENOMEM);
245 for (i = 0; i < s->numXtiles * s->numYtiles; i++){
246 J2kTile *tile = s->tile + i;
248 tile->comp = av_mallocz(s->ncomponents * sizeof(J2kComponent));
250 return AVERROR(ENOMEM);
253 s->avctx->width = s->width - s->image_offset_x;
254 s->avctx->height = s->height - s->image_offset_y;
256 switch(s->ncomponents){
258 if (s->precision > 8) {
259 s->avctx->pix_fmt = PIX_FMT_GRAY16;
261 s->avctx->pix_fmt = PIX_FMT_GRAY8;
265 if (s->precision > 8) {
266 s->avctx->pix_fmt = PIX_FMT_RGB48;
268 s->avctx->pix_fmt = PIX_FMT_RGB24;
272 s->avctx->pix_fmt = PIX_FMT_RGBA;
276 if (s->picture.data[0])
277 s->avctx->release_buffer(s->avctx, &s->picture);
279 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0)
282 s->picture.pict_type = AV_PICTURE_TYPE_I;
283 s->picture.key_frame = 1;
288 /** get common part for COD and COC segments */
289 static int get_cox(J2kDecoderContext *s, J2kCodingStyle *c)
291 if (bytestream2_get_bytes_left(&s->g) < 5)
292 return AVERROR(EINVAL);
293 c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
294 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
295 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
297 c->cblk_style = bytestream2_get_byteu(&s->g);
298 if (c->cblk_style != 0){ // cblk style
299 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
301 c->transform = bytestream2_get_byteu(&s->g); // transformation
302 if (c->csty & J2K_CSTY_PREC) {
305 for (i = 0; i < c->nreslevels; i++)
306 bytestream2_get_byte(&s->g);
311 /** get coding parameters for a particular tile or whole image*/
312 static int get_cod(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
317 if (bytestream2_get_bytes_left(&s->g) < 5)
318 return AVERROR(EINVAL);
320 tmp.log2_prec_width =
321 tmp.log2_prec_height = 15;
323 tmp.csty = bytestream2_get_byteu(&s->g);
325 if (bytestream2_get_byteu(&s->g)){ // progression level
326 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
330 tmp.nlayers = bytestream2_get_be16u(&s->g);
331 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
334 for (compno = 0; compno < s->ncomponents; compno++){
335 if (!(properties[compno] & HAD_COC))
336 memcpy(c + compno, &tmp, sizeof(J2kCodingStyle));
341 /** get coding parameters for a component in the whole image on a particular tile */
342 static int get_coc(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
346 if (bytestream2_get_bytes_left(&s->g) < 2)
347 return AVERROR(EINVAL);
349 compno = bytestream2_get_byteu(&s->g);
352 c->csty = bytestream2_get_byte(&s->g);
355 properties[compno] |= HAD_COC;
359 /** get common part for QCD and QCC segments */
360 static int get_qcx(J2kDecoderContext *s, int n, J2kQuantStyle *q)
364 if (bytestream2_get_bytes_left(&s->g) < 1)
365 return AVERROR(EINVAL);
367 x = bytestream2_get_byteu(&s->g); // Sqcd
369 q->nguardbits = x >> 5;
370 q->quantsty = x & 0x1f;
372 if (q->quantsty == J2K_QSTY_NONE){
374 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
375 return AVERROR(EINVAL);
376 for (i = 0; i < n; i++)
377 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
378 } else if (q->quantsty == J2K_QSTY_SI){
379 if (bytestream2_get_bytes_left(&s->g) < 2)
380 return AVERROR(EINVAL);
381 x = bytestream2_get_be16u(&s->g);
382 q->expn[0] = x >> 11;
383 q->mant[0] = x & 0x7ff;
384 for (i = 1; i < 32 * 3; i++){
385 int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
386 q->expn[i] = curexpn;
387 q->mant[i] = q->mant[0];
391 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
392 return AVERROR(EINVAL);
393 for (i = 0; i < n; i++){
394 x = bytestream2_get_be16u(&s->g);
395 q->expn[i] = x >> 11;
396 q->mant[i] = x & 0x7ff;
402 /** get quantization parameters for a particular tile or a whole image */
403 static int get_qcd(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
408 if (get_qcx(s, n, &tmp))
410 for (compno = 0; compno < s->ncomponents; compno++)
411 if (!(properties[compno] & HAD_QCC))
412 memcpy(q + compno, &tmp, sizeof(J2kQuantStyle));
416 /** get quantization parameters for a component in the whole image on in a particular tile */
417 static int get_qcc(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
421 if (bytestream2_get_bytes_left(&s->g) < 1)
422 return AVERROR(EINVAL);
424 compno = bytestream2_get_byteu(&s->g);
425 properties[compno] |= HAD_QCC;
426 return get_qcx(s, n-1, q+compno);
429 /** get start of tile segment */
430 static uint8_t get_sot(J2kDecoderContext *s)
432 if (bytestream2_get_bytes_left(&s->g) < 8)
433 return AVERROR(EINVAL);
435 s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
436 if((unsigned)s->curtileno >= s->numXtiles * s->numYtiles){
438 return AVERROR(EINVAL);
441 bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
443 if (!bytestream2_get_byteu(&s->g)){ ///< TPsot
444 J2kTile *tile = s->tile + s->curtileno;
447 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(J2kCodingStyle));
448 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(J2kQuantStyle));
450 bytestream2_get_byteu(&s->g); ///< TNsot
455 static int init_tile(J2kDecoderContext *s, int tileno)
458 tilex = tileno % s->numXtiles,
459 tiley = tileno / s->numXtiles;
460 J2kTile *tile = s->tile + tileno;
463 return AVERROR(ENOMEM);
464 for (compno = 0; compno < s->ncomponents; compno++){
465 J2kComponent *comp = tile->comp + compno;
466 J2kCodingStyle *codsty = tile->codsty + compno;
467 J2kQuantStyle *qntsty = tile->qntsty + compno;
468 int ret; // global bandno
470 comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
471 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
472 comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
473 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
475 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
481 /** read the number of coding passes */
482 static int getnpasses(J2kDecoderContext *s)
489 if ((num = get_bits(s, 2)) != 3)
490 return num < 0 ? num : 3 + num;
491 if ((num = get_bits(s, 5)) != 31)
492 return num < 0 ? num : 6 + num;
493 num = get_bits(s, 7);
494 return num < 0 ? num : 37 + num;
497 static int getlblockinc(J2kDecoderContext *s)
500 while (ret = get_bits(s, 1)){
508 static int decode_packet(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kResLevel *rlevel, int precno,
509 int layno, uint8_t *expn, int numgbits)
511 int bandno, cblkny, cblknx, cblkno, ret;
513 if (!(ret = get_bits(s, 1))){
519 for (bandno = 0; bandno < rlevel->nbands; bandno++){
520 J2kBand *band = rlevel->band + bandno;
521 J2kPrec *prec = band->prec + precno;
524 if (band->coord[0][0] == band->coord[0][1]
525 || band->coord[1][0] == band->coord[1][1])
528 for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
529 for(cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
530 J2kCblk *cblk = band->cblk + cblkno;
531 int incl, newpasses, llen;
534 incl = get_bits(s, 1);
536 incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
543 cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
544 if ((newpasses = getnpasses(s)) < 0)
546 if ((llen = getlblockinc(s)) < 0)
548 cblk->lblock += llen;
549 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
551 cblk->lengthinc = ret;
552 cblk->npasses += newpasses;
557 if (codsty->csty & J2K_CSTY_EPH) {
558 if (bytestream2_peek_be16(&s->g) == J2K_EPH) {
559 bytestream2_skip(&s->g, 2);
561 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
565 for (bandno = 0; bandno < rlevel->nbands; bandno++){
566 J2kBand *band = rlevel->band + bandno;
567 int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
568 for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++){
570 for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++){
571 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
572 if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
573 return AVERROR(EINVAL);
574 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
575 cblk->length += cblk->lengthinc;
583 static int decode_packets(J2kDecoderContext *s, J2kTile *tile)
585 int layno, reslevelno, compno, precno, ok_reslevel;
587 for (layno = 0; layno < tile->codsty[0].nlayers; layno++){
589 for (reslevelno = 0; ok_reslevel; reslevelno++){
591 for (compno = 0; compno < s->ncomponents; compno++){
592 J2kCodingStyle *codsty = tile->codsty + compno;
593 J2kQuantStyle *qntsty = tile->qntsty + compno;
594 if (reslevelno < codsty->nreslevels){
595 J2kResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
597 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
598 if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
599 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
609 /* TIER-1 routines */
610 static void decode_sigpass(J2kT1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
611 int vert_causal_ctx_csty_symbol)
613 int mask = 3 << (bpno - 1), y0, x, y;
615 for (y0 = 0; y0 < height; y0 += 4)
616 for (x = 0; x < width; x++)
617 for (y = y0; y < height && y < y0+4; y++){
618 if ((t1->flags[y+1][x+1] & J2K_T1_SIG_NB)
619 && !(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
620 int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 && y == 3);
621 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno,
622 vert_causal_ctx_csty_loc_symbol))){
623 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
624 if (bpass_csty_symbol)
625 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
627 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
630 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
632 t1->flags[y+1][x+1] |= J2K_T1_VIS;
637 static void decode_refpass(J2kT1Context *t1, int width, int height, int bpno)
642 phalf = 1 << (bpno - 1);
645 for (y0 = 0; y0 < height; y0 += 4)
646 for (x = 0; x < width; x++)
647 for (y = y0; y < height && y < y0+4; y++){
648 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
649 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
650 int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
651 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
652 t1->flags[y+1][x+1] |= J2K_T1_REF;
657 static void decode_clnpass(J2kDecoderContext *s, J2kT1Context *t1, int width, int height,
658 int bpno, int bandno, int seg_symbols)
660 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
662 for (y0 = 0; y0 < height; y0 += 4) {
663 for (x = 0; x < width; x++){
664 if (y0 + 3 < height && !(
665 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
666 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
667 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
668 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)))){
669 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
671 runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
672 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
679 for (y = y0 + runlen; y < y0 + 4 && y < height; y++){
681 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)))
682 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1],
686 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
687 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
688 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
691 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
697 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
698 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
699 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
700 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
702 av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
707 static int decode_cblk(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kT1Context *t1, J2kCblk *cblk,
708 int width, int height, int bandpos)
710 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
711 int bpass_csty_symbol = J2K_CBLK_BYPASS & codsty->cblk_style;
712 int vert_causal_ctx_csty_symbol = J2K_CBLK_VSC & codsty->cblk_style;
714 for (y = 0; y < height+2; y++)
715 memset(t1->flags[y], 0, (width+2)*sizeof(int));
717 for (y = 0; y < height; y++)
718 memset(t1->data[y], 0, width*sizeof(int));
720 cblk->data[cblk->length] = 0xff;
721 cblk->data[cblk->length+1] = 0xff;
722 ff_mqc_initdec(&t1->mqc, cblk->data);
726 case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
727 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
729 case 1: decode_refpass(t1, width, height, bpno+1);
730 if (bpass_csty_symbol && clnpass_cnt >= 4)
731 ff_mqc_initdec(&t1->mqc, cblk->data);
733 case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
734 codsty->cblk_style & J2K_CBLK_SEGSYM);
735 clnpass_cnt = clnpass_cnt + 1;
736 if (bpass_csty_symbol && clnpass_cnt >= 4)
737 ff_mqc_initdec(&t1->mqc, cblk->data);
750 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
752 int i, *src[3], i0, i1, i2, csize = 1;
754 for (i = 0; i < 3; i++)
755 src[i] = tile->comp[i].data;
757 for (i = 0; i < 2; i++)
758 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
760 if (tile->codsty[0].transform == FF_DWT97){
761 for (i = 0; i < csize; i++){
762 i0 = *src[0] + (*src[2] * 46802 >> 16);
763 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
764 i2 = *src[0] + (116130 * *src[1] >> 16);
770 for (i = 0; i < csize; i++){
771 i1 = *src[0] - (*src[2] + *src[1] >> 2);
781 static int decode_tile(J2kDecoderContext *s, J2kTile *tile)
783 int compno, reslevelno, bandno;
788 for (compno = 0; compno < s->ncomponents; compno++){
789 J2kComponent *comp = tile->comp + compno;
790 J2kCodingStyle *codsty = tile->codsty + compno;
792 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
793 J2kResLevel *rlevel = comp->reslevel + reslevelno;
794 for (bandno = 0; bandno < rlevel->nbands; bandno++){
795 J2kBand *band = rlevel->band + bandno;
796 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
798 bandpos = bandno + (reslevelno > 0);
800 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
802 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
803 band->coord[1][1]) - band->coord[1][0] + yy0;
805 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
808 for (cblky = 0; cblky < band->cblkny; cblky++){
809 if (reslevelno == 0 || bandno == 1)
812 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
814 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
815 band->coord[0][1]) - band->coord[0][0] + xx0;
817 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
819 decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
820 if (codsty->transform == FF_DWT53){
821 for (y = yy0; y < yy1; y+=s->cdy[compno]){
822 int *ptr = t1.data[y-yy0];
823 for (x = xx0; x < xx1; x+=s->cdx[compno]){
824 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
828 for (y = yy0; y < yy1; y+=s->cdy[compno]){
829 int *ptr = t1.data[y-yy0];
830 for (x = xx0; x < xx1; x+=s->cdx[compno]){
831 int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
832 tmp2 = FFABS(tmp>>1) + FFABS(tmp&1);
833 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
838 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
841 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
845 ff_j2k_dwt_decode(&comp->dwt, comp->data);
846 src[compno] = comp->data;
848 if (tile->codsty[0].mct)
851 if (s->precision <= 8) {
852 for (compno = 0; compno < s->ncomponents; compno++){
853 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
854 line = s->picture.data[0] + y * s->picture.linesize[0];
855 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]){
858 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
859 dst = line + x * s->ncomponents + compno;
861 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
862 *src[compno] += 1 << (s->cbps[compno]-1);
863 if (*src[compno] < 0)
865 else if (*src[compno] >= (1 << s->cbps[compno]))
866 *src[compno] = (1 << s->cbps[compno]) - 1;
867 *dst = *src[compno]++;
868 dst += s->ncomponents;
870 line += s->picture.linesize[0];
874 for (compno = 0; compno < s->ncomponents; compno++) {
875 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
876 line = s->picture.data[0] + y * s->picture.linesize[0];
877 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
880 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
881 dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
882 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
885 val = *src[compno]++ << (16 - s->cbps[compno]);
887 val = av_clip(val, 0, (1 << 16) - 1);
889 dst += s->ncomponents;
891 line += s->picture.linesize[0];
898 static void cleanup(J2kDecoderContext *s)
901 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
902 for (compno = 0; compno < s->ncomponents; compno++){
903 J2kComponent *comp = s->tile[tileno].comp + compno;
904 J2kCodingStyle *codsty = s->tile[tileno].codsty + compno;
906 ff_j2k_cleanup(comp, codsty);
908 av_freep(&s->tile[tileno].comp);
913 static int decode_codestream(J2kDecoderContext *s)
915 J2kCodingStyle *codsty = s->codsty;
916 J2kQuantStyle *qntsty = s->qntsty;
917 uint8_t *properties = s->properties;
920 int oldpos, marker, len, ret = 0;
922 if (bytestream2_get_bytes_left(&s->g) < 2){
923 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
927 marker = bytestream2_get_be16u(&s->g);
928 av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
929 oldpos = bytestream2_tell(&s->g);
931 if (marker == J2K_SOD){
932 J2kTile *tile = s->tile + s->curtileno;
933 if (ret = init_tile(s, s->curtileno)) {
934 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
937 if (ret = decode_packets(s, tile)) {
938 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
943 if (marker == J2K_EOC)
946 if (bytestream2_get_bytes_left(&s->g) < 2)
947 return AVERROR(EINVAL);
948 len = bytestream2_get_be16u(&s->g);
954 ret = get_coc(s, codsty, properties);
957 ret = get_cod(s, codsty, properties);
960 ret = get_qcc(s, len, qntsty, properties);
963 ret = get_qcd(s, len, qntsty, properties);
966 if (!(ret = get_sot(s))){
967 codsty = s->tile[s->curtileno].codsty;
968 qntsty = s->tile[s->curtileno].qntsty;
969 properties = s->tile[s->curtileno].properties;
973 // the comment is ignored
974 bytestream2_skip(&s->g, len - 2);
977 av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
978 bytestream2_skip(&s->g, len - 2);
981 if (bytestream2_tell(&s->g) - oldpos != len || ret){
982 av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
983 return ret ? ret : -1;
989 static int jp2_find_codestream(J2kDecoderContext *s)
991 uint32_t atom_size, atom;
992 int found_codestream = 0, search_range = 10;
994 while(!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
995 atom_size = bytestream2_get_be32u(&s->g);
996 atom = bytestream2_get_be32u(&s->g);
997 if (atom == JP2_CODESTREAM) {
998 found_codestream = 1;
1000 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1002 bytestream2_skipu(&s->g, atom_size - 8);
1007 if (found_codestream)
1012 static int decode_frame(AVCodecContext *avctx,
1013 void *data, int *data_size,
1016 J2kDecoderContext *s = avctx->priv_data;
1017 AVFrame *picture = data;
1021 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1024 if (bytestream2_get_bytes_left(&s->g) < 2) {
1025 ret = AVERROR(EINVAL);
1029 // check if the image is in jp2 format
1030 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1031 (bytestream2_get_be32u(&s->g) == 12) &&
1032 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1033 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1034 if(!jp2_find_codestream(s)) {
1035 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1040 bytestream2_seek(&s->g, 0, SEEK_SET);
1043 if (bytestream2_get_be16u(&s->g) != J2K_SOC){
1044 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1048 if (ret = decode_codestream(s))
1051 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1052 if (ret = decode_tile(s, s->tile + tileno))
1057 *data_size = sizeof(AVPicture);
1058 *picture = s->picture;
1060 return bytestream2_tell(&s->g);
1067 static av_cold int j2kdec_init(AVCodecContext *avctx)
1069 J2kDecoderContext *s = avctx->priv_data;
1071 avcodec_get_frame_defaults((AVFrame*)&s->picture);
1072 avctx->coded_frame = (AVFrame*)&s->picture;
1074 ff_j2k_init_tier1_luts();
1079 static av_cold int decode_end(AVCodecContext *avctx)
1081 J2kDecoderContext *s = avctx->priv_data;
1083 if (s->picture.data[0])
1084 avctx->release_buffer(avctx, &s->picture);
1089 AVCodec ff_jpeg2000_decoder = {
1091 .type = AVMEDIA_TYPE_VIDEO,
1092 .id = CODEC_ID_JPEG2000,
1093 .priv_data_size = sizeof(J2kDecoderContext),
1094 .init = j2kdec_init,
1095 .close = decode_end,
1096 .decode = decode_frame,
1097 .capabilities = CODEC_CAP_EXPERIMENTAL,
1098 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),