FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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/intfloat.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/thread.h"
35 #include "avcodec.h"
36 #include "jpeg2000.h"
37 
38 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
39 
40 /* tag tree routines */
41 
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 
54 /* allocate the memory for tag tree */
56 {
57  int pw = w, ph = h;
58  Jpeg2000TgtNode *res, *t, *t2;
59  int32_t tt_size;
60 
61  tt_size = tag_tree_size(w, h);
62 
63  t = res = av_calloc(tt_size, sizeof(*t));
64  if (!res)
65  return NULL;
66 
67  while (w > 1 || h > 1) {
68  int i, j;
69  pw = w;
70  ph = h;
71 
72  w = (w + 1) >> 1;
73  h = (h + 1) >> 1;
74  t2 = t + pw * ph;
75 
76  for (i = 0; i < ph; i++)
77  for (j = 0; j < pw; j++)
78  t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
79 
80  t = t2;
81  }
82  t[0].parent = NULL;
83  return res;
84 }
85 
86 void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
87 {
88  int i, siz = tag_tree_size(w, h);
89 
90  for (i = 0; i < siz; i++) {
91  t[i].val = val;
92  t[i].temp_val = 0;
93  t[i].vis = 0;
94  }
95 }
96 
97 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
98 
99 static int getsigctxno(int flag, int bandno)
100 {
101  int h, v, d;
102 
103  h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
104  ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
105  v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
106  ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
107  d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
108  ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
109  ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
110  ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
111 
112  if (bandno < 3) {
113  if (bandno == 1)
114  FFSWAP(int, h, v);
115  if (h == 2) return 8;
116  if (h == 1) {
117  if (v >= 1) return 7;
118  if (d >= 1) return 6;
119  return 5;
120  }
121  if (v == 2) return 4;
122  if (v == 1) return 3;
123  if (d >= 2) return 2;
124  if (d == 1) return 1;
125  } else {
126  if (d >= 3) return 8;
127  if (d == 2) {
128  if (h+v >= 1) return 7;
129  return 6;
130  }
131  if (d == 1) {
132  if (h+v >= 2) return 5;
133  if (h+v == 1) return 4;
134  return 3;
135  }
136  if (h+v >= 2) return 2;
137  if (h+v == 1) return 1;
138  }
139  return 0;
140 }
141 
143 
144 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
145 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
146 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
147 
148 static int getsgnctxno(int flag, uint8_t *xorbit)
149 {
150  int vcontrib, hcontrib;
151 
152  hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
153  [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
154  vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
155  [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
156  *xorbit = xorbittab[hcontrib][vcontrib];
157 
158  return ctxlbltab[hcontrib][vcontrib];
159 }
160 
162 {
163  int i, j;
164  for (i = 0; i < 256; i++)
165  for (j = 0; j < 4; j++)
167  for (i = 0; i < 16; i++)
168  for (j = 0; j < 16; j++)
170  getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
171 }
172 
174 {
175  static AVOnce init_static_once = AV_ONCE_INIT;
176  ff_thread_once(&init_static_once, jpeg2000_init_tier1_luts);
177 }
178 
180  int negative)
181 {
182  x++;
183  y++;
184  t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
185  if (negative) {
186  t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
187  t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
188  t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
189  t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
190  } else {
191  t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
192  t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
193  t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
194  t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
195  }
196  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
197  t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
198  t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
199  t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
200 }
201 
202 // static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
203 
204 /**
205  * 2^(x) for integer x in the range -126..128.
206  * @return correctly rounded float
207  */
208 static av_always_inline float exp2fi(int x)
209 {
210  av_assert2(-126 <= x && x <= 128);
211  /* Normal range */
212  return av_int2float((x+127) << 23);
213 }
214 
216  Jpeg2000Band *band,
217  Jpeg2000CodingStyle *codsty,
218  Jpeg2000QuantStyle *qntsty,
219  int bandno, int gbandno, int reslevelno,
220  int cbps)
221 {
222  /* TODO: Implementation of quantization step not finished,
223  * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
224  switch (qntsty->quantsty) {
225  uint8_t gain;
226  case JPEG2000_QSTY_NONE:
227  /* TODO: to verify. No quantization in this case */
228  band->f_stepsize = 1;
229  break;
230  case JPEG2000_QSTY_SI:
231  /*TODO: Compute formula to implement. */
232 // numbps = cbps +
233 // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
234 // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
235 // 2 + numbps - qntsty->expn[gbandno]);
236 // break;
237  case JPEG2000_QSTY_SE:
238  /* Exponent quantization step.
239  * Formula:
240  * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
241  * R_b = R_I + log2 (gain_b )
242  * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
243  gain = cbps;
244  band->f_stepsize = exp2fi(gain - qntsty->expn[gbandno]);
245  band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
246  break;
247  default:
248  band->f_stepsize = 0;
249  av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
250  break;
251  }
252  if (codsty->transform != FF_DWT53) {
253  int lband = 0;
254  switch (bandno + (reslevelno > 0)) {
255  case 1:
256  case 2:
257  band->f_stepsize *= F_LFTG_X * 2;
258  lband = 1;
259  break;
260  case 3:
261  band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
262  break;
263  }
264  band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
265  }
266 
267  if (band->f_stepsize > (INT_MAX >> 15)) {
268  band->f_stepsize = 0;
269  av_log(avctx, AV_LOG_ERROR, "stepsize out of range\n");
270  }
271 
272  band->i_stepsize = (int)floorf(band->f_stepsize * (1 << 15));
273 }
274 
275 static int init_prec(AVCodecContext *avctx,
276  Jpeg2000Band *band,
277  Jpeg2000ResLevel *reslevel,
279  Jpeg2000CodingStyle *codsty,
280  int precno, int bandno, int reslevelno,
281  int log2_band_prec_width,
282  int log2_band_prec_height)
283 {
284  Jpeg2000Prec *prec = band->prec + precno;
285  int nb_codeblocks, cblkno;
286 
287  prec->decoded_layers = 0;
288 
289  /* TODO: Explain formula for JPEG200 DCINEMA. */
290  /* TODO: Verify with previous count of codeblocks per band */
291 
292  /* Compute P_x0 */
293  prec->coord[0][0] = ((reslevel->coord[0][0] >> reslevel->log2_prec_width) + precno % reslevel->num_precincts_x) *
294  (1 << log2_band_prec_width);
295 
296  /* Compute P_y0 */
297  prec->coord[1][0] = ((reslevel->coord[1][0] >> reslevel->log2_prec_height) + precno / reslevel->num_precincts_x) *
298  (1 << log2_band_prec_height);
299 
300  /* Compute P_x1 */
301  prec->coord[0][1] = prec->coord[0][0] +
302  (1 << log2_band_prec_width);
303  prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
304  prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
305 
306  /* Compute P_y1 */
307  prec->coord[1][1] = prec->coord[1][0] +
308  (1 << log2_band_prec_height);
309  prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
310  prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
311 
312  prec->nb_codeblocks_width =
313  ff_jpeg2000_ceildivpow2(prec->coord[0][1],
314  band->log2_cblk_width)
315  - (prec->coord[0][0] >> band->log2_cblk_width);
316  prec->nb_codeblocks_height =
317  ff_jpeg2000_ceildivpow2(prec->coord[1][1],
318  band->log2_cblk_height)
319  - (prec->coord[1][0] >> band->log2_cblk_height);
320 
321 
322  /* Tag trees initialization */
323  prec->cblkincl =
325  prec->nb_codeblocks_height);
326  if (!prec->cblkincl)
327  return AVERROR(ENOMEM);
328 
329  prec->zerobits =
331  prec->nb_codeblocks_height);
332  if (!prec->zerobits)
333  return AVERROR(ENOMEM);
334 
335  if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
336  prec->cblk = NULL;
337  return AVERROR(ENOMEM);
338  }
339  nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
340  prec->cblk = av_calloc(nb_codeblocks, sizeof(*prec->cblk));
341  if (!prec->cblk)
342  return AVERROR(ENOMEM);
343  for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
344  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
345  int Cx0, Cy0;
346 
347  /* Compute coordinates of codeblocks */
348  /* Compute Cx0*/
349  Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
350  Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
351  cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
352 
353  /* Compute Cy0*/
354  Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
355  Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
356  cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
357 
358  /* Compute Cx1 */
359  cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
360  prec->coord[0][1]);
361 
362  /* Compute Cy1 */
363  cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
364  prec->coord[1][1]);
365  /* Update code-blocks coordinates according sub-band position */
366  if ((bandno + !!reslevelno) & 1) {
367  cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
368  comp->reslevel[reslevelno-1].coord[0][0];
369  cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
370  comp->reslevel[reslevelno-1].coord[0][0];
371  }
372  if ((bandno + !!reslevelno) & 2) {
373  cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
374  comp->reslevel[reslevelno-1].coord[1][0];
375  cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
376  comp->reslevel[reslevelno-1].coord[1][0];
377  }
378 
379  cblk->lblock = 3;
380  cblk->length = 0;
381  cblk->npasses = 0;
382  if (av_codec_is_encoder(avctx->codec)) {
383  cblk->layers = av_calloc(codsty->nlayers, sizeof(*cblk->layers));
384  if (!cblk->layers)
385  return AVERROR(ENOMEM);
386  }
387  }
388 
389  return 0;
390 }
391 
392 static int init_band(AVCodecContext *avctx,
393  Jpeg2000ResLevel *reslevel,
395  Jpeg2000CodingStyle *codsty,
396  Jpeg2000QuantStyle *qntsty,
397  int bandno, int gbandno, int reslevelno,
398  const int cbps, int dx, int dy)
399 {
400  Jpeg2000Band *band = reslevel->band + bandno;
401  uint8_t log2_band_prec_width, log2_band_prec_height;
402  int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
403  int precno;
404  int nb_precincts;
405  int i, j, ret;
406 
407  init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
408 
409  /* computation of tbx_0, tbx_1, tby_0, tby_1
410  * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
411  * codeblock width and height is computed for
412  * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
413  if (reslevelno == 0) {
414  /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
415  for (i = 0; i < 2; i++)
416  for (j = 0; j < 2; j++)
417  band->coord[i][j] =
418  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
419  declvl - 1);
420  log2_band_prec_width = reslevel->log2_prec_width;
421  log2_band_prec_height = reslevel->log2_prec_height;
422  /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
423  band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
424  reslevel->log2_prec_width);
425  band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
426  reslevel->log2_prec_height);
427  } else {
428  /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
429  /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
430  for (i = 0; i < 2; i++)
431  for (j = 0; j < 2; j++)
432  /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
433  band->coord[i][j] =
434  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
435  (((bandno + 1 >> i) & 1LL) << declvl - 1),
436  declvl);
437  /* TODO: Manage case of 3 band offsets here or
438  * in coding/decoding function? */
439 
440  /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
441  band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
442  reslevel->log2_prec_width - 1);
443  band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
444  reslevel->log2_prec_height - 1);
445 
446  log2_band_prec_width = reslevel->log2_prec_width - 1;
447  log2_band_prec_height = reslevel->log2_prec_height - 1;
448  }
449 
450  if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
451  band->prec = NULL;
452  return AVERROR(ENOMEM);
453  }
454  nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
455  band->prec = av_calloc(nb_precincts, sizeof(*band->prec));
456  if (!band->prec)
457  return AVERROR(ENOMEM);
458 
459  for (precno = 0; precno < nb_precincts; precno++) {
460  ret = init_prec(avctx, band, reslevel, comp, codsty,
461  precno, bandno, reslevelno,
462  log2_band_prec_width, log2_band_prec_height);
463  if (ret < 0)
464  return ret;
465  }
466 
467  return 0;
468 }
469 
471  Jpeg2000CodingStyle *codsty,
472  Jpeg2000QuantStyle *qntsty,
473  const int cbps, int dx, int dy,
474  AVCodecContext *avctx)
475 {
476  int reslevelno, bandno, gbandno = 0, ret, i, j;
477  uint32_t csize;
478 
479  if (codsty->nreslevels2decode <= 0) {
480  av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
481  return AVERROR_INVALIDDATA;
482  }
483 
484  if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
485  codsty->nreslevels2decode - 1,
486  codsty->transform))
487  return ret;
488 
489  if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
490  comp->coord[1][1] - comp->coord[1][0], 0, avctx))
491  return AVERROR_INVALIDDATA;
492  csize = (comp->coord[0][1] - comp->coord[0][0]) *
493  (comp->coord[1][1] - comp->coord[1][0]);
494  if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
495  comp->coord[1][1] - comp->coord[1][0] > 32768) {
496  av_log(avctx, AV_LOG_ERROR, "component size too large\n");
497  return AVERROR_PATCHWELCOME;
498  }
499 
500  if (codsty->transform == FF_DWT97) {
501  csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
502  comp->i_data = NULL;
503  comp->f_data = av_calloc(csize, sizeof(*comp->f_data));
504  if (!comp->f_data)
505  return AVERROR(ENOMEM);
506  } else {
507  csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
508  comp->f_data = NULL;
509  comp->i_data = av_calloc(csize, sizeof(*comp->i_data));
510  if (!comp->i_data)
511  return AVERROR(ENOMEM);
512  }
513  comp->reslevel = av_calloc(codsty->nreslevels, sizeof(*comp->reslevel));
514  if (!comp->reslevel)
515  return AVERROR(ENOMEM);
516  /* LOOP on resolution levels */
517  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
518  int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
519  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
520 
521  /* Compute borders for each resolution level.
522  * Computation of trx_0, trx_1, try_0 and try_1.
523  * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
524  for (i = 0; i < 2; i++)
525  for (j = 0; j < 2; j++)
526  reslevel->coord[i][j] =
527  ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
528  // update precincts size: 2^n value
529  reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
530  reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
531 
532  /* Number of bands for each resolution level */
533  if (reslevelno == 0)
534  reslevel->nbands = 1;
535  else
536  reslevel->nbands = 3;
537 
538  /* Number of precincts which span the tile for resolution level reslevelno
539  * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
540  * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
541  * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
542  * for Dcinema profiles in JPEG 2000
543  * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
544  * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
545  if (reslevel->coord[0][1] == reslevel->coord[0][0])
546  reslevel->num_precincts_x = 0;
547  else
548  reslevel->num_precincts_x =
549  ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
550  reslevel->log2_prec_width) -
551  (reslevel->coord[0][0] >> reslevel->log2_prec_width);
552 
553  if (reslevel->coord[1][1] == reslevel->coord[1][0])
554  reslevel->num_precincts_y = 0;
555  else
556  reslevel->num_precincts_y =
557  ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
558  reslevel->log2_prec_height) -
559  (reslevel->coord[1][0] >> reslevel->log2_prec_height);
560 
561  reslevel->band = av_calloc(reslevel->nbands, sizeof(*reslevel->band));
562  if (!reslevel->band)
563  return AVERROR(ENOMEM);
564 
565  if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y * reslevel->nbands > avctx->max_pixels / sizeof(*reslevel->band->prec))
566  return AVERROR(ENOMEM);
567 
568  for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
569  ret = init_band(avctx, reslevel,
570  comp, codsty, qntsty,
571  bandno, gbandno, reslevelno,
572  cbps, dx, dy);
573  if (ret < 0)
574  return ret;
575  }
576  }
577  return 0;
578 }
579 
581 {
582  int reslevelno, bandno, cblkno, precno;
583  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
584  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
585  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
586  Jpeg2000Band *band = rlevel->band + bandno;
587  for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
588  Jpeg2000Prec *prec = band->prec + precno;
591  for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
592  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
593  cblk->length = 0;
594  cblk->lblock = 3;
595  }
596  }
597  }
598  }
599 }
600 
602 {
603  int reslevelno, bandno, precno;
604  for (reslevelno = 0;
605  comp->reslevel && reslevelno < codsty->nreslevels;
606  reslevelno++) {
607  Jpeg2000ResLevel *reslevel;
608 
609  if (!comp->reslevel)
610  continue;
611 
612  reslevel = comp->reslevel + reslevelno;
613  for (bandno = 0; bandno < reslevel->nbands; bandno++) {
614  Jpeg2000Band *band;
615 
616  if (!reslevel->band)
617  continue;
618 
619  band = reslevel->band + bandno;
620  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
621  if (band->prec) {
622  Jpeg2000Prec *prec = band->prec + precno;
623  int nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
624 
625  av_freep(&prec->zerobits);
626  av_freep(&prec->cblkincl);
627  if (prec->cblk) {
628  int cblkno;
629  for (cblkno = 0; cblkno < nb_code_blocks; cblkno ++) {
630  Jpeg2000Cblk *cblk = &prec->cblk[cblkno];
631  av_freep(&cblk->data);
632  av_freep(&cblk->passes);
633  av_freep(&cblk->lengthinc);
634  av_freep(&cblk->data_start);
635  av_freep(&cblk->layers);
636  }
637  av_freep(&prec->cblk);
638  }
639  }
640  }
641 
642  av_freep(&band->prec);
643  }
644  av_freep(&reslevel->band);
645  }
646 
647  ff_dwt_destroy(&comp->dwt);
648  av_freep(&comp->reslevel);
649  av_freep(&comp->i_data);
650  av_freep(&comp->f_data);
651 }
contribtab
static const int contribtab[3][3]
Definition: jpeg2000.c:144
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
JPEG2000_T1_SGN_N
#define JPEG2000_T1_SGN_N
Definition: jpeg2000.h:98
Jpeg2000QuantStyle::quantsty
uint8_t quantsty
Definition: jpeg2000.h:164
Jpeg2000Prec::decoded_layers
int decoded_layers
Definition: jpeg2000.h:213
jpeg2000_init_tier1_luts
static void av_cold jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:161
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:79
JPEG2000_T1_SIG_NE
#define JPEG2000_T1_SIG_NE
Definition: jpeg2000.h:89
thread.h
JPEG2000_QSTY_NONE
@ JPEG2000_QSTY_NONE
Definition: jpeg2000.h:73
Jpeg2000Prec::nb_codeblocks_height
int nb_codeblocks_height
Definition: jpeg2000.h:209
floorf
static __device__ float floorf(float a)
Definition: cuda_runtime.h:172
Jpeg2000Cblk::coord
int coord[2][2]
Definition: jpeg2000.h:199
int64_t
long long int64_t
Definition: coverity.c:34
xorbittab
static const int xorbittab[3][3]
Definition: jpeg2000.c:146
ff_jpeg2000_sgnctxno_lut
uint8_t ff_jpeg2000_sgnctxno_lut[16][16]
Definition: jpeg2000.c:142
Jpeg2000Band::i_stepsize
int i_stepsize
Definition: jpeg2000.h:220
ph
static int FUNC() ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
Definition: cbs_h266_syntax_template.c:3043
w
uint8_t w
Definition: llviddspenc.c:38
Jpeg2000Prec::zerobits
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:210
init_band_stepsize
static void init_band_stepsize(AVCodecContext *avctx, Jpeg2000Band *band, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int bandno, int gbandno, int reslevelno, int cbps)
Definition: jpeg2000.c:215
Jpeg2000Prec::coord
int coord[2][2]
Definition: jpeg2000.h:214
ff_jpeg2000_sigctxno_lut
uint8_t ff_jpeg2000_sigctxno_lut[256][4]
Definition: jpeg2000.c:97
Jpeg2000Prec
Definition: jpeg2000.h:207
Jpeg2000TgtNode::parent
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:142
Jpeg2000Band
Definition: jpeg2000.h:217
FF_DWT97
@ FF_DWT97
Definition: jpeg2000dwt.h:38
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
exp2fi
static av_always_inline float exp2fi(int x)
2^(x) for integer x in the range -126..128.
Definition: jpeg2000.c:208
intfloat.h
JPEG2000_T1_SIG_N
#define JPEG2000_T1_SIG_N
Definition: jpeg2000.h:85
Jpeg2000CodingStyle::log2_cblk_width
uint8_t log2_cblk_width
Definition: jpeg2000.h:148
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:460
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
JPEG2000_T1_SGN_E
#define JPEG2000_T1_SGN_E
Definition: jpeg2000.h:101
Jpeg2000Cblk::passes
Jpeg2000Pass * passes
Definition: jpeg2000.h:197
Jpeg2000CodingStyle::log2_prec_heights
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:157
JPEG2000_T1_SIG_E
#define JPEG2000_T1_SIG_E
Definition: jpeg2000.h:86
val
static double val(void *priv, double ch)
Definition: aeval.c:77
Jpeg2000T1Context
Definition: jpeg2000.h:131
avassert.h
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
Jpeg2000ResLevel
Definition: jpeg2000.h:225
av_cold
#define av_cold
Definition: attributes.h:90
ff_jpeg2000_tag_tree_init
static Jpeg2000TgtNode * ff_jpeg2000_tag_tree_init(int w, int h)
Definition: jpeg2000.c:55
Jpeg2000CodingStyle::transform
uint8_t transform
Definition: jpeg2000.h:150
Jpeg2000Cblk::layers
Jpeg2000Layer * layers
Definition: jpeg2000.h:198
Jpeg2000ResLevel::band
Jpeg2000Band * band
Definition: jpeg2000.h:230
jpeg2000.h
Jpeg2000Cblk::data
uint8_t * data
Definition: jpeg2000.h:192
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
Jpeg2000Band::coord
int coord[2][2]
Definition: jpeg2000.h:218
AVCodecContext::max_pixels
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:1964
JPEG2000_T1_SGN_W
#define JPEG2000_T1_SGN_W
Definition: jpeg2000.h:100
Jpeg2000Band::f_stepsize
float f_stepsize
Definition: jpeg2000.h:221
JPEG2000_QSTY_SI
@ JPEG2000_QSTY_SI
Definition: jpeg2000.h:74
if
if(ret)
Definition: filter_design.txt:179
Jpeg2000Cblk::lblock
uint8_t lblock
Definition: jpeg2000.h:191
JPEG2000_T1_SIG_S
#define JPEG2000_T1_SIG_S
Definition: jpeg2000.h:88
Jpeg2000CodingStyle::log2_prec_widths
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:156
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
Jpeg2000Cblk::length
uint16_t length
Definition: jpeg2000.h:188
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
JPEG2000_T1_SIG_W
#define JPEG2000_T1_SIG_W
Definition: jpeg2000.h:87
Jpeg2000Band::prec
Jpeg2000Prec * prec
Definition: jpeg2000.h:222
Jpeg2000ResLevel::num_precincts_y
int num_precincts_y
Definition: jpeg2000.h:228
tag_tree_size
static int32_t tag_tree_size(int w, int h)
Definition: jpeg2000.c:42
Jpeg2000ResLevel::coord
int coord[2][2]
Definition: jpeg2000.h:227
F_LFTG_K
#define F_LFTG_K
Definition: jpeg2000dwt.h:33
Jpeg2000Band::log2_cblk_height
uint16_t log2_cblk_height
Definition: jpeg2000.h:219
AVOnce
#define AVOnce
Definition: thread.h:202
getsgnctxno
static int getsgnctxno(int flag, uint8_t *xorbit)
Definition: jpeg2000.c:148
init_band
static int init_band(AVCodecContext *avctx, Jpeg2000ResLevel *reslevel, Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int bandno, int gbandno, int reslevelno, const int cbps, int dx, int dy)
Definition: jpeg2000.c:392
Jpeg2000Prec::nb_codeblocks_width
int nb_codeblocks_width
Definition: jpeg2000.h:208
JPEG2000_T1_SIG_SE
#define JPEG2000_T1_SIG_SE
Definition: jpeg2000.h:91
ctxlbltab
static const int ctxlbltab[3][3]
Definition: jpeg2000.c:145
ff_jpeg2000_dwt_init
int ff_jpeg2000_dwt_init(DWTContext *s, int border[2][2], int decomp_levels, int type)
Initialize DWT.
Definition: jpeg2000dwt.c:539
Jpeg2000ResLevel::log2_prec_height
uint8_t log2_prec_height
Definition: jpeg2000.h:229
ff_jpeg2000_cleanup
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:601
ff_jpeg2000_init_component
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, const int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:470
Jpeg2000Component
Definition: jpeg2000.h:233
JPEG2000_T1_SIG_SW
#define JPEG2000_T1_SIG_SW
Definition: jpeg2000.h:92
Jpeg2000Prec::cblkincl
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:211
Jpeg2000ResLevel::nbands
uint8_t nbands
Definition: jpeg2000.h:226
ff_jpeg2000_xorbit_lut
uint8_t ff_jpeg2000_xorbit_lut[16][16]
Definition: jpeg2000.c:142
Jpeg2000Cblk
Definition: jpeg2000.h:183
ff_dwt_destroy
void ff_dwt_destroy(DWTContext *s)
Definition: jpeg2000dwt.c:622
attributes.h
Jpeg2000T1Context::stride
int stride
Definition: jpeg2000.h:135
Jpeg2000TgtNode
Definition: jpeg2000.h:138
Jpeg2000Cblk::data_start
int * data_start
Definition: jpeg2000.h:196
Jpeg2000CodingStyle::nlayers
uint8_t nlayers
Definition: jpeg2000.h:152
Jpeg2000CodingStyle::nreslevels
int nreslevels
Definition: jpeg2000.h:146
ff_jpeg2000_reinit
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:580
av_codec_is_encoder
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:79
Jpeg2000TgtNode::temp_val
uint8_t temp_val
Definition: jpeg2000.h:140
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
Jpeg2000CodingStyle::log2_cblk_height
uint8_t log2_cblk_height
Definition: jpeg2000.h:149
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
Jpeg2000ResLevel::num_precincts_x
int num_precincts_x
Definition: jpeg2000.h:228
Jpeg2000QuantStyle::expn
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:162
common.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
Jpeg2000Band::log2_cblk_width
uint16_t log2_cblk_width
Definition: jpeg2000.h:219
F_LFTG_X
#define F_LFTG_X
Definition: jpeg2000dwt.h:34
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
Jpeg2000QuantStyle::mant
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:163
avcodec.h
JPEG2000_T1_SIG
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:104
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
JPEG2000_QSTY_SE
@ JPEG2000_QSTY_SE
Definition: jpeg2000.h:75
getsigctxno
static int getsigctxno(int flag, int bandno)
Definition: jpeg2000.c:99
flag
#define flag(name)
Definition: cbs_av1.c:495
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
Jpeg2000T1Context::flags
uint16_t flags[6156]
Definition: jpeg2000.h:133
AVCodecContext
main external API structure.
Definition: avcodec.h:451
FF_DWT53
@ FF_DWT53
Definition: jpeg2000dwt.h:39
ff_jpeg2000_ceildivpow2
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:244
Jpeg2000ResLevel::log2_prec_width
uint8_t log2_prec_width
Definition: jpeg2000.h:229
ff_jpeg2000_init_tier1_luts
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:173
JPEG2000_T1_SGN_S
#define JPEG2000_T1_SGN_S
Definition: jpeg2000.h:99
JPEG2000_T1_SIG_NW
#define JPEG2000_T1_SIG_NW
Definition: jpeg2000.h:90
mem.h
Jpeg2000Cblk::npasses
uint8_t npasses
Definition: jpeg2000.h:184
Jpeg2000CodingStyle::nreslevels2decode
int nreslevels2decode
Definition: jpeg2000.h:147
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_jpeg2000_set_significance
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:179
int32_t
int32_t
Definition: audioconvert.c:56
imgutils.h
Jpeg2000TgtNode::val
uint8_t val
Definition: jpeg2000.h:139
Jpeg2000TgtNode::vis
uint8_t vis
Definition: jpeg2000.h:141
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
Jpeg2000CodingStyle
Definition: jpeg2000.h:145
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
Jpeg2000Cblk::lengthinc
uint16_t * lengthinc
Definition: jpeg2000.h:189
h
h
Definition: vp9dsp_template.c:2070
Jpeg2000QuantStyle
Definition: jpeg2000.h:161
av_image_check_size
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:318
Jpeg2000Prec::cblk
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:212
ff_tag_tree_zero
void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
Definition: jpeg2000.c:86
init_prec
static int init_prec(AVCodecContext *avctx, Jpeg2000Band *band, Jpeg2000ResLevel *reslevel, Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, int precno, int bandno, int reslevelno, int log2_band_prec_width, int log2_band_prec_height)
Definition: jpeg2000.c:275