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.

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