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.

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