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.

1096 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 "thread.h"
  31. #include "j2k.h"
  32. #include "libavutil/common.h"
  33. #define JP2_SIG_TYPE 0x6A502020
  34. #define JP2_SIG_VALUE 0x0D0A870A
  35. #define JP2_CODESTREAM 0x6A703263
  36. #define HAD_COC 0x01
  37. #define HAD_QCC 0x02
  38. typedef struct {
  39. Jpeg2000Component *comp;
  40. uint8_t properties[4];
  41. Jpeg2000CodingStyle codsty[4];
  42. Jpeg2000QuantStyle qntsty[4];
  43. } Jpeg2000Tile;
  44. typedef struct {
  45. AVCodecContext *avctx;
  46. AVFrame *picture;
  47. GetByteContext g;
  48. int width, height; ///< image width and height
  49. int image_offset_x, image_offset_y;
  50. int tile_offset_x, tile_offset_y;
  51. uint8_t cbps[4]; ///< bits per sample in particular components
  52. uint8_t sgnd[4]; ///< if a component is signed
  53. uint8_t properties[4];
  54. int cdx[4], cdy[4];
  55. int precision;
  56. int ncomponents;
  57. int tile_width, tile_height; ///< tile size
  58. int numXtiles, numYtiles;
  59. int maxtilelen;
  60. Jpeg2000CodingStyle codsty[4];
  61. Jpeg2000QuantStyle qntsty[4];
  62. int bit_index;
  63. int curtileno;
  64. Jpeg2000Tile *tile;
  65. } Jpeg2000DecoderContext;
  66. static int get_bits(Jpeg2000DecoderContext *s, int n)
  67. {
  68. int res = 0;
  69. while (--n >= 0){
  70. res <<= 1;
  71. if (s->bit_index == 0) {
  72. s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
  73. }
  74. s->bit_index--;
  75. res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
  76. }
  77. return res;
  78. }
  79. static void j2k_flush(Jpeg2000DecoderContext *s)
  80. {
  81. if (bytestream2_get_byte(&s->g) == 0xff)
  82. bytestream2_skip(&s->g, 1);
  83. s->bit_index = 8;
  84. }
  85. #if 0
  86. void printcomp(Jpeg2000Component *comp)
  87. {
  88. int i;
  89. for (i = 0; i < comp->y1 - comp->y0; i++)
  90. ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
  91. }
  92. static void nspaces(FILE *fd, int n)
  93. {
  94. while(n--) putc(' ', fd);
  95. }
  96. static void dump(Jpeg2000DecoderContext *s, FILE *fd)
  97. {
  98. int tileno, compno, reslevelno, bandno, precno;
  99. fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
  100. "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
  101. "tiles:\n",
  102. s->width, s->height, s->tile_width, s->tile_height,
  103. s->numXtiles, s->numYtiles, s->ncomponents);
  104. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  105. Jpeg2000Tile *tile = s->tile + tileno;
  106. nspaces(fd, 2);
  107. fprintf(fd, "tile %d:\n", tileno);
  108. for(compno = 0; compno < s->ncomponents; compno++){
  109. Jpeg2000Component *comp = tile->comp + compno;
  110. nspaces(fd, 4);
  111. fprintf(fd, "component %d:\n", compno);
  112. nspaces(fd, 4);
  113. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
  114. comp->x0, comp->x1, comp->y0, comp->y1);
  115. for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  116. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  117. nspaces(fd, 6);
  118. fprintf(fd, "reslevel %d:\n", reslevelno);
  119. nspaces(fd, 6);
  120. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
  121. reslevel->x0, reslevel->x1, reslevel->y0,
  122. reslevel->y1, reslevel->nbands);
  123. for(bandno = 0; bandno < reslevel->nbands; bandno++){
  124. Jpeg2000Band *band = reslevel->band + bandno;
  125. nspaces(fd, 8);
  126. fprintf(fd, "band %d:\n", bandno);
  127. nspaces(fd, 8);
  128. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
  129. "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
  130. band->x0, band->x1,
  131. band->y0, band->y1,
  132. band->codeblock_width, band->codeblock_height,
  133. band->cblknx, band->cblkny);
  134. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  135. Jpeg2000Prec *prec = band->prec + precno;
  136. nspaces(fd, 10);
  137. fprintf(fd, "prec %d:\n", precno);
  138. nspaces(fd, 10);
  139. fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
  140. prec->xi0, prec->xi1, prec->yi0, prec->yi1);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. #endif
  148. /** decode the value stored in node */
  149. static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
  150. {
  151. Jpeg2000TgtNode *stack[30];
  152. int sp = -1, curval = 0;
  153. if(!node)
  154. return AVERROR(EINVAL);
  155. while(node && !node->vis){
  156. stack[++sp] = node;
  157. node = node->parent;
  158. }
  159. if (node)
  160. curval = node->val;
  161. else
  162. curval = stack[sp]->val;
  163. while(curval < threshold && sp >= 0){
  164. if (curval < stack[sp]->val)
  165. curval = stack[sp]->val;
  166. while (curval < threshold){
  167. int ret;
  168. if ((ret = get_bits(s, 1)) > 0){
  169. stack[sp]->vis++;
  170. break;
  171. } else if (!ret)
  172. curval++;
  173. else
  174. return ret;
  175. }
  176. stack[sp]->val = curval;
  177. sp--;
  178. }
  179. return curval;
  180. }
  181. /* marker segments */
  182. /** get sizes and offsets of image, tiles; number of components */
  183. static int get_siz(Jpeg2000DecoderContext *s)
  184. {
  185. int i, ret;
  186. ThreadFrame frame = { .f = s->picture };
  187. if (bytestream2_get_bytes_left(&s->g) < 36)
  188. return AVERROR(EINVAL);
  189. bytestream2_get_be16u(&s->g); // Rsiz
  190. s->width = bytestream2_get_be32u(&s->g); // Width
  191. s->height = bytestream2_get_be32u(&s->g); // Height
  192. s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
  193. s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
  194. s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
  195. s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
  196. s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
  197. s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
  198. s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
  199. if(s->ncomponents <= 0 || s->ncomponents > 4) {
  200. av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
  201. return AVERROR(EINVAL);
  202. }
  203. if(s->tile_width<=0 || s->tile_height<=0)
  204. return AVERROR(EINVAL);
  205. if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
  206. return AVERROR(EINVAL);
  207. for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
  208. uint8_t x = bytestream2_get_byteu(&s->g);
  209. s->cbps[i] = (x & 0x7f) + 1;
  210. s->precision = FFMAX(s->cbps[i], s->precision);
  211. s->sgnd[i] = !!(x & 0x80);
  212. s->cdx[i] = bytestream2_get_byteu(&s->g);
  213. s->cdy[i] = bytestream2_get_byteu(&s->g);
  214. }
  215. s->numXtiles = ff_j2k_ceildiv(s->width - s->tile_offset_x, s->tile_width);
  216. s->numYtiles = ff_j2k_ceildiv(s->height - s->tile_offset_y, s->tile_height);
  217. if(s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
  218. return AVERROR(EINVAL);
  219. s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(Jpeg2000Tile));
  220. if (!s->tile)
  221. return AVERROR(ENOMEM);
  222. for (i = 0; i < s->numXtiles * s->numYtiles; i++){
  223. Jpeg2000Tile *tile = s->tile + i;
  224. tile->comp = av_mallocz(s->ncomponents * sizeof(Jpeg2000Component));
  225. if (!tile->comp)
  226. return AVERROR(ENOMEM);
  227. }
  228. s->avctx->width = s->width - s->image_offset_x;
  229. s->avctx->height = s->height - s->image_offset_y;
  230. switch(s->ncomponents){
  231. case 1:
  232. if (s->precision > 8) {
  233. s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  234. } else {
  235. s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  236. }
  237. break;
  238. case 3:
  239. if (s->precision > 8) {
  240. s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
  241. } else {
  242. s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
  243. }
  244. break;
  245. case 4:
  246. s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
  247. break;
  248. }
  249. if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 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(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *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 & JPEG2000_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(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
  277. {
  278. Jpeg2000CodingStyle 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(Jpeg2000CodingStyle));
  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(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *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(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *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 == JPEG2000_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 == JPEG2000_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(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
  351. {
  352. Jpeg2000QuantStyle 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(Jpeg2000QuantStyle));
  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(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *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 int get_sot(Jpeg2000DecoderContext *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. Jpeg2000Tile *tile = s->tile + s->curtileno;
  384. /* copy defaults */
  385. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
  386. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
  387. }
  388. bytestream2_get_byteu(&s->g); ///< TNsot
  389. return 0;
  390. }
  391. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  392. {
  393. int compno,
  394. tilex = tileno % s->numXtiles,
  395. tiley = tileno / s->numXtiles;
  396. Jpeg2000Tile *tile = s->tile + tileno;
  397. if (!tile->comp)
  398. return AVERROR(ENOMEM);
  399. for (compno = 0; compno < s->ncomponents; compno++){
  400. Jpeg2000Component *comp = tile->comp + compno;
  401. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  402. Jpeg2000QuantStyle *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(Jpeg2000DecoderContext *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(Jpeg2000DecoderContext *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(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *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. Jpeg2000Band *band = rlevel->band + bandno;
  449. Jpeg2000Prec *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. Jpeg2000Cblk *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 & JPEG2000_CSTY_EPH) {
  481. if (bytestream2_peek_be16(&s->g) == JPEG2000_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. Jpeg2000Band *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. Jpeg2000Cblk *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 jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *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. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  514. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  515. if (reslevelno < codsty->nreslevels){
  516. Jpeg2000ResLevel *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(Jpeg2000T1Context *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] & JPEG2000_T1_SIG_NB)
  538. && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_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] |= JPEG2000_T1_VIS;
  551. }
  552. }
  553. }
  554. static void decode_refpass(Jpeg2000T1Context *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] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_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] |= JPEG2000_T1_REF;
  568. }
  569. }
  570. }
  571. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *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] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  579. (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  580. (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  581. (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_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] & (JPEG2000_T1_SIG | JPEG2000_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] &= ~JPEG2000_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(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *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 = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
  623. int vert_causal_ctx_csty_symbol = JPEG2000_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 & JPEG2000_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(Jpeg2000DecoderContext *s, Jpeg2000Tile *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(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  683. {
  684. int compno, reslevelno, bandno;
  685. int x, y, *src[4];
  686. uint8_t *line;
  687. Jpeg2000T1Context t1;
  688. for (compno = 0; compno < s->ncomponents; compno++){
  689. Jpeg2000Component *comp = tile->comp + compno;
  690. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  691. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  692. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  693. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  694. Jpeg2000Band *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(Jpeg2000DecoderContext *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. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  792. Jpeg2000CodingStyle *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(Jpeg2000DecoderContext *s)
  800. {
  801. Jpeg2000CodingStyle *codsty = s->codsty;
  802. Jpeg2000QuantStyle *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 == JPEG2000_SOD){
  814. Jpeg2000Tile *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 = jpeg2000_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 == JPEG2000_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 JPEG2000_SIZ:
  832. ret = get_siz(s);
  833. break;
  834. case JPEG2000_COC:
  835. ret = get_coc(s, codsty, properties);
  836. break;
  837. case JPEG2000_COD:
  838. ret = get_cod(s, codsty, properties);
  839. break;
  840. case JPEG2000_QCC:
  841. ret = get_qcc(s, len, qntsty, properties);
  842. break;
  843. case JPEG2000_QCD:
  844. ret = get_qcd(s, len, qntsty, properties);
  845. break;
  846. case JPEG2000_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 JPEG2000_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(Jpeg2000DecoderContext *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. Jpeg2000DecoderContext *s = avctx->priv_data;
  894. AVFrame *picture = data;
  895. int tileno, ret;
  896. s->picture = picture;
  897. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  898. s->curtileno = -1;
  899. if (bytestream2_get_bytes_left(&s->g) < 2) {
  900. ret = AVERROR(EINVAL);
  901. goto err_out;
  902. }
  903. // check if the image is in jp2 format
  904. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  905. (bytestream2_get_be32u(&s->g) == 12) &&
  906. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  907. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  908. if(!jp2_find_codestream(s)) {
  909. av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
  910. ret = -1;
  911. goto err_out;
  912. }
  913. } else {
  914. bytestream2_seek(&s->g, 0, SEEK_SET);
  915. }
  916. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC){
  917. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  918. ret = -1;
  919. goto err_out;
  920. }
  921. if (ret = decode_codestream(s))
  922. goto err_out;
  923. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  924. if (ret = decode_tile(s, s->tile + tileno))
  925. goto err_out;
  926. cleanup(s);
  927. *got_frame = 1;
  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. Jpeg2000DecoderContext *s = avctx->priv_data;
  936. s->avctx = avctx;
  937. ff_j2k_init_tier1_luts();
  938. return 0;
  939. }
  940. AVCodec ff_j2k_decoder = {
  941. .name = "j2k",
  942. .type = AVMEDIA_TYPE_VIDEO,
  943. .id = AV_CODEC_ID_JPEG2000,
  944. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  945. .init = j2kdec_init,
  946. .decode = decode_frame,
  947. .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
  948. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  949. };