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