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