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