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.

1396 lines
49KB

  1. /*
  2. * JPEG 2000 image decoder
  3. * Copyright (c) 2007 Kamil Nowosad
  4. * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * JPEG 2000 image decoder
  25. */
  26. #include "libavutil/common.h"
  27. #include "libavutil/opt.h"
  28. #include "avcodec.h"
  29. #include "bytestream.h"
  30. #include "internal.h"
  31. #include "thread.h"
  32. #include "jpeg2000.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 Jpeg2000TilePart {
  39. uint8_t tile_index; // Tile index who refers the tile-part
  40. const uint8_t *tp_end;
  41. GetByteContext tpg; // bit stream in tile-part
  42. } Jpeg2000TilePart;
  43. /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
  44. * one per component, so tile_part elements have a size of 3 */
  45. typedef struct Jpeg2000Tile {
  46. Jpeg2000Component *comp;
  47. uint8_t properties[4];
  48. Jpeg2000CodingStyle codsty[4];
  49. Jpeg2000QuantStyle qntsty[4];
  50. Jpeg2000TilePart tile_part[3];
  51. uint16_t tp_idx; // Tile-part index
  52. } Jpeg2000Tile;
  53. typedef struct Jpeg2000DecoderContext {
  54. AVClass *class;
  55. AVCodecContext *avctx;
  56. GetByteContext g;
  57. int width, height;
  58. int image_offset_x, image_offset_y;
  59. int tile_offset_x, tile_offset_y;
  60. uint8_t cbps[4]; // bits per sample in particular components
  61. uint8_t sgnd[4]; // if a component is signed
  62. uint8_t properties[4];
  63. int cdx[4], cdy[4];
  64. int precision;
  65. int ncomponents;
  66. int tile_width, tile_height;
  67. int numXtiles, numYtiles;
  68. int maxtilelen;
  69. Jpeg2000CodingStyle codsty[4];
  70. Jpeg2000QuantStyle qntsty[4];
  71. int bit_index;
  72. int curtileno;
  73. Jpeg2000Tile *tile;
  74. /*options parameters*/
  75. int lowres;
  76. int reduction_factor;
  77. } Jpeg2000DecoderContext;
  78. /* get_bits functions for JPEG2000 packet bitstream
  79. * It is a get_bit function with a bit-stuffing routine. If the value of the
  80. * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
  81. * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
  82. static int get_bits(Jpeg2000DecoderContext *s, int n)
  83. {
  84. int res = 0;
  85. while (--n >= 0) {
  86. res <<= 1;
  87. if (s->bit_index == 0) {
  88. s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
  89. }
  90. s->bit_index--;
  91. res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
  92. }
  93. return res;
  94. }
  95. static void jpeg2000_flush(Jpeg2000DecoderContext *s)
  96. {
  97. if (bytestream2_get_byte(&s->g) == 0xff)
  98. bytestream2_skip(&s->g, 1);
  99. s->bit_index = 8;
  100. }
  101. /* decode the value stored in node */
  102. static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
  103. int threshold)
  104. {
  105. Jpeg2000TgtNode *stack[30];
  106. int sp = -1, curval = 0;
  107. if (!node)
  108. return AVERROR(EINVAL);
  109. while (node && !node->vis) {
  110. stack[++sp] = node;
  111. node = node->parent;
  112. }
  113. if (node)
  114. curval = node->val;
  115. else
  116. curval = stack[sp]->val;
  117. while (curval < threshold && sp >= 0) {
  118. if (curval < stack[sp]->val)
  119. curval = stack[sp]->val;
  120. while (curval < threshold) {
  121. int ret;
  122. if ((ret = get_bits(s, 1)) > 0) {
  123. stack[sp]->vis++;
  124. break;
  125. } else if (!ret)
  126. curval++;
  127. else
  128. return ret;
  129. }
  130. stack[sp]->val = curval;
  131. sp--;
  132. }
  133. return curval;
  134. }
  135. /* marker segments */
  136. /* get sizes and offsets of image, tiles; number of components */
  137. static int get_siz(Jpeg2000DecoderContext *s)
  138. {
  139. int i;
  140. if (bytestream2_get_bytes_left(&s->g) < 36)
  141. return AVERROR(EINVAL);
  142. s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
  143. s->width = bytestream2_get_be32u(&s->g); // Width
  144. s->height = bytestream2_get_be32u(&s->g); // Height
  145. s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
  146. s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
  147. s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
  148. s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
  149. s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
  150. s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
  151. s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
  152. if (s->ncomponents <= 0 || s->ncomponents > 4) {
  153. av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
  154. return AVERROR(EINVAL);
  155. }
  156. if (s->tile_width<=0 || s->tile_height<=0)
  157. return AVERROR(EINVAL);
  158. if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
  159. return AVERROR(EINVAL);
  160. for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
  161. uint8_t x = bytestream2_get_byteu(&s->g);
  162. s->cbps[i] = (x & 0x7f) + 1;
  163. s->precision = FFMAX(s->cbps[i], s->precision);
  164. s->sgnd[i] = !!(x & 0x80);
  165. s->cdx[i] = bytestream2_get_byteu(&s->g);
  166. s->cdy[i] = bytestream2_get_byteu(&s->g);
  167. if (s->cdx[i] != 1 || s->cdy[i] != 1) {
  168. av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values %d %d for component %d\n", s->cdx[i], s->cdy[i], i);
  169. }
  170. }
  171. s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
  172. s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
  173. if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
  174. return AVERROR(EINVAL);
  175. s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
  176. if (!s->tile)
  177. return AVERROR(ENOMEM);
  178. for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
  179. Jpeg2000Tile *tile = s->tile + i;
  180. tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
  181. if (!tile->comp)
  182. return AVERROR(ENOMEM);
  183. }
  184. /* compute image size with reduction factor */
  185. s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
  186. s->reduction_factor);
  187. s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
  188. s->reduction_factor);
  189. switch(s->ncomponents) {
  190. case 1:
  191. if (s->precision > 8)
  192. s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  193. else
  194. s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  195. break;
  196. case 3:
  197. switch (s->avctx->profile) {
  198. case FF_PROFILE_JPEG2000_DCINEMA_2K:
  199. case FF_PROFILE_JPEG2000_DCINEMA_4K:
  200. /* XYZ color-space for digital cinema profiles */
  201. s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
  202. break;
  203. default:
  204. if (s->precision > 8)
  205. s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
  206. else
  207. s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
  208. break;
  209. }
  210. break;
  211. case 4:
  212. s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
  213. break;
  214. default:
  215. /* pixel format can not be identified */
  216. s->avctx->pix_fmt = AV_PIX_FMT_NONE;
  217. break;
  218. }
  219. return 0;
  220. }
  221. /* get common part for COD and COC segments */
  222. static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
  223. {
  224. uint8_t byte;
  225. if (bytestream2_get_bytes_left(&s->g) < 5)
  226. return AVERROR(EINVAL);
  227. c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
  228. if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
  229. av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
  230. return AVERROR_INVALIDDATA;
  231. }
  232. /* compute number of resolution levels to decode */
  233. if (c->nreslevels < s->reduction_factor)
  234. c->nreslevels2decode = 1;
  235. else
  236. c->nreslevels2decode = c->nreslevels - s->reduction_factor;
  237. c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
  238. c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
  239. if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
  240. c->log2_cblk_width + c->log2_cblk_height > 14) {
  241. av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. c->cblk_style = bytestream2_get_byteu(&s->g);
  245. if (c->cblk_style != 0) { // cblk style
  246. av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
  247. }
  248. c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
  249. /* set integer 9/7 DWT in case of BITEXACT flag */
  250. if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
  251. c->transform = FF_DWT97_INT;
  252. if (c->csty & JPEG2000_CSTY_PREC) {
  253. int i;
  254. for (i = 0; i < c->nreslevels; i++) {
  255. byte = bytestream2_get_byte(&s->g);
  256. c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
  257. c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
  258. }
  259. } else {
  260. memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
  261. memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
  262. }
  263. return 0;
  264. }
  265. /* get coding parameters for a particular tile or whole image*/
  266. static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  267. uint8_t *properties)
  268. {
  269. Jpeg2000CodingStyle tmp;
  270. int compno;
  271. if (bytestream2_get_bytes_left(&s->g) < 5)
  272. return AVERROR(EINVAL);
  273. tmp.csty = bytestream2_get_byteu(&s->g);
  274. // get progression order
  275. tmp.prog_order = bytestream2_get_byteu(&s->g);
  276. tmp.nlayers = bytestream2_get_be16u(&s->g);
  277. tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
  278. get_cox(s, &tmp);
  279. for (compno = 0; compno < s->ncomponents; compno++)
  280. if (!(properties[compno] & HAD_COC))
  281. memcpy(c + compno, &tmp, sizeof(tmp));
  282. return 0;
  283. }
  284. /* Get coding parameters for a component in the whole image or a
  285. * particular tile. */
  286. static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  287. uint8_t *properties)
  288. {
  289. int compno;
  290. if (bytestream2_get_bytes_left(&s->g) < 2)
  291. return AVERROR(EINVAL);
  292. compno = bytestream2_get_byteu(&s->g);
  293. c += compno;
  294. c->csty = bytestream2_get_byteu(&s->g);
  295. get_cox(s, c);
  296. properties[compno] |= HAD_COC;
  297. return 0;
  298. }
  299. /* Get common part for QCD and QCC segments. */
  300. static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
  301. {
  302. int i, x;
  303. if (bytestream2_get_bytes_left(&s->g) < 1)
  304. return AVERROR(EINVAL);
  305. x = bytestream2_get_byteu(&s->g); // Sqcd
  306. q->nguardbits = x >> 5;
  307. q->quantsty = x & 0x1f;
  308. if (q->quantsty == JPEG2000_QSTY_NONE) {
  309. n -= 3;
  310. if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
  311. return AVERROR(EINVAL);
  312. for (i = 0; i < n; i++)
  313. q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
  314. } else if (q->quantsty == JPEG2000_QSTY_SI) {
  315. if (bytestream2_get_bytes_left(&s->g) < 2)
  316. return AVERROR(EINVAL);
  317. x = bytestream2_get_be16u(&s->g);
  318. q->expn[0] = x >> 11;
  319. q->mant[0] = x & 0x7ff;
  320. for (i = 1; i < 32 * 3; i++) {
  321. int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
  322. q->expn[i] = curexpn;
  323. q->mant[i] = q->mant[0];
  324. }
  325. } else {
  326. n = (n - 3) >> 1;
  327. if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
  328. return AVERROR(EINVAL);
  329. for (i = 0; i < n; i++) {
  330. x = bytestream2_get_be16u(&s->g);
  331. q->expn[i] = x >> 11;
  332. q->mant[i] = x & 0x7ff;
  333. }
  334. }
  335. return 0;
  336. }
  337. /* Get quantization parameters for a particular tile or a whole image. */
  338. static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  339. uint8_t *properties)
  340. {
  341. Jpeg2000QuantStyle tmp;
  342. int compno;
  343. if (get_qcx(s, n, &tmp))
  344. return -1;
  345. for (compno = 0; compno < s->ncomponents; compno++)
  346. if (!(properties[compno] & HAD_QCC))
  347. memcpy(q + compno, &tmp, sizeof(tmp));
  348. return 0;
  349. }
  350. /* Get quantization parameters for a component in the whole image
  351. * on in a particular tile. */
  352. static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  353. uint8_t *properties)
  354. {
  355. int compno;
  356. if (bytestream2_get_bytes_left(&s->g) < 1)
  357. return AVERROR(EINVAL);
  358. compno = bytestream2_get_byteu(&s->g);
  359. properties[compno] |= HAD_QCC;
  360. return get_qcx(s, n - 1, q + compno);
  361. }
  362. /* Get start of tile segment. */
  363. static int get_sot(Jpeg2000DecoderContext *s, int n)
  364. {
  365. Jpeg2000TilePart *tp;
  366. uint16_t Isot;
  367. uint32_t Psot;
  368. uint8_t TPsot;
  369. if (bytestream2_get_bytes_left(&s->g) < 8)
  370. return AVERROR(EINVAL);
  371. s->curtileno = Isot = bytestream2_get_be16u(&s->g); // Isot
  372. if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
  373. s->curtileno=0;
  374. return AVERROR(EINVAL);
  375. }
  376. Psot = bytestream2_get_be32u(&s->g); // Psot
  377. TPsot = bytestream2_get_byteu(&s->g); // TPsot
  378. /* Read TNSot but not used */
  379. bytestream2_get_byteu(&s->g); // TNsot
  380. if (TPsot >= FF_ARRAY_ELEMS(s->tile[s->curtileno].tile_part)) {
  381. av_log(s->avctx, AV_LOG_ERROR, "TPsot %d too big\n", TPsot);
  382. return AVERROR_PATCHWELCOME;
  383. }
  384. s->tile[s->curtileno].tp_idx = TPsot;
  385. tp = s->tile[s->curtileno].tile_part + TPsot;
  386. tp->tile_index = Isot;
  387. tp->tp_end = s->g.buffer + Psot - n - 2;
  388. if (!TPsot) {
  389. Jpeg2000Tile *tile = s->tile + s->curtileno;
  390. /* copy defaults */
  391. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
  392. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
  393. }
  394. return 0;
  395. }
  396. /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
  397. * Used to know the number of tile parts and lengths.
  398. * There may be multiple TLMs in the header.
  399. * TODO: The function is not used for tile-parts management, nor anywhere else.
  400. * It can be useful to allocate memory for tile parts, before managing the SOT
  401. * markers. Parsing the TLM header is needed to increment the input header
  402. * buffer.
  403. * This marker is mandatory for DCI. */
  404. static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
  405. {
  406. uint8_t Stlm, ST, SP, tile_tlm, i;
  407. bytestream2_get_byte(&s->g); /* Ztlm: skipped */
  408. Stlm = bytestream2_get_byte(&s->g);
  409. // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  410. ST = (Stlm >> 4) & 0x03;
  411. // TODO: Manage case of ST = 0b11 --> raise error
  412. SP = (Stlm >> 6) & 0x01;
  413. tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
  414. for (i = 0; i < tile_tlm; i++) {
  415. switch (ST) {
  416. case 0:
  417. break;
  418. case 1:
  419. bytestream2_get_byte(&s->g);
  420. break;
  421. case 2:
  422. bytestream2_get_be16(&s->g);
  423. break;
  424. case 3:
  425. bytestream2_get_be32(&s->g);
  426. break;
  427. }
  428. if (SP == 0) {
  429. bytestream2_get_be16(&s->g);
  430. } else {
  431. bytestream2_get_be32(&s->g);
  432. }
  433. }
  434. return 0;
  435. }
  436. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  437. {
  438. int compno;
  439. int tilex = tileno % s->numXtiles;
  440. int tiley = tileno / s->numXtiles;
  441. Jpeg2000Tile *tile = s->tile + tileno;
  442. if (!tile->comp)
  443. return AVERROR(ENOMEM);
  444. for (compno = 0; compno < s->ncomponents; compno++) {
  445. Jpeg2000Component *comp = tile->comp + compno;
  446. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  447. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  448. int ret; // global bandno
  449. comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
  450. comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
  451. comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
  452. comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
  453. comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
  454. comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
  455. comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
  456. comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
  457. if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  458. s->cbps[compno], s->cdx[compno],
  459. s->cdy[compno], s->avctx))
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. /* Read the number of coding passes. */
  465. static int getnpasses(Jpeg2000DecoderContext *s)
  466. {
  467. int num;
  468. if (!get_bits(s, 1))
  469. return 1;
  470. if (!get_bits(s, 1))
  471. return 2;
  472. if ((num = get_bits(s, 2)) != 3)
  473. return num < 0 ? num : 3 + num;
  474. if ((num = get_bits(s, 5)) != 31)
  475. return num < 0 ? num : 6 + num;
  476. num = get_bits(s, 7);
  477. return num < 0 ? num : 37 + num;
  478. }
  479. static int getlblockinc(Jpeg2000DecoderContext *s)
  480. {
  481. int res = 0, ret;
  482. while (ret = get_bits(s, 1)) {
  483. if (ret < 0)
  484. return ret;
  485. res++;
  486. }
  487. return res;
  488. }
  489. static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
  490. Jpeg2000CodingStyle *codsty,
  491. Jpeg2000ResLevel *rlevel, int precno,
  492. int layno, uint8_t *expn, int numgbits)
  493. {
  494. int bandno, cblkno, ret, nb_code_blocks;
  495. if (!(ret = get_bits(s, 1))) {
  496. jpeg2000_flush(s);
  497. return 0;
  498. } else if (ret < 0)
  499. return ret;
  500. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  501. Jpeg2000Band *band = rlevel->band + bandno;
  502. Jpeg2000Prec *prec = band->prec + precno;
  503. if (band->coord[0][0] == band->coord[0][1] ||
  504. band->coord[1][0] == band->coord[1][1])
  505. continue;
  506. nb_code_blocks = prec->nb_codeblocks_height *
  507. prec->nb_codeblocks_width;
  508. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  509. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  510. int incl, newpasses, llen;
  511. if (cblk->npasses)
  512. incl = get_bits(s, 1);
  513. else
  514. incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
  515. if (!incl)
  516. continue;
  517. else if (incl < 0)
  518. return incl;
  519. if (!cblk->npasses)
  520. cblk->nonzerobits = expn[bandno] + numgbits - 1 -
  521. tag_tree_decode(s, prec->zerobits + cblkno,
  522. 100);
  523. if ((newpasses = getnpasses(s)) < 0)
  524. return newpasses;
  525. if ((llen = getlblockinc(s)) < 0)
  526. return llen;
  527. cblk->lblock += llen;
  528. if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
  529. return ret;
  530. cblk->lengthinc = ret;
  531. cblk->npasses += newpasses;
  532. }
  533. }
  534. jpeg2000_flush(s);
  535. if (codsty->csty & JPEG2000_CSTY_EPH) {
  536. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
  537. bytestream2_skip(&s->g, 2);
  538. else
  539. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
  540. }
  541. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  542. Jpeg2000Band *band = rlevel->band + bandno;
  543. Jpeg2000Prec *prec = band->prec + precno;
  544. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  545. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  546. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  547. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
  548. || sizeof(cblk->data) < cblk->lengthinc
  549. )
  550. return AVERROR(EINVAL);
  551. /* Code-block data can be empty. In that case initialize data
  552. * with 0xFFFF. */
  553. if (cblk->lengthinc > 0) {
  554. bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
  555. } else {
  556. cblk->data[0] = 0xFF;
  557. cblk->data[1] = 0xFF;
  558. }
  559. cblk->length += cblk->lengthinc;
  560. cblk->lengthinc = 0;
  561. }
  562. }
  563. return 0;
  564. }
  565. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  566. {
  567. int layno, reslevelno, compno, precno, ok_reslevel;
  568. int x, y;
  569. s->bit_index = 8;
  570. switch (tile->codsty[0].prog_order) {
  571. case JPEG2000_PGOD_LRCP:
  572. case JPEG2000_PGOD_RLCP:
  573. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  574. ok_reslevel = 1;
  575. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  576. ok_reslevel = 0;
  577. for (compno = 0; compno < s->ncomponents; compno++) {
  578. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  579. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  580. if (reslevelno < codsty->nreslevels) {
  581. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  582. reslevelno;
  583. ok_reslevel = 1;
  584. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  585. if (jpeg2000_decode_packet(s,
  586. codsty, rlevel,
  587. precno, layno,
  588. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  589. qntsty->nguardbits))
  590. return -1;
  591. }
  592. }
  593. }
  594. }
  595. break;
  596. case JPEG2000_PGOD_CPRL:
  597. for (compno = 0; compno < s->ncomponents; compno++) {
  598. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  599. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  600. /* Set bit stream buffer address according to tile-part.
  601. * For DCinema one tile-part per component, so can be
  602. * indexed by component. */
  603. s->g = tile->tile_part[compno].tpg;
  604. /* Position loop (y axis)
  605. * TODO: Automate computing of step 256.
  606. * Fixed here, but to be computed before entering here. */
  607. for (y = 0; y < s->height; y += 256) {
  608. /* Position loop (y axis)
  609. * TODO: automate computing of step 256.
  610. * Fixed here, but to be computed before entering here. */
  611. for (x = 0; x < s->width; x += 256) {
  612. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  613. uint16_t prcx, prcy;
  614. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  615. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  616. if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
  617. (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  618. continue;
  619. if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
  620. (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  621. continue;
  622. // check if a precinct exists
  623. prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
  624. prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
  625. precno = prcx + rlevel->num_precincts_x * prcy;
  626. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  627. if (jpeg2000_decode_packet(s, codsty, rlevel,
  628. precno, layno,
  629. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  630. qntsty->nguardbits))
  631. return -1;
  632. }
  633. }
  634. }
  635. }
  636. }
  637. break;
  638. default:
  639. break;
  640. }
  641. /* EOC marker reached */
  642. bytestream2_skip(&s->g, 2);
  643. return 0;
  644. }
  645. /* TIER-1 routines */
  646. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  647. int bpno, int bandno, int bpass_csty_symbol,
  648. int vert_causal_ctx_csty_symbol)
  649. {
  650. int mask = 3 << (bpno - 1), y0, x, y;
  651. for (y0 = 0; y0 < height; y0 += 4)
  652. for (x = 0; x < width; x++)
  653. for (y = y0; y < height && y < y0 + 4; y++) {
  654. if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
  655. && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  656. int flags_mask = -1;
  657. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  658. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  659. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
  660. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  661. if (bpass_csty_symbol)
  662. t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  663. else
  664. t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  665. -mask : mask;
  666. ff_jpeg2000_set_significance(t1, x, y,
  667. t1->data[y][x] < 0);
  668. }
  669. t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
  670. }
  671. }
  672. }
  673. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  674. int bpno)
  675. {
  676. int phalf, nhalf;
  677. int y0, x, y;
  678. phalf = 1 << (bpno - 1);
  679. nhalf = -phalf;
  680. for (y0 = 0; y0 < height; y0 += 4)
  681. for (x = 0; x < width; x++)
  682. for (y = y0; y < height && y < y0 + 4; y++)
  683. if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  684. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
  685. int r = ff_mqc_decode(&t1->mqc,
  686. t1->mqc.cx_states + ctxno)
  687. ? phalf : nhalf;
  688. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  689. t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
  690. }
  691. }
  692. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  693. int width, int height, int bpno, int bandno,
  694. int seg_symbols, int vert_causal_ctx_csty_symbol)
  695. {
  696. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  697. for (y0 = 0; y0 < height; y0 += 4) {
  698. for (x = 0; x < width; x++) {
  699. if (y0 + 3 < height &&
  700. !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  701. (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  702. (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  703. (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  704. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  705. continue;
  706. runlen = ff_mqc_decode(&t1->mqc,
  707. t1->mqc.cx_states + MQC_CX_UNI);
  708. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  709. t1->mqc.cx_states +
  710. MQC_CX_UNI);
  711. dec = 1;
  712. } else {
  713. runlen = 0;
  714. dec = 0;
  715. }
  716. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  717. if (!dec) {
  718. if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  719. int flags_mask = -1;
  720. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  721. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  722. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
  723. bandno));
  724. }
  725. }
  726. if (dec) {
  727. int xorbit;
  728. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  729. &xorbit);
  730. t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
  731. t1->mqc.cx_states + ctxno) ^
  732. xorbit)
  733. ? -mask : mask;
  734. ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
  735. }
  736. dec = 0;
  737. t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
  738. }
  739. }
  740. }
  741. if (seg_symbols) {
  742. int val;
  743. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  744. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  745. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  746. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  747. if (val != 0xa)
  748. av_log(s->avctx, AV_LOG_ERROR,
  749. "Segmentation symbol value incorrect\n");
  750. }
  751. }
  752. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  753. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  754. int width, int height, int bandpos)
  755. {
  756. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
  757. int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
  758. int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
  759. for (y = 0; y < height; y++)
  760. memset(t1->data[y], 0, width * sizeof(**t1->data));
  761. /* If code-block contains no compressed data: nothing to do. */
  762. if (!cblk->length)
  763. return 0;
  764. for (y = 0; y < height+2; y++)
  765. memset(t1->flags[y], 0, (width + 2)*sizeof(**t1->flags));
  766. cblk->data[cblk->length] = 0xff;
  767. cblk->data[cblk->length+1] = 0xff;
  768. ff_mqc_initdec(&t1->mqc, cblk->data);
  769. while (passno--) {
  770. switch(pass_t) {
  771. case 0:
  772. decode_sigpass(t1, width, height, bpno + 1, bandpos,
  773. bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
  774. break;
  775. case 1:
  776. decode_refpass(t1, width, height, bpno + 1);
  777. if (bpass_csty_symbol && clnpass_cnt >= 4)
  778. ff_mqc_initdec(&t1->mqc, cblk->data);
  779. break;
  780. case 2:
  781. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  782. codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
  783. clnpass_cnt = clnpass_cnt + 1;
  784. if (bpass_csty_symbol && clnpass_cnt >= 4)
  785. ff_mqc_initdec(&t1->mqc, cblk->data);
  786. break;
  787. }
  788. pass_t++;
  789. if (pass_t == 3) {
  790. bpno--;
  791. pass_t = 0;
  792. }
  793. }
  794. return 0;
  795. }
  796. /* TODO: Verify dequantization for lossless case
  797. * comp->data can be float or int
  798. * band->stepsize can be float or int
  799. * depending on the type of DWT transformation.
  800. * see ISO/IEC 15444-1:2002 A.6.1 */
  801. /* Float dequantization of a codeblock.*/
  802. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  803. Jpeg2000Component *comp,
  804. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  805. {
  806. int i, j;
  807. int w = cblk->coord[0][1] - cblk->coord[0][0];
  808. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  809. float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  810. int *src = t1->data[j];
  811. for (i = 0; i < w; ++i)
  812. datap[i] = src[i] * band->f_stepsize;
  813. }
  814. }
  815. /* Integer dequantization of a codeblock.*/
  816. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  817. Jpeg2000Component *comp,
  818. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  819. {
  820. int i, j;
  821. int w = cblk->coord[0][1] - cblk->coord[0][0];
  822. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  823. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  824. int *src = t1->data[j];
  825. for (i = 0; i < w; ++i)
  826. datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
  827. }
  828. }
  829. /* Inverse ICT parameters in float and integer.
  830. * int value = (float value) * (1<<16) */
  831. static const float f_ict_params[4] = {
  832. 1.402f,
  833. 0.34413f,
  834. 0.71414f,
  835. 1.772f
  836. };
  837. static const int i_ict_params[4] = {
  838. 91881,
  839. 22553,
  840. 46802,
  841. 116130
  842. };
  843. static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  844. {
  845. int i, csize = 1;
  846. int32_t *src[3], i0, i1, i2;
  847. float *srcf[3], i0f, i1f, i2f;
  848. for (i = 0; i < 3; i++)
  849. if (tile->codsty[0].transform == FF_DWT97)
  850. srcf[i] = tile->comp[i].f_data;
  851. else
  852. src [i] = tile->comp[i].i_data;
  853. for (i = 0; i < 2; i++)
  854. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  855. switch (tile->codsty[0].transform) {
  856. case FF_DWT97:
  857. for (i = 0; i < csize; i++) {
  858. i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
  859. i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
  860. - (f_ict_params[2] * *srcf[2]);
  861. i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
  862. *srcf[0]++ = i0f;
  863. *srcf[1]++ = i1f;
  864. *srcf[2]++ = i2f;
  865. }
  866. break;
  867. case FF_DWT97_INT:
  868. for (i = 0; i < csize; i++) {
  869. i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
  870. i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
  871. - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
  872. i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
  873. *src[0]++ = i0;
  874. *src[1]++ = i1;
  875. *src[2]++ = i2;
  876. }
  877. break;
  878. case FF_DWT53:
  879. for (i = 0; i < csize; i++) {
  880. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  881. i0 = i1 + *src[2];
  882. i2 = i1 + *src[1];
  883. *src[0]++ = i0;
  884. *src[1]++ = i1;
  885. *src[2]++ = i2;
  886. }
  887. break;
  888. }
  889. }
  890. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  891. AVFrame *picture)
  892. {
  893. int compno, reslevelno, bandno;
  894. int x, y;
  895. uint8_t *line;
  896. Jpeg2000T1Context t1;
  897. /* Loop on tile components */
  898. for (compno = 0; compno < s->ncomponents; compno++) {
  899. Jpeg2000Component *comp = tile->comp + compno;
  900. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  901. /* Loop on resolution levels */
  902. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  903. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  904. /* Loop on bands */
  905. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  906. int nb_precincts, precno;
  907. Jpeg2000Band *band = rlevel->band + bandno;
  908. int cblkno = 0, bandpos;
  909. bandpos = bandno + (reslevelno > 0);
  910. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  911. continue;
  912. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  913. /* Loop on precincts */
  914. for (precno = 0; precno < nb_precincts; precno++) {
  915. Jpeg2000Prec *prec = band->prec + precno;
  916. /* Loop on codeblocks */
  917. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  918. int x, y;
  919. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  920. decode_cblk(s, codsty, &t1, cblk,
  921. cblk->coord[0][1] - cblk->coord[0][0],
  922. cblk->coord[1][1] - cblk->coord[1][0],
  923. bandpos);
  924. /* Manage band offsets */
  925. x = cblk->coord[0][0];
  926. y = cblk->coord[1][0];
  927. if (codsty->transform == FF_DWT97)
  928. dequantization_float(x, y, cblk, comp, &t1, band);
  929. else
  930. dequantization_int(x, y, cblk, comp, &t1, band);
  931. } /* end cblk */
  932. } /*end prec */
  933. } /* end band */
  934. } /* end reslevel */
  935. /* inverse DWT */
  936. ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
  937. } /*end comp */
  938. /* inverse MCT transformation */
  939. if (tile->codsty[0].mct)
  940. mct_decode(s, tile);
  941. if (s->precision <= 8) {
  942. for (compno = 0; compno < s->ncomponents; compno++) {
  943. Jpeg2000Component *comp = tile->comp + compno;
  944. float *datap = comp->f_data;
  945. int32_t *i_datap = comp->i_data;
  946. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  947. line = picture->data[0] + y * picture->linesize[0];
  948. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  949. uint8_t *dst;
  950. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  951. dst = line + x * s->ncomponents + compno;
  952. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
  953. int val;
  954. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  955. if (tile->codsty->transform == FF_DWT97)
  956. val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
  957. else
  958. val = *i_datap + (1 << (s->cbps[compno] - 1));
  959. val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
  960. *dst = val << (8 - s->cbps[compno]);
  961. datap++;
  962. i_datap++;
  963. dst += s->ncomponents;
  964. }
  965. line += picture->linesize[0];
  966. }
  967. }
  968. } else {
  969. for (compno = 0; compno < s->ncomponents; compno++) {
  970. Jpeg2000Component *comp = tile->comp + compno;
  971. float *datap = comp->f_data;
  972. int32_t *i_datap = comp->i_data;
  973. uint16_t *linel;
  974. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  975. linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
  976. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  977. uint16_t *dst;
  978. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  979. dst = linel + (x * s->ncomponents + compno);
  980. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
  981. int val;
  982. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  983. if (tile->codsty->transform == FF_DWT97)
  984. val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
  985. else
  986. val = *i_datap + (1 << (s->cbps[compno] - 1));
  987. val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
  988. /* align 12 bit values in little-endian mode */
  989. *dst = val << (16 - s->cbps[compno]);
  990. datap++;
  991. i_datap++;
  992. dst += s->ncomponents;
  993. }
  994. linel += picture->linesize[0] >> 1;
  995. }
  996. }
  997. }
  998. return 0;
  999. }
  1000. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  1001. {
  1002. int tileno, compno;
  1003. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1004. for (compno = 0; compno < s->ncomponents; compno++) {
  1005. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1006. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1007. ff_jpeg2000_cleanup(comp, codsty);
  1008. }
  1009. av_freep(&s->tile[tileno].comp);
  1010. }
  1011. av_freep(&s->tile);
  1012. s->numXtiles = s->numYtiles = 0;
  1013. }
  1014. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1015. {
  1016. Jpeg2000CodingStyle *codsty = s->codsty;
  1017. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1018. uint8_t *properties = s->properties;
  1019. for (;;) {
  1020. int len, ret = 0;
  1021. uint16_t marker;
  1022. int oldpos;
  1023. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1024. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1025. break;
  1026. }
  1027. marker = bytestream2_get_be16u(&s->g);
  1028. oldpos = bytestream2_tell(&s->g);
  1029. if (marker == JPEG2000_SOD) {
  1030. Jpeg2000Tile *tile = s->tile + s->curtileno;
  1031. Jpeg2000TilePart *tp = tile->tile_part + tile->tp_idx;
  1032. bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
  1033. bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
  1034. continue;
  1035. }
  1036. if (marker == JPEG2000_EOC)
  1037. break;
  1038. if (bytestream2_get_bytes_left(&s->g) < 2)
  1039. return AVERROR(EINVAL);
  1040. len = bytestream2_get_be16u(&s->g);
  1041. switch (marker) {
  1042. case JPEG2000_SIZ:
  1043. ret = get_siz(s);
  1044. if (!s->tile)
  1045. s->numXtiles = s->numYtiles = 0;
  1046. break;
  1047. case JPEG2000_COC:
  1048. ret = get_coc(s, codsty, properties);
  1049. break;
  1050. case JPEG2000_COD:
  1051. ret = get_cod(s, codsty, properties);
  1052. break;
  1053. case JPEG2000_QCC:
  1054. ret = get_qcc(s, len, qntsty, properties);
  1055. break;
  1056. case JPEG2000_QCD:
  1057. ret = get_qcd(s, len, qntsty, properties);
  1058. break;
  1059. case JPEG2000_SOT:
  1060. if (!(ret = get_sot(s, len))) {
  1061. codsty = s->tile[s->curtileno].codsty;
  1062. qntsty = s->tile[s->curtileno].qntsty;
  1063. properties = s->tile[s->curtileno].properties;
  1064. }
  1065. break;
  1066. case JPEG2000_COM:
  1067. // the comment is ignored
  1068. bytestream2_skip(&s->g, len - 2);
  1069. break;
  1070. case JPEG2000_TLM:
  1071. // Tile-part lengths
  1072. ret = get_tlm(s, len);
  1073. break;
  1074. default:
  1075. av_log(s->avctx, AV_LOG_ERROR,
  1076. "unsupported marker 0x%.4X at pos 0x%X\n",
  1077. marker, bytestream2_tell(&s->g) - 4);
  1078. bytestream2_skip(&s->g, len - 2);
  1079. break;
  1080. }
  1081. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  1082. av_log(s->avctx, AV_LOG_ERROR,
  1083. "error during processing marker segment %.4x\n", marker);
  1084. return ret ? ret : -1;
  1085. }
  1086. }
  1087. return 0;
  1088. }
  1089. /* Read bit stream packets --> T2 operation. */
  1090. static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
  1091. {
  1092. int ret = 0;
  1093. int tileno;
  1094. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1095. Jpeg2000Tile *tile = s->tile + tileno;
  1096. if (ret = init_tile(s, tileno))
  1097. return ret;
  1098. s->g = tile->tile_part[0].tpg;
  1099. if (ret = jpeg2000_decode_packets(s, tile))
  1100. return ret;
  1101. }
  1102. return 0;
  1103. }
  1104. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1105. {
  1106. uint32_t atom_size, atom;
  1107. int found_codestream = 0, search_range = 10;
  1108. while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
  1109. atom_size = bytestream2_get_be32u(&s->g);
  1110. atom = bytestream2_get_be32u(&s->g);
  1111. if (atom == JP2_CODESTREAM) {
  1112. found_codestream = 1;
  1113. } else {
  1114. if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
  1115. return 0;
  1116. bytestream2_skipu(&s->g, atom_size - 8);
  1117. search_range--;
  1118. }
  1119. }
  1120. if (found_codestream)
  1121. return 1;
  1122. return 0;
  1123. }
  1124. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1125. int *got_frame, AVPacket *avpkt)
  1126. {
  1127. Jpeg2000DecoderContext *s = avctx->priv_data;
  1128. ThreadFrame frame = { .f = data };
  1129. AVFrame *picture = data;
  1130. int tileno, ret;
  1131. s->avctx = avctx;
  1132. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  1133. s->curtileno = -1;
  1134. // reduction factor, i.e number of resolution levels to skip
  1135. s->reduction_factor = s->lowres;
  1136. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1137. ret = AVERROR(EINVAL);
  1138. goto end;
  1139. }
  1140. // check if the image is in jp2 format
  1141. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  1142. (bytestream2_get_be32u(&s->g) == 12) &&
  1143. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  1144. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  1145. if (!jp2_find_codestream(s)) {
  1146. av_log(avctx, AV_LOG_ERROR,
  1147. "couldn't find jpeg2k codestream atom\n");
  1148. ret = -1;
  1149. goto end;
  1150. }
  1151. } else {
  1152. bytestream2_seek(&s->g, 0, SEEK_SET);
  1153. }
  1154. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  1155. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1156. ret = -1;
  1157. goto end;
  1158. }
  1159. if (ret = jpeg2000_read_main_headers(s))
  1160. goto end;
  1161. /* get picture buffer */
  1162. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1163. goto end;
  1164. picture->pict_type = AV_PICTURE_TYPE_I;
  1165. picture->key_frame = 1;
  1166. if (ret = jpeg2000_read_bitstream_packets(s))
  1167. goto end;
  1168. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1169. if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
  1170. goto end;
  1171. jpeg2000_dec_cleanup(s);
  1172. *got_frame = 1;
  1173. return bytestream2_tell(&s->g);
  1174. end:
  1175. jpeg2000_dec_cleanup(s);
  1176. return ret;
  1177. }
  1178. static void jpeg2000_init_static_data(AVCodec *codec)
  1179. {
  1180. ff_jpeg2000_init_tier1_luts();
  1181. }
  1182. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1183. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1184. static const AVOption options[] = {
  1185. { "lowres", "Lower the decoding resolution by a power of two",
  1186. OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1187. { NULL },
  1188. };
  1189. static const AVProfile profiles[] = {
  1190. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1191. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1192. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1193. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1194. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1195. { FF_PROFILE_UNKNOWN },
  1196. };
  1197. static const AVClass class = {
  1198. .class_name = "jpeg2000",
  1199. .item_name = av_default_item_name,
  1200. .option = options,
  1201. .version = LIBAVUTIL_VERSION_INT,
  1202. };
  1203. AVCodec ff_jpeg2000_decoder = {
  1204. .name = "jpeg2000",
  1205. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1206. .type = AVMEDIA_TYPE_VIDEO,
  1207. .id = AV_CODEC_ID_JPEG2000,
  1208. .capabilities = CODEC_CAP_FRAME_THREADS,
  1209. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1210. .init_static_data = jpeg2000_init_static_data,
  1211. .decode = jpeg2000_decode_frame,
  1212. .priv_class = &class,
  1213. .max_lowres = 5,
  1214. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1215. };