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.

1436 lines
51KB

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