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.

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