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