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.

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