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.

1340 lines
46KB

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