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.

1045 lines
36KB

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