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.

1107 lines
38KB

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