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.

392 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. }
  132. uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
  133. static int getsgnctxno(int flag, uint8_t *xorbit)
  134. {
  135. int vcontrib, hcontrib;
  136. static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
  137. static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
  138. static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
  139. hcontrib = contribtab[flag & J2K_T1_SIG_E ? flag & J2K_T1_SGN_E ? 1:2:0]
  140. [flag & J2K_T1_SIG_W ? flag & J2K_T1_SGN_W ? 1:2:0]+1;
  141. vcontrib = contribtab[flag & J2K_T1_SIG_S ? flag & J2K_T1_SGN_S ? 1:2:0]
  142. [flag & J2K_T1_SIG_N ? flag & J2K_T1_SGN_N ? 1:2:0]+1;
  143. *xorbit = xorbittab[hcontrib][vcontrib];
  144. return ctxlbltab[hcontrib][vcontrib];
  145. }
  146. void ff_j2k_init_tier1_luts(void)
  147. {
  148. int i, j;
  149. for (i = 0; i < 256; i++)
  150. for (j = 0; j < 4; j++)
  151. ff_j2k_nbctxno_lut[i][j] = getnbctxno(i, j, 0);
  152. for (i = 0; i < 16; i++)
  153. for (j = 0; j < 16; j++)
  154. ff_j2k_sgnctxno_lut[i][j] = getsgnctxno(i + (j << 8), &ff_j2k_xorbit_lut[i][j]);
  155. }
  156. void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
  157. {
  158. x++; y++;
  159. t1->flags[y][x] |= J2K_T1_SIG;
  160. if (negative){
  161. t1->flags[y][x+1] |= J2K_T1_SIG_W | J2K_T1_SGN_W;
  162. t1->flags[y][x-1] |= J2K_T1_SIG_E | J2K_T1_SGN_E;
  163. t1->flags[y+1][x] |= J2K_T1_SIG_N | J2K_T1_SGN_N;
  164. t1->flags[y-1][x] |= J2K_T1_SIG_S | J2K_T1_SGN_S;
  165. } else{
  166. t1->flags[y][x+1] |= J2K_T1_SIG_W;
  167. t1->flags[y][x-1] |= J2K_T1_SIG_E;
  168. t1->flags[y+1][x] |= J2K_T1_SIG_N;
  169. t1->flags[y-1][x] |= J2K_T1_SIG_S;
  170. }
  171. t1->flags[y+1][x+1] |= J2K_T1_SIG_NW;
  172. t1->flags[y+1][x-1] |= J2K_T1_SIG_NE;
  173. t1->flags[y-1][x+1] |= J2K_T1_SIG_SW;
  174. t1->flags[y-1][x-1] |= J2K_T1_SIG_SE;
  175. }
  176. int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
  177. {
  178. int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
  179. if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
  180. return ret;
  181. for (i = 0; i < 2; i++)
  182. csize *= comp->coord[i][1] - comp->coord[i][0];
  183. comp->data = av_malloc(csize * sizeof(int));
  184. if (!comp->data)
  185. return AVERROR(ENOMEM);
  186. comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
  187. if (!comp->reslevel)
  188. return AVERROR(ENOMEM);
  189. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  190. int declvl = codsty->nreslevels - reslevelno;
  191. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  192. for (i = 0; i < 2; i++)
  193. for (j = 0; j < 2; j++)
  194. reslevel->coord[i][j] =
  195. ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
  196. if (reslevelno == 0)
  197. reslevel->nbands = 1;
  198. else
  199. reslevel->nbands = 3;
  200. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  201. reslevel->num_precincts_x = 0;
  202. else
  203. reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
  204. - (reslevel->coord[0][0] >> codsty->log2_prec_width);
  205. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  206. reslevel->num_precincts_y = 0;
  207. else
  208. reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
  209. - (reslevel->coord[1][0] >> codsty->log2_prec_height);
  210. reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
  211. if (!reslevel->band)
  212. return AVERROR(ENOMEM);
  213. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
  214. J2kBand *band = reslevel->band + bandno;
  215. int cblkno, precx, precy, precno;
  216. int x0, y0, x1, y1;
  217. int xi0, yi0, xi1, yi1;
  218. int cblkperprecw, cblkperprech;
  219. if (qntsty->quantsty != J2K_QSTY_NONE){
  220. const static uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
  221. int numbps;
  222. numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
  223. band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
  224. } else
  225. band->stepsize = 1 << 13;
  226. if (reslevelno == 0){ // the same everywhere
  227. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
  228. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
  229. for (i = 0; i < 2; i++)
  230. for (j = 0; j < 2; j++)
  231. band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
  232. } else{
  233. band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
  234. band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
  235. for (i = 0; i < 2; i++)
  236. for (j = 0; j < 2; j++)
  237. band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
  238. }
  239. band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width) - band->coord[0][0] / band->codeblock_width;
  240. band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
  241. for (j = 0; j < 2; j++)
  242. band->coord[0][j] = ff_j2k_ceildiv(band->coord[0][j], dx);
  243. for (j = 0; j < 2; j++)
  244. band->coord[1][j] = ff_j2k_ceildiv(band->coord[1][j], dy);
  245. band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
  246. band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
  247. band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
  248. if (!band->cblk)
  249. return AVERROR(ENOMEM);
  250. band->prec = av_malloc(reslevel->num_precincts_x * reslevel->num_precincts_y * sizeof(J2kPrec));
  251. if (!band->prec)
  252. return AVERROR(ENOMEM);
  253. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  254. J2kCblk *cblk = band->cblk + cblkno;
  255. cblk->zero = 0;
  256. cblk->lblock = 3;
  257. cblk->length = 0;
  258. cblk->lengthinc = 0;
  259. cblk->npasses = 0;
  260. }
  261. y0 = band->coord[1][0];
  262. y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
  263. yi0 = 0;
  264. yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
  265. yi1 = FFMIN(yi1, band->cblkny);
  266. cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
  267. for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
  268. for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
  269. band->prec[precno].yi0 = yi0;
  270. band->prec[precno].yi1 = yi1;
  271. }
  272. yi1 += cblkperprech;
  273. yi0 = yi1 - cblkperprech;
  274. yi1 = FFMIN(yi1, band->cblkny);
  275. }
  276. x0 = band->coord[0][0];
  277. x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
  278. xi0 = 0;
  279. xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
  280. xi1 = FFMIN(xi1, band->cblknx);
  281. cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
  282. for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
  283. for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
  284. J2kPrec *prec = band->prec + precno;
  285. prec->xi0 = xi0;
  286. prec->xi1 = xi1;
  287. prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  288. prec->yi1 - prec->yi0);
  289. prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
  290. prec->yi1 - prec->yi0);
  291. if (!prec->cblkincl || !prec->zerobits)
  292. return AVERROR(ENOMEM);
  293. }
  294. xi1 += cblkperprecw;
  295. xi0 = xi1 - cblkperprecw;
  296. xi1 = FFMIN(xi1, band->cblknx);
  297. }
  298. }
  299. }
  300. return 0;
  301. }
  302. void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
  303. {
  304. int reslevelno, bandno, cblkno, precno;
  305. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  306. J2kResLevel *rlevel = comp->reslevel + reslevelno;
  307. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  308. J2kBand *band = rlevel->band + bandno;
  309. for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
  310. J2kPrec *prec = band->prec + precno;
  311. tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  312. tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
  313. }
  314. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  315. J2kCblk *cblk = band->cblk + cblkno;
  316. cblk->length = 0;
  317. cblk->lblock = 3;
  318. }
  319. }
  320. }
  321. }
  322. void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
  323. {
  324. int reslevelno, bandno, precno;
  325. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  326. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  327. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  328. J2kBand *band = reslevel->band + bandno;
  329. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  330. J2kPrec *prec = band->prec + precno;
  331. av_freep(&prec->zerobits);
  332. av_freep(&prec->cblkincl);
  333. }
  334. av_freep(&band->cblk);
  335. av_freep(&band->prec);
  336. }
  337. av_freep(&reslevel->band);
  338. }
  339. ff_j2k_dwt_destroy(&comp->dwt);
  340. av_freep(&comp->reslevel);
  341. av_freep(&comp->data);
  342. }