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.

539 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/attributes.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/mem.h"
  30. #include "avcodec.h"
  31. #include "jpeg2000.h"
  32. #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
  33. /* tag tree routines */
  34. /* allocate the memory for tag tree */
  35. static int32_t tag_tree_size(uint16_t w, uint16_t h)
  36. {
  37. uint32_t res = 0;
  38. while (w > 1 || h > 1) {
  39. res += w * h;
  40. av_assert0(res + 1 < INT32_MAX);
  41. w = (w + 1) >> 1;
  42. h = (h + 1) >> 1;
  43. }
  44. return (int32_t)(res + 1);
  45. }
  46. static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
  47. {
  48. int pw = w, ph = h;
  49. Jpeg2000TgtNode *res, *t, *t2;
  50. int32_t tt_size;
  51. tt_size = tag_tree_size(w, h);
  52. t = res = av_mallocz_array(tt_size, sizeof(*t));
  53. if (!res)
  54. return NULL;
  55. while (w > 1 || h > 1) {
  56. int i, j;
  57. pw = w;
  58. ph = h;
  59. w = (w + 1) >> 1;
  60. h = (h + 1) >> 1;
  61. t2 = t + pw * ph;
  62. for (i = 0; i < ph; i++)
  63. for (j = 0; j < pw; j++)
  64. t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
  65. t = t2;
  66. }
  67. t[0].parent = NULL;
  68. return res;
  69. }
  70. static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
  71. {
  72. int i, siz = tag_tree_size(w, h);
  73. for (i = 0; i < siz; i++) {
  74. t[i].val = 0;
  75. t[i].vis = 0;
  76. }
  77. }
  78. uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  79. static int getsigctxno(int flag, int bandno)
  80. {
  81. int h, v, d;
  82. h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
  83. ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
  84. v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
  85. ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
  86. d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
  87. ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
  88. ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
  89. ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
  90. if (bandno < 3) {
  91. if (bandno == 1)
  92. FFSWAP(int, h, v);
  93. if (h == 2) return 8;
  94. if (h == 1) {
  95. if (v >= 1) return 7;
  96. if (d >= 1) return 6;
  97. return 5;
  98. }
  99. if (v == 2) return 4;
  100. if (v == 1) return 3;
  101. if (d >= 2) return 2;
  102. if (d == 1) return 1;
  103. } else {
  104. if (d >= 3) return 8;
  105. if (d == 2) {
  106. if (h+v >= 1) return 7;
  107. return 6;
  108. }
  109. if (d == 1) {
  110. if (h+v >= 2) return 5;
  111. if (h+v == 1) return 4;
  112. return 3;
  113. }
  114. if (h+v >= 2) return 2;
  115. if (h+v == 1) return 1;
  116. }
  117. return 0;
  118. }
  119. uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
  120. static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
  121. static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
  122. static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
  123. static int getsgnctxno(int flag, uint8_t *xorbit)
  124. {
  125. int vcontrib, hcontrib;
  126. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
  127. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
  128. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
  129. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
  130. *xorbit = xorbittab[hcontrib][vcontrib];
  131. return ctxlbltab[hcontrib][vcontrib];
  132. }
  133. void av_cold ff_jpeg2000_init_tier1_luts(void)
  134. {
  135. int i, j;
  136. for (i = 0; i < 256; i++)
  137. for (j = 0; j < 4; j++)
  138. ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
  139. for (i = 0; i < 16; i++)
  140. for (j = 0; j < 16; j++)
  141. ff_jpeg2000_sgnctxno_lut[i][j] =
  142. getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
  143. }
  144. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
  145. int negative)
  146. {
  147. x++;
  148. y++;
  149. t1->flags[y][x] |= JPEG2000_T1_SIG;
  150. if (negative) {
  151. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  152. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  153. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  154. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  155. } else {
  156. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
  157. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
  158. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
  159. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
  160. }
  161. t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
  162. t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
  163. t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
  164. t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
  165. }
  166. static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
  167. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  168. Jpeg2000CodingStyle *codsty,
  169. Jpeg2000QuantStyle *qntsty,
  170. int cbps, int dx, int dy,
  171. AVCodecContext *avctx)
  172. {
  173. uint8_t log2_band_prec_width, log2_band_prec_height;
  174. int reslevelno, bandno, gbandno = 0, ret, i, j;
  175. uint32_t csize;
  176. if (codsty->nreslevels2decode <= 0) {
  177. av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
  178. return AVERROR_INVALIDDATA;
  179. }
  180. if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
  181. codsty->nreslevels2decode - 1,
  182. codsty->transform))
  183. return ret;
  184. // component size comp->coord is uint16_t so ir cannot overflow
  185. csize = (comp->coord[0][1] - comp->coord[0][0]) *
  186. (comp->coord[1][1] - comp->coord[1][0]);
  187. if (codsty->transform == FF_DWT97) {
  188. comp->i_data = NULL;
  189. comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
  190. if (!comp->f_data)
  191. return AVERROR(ENOMEM);
  192. } else {
  193. comp->f_data = NULL;
  194. comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
  195. if (!comp->i_data)
  196. return AVERROR(ENOMEM);
  197. }
  198. comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
  199. if (!comp->reslevel)
  200. return AVERROR(ENOMEM);
  201. /* LOOP on resolution levels */
  202. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  203. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  204. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  205. /* Compute borders for each resolution level.
  206. * Computation of trx_0, trx_1, try_0 and try_1.
  207. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  208. for (i = 0; i < 2; i++)
  209. for (j = 0; j < 2; j++)
  210. reslevel->coord[i][j] =
  211. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  212. // update precincts size: 2^n value
  213. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  214. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  215. /* Number of bands for each resolution level */
  216. if (reslevelno == 0)
  217. reslevel->nbands = 1;
  218. else
  219. reslevel->nbands = 3;
  220. /* Number of precincts which span the tile for resolution level reslevelno
  221. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  222. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  223. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  224. * for Dcinema profiles in JPEG 2000
  225. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  226. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  227. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  228. reslevel->num_precincts_x = 0;
  229. else
  230. reslevel->num_precincts_x =
  231. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  232. reslevel->log2_prec_width) -
  233. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  234. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  235. reslevel->num_precincts_y = 0;
  236. else
  237. reslevel->num_precincts_y =
  238. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  239. reslevel->log2_prec_height) -
  240. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  241. reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
  242. if (!reslevel->band)
  243. return AVERROR(ENOMEM);
  244. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  245. Jpeg2000Band *band = reslevel->band + bandno;
  246. int cblkno, precno;
  247. int nb_precincts;
  248. /* TODO: Implementation of quantization step not finished,
  249. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  250. switch (qntsty->quantsty) {
  251. uint8_t gain;
  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. if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
  325. band->prec = NULL;
  326. return AVERROR(ENOMEM);
  327. }
  328. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  329. band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
  330. if (!band->prec)
  331. return AVERROR(ENOMEM);
  332. for (precno = 0; precno < nb_precincts; precno++) {
  333. Jpeg2000Prec *prec = band->prec + precno;
  334. int nb_codeblocks;
  335. /* TODO: Explain formula for JPEG200 DCINEMA. */
  336. /* TODO: Verify with previous count of codeblocks per band */
  337. /* Compute P_x0 */
  338. prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
  339. (1 << log2_band_prec_width);
  340. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  341. /* Compute P_y0 */
  342. prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
  343. (1 << log2_band_prec_height);
  344. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  345. /* Compute P_x1 */
  346. prec->coord[0][1] = prec->coord[0][0] +
  347. (1 << log2_band_prec_width);
  348. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  349. /* Compute P_y1 */
  350. prec->coord[1][1] = prec->coord[1][0] +
  351. (1 << log2_band_prec_height);
  352. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  353. prec->nb_codeblocks_width =
  354. ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
  355. prec->coord[0][0],
  356. band->log2_cblk_width);
  357. prec->nb_codeblocks_height =
  358. ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
  359. prec->coord[1][0],
  360. band->log2_cblk_height);
  361. /* Tag trees initialization */
  362. prec->cblkincl =
  363. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  364. prec->nb_codeblocks_height);
  365. if (!prec->cblkincl)
  366. return AVERROR(ENOMEM);
  367. prec->zerobits =
  368. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  369. prec->nb_codeblocks_height);
  370. if (!prec->zerobits)
  371. return AVERROR(ENOMEM);
  372. if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
  373. prec->cblk = NULL;
  374. return AVERROR(ENOMEM);
  375. }
  376. nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
  377. prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
  378. if (!prec->cblk)
  379. return AVERROR(ENOMEM);
  380. for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
  381. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  382. uint16_t Cx0, Cy0;
  383. /* Compute coordinates of codeblocks */
  384. /* Compute Cx0*/
  385. Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
  386. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  387. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  388. /* Compute Cy0*/
  389. Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
  390. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  391. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  392. /* Compute Cx1 */
  393. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  394. prec->coord[0][1]);
  395. /* Compute Cy1 */
  396. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  397. prec->coord[1][1]);
  398. /* Update code-blocks coordinates according sub-band position */
  399. if ((bandno + !!reslevelno) & 1) {
  400. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  401. comp->reslevel[reslevelno-1].coord[0][0];
  402. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  403. comp->reslevel[reslevelno-1].coord[0][0];
  404. }
  405. if ((bandno + !!reslevelno) & 2) {
  406. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  407. comp->reslevel[reslevelno-1].coord[1][0];
  408. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  409. comp->reslevel[reslevelno-1].coord[1][0];
  410. }
  411. cblk->zero = 0;
  412. cblk->lblock = 3;
  413. cblk->length = 0;
  414. cblk->lengthinc = 0;
  415. cblk->npasses = 0;
  416. }
  417. }
  418. }
  419. }
  420. return 0;
  421. }
  422. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  423. {
  424. int reslevelno, bandno, cblkno, precno;
  425. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  426. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  427. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  428. Jpeg2000Band *band = rlevel->band + bandno;
  429. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  430. Jpeg2000Prec *prec = band->prec + precno;
  431. tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  432. tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  433. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  434. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  435. cblk->length = 0;
  436. cblk->lblock = 3;
  437. }
  438. }
  439. }
  440. }
  441. }
  442. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  443. {
  444. int reslevelno, bandno, precno;
  445. for (reslevelno = 0;
  446. comp->reslevel && reslevelno < codsty->nreslevels;
  447. reslevelno++) {
  448. Jpeg2000ResLevel *reslevel;
  449. if (!comp->reslevel)
  450. continue;
  451. reslevel = comp->reslevel + reslevelno;
  452. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  453. Jpeg2000Band *band;
  454. if (!reslevel->band)
  455. continue;
  456. band = reslevel->band + bandno;
  457. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  458. if (band->prec) {
  459. Jpeg2000Prec *prec = band->prec + precno;
  460. av_freep(&prec->zerobits);
  461. av_freep(&prec->cblkincl);
  462. av_freep(&prec->cblk);
  463. }
  464. }
  465. av_freep(&band->prec);
  466. }
  467. av_freep(&reslevel->band);
  468. }
  469. ff_dwt_destroy(&comp->dwt);
  470. av_freep(&comp->reslevel);
  471. av_freep(&comp->i_data);
  472. av_freep(&comp->f_data);
  473. }