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.

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