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.

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