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.

511 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/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. } else {
  104. if (d >= 3)
  105. return 8;
  106. if (d == 2) {
  107. if (h + v >= 1)
  108. return 7;
  109. return 6;
  110. }
  111. if (d == 1) {
  112. if (h + v >= 2)
  113. return 5;
  114. if (h + v == 1)
  115. return 4;
  116. return 3;
  117. }
  118. if (h + v >= 2)
  119. return 2;
  120. if (h + v == 1)
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
  126. static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
  127. static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
  128. static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
  129. static int getsgnctxno(int flag, uint8_t *xorbit)
  130. {
  131. int vcontrib, hcontrib;
  132. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
  133. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
  134. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
  135. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
  136. *xorbit = xorbittab[hcontrib][vcontrib];
  137. return ctxlbltab[hcontrib][vcontrib];
  138. }
  139. void ff_jpeg2000_init_tier1_luts(void)
  140. {
  141. int i, j;
  142. for (i = 0; i < 256; i++)
  143. for (j = 0; j < 4; j++)
  144. ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
  145. for (i = 0; i < 16; i++)
  146. for (j = 0; j < 16; j++)
  147. ff_jpeg2000_sgnctxno_lut[i][j] =
  148. getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
  149. }
  150. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
  151. int negative)
  152. {
  153. x++;
  154. y++;
  155. t1->flags[y][x] |= JPEG2000_T1_SIG;
  156. if (negative) {
  157. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  158. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  159. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  160. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  161. } else {
  162. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
  163. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
  164. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
  165. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
  166. }
  167. t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
  168. t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
  169. t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
  170. t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
  171. }
  172. static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
  173. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  174. Jpeg2000CodingStyle *codsty,
  175. Jpeg2000QuantStyle *qntsty,
  176. int cbps, int dx, int dy,
  177. AVCodecContext *avctx)
  178. {
  179. uint8_t log2_band_prec_width, log2_band_prec_height;
  180. int reslevelno, bandno, gbandno = 0, ret, i, j;
  181. uint32_t csize;
  182. if (!codsty->nreslevels2decode) {
  183. av_log(avctx, AV_LOG_ERROR, "nreslevels2decode uninitialized\n");
  184. return AVERROR_INVALIDDATA;
  185. }
  186. if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
  187. codsty->nreslevels2decode - 1,
  188. codsty->transform))
  189. return ret;
  190. // component size comp->coord is uint16_t so ir cannot overflow
  191. csize = (comp->coord[0][1] - comp->coord[0][0]) *
  192. (comp->coord[1][1] - comp->coord[1][0]);
  193. if (codsty->transform == FF_DWT97) {
  194. comp->i_data = NULL;
  195. comp->f_data = av_malloc_array(csize, sizeof(*comp->f_data));
  196. if (!comp->f_data)
  197. return AVERROR(ENOMEM);
  198. } else {
  199. comp->f_data = NULL;
  200. comp->i_data = av_malloc_array(csize, sizeof(*comp->i_data));
  201. if (!comp->i_data)
  202. return AVERROR(ENOMEM);
  203. }
  204. comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
  205. if (!comp->reslevel)
  206. return AVERROR(ENOMEM);
  207. /* LOOP on resolution levels */
  208. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  209. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  210. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  211. /* Compute borders for each resolution level.
  212. * Computation of trx_0, trx_1, try_0 and try_1.
  213. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  214. for (i = 0; i < 2; i++)
  215. for (j = 0; j < 2; j++)
  216. reslevel->coord[i][j] =
  217. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  218. // update precincts size: 2^n value
  219. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  220. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  221. /* Number of bands for each resolution level */
  222. if (reslevelno == 0)
  223. reslevel->nbands = 1;
  224. else
  225. reslevel->nbands = 3;
  226. /* Number of precincts wich span the tile for resolution level reslevelno
  227. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  228. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  229. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  230. * for Dcinema profiles in JPEG 2000
  231. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  232. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  233. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  234. reslevel->num_precincts_x = 0;
  235. else
  236. reslevel->num_precincts_x =
  237. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  238. reslevel->log2_prec_width) -
  239. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  240. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  241. reslevel->num_precincts_y = 0;
  242. else
  243. reslevel->num_precincts_y =
  244. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  245. reslevel->log2_prec_height) -
  246. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  247. reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
  248. if (!reslevel->band)
  249. return AVERROR(ENOMEM);
  250. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  251. Jpeg2000Band *band = reslevel->band + bandno;
  252. int cblkno, precno;
  253. int nb_precincts;
  254. /* TODO: Implementation of quantization step not finished,
  255. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  256. switch (qntsty->quantsty) {
  257. uint8_t gain;
  258. int numbps;
  259. case JPEG2000_QSTY_NONE:
  260. /* TODO: to verify. No quantization in this case */
  261. band->f_stepsize = 1;
  262. break;
  263. case JPEG2000_QSTY_SI:
  264. /*TODO: Compute formula to implement. */
  265. numbps = cbps +
  266. lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)];
  267. band->f_stepsize = SHL(2048 + qntsty->mant[gbandno],
  268. 2 + numbps - qntsty->expn[gbandno]);
  269. break;
  270. case JPEG2000_QSTY_SE:
  271. /* Exponent quantization step.
  272. * Formula:
  273. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  274. * R_b = R_I + log2 (gain_b )
  275. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  276. /* TODO/WARN: value of log2 (gain_b ) not taken into account
  277. * but it works (compared to OpenJPEG). Why?
  278. * Further investigation needed. */
  279. gain = cbps;
  280. band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  281. band->f_stepsize *= qntsty->mant[gbandno] / 2048.0 + 1.0;
  282. break;
  283. default:
  284. band->f_stepsize = 0;
  285. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  286. break;
  287. }
  288. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  289. * If not set output of entropic decoder is not correct. */
  290. if (!av_codec_is_encoder(avctx->codec))
  291. band->f_stepsize *= 0.5;
  292. band->i_stepsize = band->f_stepsize * (1 << 16);
  293. /* computation of tbx_0, tbx_1, tby_0, tby_1
  294. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  295. * codeblock width and height is computed for
  296. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  297. if (reslevelno == 0) {
  298. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  299. for (i = 0; i < 2; i++)
  300. for (j = 0; j < 2; j++)
  301. band->coord[i][j] =
  302. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0],
  303. declvl - 1);
  304. log2_band_prec_width = reslevel->log2_prec_width;
  305. log2_band_prec_height = reslevel->log2_prec_height;
  306. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  307. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  308. reslevel->log2_prec_width);
  309. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  310. reslevel->log2_prec_height);
  311. } else {
  312. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  313. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  314. for (i = 0; i < 2; i++)
  315. for (j = 0; j < 2; j++)
  316. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  317. band->coord[i][j] =
  318. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] - comp->coord_o[i][0] -
  319. (((bandno + 1 >> i) & 1) << declvl - 1),
  320. declvl);
  321. /* TODO: Manage case of 3 band offsets here or
  322. * in coding/decoding function? */
  323. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  324. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  325. reslevel->log2_prec_width - 1);
  326. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  327. reslevel->log2_prec_height - 1);
  328. log2_band_prec_width = reslevel->log2_prec_width - 1;
  329. log2_band_prec_height = reslevel->log2_prec_height - 1;
  330. }
  331. for (j = 0; j < 2; j++)
  332. band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
  333. for (j = 0; j < 2; j++)
  334. band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
  335. band->prec = av_malloc_array(reslevel->num_precincts_x *
  336. reslevel->num_precincts_y,
  337. sizeof(*band->prec));
  338. if (!band->prec)
  339. return AVERROR(ENOMEM);
  340. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  341. for (precno = 0; precno < nb_precincts; precno++) {
  342. Jpeg2000Prec *prec = band->prec + precno;
  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. prec->cblk = av_mallocz_array(prec->nb_codeblocks_width *
  381. prec->nb_codeblocks_height,
  382. sizeof(*prec->cblk));
  383. if (!prec->cblk)
  384. return AVERROR(ENOMEM);
  385. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  386. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  387. uint16_t Cx0, Cy0;
  388. /* Compute coordinates of codeblocks */
  389. /* Compute Cx0*/
  390. Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
  391. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  392. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  393. /* Compute Cy0*/
  394. Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
  395. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  396. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  397. /* Compute Cx1 */
  398. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  399. prec->coord[0][1]);
  400. /* Compute Cy1 */
  401. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  402. prec->coord[1][1]);
  403. /* Update code-blocks coordinates according sub-band position */
  404. if ((bandno + !!reslevelno) & 1) {
  405. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] -
  406. comp->reslevel[reslevelno-1].coord[0][0];
  407. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] -
  408. comp->reslevel[reslevelno-1].coord[0][0];
  409. }
  410. if ((bandno + !!reslevelno) & 2) {
  411. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] -
  412. comp->reslevel[reslevelno-1].coord[1][0];
  413. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] -
  414. comp->reslevel[reslevelno-1].coord[1][0];
  415. }
  416. cblk->zero = 0;
  417. cblk->lblock = 3;
  418. cblk->length = 0;
  419. cblk->lengthinc = 0;
  420. cblk->npasses = 0;
  421. }
  422. }
  423. }
  424. }
  425. return 0;
  426. }
  427. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  428. {
  429. int reslevelno, bandno, precno;
  430. for (reslevelno = 0;
  431. comp->reslevel && reslevelno < codsty->nreslevels;
  432. reslevelno++) {
  433. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  434. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  435. Jpeg2000Band *band = reslevel->band + bandno;
  436. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  437. Jpeg2000Prec *prec = band->prec + precno;
  438. av_freep(&prec->zerobits);
  439. av_freep(&prec->cblkincl);
  440. av_freep(&prec->cblk);
  441. }
  442. av_freep(&band->prec);
  443. }
  444. av_freep(&reslevel->band);
  445. }
  446. ff_dwt_destroy(&comp->dwt);
  447. av_freep(&comp->reslevel);
  448. av_freep(&comp->i_data);
  449. av_freep(&comp->f_data);
  450. }