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.

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