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.

2373 lines
94KB

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