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.

1268 lines
44KB

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