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.

1281 lines
45KB

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