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.

484 lines
19KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/common.h"
  27. #include "libavutil/mem.h"
  28. #include "avcodec.h"
  29. #include "jpeg2000.h"
  30. #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
  31. /* tag tree routines */
  32. /* allocate the memory for tag tree */
  33. static int32_t tag_tree_size(uint16_t w, uint16_t h)
  34. {
  35. uint32_t res = 0;
  36. while (w > 1 || h > 1) {
  37. res += w * h;
  38. if (res + 1 >= INT32_MAX)
  39. return -1;
  40. w = (w + 1) >> 1;
  41. h = (h + 1) >> 1;
  42. }
  43. return (int32_t)(res + 1);
  44. }
  45. static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
  46. {
  47. int pw = w, ph = h;
  48. Jpeg2000TgtNode *res, *t, *t2;
  49. int32_t tt_size;
  50. tt_size = tag_tree_size(w, h);
  51. if (tt_size == -1)
  52. return NULL;
  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. uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  72. static int getsigctxno(int flag, int bandno)
  73. {
  74. int h, v, d;
  75. h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
  76. ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
  77. v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
  78. ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
  79. d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
  80. ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
  81. ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
  82. ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
  83. if (bandno < 3) {
  84. if (bandno == 1)
  85. FFSWAP(int, h, v);
  86. if (h == 2)
  87. return 8;
  88. if (h == 1) {
  89. if (v >= 1)
  90. return 7;
  91. if (d >= 1)
  92. return 6;
  93. return 5;
  94. }
  95. if (v == 2)
  96. return 4;
  97. if (v == 1)
  98. return 3;
  99. if (d >= 2)
  100. return 2;
  101. if (d == 1)
  102. return 1;
  103. return 0;
  104. } else {
  105. if (d >= 3)
  106. return 8;
  107. if (d == 2) {
  108. if (h + v >= 1)
  109. return 7;
  110. return 6;
  111. }
  112. if (d == 1) {
  113. if (h + v >= 2)
  114. return 5;
  115. if (h + v == 1)
  116. return 4;
  117. return 3;
  118. }
  119. if (h + v >= 2)
  120. return 2;
  121. if (h + v == 1)
  122. return 1;
  123. return 0;
  124. }
  125. return 0;
  126. }
  127. uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
  128. static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
  129. static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
  130. static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
  131. static int getsgnctxno(int flag, uint8_t *xorbit)
  132. {
  133. int vcontrib, hcontrib;
  134. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
  135. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
  136. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
  137. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
  138. *xorbit = xorbittab[hcontrib][vcontrib];
  139. return ctxlbltab[hcontrib][vcontrib];
  140. }
  141. void ff_jpeg2000_init_tier1_luts(void)
  142. {
  143. int i, j;
  144. for (i = 0; i < 256; i++)
  145. for (j = 0; j < 4; j++)
  146. ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
  147. for (i = 0; i < 16; i++)
  148. for (j = 0; j < 16; j++)
  149. ff_jpeg2000_sgnctxno_lut[i][j] =
  150. getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
  151. }
  152. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
  153. int negative)
  154. {
  155. x++;
  156. y++;
  157. t1->flags[y][x] |= JPEG2000_T1_SIG;
  158. if (negative) {
  159. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  160. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  161. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  162. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  163. } else {
  164. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
  165. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
  166. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
  167. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
  168. }
  169. t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
  170. t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
  171. t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
  172. t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
  173. }
  174. static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
  175. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  176. Jpeg2000CodingStyle *codsty,
  177. Jpeg2000QuantStyle *qntsty,
  178. int cbps, int dx, int dy,
  179. AVCodecContext *avctx)
  180. {
  181. uint8_t log2_band_prec_width, log2_band_prec_height;
  182. int reslevelno, bandno, gbandno = 0, ret, i, j;
  183. uint32_t csize = 1;
  184. if (!codsty->nreslevels2decode) {
  185. av_log(avctx, AV_LOG_ERROR, "nreslevels2decode uninitialized\n");
  186. return AVERROR_INVALIDDATA;
  187. }
  188. if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
  189. codsty->nreslevels2decode - 1,
  190. codsty->transform))
  191. return ret;
  192. // component size comp->coord is uint16_t so ir cannot overflow
  193. csize = (comp->coord[0][1] - comp->coord[0][0]) *
  194. (comp->coord[1][1] - comp->coord[1][0]);
  195. comp->data = av_malloc_array(csize, sizeof(*comp->data));
  196. if (!comp->data)
  197. return AVERROR(ENOMEM);
  198. comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
  199. if (!comp->reslevel)
  200. return AVERROR(ENOMEM);
  201. /* LOOP on resolution levels */
  202. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  203. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  204. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  205. /* Compute borders for each resolution level.
  206. * Computation of trx_0, trx_1, try_0 and try_1.
  207. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  208. for (i = 0; i < 2; i++)
  209. for (j = 0; j < 2; j++)
  210. reslevel->coord[i][j] =
  211. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  212. // update precincts size: 2^n value
  213. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  214. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  215. /* Number of bands for each resolution level */
  216. if (reslevelno == 0)
  217. reslevel->nbands = 1;
  218. else
  219. reslevel->nbands = 3;
  220. /* Number of precincts wich span the tile for resolution level reslevelno
  221. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  222. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  223. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  224. * for Dcinema profiles in JPEG 2000
  225. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  226. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  227. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  228. reslevel->num_precincts_x = 0;
  229. else
  230. reslevel->num_precincts_x =
  231. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  232. reslevel->log2_prec_width) -
  233. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  234. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  235. reslevel->num_precincts_y = 0;
  236. else
  237. reslevel->num_precincts_y =
  238. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  239. reslevel->log2_prec_height) -
  240. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  241. reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
  242. if (!reslevel->band)
  243. return AVERROR(ENOMEM);
  244. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  245. Jpeg2000Band *band = reslevel->band + bandno;
  246. int cblkno, precno;
  247. int nb_precincts;
  248. /* TODO: Implementation of quantization step not finished,
  249. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  250. switch (qntsty->quantsty) {
  251. uint8_t gain;
  252. int numbps;
  253. case JPEG2000_QSTY_NONE:
  254. /* TODO: to verify. No quantization in this case */
  255. numbps = cbps +
  256. lut_gain[codsty->transform][bandno + reslevelno > 0];
  257. band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno],
  258. 2 + numbps - qntsty->expn[gbandno]);
  259. break;
  260. case JPEG2000_QSTY_SI:
  261. /*TODO: Compute formula to implement. */
  262. band->stepsize = (float) (1 << 13);
  263. break;
  264. case JPEG2000_QSTY_SE:
  265. /* Exponent quantization step.
  266. * Formula:
  267. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  268. * R_b = R_I + log2 (gain_b )
  269. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  270. /* TODO/WARN: value of log2 (gain_b ) not taken into account
  271. * but it works (compared to OpenJPEG). Why?
  272. * Further investigation needed. */
  273. gain = cbps;
  274. band->stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  275. band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0;
  276. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  277. * If not set output of entropic decoder is not correct. */
  278. band->stepsize *= 0.5;
  279. break;
  280. default:
  281. band->stepsize = 0;
  282. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  283. break;
  284. }
  285. /* BITEXACT computing case --> convert to int */
  286. if (avctx->flags & CODEC_FLAG_BITEXACT)
  287. band->stepsize = (int32_t)(band->stepsize * (1 << 16));
  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. band->prec = av_malloc_array(reslevel->num_precincts_x *
  327. reslevel->num_precincts_y,
  328. sizeof(*band->prec));
  329. if (!band->prec)
  330. return AVERROR(ENOMEM);
  331. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  332. for (precno = 0; precno < nb_precincts; precno++) {
  333. Jpeg2000Prec *prec = band->prec + precno;
  334. /* TODO: Explain formula for JPEG200 DCINEMA. */
  335. /* TODO: Verify with previous count of codeblocks per band */
  336. /* Compute P_x0 */
  337. prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
  338. (1 << log2_band_prec_width);
  339. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  340. /* Compute P_y0 */
  341. prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
  342. (1 << log2_band_prec_height);
  343. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  344. /* Compute P_x1 */
  345. prec->coord[0][1] = prec->coord[0][0] +
  346. (1 << log2_band_prec_width);
  347. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  348. /* Compute P_y1 */
  349. prec->coord[1][1] = prec->coord[1][0] +
  350. (1 << log2_band_prec_height);
  351. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  352. prec->nb_codeblocks_width =
  353. ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
  354. prec->coord[0][0],
  355. band->log2_cblk_width);
  356. prec->nb_codeblocks_height =
  357. ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
  358. prec->coord[1][0],
  359. band->log2_cblk_height);
  360. /* Tag trees initialization */
  361. prec->cblkincl =
  362. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  363. prec->nb_codeblocks_height);
  364. if (!prec->cblkincl)
  365. return AVERROR(ENOMEM);
  366. prec->zerobits =
  367. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  368. prec->nb_codeblocks_height);
  369. if (!prec->zerobits)
  370. return AVERROR(ENOMEM);
  371. prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
  372. prec->nb_codeblocks_height,
  373. sizeof(*prec->cblk));
  374. if (!prec->cblk)
  375. return AVERROR(ENOMEM);
  376. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  377. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  378. uint16_t Cx0, Cy0;
  379. /* Compute coordinates of codeblocks */
  380. /* Compute Cx0*/
  381. Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
  382. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  383. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  384. /* Compute Cy0*/
  385. Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
  386. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  387. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  388. /* Compute Cx1 */
  389. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  390. prec->coord[0][1]);
  391. /* Compute Cy1 */
  392. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  393. prec->coord[1][1]);
  394. cblk->zero = 0;
  395. cblk->lblock = 3;
  396. cblk->length = 0;
  397. cblk->lengthinc = 0;
  398. cblk->npasses = 0;
  399. }
  400. }
  401. }
  402. }
  403. return 0;
  404. }
  405. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  406. {
  407. int reslevelno, bandno, precno;
  408. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  409. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  410. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  411. Jpeg2000Band *band = reslevel->band + bandno;
  412. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  413. Jpeg2000Prec *prec = band->prec + precno;
  414. av_freep(&prec->zerobits);
  415. av_freep(&prec->cblkincl);
  416. av_freep(&prec->cblk);
  417. }
  418. av_freep(&band->prec);
  419. }
  420. av_freep(&reslevel->band);
  421. }
  422. ff_dwt_destroy(&comp->dwt);
  423. av_freep(&comp->reslevel);
  424. av_freep(&comp->data);
  425. }