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