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.

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