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.

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