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.

365 lines
14KB

  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(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, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy)
  152. {
  153. int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
  154. if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
  155. return ret;
  156. for (i = 0; i < 2; i++)
  157. csize *= comp->coord[i][1] - comp->coord[i][0];
  158. comp->data = av_malloc(csize * sizeof(int));
  159. if (!comp->data)
  160. return AVERROR(ENOMEM);
  161. comp->reslevel = av_malloc(codsty->nreslevels * sizeof(Jpeg2000ResLevel));
  162. if (!comp->reslevel)
  163. return AVERROR(ENOMEM);
  164. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  165. int declvl = codsty->nreslevels - reslevelno;
  166. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  167. for (i = 0; i < 2; i++)
  168. for (j = 0; j < 2; j++)
  169. reslevel->coord[i][j] =
  170. ff_jpeg2000_ceildivpow2(comp->coord[i][j], declvl - 1);
  171. if (reslevelno == 0)
  172. reslevel->nbands = 1;
  173. else
  174. reslevel->nbands = 3;
  175. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  176. reslevel->num_precincts_x = 0;
  177. else
  178. reslevel->num_precincts_x = ff_jpeg2000_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
  179. - (reslevel->coord[0][0] >> codsty->log2_prec_width);
  180. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  181. reslevel->num_precincts_y = 0;
  182. else
  183. reslevel->num_precincts_y = ff_jpeg2000_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
  184. - (reslevel->coord[1][0] >> codsty->log2_prec_height);
  185. reslevel->band = av_malloc(reslevel->nbands * sizeof(Jpeg2000Band));
  186. if (!reslevel->band)
  187. return AVERROR(ENOMEM);
  188. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  189. Jpeg2000Band *band = reslevel->band + bandno;
  190. int cblkno, precx, precy, precno;
  191. int x0, y0, x1, y1;
  192. int xi0, yi0, xi1, yi1;
  193. int cblkperprecw, cblkperprech;
  194. if (qntsty->quantsty != JPEG2000_QSTY_NONE) {
  195. static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
  196. int numbps;
  197. numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
  198. band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
  199. } else
  200. band->stepsize = 1 << 13;
  201. if (reslevelno == 0) { // the same everywhere
  202. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
  203. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
  204. for (i = 0; i < 2; i++)
  205. for (j = 0; j < 2; j++)
  206. band->coord[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], declvl-1);
  207. } else{
  208. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
  209. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
  210. for (i = 0; i < 2; i++)
  211. for (j = 0; j < 2; j++)
  212. band->coord[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
  213. }
  214. band->cblknx = ff_jpeg2000_ceildiv(band->coord[0][1], band->codeblock_width) - band->coord[0][0] / band->codeblock_width;
  215. band->cblkny = ff_jpeg2000_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
  216. for (j = 0; j < 2; j++)
  217. band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
  218. for (j = 0; j < 2; j++)
  219. band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
  220. band->cblknx = ff_jpeg2000_ceildiv(band->cblknx, dx);
  221. band->cblkny = ff_jpeg2000_ceildiv(band->cblkny, dy);
  222. band->cblk = av_malloc(sizeof(Jpeg2000Cblk) * band->cblknx * band->cblkny);
  223. if (!band->cblk)
  224. return AVERROR(ENOMEM);
  225. band->prec = av_malloc(sizeof(Jpeg2000Cblk) * reslevel->num_precincts_x * reslevel->num_precincts_y);
  226. if (!band->prec)
  227. return AVERROR(ENOMEM);
  228. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++) {
  229. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  230. cblk->zero = 0;
  231. cblk->lblock = 3;
  232. cblk->length = 0;
  233. cblk->lengthinc = 0;
  234. cblk->npasses = 0;
  235. }
  236. y0 = band->coord[1][0];
  237. y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
  238. yi0 = 0;
  239. yi1 = ff_jpeg2000_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
  240. yi1 = FFMIN(yi1, band->cblkny);
  241. cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
  242. for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++) {
  243. for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++) {
  244. band->prec[precno].yi0 = yi0;
  245. band->prec[precno].yi1 = yi1;
  246. }
  247. yi1 += cblkperprech;
  248. yi0 = yi1 - cblkperprech;
  249. yi1 = FFMIN(yi1, band->cblkny);
  250. }
  251. x0 = band->coord[0][0];
  252. x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
  253. xi0 = 0;
  254. xi1 = ff_jpeg2000_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
  255. xi1 = FFMIN(xi1, band->cblknx);
  256. cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
  257. for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++) {
  258. for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0) {
  259. Jpeg2000Prec *prec = band->prec + precno;
  260. prec->xi0 = xi0;
  261. prec->xi1 = xi1;
  262. prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  263. prec->yi1 - prec->yi0);
  264. prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  265. prec->yi1 - prec->yi0);
  266. if (!prec->cblkincl || !prec->zerobits)
  267. return AVERROR(ENOMEM);
  268. }
  269. xi1 += cblkperprecw;
  270. xi0 = xi1 - cblkperprecw;
  271. xi1 = FFMIN(xi1, band->cblknx);
  272. }
  273. }
  274. }
  275. return 0;
  276. }
  277. void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  278. {
  279. int reslevelno, bandno, cblkno, precno;
  280. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  281. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  282. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  283. Jpeg2000Band *band = rlevel->band + bandno;
  284. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  285. Jpeg2000Prec *prec = band->prec + precno;
  286. tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  287. tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  288. }
  289. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++) {
  290. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  291. cblk->length = 0;
  292. cblk->lblock = 3;
  293. }
  294. }
  295. }
  296. }
  297. void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  298. {
  299. int reslevelno, bandno, precno;
  300. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  301. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  302. for (bandno = 0; bandno < reslevel->nbands ; bandno++) {
  303. Jpeg2000Band *band = reslevel->band + bandno;
  304. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  305. Jpeg2000Prec *prec = band->prec + precno;
  306. av_freep(&prec->zerobits);
  307. av_freep(&prec->cblkincl);
  308. }
  309. av_freep(&band->cblk);
  310. av_freep(&band->prec);
  311. }
  312. av_freep(&reslevel->band);
  313. }
  314. ff_j2k_dwt_destroy(&comp->dwt);
  315. av_freep(&comp->reslevel);
  316. av_freep(&comp->data);
  317. }