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.

393 lines
14KB

  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. #if 0
  30. void ff_j2k_printv(int *tab, int l)
  31. {
  32. int i;
  33. for (i = 0; i < l; i++)
  34. printf("%.3d ", tab[i]);
  35. printf("\n");
  36. }
  37. void ff_j2k_printu(uint8_t *tab, int l)
  38. {
  39. int i;
  40. for (i = 0; i < l; i++)
  41. printf("%.3hd ", tab[i]);
  42. printf("\n");
  43. }
  44. #endif
  45. /* tag tree routines */
  46. /** allocate the memory for tag tree */
  47. static int tag_tree_size(int w, int h)
  48. {
  49. int res = 0;
  50. while (w > 1 || h > 1){
  51. res += w * h;
  52. w = (w+1) >> 1;
  53. h = (h+1) >> 1;
  54. }
  55. return res + 1;
  56. }
  57. J2kTgtNode *ff_j2k_tag_tree_init(int w, int h)
  58. {
  59. int pw = w, ph = h;
  60. J2kTgtNode *res, *t, *t2;
  61. t = res = av_mallocz(tag_tree_size(w, h)*sizeof(J2kTgtNode));
  62. if (res == NULL)
  63. return NULL;
  64. while (w > 1 || h > 1){
  65. int i, j;
  66. pw = w;
  67. ph = h;
  68. w = (w+1) >> 1;
  69. h = (h+1) >> 1;
  70. t2 = t + pw*ph;
  71. for (i = 0; i < ph; i++)
  72. for (j = 0; j < pw; j++){
  73. t[i*pw + j].parent = &t2[(i>>1)*w + (j>>1)];
  74. }
  75. t = t2;
  76. }
  77. t[0].parent = NULL;
  78. return res;
  79. }
  80. static void tag_tree_zero(J2kTgtNode *t, int w, int h)
  81. {
  82. int i, siz = tag_tree_size(w, h);
  83. for (i = 0; i < siz; i++){
  84. t[i].val = 0;
  85. t[i].vis = 0;
  86. }
  87. }
  88. uint8_t ff_j2k_nbctxno_lut[256][4];
  89. static int getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
  90. {
  91. int h, v, d;
  92. h = ((flag & J2K_T1_SIG_E) ? 1:0)+
  93. ((flag & J2K_T1_SIG_W) ? 1:0);
  94. v = ((flag & J2K_T1_SIG_N) ? 1:0);
  95. if (!vert_causal_ctx_csty_symbol)
  96. v = v + ((flag & J2K_T1_SIG_S) ? 1:0);
  97. d = ((flag & J2K_T1_SIG_NE) ? 1:0)+
  98. ((flag & J2K_T1_SIG_NW) ? 1:0);
  99. if (!vert_causal_ctx_csty_symbol)
  100. d = d + ((flag & J2K_T1_SIG_SE) ? 1:0)+
  101. ((flag & J2K_T1_SIG_SW) ? 1:0);
  102. if (bandno < 3){
  103. if (bandno == 1)
  104. FFSWAP(int, h, v);
  105. if (h == 2) return 8;
  106. if (h == 1){
  107. if (v >= 1) return 7;
  108. if (d >= 1) return 6;
  109. return 5;
  110. }
  111. if (v == 2) return 4;
  112. if (v == 1) return 3;
  113. if (d >= 2) return 2;
  114. if (d == 1) return 1;
  115. return 0;
  116. } else{
  117. if (d >= 3) return 8;
  118. if (d == 2){
  119. if (h+v >= 1) return 7;
  120. return 6;
  121. }
  122. if (d == 1){
  123. if (h+v >= 2) return 5;
  124. if (h+v == 1) return 4;
  125. return 3;
  126. }
  127. if (h+v >= 2) return 2;
  128. if (h+v == 1) return 1;
  129. return 0;
  130. }
  131. assert(0);
  132. }
  133. uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
  134. static int getsgnctxno(int flag, uint8_t *xorbit)
  135. {
  136. int vcontrib, hcontrib;
  137. static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
  138. static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
  139. static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
  140. hcontrib = contribtab[flag & J2K_T1_SIG_E ? flag & J2K_T1_SGN_E ? 1:2:0]
  141. [flag & J2K_T1_SIG_W ? flag & J2K_T1_SGN_W ? 1:2:0]+1;
  142. vcontrib = contribtab[flag & J2K_T1_SIG_S ? flag & J2K_T1_SGN_S ? 1:2:0]
  143. [flag & J2K_T1_SIG_N ? flag & J2K_T1_SGN_N ? 1:2:0]+1;
  144. *xorbit = xorbittab[hcontrib][vcontrib];
  145. return ctxlbltab[hcontrib][vcontrib];
  146. }
  147. void ff_j2k_init_tier1_luts(void)
  148. {
  149. int i, j;
  150. for (i = 0; i < 256; i++)
  151. for (j = 0; j < 4; j++)
  152. ff_j2k_nbctxno_lut[i][j] = getnbctxno(i, j, 0);
  153. for (i = 0; i < 16; i++)
  154. for (j = 0; j < 16; j++)
  155. ff_j2k_sgnctxno_lut[i][j] = getsgnctxno(i + (j << 8), &ff_j2k_xorbit_lut[i][j]);
  156. }
  157. void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
  158. {
  159. x++; y++;
  160. t1->flags[y][x] |= J2K_T1_SIG;
  161. if (negative){
  162. t1->flags[y][x+1] |= J2K_T1_SIG_W | J2K_T1_SGN_W;
  163. t1->flags[y][x-1] |= J2K_T1_SIG_E | J2K_T1_SGN_E;
  164. t1->flags[y+1][x] |= J2K_T1_SIG_N | J2K_T1_SGN_N;
  165. t1->flags[y-1][x] |= J2K_T1_SIG_S | J2K_T1_SGN_S;
  166. } else{
  167. t1->flags[y][x+1] |= J2K_T1_SIG_W;
  168. t1->flags[y][x-1] |= J2K_T1_SIG_E;
  169. t1->flags[y+1][x] |= J2K_T1_SIG_N;
  170. t1->flags[y-1][x] |= J2K_T1_SIG_S;
  171. }
  172. t1->flags[y+1][x+1] |= J2K_T1_SIG_NW;
  173. t1->flags[y+1][x-1] |= J2K_T1_SIG_NE;
  174. t1->flags[y-1][x+1] |= J2K_T1_SIG_SW;
  175. t1->flags[y-1][x-1] |= J2K_T1_SIG_SE;
  176. }
  177. int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
  178. {
  179. int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
  180. if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
  181. return ret;
  182. for (i = 0; i < 2; i++)
  183. csize *= comp->coord[i][1] - comp->coord[i][0];
  184. comp->data = av_malloc(csize * sizeof(int));
  185. if (!comp->data)
  186. return AVERROR(ENOMEM);
  187. comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
  188. if (!comp->reslevel)
  189. return AVERROR(ENOMEM);
  190. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  191. int declvl = codsty->nreslevels - reslevelno;
  192. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  193. for (i = 0; i < 2; i++)
  194. for (j = 0; j < 2; j++)
  195. reslevel->coord[i][j] =
  196. ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
  197. if (reslevelno == 0)
  198. reslevel->nbands = 1;
  199. else
  200. reslevel->nbands = 3;
  201. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  202. reslevel->num_precincts_x = 0;
  203. else
  204. reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
  205. - (reslevel->coord[0][0] >> codsty->log2_prec_width);
  206. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  207. reslevel->num_precincts_y = 0;
  208. else
  209. reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
  210. - (reslevel->coord[1][0] >> codsty->log2_prec_height);
  211. reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
  212. if (!reslevel->band)
  213. return AVERROR(ENOMEM);
  214. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
  215. J2kBand *band = reslevel->band + bandno;
  216. int cblkno, precx, precy, precno;
  217. int x0, y0, x1, y1;
  218. int xi0, yi0, xi1, yi1;
  219. int cblkperprecw, cblkperprech;
  220. if (qntsty->quantsty != J2K_QSTY_NONE){
  221. const static uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
  222. int numbps;
  223. numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
  224. band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
  225. } else
  226. band->stepsize = 1 << 13;
  227. if (reslevelno == 0){ // the same everywhere
  228. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
  229. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
  230. for (i = 0; i < 2; i++)
  231. for (j = 0; j < 2; j++)
  232. band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
  233. } else{
  234. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
  235. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
  236. for (i = 0; i < 2; i++)
  237. for (j = 0; j < 2; j++)
  238. band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
  239. }
  240. band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width) - band->coord[0][0] / band->codeblock_width;
  241. band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
  242. for (j = 0; j < 2; j++)
  243. band->coord[0][j] = ff_j2k_ceildiv(band->coord[0][j], dx);
  244. for (j = 0; j < 2; j++)
  245. band->coord[1][j] = ff_j2k_ceildiv(band->coord[1][j], dy);
  246. band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
  247. band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
  248. band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
  249. if (!band->cblk)
  250. return AVERROR(ENOMEM);
  251. band->prec = av_malloc(reslevel->num_precincts_x * reslevel->num_precincts_y * sizeof(J2kPrec));
  252. if (!band->prec)
  253. return AVERROR(ENOMEM);
  254. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  255. J2kCblk *cblk = band->cblk + cblkno;
  256. cblk->zero = 0;
  257. cblk->lblock = 3;
  258. cblk->length = 0;
  259. cblk->lengthinc = 0;
  260. cblk->npasses = 0;
  261. }
  262. y0 = band->coord[1][0];
  263. y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
  264. yi0 = 0;
  265. yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
  266. yi1 = FFMIN(yi1, band->cblkny);
  267. cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
  268. for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
  269. for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
  270. band->prec[precno].yi0 = yi0;
  271. band->prec[precno].yi1 = yi1;
  272. }
  273. yi1 += cblkperprech;
  274. yi0 = yi1 - cblkperprech;
  275. yi1 = FFMIN(yi1, band->cblkny);
  276. }
  277. x0 = band->coord[0][0];
  278. x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
  279. xi0 = 0;
  280. xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
  281. xi1 = FFMIN(xi1, band->cblknx);
  282. cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
  283. for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
  284. for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
  285. J2kPrec *prec = band->prec + precno;
  286. prec->xi0 = xi0;
  287. prec->xi1 = xi1;
  288. prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  289. prec->yi1 - prec->yi0);
  290. prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  291. prec->yi1 - prec->yi0);
  292. if (!prec->cblkincl || !prec->zerobits)
  293. return AVERROR(ENOMEM);
  294. }
  295. xi1 += cblkperprecw;
  296. xi0 = xi1 - cblkperprecw;
  297. xi1 = FFMIN(xi1, band->cblknx);
  298. }
  299. }
  300. }
  301. return 0;
  302. }
  303. void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
  304. {
  305. int reslevelno, bandno, cblkno, precno;
  306. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  307. J2kResLevel *rlevel = comp->reslevel + reslevelno;
  308. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  309. J2kBand *band = rlevel->band + bandno;
  310. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
  311. J2kPrec *prec = band->prec + precno;
  312. tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  313. tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  314. }
  315. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  316. J2kCblk *cblk = band->cblk + cblkno;
  317. cblk->length = 0;
  318. cblk->lblock = 3;
  319. }
  320. }
  321. }
  322. }
  323. void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
  324. {
  325. int reslevelno, bandno, precno;
  326. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  327. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  328. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  329. J2kBand *band = reslevel->band + bandno;
  330. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  331. J2kPrec *prec = band->prec + precno;
  332. av_freep(&prec->zerobits);
  333. av_freep(&prec->cblkincl);
  334. }
  335. av_freep(&band->cblk);
  336. av_freep(&band->prec);
  337. }
  338. av_freep(&reslevel->band);
  339. }
  340. ff_j2k_dwt_destroy(&comp->dwt);
  341. av_freep(&comp->reslevel);
  342. av_freep(&comp->data);
  343. }