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/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/mem.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "jpeg2000.h"
36 
37 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
38 
39 /* tag tree routines */
40 
41 /* allocate the memory for tag tree */
42 static int32_t tag_tree_size(int w, int h)
43 {
44  int64_t res = 0;
45  while (w > 1 || h > 1) {
46  res += w * (int64_t)h;
47  av_assert0(res + 1 < INT32_MAX);
48  w = (w + 1) >> 1;
49  h = (h + 1) >> 1;
50  }
51  return (int32_t)(res + 1);
52 }
53 
55 {
56  int pw = w, ph = h;
57  Jpeg2000TgtNode *res, *t, *t2;
58  int32_t tt_size;
59 
60  tt_size = tag_tree_size(w, h);
61 
62  t = res = av_mallocz_array(tt_size, sizeof(*t));
63  if (!res)
64  return NULL;
65 
66  while (w > 1 || h > 1) {
67  int i, j;
68  pw = w;
69  ph = h;
70 
71  w = (w + 1) >> 1;
72  h = (h + 1) >> 1;
73  t2 = t + pw * ph;
74 
75  for (i = 0; i < ph; i++)
76  for (j = 0; j < pw; j++)
77  t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
78 
79  t = t2;
80  }
81  t[0].parent = NULL;
82  return res;
83 }
84 
85 static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
86 {
87  int i, siz = tag_tree_size(w, h);
88 
89  for (i = 0; i < siz; i++) {
90  t[i].val = 0;
91  t[i].vis = 0;
92  }
93 }
94 
96 
97 static int getsigctxno(int flag, int bandno)
98 {
99  int h, v, d;
100 
101  h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
102  ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
103  v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
104  ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
105  d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
106  ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
107  ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
108  ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
109 
110  if (bandno < 3) {
111  if (bandno == 1)
112  FFSWAP(int, h, v);
113  if (h == 2) return 8;
114  if (h == 1) {
115  if (v >= 1) return 7;
116  if (d >= 1) return 6;
117  return 5;
118  }
119  if (v == 2) return 4;
120  if (v == 1) return 3;
121  if (d >= 2) return 2;
122  if (d == 1) return 1;
123  } else {
124  if (d >= 3) return 8;
125  if (d == 2) {
126  if (h+v >= 1) return 7;
127  return 6;
128  }
129  if (d == 1) {
130  if (h+v >= 2) return 5;
131  if (h+v == 1) return 4;
132  return 3;
133  }
134  if (h+v >= 2) return 2;
135  if (h+v == 1) return 1;
136  }
137  return 0;
138 }
139 
141 
142 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
143 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
144 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
145 
146 static int getsgnctxno(int flag, uint8_t *xorbit)
147 {
148  int vcontrib, hcontrib;
149 
150  hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
151  [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
152  vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
153  [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
154  *xorbit = xorbittab[hcontrib][vcontrib];
155 
156  return ctxlbltab[hcontrib][vcontrib];
157 }
158 
160 {
161  int i, j;
162  for (i = 0; i < 256; i++)
163  for (j = 0; j < 4; j++)
165  for (i = 0; i < 16; i++)
166  for (j = 0; j < 16; j++)
168  getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
169 }
170 
172  int negative)
173 {
174  x++;
175  y++;
176  t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
177  if (negative) {
178  t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
179  t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
180  t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
181  t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
182  } else {
183  t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
184  t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
185  t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
186  t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
187  }
188  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
189  t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
190  t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
191  t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
192 }
193 
194 // static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
195 
198  Jpeg2000CodingStyle *codsty,
199  Jpeg2000QuantStyle *qntsty,
200  int bandno, int gbandno, int reslevelno,
201  int cbps)
202 {
203  /* TODO: Implementation of quantization step not finished,
204  * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
205  switch (qntsty->quantsty) {
206  uint8_t gain;
207  case JPEG2000_QSTY_NONE:
208  /* TODO: to verify. No quantization in this case */
209  band->f_stepsize = 1;
210  break;
211  case JPEG2000_QSTY_SI:
212  /*TODO: Compute formula to implement. */
213 // numbps = cbps +
214 // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
215 // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
216 // 2 + numbps - qntsty->expn[gbandno]);
217 // break;
218  case JPEG2000_QSTY_SE:
219  /* Exponent quantization step.
220  * Formula:
221  * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
222  * R_b = R_I + log2 (gain_b )
223  * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
224  gain = cbps;
225  band->f_stepsize = ff_exp2fi(gain - qntsty->expn[gbandno]);
226  band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
227  break;
228  default:
229  band->f_stepsize = 0;
230  av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
231  break;
232  }
233  if (codsty->transform != FF_DWT53) {
234  int lband = 0;
235  switch (bandno + (reslevelno > 0)) {
236  case 1:
237  case 2:
238  band->f_stepsize *= F_LFTG_X * 2;
239  lband = 1;
240  break;
241  case 3:
242  band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
243  break;
244  }
245  if (codsty->transform == FF_DWT97) {
246  band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
247  }
248  }
249 
250  band->i_stepsize = band->f_stepsize * (1 << 15);
251 
252  /* FIXME: In OpenJPEG code stepsize = stepsize * 0.5. Why?
253  * If not set output of entropic decoder is not correct. */
254  if (!av_codec_is_encoder(avctx->codec))
255  band->f_stepsize *= 0.5;
256 }
257 
259  Jpeg2000ResLevel *reslevel,
261  int precno, int bandno, int reslevelno,
262  int log2_band_prec_width,
263  int log2_band_prec_height)
264 {
265  Jpeg2000Prec *prec = band->prec + precno;
266  int nb_codeblocks, cblkno;
267 
268  prec->decoded_layers = 0;
269 
270  /* TODO: Explain formula for JPEG200 DCINEMA. */
271  /* TODO: Verify with previous count of codeblocks per band */
272 
273  /* Compute P_x0 */
274  prec->coord[0][0] = ((band->coord[0][0] >> log2_band_prec_width) + precno % reslevel->num_precincts_x) *
275  (1 << log2_band_prec_width);
276 
277  /* Compute P_y0 */
278  prec->coord[1][0] = ((band->coord[1][0] >> log2_band_prec_height) + precno / reslevel->num_precincts_x) *
279  (1 << log2_band_prec_height);
280 
281  /* Compute P_x1 */
282  prec->coord[0][1] = prec->coord[0][0] +
283  (1 << log2_band_prec_width);
284  prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
285  prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
286 
287  /* Compute P_y1 */
288  prec->coord[1][1] = prec->coord[1][0] +
289  (1 << log2_band_prec_height);
290  prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
291  prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
292 
293  prec->nb_codeblocks_width =
294  ff_jpeg2000_ceildivpow2(prec->coord[0][1],
295  band->log2_cblk_width)
296  - (prec->coord[0][0] >> band->log2_cblk_width);
297  prec->nb_codeblocks_height =
298  ff_jpeg2000_ceildivpow2(prec->coord[1][1],
299  band->log2_cblk_height)
300  - (prec->coord[1][0] >> band->log2_cblk_height);
301 
302 
303  /* Tag trees initialization */
304  prec->cblkincl =
306  prec->nb_codeblocks_height);
307  if (!prec->cblkincl)
308  return AVERROR(ENOMEM);
309 
310  prec->zerobits =
312  prec->nb_codeblocks_height);
313  if (!prec->zerobits)
314  return AVERROR(ENOMEM);
315 
316  if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
317  prec->cblk = NULL;
318  return AVERROR(ENOMEM);
319  }
320  nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
321  prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
322  if (!prec->cblk)
323  return AVERROR(ENOMEM);
324  for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
325  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
326  int Cx0, Cy0;
327 
328  /* Compute coordinates of codeblocks */
329  /* Compute Cx0*/
330  Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
331  Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
332  cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
333 
334  /* Compute Cy0*/
335  Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
336  Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
337  cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
338 
339  /* Compute Cx1 */
340  cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
341  prec->coord[0][1]);
342 
343  /* Compute Cy1 */
344  cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
345  prec->coord[1][1]);
346  /* Update code-blocks coordinates according sub-band position */
347  if ((bandno + !!reslevelno) & 1) {
348  cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
349  comp->reslevel[reslevelno-1].coord[0][0];
350  cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
351  comp->reslevel[reslevelno-1].coord[0][0];
352  }
353  if ((bandno + !!reslevelno) & 2) {
354  cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
355  comp->reslevel[reslevelno-1].coord[1][0];
356  cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
357  comp->reslevel[reslevelno-1].coord[1][0];
358  }
359 
360  cblk->zero = 0;
361  cblk->lblock = 3;
362  cblk->length = 0;
363  memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
364  cblk->npasses = 0;
365  }
366 
367  return 0;
368 }
369 
370 static int init_band(AVCodecContext *avctx,
371  Jpeg2000ResLevel *reslevel,
373  Jpeg2000CodingStyle *codsty,
374  Jpeg2000QuantStyle *qntsty,
375  int bandno, int gbandno, int reslevelno,
376  int cbps, int dx, int dy)
377 {
378  Jpeg2000Band *band = reslevel->band + bandno;
379  uint8_t log2_band_prec_width, log2_band_prec_height;
380  int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
381  int precno;
382  int nb_precincts;
383  int i, j, ret;
384 
385  init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
386 
387  /* computation of tbx_0, tbx_1, tby_0, tby_1
388  * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
389  * codeblock width and height is computed for
390  * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
391  if (reslevelno == 0) {
392  /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
393  for (i = 0; i < 2; i++)
394  for (j = 0; j < 2; j++)
395  band->coord[i][j] =
396  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
397  declvl - 1);
398  log2_band_prec_width = reslevel->log2_prec_width;
399  log2_band_prec_height = reslevel->log2_prec_height;
400  /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
401  band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
402  reslevel->log2_prec_width);
403  band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
404  reslevel->log2_prec_height);
405  } else {
406  /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
407  /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
408  for (i = 0; i < 2; i++)
409  for (j = 0; j < 2; j++)
410  /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
411  band->coord[i][j] =
412  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
413  (((bandno + 1 >> i) & 1LL) << declvl - 1),
414  declvl);
415  /* TODO: Manage case of 3 band offsets here or
416  * in coding/decoding function? */
417 
418  /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
419  band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
420  reslevel->log2_prec_width - 1);
421  band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
422  reslevel->log2_prec_height - 1);
423 
424  log2_band_prec_width = reslevel->log2_prec_width - 1;
425  log2_band_prec_height = reslevel->log2_prec_height - 1;
426  }
427 
428  if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
429  band->prec = NULL;
430  return AVERROR(ENOMEM);
431  }
432  nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
433  band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
434  if (!band->prec)
435  return AVERROR(ENOMEM);
436 
437  for (precno = 0; precno < nb_precincts; precno++) {
438  ret = init_prec(band, reslevel, comp,
439  precno, bandno, reslevelno,
440  log2_band_prec_width, log2_band_prec_height);
441  if (ret < 0)
442  return ret;
443  }
444 
445  return 0;
446 }
447 
449  Jpeg2000CodingStyle *codsty,
450  Jpeg2000QuantStyle *qntsty,
451  int cbps, int dx, int dy,
452  AVCodecContext *avctx)
453 {
454  int reslevelno, bandno, gbandno = 0, ret, i, j;
455  uint32_t csize;
456 
457  if (codsty->nreslevels2decode <= 0) {
458  av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
459  return AVERROR_INVALIDDATA;
460  }
461 
462  if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
463  codsty->nreslevels2decode - 1,
464  codsty->transform))
465  return ret;
466 
467  if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
468  comp->coord[1][1] - comp->coord[1][0], 0, avctx))
469  return AVERROR_INVALIDDATA;
470  csize = (comp->coord[0][1] - comp->coord[0][0]) *
471  (comp->coord[1][1] - comp->coord[1][0]);
472  if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
473  comp->coord[1][1] - comp->coord[1][0] > 32768) {
474  av_log(avctx, AV_LOG_ERROR, "component size too large\n");
475  return AVERROR_PATCHWELCOME;
476  }
477 
478  if (codsty->transform == FF_DWT97) {
479  csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
480  comp->i_data = NULL;
481  comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
482  if (!comp->f_data)
483  return AVERROR(ENOMEM);
484  } else {
485  csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
486  comp->f_data = NULL;
487  comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
488  if (!comp->i_data)
489  return AVERROR(ENOMEM);
490  }
491  comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
492  if (!comp->reslevel)
493  return AVERROR(ENOMEM);
494  /* LOOP on resolution levels */
495  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
496  int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
497  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
498 
499  /* Compute borders for each resolution level.
500  * Computation of trx_0, trx_1, try_0 and try_1.
501  * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
502  for (i = 0; i < 2; i++)
503  for (j = 0; j < 2; j++)
504  reslevel->coord[i][j] =
505  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
506  // update precincts size: 2^n value
507  reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
508  reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
509 
510  /* Number of bands for each resolution level */
511  if (reslevelno == 0)
512  reslevel->nbands = 1;
513  else
514  reslevel->nbands = 3;
515 
516  /* Number of precincts which span the tile for resolution level reslevelno
517  * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
518  * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
519  * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
520  * for Dcinema profiles in JPEG 2000
521  * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
522  * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
523  if (reslevel->coord[0][1] == reslevel->coord[0][0])
524  reslevel->num_precincts_x = 0;
525  else
526  reslevel->num_precincts_x =
527  ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
528  reslevel->log2_prec_width) -
529  (reslevel->coord[0][0] >> reslevel->log2_prec_width);
530 
531  if (reslevel->coord[1][1] == reslevel->coord[1][0])
532  reslevel->num_precincts_y = 0;
533  else
534  reslevel->num_precincts_y =
535  ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
536  reslevel->log2_prec_height) -
537  (reslevel->coord[1][0] >> reslevel->log2_prec_height);
538 
539  reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
540  if (!reslevel->band)
541  return AVERROR(ENOMEM);
542 
543  for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
544  ret = init_band(avctx, reslevel,
545  comp, codsty, qntsty,
546  bandno, gbandno, reslevelno,
547  cbps, dx, dy);
548  if (ret < 0)
549  return ret;
550  }
551  }
552  return 0;
553 }
554 
556 {
557  int reslevelno, bandno, cblkno, precno;
558  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
559  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
560  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
561  Jpeg2000Band *band = rlevel->band + bandno;
562  for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
563  Jpeg2000Prec *prec = band->prec + precno;
566  for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
567  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
568  cblk->length = 0;
569  cblk->lblock = 3;
570  }
571  }
572  }
573  }
574 }
575 
577 {
578  int reslevelno, bandno, precno;
579  for (reslevelno = 0;
580  comp->reslevel && reslevelno < codsty->nreslevels;
581  reslevelno++) {
582  Jpeg2000ResLevel *reslevel;
583 
584  if (!comp->reslevel)
585  continue;
586 
587  reslevel = comp->reslevel + reslevelno;
588  for (bandno = 0; bandno < reslevel->nbands; bandno++) {
590 
591  if (!reslevel->band)
592  continue;
593 
594  band = reslevel->band + bandno;
595  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
596  if (band->prec) {
597  Jpeg2000Prec *prec = band->prec + precno;
598  av_freep(&prec->zerobits);
599  av_freep(&prec->cblkincl);
600  av_freep(&prec->cblk);
601  }
602  }
603 
604  av_freep(&band->prec);
605  }
606  av_freep(&reslevel->band);
607  }
608 
609  ff_dwt_destroy(&comp->dwt);
610  av_freep(&comp->reslevel);
611  av_freep(&comp->i_data);
612  av_freep(&comp->f_data);
613 }
uint8_t ff_jpeg2000_sgnctxno_lut[16][16]
Definition: jpeg2000.c:140
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1684
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:159
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:576
DWTContext dwt
Definition: jpeg2000.h:208
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:184
static int init_prec(Jpeg2000Band *band, Jpeg2000ResLevel *reslevel, Jpeg2000Component *comp, int precno, int bandno, int reslevelno, int log2_band_prec_width, int log2_band_prec_height)
Definition: jpeg2000.c:258
misc image utilities
Memory handling functions.
float * f_data
Definition: jpeg2000.h:209
int flag
Definition: cpu.c:34
int nb_codeblocks_width
Definition: jpeg2000.h:181
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:151
static const int contribtab[3][3]
Definition: jpeg2000.c:142
static const int xorbittab[3][3]
Definition: jpeg2000.c:144
static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
Definition: jpeg2000.c:85
float f_stepsize
Definition: jpeg2000.h:194
static Jpeg2000TgtNode * ff_jpeg2000_tag_tree_init(int w, int h)
Definition: jpeg2000.c:54
Macro definitions for various function/variable attributes.
uint8_t npasses
Definition: jpeg2000.h:164
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:168
#define JPEG2000_T1_SIG_W
Definition: jpeg2000.h:79
uint16_t log2_cblk_width
Definition: jpeg2000.h:192
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:82
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:183
Jpeg2000Band * band
Definition: jpeg2000.h:203
uint8_t val
Definition: jpeg2000.h:129
int coord[2][2]
Definition: jpeg2000.h:211
#define JPEG2000_T1_SIG_NE
Definition: jpeg2000.h:81
#define JPEG2000_T1_SIG_SE
Definition: jpeg2000.h:83
static const int ctxlbltab[3][3]
Definition: jpeg2000.c:143
uint16_t flags[6156]
Definition: jpeg2000.h:123
#define av_log(a,...)
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:145
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_jpeg2000_dwt_init(DWTContext *s, int border[2][2], int decomp_levels, int type)
Initialize DWT.
Definition: jpeg2000dwt.c:537
static int init_band(AVCodecContext *avctx, Jpeg2000ResLevel *reslevel, Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int bandno, int gbandno, int reslevelno, int cbps, int dx, int dy)
Definition: jpeg2000.c:370
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:171
#define F_LFTG_X
Definition: jpeg2000dwt.h:34
#define AVERROR(e)
Definition: error.h:43
int nb_codeblocks_height
Definition: jpeg2000.h:182
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:146
#define t1
Definition: regdef.h:29
simple assert() macros that are a bit more flexible than ISO C assert().
uint8_t zero
Definition: jpeg2000.h:171
static void init_band_stepsize(AVCodecContext *avctx, Jpeg2000Band *band, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int bandno, int gbandno, int reslevelno, int cbps)
Definition: jpeg2000.c:196
static av_always_inline float ff_exp2fi(int x)
2^(x) for integer x
Definition: internal.h:261
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:185
#define FFMAX(a, b)
Definition: common.h:94
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:251
uint8_t lblock
Definition: jpeg2000.h:170
#define FFMIN(a, b)
Definition: common.h:96
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:131
#define JPEG2000_T1_SGN_S
Definition: jpeg2000.h:91
JPEG 2000 structures and defines common to encoder and decoder.
int i_stepsize
Definition: jpeg2000.h:193
void ff_dwt_destroy(DWTContext *s)
Definition: jpeg2000dwt.c:620
int32_t
static int32_t tag_tree_size(int w, int h)
Definition: jpeg2000.c:42
#define JPEG2000_T1_SIG_N
Definition: jpeg2000.h:77
int coord[2][2]
Definition: jpeg2000.h:200
int coord[2][2]
Definition: jpeg2000.h:177
uint16_t lengthinc[JPEG2000_MAX_PASSES]
Definition: jpeg2000.h:168
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:216
int coord[2][2]
Definition: jpeg2000.h:191
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int coord_o[2][2]
Definition: jpeg2000.h:212
Libavcodec external API header.
int coord[2][2]
Definition: jpeg2000.h:187
main external API structure.
Definition: avcodec.h:1675
uint8_t vis
Definition: jpeg2000.h:130
uint8_t log2_prec_height
Definition: jpeg2000.h:202
uint16_t length
Definition: jpeg2000.h:167
uint8_t nbands
Definition: jpeg2000.h:199
int decoded_layers
Definition: jpeg2000.h:186
#define JPEG2000_T1_SIG_S
Definition: jpeg2000.h:80
Jpeg2000ResLevel * reslevel
Definition: jpeg2000.h:207
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:555
#define JPEG2000_T1_SGN_E
Definition: jpeg2000.h:93
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:150
uint8_t quantsty
Definition: jpeg2000.h:152
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:282
uint8_t log2_cblk_width
Definition: jpeg2000.h:137
#define F_LFTG_K
Definition: jpeg2000dwt.h:33
uint16_t log2_cblk_height
Definition: jpeg2000.h:192
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:733
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:448
#define JPEG2000_T1_SIG_SW
Definition: jpeg2000.h:84
#define JPEG2000_T1_SGN_N
Definition: jpeg2000.h:90
static int getsgnctxno(int flag, uint8_t *xorbit)
Definition: jpeg2000.c:146
#define JPEG2000_T1_SIG_E
Definition: jpeg2000.h:78
Jpeg2000Prec * prec
Definition: jpeg2000.h:195
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:96
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
static int getsigctxno(int flag, int bandno)
Definition: jpeg2000.c:97
uint8_t log2_cblk_height
Definition: jpeg2000.h:137
#define FFSWAP(type, a, b)
Definition: common.h:99
uint8_t ff_jpeg2000_sigctxno_lut[256][4]
Definition: jpeg2000.c:95
uint8_t log2_prec_width
Definition: jpeg2000.h:202
#define JPEG2000_T1_SIG_NW
Definition: jpeg2000.h:82
#define JPEG2000_T1_SGN_W
Definition: jpeg2000.h:92
#define t2
Definition: regdef.h:30
uint8_t ff_jpeg2000_xorbit_lut[16][16]
Definition: jpeg2000.c:140