Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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