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.

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