2 * JPEG2000 encoder and decoder common functions
3 * Copyright (c) 2007 Kamil Nowosad
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * JPEG2000 image encoder and decoder common functions
25 * @author Kamil Nowosad
32 #define SHL(a, n) ((n)>=0 ? (a) << (n) : (a) >> -(n))
35 void ff_j2k_printv(int *tab, int l)
38 for (i = 0; i < l; i++)
39 printf("%.3d ", tab[i]);
43 void ff_j2k_printu(uint8_t *tab, int l)
46 for (i = 0; i < l; i++)
47 printf("%.3hd ", tab[i]);
52 /* tag tree routines */
54 /** allocate the memory for tag tree */
56 static int tag_tree_size(int w, int h)
59 while (w > 1 || h > 1){
67 J2kTgtNode *ff_j2k_tag_tree_init(int w, int h)
70 J2kTgtNode *res, *t, *t2;
72 t = res = av_mallocz(tag_tree_size(w, h)*sizeof(J2kTgtNode));
77 while (w > 1 || h > 1){
86 for (i = 0; i < ph; i++)
87 for (j = 0; j < pw; j++){
88 t[i*pw + j].parent = &t2[(i>>1)*w + (j>>1)];
96 static void tag_tree_zero(J2kTgtNode *t, int w, int h)
98 int i, siz = tag_tree_size(w, h);
100 for (i = 0; i < siz; i++){
106 uint8_t ff_j2k_nbctxno_lut[256][4];
108 static int getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
112 h = ((flag & J2K_T1_SIG_E) ? 1:0)+
113 ((flag & J2K_T1_SIG_W) ? 1:0);
114 v = ((flag & J2K_T1_SIG_N) ? 1:0);
115 if (!vert_causal_ctx_csty_symbol)
116 v = v + ((flag & J2K_T1_SIG_S) ? 1:0);
117 d = ((flag & J2K_T1_SIG_NE) ? 1:0)+
118 ((flag & J2K_T1_SIG_NW) ? 1:0);
119 if (!vert_causal_ctx_csty_symbol)
120 d = d + ((flag & J2K_T1_SIG_SE) ? 1:0)+
121 ((flag & J2K_T1_SIG_SW) ? 1:0);
125 if (h == 2) return 8;
127 if (v >= 1) return 7;
128 if (d >= 1) return 6;
131 if (v == 2) return 4;
132 if (v == 1) return 3;
133 if (d >= 2) return 2;
134 if (d == 1) return 1;
137 if (d >= 3) return 8;
139 if (h+v >= 1) return 7;
143 if (h+v >= 2) return 5;
144 if (h+v == 1) return 4;
147 if (h+v >= 2) return 2;
148 if (h+v == 1) return 1;
153 uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
155 static int getsgnctxno(int flag, uint8_t *xorbit)
157 int vcontrib, hcontrib;
158 static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
159 static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
160 static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
162 hcontrib = contribtab[flag & J2K_T1_SIG_E ? flag & J2K_T1_SGN_E ? 1:2:0]
163 [flag & J2K_T1_SIG_W ? flag & J2K_T1_SGN_W ? 1:2:0]+1;
164 vcontrib = contribtab[flag & J2K_T1_SIG_S ? flag & J2K_T1_SGN_S ? 1:2:0]
165 [flag & J2K_T1_SIG_N ? flag & J2K_T1_SGN_N ? 1:2:0]+1;
166 *xorbit = xorbittab[hcontrib][vcontrib];
167 return ctxlbltab[hcontrib][vcontrib];
170 void ff_j2k_init_tier1_luts(void)
173 for (i = 0; i < 256; i++)
174 for (j = 0; j < 4; j++)
175 ff_j2k_nbctxno_lut[i][j] = getnbctxno(i, j, 0);
176 for (i = 0; i < 16; i++)
177 for (j = 0; j < 16; j++)
178 ff_j2k_sgnctxno_lut[i][j] = getsgnctxno(i + (j << 8), &ff_j2k_xorbit_lut[i][j]);
181 void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
184 t1->flags[y][x] |= J2K_T1_SIG;
186 t1->flags[y][x+1] |= J2K_T1_SIG_W | J2K_T1_SGN_W;
187 t1->flags[y][x-1] |= J2K_T1_SIG_E | J2K_T1_SGN_E;
188 t1->flags[y+1][x] |= J2K_T1_SIG_N | J2K_T1_SGN_N;
189 t1->flags[y-1][x] |= J2K_T1_SIG_S | J2K_T1_SGN_S;
191 t1->flags[y][x+1] |= J2K_T1_SIG_W;
192 t1->flags[y][x-1] |= J2K_T1_SIG_E;
193 t1->flags[y+1][x] |= J2K_T1_SIG_N;
194 t1->flags[y-1][x] |= J2K_T1_SIG_S;
196 t1->flags[y+1][x+1] |= J2K_T1_SIG_NW;
197 t1->flags[y+1][x-1] |= J2K_T1_SIG_NE;
198 t1->flags[y-1][x+1] |= J2K_T1_SIG_SW;
199 t1->flags[y-1][x-1] |= J2K_T1_SIG_SE;
202 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
204 int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
206 if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
208 for (i = 0; i < 2; i++)
209 csize *= comp->coord[i][1] - comp->coord[i][0];
211 comp->data = av_malloc(csize * sizeof(int));
213 return AVERROR(ENOMEM);
214 comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
217 return AVERROR(ENOMEM);
218 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
219 int declvl = codsty->nreslevels - reslevelno;
220 J2kResLevel *reslevel = comp->reslevel + reslevelno;
222 for (i = 0; i < 2; i++)
223 for (j = 0; j < 2; j++)
224 reslevel->coord[i][j] =
225 ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
228 reslevel->nbands = 1;
230 reslevel->nbands = 3;
232 if (reslevel->coord[0][1] == reslevel->coord[0][0])
233 reslevel->num_precincts_x = 0;
235 reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
236 - (reslevel->coord[0][0] >> codsty->log2_prec_width);
238 if (reslevel->coord[1][1] == reslevel->coord[1][0])
239 reslevel->num_precincts_y = 0;
241 reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
242 - (reslevel->coord[1][0] >> codsty->log2_prec_height);
244 reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
246 return AVERROR(ENOMEM);
247 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
248 J2kBand *band = reslevel->band + bandno;
249 int cblkno, precx, precy, precno;
251 int xi0, yi0, xi1, yi1;
252 int cblkperprecw, cblkperprech;
254 if (qntsty->quantsty != J2K_QSTY_NONE){
255 static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
258 numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
259 band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
261 band->stepsize = 1 << 13;
263 if (reslevelno == 0){ // the same everywhere
264 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
265 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
266 for (i = 0; i < 2; i++)
267 for (j = 0; j < 2; j++)
268 band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
270 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
271 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
273 for (i = 0; i < 2; i++)
274 for (j = 0; j < 2; j++)
275 band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
277 band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width) - band->coord[0][0] / band->codeblock_width;
278 band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
280 for (j = 0; j < 2; j++)
281 band->coord[0][j] = ff_j2k_ceildiv(band->coord[0][j], dx);
282 for (j = 0; j < 2; j++)
283 band->coord[1][j] = ff_j2k_ceildiv(band->coord[1][j], dy);
285 band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
286 band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
288 band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
290 return AVERROR(ENOMEM);
291 band->prec = av_malloc(reslevel->num_precincts_x * reslevel->num_precincts_y * sizeof(J2kPrec));
293 return AVERROR(ENOMEM);
295 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
296 J2kCblk *cblk = band->cblk + cblkno;
304 y0 = band->coord[1][0];
305 y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
307 yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
308 yi1 = FFMIN(yi1, band->cblkny);
309 cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
310 for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
311 for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
312 band->prec[precno].yi0 = yi0;
313 band->prec[precno].yi1 = yi1;
316 yi0 = yi1 - cblkperprech;
317 yi1 = FFMIN(yi1, band->cblkny);
319 x0 = band->coord[0][0];
320 x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
322 xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
323 xi1 = FFMIN(xi1, band->cblknx);
325 cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
326 for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
327 for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
328 J2kPrec *prec = band->prec + precno;
331 prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
332 prec->yi1 - prec->yi0);
333 prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
334 prec->yi1 - prec->yi0);
335 if (!prec->cblkincl || !prec->zerobits)
336 return AVERROR(ENOMEM);
340 xi0 = xi1 - cblkperprecw;
341 xi1 = FFMIN(xi1, band->cblknx);
348 void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
350 int reslevelno, bandno, cblkno, precno;
351 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
352 J2kResLevel *rlevel = comp->reslevel + reslevelno;
353 for (bandno = 0; bandno < rlevel->nbands; bandno++){
354 J2kBand *band = rlevel->band + bandno;
355 for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
356 J2kPrec *prec = band->prec + precno;
357 tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
358 tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
360 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
361 J2kCblk *cblk = band->cblk + cblkno;
369 void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
371 int reslevelno, bandno, precno;
372 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
373 J2kResLevel *reslevel = comp->reslevel + reslevelno;
375 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
376 J2kBand *band = reslevel->band + bandno;
377 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
378 J2kPrec *prec = band->prec + precno;
379 av_freep(&prec->zerobits);
380 av_freep(&prec->cblkincl);
382 av_freep(&band->cblk);
383 av_freep(&band->prec);
385 av_freep(&reslevel->band);
388 ff_j2k_dwt_destroy(&comp->dwt);
389 av_freep(&comp->reslevel);
390 av_freep(&comp->data);