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.

1338 lines
46KB

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