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