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.

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