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