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.

1359 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. /* nreslevels = number of resolution levels
  224. = number of decomposition level +1 */
  225. c->nreslevels = bytestream_get_byte(&s->buf) + 1;
  226. if (c->nreslevels > JPEG2000_MAX_RESLEVELS)
  227. return AVERROR_INVALIDDATA;
  228. /* compute number of resolution levels to decode */
  229. if (c->nreslevels < s->reduction_factor)
  230. c->nreslevels2decode = 1;
  231. else
  232. c->nreslevels2decode = c->nreslevels - s->reduction_factor;
  233. c->log2_cblk_width = bytestream_get_byte(&s->buf) + 2; // cblk width
  234. c->log2_cblk_height = bytestream_get_byte(&s->buf) + 2; // cblk height
  235. c->cblk_style = bytestream_get_byte(&s->buf);
  236. if (c->cblk_style != 0) { // cblk style
  237. av_log(s->avctx, AV_LOG_ERROR, "no extra cblk styles supported\n");
  238. return -1;
  239. }
  240. c->transform = bytestream_get_byte(&s->buf); // DWT transformation type
  241. /* set integer 9/7 DWT in case of BITEXACT flag */
  242. if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
  243. c->transform = FF_DWT97_INT;
  244. if (c->csty & JPEG2000_CSTY_PREC) {
  245. int i;
  246. for (i = 0; i < c->nreslevels; i++) {
  247. byte = bytestream_get_byte(&s->buf);
  248. c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
  249. c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
  250. }
  251. }
  252. return 0;
  253. }
  254. /* get coding parameters for a particular tile or whole image*/
  255. static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  256. uint8_t *properties)
  257. {
  258. Jpeg2000CodingStyle tmp;
  259. int compno;
  260. if (s->buf_end - s->buf < 5)
  261. return AVERROR(EINVAL);
  262. tmp.log2_prec_width =
  263. tmp.log2_prec_height = 15;
  264. tmp.csty = bytestream_get_byte(&s->buf);
  265. // get progression order
  266. tmp.prog_order = bytestream_get_byte(&s->buf);
  267. tmp.nlayers = bytestream_get_be16(&s->buf);
  268. tmp.mct = bytestream_get_byte(&s->buf); // multiple component transformation
  269. get_cox(s, &tmp);
  270. for (compno = 0; compno < s->ncomponents; compno++)
  271. if (!(properties[compno] & HAD_COC))
  272. memcpy(c + compno, &tmp, sizeof(tmp));
  273. return 0;
  274. }
  275. /* Get coding parameters for a component in the whole image or a
  276. * particular tile. */
  277. static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  278. uint8_t *properties)
  279. {
  280. int compno;
  281. if (s->buf_end - s->buf < 2)
  282. return AVERROR(EINVAL);
  283. compno = bytestream_get_byte(&s->buf);
  284. c += compno;
  285. c->csty = bytestream_get_byte(&s->buf);
  286. get_cox(s, c);
  287. properties[compno] |= HAD_COC;
  288. return 0;
  289. }
  290. /* Get common part for QCD and QCC segments. */
  291. static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
  292. {
  293. int i, x;
  294. if (s->buf_end - s->buf < 1)
  295. return AVERROR(EINVAL);
  296. x = bytestream_get_byte(&s->buf); // Sqcd
  297. q->nguardbits = x >> 5;
  298. q->quantsty = x & 0x1f;
  299. if (q->quantsty == JPEG2000_QSTY_NONE) {
  300. n -= 3;
  301. if (s->buf_end - s->buf < n)
  302. return AVERROR(EINVAL);
  303. for (i = 0; i < n; i++)
  304. q->expn[i] = bytestream_get_byte(&s->buf) >> 3;
  305. } else if (q->quantsty == JPEG2000_QSTY_SI) {
  306. if (s->buf_end - s->buf < 2)
  307. return AVERROR(EINVAL);
  308. x = bytestream_get_be16(&s->buf);
  309. q->expn[0] = x >> 11;
  310. q->mant[0] = x & 0x7ff;
  311. for (i = 1; i < 32 * 3; i++) {
  312. int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
  313. q->expn[i] = curexpn;
  314. q->mant[i] = q->mant[0];
  315. }
  316. } else {
  317. n = (n - 3) >> 1;
  318. if (s->buf_end - s->buf < n)
  319. return AVERROR(EINVAL);
  320. for (i = 0; i < n; i++) {
  321. x = bytestream_get_be16(&s->buf);
  322. q->expn[i] = x >> 11;
  323. q->mant[i] = x & 0x7ff;
  324. }
  325. }
  326. return 0;
  327. }
  328. /* Get quantization parameters for a particular tile or a whole image. */
  329. static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  330. uint8_t *properties)
  331. {
  332. Jpeg2000QuantStyle tmp;
  333. int compno;
  334. if (get_qcx(s, n, &tmp))
  335. return -1;
  336. for (compno = 0; compno < s->ncomponents; compno++)
  337. if (!(properties[compno] & HAD_QCC))
  338. memcpy(q + compno, &tmp, sizeof(tmp));
  339. return 0;
  340. }
  341. /* Get quantization parameters for a component in the whole image
  342. * on in a particular tile. */
  343. static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  344. uint8_t *properties)
  345. {
  346. int compno;
  347. if (s->buf_end - s->buf < 1)
  348. return AVERROR(EINVAL);
  349. compno = bytestream_get_byte(&s->buf);
  350. properties[compno] |= HAD_QCC;
  351. return get_qcx(s, n - 1, q + compno);
  352. }
  353. /* Get start of tile segment. */
  354. static uint8_t get_sot(Jpeg2000DecoderContext *s, int n)
  355. {
  356. Jpeg2000TilePart *tp;
  357. uint16_t Isot;
  358. uint32_t Psot;
  359. uint8_t TPsot;
  360. if (s->buf_end - s->buf < 4)
  361. return AVERROR(EINVAL);
  362. Isot = bytestream_get_be16(&s->buf); // Isot
  363. if (Isot) {
  364. av_log(s->avctx, AV_LOG_ERROR,
  365. "Not a DCINEMA JP2K file: more than one tile\n");
  366. return -1;
  367. }
  368. Psot = bytestream_get_be32(&s->buf); // Psot
  369. TPsot = bytestream_get_byte(&s->buf); // TPsot
  370. /* Read TNSot but not used */
  371. bytestream_get_byte(&s->buf); // TNsot
  372. tp = s->tile[s->curtileno].tile_part + TPsot;
  373. tp->tile_index = Isot;
  374. tp->tp_len = Psot;
  375. tp->tp_idx = TPsot;
  376. /* Start of bit stream. Pointer to SOD marker
  377. * Check SOD marker is present. */
  378. if (JPEG2000_SOD == bytestream_get_be16(&s->buf))
  379. tp->tp_start_bstrm = s->buf;
  380. else {
  381. av_log(s->avctx, AV_LOG_ERROR, "SOD marker not found \n");
  382. return -1;
  383. }
  384. /* End address of bit stream =
  385. * start address + (Psot - size of SOT HEADER(n)
  386. * - size of SOT MARKER(2) - size of SOD marker(2) */
  387. tp->tp_end_bstrm = s->buf + (tp->tp_len - n - 4);
  388. // set buffer pointer to end of tile part header
  389. s->buf = tp->tp_end_bstrm;
  390. return 0;
  391. }
  392. /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
  393. * Used to know the number of tile parts and lengths.
  394. * There may be multiple TLMs in the header.
  395. * TODO: The function is not used for tile-parts management, nor anywhere else.
  396. * It can be useful to allocate memory for tile parts, before managing the SOT
  397. * markers. Parsing the TLM header is needed to increment the input header
  398. * buffer.
  399. * This marker is mandatory for DCI. */
  400. static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
  401. {
  402. uint8_t Stlm, ST, SP, tile_tlm, i;
  403. bytestream_get_byte(&s->buf); /* Ztlm: skipped */
  404. Stlm = bytestream_get_byte(&s->buf);
  405. // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  406. ST = (Stlm >> 4) & 0x03;
  407. // TODO: Manage case of ST = 0b11 --> raise error
  408. SP = (Stlm >> 6) & 0x01;
  409. tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
  410. for (i = 0; i < tile_tlm; i++) {
  411. switch (ST) {
  412. case 0:
  413. break;
  414. case 1:
  415. bytestream_get_byte(&s->buf);
  416. break;
  417. case 2:
  418. bytestream_get_be16(&s->buf);
  419. break;
  420. case 3:
  421. bytestream_get_be32(&s->buf);
  422. break;
  423. }
  424. if (SP == 0) {
  425. bytestream_get_be16(&s->buf);
  426. } else {
  427. bytestream_get_be32(&s->buf);
  428. }
  429. }
  430. return 0;
  431. }
  432. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  433. {
  434. int compno;
  435. int tilex = tileno % s->numXtiles;
  436. int tiley = tileno / s->numXtiles;
  437. Jpeg2000Tile *tile = s->tile + tileno;
  438. Jpeg2000CodingStyle *codsty;
  439. Jpeg2000QuantStyle *qntsty;
  440. if (!tile->comp)
  441. return AVERROR(ENOMEM);
  442. /* copy codsty, qnsty to tile. TODO: Is it the best way?
  443. * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle
  444. * and Jpeg2000QuantStyle */
  445. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(*codsty));
  446. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(*qntsty));
  447. for (compno = 0; compno < s->ncomponents; compno++) {
  448. Jpeg2000Component *comp = tile->comp + compno;
  449. int ret; // global bandno
  450. codsty = tile->codsty + compno;
  451. qntsty = tile->qntsty + compno;
  452. comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
  453. comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
  454. comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
  455. comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
  456. // FIXME: add a dcinema profile check ?
  457. // value is guaranteed by profile (orig=0, 1 tile)
  458. comp->coord[0][0] = 0;
  459. comp->coord[0][1] = s->avctx->width;
  460. comp->coord[1][0] = 0;
  461. comp->coord[1][1] = s->avctx->height;
  462. if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  463. s->cbps[compno], s->cdx[compno],
  464. s->cdy[compno], s->avctx))
  465. return ret;
  466. }
  467. return 0;
  468. }
  469. /* Read the number of coding passes. */
  470. static int getnpasses(Jpeg2000DecoderContext *s)
  471. {
  472. int num;
  473. if (!get_bits(s, 1))
  474. return 1;
  475. if (!get_bits(s, 1))
  476. return 2;
  477. if ((num = get_bits(s, 2)) != 3)
  478. return num < 0 ? num : 3 + num;
  479. if ((num = get_bits(s, 5)) != 31)
  480. return num < 0 ? num : 6 + num;
  481. num = get_bits(s, 7);
  482. return num < 0 ? num : 37 + num;
  483. }
  484. static int getlblockinc(Jpeg2000DecoderContext *s)
  485. {
  486. int res = 0, ret;
  487. while (ret = get_bits(s, 1)) {
  488. if (ret < 0)
  489. return ret;
  490. res++;
  491. }
  492. return res;
  493. }
  494. static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
  495. Jpeg2000CodingStyle *codsty,
  496. Jpeg2000ResLevel *rlevel, int precno,
  497. int layno, uint8_t *expn, int numgbits)
  498. {
  499. int bandno, cblkno, ret, nb_code_blocks;
  500. if (!(ret = get_bits(s, 1))) {
  501. jpeg2000_flush(s);
  502. return 0;
  503. } else if (ret < 0)
  504. return ret;
  505. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  506. Jpeg2000Band *band = rlevel->band + bandno;
  507. Jpeg2000Prec *prec = band->prec + precno;
  508. if (band->coord[0][0] == band->coord[0][1] ||
  509. band->coord[1][0] == band->coord[1][1])
  510. continue;
  511. prec->yi0 = 0;
  512. prec->xi0 = 0;
  513. nb_code_blocks = prec->nb_codeblocks_height *
  514. prec->nb_codeblocks_width;
  515. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  516. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  517. int incl, newpasses, llen;
  518. if (cblk->npasses)
  519. incl = get_bits(s, 1);
  520. else
  521. incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
  522. if (!incl)
  523. continue;
  524. else if (incl < 0)
  525. return incl;
  526. if (!cblk->npasses)
  527. cblk->nonzerobits = expn[bandno] + numgbits - 1 -
  528. tag_tree_decode(s, prec->zerobits + cblkno,
  529. 100);
  530. if ((newpasses = getnpasses(s)) < 0)
  531. return newpasses;
  532. if ((llen = getlblockinc(s)) < 0)
  533. return llen;
  534. cblk->lblock += llen;
  535. if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
  536. return ret;
  537. cblk->lengthinc = ret;
  538. cblk->npasses += newpasses;
  539. }
  540. }
  541. jpeg2000_flush(s);
  542. if (codsty->csty & JPEG2000_CSTY_EPH) {
  543. if (AV_RB16(s->buf) == JPEG2000_EPH)
  544. s->buf += 2;
  545. else
  546. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
  547. }
  548. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  549. Jpeg2000Band *band = rlevel->band + bandno;
  550. Jpeg2000Prec *prec = band->prec + precno;
  551. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  552. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  553. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  554. if (s->buf_end - s->buf < cblk->lengthinc)
  555. return AVERROR(EINVAL);
  556. /* Code-block data can be empty. In that case initialize data
  557. * with 0xFFFF. */
  558. if (cblk->lengthinc > 0) {
  559. bytestream_get_buffer(&s->buf, cblk->data, cblk->lengthinc);
  560. } else {
  561. cblk->data[0] = 0xFF;
  562. cblk->data[1] = 0xFF;
  563. }
  564. cblk->length += cblk->lengthinc;
  565. cblk->lengthinc = 0;
  566. }
  567. }
  568. return 0;
  569. }
  570. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  571. {
  572. int layno, reslevelno, compno, precno, ok_reslevel;
  573. uint8_t prog_order = tile->codsty[0].prog_order;
  574. uint16_t x;
  575. uint16_t y;
  576. s->bit_index = 8;
  577. switch (prog_order) {
  578. case JPEG2000_PGOD_LRCP:
  579. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  580. ok_reslevel = 1;
  581. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  582. ok_reslevel = 0;
  583. for (compno = 0; compno < s->ncomponents; compno++) {
  584. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  585. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  586. if (reslevelno < codsty->nreslevels) {
  587. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  588. reslevelno;
  589. ok_reslevel = 1;
  590. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  591. if (jpeg2000_decode_packet(s,
  592. codsty, rlevel,
  593. precno, layno,
  594. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  595. qntsty->nguardbits))
  596. return -1;
  597. }
  598. }
  599. }
  600. }
  601. break;
  602. case JPEG2000_PGOD_CPRL:
  603. for (compno = 0; compno < s->ncomponents; compno++) {
  604. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  605. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  606. /* Set bit stream buffer address according to tile-part.
  607. * For DCinema one tile-part per component, so can be
  608. * indexed by component. */
  609. s->buf = tile->tile_part[compno].tp_start_bstrm;
  610. /* Position loop (y axis)
  611. * TODO: Automate computing of step 256.
  612. * Fixed here, but to be computed before entering here. */
  613. for (y = 0; y < s->height; y += 256) {
  614. /* Position loop (y axis)
  615. * TODO: automate computing of step 256.
  616. * Fixed here, but to be computed before entering here. */
  617. for (x = 0; x < s->width; x += 256) {
  618. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  619. uint16_t prcx, prcy;
  620. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  621. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  622. if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
  623. (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  624. continue;
  625. if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
  626. (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  627. continue;
  628. // check if a precinct exists
  629. prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
  630. prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
  631. precno = prcx + rlevel->num_precincts_x * prcy;
  632. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  633. if (jpeg2000_decode_packet(s, codsty, rlevel,
  634. precno, layno,
  635. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  636. qntsty->nguardbits))
  637. return -1;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. break;
  644. default:
  645. break;
  646. }
  647. /* EOC marker reached */
  648. s->buf += 2;
  649. return 0;
  650. }
  651. /* TIER-1 routines */
  652. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  653. int bpno, int bandno)
  654. {
  655. int mask = 3 << (bpno - 1), y0, x, y;
  656. for (y0 = 0; y0 < height; y0 += 4)
  657. for (x = 0; x < width; x++)
  658. for (y = y0; y < height && y < y0 + 4; y++)
  659. if ((t1->flags[y + 1][x + 1] & JPEG2000_T1_SIG_NB)
  660. && !(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  661. if (ff_mqc_decode(&t1->mqc,
  662. t1->mqc.cx_states +
  663. ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
  664. bandno))) {
  665. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  666. &xorbit);
  667. t1->data[y][x] =
  668. (ff_mqc_decode(&t1->mqc,
  669. t1->mqc.cx_states + ctxno) ^ xorbit)
  670. ? -mask : mask;
  671. ff_jpeg2000_set_significance(t1, x, y,
  672. t1->data[y][x] < 0);
  673. }
  674. t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
  675. }
  676. }
  677. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  678. int bpno)
  679. {
  680. int phalf, nhalf;
  681. int y0, x, y;
  682. phalf = 1 << (bpno - 1);
  683. nhalf = -phalf;
  684. for (y0 = 0; y0 < height; y0 += 4)
  685. for (x = 0; x < width; x++)
  686. for (y = y0; y < height && y < y0 + 4; y++)
  687. if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  688. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
  689. int r = ff_mqc_decode(&t1->mqc,
  690. t1->mqc.cx_states + ctxno)
  691. ? phalf : nhalf;
  692. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  693. t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
  694. }
  695. }
  696. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  697. int width, int height, int bpno, int bandno,
  698. int seg_symbols)
  699. {
  700. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  701. for (y0 = 0; y0 < height; y0 += 4)
  702. for (x = 0; x < width; x++) {
  703. if (y0 + 3 < height &&
  704. !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  705. (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  706. (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  707. (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  708. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  709. continue;
  710. runlen = ff_mqc_decode(&t1->mqc,
  711. t1->mqc.cx_states + MQC_CX_UNI);
  712. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  713. t1->mqc.cx_states +
  714. MQC_CX_UNI);
  715. dec = 1;
  716. } else {
  717. runlen = 0;
  718. dec = 0;
  719. }
  720. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  721. if (!dec) {
  722. if (!(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)))
  723. dec = ff_mqc_decode(&t1->mqc,
  724. t1->mqc.cx_states +
  725. ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
  726. bandno));
  727. }
  728. if (dec) {
  729. int xorbit;
  730. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  731. &xorbit);
  732. t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
  733. t1->mqc.cx_states + ctxno) ^
  734. xorbit)
  735. ? -mask : mask;
  736. ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
  737. }
  738. dec = 0;
  739. t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
  740. }
  741. }
  742. if (seg_symbols) {
  743. int val;
  744. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  745. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  746. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  747. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  748. if (val != 0xa)
  749. av_log(s->avctx, AV_LOG_ERROR,
  750. "Segmentation symbol value incorrect\n");
  751. }
  752. }
  753. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  754. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  755. int width, int height, int bandpos)
  756. {
  757. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
  758. for (y = 0; y < height; y++)
  759. memset(t1->data[y], 0, width * sizeof(width));
  760. /* If code-block contains no compressed data: nothing to do. */
  761. if (!cblk->length)
  762. return 0;
  763. for (y = 0; y < height + 2; y++)
  764. memset(t1->flags[y], 0, (width + 2) * sizeof(width));
  765. ff_mqc_initdec(&t1->mqc, cblk->data);
  766. cblk->data[cblk->length] = 0xff;
  767. cblk->data[cblk->length + 1] = 0xff;
  768. while (passno--) {
  769. switch (pass_t) {
  770. case 0:
  771. decode_sigpass(t1, width, height, bpno + 1, bandpos);
  772. break;
  773. case 1:
  774. decode_refpass(t1, width, height, bpno + 1);
  775. break;
  776. case 2:
  777. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  778. codsty->cblk_style & JPEG2000_CBLK_SEGSYM);
  779. break;
  780. }
  781. pass_t++;
  782. if (pass_t == 3) {
  783. bpno--;
  784. pass_t = 0;
  785. }
  786. }
  787. return 0;
  788. }
  789. /* TODO: Verify dequantization for lossless case
  790. * comp->data can be float or int
  791. * band->stepsize can be float or int
  792. * depending on the type of DWT transformation.
  793. * see ISO/IEC 15444-1:2002 A.6.1 */
  794. /* Float dequantization of a codeblock.*/
  795. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  796. Jpeg2000Component *comp,
  797. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  798. {
  799. int i, j, idx;
  800. float *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
  801. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
  802. for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
  803. idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
  804. datap[idx] = (float)(t1->data[j][i]) * ((float)band->stepsize);
  805. }
  806. return;
  807. }
  808. /* Integer dequantization of a codeblock.*/
  809. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  810. Jpeg2000Component *comp,
  811. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  812. {
  813. int i, j, idx;
  814. int32_t *datap =
  815. (int32_t *) &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
  816. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
  817. for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
  818. idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
  819. datap[idx] =
  820. ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16;
  821. }
  822. return;
  823. }
  824. /* Inverse ICT parameters in float and integer.
  825. * int value = (float value) * (1<<16) */
  826. static const float f_ict_params[4] = {
  827. 1.402f,
  828. 0.34413f,
  829. 0.71414f,
  830. 1.772f
  831. };
  832. static const int i_ict_params[4] = {
  833. 91881,
  834. 22553,
  835. 46802,
  836. 116130
  837. };
  838. static int mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  839. {
  840. int i, csize = 1;
  841. int ret = 0;
  842. int32_t *src[3], i0, i1, i2;
  843. float *srcf[3], i0f, i1f, i2f;
  844. for (i = 0; i < 3; i++)
  845. if (tile->codsty[0].transform == FF_DWT97)
  846. srcf[i] = tile->comp[i].data;
  847. else
  848. src[i] = (int32_t *)tile->comp[i].data;
  849. for (i = 0; i < 2; i++)
  850. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  851. switch (tile->codsty[0].transform) {
  852. case FF_DWT97:
  853. for (i = 0; i < csize; i++) {
  854. i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
  855. i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
  856. - (f_ict_params[2] * *srcf[2]);
  857. i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
  858. *srcf[0]++ = i0f;
  859. *srcf[1]++ = i1f;
  860. *srcf[2]++ = i2f;
  861. }
  862. break;
  863. case FF_DWT97_INT:
  864. for (i = 0; i < csize; i++) {
  865. i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
  866. i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
  867. - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
  868. i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
  869. *src[0]++ = i0;
  870. *src[1]++ = i1;
  871. *src[2]++ = i2;
  872. }
  873. break;
  874. case FF_DWT53:
  875. for (i = 0; i < csize; i++) {
  876. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  877. i0 = i1 + *src[2];
  878. i2 = i1 + *src[1];
  879. *src[0]++ = i0;
  880. *src[1]++ = i1;
  881. *src[2]++ = i2;
  882. }
  883. break;
  884. }
  885. return ret;
  886. }
  887. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  888. AVFrame *picture)
  889. {
  890. int compno, reslevelno, bandno;
  891. int x, y;
  892. uint8_t *line;
  893. Jpeg2000T1Context t1;
  894. /* Loop on tile components */
  895. for (compno = 0; compno < s->ncomponents; compno++) {
  896. Jpeg2000Component *comp = tile->comp + compno;
  897. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  898. /* Loop on resolution levels */
  899. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  900. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  901. /* Loop on bands */
  902. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  903. uint16_t nb_precincts, precno;
  904. Jpeg2000Band *band = rlevel->band + bandno;
  905. int cblkno = 0, bandpos;
  906. bandpos = bandno + (reslevelno > 0);
  907. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  908. /* Loop on precincts */
  909. for (precno = 0; precno < nb_precincts; precno++) {
  910. Jpeg2000Prec *prec = band->prec + precno;
  911. /* Loop on codeblocks */
  912. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  913. int x, y;
  914. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  915. decode_cblk(s, codsty, &t1, cblk,
  916. cblk->coord[0][1] - cblk->coord[0][0],
  917. cblk->coord[1][1] - cblk->coord[1][0],
  918. bandpos);
  919. /* Manage band offsets */
  920. x = cblk->coord[0][0];
  921. y = cblk->coord[1][0];
  922. if ((reslevelno > 0) && ((bandno + 1) & 1)) {
  923. Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1);
  924. x += pres->coord[0][1] - pres->coord[0][0];
  925. }
  926. if ((reslevelno > 0) && ((bandno + 1) & 2)) {
  927. Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1);
  928. y += pres->coord[1][1] - pres->coord[1][0];
  929. }
  930. if (s->avctx->flags & CODEC_FLAG_BITEXACT)
  931. dequantization_int(x, y, cblk, comp, &t1, band);
  932. else
  933. dequantization_float(x, y, cblk, comp, &t1, band);
  934. } /* end cblk */
  935. } /*end prec */
  936. } /* end band */
  937. } /* end reslevel */
  938. /* inverse DWT */
  939. ff_dwt_decode(&comp->dwt, comp->data);
  940. } /*end comp */
  941. /* inverse MCT transformation */
  942. if (tile->codsty[0].mct)
  943. mct_decode(s, tile);
  944. if (s->avctx->pix_fmt == AV_PIX_FMT_BGRA) // RGBA -> BGRA
  945. FFSWAP(float *, tile->comp[0].data, tile->comp[2].data);
  946. if (s->precision <= 8) {
  947. for (compno = 0; compno < s->ncomponents; compno++) {
  948. Jpeg2000Component *comp = tile->comp + compno;
  949. int32_t *datap = (int32_t *)comp->data;
  950. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  951. line = picture->data[0] + y * picture->linesize[0];
  952. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  953. uint8_t *dst;
  954. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  955. dst = line + x * s->ncomponents + compno;
  956. for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
  957. *datap += 1 << (s->cbps[compno] - 1);
  958. if (*datap < 0)
  959. *datap = 0;
  960. else if (*datap >= (1 << s->cbps[compno]))
  961. *datap = (1 << s->cbps[compno]) - 1;
  962. *dst = *datap++;
  963. dst += s->ncomponents;
  964. }
  965. line += picture->linesize[0];
  966. }
  967. }
  968. } else {
  969. for (compno = 0; compno < s->ncomponents; compno++) {
  970. Jpeg2000Component *comp = tile->comp + compno;
  971. float *datap = comp->data;
  972. int32_t *i_datap = (int32_t *) comp->data;
  973. uint16_t *linel;
  974. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  975. linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
  976. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  977. uint16_t *dst;
  978. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  979. dst = linel + (x * s->ncomponents + compno);
  980. for (; x < s->avctx->width; x += s->cdx[compno]) {
  981. int16_t val;
  982. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  983. if (s->avctx->flags & CODEC_FLAG_BITEXACT)
  984. val = *i_datap + (1 << (s->cbps[compno] - 1));
  985. else
  986. val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
  987. val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
  988. /* align 12 bit values in little-endian mode */
  989. *dst = val << 4;
  990. datap++;
  991. i_datap++;
  992. dst += s->ncomponents;
  993. }
  994. linel += picture->linesize[0] >> 1;
  995. }
  996. }
  997. }
  998. return 0;
  999. }
  1000. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  1001. {
  1002. int tileno, compno;
  1003. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1004. for (compno = 0; compno < s->ncomponents; compno++) {
  1005. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1006. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1007. ff_jpeg2000_cleanup(comp, codsty);
  1008. }
  1009. av_freep(&s->tile[tileno].comp);
  1010. }
  1011. av_freep(&s->tile);
  1012. }
  1013. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1014. {
  1015. Jpeg2000CodingStyle *codsty = s->codsty;
  1016. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1017. uint8_t *properties = s->properties;
  1018. for (;;) {
  1019. int len, ret = 0;
  1020. uint16_t marker;
  1021. const uint8_t *oldbuf;
  1022. if (s->buf_end - s->buf < 2) {
  1023. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1024. break;
  1025. }
  1026. marker = bytestream_get_be16(&s->buf);
  1027. oldbuf = s->buf;
  1028. if (marker == JPEG2000_EOC)
  1029. break;
  1030. if (s->buf_end - s->buf < 2)
  1031. return AVERROR(EINVAL);
  1032. len = bytestream_get_be16(&s->buf);
  1033. switch (marker) {
  1034. case JPEG2000_SIZ:
  1035. ret = get_siz(s);
  1036. break;
  1037. case JPEG2000_COC:
  1038. ret = get_coc(s, codsty, properties);
  1039. break;
  1040. case JPEG2000_COD:
  1041. ret = get_cod(s, codsty, properties);
  1042. break;
  1043. case JPEG2000_QCC:
  1044. ret = get_qcc(s, len, qntsty, properties);
  1045. break;
  1046. case JPEG2000_QCD:
  1047. ret = get_qcd(s, len, qntsty, properties);
  1048. break;
  1049. case JPEG2000_SOT:
  1050. ret = get_sot(s, len);
  1051. break;
  1052. case JPEG2000_COM:
  1053. // the comment is ignored
  1054. s->buf += len - 2;
  1055. break;
  1056. case JPEG2000_TLM:
  1057. // Tile-part lengths
  1058. ret = get_tlm(s, len);
  1059. break;
  1060. default:
  1061. av_log(s->avctx, AV_LOG_ERROR,
  1062. "unsupported marker 0x%.4X at pos 0x%tX\n",
  1063. marker, s->buf - s->buf_start - 4);
  1064. s->buf += len - 2;
  1065. break;
  1066. }
  1067. if (((s->buf - oldbuf != len) && (marker != JPEG2000_SOT)) || ret) {
  1068. av_log(s->avctx, AV_LOG_ERROR,
  1069. "error during processing marker segment %.4x\n", marker);
  1070. return ret ? ret : -1;
  1071. }
  1072. }
  1073. return 0;
  1074. }
  1075. /* Read bit stream packets --> T2 operation. */
  1076. static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
  1077. {
  1078. int ret = 0;
  1079. Jpeg2000Tile *tile = s->tile + s->curtileno;
  1080. if (ret = init_tile(s, s->curtileno))
  1081. return ret;
  1082. if (ret = jpeg2000_decode_packets(s, tile))
  1083. return ret;
  1084. return 0;
  1085. }
  1086. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1087. {
  1088. int32_t atom_size;
  1089. int found_codestream = 0, search_range = 10;
  1090. // Skip JPEG 2000 signature atom.
  1091. s->buf += 12;
  1092. while (!found_codestream && search_range) {
  1093. atom_size = AV_RB32(s->buf);
  1094. if (AV_RB32(s->buf + 4) == JP2_CODESTREAM) {
  1095. found_codestream = 1;
  1096. s->buf += 8;
  1097. } else {
  1098. s->buf += atom_size;
  1099. search_range--;
  1100. }
  1101. }
  1102. if (found_codestream)
  1103. return 1;
  1104. return 0;
  1105. }
  1106. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1107. int *got_frame, AVPacket *avpkt)
  1108. {
  1109. Jpeg2000DecoderContext *s = avctx->priv_data;
  1110. ThreadFrame frame = { .f = data };
  1111. AVFrame *picture = data;
  1112. int tileno, ret;
  1113. s->avctx = avctx;
  1114. s->buf = s->buf_start = avpkt->data;
  1115. s->buf_end = s->buf_start + avpkt->size;
  1116. s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
  1117. // reduction factor, i.e number of resolution levels to skip
  1118. s->reduction_factor = s->lowres;
  1119. if (s->buf_end - s->buf < 2)
  1120. return AVERROR(EINVAL);
  1121. // check if the image is in jp2 format
  1122. if ((AV_RB32(s->buf) == 12) &&
  1123. (AV_RB32(s->buf + 4) == JP2_SIG_TYPE) &&
  1124. (AV_RB32(s->buf + 8) == JP2_SIG_VALUE)) {
  1125. if (!jp2_find_codestream(s)) {
  1126. av_log(avctx, AV_LOG_ERROR,
  1127. "couldn't find jpeg2k codestream atom\n");
  1128. return -1;
  1129. }
  1130. }
  1131. if (bytestream_get_be16(&s->buf) != JPEG2000_SOC) {
  1132. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1133. return -1;
  1134. }
  1135. if (ret = jpeg2000_read_main_headers(s))
  1136. goto end;
  1137. /* get picture buffer */
  1138. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
  1139. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
  1140. goto end;
  1141. }
  1142. picture->pict_type = AV_PICTURE_TYPE_I;
  1143. picture->key_frame = 1;
  1144. if (ret = jpeg2000_read_bitstream_packets(s))
  1145. goto end;
  1146. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1147. if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
  1148. goto end;
  1149. *got_frame = 1;
  1150. end:
  1151. jpeg2000_dec_cleanup(s);
  1152. return ret ? ret : s->buf - s->buf_start;
  1153. }
  1154. static void jpeg2000_init_static_data(AVCodec *codec)
  1155. {
  1156. ff_jpeg2000_init_tier1_luts();
  1157. }
  1158. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1159. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1160. static const AVOption options[] = {
  1161. { "lowres", "Lower the decoding resolution by a power of two",
  1162. OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1163. { NULL },
  1164. };
  1165. static const AVProfile profiles[] = {
  1166. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1167. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1168. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1169. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1170. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1171. { FF_PROFILE_UNKNOWN },
  1172. };
  1173. static const AVClass class = {
  1174. .class_name = "jpeg2000",
  1175. .item_name = av_default_item_name,
  1176. .option = options,
  1177. .version = LIBAVUTIL_VERSION_INT,
  1178. };
  1179. AVCodec ff_jpeg2000_decoder = {
  1180. .name = "jpeg2000",
  1181. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1182. .type = AVMEDIA_TYPE_VIDEO,
  1183. .id = AV_CODEC_ID_JPEG2000,
  1184. .capabilities = CODEC_CAP_FRAME_THREADS,
  1185. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1186. .init_static_data = jpeg2000_init_static_data,
  1187. .decode = jpeg2000_decode_frame,
  1188. .priv_class = &class,
  1189. .pix_fmts = (enum AVPixelFormat[]) { AV_PIX_FMT_XYZ12,
  1190. AV_PIX_FMT_GRAY8,
  1191. -1 },
  1192. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1193. };