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.

1052 lines
37KB

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