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.

1101 lines
39KB

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