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