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.

533 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_calloc(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_calloc(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. int numbps;
  253. case JPEG2000_QSTY_NONE:
  254. /* TODO: to verify. No quantization in this case */
  255. band->f_stepsize = 1;
  256. break;
  257. case JPEG2000_QSTY_SI:
  258. /*TODO: Compute formula to implement. */
  259. numbps = cbps +
  260. lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
  261. band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
  262. 2 + numbps - qntsty->expn[gbandno]);
  263. break;
  264. case JPEG2000_QSTY_SE:
  265. /* Exponent quantization step.
  266. * Formula:
  267. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  268. * R_b = R_I + log2 (gain_b )
  269. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  270. /* TODO/WARN: value of log2 (gain_b ) not taken into account
  271. * but it works (compared to OpenJPEG). Why?
  272. * Further investigation needed. */
  273. gain = cbps;
  274. band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  275. band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
  276. break;
  277. default:
  278. band->f_stepsize = 0;
  279. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  280. break;
  281. }
  282. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  283. * If not set output of entropic decoder is not correct. */
  284. if (!av_codec_is_encoder(avctx->codec))
  285. band->f_stepsize *= 0.5;
  286. band->i_stepsize = band->f_stepsize * (1 << 15);
  287. /* computation of tbx_0, tbx_1, tby_0, tby_1
  288. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  289. * codeblock width and height is computed for
  290. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  291. if (reslevelno == 0) {
  292. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  293. for (i = 0; i < 2; i++)
  294. for (j = 0; j < 2; j++)
  295. band->coord[i][j] =
  296. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0],
  297. declvl - 1);
  298. log2_band_prec_width = reslevel->log2_prec_width;
  299. log2_band_prec_height = reslevel->log2_prec_height;
  300. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  301. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  302. reslevel->log2_prec_width);
  303. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  304. reslevel->log2_prec_height);
  305. } else {
  306. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  307. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  308. for (i = 0; i < 2; i++)
  309. for (j = 0; j < 2; j++)
  310. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  311. band->coord[i][j] =
  312. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] -
  313. (((bandno + 1 >> i) & 1) << declvl - 1),
  314. declvl);
  315. /* TODO: Manage case of 3 band offsets here or
  316. * in coding/decoding function? */
  317. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  318. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  319. reslevel->log2_prec_width - 1);
  320. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  321. reslevel->log2_prec_height - 1);
  322. log2_band_prec_width = reslevel->log2_prec_width - 1;
  323. log2_band_prec_height = reslevel->log2_prec_height - 1;
  324. }
  325. for (j = 0; j < 2; j++)
  326. band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
  327. for (j = 0; j < 2; j++)
  328. band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
  329. band->prec = av_calloc(reslevel->num_precincts_x *
  330. (uint64_t)reslevel->num_precincts_y,
  331. sizeof(*band->prec));
  332. if (!band->prec)
  333. return AVERROR(ENOMEM);
  334. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  335. for (precno = 0; precno < nb_precincts; precno++) {
  336. Jpeg2000Prec *prec = band->prec + precno;
  337. /* TODO: Explain formula for JPEG200 DCINEMA. */
  338. /* TODO: Verify with previous count of codeblocks per band */
  339. /* Compute P_x0 */
  340. prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
  341. (1 << log2_band_prec_width);
  342. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  343. /* Compute P_y0 */
  344. prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
  345. (1 << log2_band_prec_height);
  346. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  347. /* Compute P_x1 */
  348. prec->coord[0][1] = prec->coord[0][0] +
  349. (1 << log2_band_prec_width);
  350. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  351. /* Compute P_y1 */
  352. prec->coord[1][1] = prec->coord[1][0] +
  353. (1 << log2_band_prec_height);
  354. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  355. prec->nb_codeblocks_width =
  356. ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
  357. prec->coord[0][0],
  358. band->log2_cblk_width);
  359. prec->nb_codeblocks_height =
  360. ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
  361. prec->coord[1][0],
  362. band->log2_cblk_height);
  363. /* Tag trees initialization */
  364. prec->cblkincl =
  365. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  366. prec->nb_codeblocks_height);
  367. if (!prec->cblkincl)
  368. return AVERROR(ENOMEM);
  369. prec->zerobits =
  370. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  371. prec->nb_codeblocks_height);
  372. if (!prec->zerobits)
  373. return AVERROR(ENOMEM);
  374. prec->cblk = av_mallocz_array(prec->nb_codeblocks_width *
  375. (uint64_t)prec->nb_codeblocks_height,
  376. sizeof(*prec->cblk));
  377. if (!prec->cblk)
  378. return AVERROR(ENOMEM);
  379. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  380. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  381. uint16_t Cx0, Cy0;
  382. /* Compute coordinates of codeblocks */
  383. /* Compute Cx0*/
  384. Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
  385. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  386. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  387. /* Compute Cy0*/
  388. Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
  389. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  390. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  391. /* Compute Cx1 */
  392. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  393. prec->coord[0][1]);
  394. /* Compute Cy1 */
  395. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  396. prec->coord[1][1]);
  397. /* Update code-blocks coordinates according sub-band position */
  398. if ((bandno + !!reslevelno) & 1) {
  399. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  400. comp->reslevel[reslevelno-1].coord[0][0];
  401. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  402. comp->reslevel[reslevelno-1].coord[0][0];
  403. }
  404. if ((bandno + !!reslevelno) & 2) {
  405. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  406. comp->reslevel[reslevelno-1].coord[1][0];
  407. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  408. comp->reslevel[reslevelno-1].coord[1][0];
  409. }
  410. cblk->zero = 0;
  411. cblk->lblock = 3;
  412. cblk->length = 0;
  413. cblk->lengthinc = 0;
  414. cblk->npasses = 0;
  415. }
  416. }
  417. }
  418. }
  419. return 0;
  420. }
  421. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  422. {
  423. int reslevelno, bandno, cblkno, precno;
  424. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  425. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  426. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  427. Jpeg2000Band *band = rlevel->band + bandno;
  428. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  429. Jpeg2000Prec *prec = band->prec + precno;
  430. tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  431. tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  432. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  433. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  434. cblk->length = 0;
  435. cblk->lblock = 3;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  442. {
  443. int reslevelno, bandno, precno;
  444. for (reslevelno = 0;
  445. comp->reslevel && reslevelno < codsty->nreslevels;
  446. reslevelno++) {
  447. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  448. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  449. if (reslevel->band) {
  450. Jpeg2000Band *band = reslevel->band + bandno;
  451. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  452. if (band->prec) {
  453. Jpeg2000Prec *prec = band->prec + precno;
  454. av_freep(&prec->zerobits);
  455. av_freep(&prec->cblkincl);
  456. av_freep(&prec->cblk);
  457. }
  458. }
  459. av_freep(&band->prec);
  460. }
  461. }
  462. av_freep(&reslevel->band);
  463. }
  464. ff_dwt_destroy(&comp->dwt);
  465. av_freep(&comp->reslevel);
  466. av_freep(&comp->i_data);
  467. av_freep(&comp->f_data);
  468. }