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.

1076 lines
38KB

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