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