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