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.

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