FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
jpeg2000.c
Go to the documentation of this file.
1 /*
2  * JPEG 2000 encoder and decoder common functions
3  * Copyright (c) 2007 Kamil Nowosad
4  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /**
24  * @file
25  * JPEG 2000 image encoder and decoder common functions
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mem.h"
31 #include "avcodec.h"
32 #include "jpeg2000.h"
33 
34 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
35 
36 /* tag tree routines */
37 
38 /* allocate the memory for tag tree */
39 static int32_t tag_tree_size(uint16_t w, uint16_t h)
40 {
41  uint32_t res = 0;
42  while (w > 1 || h > 1) {
43  res += w * h;
44  av_assert0(res + 1 < INT32_MAX);
45  w = (w + 1) >> 1;
46  h = (h + 1) >> 1;
47  }
48  return (int32_t)(res + 1);
49 }
50 
52 {
53  int pw = w, ph = h;
54  Jpeg2000TgtNode *res, *t, *t2;
55  int32_t tt_size;
56 
57  tt_size = tag_tree_size(w, h);
58 
59  t = res = av_mallocz_array(tt_size, sizeof(*t));
60  if (!res)
61  return NULL;
62 
63  while (w > 1 || h > 1) {
64  int i, j;
65  pw = w;
66  ph = h;
67 
68  w = (w + 1) >> 1;
69  h = (h + 1) >> 1;
70  t2 = t + pw * ph;
71 
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)];
75 
76  t = t2;
77  }
78  t[0].parent = NULL;
79  return res;
80 }
81 
82 static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
83 {
84  int i, siz = tag_tree_size(w, h);
85 
86  for (i = 0; i < siz; i++) {
87  t[i].val = 0;
88  t[i].vis = 0;
89  }
90 }
91 
93 
94 static int getsigctxno(int flag, int bandno)
95 {
96  int h, v, d;
97 
98  h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
99  ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
100  v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
101  ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
102  d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
103  ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
104  ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
105  ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
106 
107  if (bandno < 3) {
108  if (bandno == 1)
109  FFSWAP(int, h, v);
110  if (h == 2) return 8;
111  if (h == 1) {
112  if (v >= 1) return 7;
113  if (d >= 1) return 6;
114  return 5;
115  }
116  if (v == 2) return 4;
117  if (v == 1) return 3;
118  if (d >= 2) return 2;
119  if (d == 1) return 1;
120  } else {
121  if (d >= 3) return 8;
122  if (d == 2) {
123  if (h+v >= 1) return 7;
124  return 6;
125  }
126  if (d == 1) {
127  if (h+v >= 2) return 5;
128  if (h+v == 1) return 4;
129  return 3;
130  }
131  if (h+v >= 2) return 2;
132  if (h+v == 1) return 1;
133  }
134  return 0;
135 }
136 
138 
139 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
140 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
141 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
142 
143 static int getsgnctxno(int flag, uint8_t *xorbit)
144 {
145  int vcontrib, hcontrib;
146 
147  hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
148  [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
149  vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
150  [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
151  *xorbit = xorbittab[hcontrib][vcontrib];
152 
153  return ctxlbltab[hcontrib][vcontrib];
154 }
155 
157 {
158  int i, j;
159  for (i = 0; i < 256; i++)
160  for (j = 0; j < 4; j++)
162  for (i = 0; i < 16; i++)
163  for (j = 0; j < 16; j++)
165  getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
166 }
167 
169  int negative)
170 {
171  x++;
172  y++;
173  t1->flags[y][x] |= JPEG2000_T1_SIG;
174  if (negative) {
175  t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
176  t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
177  t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
178  t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
179  } else {
180  t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
181  t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
182  t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
183  t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
184  }
185  t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
186  t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
187  t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
188  t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
189 }
190 
191 static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
192 
194  Jpeg2000CodingStyle *codsty,
195  Jpeg2000QuantStyle *qntsty,
196  int cbps, int dx, int dy,
197  AVCodecContext *avctx)
198 {
199  uint8_t log2_band_prec_width, log2_band_prec_height;
200  int reslevelno, bandno, gbandno = 0, ret, i, j;
201  uint32_t csize;
202 
203  if (codsty->nreslevels2decode <= 0) {
204  av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
205  return AVERROR_INVALIDDATA;
206  }
207 
208  if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
209  codsty->nreslevels2decode - 1,
210  codsty->transform))
211  return ret;
212  // component size comp->coord is uint16_t so ir cannot overflow
213  csize = (comp->coord[0][1] - comp->coord[0][0]) *
214  (comp->coord[1][1] - comp->coord[1][0]);
215 
216  if (codsty->transform == FF_DWT97) {
217  comp->i_data = NULL;
218  comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
219  if (!comp->f_data)
220  return AVERROR(ENOMEM);
221  } else {
222  comp->f_data = NULL;
223  comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
224  if (!comp->i_data)
225  return AVERROR(ENOMEM);
226  }
227  comp->reslevel = av_calloc(codsty->nreslevels, sizeof(*comp->reslevel));
228  if (!comp->reslevel)
229  return AVERROR(ENOMEM);
230  /* LOOP on resolution levels */
231  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
232  int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
233  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
234 
235  /* Compute borders for each resolution level.
236  * Computation of trx_0, trx_1, try_0 and try_1.
237  * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
238  for (i = 0; i < 2; i++)
239  for (j = 0; j < 2; j++)
240  reslevel->coord[i][j] =
241  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
242  // update precincts size: 2^n value
243  reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
244  reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
245 
246  /* Number of bands for each resolution level */
247  if (reslevelno == 0)
248  reslevel->nbands = 1;
249  else
250  reslevel->nbands = 3;
251 
252  /* Number of precincts wich span the tile for resolution level reslevelno
253  * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
254  * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
255  * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
256  * for Dcinema profiles in JPEG 2000
257  * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
258  * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
259  if (reslevel->coord[0][1] == reslevel->coord[0][0])
260  reslevel->num_precincts_x = 0;
261  else
262  reslevel->num_precincts_x =
263  ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
264  reslevel->log2_prec_width) -
265  (reslevel->coord[0][0] >> reslevel->log2_prec_width);
266 
267  if (reslevel->coord[1][1] == reslevel->coord[1][0])
268  reslevel->num_precincts_y = 0;
269  else
270  reslevel->num_precincts_y =
271  ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
272  reslevel->log2_prec_height) -
273  (reslevel->coord[1][0] >> reslevel->log2_prec_height);
274 
275  reslevel->band = av_calloc(reslevel->nbands, sizeof(*reslevel->band));
276  if (!reslevel->band)
277  return AVERROR(ENOMEM);
278 
279  for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
280  Jpeg2000Band *band = reslevel->band + bandno;
281  int cblkno, precno;
282  int nb_precincts;
283 
284  /* TODO: Implementation of quantization step not finished,
285  * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
286  switch (qntsty->quantsty) {
287  uint8_t gain;
288  int numbps;
289  case JPEG2000_QSTY_NONE:
290  /* TODO: to verify. No quantization in this case */
291  band->f_stepsize = 1;
292  break;
293  case JPEG2000_QSTY_SI:
294  /*TODO: Compute formula to implement. */
295  numbps = cbps +
296  lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
297  band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
298  2 + numbps - qntsty->expn[gbandno]);
299  break;
300  case JPEG2000_QSTY_SE:
301  /* Exponent quantization step.
302  * Formula:
303  * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
304  * R_b = R_I + log2 (gain_b )
305  * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
306  /* TODO/WARN: value of log2 (gain_b ) not taken into account
307  * but it works (compared to OpenJPEG). Why?
308  * Further investigation needed. */
309  gain = cbps;
310  band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
311  band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
312  break;
313  default:
314  band->f_stepsize = 0;
315  av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
316  break;
317  }
318  /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
319  * If not set output of entropic decoder is not correct. */
320  if (!av_codec_is_encoder(avctx->codec))
321  band->f_stepsize *= 0.5;
322 
323  band->i_stepsize = band->f_stepsize * (1 << 15);
324 
325  /* computation of tbx_0, tbx_1, tby_0, tby_1
326  * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
327  * codeblock width and height is computed for
328  * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
329  if (reslevelno == 0) {
330  /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
331  for (i = 0; i < 2; i++)
332  for (j = 0; j < 2; j++)
333  band->coord[i][j] =
334  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0],
335  declvl - 1);
336  log2_band_prec_width = reslevel->log2_prec_width;
337  log2_band_prec_height = reslevel->log2_prec_height;
338  /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
339  band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
340  reslevel->log2_prec_width);
341  band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
342  reslevel->log2_prec_height);
343  } else {
344  /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
345  /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
346  for (i = 0; i < 2; i++)
347  for (j = 0; j < 2; j++)
348  /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
349  band->coord[i][j] =
350  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] -
351  (((bandno + 1 >> i) & 1) << declvl - 1),
352  declvl);
353  /* TODO: Manage case of 3 band offsets here or
354  * in coding/decoding function? */
355 
356  /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
357  band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
358  reslevel->log2_prec_width - 1);
359  band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
360  reslevel->log2_prec_height - 1);
361 
362  log2_band_prec_width = reslevel->log2_prec_width - 1;
363  log2_band_prec_height = reslevel->log2_prec_height - 1;
364  }
365 
366  for (j = 0; j < 2; j++)
367  band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
368  for (j = 0; j < 2; j++)
369  band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
370 
371  band->prec = av_calloc(reslevel->num_precincts_x *
372  (uint64_t)reslevel->num_precincts_y,
373  sizeof(*band->prec));
374  if (!band->prec)
375  return AVERROR(ENOMEM);
376 
377  nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
378 
379  for (precno = 0; precno < nb_precincts; precno++) {
380  Jpeg2000Prec *prec = band->prec + precno;
381 
382  /* TODO: Explain formula for JPEG200 DCINEMA. */
383  /* TODO: Verify with previous count of codeblocks per band */
384 
385  /* Compute P_x0 */
386  prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
387  (1 << log2_band_prec_width);
388  prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
389 
390  /* Compute P_y0 */
391  prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
392  (1 << log2_band_prec_height);
393  prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
394 
395  /* Compute P_x1 */
396  prec->coord[0][1] = prec->coord[0][0] +
397  (1 << log2_band_prec_width);
398  prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
399 
400  /* Compute P_y1 */
401  prec->coord[1][1] = prec->coord[1][0] +
402  (1 << log2_band_prec_height);
403  prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
404 
405  prec->nb_codeblocks_width =
406  ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
407  prec->coord[0][0],
408  band->log2_cblk_width);
409  prec->nb_codeblocks_height =
410  ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
411  prec->coord[1][0],
412  band->log2_cblk_height);
413 
414  /* Tag trees initialization */
415  prec->cblkincl =
417  prec->nb_codeblocks_height);
418  if (!prec->cblkincl)
419  return AVERROR(ENOMEM);
420 
421  prec->zerobits =
423  prec->nb_codeblocks_height);
424  if (!prec->zerobits)
425  return AVERROR(ENOMEM);
426 
427  prec->cblk = av_mallocz_array(prec->nb_codeblocks_width *
428  (uint64_t)prec->nb_codeblocks_height,
429  sizeof(*prec->cblk));
430  if (!prec->cblk)
431  return AVERROR(ENOMEM);
432  for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
433  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
434  uint16_t Cx0, Cy0;
435 
436  /* Compute coordinates of codeblocks */
437  /* Compute Cx0*/
438  Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
439  Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
440  cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
441 
442  /* Compute Cy0*/
443  Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
444  Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
445  cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
446 
447  /* Compute Cx1 */
448  cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
449  prec->coord[0][1]);
450 
451  /* Compute Cy1 */
452  cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
453  prec->coord[1][1]);
454  /* Update code-blocks coordinates according sub-band position */
455  if ((bandno + !!reslevelno) & 1) {
456  cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
457  comp->reslevel[reslevelno-1].coord[0][0];
458  cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
459  comp->reslevel[reslevelno-1].coord[0][0];
460  }
461  if ((bandno + !!reslevelno) & 2) {
462  cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
463  comp->reslevel[reslevelno-1].coord[1][0];
464  cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
465  comp->reslevel[reslevelno-1].coord[1][0];
466  }
467 
468  cblk->zero = 0;
469  cblk->lblock = 3;
470  cblk->length = 0;
471  cblk->lengthinc = 0;
472  cblk->npasses = 0;
473  }
474  }
475  }
476  }
477  return 0;
478 }
479 
481 {
482  int reslevelno, bandno, cblkno, precno;
483  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
484  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
485  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
486  Jpeg2000Band *band = rlevel->band + bandno;
487  for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
488  Jpeg2000Prec *prec = band->prec + precno;
491  for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
492  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
493  cblk->length = 0;
494  cblk->lblock = 3;
495  }
496  }
497  }
498  }
499 }
500 
502 {
503  int reslevelno, bandno, precno;
504  for (reslevelno = 0;
505  comp->reslevel && reslevelno < codsty->nreslevels;
506  reslevelno++) {
507  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
508 
509  for (bandno = 0; bandno < reslevel->nbands; bandno++) {
510  if (reslevel->band) {
511  Jpeg2000Band *band = reslevel->band + bandno;
512  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
513  if (band->prec) {
514  Jpeg2000Prec *prec = band->prec + precno;
515  av_freep(&prec->zerobits);
516  av_freep(&prec->cblkincl);
517  av_freep(&prec->cblk);
518  }
519  }
520 
521  av_freep(&band->prec);
522  }
523  }
524  av_freep(&reslevel->band);
525  }
526 
527  ff_dwt_destroy(&comp->dwt);
528  av_freep(&comp->reslevel);
529  av_freep(&comp->i_data);
530  av_freep(&comp->f_data);
531 }