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 Libav.
8 * Libav 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 * Libav 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 Libav; 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/common.h"
30 #include "libavutil/mem.h"
34 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
36 /* tag tree routines */
38 /* allocate the memory for tag tree */
39 static int32_t tag_tree_size(uint16_t w, uint16_t h)
42 while (w > 1 || h > 1) {
44 if (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);
62 t = res = av_mallocz_array(tt_size, sizeof(*t));
66 while (w > 1 || h > 1) {
75 for (i = 0; i < ph; i++)
76 for (j = 0; j < pw; j++)
77 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
85 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
87 static int getsigctxno(int flag, int bandno)
91 h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
92 ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
93 v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
94 ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
95 d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
96 ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
97 ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
98 ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
142 uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
144 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
145 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
146 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
148 static int getsgnctxno(int flag, uint8_t *xorbit)
150 int vcontrib, hcontrib;
152 hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
153 [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
154 vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
155 [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
156 *xorbit = xorbittab[hcontrib][vcontrib];
158 return ctxlbltab[hcontrib][vcontrib];
161 void av_cold ff_jpeg2000_init_tier1_luts(void)
164 for (i = 0; i < 256; i++)
165 for (j = 0; j < 4; j++)
166 ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
167 for (i = 0; i < 16; i++)
168 for (j = 0; j < 16; j++)
169 ff_jpeg2000_sgnctxno_lut[i][j] =
170 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
173 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
178 t1->flags[y][x] |= JPEG2000_T1_SIG;
180 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
181 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
182 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
183 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
185 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
186 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
187 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
188 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
190 t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
191 t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
192 t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
193 t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
196 static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
198 static void init_band_stepsize(AVCodecContext *avctx,
200 Jpeg2000CodingStyle *codsty,
201 Jpeg2000QuantStyle *qntsty,
202 int bandno, int gbandno, int reslevelno,
205 /* TODO: Implementation of quantization step not finished,
206 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
207 switch (qntsty->quantsty) {
210 case JPEG2000_QSTY_NONE:
211 /* TODO: to verify. No quantization in this case */
212 band->f_stepsize = 1;
214 case JPEG2000_QSTY_SI:
215 /*TODO: Compute formula to implement. */
217 lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
218 band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
219 2 + numbps - qntsty->expn[gbandno]);
221 case JPEG2000_QSTY_SE:
222 /* Exponent quantization step.
224 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
225 * R_b = R_I + log2 (gain_b )
226 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
227 /* TODO/WARN: value of log2 (gain_b ) not taken into account
228 * but it works (compared to OpenJPEG). Why?
229 * Further investigation needed. */
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 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
240 * If not set output of entropic decoder is not correct. */
241 if (!av_codec_is_encoder(avctx->codec))
242 band->f_stepsize *= 0.5;
244 band->i_stepsize = band->f_stepsize * (1 << 16);
247 static int init_prec(Jpeg2000Band *band,
248 Jpeg2000ResLevel *reslevel,
249 Jpeg2000Component *comp,
250 int precno, int bandno, int reslevelno,
251 int log2_band_prec_width,
252 int log2_band_prec_height)
254 Jpeg2000Prec *prec = band->prec + precno;
255 int nb_codeblocks, cblkno;
257 /* TODO: Explain formula for JPEG200 DCINEMA. */
258 /* TODO: Verify with previous count of codeblocks per band */
261 prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
262 (1 << log2_band_prec_width);
263 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
266 prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
267 (1 << log2_band_prec_height);
268 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
271 prec->coord[0][1] = prec->coord[0][0] +
272 (1 << log2_band_prec_width);
273 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
276 prec->coord[1][1] = prec->coord[1][0] +
277 (1 << log2_band_prec_height);
278 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
280 prec->nb_codeblocks_width =
281 ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
283 band->log2_cblk_width);
284 prec->nb_codeblocks_height =
285 ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
287 band->log2_cblk_height);
289 /* Tag trees initialization */
291 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
292 prec->nb_codeblocks_height);
294 return AVERROR(ENOMEM);
297 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
298 prec->nb_codeblocks_height);
300 return AVERROR(ENOMEM);
302 nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
303 prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
305 return AVERROR(ENOMEM);
306 for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
307 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
310 /* Compute coordinates of codeblocks */
312 Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
313 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
314 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
317 Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
318 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
319 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
322 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
326 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
328 /* Update code-blocks coordinates according sub-band position */
329 if ((bandno + !!reslevelno) & 1) {
330 cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
331 comp->reslevel[reslevelno-1].coord[0][0];
332 cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
333 comp->reslevel[reslevelno-1].coord[0][0];
335 if ((bandno + !!reslevelno) & 2) {
336 cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
337 comp->reslevel[reslevelno-1].coord[1][0];
338 cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
339 comp->reslevel[reslevelno-1].coord[1][0];
352 static int init_band(AVCodecContext *avctx,
353 Jpeg2000ResLevel *reslevel,
354 Jpeg2000Component *comp,
355 Jpeg2000CodingStyle *codsty,
356 Jpeg2000QuantStyle *qntsty,
357 int bandno, int gbandno, int reslevelno,
358 int cbps, int dx, int dy)
360 Jpeg2000Band *band = reslevel->band + bandno;
361 uint8_t log2_band_prec_width, log2_band_prec_height;
362 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
367 init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
370 /* computation of tbx_0, tbx_1, tby_0, tby_1
371 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
372 * codeblock width and height is computed for
373 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
374 if (reslevelno == 0) {
375 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
376 for (i = 0; i < 2; i++)
377 for (j = 0; j < 2; j++)
379 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0],
381 log2_band_prec_width = reslevel->log2_prec_width;
382 log2_band_prec_height = reslevel->log2_prec_height;
383 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
384 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
385 reslevel->log2_prec_width);
386 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
387 reslevel->log2_prec_height);
389 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
390 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
391 for (i = 0; i < 2; i++)
392 for (j = 0; j < 2; j++)
393 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
395 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] -
396 (((bandno + 1 >> i) & 1) << declvl - 1),
398 /* TODO: Manage case of 3 band offsets here or
399 * in coding/decoding function? */
401 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
402 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
403 reslevel->log2_prec_width - 1);
404 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
405 reslevel->log2_prec_height - 1);
407 log2_band_prec_width = reslevel->log2_prec_width - 1;
408 log2_band_prec_height = reslevel->log2_prec_height - 1;
411 for (j = 0; j < 2; j++)
412 band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
413 for (j = 0; j < 2; j++)
414 band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
416 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
417 band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
419 return AVERROR(ENOMEM);
421 for (precno = 0; precno < nb_precincts; precno++) {
422 ret = init_prec(band, reslevel, comp,
423 precno, bandno, reslevelno,
424 log2_band_prec_width, log2_band_prec_height);
432 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
433 Jpeg2000CodingStyle *codsty,
434 Jpeg2000QuantStyle *qntsty,
435 int cbps, int dx, int dy,
436 AVCodecContext *avctx)
438 int reslevelno, bandno, gbandno = 0, ret, i, j;
441 if (!codsty->nreslevels2decode) {
442 av_log(avctx, AV_LOG_ERROR, "nreslevels2decode uninitialized\n");
443 return AVERROR_INVALIDDATA;
446 if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
447 codsty->nreslevels2decode - 1,
450 // component size comp->coord is uint16_t so ir cannot overflow
451 csize = (comp->coord[0][1] - comp->coord[0][0]) *
452 (comp->coord[1][1] - comp->coord[1][0]);
454 if (codsty->transform == FF_DWT97) {
456 comp->f_data = av_malloc_array(csize, sizeof(*comp->f_data));
458 return AVERROR(ENOMEM);
461 comp->i_data = av_malloc_array(csize, sizeof(*comp->i_data));
463 return AVERROR(ENOMEM);
465 comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
467 return AVERROR(ENOMEM);
468 /* LOOP on resolution levels */
469 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
470 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
471 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
473 /* Compute borders for each resolution level.
474 * Computation of trx_0, trx_1, try_0 and try_1.
475 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
476 for (i = 0; i < 2; i++)
477 for (j = 0; j < 2; j++)
478 reslevel->coord[i][j] =
479 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
480 // update precincts size: 2^n value
481 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
482 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
484 /* Number of bands for each resolution level */
486 reslevel->nbands = 1;
488 reslevel->nbands = 3;
490 /* Number of precincts wich span the tile for resolution level reslevelno
491 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
492 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
493 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
494 * for Dcinema profiles in JPEG 2000
495 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
496 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
497 if (reslevel->coord[0][1] == reslevel->coord[0][0])
498 reslevel->num_precincts_x = 0;
500 reslevel->num_precincts_x =
501 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
502 reslevel->log2_prec_width) -
503 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
505 if (reslevel->coord[1][1] == reslevel->coord[1][0])
506 reslevel->num_precincts_y = 0;
508 reslevel->num_precincts_y =
509 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
510 reslevel->log2_prec_height) -
511 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
513 reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
515 return AVERROR(ENOMEM);
517 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
518 ret = init_band(avctx, reslevel,
519 comp, codsty, qntsty,
520 bandno, gbandno, reslevelno,
529 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
531 int reslevelno, bandno, precno;
533 comp->reslevel && reslevelno < codsty->nreslevels;
535 Jpeg2000ResLevel *reslevel;
540 reslevel = comp->reslevel + reslevelno;
541 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
547 band = reslevel->band + bandno;
548 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
554 prec = band->prec + precno;
555 av_freep(&prec->zerobits);
556 av_freep(&prec->cblkincl);
557 av_freep(&prec->cblk);
561 av_freep(&band->prec);
563 av_freep(&reslevel->band);
566 ff_dwt_destroy(&comp->dwt);
567 av_freep(&comp->reslevel);
568 av_freep(&comp->i_data);
569 av_freep(&comp->f_data);