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.

415 lines
16KB

  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/common.h"
  28. #include "libavutil/mem.h"
  29. #include "avcodec.h"
  30. #include "j2k.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 int tag_tree_size(int w, int h)
  35. {
  36. int res = 0;
  37. while (w > 1 || h > 1) {
  38. res += w * h;
  39. w = (w + 1) >> 1;
  40. h = (h + 1) >> 1;
  41. }
  42. return res + 1;
  43. }
  44. Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h)
  45. {
  46. int pw = w, ph = h;
  47. Jpeg2000TgtNode *res, *t, *t2;
  48. int32_t tt_size;
  49. tt_size = tag_tree_size(w, h);
  50. t = res = av_mallocz_array(tt_size, sizeof(*t));
  51. if (!res)
  52. return NULL;
  53. while (w > 1 || h > 1) {
  54. int i, j;
  55. pw = w;
  56. ph = h;
  57. w = (w + 1) >> 1;
  58. h = (h + 1) >> 1;
  59. t2 = t + pw * ph;
  60. for (i = 0; i < ph; i++)
  61. for (j = 0; j < pw; j++)
  62. t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
  63. t = t2;
  64. }
  65. t[0].parent = NULL;
  66. return res;
  67. }
  68. static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
  69. {
  70. int i, siz = tag_tree_size(w, h);
  71. for (i = 0; i < siz; i++) {
  72. t[i].val = 0;
  73. t[i].vis = 0;
  74. }
  75. }
  76. static int getsigctxno(int flag, int bandno)
  77. {
  78. int h, v, d;
  79. h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
  80. ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
  81. v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
  82. ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
  83. d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
  84. ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
  85. ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
  86. ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
  87. if (bandno < 3) {
  88. if (bandno == 1)
  89. FFSWAP(int, h, v);
  90. if (h == 2) return 8;
  91. if (h == 1) {
  92. if (v >= 1) return 7;
  93. if (d >= 1) return 6;
  94. return 5;
  95. }
  96. if (v == 2) return 4;
  97. if (v == 1) return 3;
  98. if (d >= 2) return 2;
  99. if (d == 1) return 1;
  100. } else {
  101. if (d >= 3) return 8;
  102. if (d == 2) {
  103. if (h+v >= 1) return 7;
  104. return 6;
  105. }
  106. if (d == 1) {
  107. if (h+v >= 2) return 5;
  108. if (h+v == 1) return 4;
  109. return 3;
  110. }
  111. if (h+v >= 2) return 2;
  112. if (h+v == 1) return 1;
  113. }
  114. return 0;
  115. }
  116. static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
  117. static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
  118. static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
  119. static int getsgnctxno(int flag, uint8_t *xorbit)
  120. {
  121. int vcontrib, hcontrib;
  122. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
  123. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
  124. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
  125. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
  126. *xorbit = xorbittab[hcontrib][vcontrib];
  127. return ctxlbltab[hcontrib][vcontrib];
  128. }
  129. void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y,
  130. int negative)
  131. {
  132. x++;
  133. y++;
  134. t1->flags[y][x] |= JPEG2000_T1_SIG;
  135. if (negative) {
  136. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  137. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  138. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  139. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  140. } else {
  141. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
  142. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
  143. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
  144. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
  145. }
  146. t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
  147. t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
  148. t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
  149. t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
  150. }
  151. int ff_j2k_init_component(Jpeg2000Component *comp,
  152. Jpeg2000CodingStyle *codsty,
  153. Jpeg2000QuantStyle *qntsty,
  154. int cbps, int dx, int dy)
  155. {
  156. uint8_t log2_band_prec_width, log2_band_prec_height;
  157. int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
  158. if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
  159. return ret;
  160. for (i = 0; i < 2; i++)
  161. csize *= comp->coord[i][1] - comp->coord[i][0];
  162. comp->data = av_malloc_array(csize, sizeof(*comp->data));
  163. if (!comp->data)
  164. return AVERROR(ENOMEM);
  165. comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
  166. if (!comp->reslevel)
  167. return AVERROR(ENOMEM);
  168. /* LOOP on resolution levels */
  169. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  170. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  171. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  172. /* Compute borders for each resolution level.
  173. * Computation of trx_0, trx_1, try_0 and try_1.
  174. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  175. for (i = 0; i < 2; i++)
  176. for (j = 0; j < 2; j++)
  177. reslevel->coord[i][j] =
  178. ff_jpeg2000_ceildivpow2(comp->coord[i][j], declvl - 1);
  179. // update precincts size: 2^n value
  180. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  181. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  182. /* Number of bands for each resolution level */
  183. if (reslevelno == 0)
  184. reslevel->nbands = 1;
  185. else
  186. reslevel->nbands = 3;
  187. /* Number of precincts wich span the tile for resolution level reslevelno
  188. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  189. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  190. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  191. * for Dcinema profiles in JPEG 2000
  192. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  193. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  194. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  195. reslevel->num_precincts_x = 0;
  196. else
  197. reslevel->num_precincts_x =
  198. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  199. reslevel->log2_prec_width) -
  200. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  201. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  202. reslevel->num_precincts_y = 0;
  203. else
  204. reslevel->num_precincts_y =
  205. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  206. reslevel->log2_prec_height) -
  207. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  208. reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
  209. if (!reslevel->band)
  210. return AVERROR(ENOMEM);
  211. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  212. Jpeg2000Band *band = reslevel->band + bandno;
  213. int cblkno, precx, precy, precno;
  214. int x0, y0, x1, y1;
  215. int xi0, yi0, xi1, yi1;
  216. int cblkperprecw, cblkperprech;
  217. if (qntsty->quantsty != JPEG2000_QSTY_NONE) {
  218. static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
  219. int numbps;
  220. numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
  221. band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
  222. } else
  223. band->stepsize = 1 << 13;
  224. if (reslevelno == 0) {
  225. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  226. for (i = 0; i < 2; i++)
  227. for (j = 0; j < 2; j++)
  228. band->coord[i][j] =
  229. ff_jpeg2000_ceildivpow2(comp->coord[i][j],
  230. declvl - 1);
  231. log2_band_prec_width = reslevel->log2_prec_width;
  232. log2_band_prec_height = reslevel->log2_prec_height;
  233. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  234. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  235. reslevel->log2_prec_width);
  236. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  237. reslevel->log2_prec_height);
  238. } else {
  239. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  240. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  241. for (i = 0; i < 2; i++)
  242. for (j = 0; j < 2; j++)
  243. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  244. band->coord[i][j] =
  245. ff_jpeg2000_ceildivpow2(comp->coord[i][j] -
  246. (((bandno + 1 >> i) & 1) << declvl - 1),
  247. declvl);
  248. /* TODO: Manage case of 3 band offsets here or
  249. * in coding/decoding function? */
  250. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  251. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  252. reslevel->log2_prec_width - 1);
  253. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  254. reslevel->log2_prec_height - 1);
  255. log2_band_prec_width = reslevel->log2_prec_width - 1;
  256. log2_band_prec_height = reslevel->log2_prec_height - 1;
  257. }
  258. band->cblknx = ff_jpeg2000_ceildivpow2(band->coord[0][1], band->log2_cblk_width) - (band->coord[0][0] >> band->log2_cblk_width);
  259. band->cblkny = ff_jpeg2000_ceildivpow2(band->coord[1][1], band->log2_cblk_height) - (band->coord[1][0] >> band->log2_cblk_height);
  260. for (j = 0; j < 2; j++)
  261. band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
  262. for (j = 0; j < 2; j++)
  263. band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
  264. band->cblknx = ff_jpeg2000_ceildiv(band->cblknx, dx);
  265. band->cblkny = ff_jpeg2000_ceildiv(band->cblkny, dy);
  266. band->cblk = av_malloc_array(band->cblknx *
  267. band->cblkny,
  268. sizeof(*band->cblk));
  269. if (!band->cblk)
  270. return AVERROR(ENOMEM);
  271. band->prec = av_malloc_array(reslevel->num_precincts_x *
  272. reslevel->num_precincts_y,
  273. sizeof(*band->prec));
  274. if (!band->prec)
  275. return AVERROR(ENOMEM);
  276. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++) {
  277. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  278. cblk->zero = 0;
  279. cblk->lblock = 3;
  280. cblk->length = 0;
  281. cblk->lengthinc = 0;
  282. cblk->npasses = 0;
  283. }
  284. y0 = band->coord[1][0];
  285. y1 = ((band->coord[1][0] + (1<<reslevel->log2_prec_height)) & ~((1<<reslevel->log2_prec_height)-1)) - y0;
  286. yi0 = 0;
  287. yi1 = ff_jpeg2000_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
  288. yi1 = FFMIN(yi1, band->cblkny);
  289. cblkperprech = 1<<(reslevel->log2_prec_height - codsty->log2_cblk_height);
  290. for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++) {
  291. for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++) {
  292. band->prec[precno].yi0 = yi0;
  293. band->prec[precno].yi1 = yi1;
  294. }
  295. yi1 += cblkperprech;
  296. yi0 = yi1 - cblkperprech;
  297. yi1 = FFMIN(yi1, band->cblkny);
  298. }
  299. x0 = band->coord[0][0];
  300. x1 = ((band->coord[0][0] + (1<<reslevel->log2_prec_width)) & ~((1<<reslevel->log2_prec_width)-1)) - x0;
  301. xi0 = 0;
  302. xi1 = ff_jpeg2000_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
  303. xi1 = FFMIN(xi1, band->cblknx);
  304. cblkperprecw = 1<<(reslevel->log2_prec_width - codsty->log2_cblk_width);
  305. for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++) {
  306. for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0) {
  307. Jpeg2000Prec *prec = band->prec + precno;
  308. prec->xi0 = xi0;
  309. prec->xi1 = xi1;
  310. prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  311. prec->yi1 - prec->yi0);
  312. prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  313. prec->yi1 - prec->yi0);
  314. if (!prec->cblkincl || !prec->zerobits)
  315. return AVERROR(ENOMEM);
  316. }
  317. xi1 += cblkperprecw;
  318. xi0 = xi1 - cblkperprecw;
  319. xi1 = FFMIN(xi1, band->cblknx);
  320. }
  321. }
  322. }
  323. return 0;
  324. }
  325. void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  326. {
  327. int reslevelno, bandno, cblkno, precno;
  328. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  329. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  330. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  331. Jpeg2000Band *band = rlevel->band + bandno;
  332. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  333. Jpeg2000Prec *prec = band->prec + precno;
  334. tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  335. tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  336. }
  337. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++) {
  338. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  339. cblk->length = 0;
  340. cblk->lblock = 3;
  341. }
  342. }
  343. }
  344. }
  345. void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  346. {
  347. int reslevelno, bandno, precno;
  348. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  349. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  350. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  351. Jpeg2000Band *band = reslevel->band + bandno;
  352. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  353. Jpeg2000Prec *prec = band->prec + precno;
  354. av_freep(&prec->zerobits);
  355. av_freep(&prec->cblkincl);
  356. }
  357. av_freep(&band->cblk);
  358. av_freep(&band->prec);
  359. }
  360. av_freep(&reslevel->band);
  361. }
  362. ff_j2k_dwt_destroy(&comp->dwt);
  363. av_freep(&comp->reslevel);
  364. av_freep(&comp->data);
  365. }