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.

542 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. csize += FF_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 += FF_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. /* TODO/WARN: value of log2 (gain_b ) not taken into account
  272. * but it works (compared to OpenJPEG). Why?
  273. * Further investigation needed. */
  274. gain = cbps;
  275. band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  276. band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
  277. break;
  278. default:
  279. band->f_stepsize = 0;
  280. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  281. break;
  282. }
  283. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  284. * If not set output of entropic decoder is not correct. */
  285. if (!av_codec_is_encoder(avctx->codec))
  286. band->f_stepsize *= 0.5;
  287. band->i_stepsize = band->f_stepsize * (1 << 15);
  288. /* computation of tbx_0, tbx_1, tby_0, tby_1
  289. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  290. * codeblock width and height is computed for
  291. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  292. if (reslevelno == 0) {
  293. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  294. for (i = 0; i < 2; i++)
  295. for (j = 0; j < 2; j++)
  296. band->coord[i][j] =
  297. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
  298. declvl - 1);
  299. log2_band_prec_width = reslevel->log2_prec_width;
  300. log2_band_prec_height = reslevel->log2_prec_height;
  301. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  302. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  303. reslevel->log2_prec_width);
  304. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  305. reslevel->log2_prec_height);
  306. } else {
  307. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  308. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  309. for (i = 0; i < 2; i++)
  310. for (j = 0; j < 2; j++)
  311. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  312. band->coord[i][j] =
  313. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
  314. (((bandno + 1 >> i) & 1) << declvl - 1),
  315. declvl);
  316. /* TODO: Manage case of 3 band offsets here or
  317. * in coding/decoding function? */
  318. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  319. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  320. reslevel->log2_prec_width - 1);
  321. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  322. reslevel->log2_prec_height - 1);
  323. log2_band_prec_width = reslevel->log2_prec_width - 1;
  324. log2_band_prec_height = reslevel->log2_prec_height - 1;
  325. }
  326. if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
  327. band->prec = NULL;
  328. return AVERROR(ENOMEM);
  329. }
  330. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  331. band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
  332. if (!band->prec)
  333. return AVERROR(ENOMEM);
  334. for (precno = 0; precno < nb_precincts; precno++) {
  335. Jpeg2000Prec *prec = band->prec + precno;
  336. int nb_codeblocks;
  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] = ((band->coord[0][0] >> reslevel->log2_prec_width) + 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] = ((band->coord[1][0] >> reslevel->log2_prec_height) + 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. band->log2_cblk_width)
  358. - (prec->coord[0][0] >> band->log2_cblk_width);
  359. prec->nb_codeblocks_height =
  360. ff_jpeg2000_ceildivpow2(prec->coord[1][1],
  361. band->log2_cblk_height)
  362. - (prec->coord[1][0] >> 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. if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
  375. prec->cblk = NULL;
  376. return AVERROR(ENOMEM);
  377. }
  378. nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
  379. prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
  380. if (!prec->cblk)
  381. return AVERROR(ENOMEM);
  382. for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
  383. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  384. uint16_t Cx0, Cy0;
  385. /* Compute coordinates of codeblocks */
  386. /* Compute Cx0*/
  387. Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
  388. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  389. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  390. /* Compute Cy0*/
  391. Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
  392. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  393. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  394. /* Compute Cx1 */
  395. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  396. prec->coord[0][1]);
  397. /* Compute Cy1 */
  398. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  399. prec->coord[1][1]);
  400. /* Update code-blocks coordinates according sub-band position */
  401. if ((bandno + !!reslevelno) & 1) {
  402. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  403. comp->reslevel[reslevelno-1].coord[0][0];
  404. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  405. comp->reslevel[reslevelno-1].coord[0][0];
  406. }
  407. if ((bandno + !!reslevelno) & 2) {
  408. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  409. comp->reslevel[reslevelno-1].coord[1][0];
  410. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  411. comp->reslevel[reslevelno-1].coord[1][0];
  412. }
  413. cblk->zero = 0;
  414. cblk->lblock = 3;
  415. cblk->length = 0;
  416. memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
  417. cblk->npasses = 0;
  418. }
  419. }
  420. }
  421. }
  422. return 0;
  423. }
  424. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  425. {
  426. int reslevelno, bandno, cblkno, precno;
  427. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  428. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  429. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  430. Jpeg2000Band *band = rlevel->band + bandno;
  431. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  432. Jpeg2000Prec *prec = band->prec + precno;
  433. tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  434. tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  435. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  436. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  437. cblk->length = 0;
  438. cblk->lblock = 3;
  439. }
  440. }
  441. }
  442. }
  443. }
  444. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  445. {
  446. int reslevelno, bandno, precno;
  447. for (reslevelno = 0;
  448. comp->reslevel && reslevelno < codsty->nreslevels;
  449. reslevelno++) {
  450. Jpeg2000ResLevel *reslevel;
  451. if (!comp->reslevel)
  452. continue;
  453. reslevel = comp->reslevel + reslevelno;
  454. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  455. Jpeg2000Band *band;
  456. if (!reslevel->band)
  457. continue;
  458. band = reslevel->band + bandno;
  459. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  460. if (band->prec) {
  461. Jpeg2000Prec *prec = band->prec + precno;
  462. av_freep(&prec->zerobits);
  463. av_freep(&prec->cblkincl);
  464. av_freep(&prec->cblk);
  465. }
  466. }
  467. av_freep(&band->prec);
  468. }
  469. av_freep(&reslevel->band);
  470. }
  471. ff_dwt_destroy(&comp->dwt);
  472. av_freep(&comp->reslevel);
  473. av_freep(&comp->i_data);
  474. av_freep(&comp->f_data);
  475. }