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.

1265 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->f_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 = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
  736. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
  737. for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
  738. idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
  739. datap[idx] =
  740. ((int32_t)(t1->data[j][i]) * band->i_stepsize + (1 << 15)) >> 16;
  741. }
  742. }
  743. /* Inverse ICT parameters in float and integer.
  744. * int value = (float value) * (1<<16) */
  745. static const float f_ict_params[4] = {
  746. 1.402f,
  747. 0.34413f,
  748. 0.71414f,
  749. 1.772f
  750. };
  751. static const int i_ict_params[4] = {
  752. 91881,
  753. 22553,
  754. 46802,
  755. 116130
  756. };
  757. static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  758. {
  759. int i, csize = 1;
  760. int32_t *src[3], i0, i1, i2;
  761. float *srcf[3], i0f, i1f, i2f;
  762. for (i = 0; i < 3; i++)
  763. if (tile->codsty[0].transform == FF_DWT97)
  764. srcf[i] = tile->comp[i].f_data;
  765. else
  766. src [i] = tile->comp[i].i_data;
  767. for (i = 0; i < 2; i++)
  768. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  769. switch (tile->codsty[0].transform) {
  770. case FF_DWT97:
  771. for (i = 0; i < csize; i++) {
  772. i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
  773. i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
  774. - (f_ict_params[2] * *srcf[2]);
  775. i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
  776. *srcf[0]++ = i0f;
  777. *srcf[1]++ = i1f;
  778. *srcf[2]++ = i2f;
  779. }
  780. break;
  781. case FF_DWT97_INT:
  782. for (i = 0; i < csize; i++) {
  783. i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
  784. i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
  785. - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
  786. i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
  787. *src[0]++ = i0;
  788. *src[1]++ = i1;
  789. *src[2]++ = i2;
  790. }
  791. break;
  792. case FF_DWT53:
  793. for (i = 0; i < csize; i++) {
  794. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  795. i0 = i1 + *src[2];
  796. i2 = i1 + *src[1];
  797. *src[0]++ = i0;
  798. *src[1]++ = i1;
  799. *src[2]++ = i2;
  800. }
  801. }
  802. }
  803. static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  804. {
  805. int compno, reslevelno, bandno;
  806. int x, y;
  807. uint8_t *line;
  808. Jpeg2000T1Context t1;
  809. /* Loop on tile components */
  810. for (compno = 0; compno < s->ncomponents; compno++) {
  811. Jpeg2000Component *comp = tile->comp + compno;
  812. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  813. /* Loop on resolution levels */
  814. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  815. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  816. /* Loop on bands */
  817. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  818. int nb_precincts, precno;
  819. Jpeg2000Band *band = rlevel->band + bandno;
  820. int cblkno=0, bandpos;
  821. bandpos = bandno + (reslevelno > 0);
  822. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  823. continue;
  824. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  825. /* Loop on precincts */
  826. for (precno = 0; precno < nb_precincts; precno++) {
  827. Jpeg2000Prec *prec = band->prec + precno;
  828. /* Loop on codeblocks */
  829. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  830. int x, y;
  831. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  832. decode_cblk(s, codsty, &t1, cblk,
  833. cblk->coord[0][1] - cblk->coord[0][0],
  834. cblk->coord[1][1] - cblk->coord[1][0],
  835. bandpos);
  836. /* Manage band offsets */
  837. x = cblk->coord[0][0];
  838. y = cblk->coord[1][0];
  839. if (codsty->transform == FF_DWT97)
  840. dequantization_float(x, y, cblk, comp, &t1, band);
  841. else
  842. dequantization_int(x, y, cblk, comp, &t1, band);
  843. } /* end cblk */
  844. } /*end prec */
  845. } /* end band */
  846. } /* end reslevel */
  847. ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
  848. } /*end comp */
  849. /* inverse MCT transformation */
  850. if (tile->codsty[0].mct)
  851. mct_decode(s, tile);
  852. if (s->precision <= 8) {
  853. for (compno = 0; compno < s->ncomponents; compno++) {
  854. Jpeg2000Component *comp = tile->comp + compno;
  855. float *datap = comp->f_data;
  856. int32_t *i_datap = comp->i_data;
  857. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  858. line = s->picture->data[0] + y * s->picture->linesize[0];
  859. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  860. uint8_t *dst;
  861. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  862. dst = line + x * s->ncomponents + compno;
  863. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
  864. int val;
  865. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  866. if (tile->codsty->transform == FF_DWT97)
  867. val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
  868. else
  869. val = *i_datap + (1 << (s->cbps[compno] - 1));
  870. val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
  871. *dst = val << (8 - s->cbps[compno]);
  872. datap++;
  873. i_datap++;
  874. dst += s->ncomponents;
  875. }
  876. line += s->picture->linesize[0];
  877. }
  878. }
  879. } else {
  880. for (compno = 0; compno < s->ncomponents; compno++) {
  881. Jpeg2000Component *comp = tile->comp + compno;
  882. float *datap = comp->f_data;
  883. int32_t *i_datap = comp->i_data;
  884. uint16_t *linel;
  885. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  886. linel = (uint16_t*)s->picture->data[0] + y * (s->picture->linesize[0] >> 1);
  887. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  888. uint16_t *dst;
  889. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  890. dst = linel + (x * s->ncomponents + compno);
  891. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
  892. int val;
  893. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  894. if (tile->codsty->transform == FF_DWT97)
  895. val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
  896. else
  897. val = *i_datap + (1 << (s->cbps[compno] - 1));
  898. val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
  899. /* align 12 bit values in little-endian mode */
  900. *dst = val << (16 - s->cbps[compno]);
  901. datap++;
  902. i_datap++;
  903. dst += s->ncomponents;
  904. }
  905. linel += s->picture->linesize[0]>>1;
  906. }
  907. }
  908. }
  909. return 0;
  910. }
  911. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  912. {
  913. int tileno, compno;
  914. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  915. for (compno = 0; compno < s->ncomponents; compno++) {
  916. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  917. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  918. ff_j2k_cleanup(comp, codsty);
  919. }
  920. av_freep(&s->tile[tileno].comp);
  921. }
  922. av_freep(&s->tile);
  923. }
  924. static int decode_codestream(Jpeg2000DecoderContext *s)
  925. {
  926. Jpeg2000CodingStyle *codsty = s->codsty;
  927. Jpeg2000QuantStyle *qntsty = s->qntsty;
  928. uint8_t *properties = s->properties;
  929. for (;;) {
  930. int oldpos, marker, len, ret = 0;
  931. if (bytestream2_get_bytes_left(&s->g) < 2) {
  932. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  933. break;
  934. }
  935. marker = bytestream2_get_be16u(&s->g);
  936. av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
  937. oldpos = bytestream2_tell(&s->g);
  938. if (marker == JPEG2000_SOD) {
  939. Jpeg2000Tile *tile = s->tile + s->curtileno;
  940. if (ret = init_tile(s, s->curtileno)) {
  941. av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
  942. return ret;
  943. }
  944. if (ret = jpeg2000_decode_packets(s, tile)) {
  945. av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
  946. return ret;
  947. }
  948. continue;
  949. }
  950. if (marker == JPEG2000_EOC)
  951. break;
  952. if (bytestream2_get_bytes_left(&s->g) < 2)
  953. return AVERROR(EINVAL);
  954. len = bytestream2_get_be16u(&s->g);
  955. switch (marker) {
  956. case JPEG2000_SIZ:
  957. ret = get_siz(s);
  958. if (!s->tile)
  959. s->numXtiles = s->numYtiles = 0;
  960. break;
  961. case JPEG2000_COC:
  962. ret = get_coc(s, codsty, properties);
  963. break;
  964. case JPEG2000_COD:
  965. ret = get_cod(s, codsty, properties);
  966. break;
  967. case JPEG2000_QCC:
  968. ret = get_qcc(s, len, qntsty, properties);
  969. break;
  970. case JPEG2000_QCD:
  971. ret = get_qcd(s, len, qntsty, properties);
  972. break;
  973. case JPEG2000_SOT:
  974. if (!(ret = get_sot(s))) {
  975. codsty = s->tile[s->curtileno].codsty;
  976. qntsty = s->tile[s->curtileno].qntsty;
  977. properties = s->tile[s->curtileno].properties;
  978. }
  979. break;
  980. case JPEG2000_COM:
  981. // the comment is ignored
  982. bytestream2_skip(&s->g, len - 2);
  983. break;
  984. case JPEG2000_TLM:
  985. // Tile-part lengths
  986. ret = get_tlm(s, len);
  987. break;
  988. default:
  989. av_log(s->avctx, AV_LOG_ERROR,
  990. "unsupported marker 0x%.4X at pos 0x%x\n",
  991. marker, bytestream2_tell(&s->g) - 4);
  992. bytestream2_skip(&s->g, len - 2);
  993. break;
  994. }
  995. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  996. av_log(s->avctx, AV_LOG_ERROR,
  997. "error during processing marker segment %.4x\n", marker);
  998. return ret ? ret : -1;
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1004. {
  1005. uint32_t atom_size, atom;
  1006. int found_codestream = 0, search_range = 10;
  1007. while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
  1008. atom_size = bytestream2_get_be32u(&s->g);
  1009. atom = bytestream2_get_be32u(&s->g);
  1010. if (atom == JP2_CODESTREAM) {
  1011. found_codestream = 1;
  1012. } else {
  1013. if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
  1014. return 0;
  1015. bytestream2_skipu(&s->g, atom_size - 8);
  1016. search_range--;
  1017. }
  1018. }
  1019. if (found_codestream)
  1020. return 1;
  1021. return 0;
  1022. }
  1023. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1024. int *got_frame, AVPacket *avpkt)
  1025. {
  1026. Jpeg2000DecoderContext *s = avctx->priv_data;
  1027. AVFrame *picture = data;
  1028. int tileno, ret;
  1029. s->picture = picture;
  1030. s->avctx = avctx;
  1031. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  1032. s->curtileno = -1;
  1033. // reduction factor, i.e number of resolution levels to skip
  1034. s->reduction_factor = avctx->lowres;
  1035. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1036. ret = AVERROR(EINVAL);
  1037. goto err_out;
  1038. }
  1039. // check if the image is in jp2 format
  1040. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  1041. (bytestream2_get_be32u(&s->g) == 12) &&
  1042. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  1043. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  1044. if (!jp2_find_codestream(s)) {
  1045. av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
  1046. ret = -1;
  1047. goto err_out;
  1048. }
  1049. } else {
  1050. bytestream2_seek(&s->g, 0, SEEK_SET);
  1051. }
  1052. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  1053. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1054. ret = -1;
  1055. goto err_out;
  1056. }
  1057. if (ret = decode_codestream(s))
  1058. goto err_out;
  1059. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1060. if (ret = decode_tile(s, s->tile + tileno))
  1061. goto err_out;
  1062. jpeg2000_dec_cleanup(s);
  1063. *got_frame = 1;
  1064. return bytestream2_tell(&s->g);
  1065. err_out:
  1066. jpeg2000_dec_cleanup(s);
  1067. return ret;
  1068. }
  1069. static void jpeg2000_init_static_data(AVCodec *codec)
  1070. {
  1071. ff_jpeg2000_init_tier1_luts();
  1072. }
  1073. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1074. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1075. static const AVOption options[] = {
  1076. { "lowres", "Lower the decoding resolution by a power of two",
  1077. OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1078. { NULL },
  1079. };
  1080. static const AVProfile profiles[] = {
  1081. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1082. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1083. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1084. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1085. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1086. { FF_PROFILE_UNKNOWN },
  1087. };
  1088. static const AVClass class = {
  1089. .class_name = "j2k",
  1090. .item_name = av_default_item_name,
  1091. .option = options,
  1092. .version = LIBAVUTIL_VERSION_INT,
  1093. };
  1094. AVCodec ff_j2k_decoder = {
  1095. .name = "j2k",
  1096. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1097. .type = AVMEDIA_TYPE_VIDEO,
  1098. .id = AV_CODEC_ID_JPEG2000,
  1099. .capabilities = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
  1100. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1101. .init_static_data = jpeg2000_init_static_data,
  1102. .decode = jpeg2000_decode_frame,
  1103. .priv_class = &class,
  1104. .max_lowres = 5,
  1105. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1106. };