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.

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