OSDN Git Service

aacenc_tns: tune and reduce artifacts
[android-x86/external-ffmpeg.git] / 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 void init_band_stepsize(AVCodecContext *avctx,
196                                Jpeg2000Band *band,
197                                Jpeg2000CodingStyle *codsty,
198                                Jpeg2000QuantStyle *qntsty,
199                                int bandno, int gbandno, int reslevelno,
200                                int cbps)
201 {
202     /* TODO: Implementation of quantization step not finished,
203      * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
204     switch (qntsty->quantsty) {
205         uint8_t gain;
206     case JPEG2000_QSTY_NONE:
207         /* TODO: to verify. No quantization in this case */
208         band->f_stepsize = 1;
209         break;
210     case JPEG2000_QSTY_SI:
211         /*TODO: Compute formula to implement. */
212 //         numbps = cbps +
213 //                  lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
214 //         band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
215 //                                2 + numbps - qntsty->expn[gbandno]);
216 //         break;
217     case JPEG2000_QSTY_SE:
218         /* Exponent quantization step.
219          * Formula:
220          * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
221          * R_b = R_I + log2 (gain_b )
222          * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
223         gain            = cbps;
224         band->f_stepsize  = pow(2.0, gain - qntsty->expn[gbandno]);
225         band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
226         break;
227     default:
228         band->f_stepsize = 0;
229         av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
230         break;
231     }
232     if (codsty->transform != FF_DWT53) {
233         int lband = 0;
234         switch (bandno + (reslevelno > 0)) {
235             case 1:
236             case 2:
237                 band->f_stepsize *= F_LFTG_X * 2;
238                 lband = 1;
239                 break;
240             case 3:
241                 band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
242                 break;
243         }
244         if (codsty->transform == FF_DWT97) {
245             band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
246         }
247     }
248
249     band->i_stepsize = band->f_stepsize * (1 << 15);
250
251     /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
252      * If not set output of entropic decoder is not correct. */
253     if (!av_codec_is_encoder(avctx->codec))
254         band->f_stepsize *= 0.5;
255 }
256
257 static int init_prec(Jpeg2000Band *band,
258                      Jpeg2000ResLevel *reslevel,
259                      Jpeg2000Component *comp,
260                      int precno, int bandno, int reslevelno,
261                      int log2_band_prec_width,
262                      int log2_band_prec_height)
263 {
264     Jpeg2000Prec *prec = band->prec + precno;
265     int nb_codeblocks, cblkno;
266
267     prec->decoded_layers = 0;
268
269     /* TODO: Explain formula for JPEG200 DCINEMA. */
270     /* TODO: Verify with previous count of codeblocks per band */
271
272     /* Compute P_x0 */
273     prec->coord[0][0] = ((band->coord[0][0] >> log2_band_prec_width) + precno % reslevel->num_precincts_x) *
274                         (1 << log2_band_prec_width);
275
276     /* Compute P_y0 */
277     prec->coord[1][0] = ((band->coord[1][0] >> log2_band_prec_height) + precno / reslevel->num_precincts_x) *
278                         (1 << log2_band_prec_height);
279
280     /* Compute P_x1 */
281     prec->coord[0][1] = prec->coord[0][0] +
282                         (1 << log2_band_prec_width);
283     prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
284     prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
285
286     /* Compute P_y1 */
287     prec->coord[1][1] = prec->coord[1][0] +
288                         (1 << log2_band_prec_height);
289     prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
290     prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
291
292     prec->nb_codeblocks_width =
293         ff_jpeg2000_ceildivpow2(prec->coord[0][1],
294                                 band->log2_cblk_width)
295         - (prec->coord[0][0] >> band->log2_cblk_width);
296     prec->nb_codeblocks_height =
297         ff_jpeg2000_ceildivpow2(prec->coord[1][1],
298                                 band->log2_cblk_height)
299         - (prec->coord[1][0] >> band->log2_cblk_height);
300
301
302     /* Tag trees initialization */
303     prec->cblkincl =
304         ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
305                                   prec->nb_codeblocks_height);
306     if (!prec->cblkincl)
307         return AVERROR(ENOMEM);
308
309     prec->zerobits =
310         ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
311                                   prec->nb_codeblocks_height);
312     if (!prec->zerobits)
313         return AVERROR(ENOMEM);
314
315     if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
316         prec->cblk = NULL;
317         return AVERROR(ENOMEM);
318     }
319     nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
320     prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
321     if (!prec->cblk)
322         return AVERROR(ENOMEM);
323     for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
324         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
325         int Cx0, Cy0;
326
327         /* Compute coordinates of codeblocks */
328         /* Compute Cx0*/
329         Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
330         Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << band->log2_cblk_width);
331         cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
332
333         /* Compute Cy0*/
334         Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
335         Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << band->log2_cblk_height);
336         cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
337
338         /* Compute Cx1 */
339         cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
340                                   prec->coord[0][1]);
341
342         /* Compute Cy1 */
343         cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
344                                   prec->coord[1][1]);
345         /* Update code-blocks coordinates according sub-band position */
346         if ((bandno + !!reslevelno) & 1) {
347             cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
348                                  comp->reslevel[reslevelno-1].coord[0][0];
349             cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
350                                  comp->reslevel[reslevelno-1].coord[0][0];
351         }
352         if ((bandno + !!reslevelno) & 2) {
353             cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
354                                  comp->reslevel[reslevelno-1].coord[1][0];
355             cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
356                                  comp->reslevel[reslevelno-1].coord[1][0];
357         }
358
359         cblk->zero      = 0;
360         cblk->lblock    = 3;
361         cblk->length    = 0;
362         memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
363         cblk->npasses   = 0;
364     }
365
366     return 0;
367 }
368
369 static int init_band(AVCodecContext *avctx,
370                      Jpeg2000ResLevel *reslevel,
371                      Jpeg2000Component *comp,
372                      Jpeg2000CodingStyle *codsty,
373                      Jpeg2000QuantStyle *qntsty,
374                      int bandno, int gbandno, int reslevelno,
375                      int cbps, int dx, int dy)
376 {
377     Jpeg2000Band *band = reslevel->band + bandno;
378     uint8_t log2_band_prec_width, log2_band_prec_height;
379     int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 15444-1:2002 B.5
380     int precno;
381     int nb_precincts;
382     int i, j, ret;
383
384     init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
385
386     /* computation of tbx_0, tbx_1, tby_0, tby_1
387      * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
388      * codeblock width and height is computed for
389      * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
390     if (reslevelno == 0) {
391         /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
392         for (i = 0; i < 2; i++)
393             for (j = 0; j < 2; j++)
394                 band->coord[i][j] =
395                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
396                                             declvl - 1);
397         log2_band_prec_width  = reslevel->log2_prec_width;
398         log2_band_prec_height = reslevel->log2_prec_height;
399         /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
400         band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
401                                        reslevel->log2_prec_width);
402         band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
403                                        reslevel->log2_prec_height);
404     } else {
405         /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
406         /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
407         for (i = 0; i < 2; i++)
408             for (j = 0; j < 2; j++)
409                 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
410                 band->coord[i][j] =
411                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
412                                             (((bandno + 1 >> i) & 1LL) << declvl - 1),
413                                             declvl);
414         /* TODO: Manage case of 3 band offsets here or
415          * in coding/decoding function? */
416
417         /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
418         band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
419                                        reslevel->log2_prec_width - 1);
420         band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
421                                        reslevel->log2_prec_height - 1);
422
423         log2_band_prec_width  = reslevel->log2_prec_width  - 1;
424         log2_band_prec_height = reslevel->log2_prec_height - 1;
425     }
426
427     if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
428         band->prec = NULL;
429         return AVERROR(ENOMEM);
430     }
431     nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
432     band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
433     if (!band->prec)
434         return AVERROR(ENOMEM);
435
436     for (precno = 0; precno < nb_precincts; precno++) {
437         ret = init_prec(band, reslevel, comp,
438                         precno, bandno, reslevelno,
439                         log2_band_prec_width, log2_band_prec_height);
440         if (ret < 0)
441             return ret;
442     }
443
444     return 0;
445 }
446
447 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
448                                Jpeg2000CodingStyle *codsty,
449                                Jpeg2000QuantStyle *qntsty,
450                                int cbps, int dx, int dy,
451                                AVCodecContext *avctx)
452 {
453     int reslevelno, bandno, gbandno = 0, ret, i, j;
454     uint32_t csize;
455
456     if (codsty->nreslevels2decode <= 0) {
457         av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
458         return AVERROR_INVALIDDATA;
459     }
460
461     if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
462                                    codsty->nreslevels2decode - 1,
463                                    codsty->transform))
464         return ret;
465
466     if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
467                             comp->coord[1][1] - comp->coord[1][0], 0, avctx))
468         return AVERROR_INVALIDDATA;
469     csize = (comp->coord[0][1] - comp->coord[0][0]) *
470             (comp->coord[1][1] - comp->coord[1][0]);
471     if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
472         comp->coord[1][1] - comp->coord[1][0] > 32768) {
473         av_log(avctx, AV_LOG_ERROR, "component size too large\n");
474         return AVERROR_PATCHWELCOME;
475     }
476
477     if (codsty->transform == FF_DWT97) {
478         csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
479         comp->i_data = NULL;
480         comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
481         if (!comp->f_data)
482             return AVERROR(ENOMEM);
483     } else {
484         csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
485         comp->f_data = NULL;
486         comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
487         if (!comp->i_data)
488             return AVERROR(ENOMEM);
489     }
490     comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
491     if (!comp->reslevel)
492         return AVERROR(ENOMEM);
493     /* LOOP on resolution levels */
494     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
495         int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 15444-1:2002 B.5
496         Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
497
498         /* Compute borders for each resolution level.
499          * Computation of trx_0, trx_1, try_0 and try_1.
500          * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
501         for (i = 0; i < 2; i++)
502             for (j = 0; j < 2; j++)
503                 reslevel->coord[i][j] =
504                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
505         // update precincts size: 2^n value
506         reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
507         reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
508
509         /* Number of bands for each resolution level */
510         if (reslevelno == 0)
511             reslevel->nbands = 1;
512         else
513             reslevel->nbands = 3;
514
515         /* Number of precincts which span the tile for resolution level reslevelno
516          * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
517          * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
518          * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
519          * for Dcinema profiles in JPEG 2000
520          * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
521          * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
522         if (reslevel->coord[0][1] == reslevel->coord[0][0])
523             reslevel->num_precincts_x = 0;
524         else
525             reslevel->num_precincts_x =
526                 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
527                                         reslevel->log2_prec_width) -
528                 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
529
530         if (reslevel->coord[1][1] == reslevel->coord[1][0])
531             reslevel->num_precincts_y = 0;
532         else
533             reslevel->num_precincts_y =
534                 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
535                                         reslevel->log2_prec_height) -
536                 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
537
538         reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
539         if (!reslevel->band)
540             return AVERROR(ENOMEM);
541
542         for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
543             ret = init_band(avctx, reslevel,
544                             comp, codsty, qntsty,
545                             bandno, gbandno, reslevelno,
546                             cbps, dx, dy);
547             if (ret < 0)
548                 return ret;
549         }
550     }
551     return 0;
552 }
553
554 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
555 {
556     int reslevelno, bandno, cblkno, precno;
557     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
558         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
559         for (bandno = 0; bandno < rlevel->nbands; bandno++) {
560             Jpeg2000Band *band = rlevel->band + bandno;
561             for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
562                 Jpeg2000Prec *prec = band->prec + precno;
563                 tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
564                 tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
565                 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
566                     Jpeg2000Cblk *cblk = prec->cblk + cblkno;
567                     cblk->length = 0;
568                     cblk->lblock = 3;
569                 }
570             }
571         }
572     }
573 }
574
575 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
576 {
577     int reslevelno, bandno, precno;
578     for (reslevelno = 0;
579          comp->reslevel && reslevelno < codsty->nreslevels;
580          reslevelno++) {
581         Jpeg2000ResLevel *reslevel;
582
583         if (!comp->reslevel)
584             continue;
585
586         reslevel = comp->reslevel + reslevelno;
587         for (bandno = 0; bandno < reslevel->nbands; bandno++) {
588             Jpeg2000Band *band;
589
590             if (!reslevel->band)
591                 continue;
592
593             band = reslevel->band + bandno;
594             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
595                 if (band->prec) {
596                     Jpeg2000Prec *prec = band->prec + precno;
597                     av_freep(&prec->zerobits);
598                     av_freep(&prec->cblkincl);
599                     av_freep(&prec->cblk);
600                 }
601             }
602
603             av_freep(&band->prec);
604         }
605         av_freep(&reslevel->band);
606     }
607
608     ff_dwt_destroy(&comp->dwt);
609     av_freep(&comp->reslevel);
610     av_freep(&comp->i_data);
611     av_freep(&comp->f_data);
612 }