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.

1354 lines
47KB

  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. /* Code-block data can be empty. In that case initialize data
  553. * with 0xFFFF. */
  554. if (cblk->lengthinc > 0) {
  555. bytestream_get_buffer(&s->buf, cblk->data, cblk->lengthinc);
  556. } else {
  557. cblk->data[0] = 0xFF;
  558. cblk->data[1] = 0xFF;
  559. }
  560. cblk->length += cblk->lengthinc;
  561. cblk->lengthinc = 0;
  562. }
  563. }
  564. return 0;
  565. }
  566. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  567. {
  568. int layno, reslevelno, compno, precno, ok_reslevel;
  569. uint8_t prog_order = tile->codsty[0].prog_order;
  570. uint16_t x;
  571. uint16_t y;
  572. s->bit_index = 8;
  573. switch (prog_order) {
  574. case JPEG2000_PGOD_LRCP:
  575. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  576. ok_reslevel = 1;
  577. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  578. ok_reslevel = 0;
  579. for (compno = 0; compno < s->ncomponents; compno++) {
  580. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  581. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  582. if (reslevelno < codsty->nreslevels) {
  583. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  584. reslevelno;
  585. ok_reslevel = 1;
  586. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  587. if (jpeg2000_decode_packet(s,
  588. codsty, rlevel,
  589. precno, layno,
  590. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  591. qntsty->nguardbits))
  592. return -1;
  593. }
  594. }
  595. }
  596. }
  597. break;
  598. case JPEG2000_PGOD_CPRL:
  599. for (compno = 0; compno < s->ncomponents; compno++) {
  600. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  601. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  602. /* Set bit stream buffer address according to tile-part.
  603. * For DCinema one tile-part per component, so can be
  604. * indexed by component. */
  605. s->buf = tile->tile_part[compno].tp_start_bstrm;
  606. /* Position loop (y axis)
  607. * TODO: Automate computing of step 256.
  608. * Fixed here, but to be computed before entering here. */
  609. for (y = 0; y < s->height; y += 256) {
  610. /* Position loop (y axis)
  611. * TODO: automate computing of step 256.
  612. * Fixed here, but to be computed before entering here. */
  613. for (x = 0; x < s->width; x += 256) {
  614. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  615. uint16_t prcx, prcy;
  616. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  617. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  618. if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
  619. (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  620. continue;
  621. if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
  622. (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  623. continue;
  624. // check if a precinct exists
  625. prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
  626. prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
  627. precno = prcx + rlevel->num_precincts_x * prcy;
  628. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  629. if (jpeg2000_decode_packet(s, codsty, rlevel,
  630. precno, layno,
  631. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  632. qntsty->nguardbits))
  633. return -1;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. break;
  640. default:
  641. break;
  642. }
  643. /* EOC marker reached */
  644. s->buf += 2;
  645. return 0;
  646. }
  647. /* TIER-1 routines */
  648. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  649. int bpno, int bandno)
  650. {
  651. int mask = 3 << (bpno - 1), y0, x, y;
  652. for (y0 = 0; y0 < height; y0 += 4)
  653. for (x = 0; x < width; x++)
  654. for (y = y0; y < height && y < y0 + 4; y++)
  655. if ((t1->flags[y + 1][x + 1] & JPEG2000_T1_SIG_NB)
  656. && !(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  657. if (ff_mqc_decode(&t1->mqc,
  658. t1->mqc.cx_states +
  659. ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
  660. bandno))) {
  661. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  662. &xorbit);
  663. t1->data[y][x] =
  664. (ff_mqc_decode(&t1->mqc,
  665. t1->mqc.cx_states + ctxno) ^ xorbit)
  666. ? -mask : mask;
  667. ff_jpeg2000_set_significance(t1, x, y,
  668. t1->data[y][x] < 0);
  669. }
  670. t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
  671. }
  672. }
  673. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  674. int bpno)
  675. {
  676. int phalf, nhalf;
  677. int y0, x, y;
  678. phalf = 1 << (bpno - 1);
  679. nhalf = -phalf;
  680. for (y0 = 0; y0 < height; y0 += 4)
  681. for (x = 0; x < width; x++)
  682. for (y = y0; y < height && y < y0 + 4; y++)
  683. if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  684. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
  685. int r = ff_mqc_decode(&t1->mqc,
  686. t1->mqc.cx_states + ctxno)
  687. ? phalf : nhalf;
  688. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  689. t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
  690. }
  691. }
  692. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  693. int width, int height, int bpno, int bandno,
  694. int seg_symbols)
  695. {
  696. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  697. for (y0 = 0; y0 < height; y0 += 4)
  698. for (x = 0; x < width; x++) {
  699. if (y0 + 3 < height &&
  700. !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  701. (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  702. (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  703. (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  704. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  705. continue;
  706. runlen = ff_mqc_decode(&t1->mqc,
  707. t1->mqc.cx_states + MQC_CX_UNI);
  708. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  709. t1->mqc.cx_states +
  710. MQC_CX_UNI);
  711. dec = 1;
  712. } else {
  713. runlen = 0;
  714. dec = 0;
  715. }
  716. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  717. if (!dec) {
  718. if (!(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)))
  719. dec = ff_mqc_decode(&t1->mqc,
  720. t1->mqc.cx_states +
  721. ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
  722. bandno));
  723. }
  724. if (dec) {
  725. int xorbit;
  726. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  727. &xorbit);
  728. t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
  729. t1->mqc.cx_states + ctxno) ^
  730. xorbit)
  731. ? -mask : mask;
  732. ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
  733. }
  734. dec = 0;
  735. t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
  736. }
  737. }
  738. if (seg_symbols) {
  739. int val;
  740. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  741. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  742. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  743. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  744. if (val != 0xa)
  745. av_log(s->avctx, AV_LOG_ERROR,
  746. "Segmentation symbol value incorrect\n");
  747. }
  748. }
  749. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  750. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  751. int width, int height, int bandpos)
  752. {
  753. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
  754. for (y = 0; y < height; y++)
  755. memset(t1->data[y], 0, width * sizeof(width));
  756. /* If code-block contains no compressed data: nothing to do. */
  757. if (!cblk->length)
  758. return 0;
  759. for (y = 0; y < height + 2; y++)
  760. memset(t1->flags[y], 0, (width + 2) * sizeof(width));
  761. ff_mqc_initdec(&t1->mqc, cblk->data);
  762. cblk->data[cblk->length] = 0xff;
  763. cblk->data[cblk->length + 1] = 0xff;
  764. while (passno--) {
  765. switch (pass_t) {
  766. case 0:
  767. decode_sigpass(t1, width, height, bpno + 1, bandpos);
  768. break;
  769. case 1:
  770. decode_refpass(t1, width, height, bpno + 1);
  771. break;
  772. case 2:
  773. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  774. codsty->cblk_style & JPEG2000_CBLK_SEGSYM);
  775. break;
  776. }
  777. pass_t++;
  778. if (pass_t == 3) {
  779. bpno--;
  780. pass_t = 0;
  781. }
  782. }
  783. return 0;
  784. }
  785. /* TODO: Verify dequantization for lossless case
  786. * comp->data can be float or int
  787. * band->stepsize can be float or int
  788. * depending on the type of DWT transformation.
  789. * see ISO/IEC 15444-1:2002 A.6.1 */
  790. /* Float dequantization of a codeblock.*/
  791. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  792. Jpeg2000Component *comp,
  793. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  794. {
  795. int i, j, idx;
  796. float *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
  797. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
  798. for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
  799. idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
  800. datap[idx] = (float)(t1->data[j][i]) * ((float)band->stepsize);
  801. }
  802. return;
  803. }
  804. /* Integer dequantization of a codeblock.*/
  805. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  806. Jpeg2000Component *comp,
  807. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  808. {
  809. int i, j, idx;
  810. int32_t *datap =
  811. (int32_t *) &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
  812. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
  813. for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
  814. idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
  815. datap[idx] =
  816. ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16;
  817. }
  818. return;
  819. }
  820. /* Inverse ICT parameters in float and integer.
  821. * int value = (float value) * (1<<16) */
  822. static const float f_ict_params[4] = {
  823. 1.402f,
  824. 0.34413f,
  825. 0.71414f,
  826. 1.772f
  827. };
  828. static const int i_ict_params[4] = {
  829. 91881,
  830. 22553,
  831. 46802,
  832. 116130
  833. };
  834. static int mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  835. {
  836. int i, csize = 1;
  837. int ret = 0;
  838. int32_t *src[3], i0, i1, i2;
  839. float *srcf[3], i0f, i1f, i2f;
  840. for (i = 0; i < 3; i++)
  841. if (tile->codsty[0].transform == FF_DWT97)
  842. srcf[i] = tile->comp[i].data;
  843. else
  844. src[i] = (int32_t *)tile->comp[i].data;
  845. for (i = 0; i < 2; i++)
  846. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  847. switch (tile->codsty[0].transform) {
  848. case FF_DWT97:
  849. for (i = 0; i < csize; i++) {
  850. i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
  851. i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
  852. - (f_ict_params[2] * *srcf[2]);
  853. i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
  854. *srcf[0]++ = i0f;
  855. *srcf[1]++ = i1f;
  856. *srcf[2]++ = i2f;
  857. }
  858. break;
  859. case FF_DWT97_INT:
  860. for (i = 0; i < csize; i++) {
  861. i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
  862. i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
  863. - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
  864. i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
  865. *src[0]++ = i0;
  866. *src[1]++ = i1;
  867. *src[2]++ = i2;
  868. }
  869. break;
  870. case FF_DWT53:
  871. for (i = 0; i < csize; i++) {
  872. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  873. i0 = i1 + *src[2];
  874. i2 = i1 + *src[1];
  875. *src[0]++ = i0;
  876. *src[1]++ = i1;
  877. *src[2]++ = i2;
  878. }
  879. break;
  880. }
  881. return ret;
  882. }
  883. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  884. AVFrame *picture)
  885. {
  886. int compno, reslevelno, bandno;
  887. int x, y;
  888. uint8_t *line;
  889. Jpeg2000T1Context t1;
  890. /* Loop on tile components */
  891. for (compno = 0; compno < s->ncomponents; compno++) {
  892. Jpeg2000Component *comp = tile->comp + compno;
  893. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  894. /* Loop on resolution levels */
  895. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  896. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  897. /* Loop on bands */
  898. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  899. uint16_t nb_precincts, precno;
  900. Jpeg2000Band *band = rlevel->band + bandno;
  901. int cblkno = 0, bandpos;
  902. bandpos = bandno + (reslevelno > 0);
  903. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  904. /* Loop on precincts */
  905. for (precno = 0; precno < nb_precincts; precno++) {
  906. Jpeg2000Prec *prec = band->prec + precno;
  907. /* Loop on codeblocks */
  908. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  909. int x, y;
  910. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  911. decode_cblk(s, codsty, &t1, cblk,
  912. cblk->coord[0][1] - cblk->coord[0][0],
  913. cblk->coord[1][1] - cblk->coord[1][0],
  914. bandpos);
  915. /* Manage band offsets */
  916. x = cblk->coord[0][0];
  917. y = cblk->coord[1][0];
  918. if ((reslevelno > 0) && ((bandno + 1) & 1)) {
  919. Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1);
  920. x += pres->coord[0][1] - pres->coord[0][0];
  921. }
  922. if ((reslevelno > 0) && ((bandno + 1) & 2)) {
  923. Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1);
  924. y += pres->coord[1][1] - pres->coord[1][0];
  925. }
  926. if (s->avctx->flags & CODEC_FLAG_BITEXACT)
  927. dequantization_int(x, y, cblk, comp, &t1, band);
  928. else
  929. dequantization_float(x, y, cblk, comp, &t1, band);
  930. } /* end cblk */
  931. } /*end prec */
  932. } /* end band */
  933. } /* end reslevel */
  934. /* inverse DWT */
  935. ff_dwt_decode(&comp->dwt, comp->data);
  936. } /*end comp */
  937. /* inverse MCT transformation */
  938. if (tile->codsty[0].mct)
  939. mct_decode(s, tile);
  940. if (s->avctx->pix_fmt == AV_PIX_FMT_BGRA) // RGBA -> BGRA
  941. FFSWAP(float *, tile->comp[0].data, tile->comp[2].data);
  942. if (s->precision <= 8) {
  943. for (compno = 0; compno < s->ncomponents; compno++) {
  944. Jpeg2000Component *comp = tile->comp + compno;
  945. int32_t *datap = (int32_t *)comp->data;
  946. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  947. line = picture->data[0] + y * picture->linesize[0];
  948. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  949. uint8_t *dst;
  950. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  951. dst = line + x * s->ncomponents + compno;
  952. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
  953. *datap += 1 << (s->cbps[compno] - 1);
  954. if (*datap < 0)
  955. *datap = 0;
  956. else if (*datap >= (1 << s->cbps[compno]))
  957. *datap = (1 << s->cbps[compno]) - 1;
  958. *dst = *datap++;
  959. dst += s->ncomponents;
  960. }
  961. line += picture->linesize[0];
  962. }
  963. }
  964. } else {
  965. for (compno = 0; compno < s->ncomponents; compno++) {
  966. Jpeg2000Component *comp = tile->comp + compno;
  967. float *datap = comp->data;
  968. int32_t *i_datap = (int32_t *) comp->data;
  969. uint16_t *linel;
  970. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  971. linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
  972. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  973. uint16_t *dst;
  974. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  975. dst = linel + (x * s->ncomponents + compno);
  976. for (; x < s->avctx->width; x += s->cdx[compno]) {
  977. int16_t val;
  978. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  979. if (s->avctx->flags & CODEC_FLAG_BITEXACT)
  980. val = *i_datap + (1 << (s->cbps[compno] - 1));
  981. else
  982. val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
  983. val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
  984. /* align 12 bit values in little-endian mode */
  985. *dst = val << 4;
  986. datap++;
  987. i_datap++;
  988. dst += s->ncomponents;
  989. }
  990. linel += picture->linesize[0] >> 1;
  991. }
  992. }
  993. }
  994. return 0;
  995. }
  996. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  997. {
  998. int tileno, compno;
  999. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1000. for (compno = 0; compno < s->ncomponents; compno++) {
  1001. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1002. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1003. ff_jpeg2000_cleanup(comp, codsty);
  1004. }
  1005. av_freep(&s->tile[tileno].comp);
  1006. }
  1007. av_freep(&s->tile);
  1008. }
  1009. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1010. {
  1011. Jpeg2000CodingStyle *codsty = s->codsty;
  1012. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1013. uint8_t *properties = s->properties;
  1014. for (;;) {
  1015. int len, ret = 0;
  1016. uint16_t marker;
  1017. const uint8_t *oldbuf;
  1018. if (s->buf_end - s->buf < 2) {
  1019. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1020. break;
  1021. }
  1022. marker = bytestream_get_be16(&s->buf);
  1023. oldbuf = s->buf;
  1024. if (marker == JPEG2000_EOC)
  1025. break;
  1026. if (s->buf_end - s->buf < 2)
  1027. return AVERROR(EINVAL);
  1028. len = bytestream_get_be16(&s->buf);
  1029. switch (marker) {
  1030. case JPEG2000_SIZ:
  1031. ret = get_siz(s);
  1032. break;
  1033. case JPEG2000_COC:
  1034. ret = get_coc(s, codsty, properties);
  1035. break;
  1036. case JPEG2000_COD:
  1037. ret = get_cod(s, codsty, properties);
  1038. break;
  1039. case JPEG2000_QCC:
  1040. ret = get_qcc(s, len, qntsty, properties);
  1041. break;
  1042. case JPEG2000_QCD:
  1043. ret = get_qcd(s, len, qntsty, properties);
  1044. break;
  1045. case JPEG2000_SOT:
  1046. ret = get_sot(s, len);
  1047. break;
  1048. case JPEG2000_COM:
  1049. // the comment is ignored
  1050. s->buf += len - 2;
  1051. break;
  1052. case JPEG2000_TLM:
  1053. // Tile-part lengths
  1054. ret = get_tlm(s, len);
  1055. break;
  1056. default:
  1057. av_log(s->avctx, AV_LOG_ERROR,
  1058. "unsupported marker 0x%.4X at pos 0x%tX\n",
  1059. marker, s->buf - s->buf_start - 4);
  1060. s->buf += len - 2;
  1061. break;
  1062. }
  1063. if (((s->buf - oldbuf != len) && (marker != JPEG2000_SOT)) || ret) {
  1064. av_log(s->avctx, AV_LOG_ERROR,
  1065. "error during processing marker segment %.4x\n", marker);
  1066. return ret ? ret : -1;
  1067. }
  1068. }
  1069. return 0;
  1070. }
  1071. /* Read bit stream packets --> T2 operation. */
  1072. static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
  1073. {
  1074. int ret = 0;
  1075. Jpeg2000Tile *tile = s->tile + s->curtileno;
  1076. if (ret = init_tile(s, s->curtileno))
  1077. return ret;
  1078. if (ret = jpeg2000_decode_packets(s, tile))
  1079. return ret;
  1080. return 0;
  1081. }
  1082. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1083. {
  1084. int32_t atom_size;
  1085. int found_codestream = 0, search_range = 10;
  1086. // Skip JPEG 2000 signature atom.
  1087. s->buf += 12;
  1088. while (!found_codestream && search_range) {
  1089. atom_size = AV_RB32(s->buf);
  1090. if (AV_RB32(s->buf + 4) == JP2_CODESTREAM) {
  1091. found_codestream = 1;
  1092. s->buf += 8;
  1093. } else {
  1094. s->buf += atom_size;
  1095. search_range--;
  1096. }
  1097. }
  1098. if (found_codestream)
  1099. return 1;
  1100. return 0;
  1101. }
  1102. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1103. int *got_frame, AVPacket *avpkt)
  1104. {
  1105. Jpeg2000DecoderContext *s = avctx->priv_data;
  1106. ThreadFrame frame = { .f = data };
  1107. AVFrame *picture = data;
  1108. int tileno, ret;
  1109. s->avctx = avctx;
  1110. s->buf = s->buf_start = avpkt->data;
  1111. s->buf_end = s->buf_start + avpkt->size;
  1112. s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
  1113. // reduction factor, i.e number of resolution levels to skip
  1114. s->reduction_factor = s->lowres;
  1115. if (s->buf_end - s->buf < 2)
  1116. return AVERROR(EINVAL);
  1117. // check if the image is in jp2 format
  1118. if ((AV_RB32(s->buf) == 12) &&
  1119. (AV_RB32(s->buf + 4) == JP2_SIG_TYPE) &&
  1120. (AV_RB32(s->buf + 8) == JP2_SIG_VALUE)) {
  1121. if (!jp2_find_codestream(s)) {
  1122. av_log(avctx, AV_LOG_ERROR,
  1123. "couldn't find jpeg2k codestream atom\n");
  1124. return -1;
  1125. }
  1126. }
  1127. if (bytestream_get_be16(&s->buf) != JPEG2000_SOC) {
  1128. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1129. return -1;
  1130. }
  1131. if (ret = jpeg2000_read_main_headers(s))
  1132. goto end;
  1133. /* get picture buffer */
  1134. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
  1135. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
  1136. goto end;
  1137. }
  1138. picture->pict_type = AV_PICTURE_TYPE_I;
  1139. picture->key_frame = 1;
  1140. if (ret = jpeg2000_read_bitstream_packets(s))
  1141. goto end;
  1142. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1143. if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
  1144. goto end;
  1145. *got_frame = 1;
  1146. end:
  1147. jpeg2000_dec_cleanup(s);
  1148. return ret ? ret : s->buf - s->buf_start;
  1149. }
  1150. static void jpeg2000_init_static_data(AVCodec *codec)
  1151. {
  1152. ff_jpeg2000_init_tier1_luts();
  1153. }
  1154. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1155. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1156. static const AVOption options[] = {
  1157. { "lowres", "Lower the decoding resolution by a power of two",
  1158. OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1159. { NULL },
  1160. };
  1161. static const AVProfile profiles[] = {
  1162. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1163. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1164. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1165. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1166. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1167. { FF_PROFILE_UNKNOWN },
  1168. };
  1169. static const AVClass class = {
  1170. .class_name = "jpeg2000",
  1171. .item_name = av_default_item_name,
  1172. .option = options,
  1173. .version = LIBAVUTIL_VERSION_INT,
  1174. };
  1175. AVCodec ff_jpeg2000_decoder = {
  1176. .name = "jpeg2000",
  1177. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1178. .type = AVMEDIA_TYPE_VIDEO,
  1179. .id = AV_CODEC_ID_JPEG2000,
  1180. .capabilities = CODEC_CAP_FRAME_THREADS,
  1181. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1182. .init_static_data = jpeg2000_init_static_data,
  1183. .decode = jpeg2000_decode_frame,
  1184. .priv_class = &class,
  1185. .pix_fmts = (enum AVPixelFormat[]) { AV_PIX_FMT_XYZ12,
  1186. AV_PIX_FMT_GRAY8,
  1187. -1 },
  1188. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1189. };