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/common.h"
29 #include "libavutil/mem.h"
33 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
35 /* tag tree routines */
37 /* allocate the memory for tag tree */
38 static int32_t tag_tree_size(uint16_t w, uint16_t h)
41 while (w > 1 || h > 1) {
43 if (res + 1 >= INT32_MAX)
48 return (int32_t)(res + 1);
51 static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
54 Jpeg2000TgtNode *res, *t, *t2;
57 tt_size = tag_tree_size(w, h);
61 t = res = av_mallocz_array(tt_size, sizeof(*t));
65 while (w > 1 || h > 1) {
74 for (i = 0; i < ph; i++)
75 for (j = 0; j < pw; j++)
76 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
84 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
86 static int getsigctxno(int flag, int bandno)
90 h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
91 ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
92 v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
93 ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
94 d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
95 ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
96 ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
97 ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
143 uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
145 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
146 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
147 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
149 static int getsgnctxno(int flag, uint8_t *xorbit)
151 int vcontrib, hcontrib;
153 hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
154 [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
155 vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
156 [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
157 *xorbit = xorbittab[hcontrib][vcontrib];
159 return ctxlbltab[hcontrib][vcontrib];
162 void ff_jpeg2000_init_tier1_luts(void)
165 for (i = 0; i < 256; i++)
166 for (j = 0; j < 4; j++)
167 ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
168 for (i = 0; i < 16; i++)
169 for (j = 0; j < 16; j++)
170 ff_jpeg2000_sgnctxno_lut[i][j] =
171 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
174 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
179 t1->flags[y][x] |= JPEG2000_T1_SIG;
181 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
182 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
183 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
184 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
186 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
187 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
188 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
189 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
191 t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
192 t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
193 t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
194 t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
197 static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
199 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
200 Jpeg2000CodingStyle *codsty,
201 Jpeg2000QuantStyle *qntsty,
202 int cbps, int dx, int dy,
203 AVCodecContext *avctx)
205 uint8_t log2_band_prec_width, log2_band_prec_height;
206 int reslevelno, bandno, gbandno = 0, ret, i, j;
209 if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
210 codsty->nreslevels2decode - 1,
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]);
217 comp->data = av_malloc_array(csize, sizeof(*comp->data));
219 return AVERROR(ENOMEM);
220 comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
222 return AVERROR(ENOMEM);
223 /* LOOP on resolution levels */
224 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
225 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
226 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
228 /* Compute borders for each resolution level.
229 * Computation of trx_0, trx_1, try_0 and try_1.
230 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
231 for (i = 0; i < 2; i++)
232 for (j = 0; j < 2; j++)
233 reslevel->coord[i][j] =
234 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
235 // update precincts size: 2^n value
236 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
237 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
239 /* Number of bands for each resolution level */
241 reslevel->nbands = 1;
243 reslevel->nbands = 3;
245 /* Number of precincts wich span the tile for resolution level reslevelno
246 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
247 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
248 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
249 * for Dcinema profiles in JPEG 2000
250 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
251 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
252 if (reslevel->coord[0][1] == reslevel->coord[0][0])
253 reslevel->num_precincts_x = 0;
255 reslevel->num_precincts_x =
256 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
257 reslevel->log2_prec_width) -
258 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
260 if (reslevel->coord[1][1] == reslevel->coord[1][0])
261 reslevel->num_precincts_y = 0;
263 reslevel->num_precincts_y =
264 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
265 reslevel->log2_prec_height) -
266 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
268 reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
270 return AVERROR(ENOMEM);
272 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
273 Jpeg2000Band *band = reslevel->band + bandno;
277 /* TODO: Implementation of quantization step not finished,
278 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
279 switch (qntsty->quantsty) {
282 case JPEG2000_QSTY_NONE:
283 /* TODO: to verify. No quantization in this case */
285 lut_gain[codsty->transform][bandno + reslevelno > 0];
286 band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno],
287 2 + numbps - qntsty->expn[gbandno]);
289 case JPEG2000_QSTY_SI:
290 /*TODO: Compute formula to implement. */
291 band->stepsize = (float) (1 << 13);
293 case JPEG2000_QSTY_SE:
294 /* Exponent quantization step.
296 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
297 * R_b = R_I + log2 (gain_b )
298 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
299 /* TODO/WARN: value of log2 (gain_b ) not taken into account
300 * but it works (compared to OpenJPEG). Why?
301 * Further investigation needed. */
303 band->stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
304 band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0;
305 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
306 * If not set output of entropic decoder is not correct. */
307 band->stepsize *= 0.5;
311 av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
314 /* BITEXACT computing case --> convert to int */
315 if (avctx->flags & CODEC_FLAG_BITEXACT)
316 band->stepsize = (int32_t)(band->stepsize * (1 << 16));
318 /* computation of tbx_0, tbx_1, tby_0, tby_1
319 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
320 * codeblock width and height is computed for
321 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
322 if (reslevelno == 0) {
323 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
324 for (i = 0; i < 2; i++)
325 for (j = 0; j < 2; j++)
327 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
330 log2_band_prec_width = reslevel->log2_prec_width;
331 log2_band_prec_height = reslevel->log2_prec_height;
332 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
333 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
334 reslevel->log2_prec_width);
335 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
336 reslevel->log2_prec_height);
338 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
339 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
340 for (i = 0; i < 2; i++)
341 for (j = 0; j < 2; j++)
342 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
344 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
345 (((bandno + 1 >> i) & 1) << declvl - 1),
347 /* TODO: Manage case of 3 band offsets here or
348 * in coding/decoding function? */
350 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
351 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
352 reslevel->log2_prec_width - 1);
353 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
354 reslevel->log2_prec_height - 1);
356 log2_band_prec_width = reslevel->log2_prec_width - 1;
357 log2_band_prec_height = reslevel->log2_prec_height - 1;
360 band->prec = av_malloc_array(reslevel->num_precincts_x *
361 reslevel->num_precincts_y,
362 sizeof(*band->prec));
364 return AVERROR(ENOMEM);
366 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
368 for (precno = 0; precno < nb_precincts; precno++) {
369 Jpeg2000Prec *prec = band->prec + precno;
371 /* TODO: Explain formula for JPEG200 DCINEMA. */
372 /* TODO: Verify with previous count of codeblocks per band */
375 prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
376 (1 << log2_band_prec_width);
377 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
380 prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
381 (1 << log2_band_prec_height);
382 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
385 prec->coord[0][1] = prec->coord[0][0] +
386 (1 << log2_band_prec_width);
387 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
390 prec->coord[1][1] = prec->coord[1][0] +
391 (1 << log2_band_prec_height);
392 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
394 prec->nb_codeblocks_width =
395 ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
397 band->log2_cblk_width);
398 prec->nb_codeblocks_height =
399 ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
401 band->log2_cblk_height);
403 /* Tag trees initialization */
405 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
406 prec->nb_codeblocks_height);
408 return AVERROR(ENOMEM);
411 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
412 prec->nb_codeblocks_height);
414 return AVERROR(ENOMEM);
416 prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
417 prec->nb_codeblocks_height,
418 sizeof(*prec->cblk));
420 return AVERROR(ENOMEM);
421 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
422 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
425 /* Compute coordinates of codeblocks */
427 Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
428 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
429 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
432 Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
433 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
434 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
437 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
441 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
455 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
457 int reslevelno, bandno, precno;
458 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
459 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
461 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
462 Jpeg2000Band *band = reslevel->band + bandno;
463 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
464 Jpeg2000Prec *prec = band->prec + precno;
465 av_freep(&prec->zerobits);
466 av_freep(&prec->cblkincl);
467 av_freep(&prec->cblk);
470 av_freep(&band->prec);
472 av_freep(&reslevel->band);
475 ff_dwt_destroy(&comp->dwt);
476 av_freep(&comp->reslevel);
477 av_freep(&comp->data);