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.

2184 lines
84KB

  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 <inttypes.h>
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "avcodec.h"
  34. #include "bytestream.h"
  35. #include "internal.h"
  36. #include "thread.h"
  37. #include "jpeg2000.h"
  38. #include "jpeg2000dsp.h"
  39. #define JP2_SIG_TYPE 0x6A502020
  40. #define JP2_SIG_VALUE 0x0D0A870A
  41. #define JP2_CODESTREAM 0x6A703263
  42. #define JP2_HEADER 0x6A703268
  43. #define HAD_COC 0x01
  44. #define HAD_QCC 0x02
  45. #define MAX_POCS 32
  46. typedef struct Jpeg2000POCEntry {
  47. uint16_t LYEpoc;
  48. uint16_t CSpoc;
  49. uint16_t CEpoc;
  50. uint8_t RSpoc;
  51. uint8_t REpoc;
  52. uint8_t Ppoc;
  53. } Jpeg2000POCEntry;
  54. typedef struct Jpeg2000POC {
  55. Jpeg2000POCEntry poc[MAX_POCS];
  56. int nb_poc;
  57. int is_default;
  58. } Jpeg2000POC;
  59. typedef struct Jpeg2000TilePart {
  60. uint8_t tile_index; // Tile index who refers the tile-part
  61. const uint8_t *tp_end;
  62. GetByteContext tpg; // bit stream in tile-part
  63. } Jpeg2000TilePart;
  64. /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
  65. * one per component, so tile_part elements have a size of 3 */
  66. typedef struct Jpeg2000Tile {
  67. Jpeg2000Component *comp;
  68. uint8_t properties[4];
  69. Jpeg2000CodingStyle codsty[4];
  70. Jpeg2000QuantStyle qntsty[4];
  71. Jpeg2000POC poc;
  72. Jpeg2000TilePart tile_part[256];
  73. uint16_t tp_idx; // Tile-part index
  74. int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  75. } Jpeg2000Tile;
  76. typedef struct Jpeg2000DecoderContext {
  77. AVClass *class;
  78. AVCodecContext *avctx;
  79. GetByteContext g;
  80. int width, height;
  81. int image_offset_x, image_offset_y;
  82. int tile_offset_x, tile_offset_y;
  83. uint8_t cbps[4]; // bits per sample in particular components
  84. uint8_t sgnd[4]; // if a component is signed
  85. uint8_t properties[4];
  86. int cdx[4], cdy[4];
  87. int precision;
  88. int ncomponents;
  89. int colour_space;
  90. uint32_t palette[256];
  91. int8_t pal8;
  92. int cdef[4];
  93. int tile_width, tile_height;
  94. unsigned numXtiles, numYtiles;
  95. int maxtilelen;
  96. Jpeg2000CodingStyle codsty[4];
  97. Jpeg2000QuantStyle qntsty[4];
  98. Jpeg2000POC poc;
  99. int bit_index;
  100. int curtileno;
  101. Jpeg2000Tile *tile;
  102. Jpeg2000DSPContext dsp;
  103. /*options parameters*/
  104. int reduction_factor;
  105. } Jpeg2000DecoderContext;
  106. /* get_bits functions for JPEG2000 packet bitstream
  107. * It is a get_bit function with a bit-stuffing routine. If the value of the
  108. * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
  109. * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
  110. static int get_bits(Jpeg2000DecoderContext *s, int n)
  111. {
  112. int res = 0;
  113. while (--n >= 0) {
  114. res <<= 1;
  115. if (s->bit_index == 0) {
  116. s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
  117. }
  118. s->bit_index--;
  119. res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
  120. }
  121. return res;
  122. }
  123. static void jpeg2000_flush(Jpeg2000DecoderContext *s)
  124. {
  125. if (bytestream2_get_byte(&s->g) == 0xff)
  126. bytestream2_skip(&s->g, 1);
  127. s->bit_index = 8;
  128. }
  129. /* decode the value stored in node */
  130. static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
  131. int threshold)
  132. {
  133. Jpeg2000TgtNode *stack[30];
  134. int sp = -1, curval = 0;
  135. if (!node) {
  136. av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
  137. return AVERROR_INVALIDDATA;
  138. }
  139. while (node && !node->vis) {
  140. stack[++sp] = node;
  141. node = node->parent;
  142. }
  143. if (node)
  144. curval = node->val;
  145. else
  146. curval = stack[sp]->val;
  147. while (curval < threshold && sp >= 0) {
  148. if (curval < stack[sp]->val)
  149. curval = stack[sp]->val;
  150. while (curval < threshold) {
  151. int ret;
  152. if ((ret = get_bits(s, 1)) > 0) {
  153. stack[sp]->vis++;
  154. break;
  155. } else if (!ret)
  156. curval++;
  157. else
  158. return ret;
  159. }
  160. stack[sp]->val = curval;
  161. sp--;
  162. }
  163. return curval;
  164. }
  165. static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
  166. int bpc, uint32_t log2_chroma_wh, int pal8)
  167. {
  168. int match = 1;
  169. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  170. av_assert2(desc);
  171. if (desc->nb_components != components) {
  172. return 0;
  173. }
  174. switch (components) {
  175. case 4:
  176. match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
  177. (log2_chroma_wh >> 14 & 3) == 0 &&
  178. (log2_chroma_wh >> 12 & 3) == 0;
  179. case 3:
  180. match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
  181. (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
  182. (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
  183. case 2:
  184. match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
  185. (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
  186. (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
  187. case 1:
  188. match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
  189. (log2_chroma_wh >> 2 & 3) == 0 &&
  190. (log2_chroma_wh & 3) == 0 &&
  191. (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
  192. }
  193. return match;
  194. }
  195. // pix_fmts with lower bpp have to be listed before
  196. // similar pix_fmts with higher bpp.
  197. #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
  198. #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
  199. #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
  200. AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
  201. AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
  202. AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
  203. AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
  204. AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
  205. AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
  206. AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
  207. AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
  208. AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
  209. AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
  210. #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
  211. static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
  212. static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
  213. static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
  214. static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS,
  215. YUV_PIXEL_FORMATS};
  216. static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
  217. GRAY_PIXEL_FORMATS,
  218. YUV_PIXEL_FORMATS,
  219. XYZ_PIXEL_FORMATS};
  220. /* marker segments */
  221. /* get sizes and offsets of image, tiles; number of components */
  222. static int get_siz(Jpeg2000DecoderContext *s)
  223. {
  224. int i;
  225. int ncomponents;
  226. uint32_t log2_chroma_wh = 0;
  227. const enum AVPixelFormat *possible_fmts = NULL;
  228. int possible_fmts_nb = 0;
  229. if (bytestream2_get_bytes_left(&s->g) < 36) {
  230. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
  231. return AVERROR_INVALIDDATA;
  232. }
  233. s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
  234. s->width = bytestream2_get_be32u(&s->g); // Width
  235. s->height = bytestream2_get_be32u(&s->g); // Height
  236. s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
  237. s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
  238. s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
  239. s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
  240. s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
  241. s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
  242. ncomponents = bytestream2_get_be16u(&s->g); // CSiz
  243. if (s->image_offset_x || s->image_offset_y) {
  244. avpriv_request_sample(s->avctx, "Support for image offsets");
  245. return AVERROR_PATCHWELCOME;
  246. }
  247. if (av_image_check_size(s->width, s->height, 0, s->avctx)) {
  248. avpriv_request_sample(s->avctx, "Large Dimensions");
  249. return AVERROR_PATCHWELCOME;
  250. }
  251. if (ncomponents <= 0) {
  252. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
  253. s->ncomponents);
  254. return AVERROR_INVALIDDATA;
  255. }
  256. if (ncomponents > 4) {
  257. avpriv_request_sample(s->avctx, "Support for %d components",
  258. ncomponents);
  259. return AVERROR_PATCHWELCOME;
  260. }
  261. s->ncomponents = ncomponents;
  262. if (s->tile_width <= 0 || s->tile_height <= 0) {
  263. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
  264. s->tile_width, s->tile_height);
  265. return AVERROR_INVALIDDATA;
  266. }
  267. if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
  268. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
  269. return AVERROR_INVALIDDATA;
  270. }
  271. for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
  272. uint8_t x = bytestream2_get_byteu(&s->g);
  273. s->cbps[i] = (x & 0x7f) + 1;
  274. s->precision = FFMAX(s->cbps[i], s->precision);
  275. s->sgnd[i] = !!(x & 0x80);
  276. s->cdx[i] = bytestream2_get_byteu(&s->g);
  277. s->cdy[i] = bytestream2_get_byteu(&s->g);
  278. if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
  279. || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
  280. av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
  281. return AVERROR_INVALIDDATA;
  282. }
  283. log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
  284. }
  285. s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
  286. s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
  287. if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
  288. s->numXtiles = s->numYtiles = 0;
  289. return AVERROR(EINVAL);
  290. }
  291. s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
  292. if (!s->tile) {
  293. s->numXtiles = s->numYtiles = 0;
  294. return AVERROR(ENOMEM);
  295. }
  296. for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
  297. Jpeg2000Tile *tile = s->tile + i;
  298. tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
  299. if (!tile->comp)
  300. return AVERROR(ENOMEM);
  301. }
  302. /* compute image size with reduction factor */
  303. s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
  304. s->reduction_factor);
  305. s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
  306. s->reduction_factor);
  307. if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
  308. s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
  309. possible_fmts = xyz_pix_fmts;
  310. possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
  311. } else {
  312. switch (s->colour_space) {
  313. case 16:
  314. possible_fmts = rgb_pix_fmts;
  315. possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
  316. break;
  317. case 17:
  318. possible_fmts = gray_pix_fmts;
  319. possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
  320. break;
  321. case 18:
  322. possible_fmts = yuv_pix_fmts;
  323. possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
  324. break;
  325. default:
  326. possible_fmts = all_pix_fmts;
  327. possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
  328. break;
  329. }
  330. }
  331. for (i = 0; i < possible_fmts_nb; ++i) {
  332. if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
  333. s->avctx->pix_fmt = possible_fmts[i];
  334. break;
  335. }
  336. }
  337. if (i == possible_fmts_nb) {
  338. if (ncomponents == 4 &&
  339. s->cdy[0] == 1 && s->cdx[0] == 1 &&
  340. s->cdy[1] == 1 && s->cdx[1] == 1 &&
  341. s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
  342. if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
  343. s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  344. s->cdef[0] = 0;
  345. s->cdef[1] = 1;
  346. s->cdef[2] = 2;
  347. s->cdef[3] = 3;
  348. i = 0;
  349. }
  350. }
  351. }
  352. if (i == possible_fmts_nb) {
  353. av_log(s->avctx, AV_LOG_ERROR,
  354. "Unknown pix_fmt, profile: %d, colour_space: %d, "
  355. "components: %d, precision: %d\n"
  356. "cdx[0]: %d, cdy[0]: %d\n"
  357. "cdx[1]: %d, cdy[1]: %d\n"
  358. "cdx[2]: %d, cdy[2]: %d\n"
  359. "cdx[3]: %d, cdy[3]: %d\n",
  360. s->avctx->profile, s->colour_space, ncomponents, s->precision,
  361. s->cdx[0],
  362. s->cdy[0],
  363. ncomponents > 1 ? s->cdx[1] : 0,
  364. ncomponents > 1 ? s->cdy[1] : 0,
  365. ncomponents > 2 ? s->cdx[2] : 0,
  366. ncomponents > 2 ? s->cdy[2] : 0,
  367. ncomponents > 3 ? s->cdx[3] : 0,
  368. ncomponents > 3 ? s->cdy[3] : 0);
  369. return AVERROR_PATCHWELCOME;
  370. }
  371. s->avctx->bits_per_raw_sample = s->precision;
  372. return 0;
  373. }
  374. /* get common part for COD and COC segments */
  375. static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
  376. {
  377. uint8_t byte;
  378. if (bytestream2_get_bytes_left(&s->g) < 5) {
  379. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
  380. return AVERROR_INVALIDDATA;
  381. }
  382. /* nreslevels = number of resolution levels
  383. = number of decomposition level +1 */
  384. c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
  385. if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
  386. av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
  387. return AVERROR_INVALIDDATA;
  388. }
  389. if (c->nreslevels <= s->reduction_factor) {
  390. /* we are forced to update reduction_factor as its requested value is
  391. not compatible with this bitstream, and as we might have used it
  392. already in setup earlier we have to fail this frame until
  393. reinitialization is implemented */
  394. av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
  395. s->reduction_factor = c->nreslevels - 1;
  396. return AVERROR(EINVAL);
  397. }
  398. /* compute number of resolution levels to decode */
  399. c->nreslevels2decode = c->nreslevels - s->reduction_factor;
  400. c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
  401. c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
  402. if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
  403. c->log2_cblk_width + c->log2_cblk_height > 12) {
  404. av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
  405. return AVERROR_INVALIDDATA;
  406. }
  407. c->cblk_style = bytestream2_get_byteu(&s->g);
  408. if (c->cblk_style != 0) { // cblk style
  409. av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
  410. if (c->cblk_style & JPEG2000_CBLK_BYPASS)
  411. av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
  412. }
  413. c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
  414. /* set integer 9/7 DWT in case of BITEXACT flag */
  415. if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
  416. c->transform = FF_DWT97_INT;
  417. else if (c->transform == FF_DWT53) {
  418. s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
  419. }
  420. if (c->csty & JPEG2000_CSTY_PREC) {
  421. int i;
  422. for (i = 0; i < c->nreslevels; i++) {
  423. byte = bytestream2_get_byte(&s->g);
  424. c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
  425. c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
  426. if (i)
  427. if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
  428. av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
  429. c->log2_prec_widths[i], c->log2_prec_heights[i]);
  430. c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
  431. return AVERROR_INVALIDDATA;
  432. }
  433. }
  434. } else {
  435. memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
  436. memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
  437. }
  438. return 0;
  439. }
  440. /* get coding parameters for a particular tile or whole image*/
  441. static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  442. uint8_t *properties)
  443. {
  444. Jpeg2000CodingStyle tmp;
  445. int compno, ret;
  446. if (bytestream2_get_bytes_left(&s->g) < 5) {
  447. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
  448. return AVERROR_INVALIDDATA;
  449. }
  450. tmp.csty = bytestream2_get_byteu(&s->g);
  451. // get progression order
  452. tmp.prog_order = bytestream2_get_byteu(&s->g);
  453. tmp.nlayers = bytestream2_get_be16u(&s->g);
  454. tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
  455. if (tmp.mct && s->ncomponents < 3) {
  456. av_log(s->avctx, AV_LOG_ERROR,
  457. "MCT %"PRIu8" with too few components (%d)\n",
  458. tmp.mct, s->ncomponents);
  459. return AVERROR_INVALIDDATA;
  460. }
  461. if ((ret = get_cox(s, &tmp)) < 0)
  462. return ret;
  463. for (compno = 0; compno < s->ncomponents; compno++)
  464. if (!(properties[compno] & HAD_COC))
  465. memcpy(c + compno, &tmp, sizeof(tmp));
  466. return 0;
  467. }
  468. /* Get coding parameters for a component in the whole image or a
  469. * particular tile. */
  470. static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  471. uint8_t *properties)
  472. {
  473. int compno, ret;
  474. if (bytestream2_get_bytes_left(&s->g) < 2) {
  475. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
  476. return AVERROR_INVALIDDATA;
  477. }
  478. compno = bytestream2_get_byteu(&s->g);
  479. if (compno >= s->ncomponents) {
  480. av_log(s->avctx, AV_LOG_ERROR,
  481. "Invalid compno %d. There are %d components in the image.\n",
  482. compno, s->ncomponents);
  483. return AVERROR_INVALIDDATA;
  484. }
  485. c += compno;
  486. c->csty = bytestream2_get_byteu(&s->g);
  487. if ((ret = get_cox(s, c)) < 0)
  488. return ret;
  489. properties[compno] |= HAD_COC;
  490. return 0;
  491. }
  492. /* Get common part for QCD and QCC segments. */
  493. static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
  494. {
  495. int i, x;
  496. if (bytestream2_get_bytes_left(&s->g) < 1)
  497. return AVERROR_INVALIDDATA;
  498. x = bytestream2_get_byteu(&s->g); // Sqcd
  499. q->nguardbits = x >> 5;
  500. q->quantsty = x & 0x1f;
  501. if (q->quantsty == JPEG2000_QSTY_NONE) {
  502. n -= 3;
  503. if (bytestream2_get_bytes_left(&s->g) < n ||
  504. n > JPEG2000_MAX_DECLEVELS*3)
  505. return AVERROR_INVALIDDATA;
  506. for (i = 0; i < n; i++)
  507. q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
  508. } else if (q->quantsty == JPEG2000_QSTY_SI) {
  509. if (bytestream2_get_bytes_left(&s->g) < 2)
  510. return AVERROR_INVALIDDATA;
  511. x = bytestream2_get_be16u(&s->g);
  512. q->expn[0] = x >> 11;
  513. q->mant[0] = x & 0x7ff;
  514. for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
  515. int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
  516. q->expn[i] = curexpn;
  517. q->mant[i] = q->mant[0];
  518. }
  519. } else {
  520. n = (n - 3) >> 1;
  521. if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
  522. n > JPEG2000_MAX_DECLEVELS*3)
  523. return AVERROR_INVALIDDATA;
  524. for (i = 0; i < n; i++) {
  525. x = bytestream2_get_be16u(&s->g);
  526. q->expn[i] = x >> 11;
  527. q->mant[i] = x & 0x7ff;
  528. }
  529. }
  530. return 0;
  531. }
  532. /* Get quantization parameters for a particular tile or a whole image. */
  533. static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  534. uint8_t *properties)
  535. {
  536. Jpeg2000QuantStyle tmp;
  537. int compno, ret;
  538. memset(&tmp, 0, sizeof(tmp));
  539. if ((ret = get_qcx(s, n, &tmp)) < 0)
  540. return ret;
  541. for (compno = 0; compno < s->ncomponents; compno++)
  542. if (!(properties[compno] & HAD_QCC))
  543. memcpy(q + compno, &tmp, sizeof(tmp));
  544. return 0;
  545. }
  546. /* Get quantization parameters for a component in the whole image
  547. * on in a particular tile. */
  548. static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  549. uint8_t *properties)
  550. {
  551. int compno;
  552. if (bytestream2_get_bytes_left(&s->g) < 1)
  553. return AVERROR_INVALIDDATA;
  554. compno = bytestream2_get_byteu(&s->g);
  555. if (compno >= s->ncomponents) {
  556. av_log(s->avctx, AV_LOG_ERROR,
  557. "Invalid compno %d. There are %d components in the image.\n",
  558. compno, s->ncomponents);
  559. return AVERROR_INVALIDDATA;
  560. }
  561. properties[compno] |= HAD_QCC;
  562. return get_qcx(s, n - 1, q + compno);
  563. }
  564. static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
  565. {
  566. int i;
  567. int elem_size = s->ncomponents <= 257 ? 7 : 9;
  568. Jpeg2000POC tmp = {{{0}}};
  569. if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
  570. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
  571. return AVERROR_INVALIDDATA;
  572. }
  573. if (elem_size > 7) {
  574. avpriv_request_sample(s->avctx, "Fat POC not supported");
  575. return AVERROR_PATCHWELCOME;
  576. }
  577. tmp.nb_poc = (size - 2) / elem_size;
  578. if (tmp.nb_poc > MAX_POCS) {
  579. avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
  580. return AVERROR_PATCHWELCOME;
  581. }
  582. for (i = 0; i<tmp.nb_poc; i++) {
  583. Jpeg2000POCEntry *e = &tmp.poc[i];
  584. e->RSpoc = bytestream2_get_byteu(&s->g);
  585. e->CSpoc = bytestream2_get_byteu(&s->g);
  586. e->LYEpoc = bytestream2_get_be16u(&s->g);
  587. e->REpoc = bytestream2_get_byteu(&s->g);
  588. e->CEpoc = bytestream2_get_byteu(&s->g);
  589. e->Ppoc = bytestream2_get_byteu(&s->g);
  590. if (!e->CEpoc)
  591. e->CEpoc = 256;
  592. if (e->CEpoc > s->ncomponents)
  593. e->CEpoc = s->ncomponents;
  594. if ( e->RSpoc >= e->REpoc || e->REpoc > 33
  595. || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
  596. || !e->LYEpoc) {
  597. av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
  598. e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
  599. );
  600. return AVERROR_INVALIDDATA;
  601. }
  602. }
  603. if (!p->nb_poc || p->is_default) {
  604. *p = tmp;
  605. } else {
  606. if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
  607. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
  608. return AVERROR_INVALIDDATA;
  609. }
  610. memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
  611. p->nb_poc += tmp.nb_poc;
  612. }
  613. p->is_default = 0;
  614. return 0;
  615. }
  616. /* Get start of tile segment. */
  617. static int get_sot(Jpeg2000DecoderContext *s, int n)
  618. {
  619. Jpeg2000TilePart *tp;
  620. uint16_t Isot;
  621. uint32_t Psot;
  622. unsigned TPsot;
  623. if (bytestream2_get_bytes_left(&s->g) < 8)
  624. return AVERROR_INVALIDDATA;
  625. s->curtileno = 0;
  626. Isot = bytestream2_get_be16u(&s->g); // Isot
  627. if (Isot >= s->numXtiles * s->numYtiles)
  628. return AVERROR_INVALIDDATA;
  629. s->curtileno = Isot;
  630. Psot = bytestream2_get_be32u(&s->g); // Psot
  631. TPsot = bytestream2_get_byteu(&s->g); // TPsot
  632. /* Read TNSot but not used */
  633. bytestream2_get_byteu(&s->g); // TNsot
  634. if (!Psot)
  635. Psot = bytestream2_get_bytes_left(&s->g) + n + 2;
  636. if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
  637. av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
  638. return AVERROR_INVALIDDATA;
  639. }
  640. av_assert0(TPsot < FF_ARRAY_ELEMS(s->tile[Isot].tile_part));
  641. s->tile[Isot].tp_idx = TPsot;
  642. tp = s->tile[Isot].tile_part + TPsot;
  643. tp->tile_index = Isot;
  644. tp->tp_end = s->g.buffer + Psot - n - 2;
  645. if (!TPsot) {
  646. Jpeg2000Tile *tile = s->tile + s->curtileno;
  647. /* copy defaults */
  648. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
  649. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
  650. memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
  651. tile->poc.is_default = 1;
  652. }
  653. return 0;
  654. }
  655. /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
  656. * Used to know the number of tile parts and lengths.
  657. * There may be multiple TLMs in the header.
  658. * TODO: The function is not used for tile-parts management, nor anywhere else.
  659. * It can be useful to allocate memory for tile parts, before managing the SOT
  660. * markers. Parsing the TLM header is needed to increment the input header
  661. * buffer.
  662. * This marker is mandatory for DCI. */
  663. static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
  664. {
  665. uint8_t Stlm, ST, SP, tile_tlm, i;
  666. bytestream2_get_byte(&s->g); /* Ztlm: skipped */
  667. Stlm = bytestream2_get_byte(&s->g);
  668. // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  669. ST = (Stlm >> 4) & 0x03;
  670. // TODO: Manage case of ST = 0b11 --> raise error
  671. SP = (Stlm >> 6) & 0x01;
  672. tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
  673. for (i = 0; i < tile_tlm; i++) {
  674. switch (ST) {
  675. case 0:
  676. break;
  677. case 1:
  678. bytestream2_get_byte(&s->g);
  679. break;
  680. case 2:
  681. bytestream2_get_be16(&s->g);
  682. break;
  683. case 3:
  684. bytestream2_get_be32(&s->g);
  685. break;
  686. }
  687. if (SP == 0) {
  688. bytestream2_get_be16(&s->g);
  689. } else {
  690. bytestream2_get_be32(&s->g);
  691. }
  692. }
  693. return 0;
  694. }
  695. static uint8_t get_plt(Jpeg2000DecoderContext *s, int n)
  696. {
  697. int i;
  698. av_log(s->avctx, AV_LOG_DEBUG,
  699. "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
  700. /*Zplt =*/ bytestream2_get_byte(&s->g);
  701. for (i = 0; i < n - 3; i++) {
  702. bytestream2_get_byte(&s->g);
  703. }
  704. return 0;
  705. }
  706. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  707. {
  708. int compno;
  709. int tilex = tileno % s->numXtiles;
  710. int tiley = tileno / s->numXtiles;
  711. Jpeg2000Tile *tile = s->tile + tileno;
  712. if (!tile->comp)
  713. return AVERROR(ENOMEM);
  714. tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
  715. tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
  716. tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
  717. tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
  718. for (compno = 0; compno < s->ncomponents; compno++) {
  719. Jpeg2000Component *comp = tile->comp + compno;
  720. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  721. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  722. int ret; // global bandno
  723. comp->coord_o[0][0] = tile->coord[0][0];
  724. comp->coord_o[0][1] = tile->coord[0][1];
  725. comp->coord_o[1][0] = tile->coord[1][0];
  726. comp->coord_o[1][1] = tile->coord[1][1];
  727. if (compno) {
  728. comp->coord_o[0][0] /= s->cdx[compno];
  729. comp->coord_o[0][1] /= s->cdx[compno];
  730. comp->coord_o[1][0] /= s->cdy[compno];
  731. comp->coord_o[1][1] /= s->cdy[compno];
  732. }
  733. comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
  734. comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
  735. comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
  736. comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
  737. if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  738. s->cbps[compno], s->cdx[compno],
  739. s->cdy[compno], s->avctx))
  740. return ret;
  741. }
  742. return 0;
  743. }
  744. /* Read the number of coding passes. */
  745. static int getnpasses(Jpeg2000DecoderContext *s)
  746. {
  747. int num;
  748. if (!get_bits(s, 1))
  749. return 1;
  750. if (!get_bits(s, 1))
  751. return 2;
  752. if ((num = get_bits(s, 2)) != 3)
  753. return num < 0 ? num : 3 + num;
  754. if ((num = get_bits(s, 5)) != 31)
  755. return num < 0 ? num : 6 + num;
  756. num = get_bits(s, 7);
  757. return num < 0 ? num : 37 + num;
  758. }
  759. static int getlblockinc(Jpeg2000DecoderContext *s)
  760. {
  761. int res = 0, ret;
  762. while (ret = get_bits(s, 1)) {
  763. if (ret < 0)
  764. return ret;
  765. res++;
  766. }
  767. return res;
  768. }
  769. static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
  770. Jpeg2000CodingStyle *codsty,
  771. Jpeg2000ResLevel *rlevel, int precno,
  772. int layno, uint8_t *expn, int numgbits)
  773. {
  774. int bandno, cblkno, ret, nb_code_blocks;
  775. int cwsno;
  776. if (layno < rlevel->band[0].prec[precno].decoded_layers)
  777. return 0;
  778. rlevel->band[0].prec[precno].decoded_layers = layno + 1;
  779. if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
  780. if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
  781. s->g = tile->tile_part[++(*tp_index)].tpg;
  782. }
  783. }
  784. if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
  785. bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
  786. if (!(ret = get_bits(s, 1))) {
  787. jpeg2000_flush(s);
  788. return 0;
  789. } else if (ret < 0)
  790. return ret;
  791. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  792. Jpeg2000Band *band = rlevel->band + bandno;
  793. Jpeg2000Prec *prec = band->prec + precno;
  794. if (band->coord[0][0] == band->coord[0][1] ||
  795. band->coord[1][0] == band->coord[1][1])
  796. continue;
  797. nb_code_blocks = prec->nb_codeblocks_height *
  798. prec->nb_codeblocks_width;
  799. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  800. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  801. int incl, newpasses, llen;
  802. if (cblk->npasses)
  803. incl = get_bits(s, 1);
  804. else
  805. incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
  806. if (!incl)
  807. continue;
  808. else if (incl < 0)
  809. return incl;
  810. if (!cblk->npasses) {
  811. int v = expn[bandno] + numgbits - 1 -
  812. tag_tree_decode(s, prec->zerobits + cblkno, 100);
  813. if (v < 0) {
  814. av_log(s->avctx, AV_LOG_ERROR,
  815. "nonzerobits %d invalid\n", v);
  816. return AVERROR_INVALIDDATA;
  817. }
  818. cblk->nonzerobits = v;
  819. }
  820. if ((newpasses = getnpasses(s)) < 0)
  821. return newpasses;
  822. av_assert2(newpasses > 0);
  823. if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
  824. avpriv_request_sample(s->avctx, "Too many passes");
  825. return AVERROR_PATCHWELCOME;
  826. }
  827. if ((llen = getlblockinc(s)) < 0)
  828. return llen;
  829. if (cblk->lblock + llen + av_log2(newpasses) > 16) {
  830. avpriv_request_sample(s->avctx,
  831. "Block with length beyond 16 bits");
  832. return AVERROR_PATCHWELCOME;
  833. }
  834. cblk->lblock += llen;
  835. cblk->nb_lengthinc = 0;
  836. cblk->nb_terminationsinc = 0;
  837. do {
  838. int newpasses1 = 0;
  839. while (newpasses1 < newpasses) {
  840. newpasses1 ++;
  841. if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
  842. cblk->nb_terminationsinc ++;
  843. break;
  844. }
  845. }
  846. if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
  847. return ret;
  848. if (ret > sizeof(cblk->data)) {
  849. avpriv_request_sample(s->avctx,
  850. "Block with lengthinc greater than %"SIZE_SPECIFIER"",
  851. sizeof(cblk->data));
  852. return AVERROR_PATCHWELCOME;
  853. }
  854. cblk->lengthinc[cblk->nb_lengthinc++] = ret;
  855. cblk->npasses += newpasses1;
  856. newpasses -= newpasses1;
  857. } while(newpasses);
  858. }
  859. }
  860. jpeg2000_flush(s);
  861. if (codsty->csty & JPEG2000_CSTY_EPH) {
  862. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
  863. bytestream2_skip(&s->g, 2);
  864. else
  865. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
  866. }
  867. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  868. Jpeg2000Band *band = rlevel->band + bandno;
  869. Jpeg2000Prec *prec = band->prec + precno;
  870. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  871. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  872. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  873. for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
  874. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
  875. || sizeof(cblk->data) < cblk->length + cblk->lengthinc[cwsno] + 4
  876. ) {
  877. av_log(s->avctx, AV_LOG_ERROR,
  878. "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
  879. cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
  880. return AVERROR_INVALIDDATA;
  881. }
  882. bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
  883. cblk->length += cblk->lengthinc[cwsno];
  884. cblk->lengthinc[cwsno] = 0;
  885. if (cblk->nb_terminationsinc) {
  886. cblk->nb_terminationsinc--;
  887. cblk->nb_terminations++;
  888. cblk->data[cblk->length++] = 0xFF;
  889. cblk->data[cblk->length++] = 0xFF;
  890. cblk->data_start[cblk->nb_terminations] = cblk->length;
  891. }
  892. }
  893. }
  894. }
  895. return 0;
  896. }
  897. static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  898. int RSpoc, int CSpoc,
  899. int LYEpoc, int REpoc, int CEpoc,
  900. int Ppoc, int *tp_index)
  901. {
  902. int ret = 0;
  903. int layno, reslevelno, compno, precno, ok_reslevel;
  904. int x, y;
  905. int step_x, step_y;
  906. switch (Ppoc) {
  907. case JPEG2000_PGOD_RLCP:
  908. av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
  909. ok_reslevel = 1;
  910. for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
  911. ok_reslevel = 0;
  912. for (layno = 0; layno < LYEpoc; layno++) {
  913. for (compno = CSpoc; compno < CEpoc; compno++) {
  914. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  915. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  916. if (reslevelno < codsty->nreslevels) {
  917. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  918. reslevelno;
  919. ok_reslevel = 1;
  920. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  921. if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
  922. codsty, rlevel,
  923. precno, layno,
  924. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  925. qntsty->nguardbits)) < 0)
  926. return ret;
  927. }
  928. }
  929. }
  930. }
  931. break;
  932. case JPEG2000_PGOD_LRCP:
  933. av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
  934. for (layno = 0; layno < LYEpoc; layno++) {
  935. ok_reslevel = 1;
  936. for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
  937. ok_reslevel = 0;
  938. for (compno = CSpoc; compno < CEpoc; compno++) {
  939. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  940. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  941. if (reslevelno < codsty->nreslevels) {
  942. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  943. reslevelno;
  944. ok_reslevel = 1;
  945. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  946. if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
  947. codsty, rlevel,
  948. precno, layno,
  949. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  950. qntsty->nguardbits)) < 0)
  951. return ret;
  952. }
  953. }
  954. }
  955. }
  956. break;
  957. case JPEG2000_PGOD_CPRL:
  958. av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
  959. for (compno = CSpoc; compno < CEpoc; compno++) {
  960. Jpeg2000Component *comp = tile->comp + compno;
  961. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  962. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  963. step_x = 32;
  964. step_y = 32;
  965. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  966. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  967. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  968. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  969. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  970. }
  971. av_assert0(step_x < 32 && step_y < 32);
  972. step_x = 1<<step_x;
  973. step_y = 1<<step_y;
  974. for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
  975. for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
  976. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  977. unsigned prcx, prcy;
  978. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  979. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  980. int xc = x / s->cdx[compno];
  981. int yc = y / s->cdy[compno];
  982. if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
  983. continue;
  984. if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
  985. continue;
  986. // check if a precinct exists
  987. prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
  988. prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
  989. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
  990. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
  991. precno = prcx + rlevel->num_precincts_x * prcy;
  992. if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
  993. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  994. prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
  995. continue;
  996. }
  997. for (layno = 0; layno < LYEpoc; layno++) {
  998. if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
  999. precno, layno,
  1000. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  1001. qntsty->nguardbits)) < 0)
  1002. return ret;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. break;
  1009. case JPEG2000_PGOD_RPCL:
  1010. av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
  1011. ok_reslevel = 1;
  1012. for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
  1013. ok_reslevel = 0;
  1014. step_x = 30;
  1015. step_y = 30;
  1016. for (compno = CSpoc; compno < CEpoc; compno++) {
  1017. Jpeg2000Component *comp = tile->comp + compno;
  1018. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1019. if (reslevelno < codsty->nreslevels) {
  1020. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1021. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1022. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  1023. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  1024. }
  1025. }
  1026. step_x = 1<<step_x;
  1027. step_y = 1<<step_y;
  1028. for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
  1029. for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
  1030. for (compno = CSpoc; compno < CEpoc; compno++) {
  1031. Jpeg2000Component *comp = tile->comp + compno;
  1032. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1033. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  1034. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1035. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1036. unsigned prcx, prcy;
  1037. int xc = x / s->cdx[compno];
  1038. int yc = y / s->cdy[compno];
  1039. if (reslevelno >= codsty->nreslevels)
  1040. continue;
  1041. if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
  1042. continue;
  1043. if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
  1044. continue;
  1045. // check if a precinct exists
  1046. prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
  1047. prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
  1048. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
  1049. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
  1050. precno = prcx + rlevel->num_precincts_x * prcy;
  1051. ok_reslevel = 1;
  1052. if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
  1053. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  1054. prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
  1055. continue;
  1056. }
  1057. for (layno = 0; layno < LYEpoc; layno++) {
  1058. if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
  1059. codsty, rlevel,
  1060. precno, layno,
  1061. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  1062. qntsty->nguardbits)) < 0)
  1063. return ret;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. }
  1069. break;
  1070. case JPEG2000_PGOD_PCRL:
  1071. av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
  1072. step_x = 32;
  1073. step_y = 32;
  1074. for (compno = CSpoc; compno < CEpoc; compno++) {
  1075. Jpeg2000Component *comp = tile->comp + compno;
  1076. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1077. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  1078. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1079. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1080. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  1081. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  1082. }
  1083. }
  1084. step_x = 1<<step_x;
  1085. step_y = 1<<step_y;
  1086. for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
  1087. for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
  1088. for (compno = CSpoc; compno < CEpoc; compno++) {
  1089. Jpeg2000Component *comp = tile->comp + compno;
  1090. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1091. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  1092. int xc = x / s->cdx[compno];
  1093. int yc = y / s->cdy[compno];
  1094. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  1095. unsigned prcx, prcy;
  1096. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1097. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1098. if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
  1099. continue;
  1100. if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
  1101. continue;
  1102. // check if a precinct exists
  1103. prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
  1104. prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
  1105. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
  1106. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
  1107. precno = prcx + rlevel->num_precincts_x * prcy;
  1108. if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
  1109. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  1110. prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
  1111. continue;
  1112. }
  1113. for (layno = 0; layno < LYEpoc; layno++) {
  1114. if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
  1115. precno, layno,
  1116. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  1117. qntsty->nguardbits)) < 0)
  1118. return ret;
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. break;
  1125. default:
  1126. break;
  1127. }
  1128. return ret;
  1129. }
  1130. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  1131. {
  1132. int ret = AVERROR_BUG;
  1133. int i;
  1134. int tp_index = 0;
  1135. s->bit_index = 8;
  1136. if (tile->poc.nb_poc) {
  1137. for (i=0; i<tile->poc.nb_poc; i++) {
  1138. Jpeg2000POCEntry *e = &tile->poc.poc[i];
  1139. ret = jpeg2000_decode_packets_po_iteration(s, tile,
  1140. e->RSpoc, e->CSpoc,
  1141. FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
  1142. e->REpoc,
  1143. FFMIN(e->CEpoc, s->ncomponents),
  1144. e->Ppoc, &tp_index
  1145. );
  1146. if (ret < 0)
  1147. return ret;
  1148. }
  1149. } else {
  1150. ret = jpeg2000_decode_packets_po_iteration(s, tile,
  1151. 0, 0,
  1152. tile->codsty[0].nlayers,
  1153. 33,
  1154. s->ncomponents,
  1155. tile->codsty[0].prog_order,
  1156. &tp_index
  1157. );
  1158. }
  1159. /* EOC marker reached */
  1160. bytestream2_skip(&s->g, 2);
  1161. return ret;
  1162. }
  1163. /* TIER-1 routines */
  1164. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  1165. int bpno, int bandno,
  1166. int vert_causal_ctx_csty_symbol)
  1167. {
  1168. int mask = 3 << (bpno - 1), y0, x, y;
  1169. for (y0 = 0; y0 < height; y0 += 4)
  1170. for (x = 0; x < width; x++)
  1171. for (y = y0; y < height && y < y0 + 4; y++) {
  1172. int flags_mask = -1;
  1173. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  1174. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
  1175. if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
  1176. && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  1177. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
  1178. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
  1179. if (t1->mqc.raw)
  1180. t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  1181. else
  1182. t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  1183. -mask : mask;
  1184. ff_jpeg2000_set_significance(t1, x, y,
  1185. t1->data[(y) * t1->stride + x] < 0);
  1186. }
  1187. t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
  1188. }
  1189. }
  1190. }
  1191. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  1192. int bpno, int vert_causal_ctx_csty_symbol)
  1193. {
  1194. int phalf, nhalf;
  1195. int y0, x, y;
  1196. phalf = 1 << (bpno - 1);
  1197. nhalf = -phalf;
  1198. for (y0 = 0; y0 < height; y0 += 4)
  1199. for (x = 0; x < width; x++)
  1200. for (y = y0; y < height && y < y0 + 4; y++)
  1201. if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  1202. int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
  1203. ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
  1204. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
  1205. int r = ff_mqc_decode(&t1->mqc,
  1206. t1->mqc.cx_states + ctxno)
  1207. ? phalf : nhalf;
  1208. t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
  1209. t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
  1210. }
  1211. }
  1212. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  1213. int width, int height, int bpno, int bandno,
  1214. int seg_symbols, int vert_causal_ctx_csty_symbol)
  1215. {
  1216. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  1217. for (y0 = 0; y0 < height; y0 += 4) {
  1218. for (x = 0; x < width; x++) {
  1219. int flags_mask = -1;
  1220. if (vert_causal_ctx_csty_symbol)
  1221. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
  1222. if (y0 + 3 < height &&
  1223. !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  1224. (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  1225. (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  1226. (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
  1227. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  1228. continue;
  1229. runlen = ff_mqc_decode(&t1->mqc,
  1230. t1->mqc.cx_states + MQC_CX_UNI);
  1231. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  1232. t1->mqc.cx_states +
  1233. MQC_CX_UNI);
  1234. dec = 1;
  1235. } else {
  1236. runlen = 0;
  1237. dec = 0;
  1238. }
  1239. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  1240. int flags_mask = -1;
  1241. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  1242. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
  1243. if (!dec) {
  1244. if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  1245. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
  1246. bandno));
  1247. }
  1248. }
  1249. if (dec) {
  1250. int xorbit;
  1251. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
  1252. &xorbit);
  1253. t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
  1254. t1->mqc.cx_states + ctxno) ^
  1255. xorbit)
  1256. ? -mask : mask;
  1257. ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
  1258. }
  1259. dec = 0;
  1260. t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
  1261. }
  1262. }
  1263. }
  1264. if (seg_symbols) {
  1265. int val;
  1266. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1267. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1268. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1269. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1270. if (val != 0xa)
  1271. av_log(s->avctx, AV_LOG_ERROR,
  1272. "Segmentation symbol value incorrect\n");
  1273. }
  1274. }
  1275. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  1276. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  1277. int width, int height, int bandpos)
  1278. {
  1279. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
  1280. int pass_cnt = 0;
  1281. int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
  1282. int term_cnt = 0;
  1283. int coder_type;
  1284. av_assert0(width <= 1024U && height <= 1024U);
  1285. av_assert0(width*height <= 4096);
  1286. memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
  1287. /* If code-block contains no compressed data: nothing to do. */
  1288. if (!cblk->length)
  1289. return 0;
  1290. memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
  1291. cblk->data[cblk->length] = 0xff;
  1292. cblk->data[cblk->length+1] = 0xff;
  1293. ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
  1294. while (passno--) {
  1295. switch(pass_t) {
  1296. case 0:
  1297. decode_sigpass(t1, width, height, bpno + 1, bandpos,
  1298. vert_causal_ctx_csty_symbol);
  1299. break;
  1300. case 1:
  1301. decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
  1302. break;
  1303. case 2:
  1304. av_assert2(!t1->mqc.raw);
  1305. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  1306. codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
  1307. vert_causal_ctx_csty_symbol);
  1308. break;
  1309. }
  1310. if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
  1311. ff_mqc_init_contexts(&t1->mqc);
  1312. if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
  1313. if (term_cnt >= cblk->nb_terminations) {
  1314. av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
  1315. return AVERROR_INVALIDDATA;
  1316. }
  1317. if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
  1318. av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
  1319. cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
  1320. pass_cnt, cblk->npasses);
  1321. }
  1322. ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
  1323. }
  1324. pass_t++;
  1325. if (pass_t == 3) {
  1326. bpno--;
  1327. pass_t = 0;
  1328. }
  1329. pass_cnt ++;
  1330. }
  1331. if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
  1332. av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
  1333. cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
  1334. }
  1335. return 0;
  1336. }
  1337. /* TODO: Verify dequantization for lossless case
  1338. * comp->data can be float or int
  1339. * band->stepsize can be float or int
  1340. * depending on the type of DWT transformation.
  1341. * see ISO/IEC 15444-1:2002 A.6.1 */
  1342. /* Float dequantization of a codeblock.*/
  1343. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  1344. Jpeg2000Component *comp,
  1345. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  1346. {
  1347. int i, j;
  1348. int w = cblk->coord[0][1] - cblk->coord[0][0];
  1349. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  1350. float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  1351. int *src = t1->data + j*t1->stride;
  1352. for (i = 0; i < w; ++i)
  1353. datap[i] = src[i] * band->f_stepsize;
  1354. }
  1355. }
  1356. /* Integer dequantization of a codeblock.*/
  1357. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  1358. Jpeg2000Component *comp,
  1359. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  1360. {
  1361. int i, j;
  1362. int w = cblk->coord[0][1] - cblk->coord[0][0];
  1363. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  1364. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  1365. int *src = t1->data + j*t1->stride;
  1366. if (band->i_stepsize == 32768) {
  1367. for (i = 0; i < w; ++i)
  1368. datap[i] = src[i] / 2;
  1369. } else {
  1370. // This should be VERY uncommon
  1371. for (i = 0; i < w; ++i)
  1372. datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
  1373. }
  1374. }
  1375. }
  1376. static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
  1377. Jpeg2000Component *comp,
  1378. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  1379. {
  1380. int i, j;
  1381. int w = cblk->coord[0][1] - cblk->coord[0][0];
  1382. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  1383. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  1384. int *src = t1->data + j*t1->stride;
  1385. for (i = 0; i < w; ++i)
  1386. datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
  1387. }
  1388. }
  1389. static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  1390. {
  1391. int i, csize = 1;
  1392. void *src[3];
  1393. for (i = 1; i < 3; i++) {
  1394. if (tile->codsty[0].transform != tile->codsty[i].transform) {
  1395. av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
  1396. return;
  1397. }
  1398. if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
  1399. av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
  1400. return;
  1401. }
  1402. }
  1403. for (i = 0; i < 3; i++)
  1404. if (tile->codsty[0].transform == FF_DWT97)
  1405. src[i] = tile->comp[i].f_data;
  1406. else
  1407. src[i] = tile->comp[i].i_data;
  1408. for (i = 0; i < 2; i++)
  1409. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  1410. s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
  1411. }
  1412. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  1413. AVFrame *picture)
  1414. {
  1415. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1416. int compno, reslevelno, bandno;
  1417. int x, y;
  1418. int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
  1419. int pixelsize = planar ? 1 : pixdesc->nb_components;
  1420. uint8_t *line;
  1421. Jpeg2000T1Context t1;
  1422. /* Loop on tile components */
  1423. for (compno = 0; compno < s->ncomponents; compno++) {
  1424. Jpeg2000Component *comp = tile->comp + compno;
  1425. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1426. t1.stride = (1<<codsty->log2_cblk_width) + 2;
  1427. /* Loop on resolution levels */
  1428. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  1429. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1430. /* Loop on bands */
  1431. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  1432. int nb_precincts, precno;
  1433. Jpeg2000Band *band = rlevel->band + bandno;
  1434. int cblkno = 0, bandpos;
  1435. bandpos = bandno + (reslevelno > 0);
  1436. if (band->coord[0][0] == band->coord[0][1] ||
  1437. band->coord[1][0] == band->coord[1][1])
  1438. continue;
  1439. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  1440. /* Loop on precincts */
  1441. for (precno = 0; precno < nb_precincts; precno++) {
  1442. Jpeg2000Prec *prec = band->prec + precno;
  1443. /* Loop on codeblocks */
  1444. for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
  1445. int x, y;
  1446. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  1447. decode_cblk(s, codsty, &t1, cblk,
  1448. cblk->coord[0][1] - cblk->coord[0][0],
  1449. cblk->coord[1][1] - cblk->coord[1][0],
  1450. bandpos);
  1451. x = cblk->coord[0][0] - band->coord[0][0];
  1452. y = cblk->coord[1][0] - band->coord[1][0];
  1453. if (codsty->transform == FF_DWT97)
  1454. dequantization_float(x, y, cblk, comp, &t1, band);
  1455. else if (codsty->transform == FF_DWT97_INT)
  1456. dequantization_int_97(x, y, cblk, comp, &t1, band);
  1457. else
  1458. dequantization_int(x, y, cblk, comp, &t1, band);
  1459. } /* end cblk */
  1460. } /*end prec */
  1461. } /* end band */
  1462. } /* end reslevel */
  1463. /* inverse DWT */
  1464. ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
  1465. } /*end comp */
  1466. /* inverse MCT transformation */
  1467. if (tile->codsty[0].mct)
  1468. mct_decode(s, tile);
  1469. if (s->cdef[0] < 0) {
  1470. for (x = 0; x < s->ncomponents; x++)
  1471. s->cdef[x] = x + 1;
  1472. if ((s->ncomponents & 1) == 0)
  1473. s->cdef[s->ncomponents-1] = 0;
  1474. }
  1475. if (s->precision <= 8) {
  1476. for (compno = 0; compno < s->ncomponents; compno++) {
  1477. Jpeg2000Component *comp = tile->comp + compno;
  1478. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1479. float *datap = comp->f_data;
  1480. int32_t *i_datap = comp->i_data;
  1481. int cbps = s->cbps[compno];
  1482. int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
  1483. int plane = 0;
  1484. if (planar)
  1485. plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
  1486. y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];
  1487. line = picture->data[plane] + y * picture->linesize[plane];
  1488. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
  1489. uint8_t *dst;
  1490. x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];
  1491. dst = line + x * pixelsize + compno*!planar;
  1492. if (codsty->transform == FF_DWT97) {
  1493. for (; x < w; x ++) {
  1494. int val = lrintf(*datap) + (1 << (cbps - 1));
  1495. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1496. val = av_clip(val, 0, (1 << cbps) - 1);
  1497. *dst = val << (8 - cbps);
  1498. datap++;
  1499. dst += pixelsize;
  1500. }
  1501. } else {
  1502. for (; x < w; x ++) {
  1503. int val = *i_datap + (1 << (cbps - 1));
  1504. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1505. val = av_clip(val, 0, (1 << cbps) - 1);
  1506. *dst = val << (8 - cbps);
  1507. i_datap++;
  1508. dst += pixelsize;
  1509. }
  1510. }
  1511. line += picture->linesize[plane];
  1512. }
  1513. }
  1514. } else {
  1515. int precision = picture->format == AV_PIX_FMT_XYZ12 ||
  1516. picture->format == AV_PIX_FMT_RGB48 ||
  1517. picture->format == AV_PIX_FMT_RGBA64 ||
  1518. picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
  1519. for (compno = 0; compno < s->ncomponents; compno++) {
  1520. Jpeg2000Component *comp = tile->comp + compno;
  1521. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1522. float *datap = comp->f_data;
  1523. int32_t *i_datap = comp->i_data;
  1524. uint16_t *linel;
  1525. int cbps = s->cbps[compno];
  1526. int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
  1527. int plane = 0;
  1528. if (planar)
  1529. plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
  1530. y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];
  1531. linel = (uint16_t *)picture->data[plane] + y * (picture->linesize[plane] >> 1);
  1532. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
  1533. uint16_t *dst;
  1534. x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];
  1535. dst = linel + (x * pixelsize + compno*!planar);
  1536. if (codsty->transform == FF_DWT97) {
  1537. for (; x < w; x ++) {
  1538. int val = lrintf(*datap) + (1 << (cbps - 1));
  1539. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1540. val = av_clip(val, 0, (1 << cbps) - 1);
  1541. /* align 12 bit values in little-endian mode */
  1542. *dst = val << (precision - cbps);
  1543. datap++;
  1544. dst += pixelsize;
  1545. }
  1546. } else {
  1547. for (; x < w; x ++) {
  1548. int val = *i_datap + (1 << (cbps - 1));
  1549. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
  1550. val = av_clip(val, 0, (1 << cbps) - 1);
  1551. /* align 12 bit values in little-endian mode */
  1552. *dst = val << (precision - cbps);
  1553. i_datap++;
  1554. dst += pixelsize;
  1555. }
  1556. }
  1557. linel += picture->linesize[plane] >> 1;
  1558. }
  1559. }
  1560. }
  1561. return 0;
  1562. }
  1563. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  1564. {
  1565. int tileno, compno;
  1566. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1567. if (s->tile[tileno].comp) {
  1568. for (compno = 0; compno < s->ncomponents; compno++) {
  1569. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1570. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1571. ff_jpeg2000_cleanup(comp, codsty);
  1572. }
  1573. av_freep(&s->tile[tileno].comp);
  1574. }
  1575. }
  1576. av_freep(&s->tile);
  1577. memset(s->codsty, 0, sizeof(s->codsty));
  1578. memset(s->qntsty, 0, sizeof(s->qntsty));
  1579. memset(s->properties, 0, sizeof(s->properties));
  1580. memset(&s->poc , 0, sizeof(s->poc));
  1581. s->numXtiles = s->numYtiles = 0;
  1582. s->ncomponents = 0;
  1583. }
  1584. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1585. {
  1586. Jpeg2000CodingStyle *codsty = s->codsty;
  1587. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1588. Jpeg2000POC *poc = &s->poc;
  1589. uint8_t *properties = s->properties;
  1590. for (;;) {
  1591. int len, ret = 0;
  1592. uint16_t marker;
  1593. int oldpos;
  1594. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1595. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1596. break;
  1597. }
  1598. marker = bytestream2_get_be16u(&s->g);
  1599. oldpos = bytestream2_tell(&s->g);
  1600. if (marker == JPEG2000_SOD) {
  1601. Jpeg2000Tile *tile;
  1602. Jpeg2000TilePart *tp;
  1603. if (!s->tile) {
  1604. av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
  1605. return AVERROR_INVALIDDATA;
  1606. }
  1607. if (s->curtileno < 0) {
  1608. av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
  1609. return AVERROR_INVALIDDATA;
  1610. }
  1611. tile = s->tile + s->curtileno;
  1612. tp = tile->tile_part + tile->tp_idx;
  1613. if (tp->tp_end < s->g.buffer) {
  1614. av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
  1615. return AVERROR_INVALIDDATA;
  1616. }
  1617. bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
  1618. bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
  1619. continue;
  1620. }
  1621. if (marker == JPEG2000_EOC)
  1622. break;
  1623. len = bytestream2_get_be16(&s->g);
  1624. if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
  1625. av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
  1626. return AVERROR_INVALIDDATA;
  1627. }
  1628. switch (marker) {
  1629. case JPEG2000_SIZ:
  1630. if (s->ncomponents) {
  1631. av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
  1632. return AVERROR_INVALIDDATA;
  1633. }
  1634. ret = get_siz(s);
  1635. if (!s->tile)
  1636. s->numXtiles = s->numYtiles = 0;
  1637. break;
  1638. case JPEG2000_COC:
  1639. ret = get_coc(s, codsty, properties);
  1640. break;
  1641. case JPEG2000_COD:
  1642. ret = get_cod(s, codsty, properties);
  1643. break;
  1644. case JPEG2000_QCC:
  1645. ret = get_qcc(s, len, qntsty, properties);
  1646. break;
  1647. case JPEG2000_QCD:
  1648. ret = get_qcd(s, len, qntsty, properties);
  1649. break;
  1650. case JPEG2000_POC:
  1651. ret = get_poc(s, len, poc);
  1652. break;
  1653. case JPEG2000_SOT:
  1654. if (!(ret = get_sot(s, len))) {
  1655. av_assert1(s->curtileno >= 0);
  1656. codsty = s->tile[s->curtileno].codsty;
  1657. qntsty = s->tile[s->curtileno].qntsty;
  1658. poc = &s->tile[s->curtileno].poc;
  1659. properties = s->tile[s->curtileno].properties;
  1660. }
  1661. break;
  1662. case JPEG2000_COM:
  1663. // the comment is ignored
  1664. bytestream2_skip(&s->g, len - 2);
  1665. break;
  1666. case JPEG2000_TLM:
  1667. // Tile-part lengths
  1668. ret = get_tlm(s, len);
  1669. break;
  1670. case JPEG2000_PLT:
  1671. // Packet length, tile-part header
  1672. ret = get_plt(s, len);
  1673. break;
  1674. default:
  1675. av_log(s->avctx, AV_LOG_ERROR,
  1676. "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
  1677. marker, bytestream2_tell(&s->g) - 4);
  1678. bytestream2_skip(&s->g, len - 2);
  1679. break;
  1680. }
  1681. if (bytestream2_tell(&s->g) - oldpos != len || ret) {
  1682. av_log(s->avctx, AV_LOG_ERROR,
  1683. "error during processing marker segment %.4"PRIx16"\n",
  1684. marker);
  1685. return ret ? ret : -1;
  1686. }
  1687. }
  1688. return 0;
  1689. }
  1690. /* Read bit stream packets --> T2 operation. */
  1691. static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
  1692. {
  1693. int ret = 0;
  1694. int tileno;
  1695. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1696. Jpeg2000Tile *tile = s->tile + tileno;
  1697. if ((ret = init_tile(s, tileno)) < 0)
  1698. return ret;
  1699. s->g = tile->tile_part[0].tpg;
  1700. if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
  1701. return ret;
  1702. }
  1703. return 0;
  1704. }
  1705. static int jp2_find_codestream(Jpeg2000DecoderContext *s)
  1706. {
  1707. uint32_t atom_size, atom, atom_end;
  1708. int search_range = 10;
  1709. while (search_range
  1710. &&
  1711. bytestream2_get_bytes_left(&s->g) >= 8) {
  1712. atom_size = bytestream2_get_be32u(&s->g);
  1713. atom = bytestream2_get_be32u(&s->g);
  1714. atom_end = bytestream2_tell(&s->g) + atom_size - 8;
  1715. if (atom == JP2_CODESTREAM)
  1716. return 1;
  1717. if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
  1718. return 0;
  1719. if (atom == JP2_HEADER &&
  1720. atom_size >= 16) {
  1721. uint32_t atom2_size, atom2, atom2_end;
  1722. do {
  1723. atom2_size = bytestream2_get_be32u(&s->g);
  1724. atom2 = bytestream2_get_be32u(&s->g);
  1725. atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
  1726. if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
  1727. break;
  1728. if (atom2 == JP2_CODESTREAM) {
  1729. return 1;
  1730. } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
  1731. int method = bytestream2_get_byteu(&s->g);
  1732. bytestream2_skipu(&s->g, 2);
  1733. if (method == 1) {
  1734. s->colour_space = bytestream2_get_be32u(&s->g);
  1735. }
  1736. } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
  1737. int i, size, colour_count, colour_channels, colour_depth[3];
  1738. uint32_t r, g, b;
  1739. colour_count = bytestream2_get_be16u(&s->g);
  1740. colour_channels = bytestream2_get_byteu(&s->g);
  1741. // FIXME: Do not ignore channel_sign
  1742. colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
  1743. colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
  1744. colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
  1745. size = (colour_depth[0] + 7 >> 3) * colour_count +
  1746. (colour_depth[1] + 7 >> 3) * colour_count +
  1747. (colour_depth[2] + 7 >> 3) * colour_count;
  1748. if (colour_count > 256 ||
  1749. colour_channels != 3 ||
  1750. colour_depth[0] > 16 ||
  1751. colour_depth[1] > 16 ||
  1752. colour_depth[2] > 16 ||
  1753. atom2_size < size) {
  1754. avpriv_request_sample(s->avctx, "Unknown palette");
  1755. bytestream2_seek(&s->g, atom2_end, SEEK_SET);
  1756. continue;
  1757. }
  1758. s->pal8 = 1;
  1759. for (i = 0; i < colour_count; i++) {
  1760. if (colour_depth[0] <= 8) {
  1761. r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
  1762. r |= r >> colour_depth[0];
  1763. } else {
  1764. r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
  1765. }
  1766. if (colour_depth[1] <= 8) {
  1767. g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
  1768. r |= r >> colour_depth[1];
  1769. } else {
  1770. g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
  1771. }
  1772. if (colour_depth[2] <= 8) {
  1773. b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
  1774. r |= r >> colour_depth[2];
  1775. } else {
  1776. b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
  1777. }
  1778. s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
  1779. }
  1780. } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
  1781. int n = bytestream2_get_be16u(&s->g);
  1782. for (; n>0; n--) {
  1783. int cn = bytestream2_get_be16(&s->g);
  1784. int av_unused typ = bytestream2_get_be16(&s->g);
  1785. int asoc = bytestream2_get_be16(&s->g);
  1786. if (cn < 4 && asoc < 4)
  1787. s->cdef[cn] = asoc;
  1788. }
  1789. }
  1790. bytestream2_seek(&s->g, atom2_end, SEEK_SET);
  1791. } while (atom_end - atom2_end >= 8);
  1792. } else {
  1793. search_range--;
  1794. }
  1795. bytestream2_seek(&s->g, atom_end, SEEK_SET);
  1796. }
  1797. return 0;
  1798. }
  1799. static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
  1800. {
  1801. Jpeg2000DecoderContext *s = avctx->priv_data;
  1802. ff_jpeg2000dsp_init(&s->dsp);
  1803. return 0;
  1804. }
  1805. static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
  1806. int *got_frame, AVPacket *avpkt)
  1807. {
  1808. Jpeg2000DecoderContext *s = avctx->priv_data;
  1809. ThreadFrame frame = { .f = data };
  1810. AVFrame *picture = data;
  1811. int tileno, ret;
  1812. s->avctx = avctx;
  1813. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  1814. s->curtileno = -1;
  1815. memset(s->cdef, -1, sizeof(s->cdef));
  1816. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1817. ret = AVERROR_INVALIDDATA;
  1818. goto end;
  1819. }
  1820. // check if the image is in jp2 format
  1821. if (bytestream2_get_bytes_left(&s->g) >= 12 &&
  1822. (bytestream2_get_be32u(&s->g) == 12) &&
  1823. (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
  1824. (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
  1825. if (!jp2_find_codestream(s)) {
  1826. av_log(avctx, AV_LOG_ERROR,
  1827. "Could not find Jpeg2000 codestream atom.\n");
  1828. ret = AVERROR_INVALIDDATA;
  1829. goto end;
  1830. }
  1831. } else {
  1832. bytestream2_seek(&s->g, 0, SEEK_SET);
  1833. }
  1834. while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
  1835. bytestream2_skip(&s->g, 1);
  1836. if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
  1837. av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
  1838. ret = AVERROR_INVALIDDATA;
  1839. goto end;
  1840. }
  1841. if (ret = jpeg2000_read_main_headers(s))
  1842. goto end;
  1843. /* get picture buffer */
  1844. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1845. goto end;
  1846. picture->pict_type = AV_PICTURE_TYPE_I;
  1847. picture->key_frame = 1;
  1848. if (ret = jpeg2000_read_bitstream_packets(s))
  1849. goto end;
  1850. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
  1851. if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
  1852. goto end;
  1853. jpeg2000_dec_cleanup(s);
  1854. *got_frame = 1;
  1855. if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
  1856. memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
  1857. return bytestream2_tell(&s->g);
  1858. end:
  1859. jpeg2000_dec_cleanup(s);
  1860. return ret;
  1861. }
  1862. static av_cold void jpeg2000_init_static_data(AVCodec *codec)
  1863. {
  1864. ff_jpeg2000_init_tier1_luts();
  1865. ff_mqc_init_context_tables();
  1866. }
  1867. #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
  1868. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1869. static const AVOption options[] = {
  1870. { "lowres", "Lower the decoding resolution by a power of two",
  1871. OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
  1872. { NULL },
  1873. };
  1874. static const AVProfile profiles[] = {
  1875. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
  1876. { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
  1877. { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
  1878. { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
  1879. { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
  1880. { FF_PROFILE_UNKNOWN },
  1881. };
  1882. static const AVClass jpeg2000_class = {
  1883. .class_name = "jpeg2000",
  1884. .item_name = av_default_item_name,
  1885. .option = options,
  1886. .version = LIBAVUTIL_VERSION_INT,
  1887. };
  1888. AVCodec ff_jpeg2000_decoder = {
  1889. .name = "jpeg2000",
  1890. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  1891. .type = AVMEDIA_TYPE_VIDEO,
  1892. .id = AV_CODEC_ID_JPEG2000,
  1893. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
  1894. .priv_data_size = sizeof(Jpeg2000DecoderContext),
  1895. .init_static_data = jpeg2000_init_static_data,
  1896. .init = jpeg2000_decode_init,
  1897. .decode = jpeg2000_decode_frame,
  1898. .priv_class = &jpeg2000_class,
  1899. .max_lowres = 5,
  1900. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1901. };