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.

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