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.

1089 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,
  398. Jpeg2000CodingStyle *codsty,
  399. Jpeg2000ResLevel *rlevel, int precno,
  400. int layno, uint8_t *expn, int numgbits)
  401. {
  402. int bandno, cblkno, ret, nb_code_blocks;
  403. if (!(ret = get_bits(s, 1))) {
  404. j2k_flush(s);
  405. return 0;
  406. } else if (ret < 0)
  407. return ret;
  408. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  409. Jpeg2000Band *band = rlevel->band + bandno;
  410. Jpeg2000Prec *prec = band->prec + precno;
  411. if (band->coord[0][0] == band->coord[0][1] ||
  412. band->coord[1][0] == band->coord[1][1])
  413. continue;
  414. nb_code_blocks = prec->nb_codeblocks_height *
  415. prec->nb_codeblocks_width;
  416. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  417. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  418. int incl, newpasses, llen;
  419. if (cblk->npasses)
  420. incl = get_bits(s, 1);
  421. else
  422. incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
  423. if (!incl)
  424. continue;
  425. else if (incl < 0)
  426. return incl;
  427. if (!cblk->npasses)
  428. cblk->nonzerobits = expn[bandno] + numgbits - 1 -
  429. tag_tree_decode(s, prec->zerobits + cblkno,
  430. 100);
  431. if ((newpasses = getnpasses(s)) < 0)
  432. return newpasses;
  433. if ((llen = getlblockinc(s)) < 0)
  434. return llen;
  435. cblk->lblock += llen;
  436. if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
  437. return ret;
  438. cblk->lengthinc = ret;
  439. cblk->npasses += newpasses;
  440. }
  441. }
  442. j2k_flush(s);
  443. if (codsty->csty & JPEG2000_CSTY_EPH) {
  444. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
  445. bytestream2_skip(&s->g, 2);
  446. else
  447. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
  448. }
  449. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  450. Jpeg2000Band *band = rlevel->band + bandno;
  451. Jpeg2000Prec *prec = band->prec + precno;
  452. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  453. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  454. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  455. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
  456. || sizeof(cblk->data) < cblk->lengthinc
  457. )
  458. return AVERROR(EINVAL);
  459. /* Code-block data can be empty. In that case initialize data
  460. * with 0xFFFF. */
  461. if (cblk->lengthinc > 0) {
  462. bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
  463. } else {
  464. cblk->data[0] = 0xFF;
  465. cblk->data[1] = 0xFF;
  466. }
  467. cblk->length += cblk->lengthinc;
  468. cblk->lengthinc = 0;
  469. }
  470. }
  471. return 0;
  472. }
  473. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  474. {
  475. int layno, reslevelno, compno, precno, ok_reslevel;
  476. s->bit_index = 8;
  477. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  478. ok_reslevel = 1;
  479. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  480. ok_reslevel = 0;
  481. for (compno = 0; compno < s->ncomponents; compno++) {
  482. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  483. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  484. if (reslevelno < codsty->nreslevels) {
  485. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  486. ok_reslevel = 1;
  487. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
  488. if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
  489. (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
  490. return -1;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. return 0;
  497. }
  498. /* TIER-1 routines */
  499. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
  500. int vert_causal_ctx_csty_symbol)
  501. {
  502. int mask = 3 << (bpno - 1), y0, x, y;
  503. for (y0 = 0; y0 < height; y0 += 4)
  504. for (x = 0; x < width; x++)
  505. for (y = y0; y < height && y < y0+4; y++) {
  506. if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
  507. && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  508. int flags_mask = -1;
  509. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  510. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  511. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
  512. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  513. if (bpass_csty_symbol)
  514. t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  515. else
  516. t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  517. -mask : mask;
  518. ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
  519. }
  520. t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
  521. }
  522. }
  523. }
  524. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  525. int bpno)
  526. {
  527. int phalf, nhalf;
  528. int y0, x, y;
  529. phalf = 1 << (bpno - 1);
  530. nhalf = -phalf;
  531. for (y0 = 0; y0 < height; y0 += 4)
  532. for (x = 0; x < width; x++)
  533. for (y = y0; y < height && y < y0 + 4; y++)
  534. if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  535. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
  536. int r = ff_mqc_decode(&t1->mqc,
  537. t1->mqc.cx_states + ctxno)
  538. ? phalf : nhalf;
  539. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  540. t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
  541. }
  542. }
  543. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height,
  544. int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
  545. {
  546. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  547. for (y0 = 0; y0 < height; y0 += 4) {
  548. for (x = 0; x < width; x++) {
  549. if (y0 + 3 < height &&
  550. !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  551. (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  552. (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  553. (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  554. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  555. continue;
  556. runlen = ff_mqc_decode(&t1->mqc,
  557. t1->mqc.cx_states + MQC_CX_UNI);
  558. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  559. t1->mqc.cx_states +
  560. MQC_CX_UNI);
  561. dec = 1;
  562. } else {
  563. runlen = 0;
  564. dec = 0;
  565. }
  566. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  567. if (!dec) {
  568. if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  569. int flags_mask = -1;
  570. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  571. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  572. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
  573. bandno));
  574. }
  575. }
  576. if (dec) {
  577. int xorbit;
  578. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  579. &xorbit);
  580. t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
  581. t1->mqc.cx_states + ctxno) ^
  582. xorbit)
  583. ? -mask : mask;
  584. ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
  585. }
  586. dec = 0;
  587. t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
  588. }
  589. }
  590. }
  591. if (seg_symbols) {
  592. int val;
  593. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  594. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  595. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  596. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  597. if (val != 0xa)
  598. av_log(s->avctx, AV_LOG_ERROR,
  599. "Segmentation symbol value incorrect\n");
  600. }
  601. }
  602. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  603. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  604. int width, int height, int bandpos)
  605. {
  606. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
  607. int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
  608. int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
  609. for (y = 0; y < height+2; y++)
  610. memset(t1->flags[y], 0, (width+2)*sizeof(int));
  611. for (y = 0; y < height; y++)
  612. memset(t1->data[y], 0, width*sizeof(int));
  613. cblk->data[cblk->length] = 0xff;
  614. cblk->data[cblk->length+1] = 0xff;
  615. ff_mqc_initdec(&t1->mqc, cblk->data);
  616. while (passno--) {
  617. switch(pass_t) {
  618. case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
  619. bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
  620. break;
  621. case 1: decode_refpass(t1, width, height, bpno+1);
  622. if (bpass_csty_symbol && clnpass_cnt >= 4)
  623. ff_mqc_initdec(&t1->mqc, cblk->data);
  624. break;
  625. case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
  626. codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
  627. clnpass_cnt = clnpass_cnt + 1;
  628. if (bpass_csty_symbol && clnpass_cnt >= 4)
  629. ff_mqc_initdec(&t1->mqc, cblk->data);
  630. break;
  631. }
  632. pass_t++;
  633. if (pass_t == 3) {
  634. bpno--;
  635. pass_t = 0;
  636. }
  637. }
  638. return 0;
  639. }
  640. static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  641. {
  642. int i, *src[3], i0, i1, i2, csize = 1;
  643. for (i = 0; i < 3; i++)
  644. src[i] = tile->comp[i].data;
  645. for (i = 0; i < 2; i++)
  646. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  647. if (tile->codsty[0].transform == FF_DWT97) {
  648. for (i = 0; i < csize; i++) {
  649. i0 = *src[0] + (*src[2] * 46802 >> 16);
  650. i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
  651. i2 = *src[0] + (116130 * *src[1] >> 16);
  652. *src[0]++ = i0;
  653. *src[1]++ = i1;
  654. *src[2]++ = i2;
  655. }
  656. } else{
  657. for (i = 0; i < csize; i++) {
  658. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  659. i0 = i1 + *src[2];
  660. i2 = i1 + *src[1];
  661. *src[0]++ = i0;
  662. *src[1]++ = i1;
  663. *src[2]++ = i2;
  664. }
  665. }
  666. }
  667. static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  668. {
  669. int compno, reslevelno, bandno;
  670. int x, y, *src[4];
  671. uint8_t *line;
  672. Jpeg2000T1Context t1;
  673. /* Loop on tile components */
  674. for (compno = 0; compno < s->ncomponents; compno++) {
  675. Jpeg2000Component *comp = tile->comp + compno;
  676. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  677. /* Loop on resolution levels */
  678. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  679. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  680. /* Loop on bands */
  681. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  682. Jpeg2000Band *band = rlevel->band + bandno;
  683. int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
  684. bandpos = bandno + (reslevelno > 0);
  685. yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  686. y0 = yy0;
  687. yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
  688. band->coord[1][1]) - band->coord[1][0] + yy0;
  689. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  690. continue;
  691. for (cblky = 0; cblky < band->cblkny; cblky++) {
  692. if (reslevelno == 0 || bandno == 1)
  693. xx0 = 0;
  694. else
  695. xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  696. x0 = xx0;
  697. xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
  698. band->coord[0][1]) - band->coord[0][0] + xx0;
  699. for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++) {
  700. int y, x;
  701. decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
  702. if (codsty->transform == FF_DWT53) {
  703. for (y = yy0; y < yy1; y+=s->cdy[compno]) {
  704. int *ptr = t1.data[y-yy0];
  705. for (x = xx0; x < xx1; x+=s->cdx[compno]) {
  706. comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
  707. }
  708. }
  709. } else{
  710. for (y = yy0; y < yy1; y+=s->cdy[compno]) {
  711. int *ptr = t1.data[y-yy0];
  712. for (x = xx0; x < xx1; x+=s->cdx[compno]) {
  713. int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
  714. tmp2 = FFABS(tmp>>1) + (tmp&1);
  715. comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
  716. }
  717. }
  718. }
  719. xx0 = xx1;
  720. xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
  721. }
  722. yy0 = yy1;
  723. yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
  724. }
  725. }
  726. }
  727. ff_j2k_dwt_decode(&comp->dwt, comp->data);
  728. src[compno] = comp->data;
  729. }
  730. /* inverse MCT transformation */
  731. if (tile->codsty[0].mct)
  732. mct_decode(s, tile);
  733. if (s->precision <= 8) {
  734. for (compno = 0; compno < s->ncomponents; compno++) {
  735. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  736. line = s->picture->data[0] + y * s->picture->linesize[0];
  737. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  738. uint8_t *dst;
  739. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  740. dst = line + x * s->ncomponents + compno;
  741. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
  742. *src[compno] += 1 << (s->cbps[compno]-1);
  743. if (*src[compno] < 0)
  744. *src[compno] = 0;
  745. else if (*src[compno] >= (1 << s->cbps[compno]))
  746. *src[compno] = (1 << s->cbps[compno]) - 1;
  747. *dst = *src[compno]++;
  748. dst += s->ncomponents;
  749. }
  750. line += s->picture->linesize[0];
  751. }
  752. }
  753. } else {
  754. for (compno = 0; compno < s->ncomponents; compno++) {
  755. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  756. line = s->picture->data[0] + y * s->picture->linesize[0];
  757. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  758. uint16_t *dst;
  759. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  760. dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
  761. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
  762. int32_t val;
  763. val = *src[compno]++ << (16 - s->cbps[compno]);
  764. val += 1 << 15;
  765. val = av_clip(val, 0, (1 << 16) - 1);
  766. *dst = val;
  767. dst += s->ncomponents;
  768. }
  769. line += s->picture->linesize[0];
  770. }
  771. }
  772. }
  773. return 0;
  774. }
  775. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  776. {
  777. int tileno, compno;
  778. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  779. for (compno = 0; compno < s->ncomponents; compno++) {
  780. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  781. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  782. ff_j2k_cleanup(comp, codsty);
  783. }
  784. av_freep(&s->tile[tileno].comp);
  785. }
  786. av_freep(&s->tile);
  787. }
  788. static int decode_codestream(Jpeg2000DecoderContext *s)
  789. {
  790. Jpeg2000CodingStyle *codsty = s->codsty;
  791. Jpeg2000QuantStyle *qntsty = s->qntsty;
  792. uint8_t *properties = s->properties;
  793. for (;;) {
  794. int oldpos, marker, len, ret = 0;
  795. if (bytestream2_get_bytes_left(&s->g) < 2) {
  796. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  797. break;
  798. }
  799. marker = bytestream2_get_be16u(&s->g);
  800. av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
  801. oldpos = bytestream2_tell(&s->g);
  802. if (marker == JPEG2000_SOD) {
  803. Jpeg2000Tile *tile = s->tile + s->curtileno;
  804. if (ret = init_tile(s, s->curtileno)) {
  805. av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
  806. return ret;
  807. }
  808. if (ret = jpeg2000_decode_packets(s, tile)) {
  809. av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
  810. return ret;
  811. }
  812. continue;
  813. }
  814. if (marker == JPEG2000_EOC)
  815. break;
  816. if (bytestream2_get_bytes_left(&s->g) < 2)
  817. return AVERROR(EINVAL);
  818. len = bytestream2_get_be16u(&s->g);
  819. switch (marker) {
  820. case JPEG2000_SIZ:
  821. ret = get_siz(s);
  822. if (!s->tile)
  823. s->numXtiles = s->numYtiles = 0;
  824. break;
  825. case JPEG2000_COC:
  826. ret = get_coc(s, codsty, properties);
  827. break;
  828. case JPEG2000_COD:
  829. ret = get_cod(s, codsty, properties);
  830. break;
  831. case JPEG2000_QCC:
  832. ret = get_qcc(s, len, qntsty, properties);
  833. break;
  834. case JPEG2000_QCD:
  835. ret = get_qcd(s, len, qntsty, properties);
  836. break;
  837. case JPEG2000_SOT:
  838. if (!(ret = get_sot(s))) {
  839. codsty = s->tile[s->curtileno].codsty;
  840. qntsty = s->tile[s->curtileno].qntsty;
  841. properties = s->tile[s->curtileno].properties;
  842. }
  843. break;
  844. case JPEG2000_COM:
  845. // the comment is ignored
  846. bytestream2_skip(&s->g, len - 2);
  847. break;
  848. default:
  849. av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
  850. bytestream2_skip(&s->g, len - 2);
  851. break;
  852. }
  853. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  854. av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
  855. return ret ? ret : -1;
  856. }
  857. }
  858. return 0;
  859. }
  860. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  861. {
  862. uint32_t atom_size, atom;
  863. int found_codestream = 0, search_range = 10;
  864. while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
  865. atom_size = bytestream2_get_be32u(&s->g);
  866. atom = bytestream2_get_be32u(&s->g);
  867. if (atom == JP2_CODESTREAM) {
  868. found_codestream = 1;
  869. } else {
  870. if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
  871. return 0;
  872. bytestream2_skipu(&s->g, atom_size - 8);
  873. search_range--;
  874. }
  875. }
  876. if (found_codestream)
  877. return 1;
  878. return 0;
  879. }
  880. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  881. int *got_frame, AVPacket *avpkt)
  882. {
  883. Jpeg2000DecoderContext *s = avctx->priv_data;
  884. AVFrame *picture = data;
  885. int tileno, ret;
  886. s->picture = picture;
  887. s->avctx = avctx;
  888. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  889. s->curtileno = -1;
  890. if (bytestream2_get_bytes_left(&s->g) < 2) {
  891. ret = AVERROR(EINVAL);
  892. goto err_out;
  893. }
  894. // check if the image is in jp2 format
  895. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  896. (bytestream2_get_be32u(&s->g) == 12) &&
  897. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  898. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  899. if (!jp2_find_codestream(s)) {
  900. av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
  901. ret = -1;
  902. goto err_out;
  903. }
  904. } else {
  905. bytestream2_seek(&s->g, 0, SEEK_SET);
  906. }
  907. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  908. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  909. ret = -1;
  910. goto err_out;
  911. }
  912. if (ret = decode_codestream(s))
  913. goto err_out;
  914. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  915. if (ret = decode_tile(s, s->tile + tileno))
  916. goto err_out;
  917. jpeg2000_dec_cleanup(s);
  918. *got_frame = 1;
  919. return bytestream2_tell(&s->g);
  920. err_out:
  921. jpeg2000_dec_cleanup(s);
  922. return ret;
  923. }
  924. static void jpeg2000_init_static_data(AVCodec *codec)
  925. {
  926. ff_jpeg2000_init_tier1_luts();
  927. }
  928. static const AVProfile profiles[] = {
  929. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  930. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  931. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  932. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  933. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  934. { FF_PROFILE_UNKNOWN },
  935. };
  936. AVCodec ff_j2k_decoder = {
  937. .name = "j2k",
  938. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  939. .type = AVMEDIA_TYPE_VIDEO,
  940. .id = AV_CODEC_ID_JPEG2000,
  941. .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
  942. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  943. .init_static_data = jpeg2000_init_static_data,
  944. .decode = jpeg2000_decode_frame,
  945. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  946. };