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.

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