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