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.

1486 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,
  564. 100);
  565. if (v < 0) {
  566. av_log(s->avctx, AV_LOG_ERROR, "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. cblk->lengthinc = ret;
  579. cblk->npasses += newpasses;
  580. }
  581. }
  582. jpeg2000_flush(s);
  583. if (codsty->csty & JPEG2000_CSTY_EPH) {
  584. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
  585. bytestream2_skip(&s->g, 2);
  586. else
  587. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
  588. }
  589. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  590. Jpeg2000Band *band = rlevel->band + bandno;
  591. Jpeg2000Prec *prec = band->prec + precno;
  592. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  593. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  594. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  595. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
  596. || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
  597. )
  598. return AVERROR_INVALIDDATA;
  599. bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
  600. cblk->length += cblk->lengthinc;
  601. cblk->lengthinc = 0;
  602. }
  603. }
  604. return 0;
  605. }
  606. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  607. {
  608. int layno, reslevelno, compno, precno, ok_reslevel, ret;
  609. int x, y;
  610. s->bit_index = 8;
  611. switch (tile->codsty[0].prog_order) {
  612. case JPEG2000_PGOD_LRCP:
  613. case JPEG2000_PGOD_RLCP:
  614. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  615. ok_reslevel = 1;
  616. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  617. ok_reslevel = 0;
  618. for (compno = 0; compno < s->ncomponents; compno++) {
  619. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  620. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  621. if (reslevelno < codsty->nreslevels) {
  622. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  623. reslevelno;
  624. ok_reslevel = 1;
  625. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  626. if ((ret = jpeg2000_decode_packet(s,
  627. codsty, rlevel,
  628. precno, layno,
  629. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  630. qntsty->nguardbits)) < 0)
  631. return ret;
  632. }
  633. }
  634. }
  635. }
  636. break;
  637. case JPEG2000_PGOD_CPRL:
  638. for (compno = 0; compno < s->ncomponents; compno++) {
  639. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  640. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  641. /* Set bit stream buffer address according to tile-part.
  642. * For DCinema one tile-part per component, so can be
  643. * indexed by component. */
  644. s->g = tile->tile_part[compno].tpg;
  645. /* Position loop (y axis)
  646. * TODO: Automate computing of step 256.
  647. * Fixed here, but to be computed before entering here. */
  648. for (y = 0; y < s->height; y += 256) {
  649. /* Position loop (y axis)
  650. * TODO: automate computing of step 256.
  651. * Fixed here, but to be computed before entering here. */
  652. for (x = 0; x < s->width; x += 256) {
  653. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  654. uint16_t prcx, prcy;
  655. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  656. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  657. if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
  658. (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  659. continue;
  660. if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
  661. (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  662. continue;
  663. // check if a precinct exists
  664. prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
  665. prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
  666. precno = prcx + rlevel->num_precincts_x * prcy;
  667. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  668. if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
  669. precno, layno,
  670. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  671. qntsty->nguardbits)) < 0)
  672. return ret;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. break;
  679. default:
  680. break;
  681. }
  682. /* EOC marker reached */
  683. bytestream2_skip(&s->g, 2);
  684. return 0;
  685. }
  686. /* TIER-1 routines */
  687. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  688. int bpno, int bandno, int bpass_csty_symbol,
  689. int vert_causal_ctx_csty_symbol)
  690. {
  691. int mask = 3 << (bpno - 1), y0, x, y;
  692. for (y0 = 0; y0 < height; y0 += 4)
  693. for (x = 0; x < width; x++)
  694. for (y = y0; y < height && y < y0 + 4; y++) {
  695. if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
  696. && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  697. int flags_mask = -1;
  698. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  699. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  700. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
  701. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  702. if (bpass_csty_symbol)
  703. t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  704. else
  705. t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  706. -mask : mask;
  707. ff_jpeg2000_set_significance(t1, x, y,
  708. t1->data[y][x] < 0);
  709. }
  710. t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
  711. }
  712. }
  713. }
  714. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  715. int bpno)
  716. {
  717. int phalf, nhalf;
  718. int y0, x, y;
  719. phalf = 1 << (bpno - 1);
  720. nhalf = -phalf;
  721. for (y0 = 0; y0 < height; y0 += 4)
  722. for (x = 0; x < width; x++)
  723. for (y = y0; y < height && y < y0 + 4; y++)
  724. if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  725. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
  726. int r = ff_mqc_decode(&t1->mqc,
  727. t1->mqc.cx_states + ctxno)
  728. ? phalf : nhalf;
  729. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  730. t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
  731. }
  732. }
  733. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  734. int width, int height, int bpno, int bandno,
  735. int seg_symbols, int vert_causal_ctx_csty_symbol)
  736. {
  737. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  738. for (y0 = 0; y0 < height; y0 += 4) {
  739. for (x = 0; x < width; x++) {
  740. if (y0 + 3 < height &&
  741. !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  742. (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  743. (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  744. (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  745. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  746. continue;
  747. runlen = ff_mqc_decode(&t1->mqc,
  748. t1->mqc.cx_states + MQC_CX_UNI);
  749. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  750. t1->mqc.cx_states +
  751. MQC_CX_UNI);
  752. dec = 1;
  753. } else {
  754. runlen = 0;
  755. dec = 0;
  756. }
  757. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  758. if (!dec) {
  759. if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  760. int flags_mask = -1;
  761. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  762. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  763. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
  764. bandno));
  765. }
  766. }
  767. if (dec) {
  768. int xorbit;
  769. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  770. &xorbit);
  771. t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
  772. t1->mqc.cx_states + ctxno) ^
  773. xorbit)
  774. ? -mask : mask;
  775. ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
  776. }
  777. dec = 0;
  778. t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
  779. }
  780. }
  781. }
  782. if (seg_symbols) {
  783. int val;
  784. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  785. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  786. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  787. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  788. if (val != 0xa)
  789. av_log(s->avctx, AV_LOG_ERROR,
  790. "Segmentation symbol value incorrect\n");
  791. }
  792. }
  793. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  794. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  795. int width, int height, int bandpos)
  796. {
  797. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
  798. int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
  799. int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
  800. for (y = 0; y < height; y++)
  801. memset(t1->data[y], 0, width * sizeof(**t1->data));
  802. /* If code-block contains no compressed data: nothing to do. */
  803. if (!cblk->length)
  804. return 0;
  805. for (y = 0; y < height+2; y++)
  806. memset(t1->flags[y], 0, (width + 2)*sizeof(**t1->flags));
  807. cblk->data[cblk->length] = 0xff;
  808. cblk->data[cblk->length+1] = 0xff;
  809. ff_mqc_initdec(&t1->mqc, cblk->data);
  810. while (passno--) {
  811. switch(pass_t) {
  812. case 0:
  813. decode_sigpass(t1, width, height, bpno + 1, bandpos,
  814. bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
  815. break;
  816. case 1:
  817. decode_refpass(t1, width, height, bpno + 1);
  818. if (bpass_csty_symbol && clnpass_cnt >= 4)
  819. ff_mqc_initdec(&t1->mqc, cblk->data);
  820. break;
  821. case 2:
  822. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  823. codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
  824. clnpass_cnt = clnpass_cnt + 1;
  825. if (bpass_csty_symbol && clnpass_cnt >= 4)
  826. ff_mqc_initdec(&t1->mqc, cblk->data);
  827. break;
  828. }
  829. pass_t++;
  830. if (pass_t == 3) {
  831. bpno--;
  832. pass_t = 0;
  833. }
  834. }
  835. return 0;
  836. }
  837. /* TODO: Verify dequantization for lossless case
  838. * comp->data can be float or int
  839. * band->stepsize can be float or int
  840. * depending on the type of DWT transformation.
  841. * see ISO/IEC 15444-1:2002 A.6.1 */
  842. /* Float dequantization of a codeblock.*/
  843. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  844. Jpeg2000Component *comp,
  845. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  846. {
  847. int i, j;
  848. int w = cblk->coord[0][1] - cblk->coord[0][0];
  849. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  850. float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  851. int *src = t1->data[j];
  852. for (i = 0; i < w; ++i)
  853. datap[i] = src[i] * band->f_stepsize;
  854. }
  855. }
  856. /* Integer dequantization of a codeblock.*/
  857. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  858. Jpeg2000Component *comp,
  859. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  860. {
  861. int i, j;
  862. int w = cblk->coord[0][1] - cblk->coord[0][0];
  863. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  864. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  865. int *src = t1->data[j];
  866. for (i = 0; i < w; ++i)
  867. datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
  868. }
  869. }
  870. /* Inverse ICT parameters in float and integer.
  871. * int value = (float value) * (1<<16) */
  872. static const float f_ict_params[4] = {
  873. 1.402f,
  874. 0.34413f,
  875. 0.71414f,
  876. 1.772f
  877. };
  878. static const int i_ict_params[4] = {
  879. 91881,
  880. 22553,
  881. 46802,
  882. 116130
  883. };
  884. static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  885. {
  886. int i, csize = 1;
  887. int32_t *src[3], i0, i1, i2;
  888. float *srcf[3], i0f, i1f, i2f;
  889. for (i = 0; i < 3; i++)
  890. if (tile->codsty[0].transform == FF_DWT97)
  891. srcf[i] = tile->comp[i].f_data;
  892. else
  893. src [i] = tile->comp[i].i_data;
  894. for (i = 0; i < 2; i++)
  895. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  896. switch (tile->codsty[0].transform) {
  897. case FF_DWT97:
  898. for (i = 0; i < csize; i++) {
  899. i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
  900. i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
  901. - (f_ict_params[2] * *srcf[2]);
  902. i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
  903. *srcf[0]++ = i0f;
  904. *srcf[1]++ = i1f;
  905. *srcf[2]++ = i2f;
  906. }
  907. break;
  908. case FF_DWT97_INT:
  909. for (i = 0; i < csize; i++) {
  910. i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
  911. i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
  912. - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
  913. i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
  914. *src[0]++ = i0;
  915. *src[1]++ = i1;
  916. *src[2]++ = i2;
  917. }
  918. break;
  919. case FF_DWT53:
  920. for (i = 0; i < csize; i++) {
  921. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  922. i0 = i1 + *src[2];
  923. i2 = i1 + *src[1];
  924. *src[0]++ = i0;
  925. *src[1]++ = i1;
  926. *src[2]++ = i2;
  927. }
  928. break;
  929. }
  930. }
  931. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  932. AVFrame *picture)
  933. {
  934. int compno, reslevelno, bandno;
  935. int x, y;
  936. uint8_t *line;
  937. Jpeg2000T1Context t1;
  938. /* Loop on tile components */
  939. for (compno = 0; compno < s->ncomponents; compno++) {
  940. Jpeg2000Component *comp = tile->comp + compno;
  941. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  942. /* Loop on resolution levels */
  943. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  944. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  945. /* Loop on bands */
  946. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  947. int nb_precincts, precno;
  948. Jpeg2000Band *band = rlevel->band + bandno;
  949. int cblkno = 0, bandpos;
  950. bandpos = bandno + (reslevelno > 0);
  951. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  952. continue;
  953. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  954. /* Loop on precincts */
  955. for (precno = 0; precno < nb_precincts; precno++) {
  956. Jpeg2000Prec *prec = band->prec + precno;
  957. /* Loop on codeblocks */
  958. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  959. int x, y;
  960. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  961. decode_cblk(s, codsty, &t1, cblk,
  962. cblk->coord[0][1] - cblk->coord[0][0],
  963. cblk->coord[1][1] - cblk->coord[1][0],
  964. bandpos);
  965. /* Manage band offsets */
  966. x = cblk->coord[0][0];
  967. y = cblk->coord[1][0];
  968. if (codsty->transform == FF_DWT97)
  969. dequantization_float(x, y, cblk, comp, &t1, band);
  970. else
  971. dequantization_int(x, y, cblk, comp, &t1, band);
  972. } /* end cblk */
  973. } /*end prec */
  974. } /* end band */
  975. } /* end reslevel */
  976. /* inverse DWT */
  977. ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
  978. } /*end comp */
  979. /* inverse MCT transformation */
  980. if (tile->codsty[0].mct)
  981. mct_decode(s, tile);
  982. if (s->precision <= 8) {
  983. for (compno = 0; compno < s->ncomponents; compno++) {
  984. Jpeg2000Component *comp = tile->comp + compno;
  985. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  986. float *datap = comp->f_data;
  987. int32_t *i_datap = comp->i_data;
  988. int cbps = s->cbps[compno];
  989. int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
  990. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  991. line = picture->data[0] + y * picture->linesize[0];
  992. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  993. uint8_t *dst;
  994. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  995. dst = line + x * s->ncomponents + compno;
  996. if (codsty->transform == FF_DWT97) {
  997. for (; x < w; x += s->cdx[compno]) {
  998. int val = lrintf(*datap) + (1 << (cbps - 1));
  999. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1000. val = av_clip(val, 0, (1 << cbps) - 1);
  1001. *dst = val << (8 - cbps);
  1002. datap++;
  1003. dst += s->ncomponents;
  1004. }
  1005. } else {
  1006. for (; x < w; x += s->cdx[compno]) {
  1007. int val = *i_datap + (1 << (cbps - 1));
  1008. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1009. val = av_clip(val, 0, (1 << cbps) - 1);
  1010. *dst = val << (8 - cbps);
  1011. i_datap++;
  1012. dst += s->ncomponents;
  1013. }
  1014. }
  1015. line += picture->linesize[0];
  1016. }
  1017. }
  1018. } else {
  1019. for (compno = 0; compno < s->ncomponents; compno++) {
  1020. Jpeg2000Component *comp = tile->comp + compno;
  1021. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1022. float *datap = comp->f_data;
  1023. int32_t *i_datap = comp->i_data;
  1024. uint16_t *linel;
  1025. int cbps = s->cbps[compno];
  1026. int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
  1027. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  1028. linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
  1029. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  1030. uint16_t *dst;
  1031. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  1032. dst = linel + (x * s->ncomponents + compno);
  1033. if (codsty->transform == FF_DWT97) {
  1034. for (; x < w; x += s-> cdx[compno]) {
  1035. int val = lrintf(*datap) + (1 << (cbps - 1));
  1036. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1037. val = av_clip(val, 0, (1 << cbps) - 1);
  1038. /* align 12 bit values in little-endian mode */
  1039. *dst = val << (16 - cbps);
  1040. datap++;
  1041. dst += s->ncomponents;
  1042. }
  1043. } else {
  1044. for (; x < w; x += s-> cdx[compno]) {
  1045. int val = *i_datap + (1 << (cbps - 1));
  1046. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1047. val = av_clip(val, 0, (1 << cbps) - 1);
  1048. /* align 12 bit values in little-endian mode */
  1049. *dst = val << (16 - cbps);
  1050. i_datap++;
  1051. dst += s->ncomponents;
  1052. }
  1053. }
  1054. linel += picture->linesize[0] >> 1;
  1055. }
  1056. }
  1057. }
  1058. return 0;
  1059. }
  1060. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  1061. {
  1062. int tileno, compno;
  1063. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1064. for (compno = 0; compno < s->ncomponents; compno++) {
  1065. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1066. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1067. ff_jpeg2000_cleanup(comp, codsty);
  1068. }
  1069. av_freep(&s->tile[tileno].comp);
  1070. }
  1071. av_freep(&s->tile);
  1072. s->numXtiles = s->numYtiles = 0;
  1073. }
  1074. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1075. {
  1076. Jpeg2000CodingStyle *codsty = s->codsty;
  1077. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1078. uint8_t *properties = s->properties;
  1079. for (;;) {
  1080. int len, ret = 0;
  1081. uint16_t marker;
  1082. int oldpos;
  1083. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1084. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1085. break;
  1086. }
  1087. marker = bytestream2_get_be16u(&s->g);
  1088. oldpos = bytestream2_tell(&s->g);
  1089. if (marker == JPEG2000_SOD) {
  1090. Jpeg2000Tile *tile;
  1091. Jpeg2000TilePart *tp;
  1092. if (s->curtileno < 0) {
  1093. av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
  1094. return AVERROR_INVALIDDATA;
  1095. }
  1096. tile = s->tile + s->curtileno;
  1097. tp = tile->tile_part + tile->tp_idx;
  1098. if (tp->tp_end < s->g.buffer) {
  1099. av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
  1100. return AVERROR_INVALIDDATA;
  1101. }
  1102. bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
  1103. bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
  1104. continue;
  1105. }
  1106. if (marker == JPEG2000_EOC)
  1107. break;
  1108. len = bytestream2_get_be16(&s->g);
  1109. if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
  1110. return AVERROR_INVALIDDATA;
  1111. switch (marker) {
  1112. case JPEG2000_SIZ:
  1113. ret = get_siz(s);
  1114. if (!s->tile)
  1115. s->numXtiles = s->numYtiles = 0;
  1116. break;
  1117. case JPEG2000_COC:
  1118. ret = get_coc(s, codsty, properties);
  1119. break;
  1120. case JPEG2000_COD:
  1121. ret = get_cod(s, codsty, properties);
  1122. break;
  1123. case JPEG2000_QCC:
  1124. ret = get_qcc(s, len, qntsty, properties);
  1125. break;
  1126. case JPEG2000_QCD:
  1127. ret = get_qcd(s, len, qntsty, properties);
  1128. break;
  1129. case JPEG2000_SOT:
  1130. if (!(ret = get_sot(s, len))) {
  1131. av_assert1(s->curtileno >= 0);
  1132. codsty = s->tile[s->curtileno].codsty;
  1133. qntsty = s->tile[s->curtileno].qntsty;
  1134. properties = s->tile[s->curtileno].properties;
  1135. }
  1136. break;
  1137. case JPEG2000_COM:
  1138. // the comment is ignored
  1139. bytestream2_skip(&s->g, len - 2);
  1140. break;
  1141. case JPEG2000_TLM:
  1142. // Tile-part lengths
  1143. ret = get_tlm(s, len);
  1144. break;
  1145. default:
  1146. av_log(s->avctx, AV_LOG_ERROR,
  1147. "unsupported marker 0x%.4X at pos 0x%X\n",
  1148. marker, bytestream2_tell(&s->g) - 4);
  1149. bytestream2_skip(&s->g, len - 2);
  1150. break;
  1151. }
  1152. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  1153. av_log(s->avctx, AV_LOG_ERROR,
  1154. "error during processing marker segment %.4x\n", marker);
  1155. return ret ? ret : -1;
  1156. }
  1157. }
  1158. return 0;
  1159. }
  1160. /* Read bit stream packets --> T2 operation. */
  1161. static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
  1162. {
  1163. int ret = 0;
  1164. int tileno;
  1165. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1166. Jpeg2000Tile *tile = s->tile + tileno;
  1167. if (ret = init_tile(s, tileno))
  1168. return ret;
  1169. s->g = tile->tile_part[0].tpg;
  1170. if (ret = jpeg2000_decode_packets(s, tile))
  1171. return ret;
  1172. }
  1173. return 0;
  1174. }
  1175. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1176. {
  1177. uint32_t atom_size, atom;
  1178. int found_codestream = 0, search_range = 10;
  1179. while (!found_codestream && search_range
  1180. &&
  1181. bytestream2_get_bytes_left(&s->g) >= 8) {
  1182. atom_size = bytestream2_get_be32u(&s->g);
  1183. atom = bytestream2_get_be32u(&s->g);
  1184. if (atom == JP2_CODESTREAM) {
  1185. found_codestream = 1;
  1186. } else {
  1187. if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
  1188. return 0;
  1189. bytestream2_skipu(&s->g, atom_size - 8);
  1190. search_range--;
  1191. }
  1192. }
  1193. if (found_codestream)
  1194. return 1;
  1195. return 0;
  1196. }
  1197. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1198. int *got_frame, AVPacket *avpkt)
  1199. {
  1200. Jpeg2000DecoderContext *s = avctx->priv_data;
  1201. ThreadFrame frame = { .f = data };
  1202. AVFrame *picture = data;
  1203. int tileno, ret;
  1204. s->avctx = avctx;
  1205. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  1206. s->curtileno = -1;
  1207. // reduction factor, i.e number of resolution levels to skip
  1208. s->reduction_factor = s->lowres;
  1209. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1210. ret = AVERROR_INVALIDDATA;
  1211. goto end;
  1212. }
  1213. // check if the image is in jp2 format
  1214. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  1215. (bytestream2_get_be32u(&s->g) == 12) &&
  1216. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  1217. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  1218. if (!jp2_find_codestream(s)) {
  1219. av_log(avctx, AV_LOG_ERROR,
  1220. "Could not find Jpeg2000 codestream atom.\n");
  1221. ret = AVERROR_INVALIDDATA;
  1222. goto end;
  1223. }
  1224. } else {
  1225. bytestream2_seek(&s->g, 0, SEEK_SET);
  1226. if (bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
  1227. bytestream2_skip(&s->g, 8);
  1228. }
  1229. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  1230. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1231. ret = AVERROR_INVALIDDATA;
  1232. goto end;
  1233. }
  1234. if (ret = jpeg2000_read_main_headers(s))
  1235. goto end;
  1236. /* get picture buffer */
  1237. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1238. goto end;
  1239. picture->pict_type = AV_PICTURE_TYPE_I;
  1240. picture->key_frame = 1;
  1241. if (ret = jpeg2000_read_bitstream_packets(s))
  1242. goto end;
  1243. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1244. if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
  1245. goto end;
  1246. jpeg2000_dec_cleanup(s);
  1247. *got_frame = 1;
  1248. return bytestream2_tell(&s->g);
  1249. end:
  1250. jpeg2000_dec_cleanup(s);
  1251. return ret;
  1252. }
  1253. static void jpeg2000_init_static_data(AVCodec *codec)
  1254. {
  1255. ff_jpeg2000_init_tier1_luts();
  1256. }
  1257. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1258. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1259. static const AVOption options[] = {
  1260. { "lowres", "Lower the decoding resolution by a power of two",
  1261. OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1262. { NULL },
  1263. };
  1264. static const AVProfile profiles[] = {
  1265. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1266. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1267. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1268. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1269. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1270. { FF_PROFILE_UNKNOWN },
  1271. };
  1272. static const AVClass jpeg2000_class = {
  1273. .class_name = "jpeg2000",
  1274. .item_name = av_default_item_name,
  1275. .option = options,
  1276. .version = LIBAVUTIL_VERSION_INT,
  1277. };
  1278. AVCodec ff_jpeg2000_decoder = {
  1279. .name = "jpeg2000",
  1280. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1281. .type = AVMEDIA_TYPE_VIDEO,
  1282. .id = AV_CODEC_ID_JPEG2000,
  1283. .capabilities = CODEC_CAP_FRAME_THREADS,
  1284. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1285. .init_static_data = jpeg2000_init_static_data,
  1286. .decode = jpeg2000_decode_frame,
  1287. .priv_class = &jpeg2000_class,
  1288. .max_lowres = 5,
  1289. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1290. };