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.

1492 lines
52KB

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