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.

1046 lines
36KB

  1. /*
  2. * JPEG 2000 image decoder
  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. * JPEG2000 image decoder
  24. * @file
  25. * @author Kamil Nowosad
  26. */
  27. // #define DEBUG
  28. #include "avcodec.h"
  29. #include "bytestream.h"
  30. #include "internal.h"
  31. #include "thread.h"
  32. #include "j2k.h"
  33. #include "libavutil/common.h"
  34. #define JP2_SIG_TYPE 0x6A502020
  35. #define JP2_SIG_VALUE 0x0D0A870A
  36. #define JP2_CODESTREAM 0x6A703263
  37. #define HAD_COC 0x01
  38. #define HAD_QCC 0x02
  39. typedef struct {
  40. Jpeg2000Component *comp;
  41. uint8_t properties[4];
  42. Jpeg2000CodingStyle codsty[4];
  43. Jpeg2000QuantStyle qntsty[4];
  44. } Jpeg2000Tile;
  45. typedef struct Jpeg2000DecoderContext {
  46. AVCodecContext *avctx;
  47. AVFrame *picture;
  48. GetByteContext g;
  49. int width, height;
  50. int image_offset_x, image_offset_y;
  51. int tile_offset_x, tile_offset_y;
  52. uint8_t cbps[4]; // bits per sample in particular components
  53. uint8_t sgnd[4]; // if a component is signed
  54. uint8_t properties[4];
  55. int cdx[4], cdy[4];
  56. int precision;
  57. int ncomponents;
  58. int tile_width, tile_height;
  59. int numXtiles, numYtiles;
  60. int maxtilelen;
  61. Jpeg2000CodingStyle codsty[4];
  62. Jpeg2000QuantStyle qntsty[4];
  63. int bit_index;
  64. int curtileno;
  65. Jpeg2000Tile *tile;
  66. } Jpeg2000DecoderContext;
  67. static int get_bits(Jpeg2000DecoderContext *s, int n)
  68. {
  69. int res = 0;
  70. while (--n >= 0) {
  71. res <<= 1;
  72. if (s->bit_index == 0) {
  73. s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
  74. }
  75. s->bit_index--;
  76. res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
  77. }
  78. return res;
  79. }
  80. static void j2k_flush(Jpeg2000DecoderContext *s)
  81. {
  82. if (bytestream2_get_byte(&s->g) == 0xff)
  83. bytestream2_skip(&s->g, 1);
  84. s->bit_index = 8;
  85. }
  86. /** decode the value stored in node */
  87. static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
  88. {
  89. Jpeg2000TgtNode *stack[30];
  90. int sp = -1, curval = 0;
  91. if (!node)
  92. return AVERROR(EINVAL);
  93. while (node && !node->vis) {
  94. stack[++sp] = node;
  95. node = node->parent;
  96. }
  97. if (node)
  98. curval = node->val;
  99. else
  100. curval = stack[sp]->val;
  101. while (curval < threshold && sp >= 0) {
  102. if (curval < stack[sp]->val)
  103. curval = stack[sp]->val;
  104. while (curval < threshold) {
  105. int ret;
  106. if ((ret = get_bits(s, 1)) > 0) {
  107. stack[sp]->vis++;
  108. break;
  109. } else if (!ret)
  110. curval++;
  111. else
  112. return ret;
  113. }
  114. stack[sp]->val = curval;
  115. sp--;
  116. }
  117. return curval;
  118. }
  119. /* marker segments */
  120. /* get sizes and offsets of image, tiles; number of components */
  121. static int get_siz(Jpeg2000DecoderContext *s)
  122. {
  123. int i, ret;
  124. ThreadFrame frame = { .f = s->picture };
  125. if (bytestream2_get_bytes_left(&s->g) < 36)
  126. return AVERROR(EINVAL);
  127. s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
  128. s->width = bytestream2_get_be32u(&s->g); // Width
  129. s->height = bytestream2_get_be32u(&s->g); // Height
  130. s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
  131. s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
  132. s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
  133. s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
  134. s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
  135. s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
  136. s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
  137. if (s->ncomponents <= 0 || s->ncomponents > 4) {
  138. av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
  139. return AVERROR(EINVAL);
  140. }
  141. if (s->tile_width<=0 || s->tile_height<=0)
  142. return AVERROR(EINVAL);
  143. if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
  144. return AVERROR(EINVAL);
  145. for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
  146. uint8_t x = bytestream2_get_byteu(&s->g);
  147. s->cbps[i] = (x & 0x7f) + 1;
  148. s->precision = FFMAX(s->cbps[i], s->precision);
  149. s->sgnd[i] = !!(x & 0x80);
  150. s->cdx[i] = bytestream2_get_byteu(&s->g);
  151. s->cdy[i] = bytestream2_get_byteu(&s->g);
  152. }
  153. s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
  154. s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
  155. if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
  156. return AVERROR(EINVAL);
  157. s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(Jpeg2000Tile));
  158. if (!s->tile)
  159. return AVERROR(ENOMEM);
  160. for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
  161. Jpeg2000Tile *tile = s->tile + i;
  162. tile->comp = av_mallocz(s->ncomponents * sizeof(Jpeg2000Component));
  163. if (!tile->comp)
  164. return AVERROR(ENOMEM);
  165. }
  166. s->avctx->width = s->width - s->image_offset_x;
  167. s->avctx->height = s->height - s->image_offset_y;
  168. switch(s->ncomponents) {
  169. case 1:
  170. if (s->precision > 8) {
  171. s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  172. } else {
  173. s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  174. }
  175. break;
  176. case 3:
  177. if (s->precision > 8) {
  178. s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
  179. } else {
  180. s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
  181. }
  182. break;
  183. case 4:
  184. s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
  185. break;
  186. }
  187. if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
  188. return ret;
  189. s->picture->pict_type = AV_PICTURE_TYPE_I;
  190. s->picture->key_frame = 1;
  191. return 0;
  192. }
  193. /** get common part for COD and COC segments */
  194. static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
  195. {
  196. if (bytestream2_get_bytes_left(&s->g) < 5)
  197. return AVERROR(EINVAL);
  198. c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
  199. c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
  200. c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
  201. c->cblk_style = bytestream2_get_byteu(&s->g);
  202. if (c->cblk_style != 0) { // cblk style
  203. av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
  204. }
  205. c->transform = bytestream2_get_byteu(&s->g); // transformation
  206. if (c->csty & JPEG2000_CSTY_PREC) {
  207. int i;
  208. for (i = 0; i < c->nreslevels; i++)
  209. bytestream2_get_byte(&s->g);
  210. }
  211. return 0;
  212. }
  213. /** get coding parameters for a particular tile or whole image*/
  214. static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
  215. {
  216. Jpeg2000CodingStyle tmp;
  217. int compno;
  218. if (bytestream2_get_bytes_left(&s->g) < 5)
  219. return AVERROR(EINVAL);
  220. tmp.log2_prec_width =
  221. tmp.log2_prec_height = 15;
  222. tmp.csty = bytestream2_get_byteu(&s->g);
  223. // get progression order
  224. tmp.prog_order = bytestream2_get_byteu(&s->g);
  225. if (tmp.prog_order) {
  226. av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
  227. }
  228. tmp.nlayers = bytestream2_get_be16u(&s->g);
  229. tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
  230. get_cox(s, &tmp);
  231. for (compno = 0; compno < s->ncomponents; compno++) {
  232. if (!(properties[compno] & HAD_COC))
  233. memcpy(c + compno, &tmp, sizeof(Jpeg2000CodingStyle));
  234. }
  235. return 0;
  236. }
  237. /** get coding parameters for a component in the whole image on a particular tile */
  238. static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
  239. {
  240. int compno;
  241. if (bytestream2_get_bytes_left(&s->g) < 2)
  242. return AVERROR(EINVAL);
  243. compno = bytestream2_get_byteu(&s->g);
  244. c += compno;
  245. c->csty = bytestream2_get_byte(&s->g);
  246. get_cox(s, c);
  247. properties[compno] |= HAD_COC;
  248. return 0;
  249. }
  250. /** get common part for QCD and QCC segments */
  251. static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
  252. {
  253. int i, x;
  254. if (bytestream2_get_bytes_left(&s->g) < 1)
  255. return AVERROR(EINVAL);
  256. x = bytestream2_get_byteu(&s->g); // Sqcd
  257. q->nguardbits = x >> 5;
  258. q->quantsty = x & 0x1f;
  259. if (q->quantsty == JPEG2000_QSTY_NONE) {
  260. n -= 3;
  261. if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
  262. return AVERROR(EINVAL);
  263. for (i = 0; i < n; i++)
  264. q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
  265. } else if (q->quantsty == JPEG2000_QSTY_SI) {
  266. if (bytestream2_get_bytes_left(&s->g) < 2)
  267. return AVERROR(EINVAL);
  268. x = bytestream2_get_be16u(&s->g);
  269. q->expn[0] = x >> 11;
  270. q->mant[0] = x & 0x7ff;
  271. for (i = 1; i < 32 * 3; i++) {
  272. int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
  273. q->expn[i] = curexpn;
  274. q->mant[i] = q->mant[0];
  275. }
  276. } else{
  277. n = (n - 3) >> 1;
  278. if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
  279. return AVERROR(EINVAL);
  280. for (i = 0; i < n; i++) {
  281. x = bytestream2_get_be16u(&s->g);
  282. q->expn[i] = x >> 11;
  283. q->mant[i] = x & 0x7ff;
  284. }
  285. }
  286. return 0;
  287. }
  288. /** get quantization parameters for a particular tile or a whole image */
  289. static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
  290. {
  291. Jpeg2000QuantStyle tmp;
  292. int compno;
  293. if (get_qcx(s, n, &tmp))
  294. return -1;
  295. for (compno = 0; compno < s->ncomponents; compno++)
  296. if (!(properties[compno] & HAD_QCC))
  297. memcpy(q + compno, &tmp, sizeof(Jpeg2000QuantStyle));
  298. return 0;
  299. }
  300. /** get quantization parameters for a component in the whole image on in a particular tile */
  301. static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
  302. {
  303. int compno;
  304. if (bytestream2_get_bytes_left(&s->g) < 1)
  305. return AVERROR(EINVAL);
  306. compno = bytestream2_get_byteu(&s->g);
  307. properties[compno] |= HAD_QCC;
  308. return get_qcx(s, n-1, q+compno);
  309. }
  310. /** get start of tile segment */
  311. static int get_sot(Jpeg2000DecoderContext *s)
  312. {
  313. if (bytestream2_get_bytes_left(&s->g) < 8)
  314. return AVERROR(EINVAL);
  315. s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
  316. if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
  317. s->curtileno=0;
  318. return AVERROR(EINVAL);
  319. }
  320. bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
  321. if (!bytestream2_get_byteu(&s->g)) { ///< TPsot
  322. Jpeg2000Tile *tile = s->tile + s->curtileno;
  323. /* copy defaults */
  324. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
  325. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
  326. }
  327. bytestream2_get_byteu(&s->g); ///< TNsot
  328. return 0;
  329. }
  330. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  331. {
  332. int compno,
  333. tilex = tileno % s->numXtiles,
  334. tiley = tileno / s->numXtiles;
  335. Jpeg2000Tile *tile = s->tile + tileno;
  336. if (!tile->comp)
  337. return AVERROR(ENOMEM);
  338. for (compno = 0; compno < s->ncomponents; compno++) {
  339. Jpeg2000Component *comp = tile->comp + compno;
  340. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  341. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  342. int ret; // global bandno
  343. comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
  344. comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
  345. comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
  346. comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
  347. if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. /** read the number of coding passes */
  353. static int getnpasses(Jpeg2000DecoderContext *s)
  354. {
  355. int num;
  356. if (!get_bits(s, 1))
  357. return 1;
  358. if (!get_bits(s, 1))
  359. return 2;
  360. if ((num = get_bits(s, 2)) != 3)
  361. return num < 0 ? num : 3 + num;
  362. if ((num = get_bits(s, 5)) != 31)
  363. return num < 0 ? num : 6 + num;
  364. num = get_bits(s, 7);
  365. return num < 0 ? num : 37 + num;
  366. }
  367. static int getlblockinc(Jpeg2000DecoderContext *s)
  368. {
  369. int res = 0, ret;
  370. while (ret = get_bits(s, 1)) {
  371. if (ret < 0)
  372. return ret;
  373. res++;
  374. }
  375. return res;
  376. }
  377. static int decode_packet(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno,
  378. int layno, uint8_t *expn, int numgbits)
  379. {
  380. int bandno, cblkny, cblknx, cblkno, ret;
  381. if (!(ret = get_bits(s, 1))) {
  382. j2k_flush(s);
  383. return 0;
  384. } else if (ret < 0)
  385. return ret;
  386. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  387. Jpeg2000Band *band = rlevel->band + bandno;
  388. Jpeg2000Prec *prec = band->prec + precno;
  389. int pos = 0;
  390. if (band->coord[0][0] == band->coord[0][1]
  391. || band->coord[1][0] == band->coord[1][1])
  392. continue;
  393. for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
  394. for (cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++) {
  395. Jpeg2000Cblk *cblk = band->cblk + cblkno;
  396. int incl, newpasses, llen;
  397. if (cblk->npasses)
  398. incl = get_bits(s, 1);
  399. else
  400. incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
  401. if (!incl)
  402. continue;
  403. else if (incl < 0)
  404. return incl;
  405. if (!cblk->npasses)
  406. cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
  407. if ((newpasses = getnpasses(s)) < 0)
  408. return newpasses;
  409. if ((llen = getlblockinc(s)) < 0)
  410. return llen;
  411. cblk->lblock += llen;
  412. if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
  413. return ret;
  414. cblk->lengthinc = ret;
  415. cblk->npasses += newpasses;
  416. }
  417. }
  418. j2k_flush(s);
  419. if (codsty->csty & JPEG2000_CSTY_EPH) {
  420. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH) {
  421. bytestream2_skip(&s->g, 2);
  422. } else {
  423. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
  424. }
  425. }
  426. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  427. Jpeg2000Band *band = rlevel->band + bandno;
  428. int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
  429. for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++) {
  430. int xi;
  431. for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++) {
  432. Jpeg2000Cblk *cblk = band->cblk + yi * cblknw + xi;
  433. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
  434. || sizeof(cblk->data) < cblk->lengthinc
  435. )
  436. return AVERROR(EINVAL);
  437. bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
  438. cblk->length += cblk->lengthinc;
  439. cblk->lengthinc = 0;
  440. }
  441. }
  442. }
  443. return 0;
  444. }
  445. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  446. {
  447. int layno, reslevelno, compno, precno, ok_reslevel;
  448. s->bit_index = 8;
  449. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  450. ok_reslevel = 1;
  451. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  452. ok_reslevel = 0;
  453. for (compno = 0; compno < s->ncomponents; compno++) {
  454. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  455. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  456. if (reslevelno < codsty->nreslevels) {
  457. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  458. ok_reslevel = 1;
  459. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  460. if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
  461. (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
  462. return -1;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. return 0;
  469. }
  470. /* TIER-1 routines */
  471. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
  472. int vert_causal_ctx_csty_symbol)
  473. {
  474. int mask = 3 << (bpno - 1), y0, x, y;
  475. for (y0 = 0; y0 < height; y0 += 4)
  476. for (x = 0; x < width; x++)
  477. for (y = y0; y < height && y < y0+4; y++) {
  478. if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
  479. && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  480. int flags_mask = -1;
  481. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  482. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  483. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
  484. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  485. if (bpass_csty_symbol)
  486. t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  487. else
  488. t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  489. -mask : mask;
  490. ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
  491. }
  492. t1->flags[y+1][x+1] |= JPEG2000_T1_VIS;
  493. }
  494. }
  495. }
  496. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpno)
  497. {
  498. int phalf, nhalf;
  499. int y0, x, y;
  500. phalf = 1 << (bpno - 1);
  501. nhalf = -phalf;
  502. for (y0 = 0; y0 < height; y0 += 4)
  503. for (x = 0; x < width; x++)
  504. for (y = y0; y < height && y < y0+4; y++) {
  505. if ((t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  506. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y+1][x+1]);
  507. int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
  508. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  509. t1->flags[y+1][x+1] |= JPEG2000_T1_REF;
  510. }
  511. }
  512. }
  513. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height,
  514. int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
  515. {
  516. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  517. for (y0 = 0; y0 < height; y0 += 4) {
  518. for (x = 0; x < width; x++) {
  519. if (y0 + 3 < height && !(
  520. (t1->flags[y0+1][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  521. (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  522. (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  523. (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  524. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  525. continue;
  526. runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  527. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  528. dec = 1;
  529. } else{
  530. runlen = 0;
  531. dec = 0;
  532. }
  533. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  534. if (!dec) {
  535. if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  536. int flags_mask = -1;
  537. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  538. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  539. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
  540. bandno));
  541. }
  542. }
  543. if (dec) {
  544. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  545. t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
  546. ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
  547. }
  548. dec = 0;
  549. t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
  550. }
  551. }
  552. }
  553. if (seg_symbols) {
  554. int val;
  555. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  556. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  557. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  558. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  559. if (val != 0xa) {
  560. av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
  561. }
  562. }
  563. }
  564. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  565. int width, int height, int bandpos)
  566. {
  567. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
  568. int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
  569. int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
  570. for (y = 0; y < height+2; y++)
  571. memset(t1->flags[y], 0, (width+2)*sizeof(int));
  572. for (y = 0; y < height; y++)
  573. memset(t1->data[y], 0, width*sizeof(int));
  574. cblk->data[cblk->length] = 0xff;
  575. cblk->data[cblk->length+1] = 0xff;
  576. ff_mqc_initdec(&t1->mqc, cblk->data);
  577. while (passno--) {
  578. switch(pass_t) {
  579. case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
  580. bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
  581. break;
  582. case 1: decode_refpass(t1, width, height, bpno+1);
  583. if (bpass_csty_symbol && clnpass_cnt >= 4)
  584. ff_mqc_initdec(&t1->mqc, cblk->data);
  585. break;
  586. case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
  587. codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
  588. clnpass_cnt = clnpass_cnt + 1;
  589. if (bpass_csty_symbol && clnpass_cnt >= 4)
  590. ff_mqc_initdec(&t1->mqc, cblk->data);
  591. break;
  592. }
  593. pass_t++;
  594. if (pass_t == 3) {
  595. bpno--;
  596. pass_t = 0;
  597. }
  598. }
  599. return 0;
  600. }
  601. static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  602. {
  603. int i, *src[3], i0, i1, i2, csize = 1;
  604. for (i = 0; i < 3; i++)
  605. src[i] = tile->comp[i].data;
  606. for (i = 0; i < 2; i++)
  607. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  608. if (tile->codsty[0].transform == FF_DWT97) {
  609. for (i = 0; i < csize; i++) {
  610. i0 = *src[0] + (*src[2] * 46802 >> 16);
  611. i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
  612. i2 = *src[0] + (116130 * *src[1] >> 16);
  613. *src[0]++ = i0;
  614. *src[1]++ = i1;
  615. *src[2]++ = i2;
  616. }
  617. } else{
  618. for (i = 0; i < csize; i++) {
  619. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  620. i0 = i1 + *src[2];
  621. i2 = i1 + *src[1];
  622. *src[0]++ = i0;
  623. *src[1]++ = i1;
  624. *src[2]++ = i2;
  625. }
  626. }
  627. }
  628. static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  629. {
  630. int compno, reslevelno, bandno;
  631. int x, y, *src[4];
  632. uint8_t *line;
  633. Jpeg2000T1Context t1;
  634. for (compno = 0; compno < s->ncomponents; compno++) {
  635. Jpeg2000Component *comp = tile->comp + compno;
  636. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  637. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  638. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  639. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  640. Jpeg2000Band *band = rlevel->band + bandno;
  641. int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
  642. bandpos = bandno + (reslevelno > 0);
  643. yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  644. y0 = yy0;
  645. yy1 = FFMIN(ff_jpeg2000_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
  646. band->coord[1][1]) - band->coord[1][0] + yy0;
  647. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  648. continue;
  649. for (cblky = 0; cblky < band->cblkny; cblky++) {
  650. if (reslevelno == 0 || bandno == 1)
  651. xx0 = 0;
  652. else
  653. xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  654. x0 = xx0;
  655. xx1 = FFMIN(ff_jpeg2000_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
  656. band->coord[0][1]) - band->coord[0][0] + xx0;
  657. for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++) {
  658. int y, x;
  659. decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
  660. if (codsty->transform == FF_DWT53) {
  661. for (y = yy0; y < yy1; y+=s->cdy[compno]) {
  662. int *ptr = t1.data[y-yy0];
  663. for (x = xx0; x < xx1; x+=s->cdx[compno]) {
  664. comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
  665. }
  666. }
  667. } else{
  668. for (y = yy0; y < yy1; y+=s->cdy[compno]) {
  669. int *ptr = t1.data[y-yy0];
  670. for (x = xx0; x < xx1; x+=s->cdx[compno]) {
  671. int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
  672. tmp2 = FFABS(tmp>>1) + (tmp&1);
  673. comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
  674. }
  675. }
  676. }
  677. xx0 = xx1;
  678. xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
  679. }
  680. yy0 = yy1;
  681. yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
  682. }
  683. }
  684. }
  685. ff_j2k_dwt_decode(&comp->dwt, comp->data);
  686. src[compno] = comp->data;
  687. }
  688. if (tile->codsty[0].mct)
  689. mct_decode(s, tile);
  690. if (s->precision <= 8) {
  691. for (compno = 0; compno < s->ncomponents; compno++) {
  692. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  693. line = s->picture->data[0] + y * s->picture->linesize[0];
  694. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  695. uint8_t *dst;
  696. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  697. dst = line + x * s->ncomponents + compno;
  698. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
  699. *src[compno] += 1 << (s->cbps[compno]-1);
  700. if (*src[compno] < 0)
  701. *src[compno] = 0;
  702. else if (*src[compno] >= (1 << s->cbps[compno]))
  703. *src[compno] = (1 << s->cbps[compno]) - 1;
  704. *dst = *src[compno]++;
  705. dst += s->ncomponents;
  706. }
  707. line += s->picture->linesize[0];
  708. }
  709. }
  710. } else {
  711. for (compno = 0; compno < s->ncomponents; compno++) {
  712. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  713. line = s->picture->data[0] + y * s->picture->linesize[0];
  714. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  715. uint16_t *dst;
  716. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  717. dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
  718. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
  719. int32_t val;
  720. val = *src[compno]++ << (16 - s->cbps[compno]);
  721. val += 1 << 15;
  722. val = av_clip(val, 0, (1 << 16) - 1);
  723. *dst = val;
  724. dst += s->ncomponents;
  725. }
  726. line += s->picture->linesize[0];
  727. }
  728. }
  729. }
  730. return 0;
  731. }
  732. static void cleanup(Jpeg2000DecoderContext *s)
  733. {
  734. int tileno, compno;
  735. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  736. for (compno = 0; compno < s->ncomponents; compno++) {
  737. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  738. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  739. ff_j2k_cleanup(comp, codsty);
  740. }
  741. av_freep(&s->tile[tileno].comp);
  742. }
  743. av_freep(&s->tile);
  744. }
  745. static int decode_codestream(Jpeg2000DecoderContext *s)
  746. {
  747. Jpeg2000CodingStyle *codsty = s->codsty;
  748. Jpeg2000QuantStyle *qntsty = s->qntsty;
  749. uint8_t *properties = s->properties;
  750. for (;;) {
  751. int oldpos, marker, len, ret = 0;
  752. if (bytestream2_get_bytes_left(&s->g) < 2) {
  753. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  754. break;
  755. }
  756. marker = bytestream2_get_be16u(&s->g);
  757. av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
  758. oldpos = bytestream2_tell(&s->g);
  759. if (marker == JPEG2000_SOD) {
  760. Jpeg2000Tile *tile = s->tile + s->curtileno;
  761. if (ret = init_tile(s, s->curtileno)) {
  762. av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
  763. return ret;
  764. }
  765. if (ret = jpeg2000_decode_packets(s, tile)) {
  766. av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
  767. return ret;
  768. }
  769. continue;
  770. }
  771. if (marker == JPEG2000_EOC)
  772. break;
  773. if (bytestream2_get_bytes_left(&s->g) < 2)
  774. return AVERROR(EINVAL);
  775. len = bytestream2_get_be16u(&s->g);
  776. switch (marker) {
  777. case JPEG2000_SIZ:
  778. ret = get_siz(s);
  779. if (!s->tile)
  780. s->numXtiles = s->numYtiles = 0;
  781. break;
  782. case JPEG2000_COC:
  783. ret = get_coc(s, codsty, properties);
  784. break;
  785. case JPEG2000_COD:
  786. ret = get_cod(s, codsty, properties);
  787. break;
  788. case JPEG2000_QCC:
  789. ret = get_qcc(s, len, qntsty, properties);
  790. break;
  791. case JPEG2000_QCD:
  792. ret = get_qcd(s, len, qntsty, properties);
  793. break;
  794. case JPEG2000_SOT:
  795. if (!(ret = get_sot(s))) {
  796. codsty = s->tile[s->curtileno].codsty;
  797. qntsty = s->tile[s->curtileno].qntsty;
  798. properties = s->tile[s->curtileno].properties;
  799. }
  800. break;
  801. case JPEG2000_COM:
  802. // the comment is ignored
  803. bytestream2_skip(&s->g, len - 2);
  804. break;
  805. default:
  806. av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
  807. bytestream2_skip(&s->g, len - 2);
  808. break;
  809. }
  810. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  811. av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
  812. return ret ? ret : -1;
  813. }
  814. }
  815. return 0;
  816. }
  817. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  818. {
  819. uint32_t atom_size, atom;
  820. int found_codestream = 0, search_range = 10;
  821. while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
  822. atom_size = bytestream2_get_be32u(&s->g);
  823. atom = bytestream2_get_be32u(&s->g);
  824. if (atom == JP2_CODESTREAM) {
  825. found_codestream = 1;
  826. } else {
  827. if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
  828. return 0;
  829. bytestream2_skipu(&s->g, atom_size - 8);
  830. search_range--;
  831. }
  832. }
  833. if (found_codestream)
  834. return 1;
  835. return 0;
  836. }
  837. static int decode_frame(AVCodecContext *avctx,
  838. void *data, int *got_frame,
  839. AVPacket *avpkt)
  840. {
  841. Jpeg2000DecoderContext *s = avctx->priv_data;
  842. AVFrame *picture = data;
  843. int tileno, ret;
  844. s->picture = picture;
  845. s->avctx = avctx;
  846. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  847. s->curtileno = -1;
  848. if (bytestream2_get_bytes_left(&s->g) < 2) {
  849. ret = AVERROR(EINVAL);
  850. goto err_out;
  851. }
  852. // check if the image is in jp2 format
  853. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  854. (bytestream2_get_be32u(&s->g) == 12) &&
  855. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  856. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  857. if (!jp2_find_codestream(s)) {
  858. av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
  859. ret = -1;
  860. goto err_out;
  861. }
  862. } else {
  863. bytestream2_seek(&s->g, 0, SEEK_SET);
  864. }
  865. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  866. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  867. ret = -1;
  868. goto err_out;
  869. }
  870. if (ret = decode_codestream(s))
  871. goto err_out;
  872. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  873. if (ret = decode_tile(s, s->tile + tileno))
  874. goto err_out;
  875. cleanup(s);
  876. *got_frame = 1;
  877. return bytestream2_tell(&s->g);
  878. err_out:
  879. cleanup(s);
  880. return ret;
  881. }
  882. static void jpeg2000_init_static_data(AVCodec *codec)
  883. {
  884. ff_jpeg2000_init_tier1_luts();
  885. }
  886. static const AVProfile profiles[] = {
  887. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  888. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  889. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  890. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  891. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  892. { FF_PROFILE_UNKNOWN },
  893. };
  894. AVCodec ff_j2k_decoder = {
  895. .name = "j2k",
  896. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  897. .type = AVMEDIA_TYPE_VIDEO,
  898. .id = AV_CODEC_ID_JPEG2000,
  899. .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
  900. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  901. .init_static_data = jpeg2000_init_static_data,
  902. .decode = decode_frame,
  903. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  904. };