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.

1686 lines
62KB

  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/avassert.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avcodec.h"
  31. #include "bytestream.h"
  32. #include "internal.h"
  33. #include "thread.h"
  34. #include "jpeg2000.h"
  35. #define JP2_SIG_TYPE 0x6A502020
  36. #define JP2_SIG_VALUE 0x0D0A870A
  37. #define JP2_CODESTREAM 0x6A703263
  38. #define JP2_HEADER 0x6A703268
  39. #define HAD_COC 0x01
  40. #define HAD_QCC 0x02
  41. typedef struct Jpeg2000TilePart {
  42. uint8_t tile_index; // Tile index who refers the tile-part
  43. const uint8_t *tp_end;
  44. GetByteContext tpg; // bit stream in tile-part
  45. } Jpeg2000TilePart;
  46. /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
  47. * one per component, so tile_part elements have a size of 3 */
  48. typedef struct Jpeg2000Tile {
  49. Jpeg2000Component *comp;
  50. uint8_t properties[4];
  51. Jpeg2000CodingStyle codsty[4];
  52. Jpeg2000QuantStyle qntsty[4];
  53. Jpeg2000TilePart tile_part[4];
  54. uint16_t tp_idx; // Tile-part index
  55. } Jpeg2000Tile;
  56. typedef struct Jpeg2000DecoderContext {
  57. AVClass *class;
  58. AVCodecContext *avctx;
  59. GetByteContext g;
  60. int width, height;
  61. int image_offset_x, image_offset_y;
  62. int tile_offset_x, tile_offset_y;
  63. uint8_t cbps[4]; // bits per sample in particular components
  64. uint8_t sgnd[4]; // if a component is signed
  65. uint8_t properties[4];
  66. int cdx[4], cdy[4];
  67. int precision;
  68. int ncomponents;
  69. int colour_space;
  70. uint32_t palette[256];
  71. int8_t pal8;
  72. int cdef[4];
  73. int tile_width, tile_height;
  74. unsigned numXtiles, numYtiles;
  75. int maxtilelen;
  76. Jpeg2000CodingStyle codsty[4];
  77. Jpeg2000QuantStyle qntsty[4];
  78. int bit_index;
  79. int curtileno;
  80. Jpeg2000Tile *tile;
  81. /*options parameters*/
  82. int reduction_factor;
  83. } Jpeg2000DecoderContext;
  84. /* get_bits functions for JPEG2000 packet bitstream
  85. * It is a get_bit function with a bit-stuffing routine. If the value of the
  86. * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
  87. * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
  88. static int get_bits(Jpeg2000DecoderContext *s, int n)
  89. {
  90. int res = 0;
  91. while (--n >= 0) {
  92. res <<= 1;
  93. if (s->bit_index == 0) {
  94. s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
  95. }
  96. s->bit_index--;
  97. res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
  98. }
  99. return res;
  100. }
  101. static void jpeg2000_flush(Jpeg2000DecoderContext *s)
  102. {
  103. if (bytestream2_get_byte(&s->g) == 0xff)
  104. bytestream2_skip(&s->g, 1);
  105. s->bit_index = 8;
  106. }
  107. /* decode the value stored in node */
  108. static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
  109. int threshold)
  110. {
  111. Jpeg2000TgtNode *stack[30];
  112. int sp = -1, curval = 0;
  113. if (!node)
  114. return AVERROR_INVALIDDATA;
  115. while (node && !node->vis) {
  116. stack[++sp] = node;
  117. node = node->parent;
  118. }
  119. if (node)
  120. curval = node->val;
  121. else
  122. curval = stack[sp]->val;
  123. while (curval < threshold && sp >= 0) {
  124. if (curval < stack[sp]->val)
  125. curval = stack[sp]->val;
  126. while (curval < threshold) {
  127. int ret;
  128. if ((ret = get_bits(s, 1)) > 0) {
  129. stack[sp]->vis++;
  130. break;
  131. } else if (!ret)
  132. curval++;
  133. else
  134. return ret;
  135. }
  136. stack[sp]->val = curval;
  137. sp--;
  138. }
  139. return curval;
  140. }
  141. static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
  142. int bpc, uint32_t log2_chroma_wh, int pal8)
  143. {
  144. int match = 1;
  145. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  146. if (desc->nb_components != components) {
  147. return 0;
  148. }
  149. switch (components) {
  150. case 4:
  151. match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
  152. (log2_chroma_wh >> 14 & 3) == 0 &&
  153. (log2_chroma_wh >> 12 & 3) == 0;
  154. case 3:
  155. match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
  156. (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
  157. (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
  158. case 2:
  159. match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
  160. (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
  161. (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
  162. case 1:
  163. match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
  164. (log2_chroma_wh >> 2 & 3) == 0 &&
  165. (log2_chroma_wh & 3) == 0 &&
  166. (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
  167. }
  168. return match;
  169. }
  170. // pix_fmts with lower bpp have to be listed before
  171. // similar pix_fmts with higher bpp.
  172. #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
  173. #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
  174. #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
  175. AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
  176. AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
  177. AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
  178. AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
  179. AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
  180. AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
  181. AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
  182. AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
  183. AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
  184. AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
  185. #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
  186. static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
  187. static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
  188. static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
  189. static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS};
  190. static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
  191. GRAY_PIXEL_FORMATS,
  192. YUV_PIXEL_FORMATS,
  193. XYZ_PIXEL_FORMATS};
  194. /* marker segments */
  195. /* get sizes and offsets of image, tiles; number of components */
  196. static int get_siz(Jpeg2000DecoderContext *s)
  197. {
  198. int i;
  199. int ncomponents;
  200. uint32_t log2_chroma_wh = 0;
  201. const enum AVPixelFormat *possible_fmts = NULL;
  202. int possible_fmts_nb = 0;
  203. if (bytestream2_get_bytes_left(&s->g) < 36)
  204. return AVERROR_INVALIDDATA;
  205. s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
  206. s->width = bytestream2_get_be32u(&s->g); // Width
  207. s->height = bytestream2_get_be32u(&s->g); // Height
  208. s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
  209. s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
  210. s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
  211. s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
  212. s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
  213. s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
  214. ncomponents = bytestream2_get_be16u(&s->g); // CSiz
  215. if (ncomponents <= 0) {
  216. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
  217. s->ncomponents);
  218. return AVERROR_INVALIDDATA;
  219. }
  220. if (ncomponents > 4) {
  221. avpriv_request_sample(s->avctx, "Support for %d components",
  222. s->ncomponents);
  223. return AVERROR_PATCHWELCOME;
  224. }
  225. s->ncomponents = ncomponents;
  226. if (s->tile_width <= 0 || s->tile_height <= 0) {
  227. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
  228. s->tile_width, s->tile_height);
  229. return AVERROR_INVALIDDATA;
  230. }
  231. if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
  232. return AVERROR_INVALIDDATA;
  233. for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
  234. uint8_t x = bytestream2_get_byteu(&s->g);
  235. s->cbps[i] = (x & 0x7f) + 1;
  236. s->precision = FFMAX(s->cbps[i], s->precision);
  237. s->sgnd[i] = !!(x & 0x80);
  238. s->cdx[i] = bytestream2_get_byteu(&s->g);
  239. s->cdy[i] = bytestream2_get_byteu(&s->g);
  240. if (!s->cdx[i] || !s->cdy[i]) {
  241. av_log(s->avctx, AV_LOG_ERROR, "Invalid sample seperation\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
  245. }
  246. s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
  247. s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
  248. if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
  249. s->numXtiles = s->numYtiles = 0;
  250. return AVERROR(EINVAL);
  251. }
  252. s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
  253. if (!s->tile) {
  254. s->numXtiles = s->numYtiles = 0;
  255. return AVERROR(ENOMEM);
  256. }
  257. for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
  258. Jpeg2000Tile *tile = s->tile + i;
  259. tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
  260. if (!tile->comp)
  261. return AVERROR(ENOMEM);
  262. }
  263. /* compute image size with reduction factor */
  264. s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
  265. s->reduction_factor);
  266. s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
  267. s->reduction_factor);
  268. if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
  269. s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
  270. possible_fmts = xyz_pix_fmts;
  271. possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
  272. } else {
  273. switch (s->colour_space) {
  274. case 16:
  275. possible_fmts = rgb_pix_fmts;
  276. possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
  277. break;
  278. case 17:
  279. possible_fmts = gray_pix_fmts;
  280. possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
  281. break;
  282. case 18:
  283. possible_fmts = yuv_pix_fmts;
  284. possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
  285. break;
  286. default:
  287. possible_fmts = all_pix_fmts;
  288. possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
  289. break;
  290. }
  291. }
  292. for (i = 0; i < possible_fmts_nb; ++i) {
  293. if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
  294. s->avctx->pix_fmt = possible_fmts[i];
  295. break;
  296. }
  297. }
  298. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  299. av_log(s->avctx, AV_LOG_ERROR,
  300. "Unknown pix_fmt, profile: %d, colour_space: %d, "
  301. "components: %d, precision: %d, "
  302. "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
  303. s->avctx->profile, s->colour_space, ncomponents, s->precision,
  304. ncomponents > 2 ? s->cdx[1] : 0,
  305. ncomponents > 2 ? s->cdy[1] : 0,
  306. ncomponents > 2 ? s->cdx[2] : 0,
  307. ncomponents > 2 ? s->cdy[2] : 0);
  308. }
  309. return 0;
  310. }
  311. /* get common part for COD and COC segments */
  312. static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
  313. {
  314. uint8_t byte;
  315. if (bytestream2_get_bytes_left(&s->g) < 5)
  316. return AVERROR_INVALIDDATA;
  317. /* nreslevels = number of resolution levels
  318. = number of decomposition level +1 */
  319. c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
  320. if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
  321. av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
  322. return AVERROR_INVALIDDATA;
  323. }
  324. /* compute number of resolution levels to decode */
  325. if (c->nreslevels < s->reduction_factor)
  326. c->nreslevels2decode = 1;
  327. else
  328. c->nreslevels2decode = c->nreslevels - s->reduction_factor;
  329. c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
  330. c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
  331. if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
  332. c->log2_cblk_width + c->log2_cblk_height > 12) {
  333. av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
  334. return AVERROR_INVALIDDATA;
  335. }
  336. c->cblk_style = bytestream2_get_byteu(&s->g);
  337. if (c->cblk_style != 0) { // cblk style
  338. av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
  339. }
  340. c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
  341. /* set integer 9/7 DWT in case of BITEXACT flag */
  342. if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
  343. c->transform = FF_DWT97_INT;
  344. if (c->csty & JPEG2000_CSTY_PREC) {
  345. int i;
  346. for (i = 0; i < c->nreslevels; i++) {
  347. byte = bytestream2_get_byte(&s->g);
  348. c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
  349. c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
  350. }
  351. } else {
  352. memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
  353. memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
  354. }
  355. return 0;
  356. }
  357. /* get coding parameters for a particular tile or whole image*/
  358. static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  359. uint8_t *properties)
  360. {
  361. Jpeg2000CodingStyle tmp;
  362. int compno, ret;
  363. if (bytestream2_get_bytes_left(&s->g) < 5)
  364. return AVERROR_INVALIDDATA;
  365. tmp.csty = bytestream2_get_byteu(&s->g);
  366. // get progression order
  367. tmp.prog_order = bytestream2_get_byteu(&s->g);
  368. tmp.nlayers = bytestream2_get_be16u(&s->g);
  369. tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
  370. if (tmp.mct && s->ncomponents < 3) {
  371. av_log(s->avctx, AV_LOG_ERROR,
  372. "MCT %d with too few components (%d)\n",
  373. tmp.mct, s->ncomponents);
  374. return AVERROR_INVALIDDATA;
  375. }
  376. if ((ret = get_cox(s, &tmp)) < 0)
  377. return ret;
  378. for (compno = 0; compno < s->ncomponents; compno++)
  379. if (!(properties[compno] & HAD_COC))
  380. memcpy(c + compno, &tmp, sizeof(tmp));
  381. return 0;
  382. }
  383. /* Get coding parameters for a component in the whole image or a
  384. * particular tile. */
  385. static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  386. uint8_t *properties)
  387. {
  388. int compno, ret;
  389. if (bytestream2_get_bytes_left(&s->g) < 2)
  390. return AVERROR_INVALIDDATA;
  391. compno = bytestream2_get_byteu(&s->g);
  392. if (compno >= s->ncomponents) {
  393. av_log(s->avctx, AV_LOG_ERROR,
  394. "Invalid compno %d. There are %d components in the image.\n",
  395. compno, s->ncomponents);
  396. return AVERROR_INVALIDDATA;
  397. }
  398. c += compno;
  399. c->csty = bytestream2_get_byteu(&s->g);
  400. if ((ret = get_cox(s, c)) < 0)
  401. return ret;
  402. properties[compno] |= HAD_COC;
  403. return 0;
  404. }
  405. /* Get common part for QCD and QCC segments. */
  406. static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
  407. {
  408. int i, x;
  409. if (bytestream2_get_bytes_left(&s->g) < 1)
  410. return AVERROR_INVALIDDATA;
  411. x = bytestream2_get_byteu(&s->g); // Sqcd
  412. q->nguardbits = x >> 5;
  413. q->quantsty = x & 0x1f;
  414. if (q->quantsty == JPEG2000_QSTY_NONE) {
  415. n -= 3;
  416. if (bytestream2_get_bytes_left(&s->g) < n ||
  417. n > JPEG2000_MAX_DECLEVELS*3)
  418. return AVERROR_INVALIDDATA;
  419. for (i = 0; i < n; i++)
  420. q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
  421. } else if (q->quantsty == JPEG2000_QSTY_SI) {
  422. if (bytestream2_get_bytes_left(&s->g) < 2)
  423. return AVERROR_INVALIDDATA;
  424. x = bytestream2_get_be16u(&s->g);
  425. q->expn[0] = x >> 11;
  426. q->mant[0] = x & 0x7ff;
  427. for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
  428. int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
  429. q->expn[i] = curexpn;
  430. q->mant[i] = q->mant[0];
  431. }
  432. } else {
  433. n = (n - 3) >> 1;
  434. if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
  435. n > JPEG2000_MAX_DECLEVELS*3)
  436. return AVERROR_INVALIDDATA;
  437. for (i = 0; i < n; i++) {
  438. x = bytestream2_get_be16u(&s->g);
  439. q->expn[i] = x >> 11;
  440. q->mant[i] = x & 0x7ff;
  441. }
  442. }
  443. return 0;
  444. }
  445. /* Get quantization parameters for a particular tile or a whole image. */
  446. static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  447. uint8_t *properties)
  448. {
  449. Jpeg2000QuantStyle tmp;
  450. int compno, ret;
  451. if ((ret = get_qcx(s, n, &tmp)) < 0)
  452. return ret;
  453. for (compno = 0; compno < s->ncomponents; compno++)
  454. if (!(properties[compno] & HAD_QCC))
  455. memcpy(q + compno, &tmp, sizeof(tmp));
  456. return 0;
  457. }
  458. /* Get quantization parameters for a component in the whole image
  459. * on in a particular tile. */
  460. static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  461. uint8_t *properties)
  462. {
  463. int compno;
  464. if (bytestream2_get_bytes_left(&s->g) < 1)
  465. return AVERROR_INVALIDDATA;
  466. compno = bytestream2_get_byteu(&s->g);
  467. if (compno >= s->ncomponents) {
  468. av_log(s->avctx, AV_LOG_ERROR,
  469. "Invalid compno %d. There are %d components in the image.\n",
  470. compno, s->ncomponents);
  471. return AVERROR_INVALIDDATA;
  472. }
  473. properties[compno] |= HAD_QCC;
  474. return get_qcx(s, n - 1, q + compno);
  475. }
  476. /* Get start of tile segment. */
  477. static int get_sot(Jpeg2000DecoderContext *s, int n)
  478. {
  479. Jpeg2000TilePart *tp;
  480. uint16_t Isot;
  481. uint32_t Psot;
  482. uint8_t TPsot;
  483. if (bytestream2_get_bytes_left(&s->g) < 8)
  484. return AVERROR_INVALIDDATA;
  485. s->curtileno = 0;
  486. Isot = bytestream2_get_be16u(&s->g); // Isot
  487. if (Isot >= s->numXtiles * s->numYtiles)
  488. return AVERROR_INVALIDDATA;
  489. s->curtileno = Isot;
  490. Psot = bytestream2_get_be32u(&s->g); // Psot
  491. TPsot = bytestream2_get_byteu(&s->g); // TPsot
  492. /* Read TNSot but not used */
  493. bytestream2_get_byteu(&s->g); // TNsot
  494. if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
  495. av_log(s->avctx, AV_LOG_ERROR, "Psot %d too big\n", Psot);
  496. return AVERROR_INVALIDDATA;
  497. }
  498. if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
  499. avpriv_request_sample(s->avctx, "Support for %d components", TPsot);
  500. return AVERROR_PATCHWELCOME;
  501. }
  502. s->tile[Isot].tp_idx = TPsot;
  503. tp = s->tile[Isot].tile_part + TPsot;
  504. tp->tile_index = Isot;
  505. tp->tp_end = s->g.buffer + Psot - n - 2;
  506. if (!TPsot) {
  507. Jpeg2000Tile *tile = s->tile + s->curtileno;
  508. /* copy defaults */
  509. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
  510. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
  511. }
  512. return 0;
  513. }
  514. /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
  515. * Used to know the number of tile parts and lengths.
  516. * There may be multiple TLMs in the header.
  517. * TODO: The function is not used for tile-parts management, nor anywhere else.
  518. * It can be useful to allocate memory for tile parts, before managing the SOT
  519. * markers. Parsing the TLM header is needed to increment the input header
  520. * buffer.
  521. * This marker is mandatory for DCI. */
  522. static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
  523. {
  524. uint8_t Stlm, ST, SP, tile_tlm, i;
  525. bytestream2_get_byte(&s->g); /* Ztlm: skipped */
  526. Stlm = bytestream2_get_byte(&s->g);
  527. // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  528. ST = (Stlm >> 4) & 0x03;
  529. // TODO: Manage case of ST = 0b11 --> raise error
  530. SP = (Stlm >> 6) & 0x01;
  531. tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
  532. for (i = 0; i < tile_tlm; i++) {
  533. switch (ST) {
  534. case 0:
  535. break;
  536. case 1:
  537. bytestream2_get_byte(&s->g);
  538. break;
  539. case 2:
  540. bytestream2_get_be16(&s->g);
  541. break;
  542. case 3:
  543. bytestream2_get_be32(&s->g);
  544. break;
  545. }
  546. if (SP == 0) {
  547. bytestream2_get_be16(&s->g);
  548. } else {
  549. bytestream2_get_be32(&s->g);
  550. }
  551. }
  552. return 0;
  553. }
  554. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  555. {
  556. int compno;
  557. int tilex = tileno % s->numXtiles;
  558. int tiley = tileno / s->numXtiles;
  559. Jpeg2000Tile *tile = s->tile + tileno;
  560. if (!tile->comp)
  561. return AVERROR(ENOMEM);
  562. for (compno = 0; compno < s->ncomponents; compno++) {
  563. Jpeg2000Component *comp = tile->comp + compno;
  564. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  565. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  566. int ret; // global bandno
  567. comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
  568. comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
  569. comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
  570. comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
  571. comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
  572. comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
  573. comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
  574. comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
  575. if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  576. s->cbps[compno], s->cdx[compno],
  577. s->cdy[compno], s->avctx))
  578. return ret;
  579. }
  580. return 0;
  581. }
  582. /* Read the number of coding passes. */
  583. static int getnpasses(Jpeg2000DecoderContext *s)
  584. {
  585. int num;
  586. if (!get_bits(s, 1))
  587. return 1;
  588. if (!get_bits(s, 1))
  589. return 2;
  590. if ((num = get_bits(s, 2)) != 3)
  591. return num < 0 ? num : 3 + num;
  592. if ((num = get_bits(s, 5)) != 31)
  593. return num < 0 ? num : 6 + num;
  594. num = get_bits(s, 7);
  595. return num < 0 ? num : 37 + num;
  596. }
  597. static int getlblockinc(Jpeg2000DecoderContext *s)
  598. {
  599. int res = 0, ret;
  600. while (ret = get_bits(s, 1)) {
  601. if (ret < 0)
  602. return ret;
  603. res++;
  604. }
  605. return res;
  606. }
  607. static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
  608. Jpeg2000CodingStyle *codsty,
  609. Jpeg2000ResLevel *rlevel, int precno,
  610. int layno, uint8_t *expn, int numgbits)
  611. {
  612. int bandno, cblkno, ret, nb_code_blocks;
  613. if (!(ret = get_bits(s, 1))) {
  614. jpeg2000_flush(s);
  615. return 0;
  616. } else if (ret < 0)
  617. return ret;
  618. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  619. Jpeg2000Band *band = rlevel->band + bandno;
  620. Jpeg2000Prec *prec = band->prec + precno;
  621. if (band->coord[0][0] == band->coord[0][1] ||
  622. band->coord[1][0] == band->coord[1][1])
  623. continue;
  624. nb_code_blocks = prec->nb_codeblocks_height *
  625. prec->nb_codeblocks_width;
  626. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  627. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  628. int incl, newpasses, llen;
  629. if (cblk->npasses)
  630. incl = get_bits(s, 1);
  631. else
  632. incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
  633. if (!incl)
  634. continue;
  635. else if (incl < 0)
  636. return incl;
  637. if (!cblk->npasses) {
  638. int v = expn[bandno] + numgbits - 1 -
  639. tag_tree_decode(s, prec->zerobits + cblkno, 100);
  640. if (v < 0) {
  641. av_log(s->avctx, AV_LOG_ERROR,
  642. "nonzerobits %d invalid\n", v);
  643. return AVERROR_INVALIDDATA;
  644. }
  645. cblk->nonzerobits = v;
  646. }
  647. if ((newpasses = getnpasses(s)) < 0)
  648. return newpasses;
  649. if ((llen = getlblockinc(s)) < 0)
  650. return llen;
  651. cblk->lblock += llen;
  652. if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
  653. return ret;
  654. if (ret > sizeof(cblk->data)) {
  655. avpriv_request_sample(s->avctx,
  656. "Block with lengthinc greater than %zu",
  657. sizeof(cblk->data));
  658. return AVERROR_PATCHWELCOME;
  659. }
  660. cblk->lengthinc = ret;
  661. cblk->npasses += newpasses;
  662. }
  663. }
  664. jpeg2000_flush(s);
  665. if (codsty->csty & JPEG2000_CSTY_EPH) {
  666. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
  667. bytestream2_skip(&s->g, 2);
  668. else
  669. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
  670. }
  671. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  672. Jpeg2000Band *band = rlevel->band + bandno;
  673. Jpeg2000Prec *prec = band->prec + precno;
  674. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  675. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  676. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  677. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
  678. || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
  679. )
  680. return AVERROR_INVALIDDATA;
  681. bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
  682. cblk->length += cblk->lengthinc;
  683. cblk->lengthinc = 0;
  684. }
  685. }
  686. return 0;
  687. }
  688. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  689. {
  690. int ret = 0;
  691. int layno, reslevelno, compno, precno, ok_reslevel;
  692. int x, y;
  693. s->bit_index = 8;
  694. switch (tile->codsty[0].prog_order) {
  695. case JPEG2000_PGOD_RLCP:
  696. avpriv_request_sample(s->avctx, "Progression order RLCP");
  697. case JPEG2000_PGOD_LRCP:
  698. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  699. ok_reslevel = 1;
  700. for (reslevelno = 0; ok_reslevel; reslevelno++) {
  701. ok_reslevel = 0;
  702. for (compno = 0; compno < s->ncomponents; compno++) {
  703. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  704. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  705. if (reslevelno < codsty->nreslevels) {
  706. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  707. reslevelno;
  708. ok_reslevel = 1;
  709. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  710. if ((ret = jpeg2000_decode_packet(s,
  711. codsty, rlevel,
  712. precno, layno,
  713. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  714. qntsty->nguardbits)) < 0)
  715. return ret;
  716. }
  717. }
  718. }
  719. }
  720. break;
  721. case JPEG2000_PGOD_CPRL:
  722. for (compno = 0; compno < s->ncomponents; compno++) {
  723. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  724. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  725. /* Set bit stream buffer address according to tile-part.
  726. * For DCinema one tile-part per component, so can be
  727. * indexed by component. */
  728. s->g = tile->tile_part[compno].tpg;
  729. /* Position loop (y axis)
  730. * TODO: Automate computing of step 256.
  731. * Fixed here, but to be computed before entering here. */
  732. for (y = 0; y < s->height; y += 256) {
  733. /* Position loop (y axis)
  734. * TODO: automate computing of step 256.
  735. * Fixed here, but to be computed before entering here. */
  736. for (x = 0; x < s->width; x += 256) {
  737. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  738. uint16_t prcx, prcy;
  739. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  740. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
  741. if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
  742. (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  743. continue;
  744. if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
  745. (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
  746. continue;
  747. // check if a precinct exists
  748. prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
  749. prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
  750. precno = prcx + rlevel->num_precincts_x * prcy;
  751. for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
  752. if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
  753. precno, layno,
  754. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  755. qntsty->nguardbits)) < 0)
  756. return ret;
  757. }
  758. }
  759. }
  760. }
  761. }
  762. break;
  763. case JPEG2000_PGOD_RPCL:
  764. avpriv_request_sample(s->avctx, "Progression order RPCL");
  765. ret = AVERROR_PATCHWELCOME;
  766. break;
  767. case JPEG2000_PGOD_PCRL:
  768. avpriv_request_sample(s->avctx, "Progression order PCRL");
  769. ret = AVERROR_PATCHWELCOME;
  770. break;
  771. default:
  772. break;
  773. }
  774. /* EOC marker reached */
  775. bytestream2_skip(&s->g, 2);
  776. return ret;
  777. }
  778. /* TIER-1 routines */
  779. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  780. int bpno, int bandno, int bpass_csty_symbol,
  781. int vert_causal_ctx_csty_symbol)
  782. {
  783. int mask = 3 << (bpno - 1), y0, x, y;
  784. for (y0 = 0; y0 < height; y0 += 4)
  785. for (x = 0; x < width; x++)
  786. for (y = y0; y < height && y < y0 + 4; y++) {
  787. if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
  788. && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  789. int flags_mask = -1;
  790. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  791. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  792. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
  793. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  794. if (bpass_csty_symbol)
  795. t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  796. else
  797. t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  798. -mask : mask;
  799. ff_jpeg2000_set_significance(t1, x, y,
  800. t1->data[y][x] < 0);
  801. }
  802. t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
  803. }
  804. }
  805. }
  806. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  807. int bpno)
  808. {
  809. int phalf, nhalf;
  810. int y0, x, y;
  811. phalf = 1 << (bpno - 1);
  812. nhalf = -phalf;
  813. for (y0 = 0; y0 < height; y0 += 4)
  814. for (x = 0; x < width; x++)
  815. for (y = y0; y < height && y < y0 + 4; y++)
  816. if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  817. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
  818. int r = ff_mqc_decode(&t1->mqc,
  819. t1->mqc.cx_states + ctxno)
  820. ? phalf : nhalf;
  821. t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
  822. t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
  823. }
  824. }
  825. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  826. int width, int height, int bpno, int bandno,
  827. int seg_symbols, int vert_causal_ctx_csty_symbol)
  828. {
  829. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  830. for (y0 = 0; y0 < height; y0 += 4) {
  831. for (x = 0; x < width; x++) {
  832. if (y0 + 3 < height &&
  833. !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  834. (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  835. (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  836. (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
  837. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  838. continue;
  839. runlen = ff_mqc_decode(&t1->mqc,
  840. t1->mqc.cx_states + MQC_CX_UNI);
  841. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  842. t1->mqc.cx_states +
  843. MQC_CX_UNI);
  844. dec = 1;
  845. } else {
  846. runlen = 0;
  847. dec = 0;
  848. }
  849. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  850. if (!dec) {
  851. if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  852. int flags_mask = -1;
  853. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  854. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
  855. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
  856. bandno));
  857. }
  858. }
  859. if (dec) {
  860. int xorbit;
  861. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
  862. &xorbit);
  863. t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
  864. t1->mqc.cx_states + ctxno) ^
  865. xorbit)
  866. ? -mask : mask;
  867. ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
  868. }
  869. dec = 0;
  870. t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
  871. }
  872. }
  873. }
  874. if (seg_symbols) {
  875. int val;
  876. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  877. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  878. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  879. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  880. if (val != 0xa)
  881. av_log(s->avctx, AV_LOG_ERROR,
  882. "Segmentation symbol value incorrect\n");
  883. }
  884. }
  885. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  886. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  887. int width, int height, int bandpos)
  888. {
  889. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
  890. int clnpass_cnt = 0;
  891. int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
  892. int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
  893. for (y = 0; y < height; y++)
  894. memset(t1->data[y], 0, width * sizeof(**t1->data));
  895. /* If code-block contains no compressed data: nothing to do. */
  896. if (!cblk->length)
  897. return 0;
  898. for (y = 0; y < height + 2; y++)
  899. memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
  900. cblk->data[cblk->length] = 0xff;
  901. cblk->data[cblk->length+1] = 0xff;
  902. ff_mqc_initdec(&t1->mqc, cblk->data);
  903. while (passno--) {
  904. switch(pass_t) {
  905. case 0:
  906. decode_sigpass(t1, width, height, bpno + 1, bandpos,
  907. bpass_csty_symbol && (clnpass_cnt >= 4),
  908. vert_causal_ctx_csty_symbol);
  909. break;
  910. case 1:
  911. decode_refpass(t1, width, height, bpno + 1);
  912. if (bpass_csty_symbol && clnpass_cnt >= 4)
  913. ff_mqc_initdec(&t1->mqc, cblk->data);
  914. break;
  915. case 2:
  916. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  917. codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
  918. vert_causal_ctx_csty_symbol);
  919. clnpass_cnt = clnpass_cnt + 1;
  920. if (bpass_csty_symbol && clnpass_cnt >= 4)
  921. ff_mqc_initdec(&t1->mqc, cblk->data);
  922. break;
  923. }
  924. pass_t++;
  925. if (pass_t == 3) {
  926. bpno--;
  927. pass_t = 0;
  928. }
  929. }
  930. return 0;
  931. }
  932. /* TODO: Verify dequantization for lossless case
  933. * comp->data can be float or int
  934. * band->stepsize can be float or int
  935. * depending on the type of DWT transformation.
  936. * see ISO/IEC 15444-1:2002 A.6.1 */
  937. /* Float dequantization of a codeblock.*/
  938. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  939. Jpeg2000Component *comp,
  940. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  941. {
  942. int i, j;
  943. int w = cblk->coord[0][1] - cblk->coord[0][0];
  944. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  945. float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  946. int *src = t1->data[j];
  947. for (i = 0; i < w; ++i)
  948. datap[i] = src[i] * band->f_stepsize;
  949. }
  950. }
  951. /* Integer dequantization of a codeblock.*/
  952. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  953. Jpeg2000Component *comp,
  954. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  955. {
  956. int i, j;
  957. int w = cblk->coord[0][1] - cblk->coord[0][0];
  958. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  959. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  960. int *src = t1->data[j];
  961. for (i = 0; i < w; ++i)
  962. datap[i] = (src[i] * band->i_stepsize + (1 << 14)) >> 15;
  963. }
  964. }
  965. /* Inverse ICT parameters in float and integer.
  966. * int value = (float value) * (1<<16) */
  967. static const float f_ict_params[4] = {
  968. 1.402f,
  969. 0.34413f,
  970. 0.71414f,
  971. 1.772f
  972. };
  973. static const int i_ict_params[4] = {
  974. 91881,
  975. 22553,
  976. 46802,
  977. 116130
  978. };
  979. static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  980. {
  981. int i, csize = 1;
  982. int32_t *src[3], i0, i1, i2;
  983. float *srcf[3], i0f, i1f, i2f;
  984. for (i = 0; i < 3; i++)
  985. if (tile->codsty[0].transform == FF_DWT97)
  986. srcf[i] = tile->comp[i].f_data;
  987. else
  988. src [i] = tile->comp[i].i_data;
  989. for (i = 0; i < 2; i++)
  990. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  991. switch (tile->codsty[0].transform) {
  992. case FF_DWT97:
  993. for (i = 0; i < csize; i++) {
  994. i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
  995. i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
  996. - (f_ict_params[2] * *srcf[2]);
  997. i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
  998. *srcf[0]++ = i0f;
  999. *srcf[1]++ = i1f;
  1000. *srcf[2]++ = i2f;
  1001. }
  1002. break;
  1003. case FF_DWT97_INT:
  1004. for (i = 0; i < csize; i++) {
  1005. i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
  1006. i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
  1007. - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
  1008. i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
  1009. *src[0]++ = i0;
  1010. *src[1]++ = i1;
  1011. *src[2]++ = i2;
  1012. }
  1013. break;
  1014. case FF_DWT53:
  1015. for (i = 0; i < csize; i++) {
  1016. i1 = *src[0] - (*src[2] + *src[1] >> 2);
  1017. i0 = i1 + *src[2];
  1018. i2 = i1 + *src[1];
  1019. *src[0]++ = i0;
  1020. *src[1]++ = i1;
  1021. *src[2]++ = i2;
  1022. }
  1023. break;
  1024. }
  1025. }
  1026. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  1027. AVFrame *picture)
  1028. {
  1029. int compno, reslevelno, bandno;
  1030. int x, y;
  1031. uint8_t *line;
  1032. Jpeg2000T1Context t1;
  1033. /* Loop on tile components */
  1034. for (compno = 0; compno < s->ncomponents; compno++) {
  1035. Jpeg2000Component *comp = tile->comp + compno;
  1036. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1037. /* Loop on resolution levels */
  1038. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  1039. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1040. /* Loop on bands */
  1041. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  1042. int nb_precincts, precno;
  1043. Jpeg2000Band *band = rlevel->band + bandno;
  1044. int cblkno = 0, bandpos;
  1045. bandpos = bandno + (reslevelno > 0);
  1046. if (band->coord[0][0] == band->coord[0][1] ||
  1047. band->coord[1][0] == band->coord[1][1])
  1048. continue;
  1049. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  1050. /* Loop on precincts */
  1051. for (precno = 0; precno < nb_precincts; precno++) {
  1052. Jpeg2000Prec *prec = band->prec + precno;
  1053. /* Loop on codeblocks */
  1054. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  1055. int x, y;
  1056. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  1057. decode_cblk(s, codsty, &t1, cblk,
  1058. cblk->coord[0][1] - cblk->coord[0][0],
  1059. cblk->coord[1][1] - cblk->coord[1][0],
  1060. bandpos);
  1061. x = cblk->coord[0][0];
  1062. y = cblk->coord[1][0];
  1063. if (codsty->transform == FF_DWT97)
  1064. dequantization_float(x, y, cblk, comp, &t1, band);
  1065. else
  1066. dequantization_int(x, y, cblk, comp, &t1, band);
  1067. } /* end cblk */
  1068. } /*end prec */
  1069. } /* end band */
  1070. } /* end reslevel */
  1071. /* inverse DWT */
  1072. ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
  1073. } /*end comp */
  1074. /* inverse MCT transformation */
  1075. if (tile->codsty[0].mct)
  1076. mct_decode(s, tile);
  1077. if (s->cdef[0] < 0) {
  1078. for (x = 0; x < s->ncomponents; x++)
  1079. s->cdef[x] = x + 1;
  1080. if ((s->ncomponents & 1) == 0)
  1081. s->cdef[s->ncomponents-1] = 0;
  1082. }
  1083. if (s->precision <= 8) {
  1084. for (compno = 0; compno < s->ncomponents; compno++) {
  1085. Jpeg2000Component *comp = tile->comp + compno;
  1086. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1087. float *datap = comp->f_data;
  1088. int32_t *i_datap = comp->i_data;
  1089. int cbps = s->cbps[compno];
  1090. int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
  1091. int planar = !!picture->data[2];
  1092. int pixelsize = planar ? 1 : s->ncomponents;
  1093. int plane = 0;
  1094. if (planar)
  1095. plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
  1096. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  1097. line = picture->data[plane] + y * picture->linesize[plane];
  1098. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  1099. uint8_t *dst;
  1100. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  1101. dst = line + x * pixelsize + compno*!planar;
  1102. if (codsty->transform == FF_DWT97) {
  1103. for (; x < w; x += s->cdx[compno]) {
  1104. int val = lrintf(*datap) + (1 << (cbps - 1));
  1105. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1106. val = av_clip(val, 0, (1 << cbps) - 1);
  1107. *dst = val << (8 - cbps);
  1108. datap++;
  1109. dst += pixelsize;
  1110. }
  1111. } else {
  1112. for (; x < w; x += s->cdx[compno]) {
  1113. int val = *i_datap + (1 << (cbps - 1));
  1114. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1115. val = av_clip(val, 0, (1 << cbps) - 1);
  1116. *dst = val << (8 - cbps);
  1117. i_datap++;
  1118. dst += pixelsize;
  1119. }
  1120. }
  1121. line += picture->linesize[plane];
  1122. }
  1123. }
  1124. } else {
  1125. for (compno = 0; compno < s->ncomponents; compno++) {
  1126. Jpeg2000Component *comp = tile->comp + compno;
  1127. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1128. float *datap = comp->f_data;
  1129. int32_t *i_datap = comp->i_data;
  1130. uint16_t *linel;
  1131. int cbps = s->cbps[compno];
  1132. int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
  1133. int planar = !!picture->data[2];
  1134. int pixelsize = planar ? 1 : s->ncomponents;
  1135. int plane = 0;
  1136. if (planar)
  1137. plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
  1138. y = tile->comp[compno].coord[1][0] - s->image_offset_y;
  1139. linel = (uint16_t *)picture->data[plane] + y * (picture->linesize[plane] >> 1);
  1140. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
  1141. uint16_t *dst;
  1142. x = tile->comp[compno].coord[0][0] - s->image_offset_x;
  1143. dst = linel + (x * pixelsize + compno*!planar);
  1144. if (codsty->transform == FF_DWT97) {
  1145. for (; x < w; x += s-> cdx[compno]) {
  1146. int val = lrintf(*datap) + (1 << (cbps - 1));
  1147. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1148. val = av_clip(val, 0, (1 << cbps) - 1);
  1149. /* align 12 bit values in little-endian mode */
  1150. *dst = val << (16 - cbps);
  1151. datap++;
  1152. dst += pixelsize;
  1153. }
  1154. } else {
  1155. for (; x < w; x += s-> cdx[compno]) {
  1156. int val = *i_datap + (1 << (cbps - 1));
  1157. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1158. val = av_clip(val, 0, (1 << cbps) - 1);
  1159. /* align 12 bit values in little-endian mode */
  1160. *dst = val << (16 - cbps);
  1161. i_datap++;
  1162. dst += pixelsize;
  1163. }
  1164. }
  1165. linel += picture->linesize[plane] >> 1;
  1166. }
  1167. }
  1168. }
  1169. return 0;
  1170. }
  1171. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  1172. {
  1173. int tileno, compno;
  1174. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1175. for (compno = 0; compno < s->ncomponents; compno++) {
  1176. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1177. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1178. ff_jpeg2000_cleanup(comp, codsty);
  1179. }
  1180. av_freep(&s->tile[tileno].comp);
  1181. }
  1182. av_freep(&s->tile);
  1183. s->numXtiles = s->numYtiles = 0;
  1184. }
  1185. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1186. {
  1187. Jpeg2000CodingStyle *codsty = s->codsty;
  1188. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1189. uint8_t *properties = s->properties;
  1190. for (;;) {
  1191. int len, ret = 0;
  1192. uint16_t marker;
  1193. int oldpos;
  1194. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1195. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1196. break;
  1197. }
  1198. marker = bytestream2_get_be16u(&s->g);
  1199. oldpos = bytestream2_tell(&s->g);
  1200. if (marker == JPEG2000_SOD) {
  1201. Jpeg2000Tile *tile;
  1202. Jpeg2000TilePart *tp;
  1203. if (s->curtileno < 0) {
  1204. av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
  1205. return AVERROR_INVALIDDATA;
  1206. }
  1207. tile = s->tile + s->curtileno;
  1208. tp = tile->tile_part + tile->tp_idx;
  1209. if (tp->tp_end < s->g.buffer) {
  1210. av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
  1211. return AVERROR_INVALIDDATA;
  1212. }
  1213. bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
  1214. bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
  1215. continue;
  1216. }
  1217. if (marker == JPEG2000_EOC)
  1218. break;
  1219. len = bytestream2_get_be16(&s->g);
  1220. if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
  1221. return AVERROR_INVALIDDATA;
  1222. switch (marker) {
  1223. case JPEG2000_SIZ:
  1224. ret = get_siz(s);
  1225. if (!s->tile)
  1226. s->numXtiles = s->numYtiles = 0;
  1227. break;
  1228. case JPEG2000_COC:
  1229. ret = get_coc(s, codsty, properties);
  1230. break;
  1231. case JPEG2000_COD:
  1232. ret = get_cod(s, codsty, properties);
  1233. break;
  1234. case JPEG2000_QCC:
  1235. ret = get_qcc(s, len, qntsty, properties);
  1236. break;
  1237. case JPEG2000_QCD:
  1238. ret = get_qcd(s, len, qntsty, properties);
  1239. break;
  1240. case JPEG2000_SOT:
  1241. if (!(ret = get_sot(s, len))) {
  1242. av_assert1(s->curtileno >= 0);
  1243. codsty = s->tile[s->curtileno].codsty;
  1244. qntsty = s->tile[s->curtileno].qntsty;
  1245. properties = s->tile[s->curtileno].properties;
  1246. }
  1247. break;
  1248. case JPEG2000_COM:
  1249. // the comment is ignored
  1250. bytestream2_skip(&s->g, len - 2);
  1251. break;
  1252. case JPEG2000_TLM:
  1253. // Tile-part lengths
  1254. ret = get_tlm(s, len);
  1255. break;
  1256. default:
  1257. av_log(s->avctx, AV_LOG_ERROR,
  1258. "unsupported marker 0x%.4X at pos 0x%X\n",
  1259. marker, bytestream2_tell(&s->g) - 4);
  1260. bytestream2_skip(&s->g, len - 2);
  1261. break;
  1262. }
  1263. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  1264. av_log(s->avctx, AV_LOG_ERROR,
  1265. "error during processing marker segment %.4x\n", marker);
  1266. return ret ? ret : -1;
  1267. }
  1268. }
  1269. return 0;
  1270. }
  1271. /* Read bit stream packets --> T2 operation. */
  1272. static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
  1273. {
  1274. int ret = 0;
  1275. int tileno;
  1276. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1277. Jpeg2000Tile *tile = s->tile + tileno;
  1278. if (ret = init_tile(s, tileno))
  1279. return ret;
  1280. s->g = tile->tile_part[0].tpg;
  1281. if (ret = jpeg2000_decode_packets(s, tile))
  1282. return ret;
  1283. }
  1284. return 0;
  1285. }
  1286. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1287. {
  1288. uint32_t atom_size, atom, atom_end;
  1289. int search_range = 10;
  1290. while (search_range
  1291. &&
  1292. bytestream2_get_bytes_left(&s->g) >= 8) {
  1293. atom_size = bytestream2_get_be32u(&s->g);
  1294. atom = bytestream2_get_be32u(&s->g);
  1295. atom_end = bytestream2_tell(&s->g) + atom_size - 8;
  1296. if (atom == JP2_CODESTREAM)
  1297. return 1;
  1298. if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
  1299. return 0;
  1300. if (atom == JP2_HEADER &&
  1301. atom_size >= 16) {
  1302. uint32_t atom2_size, atom2, atom2_end;
  1303. do {
  1304. atom2_size = bytestream2_get_be32u(&s->g);
  1305. atom2 = bytestream2_get_be32u(&s->g);
  1306. atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
  1307. if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
  1308. break;
  1309. if (atom2 == JP2_CODESTREAM) {
  1310. return 1;
  1311. } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
  1312. int method = bytestream2_get_byteu(&s->g);
  1313. bytestream2_skipu(&s->g, 2);
  1314. if (method == 1) {
  1315. s->colour_space = bytestream2_get_be32u(&s->g);
  1316. }
  1317. } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
  1318. int i, size, colour_count, colour_channels, colour_depth[3];
  1319. uint32_t r, g, b;
  1320. colour_count = bytestream2_get_be16u(&s->g);
  1321. colour_channels = bytestream2_get_byteu(&s->g);
  1322. // FIXME: Do not ignore channel_sign
  1323. colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
  1324. colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
  1325. colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
  1326. size = (colour_depth[0] + 7 >> 3) * colour_count +
  1327. (colour_depth[1] + 7 >> 3) * colour_count +
  1328. (colour_depth[2] + 7 >> 3) * colour_count;
  1329. if (colour_count > 256 ||
  1330. colour_channels != 3 ||
  1331. colour_depth[0] > 16 ||
  1332. colour_depth[1] > 16 ||
  1333. colour_depth[2] > 16 ||
  1334. atom2_size < size) {
  1335. avpriv_request_sample(s->avctx, "Unknown palette");
  1336. bytestream2_seek(&s->g, atom2_end, SEEK_SET);
  1337. continue;
  1338. }
  1339. s->pal8 = 1;
  1340. for (i = 0; i < colour_count; i++) {
  1341. if (colour_depth[0] <= 8) {
  1342. r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
  1343. r |= r >> colour_depth[0];
  1344. } else {
  1345. r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
  1346. }
  1347. if (colour_depth[1] <= 8) {
  1348. g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
  1349. r |= r >> colour_depth[1];
  1350. } else {
  1351. g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
  1352. }
  1353. if (colour_depth[2] <= 8) {
  1354. b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
  1355. r |= r >> colour_depth[2];
  1356. } else {
  1357. b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
  1358. }
  1359. s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
  1360. }
  1361. } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
  1362. int n = bytestream2_get_be16u(&s->g);
  1363. for (; n>0; n--) {
  1364. int cn = bytestream2_get_be16(&s->g);
  1365. int av_unused typ = bytestream2_get_be16(&s->g);
  1366. int asoc = bytestream2_get_be16(&s->g);
  1367. if (cn < 4 || asoc < 4)
  1368. s->cdef[cn] = asoc;
  1369. }
  1370. }
  1371. bytestream2_seek(&s->g, atom2_end, SEEK_SET);
  1372. } while (atom_end - atom2_end >= 8);
  1373. } else {
  1374. search_range--;
  1375. }
  1376. bytestream2_seek(&s->g, atom_end, SEEK_SET);
  1377. }
  1378. return 0;
  1379. }
  1380. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1381. int *got_frame, AVPacket *avpkt)
  1382. {
  1383. Jpeg2000DecoderContext *s = avctx->priv_data;
  1384. ThreadFrame frame = { .f = data };
  1385. AVFrame *picture = data;
  1386. int tileno, ret;
  1387. s->avctx = avctx;
  1388. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  1389. s->curtileno = -1;
  1390. memset(s->cdef, -1, sizeof(s->cdef));
  1391. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1392. ret = AVERROR_INVALIDDATA;
  1393. goto end;
  1394. }
  1395. // check if the image is in jp2 format
  1396. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  1397. (bytestream2_get_be32u(&s->g) == 12) &&
  1398. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  1399. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  1400. if (!jp2_find_codestream(s)) {
  1401. av_log(avctx, AV_LOG_ERROR,
  1402. "Could not find Jpeg2000 codestream atom.\n");
  1403. ret = AVERROR_INVALIDDATA;
  1404. goto end;
  1405. }
  1406. } else {
  1407. bytestream2_seek(&s->g, 0, SEEK_SET);
  1408. }
  1409. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  1410. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1411. ret = AVERROR_INVALIDDATA;
  1412. goto end;
  1413. }
  1414. if (ret = jpeg2000_read_main_headers(s))
  1415. goto end;
  1416. /* get picture buffer */
  1417. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1418. goto end;
  1419. picture->pict_type = AV_PICTURE_TYPE_I;
  1420. picture->key_frame = 1;
  1421. if (ret = jpeg2000_read_bitstream_packets(s))
  1422. goto end;
  1423. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1424. if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
  1425. goto end;
  1426. jpeg2000_dec_cleanup(s);
  1427. *got_frame = 1;
  1428. if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
  1429. memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
  1430. return bytestream2_tell(&s->g);
  1431. end:
  1432. jpeg2000_dec_cleanup(s);
  1433. return ret;
  1434. }
  1435. static void jpeg2000_init_static_data(AVCodec *codec)
  1436. {
  1437. ff_jpeg2000_init_tier1_luts();
  1438. ff_mqc_init_context_tables();
  1439. }
  1440. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1441. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1442. static const AVOption options[] = {
  1443. { "lowres", "Lower the decoding resolution by a power of two",
  1444. OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1445. { NULL },
  1446. };
  1447. static const AVProfile profiles[] = {
  1448. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1449. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1450. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1451. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1452. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1453. { FF_PROFILE_UNKNOWN },
  1454. };
  1455. static const AVClass jpeg2000_class = {
  1456. .class_name = "jpeg2000",
  1457. .item_name = av_default_item_name,
  1458. .option = options,
  1459. .version = LIBAVUTIL_VERSION_INT,
  1460. };
  1461. AVCodec ff_jpeg2000_decoder = {
  1462. .name = "jpeg2000",
  1463. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1464. .type = AVMEDIA_TYPE_VIDEO,
  1465. .id = AV_CODEC_ID_JPEG2000,
  1466. .capabilities = CODEC_CAP_FRAME_THREADS,
  1467. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1468. .init_static_data = jpeg2000_init_static_data,
  1469. .decode = jpeg2000_decode_frame,
  1470. .priv_class = &jpeg2000_class,
  1471. .max_lowres = 5,
  1472. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1473. };