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.

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