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