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.

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