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.

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