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.

632 lines
24KB

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