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.

1364 lines
48KB

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