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.

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