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.

1193 lines
41KB

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