You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
22KB

  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. * @file
  24. * JPEG 2000 image encoder and decoder common functions
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/mem.h"
  29. #include "avcodec.h"
  30. #include "jpeg2000.h"
  31. #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
  32. /* tag tree routines */
  33. /* allocate the memory for tag tree */
  34. static int32_t tag_tree_size(uint16_t w, uint16_t h)
  35. {
  36. uint32_t res = 0;
  37. while (w > 1 || h > 1) {
  38. res += w * h;
  39. av_assert0(res + 1 < INT32_MAX);
  40. w = (w + 1) >> 1;
  41. h = (h + 1) >> 1;
  42. }
  43. return (int32_t)(res + 1);
  44. }
  45. static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
  46. {
  47. int pw = w, ph = h;
  48. Jpeg2000TgtNode *res, *t, *t2;
  49. int32_t tt_size;
  50. tt_size = tag_tree_size(w, h);
  51. t = res = av_mallocz_array(tt_size, sizeof(*t));
  52. if (!res)
  53. return NULL;
  54. while (w > 1 || h > 1) {
  55. int i, j;
  56. pw = w;
  57. ph = h;
  58. w = (w + 1) >> 1;
  59. h = (h + 1) >> 1;
  60. t2 = t + pw * ph;
  61. for (i = 0; i < ph; i++)
  62. for (j = 0; j < pw; j++)
  63. t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
  64. t = t2;
  65. }
  66. t[0].parent = NULL;
  67. return res;
  68. }
  69. static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
  70. {
  71. int i, siz = tag_tree_size(w, h);
  72. for (i = 0; i < siz; i++) {
  73. t[i].val = 0;
  74. t[i].vis = 0;
  75. }
  76. }
  77. uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  78. static int getsigctxno(int flag, int bandno)
  79. {
  80. int h, v, d;
  81. h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
  82. ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
  83. v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
  84. ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
  85. d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
  86. ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
  87. ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
  88. ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
  89. if (bandno < 3) {
  90. if (bandno == 1)
  91. FFSWAP(int, h, v);
  92. if (h == 2) return 8;
  93. if (h == 1) {
  94. if (v >= 1) return 7;
  95. if (d >= 1) return 6;
  96. return 5;
  97. }
  98. if (v == 2) return 4;
  99. if (v == 1) return 3;
  100. if (d >= 2) return 2;
  101. if (d == 1) return 1;
  102. } else {
  103. if (d >= 3) return 8;
  104. if (d == 2) {
  105. if (h+v >= 1) return 7;
  106. return 6;
  107. }
  108. if (d == 1) {
  109. if (h+v >= 2) return 5;
  110. if (h+v == 1) return 4;
  111. return 3;
  112. }
  113. if (h+v >= 2) return 2;
  114. if (h+v == 1) return 1;
  115. }
  116. return 0;
  117. }
  118. uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
  119. static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
  120. static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
  121. static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
  122. static int getsgnctxno(int flag, uint8_t *xorbit)
  123. {
  124. int vcontrib, hcontrib;
  125. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
  126. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
  127. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
  128. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
  129. *xorbit = xorbittab[hcontrib][vcontrib];
  130. return ctxlbltab[hcontrib][vcontrib];
  131. }
  132. void ff_jpeg2000_init_tier1_luts(void)
  133. {
  134. int i, j;
  135. for (i = 0; i < 256; i++)
  136. for (j = 0; j < 4; j++)
  137. ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
  138. for (i = 0; i < 16; i++)
  139. for (j = 0; j < 16; j++)
  140. ff_jpeg2000_sgnctxno_lut[i][j] =
  141. getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
  142. }
  143. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
  144. int negative)
  145. {
  146. x++;
  147. y++;
  148. t1->flags[y][x] |= JPEG2000_T1_SIG;
  149. if (negative) {
  150. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  151. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  152. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  153. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  154. } else {
  155. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
  156. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
  157. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
  158. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
  159. }
  160. t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
  161. t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
  162. t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
  163. t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
  164. }
  165. static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
  166. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  167. Jpeg2000CodingStyle *codsty,
  168. Jpeg2000QuantStyle *qntsty,
  169. int cbps, int dx, int dy,
  170. AVCodecContext *avctx)
  171. {
  172. uint8_t log2_band_prec_width, log2_band_prec_height;
  173. int reslevelno, bandno, gbandno = 0, ret, i, j;
  174. uint32_t csize;
  175. if (codsty->nreslevels2decode <= 0) {
  176. av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
  180. codsty->nreslevels2decode - 1,
  181. codsty->transform))
  182. return ret;
  183. // component size comp->coord is uint16_t so ir cannot overflow
  184. csize = (comp->coord[0][1] - comp->coord[0][0]) *
  185. (comp->coord[1][1] - comp->coord[1][0]);
  186. if (codsty->transform == FF_DWT97) {
  187. comp->i_data = NULL;
  188. comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
  189. if (!comp->f_data)
  190. return AVERROR(ENOMEM);
  191. } else {
  192. comp->f_data = NULL;
  193. comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
  194. if (!comp->i_data)
  195. return AVERROR(ENOMEM);
  196. }
  197. comp->reslevel = av_calloc(codsty->nreslevels, sizeof(*comp->reslevel));
  198. if (!comp->reslevel)
  199. return AVERROR(ENOMEM);
  200. /* LOOP on resolution levels */
  201. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  202. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  203. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  204. /* Compute borders for each resolution level.
  205. * Computation of trx_0, trx_1, try_0 and try_1.
  206. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  207. for (i = 0; i < 2; i++)
  208. for (j = 0; j < 2; j++)
  209. reslevel->coord[i][j] =
  210. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  211. // update precincts size: 2^n value
  212. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  213. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  214. /* Number of bands for each resolution level */
  215. if (reslevelno == 0)
  216. reslevel->nbands = 1;
  217. else
  218. reslevel->nbands = 3;
  219. /* Number of precincts which span the tile for resolution level reslevelno
  220. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  221. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  222. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  223. * for Dcinema profiles in JPEG 2000
  224. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  225. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  226. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  227. reslevel->num_precincts_x = 0;
  228. else
  229. reslevel->num_precincts_x =
  230. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  231. reslevel->log2_prec_width) -
  232. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  233. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  234. reslevel->num_precincts_y = 0;
  235. else
  236. reslevel->num_precincts_y =
  237. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  238. reslevel->log2_prec_height) -
  239. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  240. reslevel->band = av_calloc(reslevel->nbands, sizeof(*reslevel->band));
  241. if (!reslevel->band)
  242. return AVERROR(ENOMEM);
  243. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  244. Jpeg2000Band *band = reslevel->band + bandno;
  245. int cblkno, precno;
  246. int nb_precincts;
  247. /* TODO: Implementation of quantization step not finished,
  248. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  249. switch (qntsty->quantsty) {
  250. uint8_t gain;
  251. int numbps;
  252. case JPEG2000_QSTY_NONE:
  253. /* TODO: to verify. No quantization in this case */
  254. band->f_stepsize = 1;
  255. break;
  256. case JPEG2000_QSTY_SI:
  257. /*TODO: Compute formula to implement. */
  258. numbps = cbps +
  259. lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
  260. band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
  261. 2 + numbps - qntsty->expn[gbandno]);
  262. break;
  263. case JPEG2000_QSTY_SE:
  264. /* Exponent quantization step.
  265. * Formula:
  266. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  267. * R_b = R_I + log2 (gain_b )
  268. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  269. /* TODO/WARN: value of log2 (gain_b ) not taken into account
  270. * but it works (compared to OpenJPEG). Why?
  271. * Further investigation needed. */
  272. gain = cbps;
  273. band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  274. band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
  275. break;
  276. default:
  277. band->f_stepsize = 0;
  278. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  279. break;
  280. }
  281. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  282. * If not set output of entropic decoder is not correct. */
  283. if (!av_codec_is_encoder(avctx->codec))
  284. band->f_stepsize *= 0.5;
  285. band->i_stepsize = band->f_stepsize * (1 << 15);
  286. /* computation of tbx_0, tbx_1, tby_0, tby_1
  287. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  288. * codeblock width and height is computed for
  289. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  290. if (reslevelno == 0) {
  291. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  292. for (i = 0; i < 2; i++)
  293. for (j = 0; j < 2; j++)
  294. band->coord[i][j] =
  295. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0],
  296. declvl - 1);
  297. log2_band_prec_width = reslevel->log2_prec_width;
  298. log2_band_prec_height = reslevel->log2_prec_height;
  299. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  300. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  301. reslevel->log2_prec_width);
  302. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  303. reslevel->log2_prec_height);
  304. } else {
  305. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  306. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  307. for (i = 0; i < 2; i++)
  308. for (j = 0; j < 2; j++)
  309. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  310. band->coord[i][j] =
  311. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] -
  312. (((bandno + 1 >> i) & 1) << declvl - 1),
  313. declvl);
  314. /* TODO: Manage case of 3 band offsets here or
  315. * in coding/decoding function? */
  316. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  317. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  318. reslevel->log2_prec_width - 1);
  319. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  320. reslevel->log2_prec_height - 1);
  321. log2_band_prec_width = reslevel->log2_prec_width - 1;
  322. log2_band_prec_height = reslevel->log2_prec_height - 1;
  323. }
  324. for (j = 0; j < 2; j++)
  325. band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
  326. for (j = 0; j < 2; j++)
  327. band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
  328. band->prec = av_calloc(reslevel->num_precincts_x *
  329. (uint64_t)reslevel->num_precincts_y,
  330. sizeof(*band->prec));
  331. if (!band->prec)
  332. return AVERROR(ENOMEM);
  333. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  334. for (precno = 0; precno < nb_precincts; precno++) {
  335. Jpeg2000Prec *prec = band->prec + precno;
  336. /* TODO: Explain formula for JPEG200 DCINEMA. */
  337. /* TODO: Verify with previous count of codeblocks per band */
  338. /* Compute P_x0 */
  339. prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
  340. (1 << log2_band_prec_width);
  341. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  342. /* Compute P_y0 */
  343. prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
  344. (1 << log2_band_prec_height);
  345. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  346. /* Compute P_x1 */
  347. prec->coord[0][1] = prec->coord[0][0] +
  348. (1 << log2_band_prec_width);
  349. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  350. /* Compute P_y1 */
  351. prec->coord[1][1] = prec->coord[1][0] +
  352. (1 << log2_band_prec_height);
  353. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  354. prec->nb_codeblocks_width =
  355. ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
  356. prec->coord[0][0],
  357. band->log2_cblk_width);
  358. prec->nb_codeblocks_height =
  359. ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
  360. prec->coord[1][0],
  361. band->log2_cblk_height);
  362. /* Tag trees initialization */
  363. prec->cblkincl =
  364. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  365. prec->nb_codeblocks_height);
  366. if (!prec->cblkincl)
  367. return AVERROR(ENOMEM);
  368. prec->zerobits =
  369. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  370. prec->nb_codeblocks_height);
  371. if (!prec->zerobits)
  372. return AVERROR(ENOMEM);
  373. prec->cblk = av_mallocz_array(prec->nb_codeblocks_width *
  374. (uint64_t)prec->nb_codeblocks_height,
  375. sizeof(*prec->cblk));
  376. if (!prec->cblk)
  377. return AVERROR(ENOMEM);
  378. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  379. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  380. uint16_t Cx0, Cy0;
  381. /* Compute coordinates of codeblocks */
  382. /* Compute Cx0*/
  383. Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
  384. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  385. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  386. /* Compute Cy0*/
  387. Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
  388. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  389. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  390. /* Compute Cx1 */
  391. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  392. prec->coord[0][1]);
  393. /* Compute Cy1 */
  394. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  395. prec->coord[1][1]);
  396. /* Update code-blocks coordinates according sub-band position */
  397. if ((bandno + !!reslevelno) & 1) {
  398. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  399. comp->reslevel[reslevelno-1].coord[0][0];
  400. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  401. comp->reslevel[reslevelno-1].coord[0][0];
  402. }
  403. if ((bandno + !!reslevelno) & 2) {
  404. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  405. comp->reslevel[reslevelno-1].coord[1][0];
  406. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  407. comp->reslevel[reslevelno-1].coord[1][0];
  408. }
  409. cblk->zero = 0;
  410. cblk->lblock = 3;
  411. cblk->length = 0;
  412. cblk->lengthinc = 0;
  413. cblk->npasses = 0;
  414. }
  415. }
  416. }
  417. }
  418. return 0;
  419. }
  420. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  421. {
  422. int reslevelno, bandno, cblkno, precno;
  423. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  424. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  425. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  426. Jpeg2000Band *band = rlevel->band + bandno;
  427. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  428. Jpeg2000Prec *prec = band->prec + precno;
  429. tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  430. tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  431. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  432. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  433. cblk->length = 0;
  434. cblk->lblock = 3;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  441. {
  442. int reslevelno, bandno, precno;
  443. for (reslevelno = 0;
  444. comp->reslevel && reslevelno < codsty->nreslevels;
  445. reslevelno++) {
  446. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  447. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  448. if (reslevel->band) {
  449. Jpeg2000Band *band = reslevel->band + bandno;
  450. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  451. if (band->prec) {
  452. Jpeg2000Prec *prec = band->prec + precno;
  453. av_freep(&prec->zerobits);
  454. av_freep(&prec->cblkincl);
  455. av_freep(&prec->cblk);
  456. }
  457. }
  458. av_freep(&band->prec);
  459. }
  460. }
  461. av_freep(&reslevel->band);
  462. }
  463. ff_dwt_destroy(&comp->dwt);
  464. av_freep(&comp->reslevel);
  465. av_freep(&comp->i_data);
  466. av_freep(&comp->f_data);
  467. }