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