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.

558 lines
23KB

  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) * t1->stride + x] |= JPEG2000_T1_SIG;
  150. if (negative) {
  151. t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  152. t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  153. t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  154. t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  155. } else {
  156. t1->flags[(y) * t1->stride + x + 1] |= JPEG2000_T1_SIG_W;
  157. t1->flags[(y) * t1->stride + x - 1] |= JPEG2000_T1_SIG_E;
  158. t1->flags[(y + 1) * t1->stride + x] |= JPEG2000_T1_SIG_N;
  159. t1->flags[(y - 1) * t1->stride + x] |= JPEG2000_T1_SIG_S;
  160. }
  161. t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_NW;
  162. t1->flags[(y + 1) * t1->stride + x - 1] |= JPEG2000_T1_SIG_NE;
  163. t1->flags[(y - 1) * t1->stride + x + 1] |= JPEG2000_T1_SIG_SW;
  164. t1->flags[(y - 1) * t1->stride + 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. csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
  189. comp->i_data = NULL;
  190. comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
  191. if (!comp->f_data)
  192. return AVERROR(ENOMEM);
  193. } else {
  194. csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
  195. comp->f_data = NULL;
  196. comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
  197. if (!comp->i_data)
  198. return AVERROR(ENOMEM);
  199. }
  200. comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
  201. if (!comp->reslevel)
  202. return AVERROR(ENOMEM);
  203. /* LOOP on resolution levels */
  204. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  205. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  206. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  207. /* Compute borders for each resolution level.
  208. * Computation of trx_0, trx_1, try_0 and try_1.
  209. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  210. for (i = 0; i < 2; i++)
  211. for (j = 0; j < 2; j++)
  212. reslevel->coord[i][j] =
  213. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  214. // update precincts size: 2^n value
  215. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  216. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  217. /* Number of bands for each resolution level */
  218. if (reslevelno == 0)
  219. reslevel->nbands = 1;
  220. else
  221. reslevel->nbands = 3;
  222. /* Number of precincts which span the tile for resolution level reslevelno
  223. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  224. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  225. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  226. * for Dcinema profiles in JPEG 2000
  227. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  228. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  229. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  230. reslevel->num_precincts_x = 0;
  231. else
  232. reslevel->num_precincts_x =
  233. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  234. reslevel->log2_prec_width) -
  235. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  236. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  237. reslevel->num_precincts_y = 0;
  238. else
  239. reslevel->num_precincts_y =
  240. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  241. reslevel->log2_prec_height) -
  242. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  243. reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
  244. if (!reslevel->band)
  245. return AVERROR(ENOMEM);
  246. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  247. Jpeg2000Band *band = reslevel->band + bandno;
  248. int cblkno, precno;
  249. int nb_precincts;
  250. /* TODO: Implementation of quantization step not finished,
  251. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  252. switch (qntsty->quantsty) {
  253. uint8_t gain;
  254. case JPEG2000_QSTY_NONE:
  255. /* TODO: to verify. No quantization in this case */
  256. band->f_stepsize = 1;
  257. break;
  258. case JPEG2000_QSTY_SI:
  259. /*TODO: Compute formula to implement. */
  260. // numbps = cbps +
  261. // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
  262. // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
  263. // 2 + numbps - qntsty->expn[gbandno]);
  264. // break;
  265. case JPEG2000_QSTY_SE:
  266. /* Exponent quantization step.
  267. * Formula:
  268. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  269. * R_b = R_I + log2 (gain_b )
  270. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  271. gain = cbps;
  272. band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  273. band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
  274. break;
  275. default:
  276. band->f_stepsize = 0;
  277. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  278. break;
  279. }
  280. if (codsty->transform != FF_DWT53) {
  281. int lband = 0;
  282. switch (bandno + (reslevelno > 0)) {
  283. case 1:
  284. case 2:
  285. band->f_stepsize *= F_LFTG_X * 2;
  286. lband = 1;
  287. break;
  288. case 3:
  289. band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
  290. break;
  291. }
  292. if (codsty->transform == FF_DWT97) {
  293. band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
  294. }
  295. }
  296. band->i_stepsize = band->f_stepsize * (1 << 15);
  297. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  298. * If not set output of entropic decoder is not correct. */
  299. if (!av_codec_is_encoder(avctx->codec))
  300. band->f_stepsize *= 0.5;
  301. /* computation of tbx_0, tbx_1, tby_0, tby_1
  302. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  303. * codeblock width and height is computed for
  304. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  305. if (reslevelno == 0) {
  306. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  307. for (i = 0; i < 2; i++)
  308. for (j = 0; j < 2; j++)
  309. band->coord[i][j] =
  310. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
  311. declvl - 1);
  312. log2_band_prec_width = reslevel->log2_prec_width;
  313. log2_band_prec_height = reslevel->log2_prec_height;
  314. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  315. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  316. reslevel->log2_prec_width);
  317. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  318. reslevel->log2_prec_height);
  319. } else {
  320. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  321. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  322. for (i = 0; i < 2; i++)
  323. for (j = 0; j < 2; j++)
  324. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  325. band->coord[i][j] =
  326. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
  327. (((bandno + 1 >> i) & 1LL) << declvl - 1),
  328. declvl);
  329. /* TODO: Manage case of 3 band offsets here or
  330. * in coding/decoding function? */
  331. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  332. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  333. reslevel->log2_prec_width - 1);
  334. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  335. reslevel->log2_prec_height - 1);
  336. log2_band_prec_width = reslevel->log2_prec_width - 1;
  337. log2_band_prec_height = reslevel->log2_prec_height - 1;
  338. }
  339. if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
  340. band->prec = NULL;
  341. return AVERROR(ENOMEM);
  342. }
  343. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  344. band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
  345. if (!band->prec)
  346. return AVERROR(ENOMEM);
  347. for (precno = 0; precno < nb_precincts; precno++) {
  348. Jpeg2000Prec *prec = band->prec + precno;
  349. int nb_codeblocks;
  350. prec->decoded_layers = 0;
  351. /* TODO: Explain formula for JPEG200 DCINEMA. */
  352. /* TODO: Verify with previous count of codeblocks per band */
  353. /* Compute P_x0 */
  354. prec->coord[0][0] = ((band->coord[0][0] >> log2_band_prec_width) + precno % reslevel->num_precincts_x) *
  355. (1 << log2_band_prec_width);
  356. /* Compute P_y0 */
  357. prec->coord[1][0] = ((band->coord[1][0] >> log2_band_prec_height) + precno / reslevel->num_precincts_x) *
  358. (1 << log2_band_prec_height);
  359. /* Compute P_x1 */
  360. prec->coord[0][1] = prec->coord[0][0] +
  361. (1 << log2_band_prec_width);
  362. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  363. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  364. /* Compute P_y1 */
  365. prec->coord[1][1] = prec->coord[1][0] +
  366. (1 << log2_band_prec_height);
  367. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  368. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  369. prec->nb_codeblocks_width =
  370. ff_jpeg2000_ceildivpow2(prec->coord[0][1],
  371. band->log2_cblk_width)
  372. - (prec->coord[0][0] >> band->log2_cblk_width);
  373. prec->nb_codeblocks_height =
  374. ff_jpeg2000_ceildivpow2(prec->coord[1][1],
  375. band->log2_cblk_height)
  376. - (prec->coord[1][0] >> band->log2_cblk_height);
  377. /* Tag trees initialization */
  378. prec->cblkincl =
  379. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  380. prec->nb_codeblocks_height);
  381. if (!prec->cblkincl)
  382. return AVERROR(ENOMEM);
  383. prec->zerobits =
  384. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  385. prec->nb_codeblocks_height);
  386. if (!prec->zerobits)
  387. return AVERROR(ENOMEM);
  388. if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
  389. prec->cblk = NULL;
  390. return AVERROR(ENOMEM);
  391. }
  392. nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
  393. prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
  394. if (!prec->cblk)
  395. return AVERROR(ENOMEM);
  396. for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
  397. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  398. uint16_t Cx0, Cy0;
  399. /* Compute coordinates of codeblocks */
  400. /* Compute Cx0*/
  401. Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
  402. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  403. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  404. /* Compute Cy0*/
  405. Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
  406. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  407. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  408. /* Compute Cx1 */
  409. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  410. prec->coord[0][1]);
  411. /* Compute Cy1 */
  412. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  413. prec->coord[1][1]);
  414. /* Update code-blocks coordinates according sub-band position */
  415. if ((bandno + !!reslevelno) & 1) {
  416. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  417. comp->reslevel[reslevelno-1].coord[0][0];
  418. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  419. comp->reslevel[reslevelno-1].coord[0][0];
  420. }
  421. if ((bandno + !!reslevelno) & 2) {
  422. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  423. comp->reslevel[reslevelno-1].coord[1][0];
  424. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  425. comp->reslevel[reslevelno-1].coord[1][0];
  426. }
  427. cblk->zero = 0;
  428. cblk->lblock = 3;
  429. cblk->length = 0;
  430. memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
  431. cblk->npasses = 0;
  432. }
  433. }
  434. }
  435. }
  436. return 0;
  437. }
  438. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  439. {
  440. int reslevelno, bandno, cblkno, precno;
  441. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  442. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  443. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  444. Jpeg2000Band *band = rlevel->band + bandno;
  445. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  446. Jpeg2000Prec *prec = band->prec + precno;
  447. tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  448. tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  449. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  450. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  451. cblk->length = 0;
  452. cblk->lblock = 3;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  459. {
  460. int reslevelno, bandno, precno;
  461. for (reslevelno = 0;
  462. comp->reslevel && reslevelno < codsty->nreslevels;
  463. reslevelno++) {
  464. Jpeg2000ResLevel *reslevel;
  465. if (!comp->reslevel)
  466. continue;
  467. reslevel = comp->reslevel + reslevelno;
  468. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  469. Jpeg2000Band *band;
  470. if (!reslevel->band)
  471. continue;
  472. band = reslevel->band + bandno;
  473. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  474. if (band->prec) {
  475. Jpeg2000Prec *prec = band->prec + precno;
  476. av_freep(&prec->zerobits);
  477. av_freep(&prec->cblkincl);
  478. av_freep(&prec->cblk);
  479. }
  480. }
  481. av_freep(&band->prec);
  482. }
  483. av_freep(&reslevel->band);
  484. }
  485. ff_dwt_destroy(&comp->dwt);
  486. av_freep(&comp->reslevel);
  487. av_freep(&comp->i_data);
  488. av_freep(&comp->f_data);
  489. }