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.

1371 lines
48KB

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