avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / jpeg2000.c
blob79115009016217eb10bb6960b82d5a1f76edbb62
1 /*
2 * JPEG 2000 encoder and decoder common functions
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
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.
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.
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
23 /**
24 * @file
25 * JPEG 2000 image encoder and decoder common functions
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 "libavutil/thread.h"
34 #include "avcodec.h"
35 #include "jpeg2000.h"
37 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
39 /* tag tree routines */
41 static int32_t tag_tree_size(int w, int h)
43 int64_t res = 0;
44 while (w > 1 || h > 1) {
45 res += w * (int64_t)h;
46 av_assert0(res + 1 < INT32_MAX);
47 w = (w + 1) >> 1;
48 h = (h + 1) >> 1;
50 return (int32_t)(res + 1);
53 /* allocate the memory for tag tree */
54 static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
56 int pw = w, ph = h;
57 Jpeg2000TgtNode *res, *t, *t2;
58 int32_t tt_size;
60 tt_size = tag_tree_size(w, h);
62 t = res = av_calloc(tt_size, sizeof(*t));
63 if (!res)
64 return NULL;
66 while (w > 1 || h > 1) {
67 int i, j;
68 pw = w;
69 ph = h;
71 w = (w + 1) >> 1;
72 h = (h + 1) >> 1;
73 t2 = t + pw * ph;
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)];
79 t = t2;
81 t[0].parent = NULL;
82 return res;
85 void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
87 int i, siz = tag_tree_size(w, h);
89 for (i = 0; i < siz; i++) {
90 t[i].val = val;
91 t[i].temp_val = 0;
92 t[i].vis = 0;
96 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
98 static int getsigctxno(int flag, int bandno)
100 int h, v, d;
102 h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
103 ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
104 v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
105 ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
106 d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
107 ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
108 ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
109 ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
111 if (bandno < 3) {
112 if (bandno == 1)
113 FFSWAP(int, h, v);
114 if (h == 2) return 8;
115 if (h == 1) {
116 if (v >= 1) return 7;
117 if (d >= 1) return 6;
118 return 5;
120 if (v == 2) return 4;
121 if (v == 1) return 3;
122 if (d >= 2) return 2;
123 if (d == 1) return 1;
124 } else {
125 if (d >= 3) return 8;
126 if (d == 2) {
127 if (h+v >= 1) return 7;
128 return 6;
130 if (d == 1) {
131 if (h+v >= 2) return 5;
132 if (h+v == 1) return 4;
133 return 3;
135 if (h+v >= 2) return 2;
136 if (h+v == 1) return 1;
138 return 0;
141 uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
143 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
144 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
145 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
147 static int getsgnctxno(int flag, uint8_t *xorbit)
149 int vcontrib, hcontrib;
151 hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
152 [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
153 vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
154 [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
155 *xorbit = xorbittab[hcontrib][vcontrib];
157 return ctxlbltab[hcontrib][vcontrib];
160 static void av_cold jpeg2000_init_tier1_luts(void)
162 int i, j;
163 for (i = 0; i < 256; i++)
164 for (j = 0; j < 4; j++)
165 ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
166 for (i = 0; i < 16; i++)
167 for (j = 0; j < 16; j++)
168 ff_jpeg2000_sgnctxno_lut[i][j] =
169 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
172 void av_cold ff_jpeg2000_init_tier1_luts(void)
174 static AVOnce init_static_once = AV_ONCE_INIT;
175 ff_thread_once(&init_static_once, jpeg2000_init_tier1_luts);
178 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
179 int negative)
181 x++;
182 y++;
183 t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
184 if (negative) {
185 t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
186 t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
187 t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
188 t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
189 } else {
190 t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
191 t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
192 t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
193 t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
195 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
196 t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
197 t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
198 t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
201 // static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
204 * 2^(x) for integer x in the range -126..128.
205 * @return correctly rounded float
207 static av_always_inline float exp2fi(int x)
209 av_assert2(-126 <= x && x <= 128);
210 /* Normal range */
211 return av_int2float((x+127) << 23);
214 static void init_band_stepsize(AVCodecContext *avctx,
215 Jpeg2000Band *band,
216 Jpeg2000CodingStyle *codsty,
217 Jpeg2000QuantStyle *qntsty,
218 int bandno, int gbandno, int reslevelno,
219 int cbps)
221 /* TODO: Implementation of quantization step not finished,
222 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
223 switch (qntsty->quantsty) {
224 uint8_t gain;
225 case JPEG2000_QSTY_NONE:
226 /* TODO: to verify. No quantization in this case */
227 band->f_stepsize = 1;
228 break;
229 case JPEG2000_QSTY_SI:
230 /*TODO: Compute formula to implement. */
231 // numbps = cbps +
232 // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
233 // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
234 // 2 + numbps - qntsty->expn[gbandno]);
235 // break;
236 case JPEG2000_QSTY_SE:
237 /* Exponent quantization step.
238 * Formula:
239 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
240 * R_b = R_I + log2 (gain_b )
241 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
242 gain = cbps;
243 band->f_stepsize = exp2fi(gain - qntsty->expn[gbandno]);
244 band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
245 break;
246 default:
247 band->f_stepsize = 0;
248 av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
249 break;
251 if (codsty->transform != FF_DWT53) {
252 int lband = 0;
253 switch (bandno + (reslevelno > 0)) {
254 case 1:
255 case 2:
256 band->f_stepsize *= F_LFTG_X * 2;
257 lband = 1;
258 break;
259 case 3:
260 band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
261 break;
263 band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
266 if (band->f_stepsize > (INT_MAX >> 15)) {
267 band->f_stepsize = 0;
268 av_log(avctx, AV_LOG_ERROR, "stepsize out of range\n");
271 band->i_stepsize = (int)floorf(band->f_stepsize * (1 << 15));
274 static int init_prec(AVCodecContext *avctx,
275 Jpeg2000Band *band,
276 Jpeg2000ResLevel *reslevel,
277 Jpeg2000Component *comp,
278 Jpeg2000CodingStyle *codsty,
279 int precno, int bandno, int reslevelno,
280 int log2_band_prec_width,
281 int log2_band_prec_height)
283 Jpeg2000Prec *prec = band->prec + precno;
284 int nb_codeblocks, cblkno;
286 prec->decoded_layers = 0;
288 /* TODO: Explain formula for JPEG200 DCINEMA. */
289 /* TODO: Verify with previous count of codeblocks per band */
291 /* Compute P_x0 */
292 prec->coord[0][0] = ((reslevel->coord[0][0] >> reslevel->log2_prec_width) + precno % reslevel->num_precincts_x) *
293 (1 << log2_band_prec_width);
295 /* Compute P_y0 */
296 prec->coord[1][0] = ((reslevel->coord[1][0] >> reslevel->log2_prec_height) + precno / reslevel->num_precincts_x) *
297 (1 << log2_band_prec_height);
299 /* Compute P_x1 */
300 prec->coord[0][1] = prec->coord[0][0] +
301 (1 << log2_band_prec_width);
302 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
303 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
305 /* Compute P_y1 */
306 prec->coord[1][1] = prec->coord[1][0] +
307 (1 << log2_band_prec_height);
308 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
309 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
311 prec->nb_codeblocks_width =
312 ff_jpeg2000_ceildivpow2(prec->coord[0][1],
313 band->log2_cblk_width)
314 - (prec->coord[0][0] >> band->log2_cblk_width);
315 prec->nb_codeblocks_height =
316 ff_jpeg2000_ceildivpow2(prec->coord[1][1],
317 band->log2_cblk_height)
318 - (prec->coord[1][0] >> band->log2_cblk_height);
321 /* Tag trees initialization */
322 prec->cblkincl =
323 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
324 prec->nb_codeblocks_height);
325 if (!prec->cblkincl)
326 return AVERROR(ENOMEM);
328 prec->zerobits =
329 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
330 prec->nb_codeblocks_height);
331 if (!prec->zerobits)
332 return AVERROR(ENOMEM);
334 if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
335 prec->cblk = NULL;
336 return AVERROR(ENOMEM);
338 nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
339 prec->cblk = av_calloc(nb_codeblocks, sizeof(*prec->cblk));
340 if (!prec->cblk)
341 return AVERROR(ENOMEM);
342 for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
343 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
344 int Cx0, Cy0;
346 /* Compute coordinates of codeblocks */
347 /* Compute Cx0*/
348 Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
349 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
350 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
352 /* Compute Cy0*/
353 Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
354 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
355 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
357 /* Compute Cx1 */
358 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
359 prec->coord[0][1]);
361 /* Compute Cy1 */
362 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
363 prec->coord[1][1]);
364 /* Update code-blocks coordinates according sub-band position */
365 if ((bandno + !!reslevelno) & 1) {
366 cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
367 comp->reslevel[reslevelno-1].coord[0][0];
368 cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
369 comp->reslevel[reslevelno-1].coord[0][0];
371 if ((bandno + !!reslevelno) & 2) {
372 cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
373 comp->reslevel[reslevelno-1].coord[1][0];
374 cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
375 comp->reslevel[reslevelno-1].coord[1][0];
378 cblk->lblock = 3;
379 cblk->length = 0;
380 cblk->npasses = 0;
381 if (av_codec_is_encoder(avctx->codec)) {
382 cblk->layers = av_calloc(codsty->nlayers, sizeof(*cblk->layers));
383 if (!cblk->layers)
384 return AVERROR(ENOMEM);
388 return 0;
391 static int init_band(AVCodecContext *avctx,
392 Jpeg2000ResLevel *reslevel,
393 Jpeg2000Component *comp,
394 Jpeg2000CodingStyle *codsty,
395 Jpeg2000QuantStyle *qntsty,
396 int bandno, int gbandno, int reslevelno,
397 const int cbps, int dx, int dy)
399 Jpeg2000Band *band = reslevel->band + bandno;
400 uint8_t log2_band_prec_width, log2_band_prec_height;
401 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
402 int precno;
403 int nb_precincts;
404 int i, j, ret;
406 init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
408 /* computation of tbx_0, tbx_1, tby_0, tby_1
409 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
410 * codeblock width and height is computed for
411 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
412 if (reslevelno == 0) {
413 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
414 for (i = 0; i < 2; i++)
415 for (j = 0; j < 2; j++)
416 band->coord[i][j] =
417 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
418 declvl - 1);
419 log2_band_prec_width = reslevel->log2_prec_width;
420 log2_band_prec_height = reslevel->log2_prec_height;
421 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
422 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
423 reslevel->log2_prec_width);
424 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
425 reslevel->log2_prec_height);
426 } else {
427 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
428 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
429 for (i = 0; i < 2; i++)
430 for (j = 0; j < 2; j++)
431 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
432 band->coord[i][j] =
433 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
434 (((bandno + 1 >> i) & 1LL) << declvl - 1),
435 declvl);
436 /* TODO: Manage case of 3 band offsets here or
437 * in coding/decoding function? */
439 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
440 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
441 reslevel->log2_prec_width - 1);
442 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
443 reslevel->log2_prec_height - 1);
445 log2_band_prec_width = reslevel->log2_prec_width - 1;
446 log2_band_prec_height = reslevel->log2_prec_height - 1;
449 if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
450 band->prec = NULL;
451 return AVERROR(ENOMEM);
453 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
454 band->prec = av_calloc(nb_precincts, sizeof(*band->prec));
455 if (!band->prec)
456 return AVERROR(ENOMEM);
458 for (precno = 0; precno < nb_precincts; precno++) {
459 ret = init_prec(avctx, band, reslevel, comp, codsty,
460 precno, bandno, reslevelno,
461 log2_band_prec_width, log2_band_prec_height);
462 if (ret < 0)
463 return ret;
466 return 0;
469 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
470 Jpeg2000CodingStyle *codsty,
471 Jpeg2000QuantStyle *qntsty,
472 const int cbps, int dx, int dy,
473 AVCodecContext *avctx)
475 int reslevelno, bandno, gbandno = 0, ret, i, j;
476 uint32_t csize;
478 if (codsty->nreslevels2decode <= 0) {
479 av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
480 return AVERROR_INVALIDDATA;
483 if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
484 codsty->nreslevels2decode - 1,
485 codsty->transform))
486 return ret;
488 if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
489 comp->coord[1][1] - comp->coord[1][0], 0, avctx))
490 return AVERROR_INVALIDDATA;
491 csize = (comp->coord[0][1] - comp->coord[0][0]) *
492 (comp->coord[1][1] - comp->coord[1][0]);
493 if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
494 comp->coord[1][1] - comp->coord[1][0] > 32768) {
495 av_log(avctx, AV_LOG_ERROR, "component size too large\n");
496 return AVERROR_PATCHWELCOME;
499 if (codsty->transform == FF_DWT97) {
500 csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
501 comp->i_data = NULL;
502 comp->f_data = av_calloc(csize, sizeof(*comp->f_data));
503 if (!comp->f_data)
504 return AVERROR(ENOMEM);
505 } else {
506 csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
507 comp->f_data = NULL;
508 comp->i_data = av_calloc(csize, sizeof(*comp->i_data));
509 if (!comp->i_data)
510 return AVERROR(ENOMEM);
512 comp->reslevel = av_calloc(codsty->nreslevels, sizeof(*comp->reslevel));
513 if (!comp->reslevel)
514 return AVERROR(ENOMEM);
515 /* LOOP on resolution levels */
516 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
517 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
518 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
520 /* Compute borders for each resolution level.
521 * Computation of trx_0, trx_1, try_0 and try_1.
522 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
523 for (i = 0; i < 2; i++)
524 for (j = 0; j < 2; j++)
525 reslevel->coord[i][j] =
526 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
527 // update precincts size: 2^n value
528 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
529 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
531 /* Number of bands for each resolution level */
532 if (reslevelno == 0)
533 reslevel->nbands = 1;
534 else
535 reslevel->nbands = 3;
537 /* Number of precincts which span the tile for resolution level reslevelno
538 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
539 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
540 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
541 * for Dcinema profiles in JPEG 2000
542 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
543 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
544 if (reslevel->coord[0][1] == reslevel->coord[0][0])
545 reslevel->num_precincts_x = 0;
546 else
547 reslevel->num_precincts_x =
548 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
549 reslevel->log2_prec_width) -
550 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
552 if (reslevel->coord[1][1] == reslevel->coord[1][0])
553 reslevel->num_precincts_y = 0;
554 else
555 reslevel->num_precincts_y =
556 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
557 reslevel->log2_prec_height) -
558 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
560 reslevel->band = av_calloc(reslevel->nbands, sizeof(*reslevel->band));
561 if (!reslevel->band)
562 return AVERROR(ENOMEM);
564 if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y * reslevel->nbands > avctx->max_pixels / sizeof(*reslevel->band->prec))
565 return AVERROR(ENOMEM);
567 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
568 ret = init_band(avctx, reslevel,
569 comp, codsty, qntsty,
570 bandno, gbandno, reslevelno,
571 cbps, dx, dy);
572 if (ret < 0)
573 return ret;
576 return 0;
579 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
581 int reslevelno, bandno, cblkno, precno;
582 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
583 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
584 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
585 Jpeg2000Band *band = rlevel->band + bandno;
586 for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
587 Jpeg2000Prec *prec = band->prec + precno;
588 ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 0);
589 ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 0);
590 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
591 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
592 cblk->length = 0;
593 cblk->lblock = 3;
600 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
602 int reslevelno, bandno, precno;
603 for (reslevelno = 0;
604 comp->reslevel && reslevelno < codsty->nreslevels;
605 reslevelno++) {
606 Jpeg2000ResLevel *reslevel;
608 if (!comp->reslevel)
609 continue;
611 reslevel = comp->reslevel + reslevelno;
612 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
613 Jpeg2000Band *band;
615 if (!reslevel->band)
616 continue;
618 band = reslevel->band + bandno;
619 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
620 if (band->prec) {
621 Jpeg2000Prec *prec = band->prec + precno;
622 int nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
624 av_freep(&prec->zerobits);
625 av_freep(&prec->cblkincl);
626 if (prec->cblk) {
627 int cblkno;
628 for (cblkno = 0; cblkno < nb_code_blocks; cblkno ++) {
629 Jpeg2000Cblk *cblk = &prec->cblk[cblkno];
630 av_freep(&cblk->data);
631 av_freep(&cblk->passes);
632 av_freep(&cblk->lengthinc);
633 av_freep(&cblk->data_start);
634 av_freep(&cblk->layers);
636 av_freep(&prec->cblk);
641 av_freep(&band->prec);
643 av_freep(&reslevel->band);
646 ff_dwt_destroy(&comp->dwt);
647 av_freep(&comp->reslevel);
648 av_freep(&comp->i_data);
649 av_freep(&comp->f_data);