LCOV - code coverage report
Current view: top level - libavcodec - jpeg2000.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 303 333 91.0 %
Date: 2017-12-16 01:21:47 Functions: 13 13 100.0 %

          Line data    Source code
       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      305734 : static int32_t tag_tree_size(int w, int h)
      43             : {
      44      305734 :     int64_t res = 0;
      45      853162 :     while (w > 1 || h > 1) {
      46      241694 :         res += w * (int64_t)h;
      47      241694 :         av_assert0(res + 1 < INT32_MAX);
      48      241694 :         w = (w + 1) >> 1;
      49      241694 :         h = (h + 1) >> 1;
      50             :     }
      51      305734 :     return (int32_t)(res + 1);
      52             : }
      53             : 
      54      157534 : static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
      55             : {
      56      157534 :     int pw = w, ph = h;
      57             :     Jpeg2000TgtNode *res, *t, *t2;
      58             :     int32_t tt_size;
      59             : 
      60      157534 :     tt_size = tag_tree_size(w, h);
      61             : 
      62      157534 :     t = res = av_mallocz_array(tt_size, sizeof(*t));
      63      157534 :     if (!res)
      64           0 :         return NULL;
      65             : 
      66      441562 :     while (w > 1 || h > 1) {
      67             :         int i, j;
      68      126494 :         pw = w;
      69      126494 :         ph = h;
      70             : 
      71      126494 :         w  = (w + 1) >> 1;
      72      126494 :         h  = (h + 1) >> 1;
      73      126494 :         t2 = t + pw * ph;
      74             : 
      75      445158 :         for (i = 0; i < ph; i++)
      76     1408754 :             for (j = 0; j < pw; j++)
      77     1090090 :                 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
      78             : 
      79      126494 :         t = t2;
      80             :     }
      81      157534 :     t[0].parent = NULL;
      82      157534 :     return res;
      83             : }
      84             : 
      85      148200 : static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
      86             : {
      87      148200 :     int i, siz = tag_tree_size(w, h);
      88             : 
      89     1291800 :     for (i = 0; i < siz; i++) {
      90     1143600 :         t[i].val = 0;
      91     1143600 :         t[i].vis = 0;
      92             :     }
      93      148200 : }
      94             : 
      95             : uint8_t ff_jpeg2000_sigctxno_lut[256][4];
      96             : 
      97     5738496 : static int getsigctxno(int flag, int bandno)
      98             : {
      99             :     int h, v, d;
     100             : 
     101    11476992 :     h = ((flag & JPEG2000_T1_SIG_E)  ? 1 : 0) +
     102     5738496 :         ((flag & JPEG2000_T1_SIG_W)  ? 1 : 0);
     103    11476992 :     v = ((flag & JPEG2000_T1_SIG_N)  ? 1 : 0) +
     104     5738496 :         ((flag & JPEG2000_T1_SIG_S)  ? 1 : 0);
     105    17215488 :     d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
     106    11476992 :         ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
     107     5738496 :         ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
     108     5738496 :         ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
     109             : 
     110     5738496 :     if (bandno < 3) {
     111     4303872 :         if (bandno == 1)
     112     1434624 :             FFSWAP(int, h, v);
     113     4303872 :         if (h == 2) return 8;
     114     3227904 :         if (h == 1) {
     115     2151936 :             if (v >= 1) return 7;
     116      537984 :             if (d >= 1) return 6;
     117       33624 :             return 5;
     118             :         }
     119     1075968 :         if (v == 2) return 4;
     120      806976 :         if (v == 1) return 3;
     121      268992 :         if (d >= 2) return 2;
     122       84060 :         if (d == 1) return 1;
     123             :     } else {
     124     1434624 :         if (d >= 3) return 8;
     125      986304 :         if (d == 2) {
     126      537984 :             if (h+v >= 1) return 7;
     127       33624 :             return 6;
     128             :         }
     129      448320 :         if (d == 1) {
     130      358656 :             if (h+v >= 2) return 5;
     131      112080 :             if (h+v == 1) return 4;
     132       22416 :             return 3;
     133             :         }
     134       89664 :         if (h+v >= 2) return 2;
     135       28020 :         if (h+v == 1) return 1;
     136             :     }
     137       22416 :     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     1434624 : static int getsgnctxno(int flag, uint8_t *xorbit)
     147             : {
     148             :     int vcontrib, hcontrib;
     149             : 
     150     2869248 :     hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
     151     1434624 :                          [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
     152     2869248 :     vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
     153     1434624 :                          [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
     154     1434624 :     *xorbit = xorbittab[hcontrib][vcontrib];
     155             : 
     156     1434624 :     return ctxlbltab[hcontrib][vcontrib];
     157             : }
     158             : 
     159        5604 : void av_cold ff_jpeg2000_init_tier1_luts(void)
     160             : {
     161             :     int i, j;
     162     1440228 :     for (i = 0; i < 256; i++)
     163     7173120 :         for (j = 0; j < 4; j++)
     164     5738496 :             ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
     165       95268 :     for (i = 0; i < 16; i++)
     166     1524288 :         for (j = 0; j < 16; j++)
     167     1434624 :             ff_jpeg2000_sgnctxno_lut[i][j] =
     168     1434624 :                 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
     169        5604 : }
     170             : 
     171    97248712 : void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
     172             :                                   int negative)
     173             : {
     174    97248712 :     x++;
     175    97248712 :     y++;
     176    97248712 :     t1->flags[(y) * t1->stride + x] |= JPEG2000_T1_SIG;
     177    97248712 :     if (negative) {
     178    46117506 :         t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
     179    46117506 :         t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
     180    46117506 :         t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
     181    46117506 :         t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
     182             :     } else {
     183    51131206 :         t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
     184    51131206 :         t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
     185    51131206 :         t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
     186    51131206 :         t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
     187             :     }
     188    97248712 :     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
     189    97248712 :     t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
     190    97248712 :     t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
     191    97248712 :     t1->flags[(y - 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_SE;
     192    97248712 : }
     193             : 
     194             : // static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } }; (unused)
     195             : 
     196       77336 : 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       77336 :     switch (qntsty->quantsty) {
     206             :         uint8_t gain;
     207       38532 :     case JPEG2000_QSTY_NONE:
     208             :         /* TODO: to verify. No quantization in this case */
     209       38532 :         band->f_stepsize = 1;
     210       38532 :         break;
     211       38804 :     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       38804 :         gain            = cbps;
     225       38804 :         band->f_stepsize  = ff_exp2fi(gain - qntsty->expn[gbandno]);
     226       38804 :         band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
     227       38804 :         break;
     228           0 :     default:
     229           0 :         band->f_stepsize = 0;
     230           0 :         av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
     231           0 :         break;
     232             :     }
     233       77336 :     if (codsty->transform != FF_DWT53) {
     234       38804 :         int lband = 0;
     235       38804 :         switch (bandno + (reslevelno > 0)) {
     236       24506 :             case 1:
     237             :             case 2:
     238       24506 :                 band->f_stepsize *= F_LFTG_X * 2;
     239       24506 :                 lband = 1;
     240       24506 :                 break;
     241       12253 :             case 3:
     242       12253 :                 band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
     243       12253 :                 break;
     244             :         }
     245       38804 :         if (codsty->transform == FF_DWT97) {
     246         128 :             band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
     247             :         }
     248             :     }
     249             : 
     250       77336 :     band->i_stepsize = band->f_stepsize * (1 << 15);
     251             : 
     252             :     /* FIXME: In OpenJPEG code stepsize = stepsize * 0.5. Why?
     253             :      * If not set output of entropic decoder is not correct. */
     254       77336 :     if (!av_codec_is_encoder(avctx->codec))
     255       75854 :         band->f_stepsize *= 0.5;
     256       77336 : }
     257             : 
     258       78767 : static int init_prec(Jpeg2000Band *band,
     259             :                      Jpeg2000ResLevel *reslevel,
     260             :                      Jpeg2000Component *comp,
     261             :                      int precno, int bandno, int reslevelno,
     262             :                      int log2_band_prec_width,
     263             :                      int log2_band_prec_height)
     264             : {
     265       78767 :     Jpeg2000Prec *prec = band->prec + precno;
     266             :     int nb_codeblocks, cblkno;
     267             : 
     268       78767 :     prec->decoded_layers = 0;
     269             : 
     270             :     /* TODO: Explain formula for JPEG200 DCINEMA. */
     271             :     /* TODO: Verify with previous count of codeblocks per band */
     272             : 
     273             :     /* Compute P_x0 */
     274       78767 :     prec->coord[0][0] = ((band->coord[0][0] >> log2_band_prec_width) + precno % reslevel->num_precincts_x) *
     275             :                         (1 << log2_band_prec_width);
     276             : 
     277             :     /* Compute P_y0 */
     278       78767 :     prec->coord[1][0] = ((band->coord[1][0] >> log2_band_prec_height) + precno / reslevel->num_precincts_x) *
     279             :                         (1 << log2_band_prec_height);
     280             : 
     281             :     /* Compute P_x1 */
     282      157534 :     prec->coord[0][1] = prec->coord[0][0] +
     283       78767 :                         (1 << log2_band_prec_width);
     284       78767 :     prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
     285       78767 :     prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
     286             : 
     287             :     /* Compute P_y1 */
     288      157534 :     prec->coord[1][1] = prec->coord[1][0] +
     289       78767 :                         (1 << log2_band_prec_height);
     290       78767 :     prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
     291       78767 :     prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
     292             : 
     293       78767 :     prec->nb_codeblocks_width =
     294       78767 :         ff_jpeg2000_ceildivpow2(prec->coord[0][1],
     295       78767 :                                 band->log2_cblk_width)
     296       78767 :         - (prec->coord[0][0] >> band->log2_cblk_width);
     297       78767 :     prec->nb_codeblocks_height =
     298       78767 :         ff_jpeg2000_ceildivpow2(prec->coord[1][1],
     299       78767 :                                 band->log2_cblk_height)
     300       78767 :         - (prec->coord[1][0] >> band->log2_cblk_height);
     301             : 
     302             : 
     303             :     /* Tag trees initialization */
     304       78767 :     prec->cblkincl =
     305       78767 :         ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
     306             :                                   prec->nb_codeblocks_height);
     307       78767 :     if (!prec->cblkincl)
     308           0 :         return AVERROR(ENOMEM);
     309             : 
     310       78767 :     prec->zerobits =
     311       78767 :         ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
     312             :                                   prec->nb_codeblocks_height);
     313       78767 :     if (!prec->zerobits)
     314           0 :         return AVERROR(ENOMEM);
     315             : 
     316       78767 :     if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
     317           0 :         prec->cblk = NULL;
     318           0 :         return AVERROR(ENOMEM);
     319             :     }
     320       78767 :     nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
     321       78767 :     prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
     322       78767 :     if (!prec->cblk)
     323           0 :         return AVERROR(ENOMEM);
     324      533858 :     for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
     325      455091 :         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
     326             :         int Cx0, Cy0;
     327             : 
     328             :         /* Compute coordinates of codeblocks */
     329             :         /* Compute Cx0*/
     330      455091 :         Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
     331      455091 :         Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << band->log2_cblk_width);
     332      455091 :         cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
     333             : 
     334             :         /* Compute Cy0*/
     335      455091 :         Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
     336      455091 :         Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << band->log2_cblk_height);
     337      455091 :         cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
     338             : 
     339             :         /* Compute Cx1 */
     340      455091 :         cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
     341             :                                   prec->coord[0][1]);
     342             : 
     343             :         /* Compute Cy1 */
     344      455091 :         cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
     345             :                                   prec->coord[1][1]);
     346             :         /* Update code-blocks coordinates according sub-band position */
     347      455091 :         if ((bandno + !!reslevelno) & 1) {
     348      601316 :             cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
     349      300658 :                                  comp->reslevel[reslevelno-1].coord[0][0];
     350      601316 :             cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
     351      300658 :                                  comp->reslevel[reslevelno-1].coord[0][0];
     352             :         }
     353      455091 :         if ((bandno + !!reslevelno) & 2) {
     354      601316 :             cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
     355      300658 :                                  comp->reslevel[reslevelno-1].coord[1][0];
     356      601316 :             cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
     357      300658 :                                  comp->reslevel[reslevelno-1].coord[1][0];
     358             :         }
     359             : 
     360      455091 :         cblk->lblock    = 3;
     361      455091 :         cblk->length    = 0;
     362      455091 :         memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
     363      455091 :         cblk->npasses   = 0;
     364             :     }
     365             : 
     366       78767 :     return 0;
     367             : }
     368             : 
     369       77336 : 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       77336 :     Jpeg2000Band *band = reslevel->band + bandno;
     378             :     uint8_t log2_band_prec_width, log2_band_prec_height;
     379       77336 :     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       77336 :     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       77336 :     if (reslevelno == 0) {
     391             :         /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
     392       12219 :         for (i = 0; i < 2; i++)
     393       24438 :             for (j = 0; j < 2; j++)
     394       16292 :                 band->coord[i][j] =
     395       16292 :                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
     396             :                                             declvl - 1);
     397        4073 :         log2_band_prec_width  = reslevel->log2_prec_width;
     398        4073 :         log2_band_prec_height = reslevel->log2_prec_height;
     399             :         /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
     400        4073 :         band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
     401             :                                        reslevel->log2_prec_width);
     402        4073 :         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      219789 :         for (i = 0; i < 2; i++)
     408      439578 :             for (j = 0; j < 2; j++)
     409             :                 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
     410      293052 :                 band->coord[i][j] =
     411      586104 :                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
     412      293052 :                                             (((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       73263 :         band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
     419             :                                        reslevel->log2_prec_width - 1);
     420       73263 :         band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
     421             :                                        reslevel->log2_prec_height - 1);
     422             : 
     423       73263 :         log2_band_prec_width  = reslevel->log2_prec_width  - 1;
     424       73263 :         log2_band_prec_height = reslevel->log2_prec_height - 1;
     425             :     }
     426             : 
     427       77336 :     if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
     428           0 :         band->prec = NULL;
     429           0 :         return AVERROR(ENOMEM);
     430             :     }
     431       77336 :     nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
     432       77336 :     band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
     433       77336 :     if (!band->prec)
     434           0 :         return AVERROR(ENOMEM);
     435             : 
     436      156103 :     for (precno = 0; precno < nb_precincts; precno++) {
     437       78767 :         ret = init_prec(band, reslevel, comp,
     438             :                         precno, bandno, reslevelno,
     439             :                         log2_band_prec_width, log2_band_prec_height);
     440       78767 :         if (ret < 0)
     441           0 :             return ret;
     442             :     }
     443             : 
     444       77336 :     return 0;
     445             : }
     446             : 
     447        4073 : 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        4073 :     int reslevelno, bandno, gbandno = 0, ret, i, j;
     454             :     uint32_t csize;
     455             : 
     456        4073 :     if (codsty->nreslevels2decode <= 0) {
     457           0 :         av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
     458           0 :         return AVERROR_INVALIDDATA;
     459             :     }
     460             : 
     461        8146 :     if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
     462        4073 :                                    codsty->nreslevels2decode - 1,
     463        4073 :                                    codsty->transform))
     464           0 :         return ret;
     465             : 
     466        4073 :     if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
     467        4073 :                             comp->coord[1][1] - comp->coord[1][0], 0, avctx))
     468           0 :         return AVERROR_INVALIDDATA;
     469        8146 :     csize = (comp->coord[0][1] - comp->coord[0][0]) *
     470        4073 :             (comp->coord[1][1] - comp->coord[1][0]);
     471        8146 :     if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
     472        4073 :         comp->coord[1][1] - comp->coord[1][0] > 32768) {
     473           0 :         av_log(avctx, AV_LOG_ERROR, "component size too large\n");
     474           0 :         return AVERROR_PATCHWELCOME;
     475             :     }
     476             : 
     477        4073 :     if (codsty->transform == FF_DWT97) {
     478           8 :         csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
     479           8 :         comp->i_data = NULL;
     480           8 :         comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
     481           8 :         if (!comp->f_data)
     482           0 :             return AVERROR(ENOMEM);
     483             :     } else {
     484        4065 :         csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
     485        4065 :         comp->f_data = NULL;
     486        4065 :         comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
     487        4065 :         if (!comp->i_data)
     488           0 :             return AVERROR(ENOMEM);
     489             :     }
     490        4073 :     comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
     491        4073 :     if (!comp->reslevel)
     492           0 :         return AVERROR(ENOMEM);
     493             :     /* LOOP on resolution levels */
     494       32567 :     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
     495       28494 :         int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 15444-1:2002 B.5
     496       28494 :         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       85482 :         for (i = 0; i < 2; i++)
     502      170964 :             for (j = 0; j < 2; j++)
     503      113976 :                 reslevel->coord[i][j] =
     504      113976 :                     ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
     505             :         // update precincts size: 2^n value
     506       28494 :         reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
     507       28494 :         reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
     508       28494 :         if (!reslevel->log2_prec_width || !reslevel->log2_prec_height) {
     509           0 :             return AVERROR_INVALIDDATA;
     510             :         }
     511             : 
     512             :         /* Number of bands for each resolution level */
     513       28494 :         if (reslevelno == 0)
     514        4073 :             reslevel->nbands = 1;
     515             :         else
     516       24421 :             reslevel->nbands = 3;
     517             : 
     518             :         /* Number of precincts which span the tile for resolution level reslevelno
     519             :          * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
     520             :          * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
     521             :          * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
     522             :          * for Dcinema profiles in JPEG 2000
     523             :          * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
     524             :          * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
     525       28494 :         if (reslevel->coord[0][1] == reslevel->coord[0][0])
     526           0 :             reslevel->num_precincts_x = 0;
     527             :         else
     528       28494 :             reslevel->num_precincts_x =
     529       28494 :                 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
     530       56988 :                                         reslevel->log2_prec_width) -
     531       28494 :                 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
     532             : 
     533       28494 :         if (reslevel->coord[1][1] == reslevel->coord[1][0])
     534           0 :             reslevel->num_precincts_y = 0;
     535             :         else
     536       28494 :             reslevel->num_precincts_y =
     537       28494 :                 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
     538       56988 :                                         reslevel->log2_prec_height) -
     539       28494 :                 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
     540             : 
     541       28494 :         reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
     542       28494 :         if (!reslevel->band)
     543           0 :             return AVERROR(ENOMEM);
     544             : 
     545      105830 :         for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
     546       77336 :             ret = init_band(avctx, reslevel,
     547             :                             comp, codsty, qntsty,
     548             :                             bandno, gbandno, reslevelno,
     549             :                             cbps, dx, dy);
     550       77336 :             if (ret < 0)
     551           0 :                 return ret;
     552             :         }
     553             :     }
     554        4073 :     return 0;
     555             : }
     556             : 
     557        3900 : void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
     558             : {
     559             :     int reslevelno, bandno, cblkno, precno;
     560       31200 :     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
     561       27300 :         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
     562      101400 :         for (bandno = 0; bandno < rlevel->nbands; bandno++) {
     563       74100 :             Jpeg2000Band *band = rlevel->band + bandno;
     564      148200 :             for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
     565       74100 :                 Jpeg2000Prec *prec = band->prec + precno;
     566       74100 :                 tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
     567       74100 :                 tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
     568      491100 :                 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
     569      417000 :                     Jpeg2000Cblk *cblk = prec->cblk + cblkno;
     570      417000 :                     cblk->length = 0;
     571      417000 :                     cblk->lblock = 3;
     572             :                 }
     573             :             }
     574             :         }
     575             :     }
     576        3900 : }
     577             : 
     578        4073 : void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
     579             : {
     580             :     int reslevelno, bandno, precno;
     581       36640 :     for (reslevelno = 0;
     582       65134 :          comp->reslevel && reslevelno < codsty->nreslevels;
     583       28494 :          reslevelno++) {
     584             :         Jpeg2000ResLevel *reslevel;
     585             : 
     586       28494 :         if (!comp->reslevel)
     587           0 :             continue;
     588             : 
     589       28494 :         reslevel = comp->reslevel + reslevelno;
     590      105830 :         for (bandno = 0; bandno < reslevel->nbands; bandno++) {
     591             :             Jpeg2000Band *band;
     592             : 
     593       77336 :             if (!reslevel->band)
     594           0 :                 continue;
     595             : 
     596       77336 :             band = reslevel->band + bandno;
     597      156103 :             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
     598       78767 :                 if (band->prec) {
     599       78767 :                     Jpeg2000Prec *prec = band->prec + precno;
     600       78767 :                     int nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
     601             : 
     602       78767 :                     av_freep(&prec->zerobits);
     603       78767 :                     av_freep(&prec->cblkincl);
     604       78767 :                     if (prec->cblk) {
     605             :                         int cblkno;
     606      533858 :                         for (cblkno = 0; cblkno < nb_code_blocks; cblkno ++) {
     607      455091 :                             Jpeg2000Cblk *cblk = &prec->cblk[cblkno];
     608      455091 :                             av_freep(&cblk->data);
     609      455091 :                             av_freep(&cblk->passes);
     610             :                         }
     611       78767 :                         av_freep(&prec->cblk);
     612             :                     }
     613             :                 }
     614             :             }
     615             : 
     616       77336 :             av_freep(&band->prec);
     617             :         }
     618       28494 :         av_freep(&reslevel->band);
     619             :     }
     620             : 
     621        4073 :     ff_dwt_destroy(&comp->dwt);
     622        4073 :     av_freep(&comp->reslevel);
     623        4073 :     av_freep(&comp->i_data);
     624        4073 :     av_freep(&comp->f_data);
     625        4073 : }

Generated by: LCOV version 1.13