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