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.

1497 lines
53KB

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