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.

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