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.

1196 lines
42KB

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