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.

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