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.

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