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.

2170 lines
86KB

  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 >= 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 >= 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 >= 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 >= 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 (s->width > 32768U || s->height > 32768U) {
  247. avpriv_request_sample(s->avctx, "Large Dimensions");
  248. return AVERROR_PATCHWELCOME;
  249. }
  250. if (ncomponents <= 0) {
  251. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
  252. s->ncomponents);
  253. return AVERROR_INVALIDDATA;
  254. }
  255. if (ncomponents > 4) {
  256. avpriv_request_sample(s->avctx, "Support for %d components",
  257. ncomponents);
  258. return AVERROR_PATCHWELCOME;
  259. }
  260. s->ncomponents = ncomponents;
  261. if (s->tile_width <= 0 || s->tile_height <= 0) {
  262. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
  263. s->tile_width, s->tile_height);
  264. return AVERROR_INVALIDDATA;
  265. }
  266. if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
  267. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
  268. return AVERROR_INVALIDDATA;
  269. }
  270. for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
  271. uint8_t x = bytestream2_get_byteu(&s->g);
  272. s->cbps[i] = (x & 0x7f) + 1;
  273. s->precision = FFMAX(s->cbps[i], s->precision);
  274. s->sgnd[i] = !!(x & 0x80);
  275. s->cdx[i] = bytestream2_get_byteu(&s->g);
  276. s->cdy[i] = bytestream2_get_byteu(&s->g);
  277. if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
  278. || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
  279. av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
  280. return AVERROR_INVALIDDATA;
  281. }
  282. log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
  283. }
  284. s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
  285. s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
  286. if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
  287. s->numXtiles = s->numYtiles = 0;
  288. return AVERROR(EINVAL);
  289. }
  290. s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
  291. if (!s->tile) {
  292. s->numXtiles = s->numYtiles = 0;
  293. return AVERROR(ENOMEM);
  294. }
  295. for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
  296. Jpeg2000Tile *tile = s->tile + i;
  297. tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
  298. if (!tile->comp)
  299. return AVERROR(ENOMEM);
  300. }
  301. /* compute image size with reduction factor */
  302. s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
  303. s->reduction_factor);
  304. s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
  305. s->reduction_factor);
  306. if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
  307. s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
  308. possible_fmts = xyz_pix_fmts;
  309. possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
  310. } else {
  311. switch (s->colour_space) {
  312. case 16:
  313. possible_fmts = rgb_pix_fmts;
  314. possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
  315. break;
  316. case 17:
  317. possible_fmts = gray_pix_fmts;
  318. possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
  319. break;
  320. case 18:
  321. possible_fmts = yuv_pix_fmts;
  322. possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
  323. break;
  324. default:
  325. possible_fmts = all_pix_fmts;
  326. possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
  327. break;
  328. }
  329. }
  330. for (i = 0; i < possible_fmts_nb; ++i) {
  331. if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
  332. s->avctx->pix_fmt = possible_fmts[i];
  333. break;
  334. }
  335. }
  336. if (i == possible_fmts_nb) {
  337. if (ncomponents == 4 &&
  338. s->cdy[0] == 1 && s->cdx[0] == 1 &&
  339. s->cdy[1] == 1 && s->cdx[1] == 1 &&
  340. s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
  341. if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
  342. s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  343. s->cdef[0] = 0;
  344. s->cdef[1] = 1;
  345. s->cdef[2] = 2;
  346. s->cdef[3] = 3;
  347. i = 0;
  348. }
  349. }
  350. }
  351. if (i == possible_fmts_nb) {
  352. av_log(s->avctx, AV_LOG_ERROR,
  353. "Unknown pix_fmt, profile: %d, colour_space: %d, "
  354. "components: %d, precision: %d\n"
  355. "cdx[0]: %d, cdy[0]: %d\n"
  356. "cdx[1]: %d, cdy[1]: %d\n"
  357. "cdx[2]: %d, cdy[2]: %d\n"
  358. "cdx[3]: %d, cdy[3]: %d\n",
  359. s->avctx->profile, s->colour_space, ncomponents, s->precision,
  360. s->cdx[0],
  361. s->cdy[0],
  362. ncomponents > 1 ? s->cdx[1] : 0,
  363. ncomponents > 1 ? s->cdy[1] : 0,
  364. ncomponents > 2 ? s->cdx[2] : 0,
  365. ncomponents > 2 ? s->cdy[2] : 0,
  366. ncomponents > 3 ? s->cdx[3] : 0,
  367. ncomponents > 3 ? s->cdy[3] : 0);
  368. return AVERROR_PATCHWELCOME;
  369. }
  370. s->avctx->bits_per_raw_sample = s->precision;
  371. return 0;
  372. }
  373. /* get common part for COD and COC segments */
  374. static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
  375. {
  376. uint8_t byte;
  377. if (bytestream2_get_bytes_left(&s->g) < 5) {
  378. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
  379. return AVERROR_INVALIDDATA;
  380. }
  381. /* nreslevels = number of resolution levels
  382. = number of decomposition level +1 */
  383. c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
  384. if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
  385. av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
  386. return AVERROR_INVALIDDATA;
  387. }
  388. if (c->nreslevels <= s->reduction_factor) {
  389. /* we are forced to update reduction_factor as its requested value is
  390. not compatible with this bitstream, and as we might have used it
  391. already in setup earlier we have to fail this frame until
  392. reinitialization is implemented */
  393. av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
  394. s->reduction_factor = c->nreslevels - 1;
  395. return AVERROR(EINVAL);
  396. }
  397. /* compute number of resolution levels to decode */
  398. c->nreslevels2decode = c->nreslevels - s->reduction_factor;
  399. c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
  400. c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
  401. if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
  402. c->log2_cblk_width + c->log2_cblk_height > 12) {
  403. av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
  404. return AVERROR_INVALIDDATA;
  405. }
  406. c->cblk_style = bytestream2_get_byteu(&s->g);
  407. if (c->cblk_style != 0) { // cblk style
  408. av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
  409. if (c->cblk_style & JPEG2000_CBLK_BYPASS)
  410. av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
  411. }
  412. c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
  413. /* set integer 9/7 DWT in case of BITEXACT flag */
  414. if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
  415. c->transform = FF_DWT97_INT;
  416. else if (c->transform == FF_DWT53) {
  417. s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
  418. }
  419. if (c->csty & JPEG2000_CSTY_PREC) {
  420. int i;
  421. for (i = 0; i < c->nreslevels; i++) {
  422. byte = bytestream2_get_byte(&s->g);
  423. c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
  424. c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
  425. if (i)
  426. if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
  427. av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
  428. c->log2_prec_widths[i], c->log2_prec_heights[i]);
  429. c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
  430. return AVERROR_INVALIDDATA;
  431. }
  432. }
  433. } else {
  434. memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
  435. memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
  436. }
  437. return 0;
  438. }
  439. /* get coding parameters for a particular tile or whole image*/
  440. static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  441. uint8_t *properties)
  442. {
  443. Jpeg2000CodingStyle tmp;
  444. int compno, ret;
  445. if (bytestream2_get_bytes_left(&s->g) < 5) {
  446. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
  447. return AVERROR_INVALIDDATA;
  448. }
  449. tmp.csty = bytestream2_get_byteu(&s->g);
  450. // get progression order
  451. tmp.prog_order = bytestream2_get_byteu(&s->g);
  452. tmp.nlayers = bytestream2_get_be16u(&s->g);
  453. tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
  454. if (tmp.mct && s->ncomponents < 3) {
  455. av_log(s->avctx, AV_LOG_ERROR,
  456. "MCT %"PRIu8" with too few components (%d)\n",
  457. tmp.mct, s->ncomponents);
  458. return AVERROR_INVALIDDATA;
  459. }
  460. if ((ret = get_cox(s, &tmp)) < 0)
  461. return ret;
  462. for (compno = 0; compno < s->ncomponents; compno++)
  463. if (!(properties[compno] & HAD_COC))
  464. memcpy(c + compno, &tmp, sizeof(tmp));
  465. return 0;
  466. }
  467. /* Get coding parameters for a component in the whole image or a
  468. * particular tile. */
  469. static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
  470. uint8_t *properties)
  471. {
  472. int compno, ret;
  473. if (bytestream2_get_bytes_left(&s->g) < 2) {
  474. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
  475. return AVERROR_INVALIDDATA;
  476. }
  477. compno = bytestream2_get_byteu(&s->g);
  478. if (compno >= s->ncomponents) {
  479. av_log(s->avctx, AV_LOG_ERROR,
  480. "Invalid compno %d. There are %d components in the image.\n",
  481. compno, s->ncomponents);
  482. return AVERROR_INVALIDDATA;
  483. }
  484. c += compno;
  485. c->csty = bytestream2_get_byteu(&s->g);
  486. if ((ret = get_cox(s, c)) < 0)
  487. return ret;
  488. properties[compno] |= HAD_COC;
  489. return 0;
  490. }
  491. /* Get common part for QCD and QCC segments. */
  492. static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
  493. {
  494. int i, x;
  495. if (bytestream2_get_bytes_left(&s->g) < 1)
  496. return AVERROR_INVALIDDATA;
  497. x = bytestream2_get_byteu(&s->g); // Sqcd
  498. q->nguardbits = x >> 5;
  499. q->quantsty = x & 0x1f;
  500. if (q->quantsty == JPEG2000_QSTY_NONE) {
  501. n -= 3;
  502. if (bytestream2_get_bytes_left(&s->g) < n ||
  503. n > JPEG2000_MAX_DECLEVELS*3)
  504. return AVERROR_INVALIDDATA;
  505. for (i = 0; i < n; i++)
  506. q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
  507. } else if (q->quantsty == JPEG2000_QSTY_SI) {
  508. if (bytestream2_get_bytes_left(&s->g) < 2)
  509. return AVERROR_INVALIDDATA;
  510. x = bytestream2_get_be16u(&s->g);
  511. q->expn[0] = x >> 11;
  512. q->mant[0] = x & 0x7ff;
  513. for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
  514. int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
  515. q->expn[i] = curexpn;
  516. q->mant[i] = q->mant[0];
  517. }
  518. } else {
  519. n = (n - 3) >> 1;
  520. if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
  521. n > JPEG2000_MAX_DECLEVELS*3)
  522. return AVERROR_INVALIDDATA;
  523. for (i = 0; i < n; i++) {
  524. x = bytestream2_get_be16u(&s->g);
  525. q->expn[i] = x >> 11;
  526. q->mant[i] = x & 0x7ff;
  527. }
  528. }
  529. return 0;
  530. }
  531. /* Get quantization parameters for a particular tile or a whole image. */
  532. static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  533. uint8_t *properties)
  534. {
  535. Jpeg2000QuantStyle tmp;
  536. int compno, ret;
  537. memset(&tmp, 0, sizeof(tmp));
  538. if ((ret = get_qcx(s, n, &tmp)) < 0)
  539. return ret;
  540. for (compno = 0; compno < s->ncomponents; compno++)
  541. if (!(properties[compno] & HAD_QCC))
  542. memcpy(q + compno, &tmp, sizeof(tmp));
  543. return 0;
  544. }
  545. /* Get quantization parameters for a component in the whole image
  546. * on in a particular tile. */
  547. static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
  548. uint8_t *properties)
  549. {
  550. int compno;
  551. if (bytestream2_get_bytes_left(&s->g) < 1)
  552. return AVERROR_INVALIDDATA;
  553. compno = bytestream2_get_byteu(&s->g);
  554. if (compno >= s->ncomponents) {
  555. av_log(s->avctx, AV_LOG_ERROR,
  556. "Invalid compno %d. There are %d components in the image.\n",
  557. compno, s->ncomponents);
  558. return AVERROR_INVALIDDATA;
  559. }
  560. properties[compno] |= HAD_QCC;
  561. return get_qcx(s, n - 1, q + compno);
  562. }
  563. static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
  564. {
  565. int i;
  566. int elem_size = s->ncomponents <= 257 ? 7 : 9;
  567. Jpeg2000POC tmp = {{{0}}};
  568. if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
  569. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
  570. return AVERROR_INVALIDDATA;
  571. }
  572. if (elem_size > 7) {
  573. avpriv_request_sample(s->avctx, "Fat POC not supported");
  574. return AVERROR_PATCHWELCOME;
  575. }
  576. tmp.nb_poc = (size - 2) / elem_size;
  577. if (tmp.nb_poc > MAX_POCS) {
  578. avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
  579. return AVERROR_PATCHWELCOME;
  580. }
  581. for (i = 0; i<tmp.nb_poc; i++) {
  582. Jpeg2000POCEntry *e = &tmp.poc[i];
  583. e->RSpoc = bytestream2_get_byteu(&s->g);
  584. e->CSpoc = bytestream2_get_byteu(&s->g);
  585. e->LYEpoc = bytestream2_get_be16u(&s->g);
  586. e->REpoc = bytestream2_get_byteu(&s->g);
  587. e->CEpoc = bytestream2_get_byteu(&s->g);
  588. e->Ppoc = bytestream2_get_byteu(&s->g);
  589. if (!e->CEpoc)
  590. e->CEpoc = 256;
  591. if (e->CEpoc > s->ncomponents)
  592. e->CEpoc = s->ncomponents;
  593. if ( e->RSpoc >= e->REpoc || e->REpoc > 33
  594. || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
  595. || !e->LYEpoc) {
  596. av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
  597. e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
  598. );
  599. return AVERROR_INVALIDDATA;
  600. }
  601. }
  602. if (!p->nb_poc || p->is_default) {
  603. *p = tmp;
  604. } else {
  605. if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
  606. av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
  607. return AVERROR_INVALIDDATA;
  608. }
  609. memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
  610. p->nb_poc += tmp.nb_poc;
  611. }
  612. p->is_default = 0;
  613. return 0;
  614. }
  615. /* Get start of tile segment. */
  616. static int get_sot(Jpeg2000DecoderContext *s, int n)
  617. {
  618. Jpeg2000TilePart *tp;
  619. uint16_t Isot;
  620. uint32_t Psot;
  621. unsigned TPsot;
  622. if (bytestream2_get_bytes_left(&s->g) < 8)
  623. return AVERROR_INVALIDDATA;
  624. s->curtileno = 0;
  625. Isot = bytestream2_get_be16u(&s->g); // Isot
  626. if (Isot >= s->numXtiles * s->numYtiles)
  627. return AVERROR_INVALIDDATA;
  628. s->curtileno = Isot;
  629. Psot = bytestream2_get_be32u(&s->g); // Psot
  630. TPsot = bytestream2_get_byteu(&s->g); // TPsot
  631. /* Read TNSot but not used */
  632. bytestream2_get_byteu(&s->g); // TNsot
  633. if (!Psot)
  634. Psot = bytestream2_get_bytes_left(&s->g) + n + 2;
  635. if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
  636. av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
  637. return AVERROR_INVALIDDATA;
  638. }
  639. av_assert0(TPsot < FF_ARRAY_ELEMS(s->tile[Isot].tile_part));
  640. s->tile[Isot].tp_idx = TPsot;
  641. tp = s->tile[Isot].tile_part + TPsot;
  642. tp->tile_index = Isot;
  643. tp->tp_end = s->g.buffer + Psot - n - 2;
  644. if (!TPsot) {
  645. Jpeg2000Tile *tile = s->tile + s->curtileno;
  646. /* copy defaults */
  647. memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
  648. memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
  649. memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
  650. tile->poc.is_default = 1;
  651. }
  652. return 0;
  653. }
  654. /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
  655. * Used to know the number of tile parts and lengths.
  656. * There may be multiple TLMs in the header.
  657. * TODO: The function is not used for tile-parts management, nor anywhere else.
  658. * It can be useful to allocate memory for tile parts, before managing the SOT
  659. * markers. Parsing the TLM header is needed to increment the input header
  660. * buffer.
  661. * This marker is mandatory for DCI. */
  662. static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
  663. {
  664. uint8_t Stlm, ST, SP, tile_tlm, i;
  665. bytestream2_get_byte(&s->g); /* Ztlm: skipped */
  666. Stlm = bytestream2_get_byte(&s->g);
  667. // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  668. ST = (Stlm >> 4) & 0x03;
  669. // TODO: Manage case of ST = 0b11 --> raise error
  670. SP = (Stlm >> 6) & 0x01;
  671. tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
  672. for (i = 0; i < tile_tlm; i++) {
  673. switch (ST) {
  674. case 0:
  675. break;
  676. case 1:
  677. bytestream2_get_byte(&s->g);
  678. break;
  679. case 2:
  680. bytestream2_get_be16(&s->g);
  681. break;
  682. case 3:
  683. bytestream2_get_be32(&s->g);
  684. break;
  685. }
  686. if (SP == 0) {
  687. bytestream2_get_be16(&s->g);
  688. } else {
  689. bytestream2_get_be32(&s->g);
  690. }
  691. }
  692. return 0;
  693. }
  694. static uint8_t get_plt(Jpeg2000DecoderContext *s, int n)
  695. {
  696. int i;
  697. av_log(s->avctx, AV_LOG_DEBUG,
  698. "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
  699. /*Zplt =*/ bytestream2_get_byte(&s->g);
  700. for (i = 0; i < n - 3; i++) {
  701. bytestream2_get_byte(&s->g);
  702. }
  703. return 0;
  704. }
  705. static int init_tile(Jpeg2000DecoderContext *s, int tileno)
  706. {
  707. int compno;
  708. int tilex = tileno % s->numXtiles;
  709. int tiley = tileno / s->numXtiles;
  710. Jpeg2000Tile *tile = s->tile + tileno;
  711. if (!tile->comp)
  712. return AVERROR(ENOMEM);
  713. tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
  714. tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
  715. tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
  716. tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
  717. for (compno = 0; compno < s->ncomponents; compno++) {
  718. Jpeg2000Component *comp = tile->comp + compno;
  719. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  720. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  721. int ret; // global bandno
  722. comp->coord_o[0][0] = tile->coord[0][0];
  723. comp->coord_o[0][1] = tile->coord[0][1];
  724. comp->coord_o[1][0] = tile->coord[1][0];
  725. comp->coord_o[1][1] = tile->coord[1][1];
  726. if (compno) {
  727. comp->coord_o[0][0] /= s->cdx[compno];
  728. comp->coord_o[0][1] /= s->cdx[compno];
  729. comp->coord_o[1][0] /= s->cdy[compno];
  730. comp->coord_o[1][1] /= s->cdy[compno];
  731. }
  732. comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
  733. comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
  734. comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
  735. comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
  736. if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  737. s->cbps[compno], s->cdx[compno],
  738. s->cdy[compno], s->avctx))
  739. return ret;
  740. }
  741. return 0;
  742. }
  743. /* Read the number of coding passes. */
  744. static int getnpasses(Jpeg2000DecoderContext *s)
  745. {
  746. int num;
  747. if (!get_bits(s, 1))
  748. return 1;
  749. if (!get_bits(s, 1))
  750. return 2;
  751. if ((num = get_bits(s, 2)) != 3)
  752. return num < 0 ? num : 3 + num;
  753. if ((num = get_bits(s, 5)) != 31)
  754. return num < 0 ? num : 6 + num;
  755. num = get_bits(s, 7);
  756. return num < 0 ? num : 37 + num;
  757. }
  758. static int getlblockinc(Jpeg2000DecoderContext *s)
  759. {
  760. int res = 0, ret;
  761. while (ret = get_bits(s, 1)) {
  762. if (ret < 0)
  763. return ret;
  764. res++;
  765. }
  766. return res;
  767. }
  768. static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
  769. Jpeg2000CodingStyle *codsty,
  770. Jpeg2000ResLevel *rlevel, int precno,
  771. int layno, uint8_t *expn, int numgbits)
  772. {
  773. int bandno, cblkno, ret, nb_code_blocks;
  774. int cwsno;
  775. if (layno < rlevel->band[0].prec[precno].decoded_layers)
  776. return 0;
  777. rlevel->band[0].prec[precno].decoded_layers = layno + 1;
  778. if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
  779. if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
  780. s->g = tile->tile_part[++(*tp_index)].tpg;
  781. }
  782. }
  783. if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
  784. bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
  785. if (!(ret = get_bits(s, 1))) {
  786. jpeg2000_flush(s);
  787. return 0;
  788. } else if (ret < 0)
  789. return ret;
  790. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  791. Jpeg2000Band *band = rlevel->band + bandno;
  792. Jpeg2000Prec *prec = band->prec + precno;
  793. if (band->coord[0][0] == band->coord[0][1] ||
  794. band->coord[1][0] == band->coord[1][1])
  795. continue;
  796. nb_code_blocks = prec->nb_codeblocks_height *
  797. prec->nb_codeblocks_width;
  798. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  799. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  800. int incl, newpasses, llen;
  801. if (cblk->npasses)
  802. incl = get_bits(s, 1);
  803. else
  804. incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
  805. if (!incl)
  806. continue;
  807. else if (incl < 0)
  808. return incl;
  809. if (!cblk->npasses) {
  810. int v = expn[bandno] + numgbits - 1 -
  811. tag_tree_decode(s, prec->zerobits + cblkno, 100);
  812. if (v < 0) {
  813. av_log(s->avctx, AV_LOG_ERROR,
  814. "nonzerobits %d invalid\n", v);
  815. return AVERROR_INVALIDDATA;
  816. }
  817. cblk->nonzerobits = v;
  818. }
  819. if ((newpasses = getnpasses(s)) < 0)
  820. return newpasses;
  821. av_assert2(newpasses > 0);
  822. if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
  823. avpriv_request_sample(s->avctx, "Too many passes");
  824. return AVERROR_PATCHWELCOME;
  825. }
  826. if ((llen = getlblockinc(s)) < 0)
  827. return llen;
  828. if (cblk->lblock + llen + av_log2(newpasses) > 16) {
  829. avpriv_request_sample(s->avctx,
  830. "Block with length beyond 16 bits");
  831. return AVERROR_PATCHWELCOME;
  832. }
  833. cblk->lblock += llen;
  834. cblk->nb_lengthinc = 0;
  835. cblk->nb_terminationsinc = 0;
  836. do {
  837. int newpasses1 = 0;
  838. while (newpasses1 < newpasses) {
  839. newpasses1 ++;
  840. if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
  841. cblk->nb_terminationsinc ++;
  842. break;
  843. }
  844. }
  845. if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
  846. return ret;
  847. if (ret > sizeof(cblk->data)) {
  848. avpriv_request_sample(s->avctx,
  849. "Block with lengthinc greater than %"SIZE_SPECIFIER"",
  850. sizeof(cblk->data));
  851. return AVERROR_PATCHWELCOME;
  852. }
  853. cblk->lengthinc[cblk->nb_lengthinc++] = ret;
  854. cblk->npasses += newpasses1;
  855. newpasses -= newpasses1;
  856. } while(newpasses);
  857. }
  858. }
  859. jpeg2000_flush(s);
  860. if (codsty->csty & JPEG2000_CSTY_EPH) {
  861. if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
  862. bytestream2_skip(&s->g, 2);
  863. else
  864. av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
  865. }
  866. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  867. Jpeg2000Band *band = rlevel->band + bandno;
  868. Jpeg2000Prec *prec = band->prec + precno;
  869. nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  870. for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
  871. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  872. for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
  873. if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
  874. || sizeof(cblk->data) < cblk->length + cblk->lengthinc[cwsno] + 4
  875. ) {
  876. av_log(s->avctx, AV_LOG_ERROR,
  877. "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
  878. cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
  879. return AVERROR_INVALIDDATA;
  880. }
  881. bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
  882. cblk->length += cblk->lengthinc[cwsno];
  883. cblk->lengthinc[cwsno] = 0;
  884. if (cblk->nb_terminationsinc) {
  885. cblk->nb_terminationsinc--;
  886. cblk->nb_terminations++;
  887. cblk->data[cblk->length++] = 0xFF;
  888. cblk->data[cblk->length++] = 0xFF;
  889. cblk->data_start[cblk->nb_terminations] = cblk->length;
  890. }
  891. }
  892. }
  893. }
  894. return 0;
  895. }
  896. static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  897. int RSpoc, int CSpoc,
  898. int LYEpoc, int REpoc, int CEpoc,
  899. int Ppoc, int *tp_index)
  900. {
  901. int ret = 0;
  902. int layno, reslevelno, compno, precno, ok_reslevel;
  903. int x, y;
  904. int step_x, step_y;
  905. switch (Ppoc) {
  906. case JPEG2000_PGOD_RLCP:
  907. av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
  908. ok_reslevel = 1;
  909. for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
  910. ok_reslevel = 0;
  911. for (layno = 0; layno < LYEpoc; layno++) {
  912. for (compno = CSpoc; compno < CEpoc; compno++) {
  913. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  914. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  915. if (reslevelno < codsty->nreslevels) {
  916. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  917. reslevelno;
  918. ok_reslevel = 1;
  919. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  920. if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
  921. codsty, rlevel,
  922. precno, layno,
  923. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  924. qntsty->nguardbits)) < 0)
  925. return ret;
  926. }
  927. }
  928. }
  929. }
  930. break;
  931. case JPEG2000_PGOD_LRCP:
  932. av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
  933. for (layno = 0; layno < LYEpoc; layno++) {
  934. ok_reslevel = 1;
  935. for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
  936. ok_reslevel = 0;
  937. for (compno = CSpoc; compno < CEpoc; compno++) {
  938. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  939. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  940. if (reslevelno < codsty->nreslevels) {
  941. Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
  942. reslevelno;
  943. ok_reslevel = 1;
  944. for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
  945. if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
  946. codsty, rlevel,
  947. precno, layno,
  948. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  949. qntsty->nguardbits)) < 0)
  950. return ret;
  951. }
  952. }
  953. }
  954. }
  955. break;
  956. case JPEG2000_PGOD_CPRL:
  957. av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
  958. for (compno = CSpoc; compno < CEpoc; compno++) {
  959. Jpeg2000Component *comp = tile->comp + compno;
  960. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  961. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  962. step_x = 32;
  963. step_y = 32;
  964. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  965. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  966. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  967. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  968. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  969. }
  970. av_assert0(step_x < 32 && step_y < 32);
  971. step_x = 1<<step_x;
  972. step_y = 1<<step_y;
  973. for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
  974. for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
  975. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  976. unsigned prcx, prcy;
  977. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  978. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  979. int xc = x / s->cdx[compno];
  980. int yc = y / s->cdy[compno];
  981. if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
  982. continue;
  983. if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
  984. continue;
  985. // check if a precinct exists
  986. prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
  987. prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
  988. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
  989. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
  990. precno = prcx + rlevel->num_precincts_x * prcy;
  991. if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
  992. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  993. prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
  994. continue;
  995. }
  996. for (layno = 0; layno < LYEpoc; layno++) {
  997. if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
  998. precno, layno,
  999. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  1000. qntsty->nguardbits)) < 0)
  1001. return ret;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. }
  1007. break;
  1008. case JPEG2000_PGOD_RPCL:
  1009. av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
  1010. ok_reslevel = 1;
  1011. for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
  1012. ok_reslevel = 0;
  1013. step_x = 30;
  1014. step_y = 30;
  1015. for (compno = CSpoc; compno < CEpoc; compno++) {
  1016. Jpeg2000Component *comp = tile->comp + compno;
  1017. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1018. if (reslevelno < codsty->nreslevels) {
  1019. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1020. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1021. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  1022. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  1023. }
  1024. }
  1025. step_x = 1<<step_x;
  1026. step_y = 1<<step_y;
  1027. for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
  1028. for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
  1029. for (compno = CSpoc; compno < CEpoc; compno++) {
  1030. Jpeg2000Component *comp = tile->comp + compno;
  1031. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1032. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  1033. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1034. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1035. unsigned prcx, prcy;
  1036. int xc = x / s->cdx[compno];
  1037. int yc = y / s->cdy[compno];
  1038. if (reslevelno >= codsty->nreslevels)
  1039. continue;
  1040. if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
  1041. continue;
  1042. if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
  1043. continue;
  1044. // check if a precinct exists
  1045. prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
  1046. prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
  1047. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
  1048. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
  1049. precno = prcx + rlevel->num_precincts_x * prcy;
  1050. ok_reslevel = 1;
  1051. if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
  1052. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  1053. prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
  1054. continue;
  1055. }
  1056. for (layno = 0; layno < LYEpoc; layno++) {
  1057. if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
  1058. codsty, rlevel,
  1059. precno, layno,
  1060. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  1061. qntsty->nguardbits)) < 0)
  1062. return ret;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. }
  1068. break;
  1069. case JPEG2000_PGOD_PCRL:
  1070. av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
  1071. step_x = 32;
  1072. step_y = 32;
  1073. for (compno = CSpoc; compno < CEpoc; compno++) {
  1074. Jpeg2000Component *comp = tile->comp + compno;
  1075. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1076. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  1077. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1078. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1079. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  1080. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  1081. }
  1082. }
  1083. if (step_x >= 31 || step_y >= 31){
  1084. avpriv_request_sample(s->avctx, "PCRL with large step");
  1085. return AVERROR_PATCHWELCOME;
  1086. }
  1087. step_x = 1<<step_x;
  1088. step_y = 1<<step_y;
  1089. for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
  1090. for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
  1091. for (compno = CSpoc; compno < CEpoc; compno++) {
  1092. Jpeg2000Component *comp = tile->comp + compno;
  1093. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1094. Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
  1095. int xc = x / s->cdx[compno];
  1096. int yc = y / s->cdy[compno];
  1097. for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
  1098. unsigned prcx, prcy;
  1099. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1100. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1101. if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
  1102. continue;
  1103. if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
  1104. continue;
  1105. // check if a precinct exists
  1106. prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
  1107. prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
  1108. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
  1109. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
  1110. precno = prcx + rlevel->num_precincts_x * prcy;
  1111. if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
  1112. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  1113. prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
  1114. continue;
  1115. }
  1116. for (layno = 0; layno < LYEpoc; layno++) {
  1117. if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
  1118. precno, layno,
  1119. qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
  1120. qntsty->nguardbits)) < 0)
  1121. return ret;
  1122. }
  1123. }
  1124. }
  1125. }
  1126. }
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. return ret;
  1132. }
  1133. static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  1134. {
  1135. int ret = AVERROR_BUG;
  1136. int i;
  1137. int tp_index = 0;
  1138. s->bit_index = 8;
  1139. if (tile->poc.nb_poc) {
  1140. for (i=0; i<tile->poc.nb_poc; i++) {
  1141. Jpeg2000POCEntry *e = &tile->poc.poc[i];
  1142. ret = jpeg2000_decode_packets_po_iteration(s, tile,
  1143. e->RSpoc, e->CSpoc,
  1144. FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
  1145. e->REpoc,
  1146. FFMIN(e->CEpoc, s->ncomponents),
  1147. e->Ppoc, &tp_index
  1148. );
  1149. if (ret < 0)
  1150. return ret;
  1151. }
  1152. } else {
  1153. ret = jpeg2000_decode_packets_po_iteration(s, tile,
  1154. 0, 0,
  1155. tile->codsty[0].nlayers,
  1156. 33,
  1157. s->ncomponents,
  1158. tile->codsty[0].prog_order,
  1159. &tp_index
  1160. );
  1161. }
  1162. /* EOC marker reached */
  1163. bytestream2_skip(&s->g, 2);
  1164. return ret;
  1165. }
  1166. /* TIER-1 routines */
  1167. static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
  1168. int bpno, int bandno,
  1169. int vert_causal_ctx_csty_symbol)
  1170. {
  1171. int mask = 3 << (bpno - 1), y0, x, y;
  1172. for (y0 = 0; y0 < height; y0 += 4)
  1173. for (x = 0; x < width; x++)
  1174. for (y = y0; y < height && y < y0 + 4; y++) {
  1175. int flags_mask = -1;
  1176. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  1177. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
  1178. if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
  1179. && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  1180. if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
  1181. int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
  1182. if (t1->mqc.raw)
  1183. t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
  1184. else
  1185. t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
  1186. -mask : mask;
  1187. ff_jpeg2000_set_significance(t1, x, y,
  1188. t1->data[(y) * t1->stride + x] < 0);
  1189. }
  1190. t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
  1191. }
  1192. }
  1193. }
  1194. static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
  1195. int bpno, int vert_causal_ctx_csty_symbol)
  1196. {
  1197. int phalf, nhalf;
  1198. int y0, x, y;
  1199. phalf = 1 << (bpno - 1);
  1200. nhalf = -phalf;
  1201. for (y0 = 0; y0 < height; y0 += 4)
  1202. for (x = 0; x < width; x++)
  1203. for (y = y0; y < height && y < y0 + 4; y++)
  1204. if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
  1205. int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
  1206. ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
  1207. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
  1208. int r = ff_mqc_decode(&t1->mqc,
  1209. t1->mqc.cx_states + ctxno)
  1210. ? phalf : nhalf;
  1211. t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
  1212. t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
  1213. }
  1214. }
  1215. static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
  1216. int width, int height, int bpno, int bandno,
  1217. int seg_symbols, int vert_causal_ctx_csty_symbol)
  1218. {
  1219. int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
  1220. for (y0 = 0; y0 < height; y0 += 4) {
  1221. for (x = 0; x < width; x++) {
  1222. int flags_mask = -1;
  1223. if (vert_causal_ctx_csty_symbol)
  1224. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
  1225. if (y0 + 3 < height &&
  1226. !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  1227. (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  1228. (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  1229. (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
  1230. if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
  1231. continue;
  1232. runlen = ff_mqc_decode(&t1->mqc,
  1233. t1->mqc.cx_states + MQC_CX_UNI);
  1234. runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
  1235. t1->mqc.cx_states +
  1236. MQC_CX_UNI);
  1237. dec = 1;
  1238. } else {
  1239. runlen = 0;
  1240. dec = 0;
  1241. }
  1242. for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
  1243. int flags_mask = -1;
  1244. if (vert_causal_ctx_csty_symbol && y == y0 + 3)
  1245. flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
  1246. if (!dec) {
  1247. if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
  1248. dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
  1249. bandno));
  1250. }
  1251. }
  1252. if (dec) {
  1253. int xorbit;
  1254. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
  1255. &xorbit);
  1256. t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
  1257. t1->mqc.cx_states + ctxno) ^
  1258. xorbit)
  1259. ? -mask : mask;
  1260. ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
  1261. }
  1262. dec = 0;
  1263. t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
  1264. }
  1265. }
  1266. }
  1267. if (seg_symbols) {
  1268. int val;
  1269. val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1270. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1271. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1272. val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
  1273. if (val != 0xa)
  1274. av_log(s->avctx, AV_LOG_ERROR,
  1275. "Segmentation symbol value incorrect\n");
  1276. }
  1277. }
  1278. static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
  1279. Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
  1280. int width, int height, int bandpos)
  1281. {
  1282. int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
  1283. int pass_cnt = 0;
  1284. int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
  1285. int term_cnt = 0;
  1286. int coder_type;
  1287. av_assert0(width <= 1024U && height <= 1024U);
  1288. av_assert0(width*height <= 4096);
  1289. memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
  1290. /* If code-block contains no compressed data: nothing to do. */
  1291. if (!cblk->length)
  1292. return 0;
  1293. memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
  1294. cblk->data[cblk->length] = 0xff;
  1295. cblk->data[cblk->length+1] = 0xff;
  1296. ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
  1297. while (passno--) {
  1298. switch(pass_t) {
  1299. case 0:
  1300. decode_sigpass(t1, width, height, bpno + 1, bandpos,
  1301. vert_causal_ctx_csty_symbol);
  1302. break;
  1303. case 1:
  1304. decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
  1305. break;
  1306. case 2:
  1307. av_assert2(!t1->mqc.raw);
  1308. decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
  1309. codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
  1310. vert_causal_ctx_csty_symbol);
  1311. break;
  1312. }
  1313. if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
  1314. ff_mqc_init_contexts(&t1->mqc);
  1315. if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
  1316. if (term_cnt >= cblk->nb_terminations) {
  1317. av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
  1318. return AVERROR_INVALIDDATA;
  1319. }
  1320. if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
  1321. av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
  1322. cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
  1323. pass_cnt, cblk->npasses);
  1324. }
  1325. ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
  1326. }
  1327. pass_t++;
  1328. if (pass_t == 3) {
  1329. bpno--;
  1330. pass_t = 0;
  1331. }
  1332. pass_cnt ++;
  1333. }
  1334. if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
  1335. av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
  1336. cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
  1337. }
  1338. return 0;
  1339. }
  1340. /* TODO: Verify dequantization for lossless case
  1341. * comp->data can be float or int
  1342. * band->stepsize can be float or int
  1343. * depending on the type of DWT transformation.
  1344. * see ISO/IEC 15444-1:2002 A.6.1 */
  1345. /* Float dequantization of a codeblock.*/
  1346. static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
  1347. Jpeg2000Component *comp,
  1348. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  1349. {
  1350. int i, j;
  1351. int w = cblk->coord[0][1] - cblk->coord[0][0];
  1352. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  1353. float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  1354. int *src = t1->data + j*t1->stride;
  1355. for (i = 0; i < w; ++i)
  1356. datap[i] = src[i] * band->f_stepsize;
  1357. }
  1358. }
  1359. /* Integer dequantization of a codeblock.*/
  1360. static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
  1361. Jpeg2000Component *comp,
  1362. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  1363. {
  1364. int i, j;
  1365. int w = cblk->coord[0][1] - cblk->coord[0][0];
  1366. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  1367. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  1368. int *src = t1->data + j*t1->stride;
  1369. if (band->i_stepsize == 32768) {
  1370. for (i = 0; i < w; ++i)
  1371. datap[i] = src[i] / 2;
  1372. } else {
  1373. // This should be VERY uncommon
  1374. for (i = 0; i < w; ++i)
  1375. datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
  1376. }
  1377. }
  1378. }
  1379. static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
  1380. Jpeg2000Component *comp,
  1381. Jpeg2000T1Context *t1, Jpeg2000Band *band)
  1382. {
  1383. int i, j;
  1384. int w = cblk->coord[0][1] - cblk->coord[0][0];
  1385. for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
  1386. int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
  1387. int *src = t1->data + j*t1->stride;
  1388. for (i = 0; i < w; ++i)
  1389. datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
  1390. }
  1391. }
  1392. static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  1393. {
  1394. int i, csize = 1;
  1395. void *src[3];
  1396. for (i = 1; i < 3; i++) {
  1397. if (tile->codsty[0].transform != tile->codsty[i].transform) {
  1398. av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
  1399. return;
  1400. }
  1401. if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
  1402. av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
  1403. return;
  1404. }
  1405. }
  1406. for (i = 0; i < 3; i++)
  1407. if (tile->codsty[0].transform == FF_DWT97)
  1408. src[i] = tile->comp[i].f_data;
  1409. else
  1410. src[i] = tile->comp[i].i_data;
  1411. for (i = 0; i < 2; i++)
  1412. csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
  1413. s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
  1414. }
  1415. static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
  1416. {
  1417. Jpeg2000T1Context t1;
  1418. int compno, reslevelno, bandno;
  1419. /* Loop on tile components */
  1420. for (compno = 0; compno < s->ncomponents; compno++) {
  1421. Jpeg2000Component *comp = tile->comp + compno;
  1422. Jpeg2000CodingStyle *codsty = tile->codsty + compno;
  1423. t1.stride = (1<<codsty->log2_cblk_width) + 2;
  1424. /* Loop on resolution levels */
  1425. for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
  1426. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  1427. /* Loop on bands */
  1428. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  1429. int nb_precincts, precno;
  1430. Jpeg2000Band *band = rlevel->band + bandno;
  1431. int cblkno = 0, bandpos;
  1432. bandpos = bandno + (reslevelno > 0);
  1433. if (band->coord[0][0] == band->coord[0][1] ||
  1434. band->coord[1][0] == band->coord[1][1])
  1435. continue;
  1436. nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
  1437. /* Loop on precincts */
  1438. for (precno = 0; precno < nb_precincts; precno++) {
  1439. Jpeg2000Prec *prec = band->prec + precno;
  1440. /* Loop on codeblocks */
  1441. for (cblkno = 0;
  1442. cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
  1443. cblkno++) {
  1444. int x, y;
  1445. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  1446. decode_cblk(s, codsty, &t1, cblk,
  1447. cblk->coord[0][1] - cblk->coord[0][0],
  1448. cblk->coord[1][1] - cblk->coord[1][0],
  1449. bandpos);
  1450. x = cblk->coord[0][0] - band->coord[0][0];
  1451. y = cblk->coord[1][0] - band->coord[1][0];
  1452. if (codsty->transform == FF_DWT97)
  1453. dequantization_float(x, y, cblk, comp, &t1, band);
  1454. else if (codsty->transform == FF_DWT97_INT)
  1455. dequantization_int_97(x, y, cblk, comp, &t1, band);
  1456. else
  1457. dequantization_int(x, y, cblk, comp, &t1, band);
  1458. } /* end cblk */
  1459. } /*end prec */
  1460. } /* end band */
  1461. } /* end reslevel */
  1462. /* inverse DWT */
  1463. ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
  1464. } /*end comp */
  1465. }
  1466. #define WRITE_FRAME(D, PIXEL) \
  1467. static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
  1468. AVFrame * picture, int precision) \
  1469. { \
  1470. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
  1471. int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
  1472. int pixelsize = planar ? 1 : pixdesc->nb_components; \
  1473. \
  1474. int compno; \
  1475. int x, y; \
  1476. \
  1477. for (compno = 0; compno < s->ncomponents; compno++) { \
  1478. Jpeg2000Component *comp = tile->comp + compno; \
  1479. Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
  1480. PIXEL *line; \
  1481. float *datap = comp->f_data; \
  1482. int32_t *i_datap = comp->i_data; \
  1483. int cbps = s->cbps[compno]; \
  1484. int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
  1485. int plane = 0; \
  1486. \
  1487. if (planar) \
  1488. plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
  1489. \
  1490. y = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno]; \
  1491. line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
  1492. for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) { \
  1493. PIXEL *dst; \
  1494. \
  1495. x = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno]; \
  1496. dst = line + x * pixelsize + compno*!planar; \
  1497. \
  1498. if (codsty->transform == FF_DWT97) { \
  1499. for (; x < w; x++) { \
  1500. int val = lrintf(*datap) + (1 << (cbps - 1)); \
  1501. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
  1502. val = av_clip(val, 0, (1 << cbps) - 1); \
  1503. *dst = val << (precision - cbps); \
  1504. datap++; \
  1505. dst += pixelsize; \
  1506. } \
  1507. } else { \
  1508. for (; x < w; x++) { \
  1509. int val = *i_datap + (1 << (cbps - 1)); \
  1510. /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
  1511. val = av_clip(val, 0, (1 << cbps) - 1); \
  1512. *dst = val << (precision - cbps); \
  1513. i_datap++; \
  1514. dst += pixelsize; \
  1515. } \
  1516. } \
  1517. line += picture->linesize[plane] / sizeof(PIXEL); \
  1518. } \
  1519. } \
  1520. \
  1521. }
  1522. WRITE_FRAME(8, uint8_t)
  1523. WRITE_FRAME(16, uint16_t)
  1524. #undef WRITE_FRAME
  1525. static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  1526. AVFrame *picture)
  1527. {
  1528. int x;
  1529. tile_codeblocks(s, tile);
  1530. /* inverse MCT transformation */
  1531. if (tile->codsty[0].mct)
  1532. mct_decode(s, tile);
  1533. if (s->cdef[0] < 0) {
  1534. for (x = 0; x < s->ncomponents; x++)
  1535. s->cdef[x] = x + 1;
  1536. if ((s->ncomponents & 1) == 0)
  1537. s->cdef[s->ncomponents-1] = 0;
  1538. }
  1539. if (s->precision <= 8) {
  1540. write_frame_8(s, tile, picture, 8);
  1541. } else {
  1542. int precision = picture->format == AV_PIX_FMT_XYZ12 ||
  1543. picture->format == AV_PIX_FMT_RGB48 ||
  1544. picture->format == AV_PIX_FMT_RGBA64 ||
  1545. picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
  1546. write_frame_16(s, tile, picture, precision);
  1547. }
  1548. return 0;
  1549. }
  1550. static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
  1551. {
  1552. int tileno, compno;
  1553. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
  1554. if (s->tile[tileno].comp) {
  1555. for (compno = 0; compno < s->ncomponents; compno++) {
  1556. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1557. Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
  1558. ff_jpeg2000_cleanup(comp, codsty);
  1559. }
  1560. av_freep(&s->tile[tileno].comp);
  1561. }
  1562. }
  1563. av_freep(&s->tile);
  1564. memset(s->codsty, 0, sizeof(s->codsty));
  1565. memset(s->qntsty, 0, sizeof(s->qntsty));
  1566. memset(s->properties, 0, sizeof(s->properties));
  1567. memset(&s->poc , 0, sizeof(s->poc));
  1568. s->numXtiles = s->numYtiles = 0;
  1569. s->ncomponents = 0;
  1570. }
  1571. static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
  1572. {
  1573. Jpeg2000CodingStyle *codsty = s->codsty;
  1574. Jpeg2000QuantStyle *qntsty = s->qntsty;
  1575. Jpeg2000POC *poc = &s->poc;
  1576. uint8_t *properties = s->properties;
  1577. for (;;) {
  1578. int len, ret = 0;
  1579. uint16_t marker;
  1580. int oldpos;
  1581. if (bytestream2_get_bytes_left(&s->g) < 2) {
  1582. av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
  1583. break;
  1584. }
  1585. marker = bytestream2_get_be16u(&s->g);
  1586. oldpos = bytestream2_tell(&s->g);
  1587. if (marker == JPEG2000_SOD) {
  1588. Jpeg2000Tile *tile;
  1589. Jpeg2000TilePart *tp;
  1590. if (!s->tile) {
  1591. av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
  1592. return AVERROR_INVALIDDATA;
  1593. }
  1594. if (s->curtileno < 0) {
  1595. av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
  1596. return AVERROR_INVALIDDATA;
  1597. }
  1598. tile = s->tile + s->curtileno;
  1599. tp = tile->tile_part + tile->tp_idx;
  1600. if (tp->tp_end < s->g.buffer) {
  1601. av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
  1602. return AVERROR_INVALIDDATA;
  1603. }
  1604. bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
  1605. bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
  1606. continue;
  1607. }
  1608. if (marker == JPEG2000_EOC)
  1609. break;
  1610. len = bytestream2_get_be16(&s->g);
  1611. if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
  1612. av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
  1613. return AVERROR_INVALIDDATA;
  1614. }
  1615. switch (marker) {
  1616. case JPEG2000_SIZ:
  1617. if (s->ncomponents) {
  1618. av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
  1619. return AVERROR_INVALIDDATA;
  1620. }
  1621. ret = get_siz(s);
  1622. if (!s->tile)
  1623. s->numXtiles = s->numYtiles = 0;
  1624. break;
  1625. case JPEG2000_COC:
  1626. ret = get_coc(s, codsty, properties);
  1627. break;
  1628. case JPEG2000_COD:
  1629. ret = get_cod(s, codsty, properties);
  1630. break;
  1631. case JPEG2000_QCC:
  1632. ret = get_qcc(s, len, qntsty, properties);
  1633. break;
  1634. case JPEG2000_QCD:
  1635. ret = get_qcd(s, len, qntsty, properties);
  1636. break;
  1637. case JPEG2000_POC:
  1638. ret = get_poc(s, len, poc);
  1639. break;
  1640. case JPEG2000_SOT:
  1641. if (!(ret = get_sot(s, len))) {
  1642. av_assert1(s->curtileno >= 0);
  1643. codsty = s->tile[s->curtileno].codsty;
  1644. qntsty = s->tile[s->curtileno].qntsty;
  1645. poc = &s->tile[s->curtileno].poc;
  1646. properties = s->tile[s->curtileno].properties;
  1647. }
  1648. break;
  1649. case JPEG2000_PLM:
  1650. // the PLM marker is ignored
  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. };