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
24 * JPEG 2000 image encoder and decoder common functions
26 * @author Kamil Nowosad
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 int tag_tree_size(int w, int h)
43 while (w > 1 || h > 1) {
51 Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h)
54 Jpeg2000TgtNode *res, *t, *t2;
57 tt_size = tag_tree_size(w, h);
59 t = res = av_mallocz_array(tt_size, sizeof(*t));
63 while (w > 1 || h > 1) {
72 for (i = 0; i < ph; i++)
73 for (j = 0; j < pw; j++)
74 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
82 static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
84 int i, siz = tag_tree_size(w, h);
86 for (i = 0; i < siz; i++) {
92 static int getsigctxno(int flag, int bandno)
96 h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
97 ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
98 v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
99 ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
100 d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
101 ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
102 ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
103 ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
108 if (h == 2) return 8;
110 if (v >= 1) return 7;
111 if (d >= 1) return 6;
114 if (v == 2) return 4;
115 if (v == 1) return 3;
116 if (d >= 2) return 2;
117 if (d == 1) return 1;
119 if (d >= 3) return 8;
121 if (h+v >= 1) return 7;
125 if (h+v >= 2) return 5;
126 if (h+v == 1) return 4;
129 if (h+v >= 2) return 2;
130 if (h+v == 1) return 1;
136 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
137 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
138 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
140 static int getsgnctxno(int flag, uint8_t *xorbit)
142 int vcontrib, hcontrib;
144 hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
145 [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
146 vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
147 [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
148 *xorbit = xorbittab[hcontrib][vcontrib];
150 return ctxlbltab[hcontrib][vcontrib];
153 void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y,
158 t1->flags[y][x] |= JPEG2000_T1_SIG;
160 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
161 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
162 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
163 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
165 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
166 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
167 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
168 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
170 t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
171 t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
172 t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
173 t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
176 static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
178 int ff_j2k_init_component(Jpeg2000Component *comp,
179 Jpeg2000CodingStyle *codsty,
180 Jpeg2000QuantStyle *qntsty,
181 int cbps, int dx, int dy,
182 AVCodecContext *avctx)
184 uint8_t log2_band_prec_width, log2_band_prec_height;
185 int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
187 if (ret=ff_jpeg2000_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels2decode-1, codsty->transform == FF_DWT53 ? FF_DWT53 : FF_DWT97_INT))
189 for (i = 0; i < 2; i++)
190 csize *= comp->coord[i][1] - comp->coord[i][0];
192 comp->data = av_malloc_array(csize, sizeof(*comp->data));
194 return AVERROR(ENOMEM);
195 comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
197 return AVERROR(ENOMEM);
198 /* LOOP on resolution levels */
199 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
200 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
201 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
203 /* Compute borders for each resolution level.
204 * Computation of trx_0, trx_1, try_0 and try_1.
205 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
206 for (i = 0; i < 2; i++)
207 for (j = 0; j < 2; j++)
208 reslevel->coord[i][j] =
209 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
210 // update precincts size: 2^n value
211 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
212 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
214 /* Number of bands for each resolution level */
216 reslevel->nbands = 1;
218 reslevel->nbands = 3;
220 /* Number of precincts wich span the tile for resolution level reslevelno
221 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
222 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
223 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
224 * for Dcinema profiles in JPEG 2000
225 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
226 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
227 if (reslevel->coord[0][1] == reslevel->coord[0][0])
228 reslevel->num_precincts_x = 0;
230 reslevel->num_precincts_x =
231 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
232 reslevel->log2_prec_width) -
233 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
235 if (reslevel->coord[1][1] == reslevel->coord[1][0])
236 reslevel->num_precincts_y = 0;
238 reslevel->num_precincts_y =
239 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
240 reslevel->log2_prec_height) -
241 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
243 reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
245 return AVERROR(ENOMEM);
247 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
248 Jpeg2000Band *band = reslevel->band + bandno;
253 /* TODO: Implementation of quantization step not finished,
254 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
255 switch (qntsty->quantsty) {
258 case JPEG2000_QSTY_NONE:
259 /* TODO: to verify. No quantization in this case */
262 case JPEG2000_QSTY_SI:
263 /*TODO: Compute formula to implement. */
265 lut_gain[codsty->transform][bandno + reslevelno > 0];
266 stepsize = SHL(2048 + qntsty->mant[gbandno],
267 2 + numbps - qntsty->expn[gbandno]);
269 case JPEG2000_QSTY_SE:
270 /* Exponent quantization step.
272 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
273 * R_b = R_I + log2 (gain_b )
274 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
275 /* TODO/WARN: value of log2 (gain_b ) not taken into account
276 * but it works (compared to OpenJPEG). Why?
277 * Further investigation needed. */
279 stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
280 stepsize *= (qntsty->mant[gbandno] / 2048.0 + 1.0);
281 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
282 * If not set output of entropic decoder is not correct. */
287 av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
290 /* BITEXACT computing case --> convert to int */
291 // if (avctx->flags & CODEC_FLAG_BITEXACT)
292 band->stepsize = stepsize * (1 << 16);
294 /* computation of tbx_0, tbx_1, tby_0, tby_1
295 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
296 * codeblock width and height is computed for
297 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
298 if (reslevelno == 0) {
299 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
300 for (i = 0; i < 2; i++)
301 for (j = 0; j < 2; j++)
303 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0],
305 log2_band_prec_width = reslevel->log2_prec_width;
306 log2_band_prec_height = reslevel->log2_prec_height;
307 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
308 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
309 reslevel->log2_prec_width);
310 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
311 reslevel->log2_prec_height);
313 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
314 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
315 for (i = 0; i < 2; i++)
316 for (j = 0; j < 2; j++)
317 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
319 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] -
320 (((bandno + 1 >> i) & 1) << declvl - 1),
322 /* TODO: Manage case of 3 band offsets here or
323 * in coding/decoding function? */
325 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
326 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
327 reslevel->log2_prec_width - 1);
328 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
329 reslevel->log2_prec_height - 1);
331 log2_band_prec_width = reslevel->log2_prec_width - 1;
332 log2_band_prec_height = reslevel->log2_prec_height - 1;
335 for (j = 0; j < 2; j++)
336 band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
337 for (j = 0; j < 2; j++)
338 band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
340 band->prec = av_malloc_array(reslevel->num_precincts_x *
341 reslevel->num_precincts_y,
342 sizeof(*band->prec));
344 return AVERROR(ENOMEM);
346 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
348 for (precno = 0; precno < nb_precincts; precno++) {
349 Jpeg2000Prec *prec = band->prec + precno;
351 /* TODO: Explain formula for JPEG200 DCINEMA. */
352 /* TODO: Verify with previous count of codeblocks per band */
355 prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
356 (1 << log2_band_prec_width);
357 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
360 prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
361 (1 << log2_band_prec_height);
362 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
365 prec->coord[0][1] = prec->coord[0][0] +
366 (1 << log2_band_prec_width);
367 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
370 prec->coord[1][1] = prec->coord[1][0] +
371 (1 << log2_band_prec_height);
372 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
374 prec->nb_codeblocks_width =
375 ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
377 band->log2_cblk_width);
378 prec->nb_codeblocks_height =
379 ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
381 band->log2_cblk_height);
383 /* Tag trees initialization */
385 ff_j2k_tag_tree_init(prec->nb_codeblocks_width,
386 prec->nb_codeblocks_height);
388 return AVERROR(ENOMEM);
391 ff_j2k_tag_tree_init(prec->nb_codeblocks_width,
392 prec->nb_codeblocks_height);
394 return AVERROR(ENOMEM);
396 prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
397 prec->nb_codeblocks_height,
398 sizeof(*prec->cblk));
400 return AVERROR(ENOMEM);
401 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
402 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
405 /* Compute coordinates of codeblocks */
407 Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
408 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
409 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
412 Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
413 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
414 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
417 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
421 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
424 if((bandno + !!reslevelno) & 1) {
425 cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
426 cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
428 if((bandno + !!reslevelno) & 2) {
429 cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
430 cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
445 void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
447 int reslevelno, bandno, cblkno, precno;
448 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
449 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
450 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
451 Jpeg2000Band *band = rlevel->band + bandno;
452 for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
453 Jpeg2000Prec *prec = band->prec + precno;
454 tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
455 tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
456 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
457 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
466 void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
468 int reslevelno, bandno, precno;
469 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
470 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
472 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
473 Jpeg2000Band *band = reslevel->band + bandno;
474 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
475 Jpeg2000Prec *prec = band->prec + precno;
476 av_freep(&prec->zerobits);
477 av_freep(&prec->cblkincl);
478 av_freep(&prec->cblk);
481 av_freep(&band->prec);
483 av_freep(&reslevel->band);
486 ff_dwt_destroy(&comp->dwt);
487 av_freep(&comp->reslevel);
488 av_freep(&comp->data);