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;
154 uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
156 static int getsgnctxno(int flag, uint8_t *xorbit)
158 int vcontrib, hcontrib;
159 static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
160 static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
161 static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
163 hcontrib = contribtab[flag & J2K_T1_SIG_E ? flag & J2K_T1_SGN_E ? 1:2:0]
164 [flag & J2K_T1_SIG_W ? flag & J2K_T1_SGN_W ? 1:2:0]+1;
165 vcontrib = contribtab[flag & J2K_T1_SIG_S ? flag & J2K_T1_SGN_S ? 1:2:0]
166 [flag & J2K_T1_SIG_N ? flag & J2K_T1_SGN_N ? 1:2:0]+1;
167 *xorbit = xorbittab[hcontrib][vcontrib];
168 return ctxlbltab[hcontrib][vcontrib];
171 void ff_j2k_init_tier1_luts(void)
174 for (i = 0; i < 256; i++)
175 for (j = 0; j < 4; j++)
176 ff_j2k_nbctxno_lut[i][j] = getnbctxno(i, j, 0);
177 for (i = 0; i < 16; i++)
178 for (j = 0; j < 16; j++)
179 ff_j2k_sgnctxno_lut[i][j] = getsgnctxno(i + (j << 8), &ff_j2k_xorbit_lut[i][j]);
182 void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
185 t1->flags[y][x] |= J2K_T1_SIG;
187 t1->flags[y][x+1] |= J2K_T1_SIG_W | J2K_T1_SGN_W;
188 t1->flags[y][x-1] |= J2K_T1_SIG_E | J2K_T1_SGN_E;
189 t1->flags[y+1][x] |= J2K_T1_SIG_N | J2K_T1_SGN_N;
190 t1->flags[y-1][x] |= J2K_T1_SIG_S | J2K_T1_SGN_S;
192 t1->flags[y][x+1] |= J2K_T1_SIG_W;
193 t1->flags[y][x-1] |= J2K_T1_SIG_E;
194 t1->flags[y+1][x] |= J2K_T1_SIG_N;
195 t1->flags[y-1][x] |= J2K_T1_SIG_S;
197 t1->flags[y+1][x+1] |= J2K_T1_SIG_NW;
198 t1->flags[y+1][x-1] |= J2K_T1_SIG_NE;
199 t1->flags[y-1][x+1] |= J2K_T1_SIG_SW;
200 t1->flags[y-1][x-1] |= J2K_T1_SIG_SE;
203 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
205 int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
207 if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
209 for (i = 0; i < 2; i++)
210 csize *= comp->coord[i][1] - comp->coord[i][0];
212 comp->data = av_malloc(csize * sizeof(int));
214 return AVERROR(ENOMEM);
215 comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
218 return AVERROR(ENOMEM);
219 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
220 int declvl = codsty->nreslevels - reslevelno;
221 J2kResLevel *reslevel = comp->reslevel + reslevelno;
223 for (i = 0; i < 2; i++)
224 for (j = 0; j < 2; j++)
225 reslevel->coord[i][j] =
226 ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
229 reslevel->nbands = 1;
231 reslevel->nbands = 3;
233 if (reslevel->coord[0][1] == reslevel->coord[0][0])
234 reslevel->num_precincts_x = 0;
236 reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
237 - (reslevel->coord[0][0] >> codsty->log2_prec_width);
239 if (reslevel->coord[1][1] == reslevel->coord[1][0])
240 reslevel->num_precincts_y = 0;
242 reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
243 - (reslevel->coord[1][0] >> codsty->log2_prec_height);
245 reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
247 return AVERROR(ENOMEM);
248 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
249 J2kBand *band = reslevel->band + bandno;
250 int cblkno, precx, precy, precno;
252 int xi0, yi0, xi1, yi1;
253 int cblkperprecw, cblkperprech;
255 if (qntsty->quantsty != J2K_QSTY_NONE){
256 const static uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
259 numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
260 band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
262 band->stepsize = 1 << 13;
264 if (reslevelno == 0){ // the same everywhere
265 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
266 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
267 for (i = 0; i < 2; i++)
268 for (j = 0; j < 2; j++)
269 band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
271 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
272 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
274 for (i = 0; i < 2; i++)
275 for (j = 0; j < 2; j++)
276 band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
278 band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width) - band->coord[0][0] / band->codeblock_width;
279 band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
281 for (j = 0; j < 2; j++)
282 band->coord[0][j] = ff_j2k_ceildiv(band->coord[0][j], dx);
283 for (j = 0; j < 2; j++)
284 band->coord[1][j] = ff_j2k_ceildiv(band->coord[1][j], dy);
286 band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
287 band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
289 band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
291 return AVERROR(ENOMEM);
292 band->prec = av_malloc(reslevel->num_precincts_x * reslevel->num_precincts_y * sizeof(J2kPrec));
294 return AVERROR(ENOMEM);
296 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
297 J2kCblk *cblk = band->cblk + cblkno;
305 y0 = band->coord[1][0];
306 y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
308 yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
309 yi1 = FFMIN(yi1, band->cblkny);
310 cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
311 for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
312 for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
313 band->prec[precno].yi0 = yi0;
314 band->prec[precno].yi1 = yi1;
317 yi0 = yi1 - cblkperprech;
318 yi1 = FFMIN(yi1, band->cblkny);
320 x0 = band->coord[0][0];
321 x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
323 xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
324 xi1 = FFMIN(xi1, band->cblknx);
326 cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
327 for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
328 for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
329 J2kPrec *prec = band->prec + precno;
332 prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
333 prec->yi1 - prec->yi0);
334 prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
335 prec->yi1 - prec->yi0);
336 if (!prec->cblkincl || !prec->zerobits)
337 return AVERROR(ENOMEM);
341 xi0 = xi1 - cblkperprecw;
342 xi1 = FFMIN(xi1, band->cblknx);
349 void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
351 int reslevelno, bandno, cblkno, precno;
352 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
353 J2kResLevel *rlevel = comp->reslevel + reslevelno;
354 for (bandno = 0; bandno < rlevel->nbands; bandno++){
355 J2kBand *band = rlevel->band + bandno;
356 for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
357 J2kPrec *prec = band->prec + precno;
358 tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
359 tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
361 for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
362 J2kCblk *cblk = band->cblk + cblkno;
370 void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
372 int reslevelno, bandno, precno;
373 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
374 J2kResLevel *reslevel = comp->reslevel + reslevelno;
376 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
377 J2kBand *band = reslevel->band + bandno;
378 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
379 J2kPrec *prec = band->prec + precno;
380 av_freep(&prec->zerobits);
381 av_freep(&prec->cblkincl);
383 av_freep(&band->cblk);
384 av_freep(&band->prec);
386 av_freep(&reslevel->band);
389 ff_j2k_dwt_destroy(&comp->dwt);
390 av_freep(&comp->reslevel);
391 av_freep(&comp->data);