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.

1094 lines
37KB

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