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.

1091 lines
37KB

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