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.

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