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