FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
j2k.c
Go to the documentation of this file.
1 /*
2  * JPEG2000 encoder and decoder common functions
3  * Copyright (c) 2007 Kamil Nowosad
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * JPEG2000 image encoder and decoder common functions
24  * @file
25  * @author Kamil Nowosad
26  */
27 
28 
29 #include "avcodec.h"
30 #include "j2k.h"
31 
32 #define SHL(a, n) ((n)>=0 ? (a) << (n) : (a) >> -(n))
33 
34 #if 0
35 void ff_j2k_printv(int *tab, int l)
36 {
37  int i;
38  for (i = 0; i < l; i++)
39  av_log(NULL, AV_LOG_DEBUG, "%.3d ", tab[i]);
40  av_log(NULL, AV_LOG_DEBUG, "\n");
41 }
42 
43 void ff_j2k_printu(uint8_t *tab, int l)
44 {
45  int i;
46  for (i = 0; i < l; i++)
47  av_log(NULL, AV_LOG_DEBUG, "%.3hd ", tab[i]);
48  av_log(NULL, AV_LOG_DEBUG, "\n");
49 }
50 #endif
51 
52 /* tag tree routines */
53 
54 /** allocate the memory for tag tree */
55 
56 static int tag_tree_size(int w, int h)
57 {
58  int res = 0;
59  while (w > 1 || h > 1){
60  res += w * h;
61  w = (w+1) >> 1;
62  h = (h+1) >> 1;
63  }
64  return res + 1;
65 }
66 
68 {
69  int pw = w, ph = h;
70  J2kTgtNode *res, *t, *t2;
71 
72  t = res = av_mallocz(tag_tree_size(w, h)*sizeof(J2kTgtNode));
73 
74  if (res == NULL)
75  return NULL;
76 
77  while (w > 1 || h > 1){
78  int i, j;
79  pw = w;
80  ph = h;
81 
82  w = (w+1) >> 1;
83  h = (h+1) >> 1;
84  t2 = t + pw*ph;
85 
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)];
89  }
90  t = t2;
91  }
92  t[0].parent = NULL;
93  return res;
94 }
95 
96 static void tag_tree_zero(J2kTgtNode *t, int w, int h)
97 {
98  int i, siz = tag_tree_size(w, h);
99 
100  for (i = 0; i < siz; i++){
101  t[i].val = 0;
102  t[i].vis = 0;
103  }
104 }
105 
107 
108 static int getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
109 {
110  int h, v, d;
111 
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);
122  if (bandno < 3){
123  if (bandno == 1)
124  FFSWAP(int, h, v);
125  if (h == 2) return 8;
126  if (h == 1){
127  if (v >= 1) return 7;
128  if (d >= 1) return 6;
129  return 5;
130  }
131  if (v == 2) return 4;
132  if (v == 1) return 3;
133  if (d >= 2) return 2;
134  if (d == 1) return 1;
135  return 0;
136  } else{
137  if (d >= 3) return 8;
138  if (d == 2){
139  if (h+v >= 1) return 7;
140  return 6;
141  }
142  if (d == 1){
143  if (h+v >= 2) return 5;
144  if (h+v == 1) return 4;
145  return 3;
146  }
147  if (h+v >= 2) return 2;
148  if (h+v == 1) return 1;
149  return 0;
150  }
151 }
152 
154 
155 static int getsgnctxno(int flag, uint8_t *xorbit)
156 {
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}};
161 
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];
168 }
169 
171 {
172  int i, j;
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]);
179 }
180 
181 void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
182 {
183  x++; y++;
184  t1->flags[y][x] |= J2K_T1_SIG;
185  if (negative){
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;
190  } else{
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;
195  }
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;
200 }
201 
202 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
203 {
204  int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
205 
206  if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
207  return ret;
208  for (i = 0; i < 2; i++)
209  csize *= comp->coord[i][1] - comp->coord[i][0];
210 
211  comp->data = av_malloc(csize * sizeof(int));
212  if (!comp->data)
213  return AVERROR(ENOMEM);
214  comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
215 
216  if (!comp->reslevel)
217  return AVERROR(ENOMEM);
218  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
219  int declvl = codsty->nreslevels - reslevelno;
220  J2kResLevel *reslevel = comp->reslevel + reslevelno;
221 
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);
226 
227  if (reslevelno == 0)
228  reslevel->nbands = 1;
229  else
230  reslevel->nbands = 3;
231 
232  if (reslevel->coord[0][1] == reslevel->coord[0][0])
233  reslevel->num_precincts_x = 0;
234  else
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);
237 
238  if (reslevel->coord[1][1] == reslevel->coord[1][0])
239  reslevel->num_precincts_y = 0;
240  else
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);
243 
244  reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
245  if (!reslevel->band)
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;
250  int x0, y0, x1, y1;
251  int xi0, yi0, xi1, yi1;
252  int cblkperprecw, cblkperprech;
253 
254  if (qntsty->quantsty != J2K_QSTY_NONE){
255  static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
256  int numbps;
257 
258  numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
259  band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
260  } else
261  band->stepsize = 1 << 13;
262 
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);
269  } else{
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);
272 
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);
276  }
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;
279 
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);
284 
285  band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
286  band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
287 
288  band->cblk = av_malloc(sizeof(J2kCblk) * band->cblknx * band->cblkny);
289  if (!band->cblk)
290  return AVERROR(ENOMEM);
291  band->prec = av_malloc(sizeof(J2kCblk) * reslevel->num_precincts_x * reslevel->num_precincts_y);
292  if (!band->prec)
293  return AVERROR(ENOMEM);
294 
295  for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
296  J2kCblk *cblk = band->cblk + cblkno;
297  cblk->zero = 0;
298  cblk->lblock = 3;
299  cblk->length = 0;
300  cblk->lengthinc = 0;
301  cblk->npasses = 0;
302  }
303 
304  y0 = band->coord[1][0];
305  y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
306  yi0 = 0;
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;
314  }
315  yi1 += cblkperprech;
316  yi0 = yi1 - cblkperprech;
317  yi1 = FFMIN(yi1, band->cblkny);
318  }
319  x0 = band->coord[0][0];
320  x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
321  xi0 = 0;
322  xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
323  xi1 = FFMIN(xi1, band->cblknx);
324 
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;
329  prec->xi0 = xi0;
330  prec->xi1 = xi1;
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);
337 
338  }
339  xi1 += cblkperprecw;
340  xi0 = xi1 - cblkperprecw;
341  xi1 = FFMIN(xi1, band->cblknx);
342  }
343  }
344  }
345  return 0;
346 }
347 
349 {
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);
359  }
360  for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
361  J2kCblk *cblk = band->cblk + cblkno;
362  cblk->length = 0;
363  cblk->lblock = 3;
364  }
365  }
366  }
367 }
368 
370 {
371  int reslevelno, bandno, precno;
372  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
373  J2kResLevel *reslevel = comp->reslevel + reslevelno;
374 
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);
381  }
382  av_freep(&band->cblk);
383  av_freep(&band->prec);
384  }
385  av_freep(&reslevel->band);
386  }
387 
388  ff_j2k_dwt_destroy(&comp->dwt);
389  av_freep(&comp->reslevel);
390  av_freep(&comp->data);
391 }