| 1 | /* |
| 2 | * JPEG 2000 encoder and decoder common functions |
| 3 | * Copyright (c) 2007 Kamil Nowosad |
| 4 | * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com> |
| 5 | * |
| 6 | * This file is part of FFmpeg. |
| 7 | * |
| 8 | * FFmpeg is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * FFmpeg is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with FFmpeg; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * @file |
| 25 | * JPEG 2000 image encoder and decoder common functions |
| 26 | */ |
| 27 | |
| 28 | #include "libavutil/attributes.h" |
| 29 | #include "libavutil/avassert.h" |
| 30 | #include "libavutil/common.h" |
| 31 | #include "libavutil/mem.h" |
| 32 | #include "avcodec.h" |
| 33 | #include "jpeg2000.h" |
| 34 | |
| 35 | #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n)) |
| 36 | |
| 37 | /* tag tree routines */ |
| 38 | |
| 39 | /* allocate the memory for tag tree */ |
| 40 | static int32_t tag_tree_size(uint16_t w, uint16_t h) |
| 41 | { |
| 42 | uint32_t res = 0; |
| 43 | while (w > 1 || h > 1) { |
| 44 | res += w * h; |
| 45 | av_assert0(res + 1 < INT32_MAX); |
| 46 | w = (w + 1) >> 1; |
| 47 | h = (h + 1) >> 1; |
| 48 | } |
| 49 | return (int32_t)(res + 1); |
| 50 | } |
| 51 | |
| 52 | static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h) |
| 53 | { |
| 54 | int pw = w, ph = h; |
| 55 | Jpeg2000TgtNode *res, *t, *t2; |
| 56 | int32_t tt_size; |
| 57 | |
| 58 | tt_size = tag_tree_size(w, h); |
| 59 | |
| 60 | t = res = av_mallocz_array(tt_size, sizeof(*t)); |
| 61 | if (!res) |
| 62 | return NULL; |
| 63 | |
| 64 | while (w > 1 || h > 1) { |
| 65 | int i, j; |
| 66 | pw = w; |
| 67 | ph = h; |
| 68 | |
| 69 | w = (w + 1) >> 1; |
| 70 | h = (h + 1) >> 1; |
| 71 | t2 = t + pw * ph; |
| 72 | |
| 73 | for (i = 0; i < ph; i++) |
| 74 | for (j = 0; j < pw; j++) |
| 75 | t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)]; |
| 76 | |
| 77 | t = t2; |
| 78 | } |
| 79 | t[0].parent = NULL; |
| 80 | return res; |
| 81 | } |
| 82 | |
| 83 | static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h) |
| 84 | { |
| 85 | int i, siz = tag_tree_size(w, h); |
| 86 | |
| 87 | for (i = 0; i < siz; i++) { |
| 88 | t[i].val = 0; |
| 89 | t[i].vis = 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | uint8_t ff_jpeg2000_sigctxno_lut[256][4]; |
| 94 | |
| 95 | static int getsigctxno(int flag, int bandno) |
| 96 | { |
| 97 | int h, v, d; |
| 98 | |
| 99 | h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) + |
| 100 | ((flag & JPEG2000_T1_SIG_W) ? 1 : 0); |
| 101 | v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) + |
| 102 | ((flag & JPEG2000_T1_SIG_S) ? 1 : 0); |
| 103 | d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) + |
| 104 | ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) + |
| 105 | ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) + |
| 106 | ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0); |
| 107 | |
| 108 | if (bandno < 3) { |
| 109 | if (bandno == 1) |
| 110 | FFSWAP(int, h, v); |
| 111 | if (h == 2) return 8; |
| 112 | if (h == 1) { |
| 113 | if (v >= 1) return 7; |
| 114 | if (d >= 1) return 6; |
| 115 | return 5; |
| 116 | } |
| 117 | if (v == 2) return 4; |
| 118 | if (v == 1) return 3; |
| 119 | if (d >= 2) return 2; |
| 120 | if (d == 1) return 1; |
| 121 | } else { |
| 122 | if (d >= 3) return 8; |
| 123 | if (d == 2) { |
| 124 | if (h+v >= 1) return 7; |
| 125 | return 6; |
| 126 | } |
| 127 | if (d == 1) { |
| 128 | if (h+v >= 2) return 5; |
| 129 | if (h+v == 1) return 4; |
| 130 | return 3; |
| 131 | } |
| 132 | if (h+v >= 2) return 2; |
| 133 | if (h+v == 1) return 1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16]; |
| 139 | |
| 140 | static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } }; |
| 141 | static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } }; |
| 142 | static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } }; |
| 143 | |
| 144 | static int getsgnctxno(int flag, uint8_t *xorbit) |
| 145 | { |
| 146 | int vcontrib, hcontrib; |
| 147 | |
| 148 | hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0] |
| 149 | [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1; |
| 150 | vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0] |
| 151 | [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1; |
| 152 | *xorbit = xorbittab[hcontrib][vcontrib]; |
| 153 | |
| 154 | return ctxlbltab[hcontrib][vcontrib]; |
| 155 | } |
| 156 | |
| 157 | void av_cold ff_jpeg2000_init_tier1_luts(void) |
| 158 | { |
| 159 | int i, j; |
| 160 | for (i = 0; i < 256; i++) |
| 161 | for (j = 0; j < 4; j++) |
| 162 | ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j); |
| 163 | for (i = 0; i < 16; i++) |
| 164 | for (j = 0; j < 16; j++) |
| 165 | ff_jpeg2000_sgnctxno_lut[i][j] = |
| 166 | getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]); |
| 167 | } |
| 168 | |
| 169 | void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, |
| 170 | int negative) |
| 171 | { |
| 172 | x++; |
| 173 | y++; |
| 174 | t1->flags[y][x] |= JPEG2000_T1_SIG; |
| 175 | if (negative) { |
| 176 | t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W; |
| 177 | t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E; |
| 178 | t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N; |
| 179 | t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S; |
| 180 | } else { |
| 181 | t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W; |
| 182 | t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E; |
| 183 | t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N; |
| 184 | t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S; |
| 185 | } |
| 186 | t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW; |
| 187 | t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE; |
| 188 | t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW; |
| 189 | t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE; |
| 190 | } |
| 191 | |
| 192 | static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; |
| 193 | |
| 194 | int ff_jpeg2000_init_component(Jpeg2000Component *comp, |
| 195 | Jpeg2000CodingStyle *codsty, |
| 196 | Jpeg2000QuantStyle *qntsty, |
| 197 | int cbps, int dx, int dy, |
| 198 | AVCodecContext *avctx) |
| 199 | { |
| 200 | uint8_t log2_band_prec_width, log2_band_prec_height; |
| 201 | int reslevelno, bandno, gbandno = 0, ret, i, j; |
| 202 | uint32_t csize; |
| 203 | |
| 204 | if (codsty->nreslevels2decode <= 0) { |
| 205 | av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode); |
| 206 | return AVERROR_INVALIDDATA; |
| 207 | } |
| 208 | |
| 209 | if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord, |
| 210 | codsty->nreslevels2decode - 1, |
| 211 | codsty->transform)) |
| 212 | return ret; |
| 213 | // component size comp->coord is uint16_t so ir cannot overflow |
| 214 | csize = (comp->coord[0][1] - comp->coord[0][0]) * |
| 215 | (comp->coord[1][1] - comp->coord[1][0]); |
| 216 | |
| 217 | if (codsty->transform == FF_DWT97) { |
| 218 | comp->i_data = NULL; |
| 219 | comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data)); |
| 220 | if (!comp->f_data) |
| 221 | return AVERROR(ENOMEM); |
| 222 | } else { |
| 223 | comp->f_data = NULL; |
| 224 | comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data)); |
| 225 | if (!comp->i_data) |
| 226 | return AVERROR(ENOMEM); |
| 227 | } |
| 228 | comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel)); |
| 229 | if (!comp->reslevel) |
| 230 | return AVERROR(ENOMEM); |
| 231 | /* LOOP on resolution levels */ |
| 232 | for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) { |
| 233 | int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5 |
| 234 | Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno; |
| 235 | |
| 236 | /* Compute borders for each resolution level. |
| 237 | * Computation of trx_0, trx_1, try_0 and try_1. |
| 238 | * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */ |
| 239 | for (i = 0; i < 2; i++) |
| 240 | for (j = 0; j < 2; j++) |
| 241 | reslevel->coord[i][j] = |
| 242 | ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1); |
| 243 | // update precincts size: 2^n value |
| 244 | reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno]; |
| 245 | reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno]; |
| 246 | |
| 247 | /* Number of bands for each resolution level */ |
| 248 | if (reslevelno == 0) |
| 249 | reslevel->nbands = 1; |
| 250 | else |
| 251 | reslevel->nbands = 3; |
| 252 | |
| 253 | /* Number of precincts which span the tile for resolution level reslevelno |
| 254 | * see B.6 in ISO/IEC 15444-1:2002 eq. B-16 |
| 255 | * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width) |
| 256 | * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width) |
| 257 | * for Dcinema profiles in JPEG 2000 |
| 258 | * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| |
| 259 | * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */ |
| 260 | if (reslevel->coord[0][1] == reslevel->coord[0][0]) |
| 261 | reslevel->num_precincts_x = 0; |
| 262 | else |
| 263 | reslevel->num_precincts_x = |
| 264 | ff_jpeg2000_ceildivpow2(reslevel->coord[0][1], |
| 265 | reslevel->log2_prec_width) - |
| 266 | (reslevel->coord[0][0] >> reslevel->log2_prec_width); |
| 267 | |
| 268 | if (reslevel->coord[1][1] == reslevel->coord[1][0]) |
| 269 | reslevel->num_precincts_y = 0; |
| 270 | else |
| 271 | reslevel->num_precincts_y = |
| 272 | ff_jpeg2000_ceildivpow2(reslevel->coord[1][1], |
| 273 | reslevel->log2_prec_height) - |
| 274 | (reslevel->coord[1][0] >> reslevel->log2_prec_height); |
| 275 | |
| 276 | reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band)); |
| 277 | if (!reslevel->band) |
| 278 | return AVERROR(ENOMEM); |
| 279 | |
| 280 | for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) { |
| 281 | Jpeg2000Band *band = reslevel->band + bandno; |
| 282 | int cblkno, precno; |
| 283 | int nb_precincts; |
| 284 | |
| 285 | /* TODO: Implementation of quantization step not finished, |
| 286 | * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */ |
| 287 | switch (qntsty->quantsty) { |
| 288 | uint8_t gain; |
| 289 | int numbps; |
| 290 | case JPEG2000_QSTY_NONE: |
| 291 | /* TODO: to verify. No quantization in this case */ |
| 292 | band->f_stepsize = 1; |
| 293 | break; |
| 294 | case JPEG2000_QSTY_SI: |
| 295 | /*TODO: Compute formula to implement. */ |
| 296 | numbps = cbps + |
| 297 | lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)]; |
| 298 | band->f_stepsize = SHL(2048 + qntsty->mant[gbandno], |
| 299 | 2 + numbps - qntsty->expn[gbandno]); |
| 300 | break; |
| 301 | case JPEG2000_QSTY_SE: |
| 302 | /* Exponent quantization step. |
| 303 | * Formula: |
| 304 | * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11)) |
| 305 | * R_b = R_I + log2 (gain_b ) |
| 306 | * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */ |
| 307 | /* TODO/WARN: value of log2 (gain_b ) not taken into account |
| 308 | * but it works (compared to OpenJPEG). Why? |
| 309 | * Further investigation needed. */ |
| 310 | gain = cbps; |
| 311 | band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]); |
| 312 | band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0; |
| 313 | break; |
| 314 | default: |
| 315 | band->f_stepsize = 0; |
| 316 | av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n"); |
| 317 | break; |
| 318 | } |
| 319 | /* FIXME: In openjepg code stespize = stepsize * 0.5. Why? |
| 320 | * If not set output of entropic decoder is not correct. */ |
| 321 | if (!av_codec_is_encoder(avctx->codec)) |
| 322 | band->f_stepsize *= 0.5; |
| 323 | |
| 324 | band->i_stepsize = band->f_stepsize * (1 << 15); |
| 325 | |
| 326 | /* computation of tbx_0, tbx_1, tby_0, tby_1 |
| 327 | * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1 |
| 328 | * codeblock width and height is computed for |
| 329 | * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */ |
| 330 | if (reslevelno == 0) { |
| 331 | /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */ |
| 332 | for (i = 0; i < 2; i++) |
| 333 | for (j = 0; j < 2; j++) |
| 334 | band->coord[i][j] = |
| 335 | ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0], |
| 336 | declvl - 1); |
| 337 | log2_band_prec_width = reslevel->log2_prec_width; |
| 338 | log2_band_prec_height = reslevel->log2_prec_height; |
| 339 | /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */ |
| 340 | band->log2_cblk_width = FFMIN(codsty->log2_cblk_width, |
| 341 | reslevel->log2_prec_width); |
| 342 | band->log2_cblk_height = FFMIN(codsty->log2_cblk_height, |
| 343 | reslevel->log2_prec_height); |
| 344 | } else { |
| 345 | /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */ |
| 346 | /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */ |
| 347 | for (i = 0; i < 2; i++) |
| 348 | for (j = 0; j < 2; j++) |
| 349 | /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */ |
| 350 | band->coord[i][j] = |
| 351 | ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] - |
| 352 | (((bandno + 1 >> i) & 1) << declvl - 1), |
| 353 | declvl); |
| 354 | /* TODO: Manage case of 3 band offsets here or |
| 355 | * in coding/decoding function? */ |
| 356 | |
| 357 | /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */ |
| 358 | band->log2_cblk_width = FFMIN(codsty->log2_cblk_width, |
| 359 | reslevel->log2_prec_width - 1); |
| 360 | band->log2_cblk_height = FFMIN(codsty->log2_cblk_height, |
| 361 | reslevel->log2_prec_height - 1); |
| 362 | |
| 363 | log2_band_prec_width = reslevel->log2_prec_width - 1; |
| 364 | log2_band_prec_height = reslevel->log2_prec_height - 1; |
| 365 | } |
| 366 | |
| 367 | for (j = 0; j < 2; j++) |
| 368 | band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx); |
| 369 | for (j = 0; j < 2; j++) |
| 370 | band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy); |
| 371 | |
| 372 | band->prec = av_mallocz_array(reslevel->num_precincts_x * |
| 373 | (uint64_t)reslevel->num_precincts_y, |
| 374 | sizeof(*band->prec)); |
| 375 | if (!band->prec) |
| 376 | return AVERROR(ENOMEM); |
| 377 | |
| 378 | nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y; |
| 379 | |
| 380 | for (precno = 0; precno < nb_precincts; precno++) { |
| 381 | Jpeg2000Prec *prec = band->prec + precno; |
| 382 | |
| 383 | /* TODO: Explain formula for JPEG200 DCINEMA. */ |
| 384 | /* TODO: Verify with previous count of codeblocks per band */ |
| 385 | |
| 386 | /* Compute P_x0 */ |
| 387 | prec->coord[0][0] = (precno % reslevel->num_precincts_x) * |
| 388 | (1 << log2_band_prec_width); |
| 389 | prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]); |
| 390 | |
| 391 | /* Compute P_y0 */ |
| 392 | prec->coord[1][0] = (precno / reslevel->num_precincts_x) * |
| 393 | (1 << log2_band_prec_height); |
| 394 | prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]); |
| 395 | |
| 396 | /* Compute P_x1 */ |
| 397 | prec->coord[0][1] = prec->coord[0][0] + |
| 398 | (1 << log2_band_prec_width); |
| 399 | prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]); |
| 400 | |
| 401 | /* Compute P_y1 */ |
| 402 | prec->coord[1][1] = prec->coord[1][0] + |
| 403 | (1 << log2_band_prec_height); |
| 404 | prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]); |
| 405 | |
| 406 | prec->nb_codeblocks_width = |
| 407 | ff_jpeg2000_ceildivpow2(prec->coord[0][1] - |
| 408 | prec->coord[0][0], |
| 409 | band->log2_cblk_width); |
| 410 | prec->nb_codeblocks_height = |
| 411 | ff_jpeg2000_ceildivpow2(prec->coord[1][1] - |
| 412 | prec->coord[1][0], |
| 413 | band->log2_cblk_height); |
| 414 | |
| 415 | /* Tag trees initialization */ |
| 416 | prec->cblkincl = |
| 417 | ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width, |
| 418 | prec->nb_codeblocks_height); |
| 419 | if (!prec->cblkincl) |
| 420 | return AVERROR(ENOMEM); |
| 421 | |
| 422 | prec->zerobits = |
| 423 | ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width, |
| 424 | prec->nb_codeblocks_height); |
| 425 | if (!prec->zerobits) |
| 426 | return AVERROR(ENOMEM); |
| 427 | |
| 428 | prec->cblk = av_mallocz_array(prec->nb_codeblocks_width * |
| 429 | (uint64_t)prec->nb_codeblocks_height, |
| 430 | sizeof(*prec->cblk)); |
| 431 | if (!prec->cblk) |
| 432 | return AVERROR(ENOMEM); |
| 433 | for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) { |
| 434 | Jpeg2000Cblk *cblk = prec->cblk + cblkno; |
| 435 | uint16_t Cx0, Cy0; |
| 436 | |
| 437 | /* Compute coordinates of codeblocks */ |
| 438 | /* Compute Cx0*/ |
| 439 | Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width; |
| 440 | Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width); |
| 441 | cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]); |
| 442 | |
| 443 | /* Compute Cy0*/ |
| 444 | Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height; |
| 445 | Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height); |
| 446 | cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]); |
| 447 | |
| 448 | /* Compute Cx1 */ |
| 449 | cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width), |
| 450 | prec->coord[0][1]); |
| 451 | |
| 452 | /* Compute Cy1 */ |
| 453 | cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height), |
| 454 | prec->coord[1][1]); |
| 455 | /* Update code-blocks coordinates according sub-band position */ |
| 456 | if ((bandno + !!reslevelno) & 1) { |
| 457 | cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] - |
| 458 | comp->reslevel[reslevelno-1].coord[0][0]; |
| 459 | cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] - |
| 460 | comp->reslevel[reslevelno-1].coord[0][0]; |
| 461 | } |
| 462 | if ((bandno + !!reslevelno) & 2) { |
| 463 | cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] - |
| 464 | comp->reslevel[reslevelno-1].coord[1][0]; |
| 465 | cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] - |
| 466 | comp->reslevel[reslevelno-1].coord[1][0]; |
| 467 | } |
| 468 | |
| 469 | cblk->zero = 0; |
| 470 | cblk->lblock = 3; |
| 471 | cblk->length = 0; |
| 472 | cblk->lengthinc = 0; |
| 473 | cblk->npasses = 0; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty) |
| 482 | { |
| 483 | int reslevelno, bandno, cblkno, precno; |
| 484 | for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) { |
| 485 | Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno; |
| 486 | for (bandno = 0; bandno < rlevel->nbands; bandno++) { |
| 487 | Jpeg2000Band *band = rlevel->band + bandno; |
| 488 | for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) { |
| 489 | Jpeg2000Prec *prec = band->prec + precno; |
| 490 | tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height); |
| 491 | tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height); |
| 492 | for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) { |
| 493 | Jpeg2000Cblk *cblk = prec->cblk + cblkno; |
| 494 | cblk->length = 0; |
| 495 | cblk->lblock = 3; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty) |
| 503 | { |
| 504 | int reslevelno, bandno, precno; |
| 505 | for (reslevelno = 0; |
| 506 | comp->reslevel && reslevelno < codsty->nreslevels; |
| 507 | reslevelno++) { |
| 508 | Jpeg2000ResLevel *reslevel; |
| 509 | |
| 510 | if (!comp->reslevel) |
| 511 | continue; |
| 512 | |
| 513 | reslevel = comp->reslevel + reslevelno; |
| 514 | for (bandno = 0; bandno < reslevel->nbands; bandno++) { |
| 515 | Jpeg2000Band *band; |
| 516 | |
| 517 | if (!reslevel->band) |
| 518 | continue; |
| 519 | |
| 520 | band = reslevel->band + bandno; |
| 521 | for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) { |
| 522 | if (band->prec) { |
| 523 | Jpeg2000Prec *prec = band->prec + precno; |
| 524 | av_freep(&prec->zerobits); |
| 525 | av_freep(&prec->cblkincl); |
| 526 | av_freep(&prec->cblk); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | av_freep(&band->prec); |
| 531 | } |
| 532 | av_freep(&reslevel->band); |
| 533 | } |
| 534 | |
| 535 | ff_dwt_destroy(&comp->dwt); |
| 536 | av_freep(&comp->reslevel); |
| 537 | av_freep(&comp->i_data); |
| 538 | av_freep(&comp->f_data); |
| 539 | } |