2 * JPEG 2000 encoder and decoder common functions
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
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.
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.
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
25 * JPEG 2000 image encoder and decoder common functions
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/mem.h"
35 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
37 /* tag tree routines */
39 /* allocate the memory for tag tree */
40 static int32_t tag_tree_size(uint16_t w, uint16_t h)
43 while (w > 1 || h > 1) {
45 av_assert0(res + 1 < INT32_MAX);
49 return (int32_t)(res + 1);
52 static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
55 Jpeg2000TgtNode *res, *t, *t2;
58 tt_size = tag_tree_size(w, h);
60 t = res = av_mallocz_array(tt_size, sizeof(*t));
64 while (w > 1 || h > 1) {
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)];
83 static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
85 int i, siz = tag_tree_size(w, h);
87 for (i = 0; i < siz; i++) {
93 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
95 static int getsigctxno(int flag, int bandno)
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);
111 if (h == 2) return 8;
113 if (v >= 1) return 7;
114 if (d >= 1) return 6;
117 if (v == 2) return 4;
118 if (v == 1) return 3;
119 if (d >= 2) return 2;
120 if (d == 1) return 1;
122 if (d >= 3) return 8;
124 if (h+v >= 1) return 7;
128 if (h+v >= 2) return 5;
129 if (h+v == 1) return 4;
132 if (h+v >= 2) return 2;
133 if (h+v == 1) return 1;
138 uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
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 } };
144 static int getsgnctxno(int flag, uint8_t *xorbit)
146 int vcontrib, hcontrib;
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];
154 return ctxlbltab[hcontrib][vcontrib];
157 void av_cold ff_jpeg2000_init_tier1_luts(void)
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]);
169 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
174 t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
176 t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
177 t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
178 t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
179 t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
181 t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
182 t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
183 t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
184 t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
186 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
187 t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
188 t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
189 t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
192 // static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
194 static int init_band(AVCodecContext *avctx,
195 Jpeg2000ResLevel *reslevel,
196 Jpeg2000Component *comp,
197 Jpeg2000CodingStyle *codsty,
198 Jpeg2000QuantStyle *qntsty,
199 int bandno, int gbandno, int reslevelno,
200 int cbps, int dx, int dy)
202 Jpeg2000Band *band = reslevel->band + bandno;
203 uint8_t log2_band_prec_width, log2_band_prec_height;
204 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
209 /* TODO: Implementation of quantization step not finished,
210 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
211 switch (qntsty->quantsty) {
213 case JPEG2000_QSTY_NONE:
214 /* TODO: to verify. No quantization in this case */
215 band->f_stepsize = 1;
217 case JPEG2000_QSTY_SI:
218 /*TODO: Compute formula to implement. */
220 // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
221 // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
222 // 2 + numbps - qntsty->expn[gbandno]);
224 case JPEG2000_QSTY_SE:
225 /* Exponent quantization step.
227 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
228 * R_b = R_I + log2 (gain_b )
229 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
231 band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
232 band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
235 band->f_stepsize = 0;
236 av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
239 if (codsty->transform != FF_DWT53) {
241 switch (bandno + (reslevelno > 0)) {
244 band->f_stepsize *= F_LFTG_X * 2;
248 band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
251 if (codsty->transform == FF_DWT97) {
252 band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
256 band->i_stepsize = band->f_stepsize * (1 << 15);
258 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
259 * If not set output of entropic decoder is not correct. */
260 if (!av_codec_is_encoder(avctx->codec))
261 band->f_stepsize *= 0.5;
263 /* computation of tbx_0, tbx_1, tby_0, tby_1
264 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
265 * codeblock width and height is computed for
266 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
267 if (reslevelno == 0) {
268 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
269 for (i = 0; i < 2; i++)
270 for (j = 0; j < 2; j++)
272 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
274 log2_band_prec_width = reslevel->log2_prec_width;
275 log2_band_prec_height = reslevel->log2_prec_height;
276 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
277 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
278 reslevel->log2_prec_width);
279 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
280 reslevel->log2_prec_height);
282 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
283 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
284 for (i = 0; i < 2; i++)
285 for (j = 0; j < 2; j++)
286 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
288 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
289 (((bandno + 1 >> i) & 1LL) << declvl - 1),
291 /* TODO: Manage case of 3 band offsets here or
292 * in coding/decoding function? */
294 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
295 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
296 reslevel->log2_prec_width - 1);
297 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
298 reslevel->log2_prec_height - 1);
300 log2_band_prec_width = reslevel->log2_prec_width - 1;
301 log2_band_prec_height = reslevel->log2_prec_height - 1;
304 if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
306 return AVERROR(ENOMEM);
308 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
309 band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
311 return AVERROR(ENOMEM);
313 for (precno = 0; precno < nb_precincts; precno++) {
314 Jpeg2000Prec *prec = band->prec + precno;
317 prec->decoded_layers = 0;
319 /* TODO: Explain formula for JPEG200 DCINEMA. */
320 /* TODO: Verify with previous count of codeblocks per band */
323 prec->coord[0][0] = ((band->coord[0][0] >> log2_band_prec_width) + precno % reslevel->num_precincts_x) *
324 (1 << log2_band_prec_width);
327 prec->coord[1][0] = ((band->coord[1][0] >> log2_band_prec_height) + precno / reslevel->num_precincts_x) *
328 (1 << log2_band_prec_height);
331 prec->coord[0][1] = prec->coord[0][0] +
332 (1 << log2_band_prec_width);
333 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
334 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
337 prec->coord[1][1] = prec->coord[1][0] +
338 (1 << log2_band_prec_height);
339 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
340 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
342 prec->nb_codeblocks_width =
343 ff_jpeg2000_ceildivpow2(prec->coord[0][1],
344 band->log2_cblk_width)
345 - (prec->coord[0][0] >> band->log2_cblk_width);
346 prec->nb_codeblocks_height =
347 ff_jpeg2000_ceildivpow2(prec->coord[1][1],
348 band->log2_cblk_height)
349 - (prec->coord[1][0] >> band->log2_cblk_height);
352 /* Tag trees initialization */
354 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
355 prec->nb_codeblocks_height);
357 return AVERROR(ENOMEM);
360 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
361 prec->nb_codeblocks_height);
363 return AVERROR(ENOMEM);
365 if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
367 return AVERROR(ENOMEM);
369 nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
370 prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
372 return AVERROR(ENOMEM);
373 for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
374 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
377 /* Compute coordinates of codeblocks */
379 Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
380 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
381 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
384 Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
385 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
386 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
389 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
393 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
395 /* Update code-blocks coordinates according sub-band position */
396 if ((bandno + !!reslevelno) & 1) {
397 cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
398 comp->reslevel[reslevelno-1].coord[0][0];
399 cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
400 comp->reslevel[reslevelno-1].coord[0][0];
402 if ((bandno + !!reslevelno) & 2) {
403 cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
404 comp->reslevel[reslevelno-1].coord[1][0];
405 cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
406 comp->reslevel[reslevelno-1].coord[1][0];
412 memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
420 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
421 Jpeg2000CodingStyle *codsty,
422 Jpeg2000QuantStyle *qntsty,
423 int cbps, int dx, int dy,
424 AVCodecContext *avctx)
426 int reslevelno, bandno, gbandno = 0, ret, i, j;
429 if (codsty->nreslevels2decode <= 0) {
430 av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
431 return AVERROR_INVALIDDATA;
434 if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
435 codsty->nreslevels2decode - 1,
438 // component size comp->coord is uint16_t so ir cannot overflow
439 csize = (comp->coord[0][1] - comp->coord[0][0]) *
440 (comp->coord[1][1] - comp->coord[1][0]);
442 if (codsty->transform == FF_DWT97) {
443 csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
445 comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
447 return AVERROR(ENOMEM);
449 csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
451 comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
453 return AVERROR(ENOMEM);
455 comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
457 return AVERROR(ENOMEM);
458 /* LOOP on resolution levels */
459 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
460 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
461 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
463 /* Compute borders for each resolution level.
464 * Computation of trx_0, trx_1, try_0 and try_1.
465 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
466 for (i = 0; i < 2; i++)
467 for (j = 0; j < 2; j++)
468 reslevel->coord[i][j] =
469 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
470 // update precincts size: 2^n value
471 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
472 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
474 /* Number of bands for each resolution level */
476 reslevel->nbands = 1;
478 reslevel->nbands = 3;
480 /* Number of precincts which span the tile for resolution level reslevelno
481 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
482 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
483 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
484 * for Dcinema profiles in JPEG 2000
485 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
486 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
487 if (reslevel->coord[0][1] == reslevel->coord[0][0])
488 reslevel->num_precincts_x = 0;
490 reslevel->num_precincts_x =
491 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
492 reslevel->log2_prec_width) -
493 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
495 if (reslevel->coord[1][1] == reslevel->coord[1][0])
496 reslevel->num_precincts_y = 0;
498 reslevel->num_precincts_y =
499 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
500 reslevel->log2_prec_height) -
501 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
503 reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
505 return AVERROR(ENOMEM);
507 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
508 ret = init_band(avctx, reslevel,
509 comp, codsty, qntsty,
510 bandno, gbandno, reslevelno,
519 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
521 int reslevelno, bandno, cblkno, precno;
522 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
523 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
524 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
525 Jpeg2000Band *band = rlevel->band + bandno;
526 for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
527 Jpeg2000Prec *prec = band->prec + precno;
528 tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
529 tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
530 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
531 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
540 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
542 int reslevelno, bandno, precno;
544 comp->reslevel && reslevelno < codsty->nreslevels;
546 Jpeg2000ResLevel *reslevel;
551 reslevel = comp->reslevel + reslevelno;
552 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
558 band = reslevel->band + bandno;
559 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
561 Jpeg2000Prec *prec = band->prec + precno;
562 av_freep(&prec->zerobits);
563 av_freep(&prec->cblkincl);
564 av_freep(&prec->cblk);
568 av_freep(&band->prec);
570 av_freep(&reslevel->band);
573 ff_dwt_destroy(&comp->dwt);
574 av_freep(&comp->reslevel);
575 av_freep(&comp->i_data);
576 av_freep(&comp->f_data);