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.

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