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.

571 lines
20KB

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