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.

628 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(int w, int h)
  37. {
  38. int64_t res = 0;
  39. while (w > 1 || h > 1) {
  40. res += w * (int64_t)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. static inline float exp2fi(int x) {
  169. /* Normal range */
  170. if (-126 <= x && x <= 128)
  171. return av_int2float(x+127 << 23);
  172. /* Too large */
  173. else if (x > 128)
  174. return INFINITY;
  175. /* Subnormal numbers */
  176. else if (x > -150)
  177. return av_int2float(1 << (x+149));
  178. /* Negligibly small */
  179. else
  180. return 0;
  181. }
  182. static void init_band_stepsize(AVCodecContext *avctx,
  183. Jpeg2000Band *band,
  184. Jpeg2000CodingStyle *codsty,
  185. Jpeg2000QuantStyle *qntsty,
  186. int bandno, int gbandno, int reslevelno,
  187. int cbps)
  188. {
  189. /* TODO: Implementation of quantization step not finished,
  190. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  191. switch (qntsty->quantsty) {
  192. uint8_t gain;
  193. case JPEG2000_QSTY_NONE:
  194. /* TODO: to verify. No quantization in this case */
  195. band->f_stepsize = 1;
  196. break;
  197. case JPEG2000_QSTY_SI:
  198. /*TODO: Compute formula to implement. */
  199. // numbps = cbps +
  200. // lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
  201. // band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
  202. // 2 + numbps - qntsty->expn[gbandno]);
  203. // break;
  204. case JPEG2000_QSTY_SE:
  205. /* Exponent quantization step.
  206. * Formula:
  207. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  208. * R_b = R_I + log2 (gain_b )
  209. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  210. gain = cbps;
  211. band->f_stepsize = exp2fi(gain - qntsty->expn[gbandno]);
  212. band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
  213. break;
  214. default:
  215. band->f_stepsize = 0;
  216. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  217. break;
  218. }
  219. if (codsty->transform != FF_DWT53) {
  220. int lband = 0;
  221. switch (bandno + (reslevelno > 0)) {
  222. case 1:
  223. case 2:
  224. band->f_stepsize *= F_LFTG_X * 2;
  225. lband = 1;
  226. break;
  227. case 3:
  228. band->f_stepsize *= F_LFTG_X * F_LFTG_X * 4;
  229. break;
  230. }
  231. if (codsty->transform == FF_DWT97) {
  232. band->f_stepsize *= pow(F_LFTG_K, 2*(codsty->nreslevels2decode - reslevelno) + lband - 2);
  233. }
  234. }
  235. band->i_stepsize = band->f_stepsize * (1 << 15);
  236. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  237. * If not set output of entropic decoder is not correct. */
  238. if (!av_codec_is_encoder(avctx->codec))
  239. band->f_stepsize *= 0.5;
  240. }
  241. static int init_prec(Jpeg2000Band *band,
  242. Jpeg2000ResLevel *reslevel,
  243. Jpeg2000Component *comp,
  244. int precno, int bandno, int reslevelno,
  245. int log2_band_prec_width,
  246. int log2_band_prec_height)
  247. {
  248. Jpeg2000Prec *prec = band->prec + precno;
  249. int nb_codeblocks, cblkno;
  250. prec->decoded_layers = 0;
  251. /* TODO: Explain formula for JPEG200 DCINEMA. */
  252. /* TODO: Verify with previous count of codeblocks per band */
  253. /* Compute P_x0 */
  254. prec->coord[0][0] = ((band->coord[0][0] >> log2_band_prec_width) + precno % reslevel->num_precincts_x) *
  255. (1 << log2_band_prec_width);
  256. /* Compute P_y0 */
  257. prec->coord[1][0] = ((band->coord[1][0] >> log2_band_prec_height) + precno / reslevel->num_precincts_x) *
  258. (1 << log2_band_prec_height);
  259. /* Compute P_x1 */
  260. prec->coord[0][1] = prec->coord[0][0] +
  261. (1 << log2_band_prec_width);
  262. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  263. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  264. /* Compute P_y1 */
  265. prec->coord[1][1] = prec->coord[1][0] +
  266. (1 << log2_band_prec_height);
  267. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  268. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  269. prec->nb_codeblocks_width =
  270. ff_jpeg2000_ceildivpow2(prec->coord[0][1],
  271. band->log2_cblk_width)
  272. - (prec->coord[0][0] >> band->log2_cblk_width);
  273. prec->nb_codeblocks_height =
  274. ff_jpeg2000_ceildivpow2(prec->coord[1][1],
  275. band->log2_cblk_height)
  276. - (prec->coord[1][0] >> band->log2_cblk_height);
  277. /* Tag trees initialization */
  278. prec->cblkincl =
  279. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  280. prec->nb_codeblocks_height);
  281. if (!prec->cblkincl)
  282. return AVERROR(ENOMEM);
  283. prec->zerobits =
  284. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  285. prec->nb_codeblocks_height);
  286. if (!prec->zerobits)
  287. return AVERROR(ENOMEM);
  288. if (prec->nb_codeblocks_width * (uint64_t)prec->nb_codeblocks_height > INT_MAX) {
  289. prec->cblk = NULL;
  290. return AVERROR(ENOMEM);
  291. }
  292. nb_codeblocks = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
  293. prec->cblk = av_mallocz_array(nb_codeblocks, sizeof(*prec->cblk));
  294. if (!prec->cblk)
  295. return AVERROR(ENOMEM);
  296. for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
  297. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  298. int Cx0, Cy0;
  299. /* Compute coordinates of codeblocks */
  300. /* Compute Cx0*/
  301. Cx0 = ((prec->coord[0][0]) >> band->log2_cblk_width) << band->log2_cblk_width;
  302. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  303. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  304. /* Compute Cy0*/
  305. Cy0 = ((prec->coord[1][0]) >> band->log2_cblk_height) << band->log2_cblk_height;
  306. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  307. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  308. /* Compute Cx1 */
  309. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  310. prec->coord[0][1]);
  311. /* Compute Cy1 */
  312. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  313. prec->coord[1][1]);
  314. /* Update code-blocks coordinates according sub-band position */
  315. if ((bandno + !!reslevelno) & 1) {
  316. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  317. comp->reslevel[reslevelno-1].coord[0][0];
  318. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  319. comp->reslevel[reslevelno-1].coord[0][0];
  320. }
  321. if ((bandno + !!reslevelno) & 2) {
  322. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  323. comp->reslevel[reslevelno-1].coord[1][0];
  324. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  325. comp->reslevel[reslevelno-1].coord[1][0];
  326. }
  327. cblk->zero = 0;
  328. cblk->lblock = 3;
  329. cblk->length = 0;
  330. memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc));
  331. cblk->npasses = 0;
  332. }
  333. return 0;
  334. }
  335. static int init_band(AVCodecContext *avctx,
  336. Jpeg2000ResLevel *reslevel,
  337. Jpeg2000Component *comp,
  338. Jpeg2000CodingStyle *codsty,
  339. Jpeg2000QuantStyle *qntsty,
  340. int bandno, int gbandno, int reslevelno,
  341. int cbps, int dx, int dy)
  342. {
  343. Jpeg2000Band *band = reslevel->band + bandno;
  344. uint8_t log2_band_prec_width, log2_band_prec_height;
  345. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  346. int precno;
  347. int nb_precincts;
  348. int i, j, ret;
  349. init_band_stepsize(avctx, band, codsty, qntsty, bandno, gbandno, reslevelno, cbps);
  350. /* computation of tbx_0, tbx_1, tby_0, tby_1
  351. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  352. * codeblock width and height is computed for
  353. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  354. if (reslevelno == 0) {
  355. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  356. for (i = 0; i < 2; i++)
  357. for (j = 0; j < 2; j++)
  358. band->coord[i][j] =
  359. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
  360. declvl - 1);
  361. log2_band_prec_width = reslevel->log2_prec_width;
  362. log2_band_prec_height = reslevel->log2_prec_height;
  363. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  364. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  365. reslevel->log2_prec_width);
  366. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  367. reslevel->log2_prec_height);
  368. } else {
  369. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  370. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  371. for (i = 0; i < 2; i++)
  372. for (j = 0; j < 2; j++)
  373. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  374. band->coord[i][j] =
  375. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
  376. (((bandno + 1 >> i) & 1LL) << declvl - 1),
  377. declvl);
  378. /* TODO: Manage case of 3 band offsets here or
  379. * in coding/decoding function? */
  380. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  381. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  382. reslevel->log2_prec_width - 1);
  383. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  384. reslevel->log2_prec_height - 1);
  385. log2_band_prec_width = reslevel->log2_prec_width - 1;
  386. log2_band_prec_height = reslevel->log2_prec_height - 1;
  387. }
  388. if (reslevel->num_precincts_x * (uint64_t)reslevel->num_precincts_y > INT_MAX) {
  389. band->prec = NULL;
  390. return AVERROR(ENOMEM);
  391. }
  392. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  393. band->prec = av_mallocz_array(nb_precincts, sizeof(*band->prec));
  394. if (!band->prec)
  395. return AVERROR(ENOMEM);
  396. for (precno = 0; precno < nb_precincts; precno++) {
  397. ret = init_prec(band, reslevel, comp,
  398. precno, bandno, reslevelno,
  399. log2_band_prec_width, log2_band_prec_height);
  400. if (ret < 0)
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  406. Jpeg2000CodingStyle *codsty,
  407. Jpeg2000QuantStyle *qntsty,
  408. int cbps, int dx, int dy,
  409. AVCodecContext *avctx)
  410. {
  411. int reslevelno, bandno, gbandno = 0, ret, i, j;
  412. uint32_t csize;
  413. if (codsty->nreslevels2decode <= 0) {
  414. av_log(avctx, AV_LOG_ERROR, "nreslevels2decode %d invalid or uninitialized\n", codsty->nreslevels2decode);
  415. return AVERROR_INVALIDDATA;
  416. }
  417. if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
  418. codsty->nreslevels2decode - 1,
  419. codsty->transform))
  420. return ret;
  421. if (av_image_check_size(comp->coord[0][1] - comp->coord[0][0],
  422. comp->coord[1][1] - comp->coord[1][0], 0, avctx))
  423. return AVERROR_INVALIDDATA;
  424. csize = (comp->coord[0][1] - comp->coord[0][0]) *
  425. (comp->coord[1][1] - comp->coord[1][0]);
  426. if (comp->coord[0][1] - comp->coord[0][0] > 32768 ||
  427. comp->coord[1][1] - comp->coord[1][0] > 32768) {
  428. av_log(avctx, AV_LOG_ERROR, "component size too large\n");
  429. return AVERROR_PATCHWELCOME;
  430. }
  431. if (codsty->transform == FF_DWT97) {
  432. csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->f_data);
  433. comp->i_data = NULL;
  434. comp->f_data = av_mallocz_array(csize, sizeof(*comp->f_data));
  435. if (!comp->f_data)
  436. return AVERROR(ENOMEM);
  437. } else {
  438. csize += AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*comp->i_data);
  439. comp->f_data = NULL;
  440. comp->i_data = av_mallocz_array(csize, sizeof(*comp->i_data));
  441. if (!comp->i_data)
  442. return AVERROR(ENOMEM);
  443. }
  444. comp->reslevel = av_mallocz_array(codsty->nreslevels, sizeof(*comp->reslevel));
  445. if (!comp->reslevel)
  446. return AVERROR(ENOMEM);
  447. /* LOOP on resolution levels */
  448. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  449. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  450. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  451. /* Compute borders for each resolution level.
  452. * Computation of trx_0, trx_1, try_0 and try_1.
  453. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  454. for (i = 0; i < 2; i++)
  455. for (j = 0; j < 2; j++)
  456. reslevel->coord[i][j] =
  457. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  458. // update precincts size: 2^n value
  459. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  460. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  461. /* Number of bands for each resolution level */
  462. if (reslevelno == 0)
  463. reslevel->nbands = 1;
  464. else
  465. reslevel->nbands = 3;
  466. /* Number of precincts which span the tile for resolution level reslevelno
  467. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  468. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  469. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  470. * for Dcinema profiles in JPEG 2000
  471. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  472. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  473. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  474. reslevel->num_precincts_x = 0;
  475. else
  476. reslevel->num_precincts_x =
  477. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  478. reslevel->log2_prec_width) -
  479. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  480. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  481. reslevel->num_precincts_y = 0;
  482. else
  483. reslevel->num_precincts_y =
  484. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  485. reslevel->log2_prec_height) -
  486. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  487. reslevel->band = av_mallocz_array(reslevel->nbands, sizeof(*reslevel->band));
  488. if (!reslevel->band)
  489. return AVERROR(ENOMEM);
  490. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  491. ret = init_band(avctx, reslevel,
  492. comp, codsty, qntsty,
  493. bandno, gbandno, reslevelno,
  494. cbps, dx, dy);
  495. if (ret < 0)
  496. return ret;
  497. }
  498. }
  499. return 0;
  500. }
  501. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  502. {
  503. int reslevelno, bandno, cblkno, precno;
  504. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  505. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  506. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  507. Jpeg2000Band *band = rlevel->band + bandno;
  508. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  509. Jpeg2000Prec *prec = band->prec + precno;
  510. tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  511. tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height);
  512. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  513. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  514. cblk->length = 0;
  515. cblk->lblock = 3;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  522. {
  523. int reslevelno, bandno, precno;
  524. for (reslevelno = 0;
  525. comp->reslevel && reslevelno < codsty->nreslevels;
  526. reslevelno++) {
  527. Jpeg2000ResLevel *reslevel;
  528. if (!comp->reslevel)
  529. continue;
  530. reslevel = comp->reslevel + reslevelno;
  531. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  532. Jpeg2000Band *band;
  533. if (!reslevel->band)
  534. continue;
  535. band = reslevel->band + bandno;
  536. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  537. if (band->prec) {
  538. Jpeg2000Prec *prec = band->prec + precno;
  539. av_freep(&prec->zerobits);
  540. av_freep(&prec->cblkincl);
  541. av_freep(&prec->cblk);
  542. }
  543. }
  544. av_freep(&band->prec);
  545. }
  546. av_freep(&reslevel->band);
  547. }
  548. ff_dwt_destroy(&comp->dwt);
  549. av_freep(&comp->reslevel);
  550. av_freep(&comp->i_data);
  551. av_freep(&comp->f_data);
  552. }