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.

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