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"
34 #include "libavutil/common.h"
36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
45 uint8_t properties[4];
46 J2kCodingStyle codsty[4];
47 J2kQuantStyle qntsty[4];
51 AVCodecContext *avctx;
55 int width, height; ///< image width and height
56 int image_offset_x, image_offset_y;
57 int tile_offset_x, tile_offset_y;
58 uint8_t cbps[4]; ///< bits per sample in particular components
59 uint8_t sgnd[4]; ///< if a component is signed
60 uint8_t properties[4];
64 int tile_width, tile_height; ///< tile size
65 int numXtiles, numYtiles;
68 J2kCodingStyle codsty[4];
69 J2kQuantStyle qntsty[4];
78 static int get_bits(J2kDecoderContext *s, int n)
84 if (s->bit_index == 0) {
85 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
88 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
93 static void j2k_flush(J2kDecoderContext *s)
95 if (bytestream2_get_byte(&s->g) == 0xff)
96 bytestream2_skip(&s->g, 1);
100 void printcomp(J2kComponent *comp)
103 for (i = 0; i < comp->y1 - comp->y0; i++)
104 ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
107 static void nspaces(FILE *fd, int n)
109 while(n--) putc(' ', fd);
112 static void dump(J2kDecoderContext *s, FILE *fd)
114 int tileno, compno, reslevelno, bandno, precno;
115 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
116 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
118 s->width, s->height, s->tile_width, s->tile_height,
119 s->numXtiles, s->numYtiles, s->ncomponents);
120 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
121 J2kTile *tile = s->tile + tileno;
123 fprintf(fd, "tile %d:\n", tileno);
124 for(compno = 0; compno < s->ncomponents; compno++){
125 J2kComponent *comp = tile->comp + compno;
127 fprintf(fd, "component %d:\n", compno);
129 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
130 comp->x0, comp->x1, comp->y0, comp->y1);
131 for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
132 J2kResLevel *reslevel = comp->reslevel + reslevelno;
134 fprintf(fd, "reslevel %d:\n", reslevelno);
136 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
137 reslevel->x0, reslevel->x1, reslevel->y0,
138 reslevel->y1, reslevel->nbands);
139 for(bandno = 0; bandno < reslevel->nbands; bandno++){
140 J2kBand *band = reslevel->band + bandno;
142 fprintf(fd, "band %d:\n", bandno);
144 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
145 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
148 band->codeblock_width, band->codeblock_height,
149 band->cblknx, band->cblkny);
150 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
151 J2kPrec *prec = band->prec + precno;
153 fprintf(fd, "prec %d:\n", precno);
155 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
156 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
165 /** decode the value stored in node */
166 static int tag_tree_decode(J2kDecoderContext *s, J2kTgtNode *node, int threshold)
168 J2kTgtNode *stack[30];
169 int sp = -1, curval = 0;
172 return AVERROR(EINVAL);
174 while(node && !node->vis){
182 curval = stack[sp]->val;
184 while(curval < threshold && sp >= 0){
185 if (curval < stack[sp]->val)
186 curval = stack[sp]->val;
187 while (curval < threshold){
189 if ((ret = get_bits(s, 1)) > 0){
197 stack[sp]->val = curval;
203 /* marker segments */
204 /** get sizes and offsets of image, tiles; number of components */
205 static int get_siz(J2kDecoderContext *s)
209 if (bytestream2_get_bytes_left(&s->g) < 36)
210 return AVERROR(EINVAL);
212 bytestream2_get_be16u(&s->g); // Rsiz (skipped)
213 s->width = bytestream2_get_be32u(&s->g); // width
214 s->height = bytestream2_get_be32u(&s->g); // height
215 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
216 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
218 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
219 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
220 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
221 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
222 s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
224 if(s->ncomponents <= 0 || s->ncomponents > 4) {
225 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
226 return AVERROR(EINVAL);
228 if(s->tile_width<=0 || s->tile_height<=0)
229 return AVERROR(EINVAL);
231 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
232 return AVERROR(EINVAL);
234 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
235 uint8_t x = bytestream2_get_byteu(&s->g);
236 s->cbps[i] = (x & 0x7f) + 1;
237 s->precision = FFMAX(s->cbps[i], s->precision);
238 s->sgnd[i] = !!(x & 0x80);
239 s->cdx[i] = bytestream2_get_byteu(&s->g);
240 s->cdy[i] = bytestream2_get_byteu(&s->g);
243 s->numXtiles = ff_j2k_ceildiv(s->width - s->tile_offset_x, s->tile_width);
244 s->numYtiles = ff_j2k_ceildiv(s->height - s->tile_offset_y, s->tile_height);
246 if(s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(J2kTile))
247 return AVERROR(EINVAL);
249 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(J2kTile));
251 return AVERROR(ENOMEM);
253 for (i = 0; i < s->numXtiles * s->numYtiles; i++){
254 J2kTile *tile = s->tile + i;
256 tile->comp = av_mallocz(s->ncomponents * sizeof(J2kComponent));
258 return AVERROR(ENOMEM);
261 s->avctx->width = s->width - s->image_offset_x;
262 s->avctx->height = s->height - s->image_offset_y;
264 switch(s->ncomponents){
266 if (s->precision > 8) {
267 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
269 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
273 if (s->precision > 8) {
274 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
276 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
280 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
284 if (s->picture.data[0])
285 s->avctx->release_buffer(s->avctx, &s->picture);
287 if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0)
290 s->picture.pict_type = AV_PICTURE_TYPE_I;
291 s->picture.key_frame = 1;
296 /** get common part for COD and COC segments */
297 static int get_cox(J2kDecoderContext *s, J2kCodingStyle *c)
299 if (bytestream2_get_bytes_left(&s->g) < 5)
300 return AVERROR(EINVAL);
301 c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
302 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
303 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
305 c->cblk_style = bytestream2_get_byteu(&s->g);
306 if (c->cblk_style != 0){ // cblk style
307 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
309 c->transform = bytestream2_get_byteu(&s->g); // transformation
310 if (c->csty & J2K_CSTY_PREC) {
313 for (i = 0; i < c->nreslevels; i++)
314 bytestream2_get_byte(&s->g);
319 /** get coding parameters for a particular tile or whole image*/
320 static int get_cod(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
325 if (bytestream2_get_bytes_left(&s->g) < 5)
326 return AVERROR(EINVAL);
328 tmp.log2_prec_width =
329 tmp.log2_prec_height = 15;
331 tmp.csty = bytestream2_get_byteu(&s->g);
333 if (bytestream2_get_byteu(&s->g)){ // progression level
334 av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
338 tmp.nlayers = bytestream2_get_be16u(&s->g);
339 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
342 for (compno = 0; compno < s->ncomponents; compno++){
343 if (!(properties[compno] & HAD_COC))
344 memcpy(c + compno, &tmp, sizeof(J2kCodingStyle));
349 /** get coding parameters for a component in the whole image on a particular tile */
350 static int get_coc(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
354 if (bytestream2_get_bytes_left(&s->g) < 2)
355 return AVERROR(EINVAL);
357 compno = bytestream2_get_byteu(&s->g);
360 c->csty = bytestream2_get_byte(&s->g);
363 properties[compno] |= HAD_COC;
367 /** get common part for QCD and QCC segments */
368 static int get_qcx(J2kDecoderContext *s, int n, J2kQuantStyle *q)
372 if (bytestream2_get_bytes_left(&s->g) < 1)
373 return AVERROR(EINVAL);
375 x = bytestream2_get_byteu(&s->g); // Sqcd
377 q->nguardbits = x >> 5;
378 q->quantsty = x & 0x1f;
380 if (q->quantsty == J2K_QSTY_NONE){
382 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
383 return AVERROR(EINVAL);
384 for (i = 0; i < n; i++)
385 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
386 } else if (q->quantsty == J2K_QSTY_SI){
387 if (bytestream2_get_bytes_left(&s->g) < 2)
388 return AVERROR(EINVAL);
389 x = bytestream2_get_be16u(&s->g);
390 q->expn[0] = x >> 11;
391 q->mant[0] = x & 0x7ff;
392 for (i = 1; i < 32 * 3; i++){
393 int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
394 q->expn[i] = curexpn;
395 q->mant[i] = q->mant[0];
399 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
400 return AVERROR(EINVAL);
401 for (i = 0; i < n; i++){
402 x = bytestream2_get_be16u(&s->g);
403 q->expn[i] = x >> 11;
404 q->mant[i] = x & 0x7ff;
410 /** get quantization parameters for a particular tile or a whole image */
411 static int get_qcd(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
416 if (get_qcx(s, n, &tmp))
418 for (compno = 0; compno < s->ncomponents; compno++)
419 if (!(properties[compno] & HAD_QCC))
420 memcpy(q + compno, &tmp, sizeof(J2kQuantStyle));
424 /** get quantization parameters for a component in the whole image on in a particular tile */
425 static int get_qcc(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
429 if (bytestream2_get_bytes_left(&s->g) < 1)
430 return AVERROR(EINVAL);
432 compno = bytestream2_get_byteu(&s->g);
433 properties[compno] |= HAD_QCC;
434 return get_qcx(s, n-1, q+compno);
437 /** get start of tile segment */
438 static uint8_t get_sot(J2kDecoderContext *s)
440 if (bytestream2_get_bytes_left(&s->g) < 8)
441 return AVERROR(EINVAL);
443 s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
444 if((unsigned)s->curtileno >= s->numXtiles * s->numYtiles){
446 return AVERROR(EINVAL);
449 bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
451 if (!bytestream2_get_byteu(&s->g)){ ///< TPsot
452 J2kTile *tile = s->tile + s->curtileno;
455 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(J2kCodingStyle));
456 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(J2kQuantStyle));
458 bytestream2_get_byteu(&s->g); ///< TNsot
463 static int init_tile(J2kDecoderContext *s, int tileno)
466 tilex = tileno % s->numXtiles,
467 tiley = tileno / s->numXtiles;
468 J2kTile *tile = s->tile + tileno;
471 return AVERROR(ENOMEM);
472 for (compno = 0; compno < s->ncomponents; compno++){
473 J2kComponent *comp = tile->comp + compno;
474 J2kCodingStyle *codsty = tile->codsty + compno;
475 J2kQuantStyle *qntsty = tile->qntsty + compno;
476 int ret; // global bandno
478 comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
479 comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
480 comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
481 comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
483 if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
489 /** read the number of coding passes */
490 static int getnpasses(J2kDecoderContext *s)
497 if ((num = get_bits(s, 2)) != 3)
498 return num < 0 ? num : 3 + num;
499 if ((num = get_bits(s, 5)) != 31)
500 return num < 0 ? num : 6 + num;
501 num = get_bits(s, 7);
502 return num < 0 ? num : 37 + num;
505 static int getlblockinc(J2kDecoderContext *s)
508 while (ret = get_bits(s, 1)){
516 static int decode_packet(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kResLevel *rlevel, int precno,
517 int layno, uint8_t *expn, int numgbits)
519 int bandno, cblkny, cblknx, cblkno, ret;
521 if (!(ret = get_bits(s, 1))){
527 for (bandno = 0; bandno < rlevel->nbands; bandno++){
528 J2kBand *band = rlevel->band + bandno;
529 J2kPrec *prec = band->prec + precno;
532 if (band->coord[0][0] == band->coord[0][1]
533 || band->coord[1][0] == band->coord[1][1])
536 for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
537 for(cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
538 J2kCblk *cblk = band->cblk + cblkno;
539 int incl, newpasses, llen;
542 incl = get_bits(s, 1);
544 incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
551 cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
552 if ((newpasses = getnpasses(s)) < 0)
554 if ((llen = getlblockinc(s)) < 0)
556 cblk->lblock += llen;
557 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
559 cblk->lengthinc = ret;
560 cblk->npasses += newpasses;
565 if (codsty->csty & J2K_CSTY_EPH) {
566 if (bytestream2_peek_be16(&s->g) == J2K_EPH) {
567 bytestream2_skip(&s->g, 2);
569 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
573 for (bandno = 0; bandno < rlevel->nbands; bandno++){
574 J2kBand *band = rlevel->band + bandno;
575 int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
576 for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++){
578 for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++){
579 J2kCblk *cblk = band->cblk + yi * cblknw + xi;
580 if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
581 return AVERROR(EINVAL);
582 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
583 cblk->length += cblk->lengthinc;
591 static int decode_packets(J2kDecoderContext *s, J2kTile *tile)
593 int layno, reslevelno, compno, precno, ok_reslevel;
595 for (layno = 0; layno < tile->codsty[0].nlayers; layno++){
597 for (reslevelno = 0; ok_reslevel; reslevelno++){
599 for (compno = 0; compno < s->ncomponents; compno++){
600 J2kCodingStyle *codsty = tile->codsty + compno;
601 J2kQuantStyle *qntsty = tile->qntsty + compno;
602 if (reslevelno < codsty->nreslevels){
603 J2kResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
605 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
606 if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
607 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
617 /* TIER-1 routines */
618 static void decode_sigpass(J2kT1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
619 int vert_causal_ctx_csty_symbol)
621 int mask = 3 << (bpno - 1), y0, x, y;
623 for (y0 = 0; y0 < height; y0 += 4)
624 for (x = 0; x < width; x++)
625 for (y = y0; y < height && y < y0+4; y++){
626 if ((t1->flags[y+1][x+1] & J2K_T1_SIG_NB)
627 && !(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
628 int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 && y == 3);
629 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno,
630 vert_causal_ctx_csty_loc_symbol))){
631 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
632 if (bpass_csty_symbol)
633 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
635 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
638 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
640 t1->flags[y+1][x+1] |= J2K_T1_VIS;
645 static void decode_refpass(J2kT1Context *t1, int width, int height, int bpno)
650 phalf = 1 << (bpno - 1);
653 for (y0 = 0; y0 < height; y0 += 4)
654 for (x = 0; x < width; x++)
655 for (y = y0; y < height && y < y0+4; y++){
656 if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
657 int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
658 int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
659 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
660 t1->flags[y+1][x+1] |= J2K_T1_REF;
665 static void decode_clnpass(J2kDecoderContext *s, J2kT1Context *t1, int width, int height,
666 int bpno, int bandno, int seg_symbols)
668 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
670 for (y0 = 0; y0 < height; y0 += 4) {
671 for (x = 0; x < width; x++){
672 if (y0 + 3 < height && !(
673 (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
674 (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
675 (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
676 (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)))){
677 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
679 runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
680 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
687 for (y = y0 + runlen; y < y0 + 4 && y < height; y++){
689 if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)))
690 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1],
694 int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
695 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
696 ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
699 t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
705 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
706 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
707 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
708 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
710 av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
715 static int decode_cblk(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kT1Context *t1, J2kCblk *cblk,
716 int width, int height, int bandpos)
718 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
719 int bpass_csty_symbol = J2K_CBLK_BYPASS & codsty->cblk_style;
720 int vert_causal_ctx_csty_symbol = J2K_CBLK_VSC & codsty->cblk_style;
722 for (y = 0; y < height+2; y++)
723 memset(t1->flags[y], 0, (width+2)*sizeof(int));
725 for (y = 0; y < height; y++)
726 memset(t1->data[y], 0, width*sizeof(int));
728 cblk->data[cblk->length] = 0xff;
729 cblk->data[cblk->length+1] = 0xff;
730 ff_mqc_initdec(&t1->mqc, cblk->data);
734 case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
735 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
737 case 1: decode_refpass(t1, width, height, bpno+1);
738 if (bpass_csty_symbol && clnpass_cnt >= 4)
739 ff_mqc_initdec(&t1->mqc, cblk->data);
741 case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
742 codsty->cblk_style & J2K_CBLK_SEGSYM);
743 clnpass_cnt = clnpass_cnt + 1;
744 if (bpass_csty_symbol && clnpass_cnt >= 4)
745 ff_mqc_initdec(&t1->mqc, cblk->data);
758 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
760 int i, *src[3], i0, i1, i2, csize = 1;
762 for (i = 0; i < 3; i++)
763 src[i] = tile->comp[i].data;
765 for (i = 0; i < 2; i++)
766 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
768 if (tile->codsty[0].transform == FF_DWT97){
769 for (i = 0; i < csize; i++){
770 i0 = *src[0] + (*src[2] * 46802 >> 16);
771 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
772 i2 = *src[0] + (116130 * *src[1] >> 16);
778 for (i = 0; i < csize; i++){
779 i1 = *src[0] - (*src[2] + *src[1] >> 2);
789 static int decode_tile(J2kDecoderContext *s, J2kTile *tile)
791 int compno, reslevelno, bandno;
796 for (compno = 0; compno < s->ncomponents; compno++){
797 J2kComponent *comp = tile->comp + compno;
798 J2kCodingStyle *codsty = tile->codsty + compno;
800 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
801 J2kResLevel *rlevel = comp->reslevel + reslevelno;
802 for (bandno = 0; bandno < rlevel->nbands; bandno++){
803 J2kBand *band = rlevel->band + bandno;
804 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
806 bandpos = bandno + (reslevelno > 0);
808 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
810 yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
811 band->coord[1][1]) - band->coord[1][0] + yy0;
813 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
816 for (cblky = 0; cblky < band->cblkny; cblky++){
817 if (reslevelno == 0 || bandno == 1)
820 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
822 xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
823 band->coord[0][1]) - band->coord[0][0] + xx0;
825 for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
827 decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
828 if (codsty->transform == FF_DWT53){
829 for (y = yy0; y < yy1; y+=s->cdy[compno]){
830 int *ptr = t1.data[y-yy0];
831 for (x = xx0; x < xx1; x+=s->cdx[compno]){
832 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
836 for (y = yy0; y < yy1; y+=s->cdy[compno]){
837 int *ptr = t1.data[y-yy0];
838 for (x = xx0; x < xx1; x+=s->cdx[compno]){
839 int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
840 tmp2 = FFABS(tmp>>1) + (tmp&1);
841 comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
846 xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
849 yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
853 ff_j2k_dwt_decode(&comp->dwt, comp->data);
854 src[compno] = comp->data;
856 if (tile->codsty[0].mct)
859 if (s->precision <= 8) {
860 for (compno = 0; compno < s->ncomponents; compno++){
861 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
862 line = s->picture.data[0] + y * s->picture.linesize[0];
863 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]){
866 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
867 dst = line + x * s->ncomponents + compno;
869 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
870 *src[compno] += 1 << (s->cbps[compno]-1);
871 if (*src[compno] < 0)
873 else if (*src[compno] >= (1 << s->cbps[compno]))
874 *src[compno] = (1 << s->cbps[compno]) - 1;
875 *dst = *src[compno]++;
876 dst += s->ncomponents;
878 line += s->picture.linesize[0];
882 for (compno = 0; compno < s->ncomponents; compno++) {
883 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
884 line = s->picture.data[0] + y * s->picture.linesize[0];
885 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
888 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
889 dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
890 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
893 val = *src[compno]++ << (16 - s->cbps[compno]);
895 val = av_clip(val, 0, (1 << 16) - 1);
897 dst += s->ncomponents;
899 line += s->picture.linesize[0];
906 static void cleanup(J2kDecoderContext *s)
909 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
910 for (compno = 0; compno < s->ncomponents; compno++){
911 J2kComponent *comp = s->tile[tileno].comp + compno;
912 J2kCodingStyle *codsty = s->tile[tileno].codsty + compno;
914 ff_j2k_cleanup(comp, codsty);
916 av_freep(&s->tile[tileno].comp);
921 static int decode_codestream(J2kDecoderContext *s)
923 J2kCodingStyle *codsty = s->codsty;
924 J2kQuantStyle *qntsty = s->qntsty;
925 uint8_t *properties = s->properties;
928 int oldpos, marker, len, ret = 0;
930 if (bytestream2_get_bytes_left(&s->g) < 2){
931 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
935 marker = bytestream2_get_be16u(&s->g);
936 av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
937 oldpos = bytestream2_tell(&s->g);
939 if (marker == J2K_SOD){
940 J2kTile *tile = s->tile + s->curtileno;
941 if (ret = init_tile(s, s->curtileno)) {
942 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
945 if (ret = decode_packets(s, tile)) {
946 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
951 if (marker == J2K_EOC)
954 if (bytestream2_get_bytes_left(&s->g) < 2)
955 return AVERROR(EINVAL);
956 len = bytestream2_get_be16u(&s->g);
962 ret = get_coc(s, codsty, properties);
965 ret = get_cod(s, codsty, properties);
968 ret = get_qcc(s, len, qntsty, properties);
971 ret = get_qcd(s, len, qntsty, properties);
974 if (!(ret = get_sot(s))){
975 codsty = s->tile[s->curtileno].codsty;
976 qntsty = s->tile[s->curtileno].qntsty;
977 properties = s->tile[s->curtileno].properties;
981 // the comment is ignored
982 bytestream2_skip(&s->g, len - 2);
985 av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
986 bytestream2_skip(&s->g, len - 2);
989 if (bytestream2_tell(&s->g) - oldpos != len || ret){
990 av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
991 return ret ? ret : -1;
997 static int jp2_find_codestream(J2kDecoderContext *s)
999 uint32_t atom_size, atom;
1000 int found_codestream = 0, search_range = 10;
1002 while(!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1003 atom_size = bytestream2_get_be32u(&s->g);
1004 atom = bytestream2_get_be32u(&s->g);
1005 if (atom == JP2_CODESTREAM) {
1006 found_codestream = 1;
1008 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1010 bytestream2_skipu(&s->g, atom_size - 8);
1015 if (found_codestream)
1020 static int decode_frame(AVCodecContext *avctx,
1021 void *data, int *got_frame,
1024 J2kDecoderContext *s = avctx->priv_data;
1025 AVFrame *picture = data;
1028 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1031 if (bytestream2_get_bytes_left(&s->g) < 2) {
1032 ret = AVERROR(EINVAL);
1036 // check if the image is in jp2 format
1037 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1038 (bytestream2_get_be32u(&s->g) == 12) &&
1039 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1040 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1041 if(!jp2_find_codestream(s)) {
1042 av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1047 bytestream2_seek(&s->g, 0, SEEK_SET);
1050 if (bytestream2_get_be16u(&s->g) != J2K_SOC){
1051 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1055 if (ret = decode_codestream(s))
1058 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1059 if (ret = decode_tile(s, s->tile + tileno))
1065 *picture = s->picture;
1067 return bytestream2_tell(&s->g);
1074 static av_cold int j2kdec_init(AVCodecContext *avctx)
1076 J2kDecoderContext *s = avctx->priv_data;
1079 avcodec_get_frame_defaults((AVFrame*)&s->picture);
1080 avctx->coded_frame = (AVFrame*)&s->picture;
1082 ff_j2k_init_tier1_luts();
1087 static av_cold int decode_end(AVCodecContext *avctx)
1089 J2kDecoderContext *s = avctx->priv_data;
1091 if (s->picture.data[0])
1092 avctx->release_buffer(avctx, &s->picture);
1097 AVCodec ff_jpeg2000_decoder = {
1099 .type = AVMEDIA_TYPE_VIDEO,
1100 .id = AV_CODEC_ID_JPEG2000,
1101 .priv_data_size = sizeof(J2kDecoderContext),
1102 .init = j2kdec_init,
1103 .close = decode_end,
1104 .decode = decode_frame,
1105 .capabilities = CODEC_CAP_EXPERIMENTAL,
1106 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),