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.

524 lines
20KB

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