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.

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