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.

484 lines
19KB

  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. * @file
  24. * JPEG 2000 image encoder and decoder common functions
  25. */
  26. #include "libavutil/common.h"
  27. #include "libavutil/mem.h"
  28. #include "avcodec.h"
  29. #include "jpeg2000.h"
  30. #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
  31. /* tag tree routines */
  32. /* allocate the memory for tag tree */
  33. static int32_t tag_tree_size(uint16_t w, uint16_t h)
  34. {
  35. uint32_t res = 0;
  36. while (w > 1 || h > 1) {
  37. res += w * h;
  38. if (res + 1 >= INT32_MAX)
  39. return -1;
  40. w = (w + 1) >> 1;
  41. h = (h + 1) >> 1;
  42. }
  43. return (int32_t)(res + 1);
  44. }
  45. static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
  46. {
  47. int pw = w, ph = h;
  48. Jpeg2000TgtNode *res, *t, *t2;
  49. int32_t tt_size;
  50. tt_size = tag_tree_size(w, h);
  51. if (tt_size == -1)
  52. return NULL;
  53. t = res = av_mallocz_array(tt_size, sizeof(*t));
  54. if (!res)
  55. return NULL;
  56. while (w > 1 || h > 1) {
  57. int i, j;
  58. pw = w;
  59. ph = h;
  60. w = (w + 1) >> 1;
  61. h = (h + 1) >> 1;
  62. t2 = t + pw * ph;
  63. for (i = 0; i < ph; i++)
  64. for (j = 0; j < pw; j++)
  65. t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
  66. t = t2;
  67. }
  68. t[0].parent = NULL;
  69. return res;
  70. }
  71. uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  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. uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
  113. static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
  114. static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
  115. static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
  116. static int getsgnctxno(int flag, uint8_t *xorbit)
  117. {
  118. int vcontrib, hcontrib;
  119. hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
  120. [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
  121. vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
  122. [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
  123. *xorbit = xorbittab[hcontrib][vcontrib];
  124. return ctxlbltab[hcontrib][vcontrib];
  125. }
  126. void ff_jpeg2000_init_tier1_luts(void)
  127. {
  128. int i, j;
  129. for (i = 0; i < 256; i++)
  130. for (j = 0; j < 4; j++)
  131. ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
  132. for (i = 0; i < 16; i++)
  133. for (j = 0; j < 16; j++)
  134. ff_jpeg2000_sgnctxno_lut[i][j] =
  135. getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
  136. }
  137. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
  138. int negative)
  139. {
  140. x++;
  141. y++;
  142. t1->flags[y][x] |= JPEG2000_T1_SIG;
  143. if (negative) {
  144. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
  145. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
  146. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
  147. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
  148. } else {
  149. t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
  150. t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
  151. t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
  152. t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
  153. }
  154. t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
  155. t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
  156. t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
  157. t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
  158. }
  159. static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
  160. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  161. Jpeg2000CodingStyle *codsty,
  162. Jpeg2000QuantStyle *qntsty,
  163. int cbps, int dx, int dy,
  164. AVCodecContext *avctx)
  165. {
  166. uint8_t log2_band_prec_width, log2_band_prec_height;
  167. int reslevelno, bandno, gbandno = 0, ret, i, j;
  168. uint32_t csize = 1;
  169. if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
  170. codsty->nreslevels2decode - 1,
  171. codsty->transform))
  172. return ret;
  173. // component size comp->coord is uint16_t so ir cannot overflow
  174. csize = (comp->coord[0][1] - comp->coord[0][0]) *
  175. (comp->coord[1][1] - comp->coord[1][0]);
  176. if (codsty->transform == FF_DWT97) {
  177. comp->i_data = NULL;
  178. comp->f_data = av_malloc_array(csize, sizeof(*comp->f_data));
  179. if (!comp->f_data)
  180. return AVERROR(ENOMEM);
  181. } else {
  182. comp->f_data = NULL;
  183. comp->i_data = av_malloc_array(csize, sizeof(*comp->i_data));
  184. if (!comp->i_data)
  185. return AVERROR(ENOMEM);
  186. }
  187. comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
  188. if (!comp->reslevel)
  189. return AVERROR(ENOMEM);
  190. /* LOOP on resolution levels */
  191. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  192. int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
  193. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  194. /* Compute borders for each resolution level.
  195. * Computation of trx_0, trx_1, try_0 and try_1.
  196. * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
  197. for (i = 0; i < 2; i++)
  198. for (j = 0; j < 2; j++)
  199. reslevel->coord[i][j] =
  200. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
  201. // update precincts size: 2^n value
  202. reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
  203. reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
  204. /* Number of bands for each resolution level */
  205. if (reslevelno == 0)
  206. reslevel->nbands = 1;
  207. else
  208. reslevel->nbands = 3;
  209. /* Number of precincts wich span the tile for resolution level reslevelno
  210. * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
  211. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
  212. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
  213. * for Dcinema profiles in JPEG 2000
  214. * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
  215. * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
  216. if (reslevel->coord[0][1] == reslevel->coord[0][0])
  217. reslevel->num_precincts_x = 0;
  218. else
  219. reslevel->num_precincts_x =
  220. ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
  221. reslevel->log2_prec_width) -
  222. (reslevel->coord[0][0] >> reslevel->log2_prec_width);
  223. if (reslevel->coord[1][1] == reslevel->coord[1][0])
  224. reslevel->num_precincts_y = 0;
  225. else
  226. reslevel->num_precincts_y =
  227. ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
  228. reslevel->log2_prec_height) -
  229. (reslevel->coord[1][0] >> reslevel->log2_prec_height);
  230. reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
  231. if (!reslevel->band)
  232. return AVERROR(ENOMEM);
  233. for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
  234. Jpeg2000Band *band = reslevel->band + bandno;
  235. int cblkno, precno;
  236. int nb_precincts;
  237. /* TODO: Implementation of quantization step not finished,
  238. * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
  239. switch (qntsty->quantsty) {
  240. uint8_t gain;
  241. int numbps;
  242. case JPEG2000_QSTY_NONE:
  243. /* TODO: to verify. No quantization in this case */
  244. band->f_stepsize = (float) (1 << 13);
  245. break;
  246. case JPEG2000_QSTY_SI:
  247. /*TODO: Compute formula to implement. */
  248. numbps = cbps +
  249. lut_gain[codsty->transform][bandno + (reslevelno > 0)];
  250. band->f_stepsize = (float)SHL(2048 + qntsty->mant[gbandno],
  251. 2 + numbps - qntsty->expn[gbandno]);
  252. break;
  253. case JPEG2000_QSTY_SE:
  254. /* Exponent quantization step.
  255. * Formula:
  256. * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
  257. * R_b = R_I + log2 (gain_b )
  258. * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
  259. /* TODO/WARN: value of log2 (gain_b ) not taken into account
  260. * but it works (compared to OpenJPEG). Why?
  261. * Further investigation needed. */
  262. gain = cbps;
  263. band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
  264. band->f_stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0;
  265. break;
  266. default:
  267. band->f_stepsize = 0;
  268. av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
  269. break;
  270. }
  271. /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
  272. * If not set output of entropic decoder is not correct. */
  273. if (!av_codec_is_encoder(avctx->codec))
  274. band->f_stepsize *= 0.5;
  275. band->i_stepsize = (int32_t)(band->f_stepsize * (1 << 16));
  276. /* computation of tbx_0, tbx_1, tby_0, tby_1
  277. * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
  278. * codeblock width and height is computed for
  279. * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
  280. if (reslevelno == 0) {
  281. /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
  282. for (i = 0; i < 2; i++)
  283. for (j = 0; j < 2; j++)
  284. band->coord[i][j] =
  285. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
  286. declvl - 1);
  287. log2_band_prec_width = reslevel->log2_prec_width;
  288. log2_band_prec_height = reslevel->log2_prec_height;
  289. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  290. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  291. reslevel->log2_prec_width);
  292. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  293. reslevel->log2_prec_height);
  294. } else {
  295. /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
  296. /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
  297. for (i = 0; i < 2; i++)
  298. for (j = 0; j < 2; j++)
  299. /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
  300. band->coord[i][j] =
  301. ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
  302. (((bandno + 1 >> i) & 1) << declvl - 1),
  303. declvl);
  304. /* TODO: Manage case of 3 band offsets here or
  305. * in coding/decoding function? */
  306. /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
  307. band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
  308. reslevel->log2_prec_width - 1);
  309. band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
  310. reslevel->log2_prec_height - 1);
  311. log2_band_prec_width = reslevel->log2_prec_width - 1;
  312. log2_band_prec_height = reslevel->log2_prec_height - 1;
  313. }
  314. band->prec = av_malloc_array(reslevel->num_precincts_x *
  315. reslevel->num_precincts_y,
  316. sizeof(*band->prec));
  317. if (!band->prec)
  318. return AVERROR(ENOMEM);
  319. nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
  320. for (precno = 0; precno < nb_precincts; precno++) {
  321. Jpeg2000Prec *prec = band->prec + precno;
  322. /* TODO: Explain formula for JPEG200 DCINEMA. */
  323. /* TODO: Verify with previous count of codeblocks per band */
  324. /* Compute P_x0 */
  325. prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
  326. (1 << log2_band_prec_width);
  327. prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
  328. /* Compute P_y0 */
  329. prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
  330. (1 << log2_band_prec_height);
  331. prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
  332. /* Compute P_x1 */
  333. prec->coord[0][1] = prec->coord[0][0] +
  334. (1 << log2_band_prec_width);
  335. prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
  336. /* Compute P_y1 */
  337. prec->coord[1][1] = prec->coord[1][0] +
  338. (1 << log2_band_prec_height);
  339. prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
  340. prec->nb_codeblocks_width =
  341. ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
  342. prec->coord[0][0],
  343. band->log2_cblk_width);
  344. prec->nb_codeblocks_height =
  345. ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
  346. prec->coord[1][0],
  347. band->log2_cblk_height);
  348. /* Tag trees initialization */
  349. prec->cblkincl =
  350. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  351. prec->nb_codeblocks_height);
  352. if (!prec->cblkincl)
  353. return AVERROR(ENOMEM);
  354. prec->zerobits =
  355. ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
  356. prec->nb_codeblocks_height);
  357. if (!prec->zerobits)
  358. return AVERROR(ENOMEM);
  359. prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
  360. prec->nb_codeblocks_height,
  361. sizeof(*prec->cblk));
  362. if (!prec->cblk)
  363. return AVERROR(ENOMEM);
  364. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  365. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  366. uint16_t Cx0, Cy0;
  367. /* Compute coordinates of codeblocks */
  368. /* Compute Cx0*/
  369. Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
  370. Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
  371. cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
  372. /* Compute Cy0*/
  373. Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
  374. Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
  375. cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
  376. /* Compute Cx1 */
  377. cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
  378. prec->coord[0][1]);
  379. /* Compute Cy1 */
  380. cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
  381. prec->coord[1][1]);
  382. if((bandno + !!reslevelno) & 1) {
  383. cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  384. cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  385. }
  386. if((bandno + !!reslevelno) & 2) {
  387. cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  388. cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  389. }
  390. cblk->zero = 0;
  391. cblk->lblock = 3;
  392. cblk->length = 0;
  393. cblk->lengthinc = 0;
  394. cblk->npasses = 0;
  395. }
  396. }
  397. }
  398. }
  399. return 0;
  400. }
  401. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
  402. {
  403. int reslevelno, bandno, precno;
  404. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  405. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  406. for (bandno = 0; bandno < reslevel->nbands; bandno++) {
  407. Jpeg2000Band *band = reslevel->band + bandno;
  408. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
  409. Jpeg2000Prec *prec = band->prec + precno;
  410. av_freep(&prec->zerobits);
  411. av_freep(&prec->cblkincl);
  412. av_freep(&prec->cblk);
  413. }
  414. av_freep(&band->prec);
  415. }
  416. av_freep(&reslevel->band);
  417. }
  418. ff_dwt_destroy(&comp->dwt);
  419. av_freep(&comp->reslevel);
  420. av_freep(&comp->i_data);
  421. av_freep(&comp->f_data);
  422. }