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.

357 lines
13KB

  1. /*
  2. * JPEG2000 encoder and decoder common functions
  3. * Copyright (c) 2007 Kamil Nowosad
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * JPEG2000 image encoder and decoder common functions
  23. * @file
  24. * @author Kamil Nowosad
  25. */
  26. #include "avcodec.h"
  27. #include "j2k.h"
  28. #define SHL(a, n) ((n)>=0 ? (a) << (n) : (a) >> -(n))
  29. /* tag tree routines */
  30. /** allocate the memory for tag tree */
  31. static int tag_tree_size(int w, int h)
  32. {
  33. int res = 0;
  34. while (w > 1 || h > 1){
  35. res += w * h;
  36. w = (w+1) >> 1;
  37. h = (h+1) >> 1;
  38. }
  39. return res + 1;
  40. }
  41. Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h)
  42. {
  43. int pw = w, ph = h;
  44. Jpeg2000TgtNode *res, *t, *t2;
  45. t = res = av_mallocz(tag_tree_size(w, h)*sizeof(Jpeg2000TgtNode));
  46. if (res == NULL)
  47. return NULL;
  48. while (w > 1 || h > 1){
  49. int i, j;
  50. pw = w;
  51. ph = h;
  52. w = (w+1) >> 1;
  53. h = (h+1) >> 1;
  54. t2 = t + pw*ph;
  55. for (i = 0; i < ph; i++)
  56. for (j = 0; j < pw; j++){
  57. t[i*pw + j].parent = &t2[(i>>1)*w + (j>>1)];
  58. }
  59. t = t2;
  60. }
  61. t[0].parent = NULL;
  62. return res;
  63. }
  64. static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
  65. {
  66. int i, siz = tag_tree_size(w, h);
  67. for (i = 0; i < siz; i++){
  68. t[i].val = 0;
  69. t[i].vis = 0;
  70. }
  71. }
  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) return 8;
  87. if (h == 1){
  88. if (v >= 1) return 7;
  89. if (d >= 1) return 6;
  90. return 5;
  91. }
  92. if (v == 2) return 4;
  93. if (v == 1) return 3;
  94. if (d >= 2) return 2;
  95. if (d == 1) return 1;
  96. } else{
  97. if (d >= 3) return 8;
  98. if (d == 2){
  99. if (h+v >= 1) return 7;
  100. return 6;
  101. }
  102. if (d == 1){
  103. if (h+v >= 2) return 5;
  104. if (h+v == 1) return 4;
  105. return 3;
  106. }
  107. if (h+v >= 2) return 2;
  108. if (h+v == 1) return 1;
  109. }
  110. return 0;
  111. }
  112. static int getsgnctxno(int flag, uint8_t *xorbit)
  113. {
  114. int vcontrib, hcontrib;
  115. static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
  116. static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
  117. static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
  118. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1:2:0]
  119. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1:2:0]+1;
  120. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1:2:0]
  121. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1:2:0]+1;
  122. *xorbit = xorbittab[hcontrib][vcontrib];
  123. return ctxlbltab[hcontrib][vcontrib];
  124. }
  125. void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y, int negative)
  126. {
  127. x++; y++;
  128. t1->flags[y][x] |= JPEG2000_T1_SIG;
  129. if (negative){
  130. t1->flags[y][x+1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  131. t1->flags[y][x-1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  132. t1->flags[y+1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  133. t1->flags[y-1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  134. } else{
  135. t1->flags[y][x+1] |= JPEG2000_T1_SIG_W;
  136. t1->flags[y][x-1] |= JPEG2000_T1_SIG_E;
  137. t1->flags[y+1][x] |= JPEG2000_T1_SIG_N;
  138. t1->flags[y-1][x] |= JPEG2000_T1_SIG_S;
  139. }
  140. t1->flags[y+1][x+1] |= JPEG2000_T1_SIG_NW;
  141. t1->flags[y+1][x-1] |= JPEG2000_T1_SIG_NE;
  142. t1->flags[y-1][x+1] |= JPEG2000_T1_SIG_SW;
  143. t1->flags[y-1][x-1] |= JPEG2000_T1_SIG_SE;
  144. }
  145. int ff_j2k_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy)
  146. {
  147. int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
  148. if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
  149. return ret;
  150. for (i = 0; i < 2; i++)
  151. csize *= comp->coord[i][1] - comp->coord[i][0];
  152. comp->data = av_malloc(csize * sizeof(int));
  153. if (!comp->data)
  154. return AVERROR(ENOMEM);
  155. comp->reslevel = av_malloc(codsty->nreslevels * sizeof(Jpeg2000ResLevel));
  156. if (!comp->reslevel)
  157. return AVERROR(ENOMEM);
  158. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  159. int declvl = codsty->nreslevels - reslevelno;
  160. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  161. for (i = 0; i < 2; i++)
  162. for (j = 0; j < 2; j++)
  163. reslevel->coord[i][j] =
  164. ff_jpeg2000_ceildivpow2(comp->coord[i][j], declvl - 1);
  165. if (reslevelno == 0)
  166. reslevel->nbands = 1;
  167. else
  168. reslevel->nbands = 3;
  169. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  170. reslevel->num_precincts_x = 0;
  171. else
  172. reslevel->num_precincts_x = ff_jpeg2000_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
  173. - (reslevel->coord[0][0] >> codsty->log2_prec_width);
  174. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  175. reslevel->num_precincts_y = 0;
  176. else
  177. reslevel->num_precincts_y = ff_jpeg2000_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
  178. - (reslevel->coord[1][0] >> codsty->log2_prec_height);
  179. reslevel->band = av_malloc(reslevel->nbands * sizeof(Jpeg2000Band));
  180. if (!reslevel->band)
  181. return AVERROR(ENOMEM);
  182. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
  183. Jpeg2000Band *band = reslevel->band + bandno;
  184. int cblkno, precx, precy, precno;
  185. int x0, y0, x1, y1;
  186. int xi0, yi0, xi1, yi1;
  187. int cblkperprecw, cblkperprech;
  188. if (qntsty->quantsty != JPEG2000_QSTY_NONE){
  189. static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
  190. int numbps;
  191. numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
  192. band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
  193. } else
  194. band->stepsize = 1 << 13;
  195. if (reslevelno == 0){ // the same everywhere
  196. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
  197. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
  198. for (i = 0; i < 2; i++)
  199. for (j = 0; j < 2; j++)
  200. band->coord[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], declvl-1);
  201. } else{
  202. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
  203. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
  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] - (((bandno+1>>i)&1) << declvl-1), declvl);
  207. }
  208. band->cblknx = ff_jpeg2000_ceildiv(band->coord[0][1], band->codeblock_width) - band->coord[0][0] / band->codeblock_width;
  209. band->cblkny = ff_jpeg2000_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
  210. for (j = 0; j < 2; j++)
  211. band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
  212. for (j = 0; j < 2; j++)
  213. band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
  214. band->cblknx = ff_jpeg2000_ceildiv(band->cblknx, dx);
  215. band->cblkny = ff_jpeg2000_ceildiv(band->cblkny, dy);
  216. band->cblk = av_malloc(sizeof(Jpeg2000Cblk) * band->cblknx * band->cblkny);
  217. if (!band->cblk)
  218. return AVERROR(ENOMEM);
  219. band->prec = av_malloc(sizeof(Jpeg2000Cblk) * reslevel->num_precincts_x * reslevel->num_precincts_y);
  220. if (!band->prec)
  221. return AVERROR(ENOMEM);
  222. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  223. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  224. cblk->zero = 0;
  225. cblk->lblock = 3;
  226. cblk->length = 0;
  227. cblk->lengthinc = 0;
  228. cblk->npasses = 0;
  229. }
  230. y0 = band->coord[1][0];
  231. y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
  232. yi0 = 0;
  233. yi1 = ff_jpeg2000_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
  234. yi1 = FFMIN(yi1, band->cblkny);
  235. cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
  236. for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
  237. for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
  238. band->prec[precno].yi0 = yi0;
  239. band->prec[precno].yi1 = yi1;
  240. }
  241. yi1 += cblkperprech;
  242. yi0 = yi1 - cblkperprech;
  243. yi1 = FFMIN(yi1, band->cblkny);
  244. }
  245. x0 = band->coord[0][0];
  246. x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
  247. xi0 = 0;
  248. xi1 = ff_jpeg2000_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
  249. xi1 = FFMIN(xi1, band->cblknx);
  250. cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
  251. for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
  252. for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
  253. Jpeg2000Prec *prec = band->prec + precno;
  254. prec->xi0 = xi0;
  255. prec->xi1 = xi1;
  256. prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  257. prec->yi1 - prec->yi0);
  258. prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  259. prec->yi1 - prec->yi0);
  260. if (!prec->cblkincl || !prec->zerobits)
  261. return AVERROR(ENOMEM);
  262. }
  263. xi1 += cblkperprecw;
  264. xi0 = xi1 - cblkperprecw;
  265. xi1 = FFMIN(xi1, band->cblknx);
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  272. {
  273. int reslevelno, bandno, cblkno, precno;
  274. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  275. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  276. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  277. Jpeg2000Band *band = rlevel->band + bandno;
  278. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
  279. Jpeg2000Prec *prec = band->prec + precno;
  280. tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  281. tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  282. }
  283. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  284. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  285. cblk->length = 0;
  286. cblk->lblock = 3;
  287. }
  288. }
  289. }
  290. }
  291. void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  292. {
  293. int reslevelno, bandno, precno;
  294. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  295. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  296. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  297. Jpeg2000Band *band = reslevel->band + bandno;
  298. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  299. Jpeg2000Prec *prec = band->prec + precno;
  300. av_freep(&prec->zerobits);
  301. av_freep(&prec->cblkincl);
  302. }
  303. av_freep(&band->cblk);
  304. av_freep(&band->prec);
  305. }
  306. av_freep(&reslevel->band);
  307. }
  308. ff_j2k_dwt_destroy(&comp->dwt);
  309. av_freep(&comp->reslevel);
  310. av_freep(&comp->data);
  311. }