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