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