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 "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 #include "thread.h"
36 #include "jpeg2000.h"
37 
38 #define JP2_SIG_TYPE 0x6A502020
39 #define JP2_SIG_VALUE 0x0D0A870A
40 #define JP2_CODESTREAM 0x6A703263
41 #define JP2_HEADER 0x6A703268
42 
43 #define HAD_COC 0x01
44 #define HAD_QCC 0x02
45 
46 typedef struct Jpeg2000TilePart {
47  uint8_t tile_index; // Tile index who refers the tile-part
48  const uint8_t *tp_end;
49  GetByteContext tpg; // bit stream in tile-part
51 
52 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
53  * one per component, so tile_part elements have a size of 3 */
54 typedef struct Jpeg2000Tile {
60  uint16_t tp_idx; // Tile-part index
61 } Jpeg2000Tile;
62 
63 typedef struct Jpeg2000DecoderContext {
64  AVClass *class;
67 
68  int width, height;
71  uint8_t cbps[4]; // bits per sample in particular components
72  uint8_t sgnd[4]; // if a component is signed
74  int cdx[4], cdy[4];
75  int precision;
78  uint32_t palette[256];
79  int8_t pal8;
80  int cdef[4];
82  unsigned numXtiles, numYtiles;
84 
87 
88  int bit_index;
89 
90  int curtileno;
91 
93 
94  /*options parameters*/
97 
98 /* get_bits functions for JPEG2000 packet bitstream
99  * It is a get_bit function with a bit-stuffing routine. If the value of the
100  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
101  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
103 {
104  int res = 0;
105 
106  while (--n >= 0) {
107  res <<= 1;
108  if (s->bit_index == 0) {
109  s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
110  }
111  s->bit_index--;
112  res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
113  }
114  return res;
115 }
116 
118 {
119  if (bytestream2_get_byte(&s->g) == 0xff)
120  bytestream2_skip(&s->g, 1);
121  s->bit_index = 8;
122 }
123 
124 /* decode the value stored in node */
126  int threshold)
127 {
128  Jpeg2000TgtNode *stack[30];
129  int sp = -1, curval = 0;
130 
131  if (!node)
132  return AVERROR_INVALIDDATA;
133 
134  while (node && !node->vis) {
135  stack[++sp] = node;
136  node = node->parent;
137  }
138 
139  if (node)
140  curval = node->val;
141  else
142  curval = stack[sp]->val;
143 
144  while (curval < threshold && sp >= 0) {
145  if (curval < stack[sp]->val)
146  curval = stack[sp]->val;
147  while (curval < threshold) {
148  int ret;
149  if ((ret = get_bits(s, 1)) > 0) {
150  stack[sp]->vis++;
151  break;
152  } else if (!ret)
153  curval++;
154  else
155  return ret;
156  }
157  stack[sp]->val = curval;
158  sp--;
159  }
160  return curval;
161 }
162 
163 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
164  int bpc, uint32_t log2_chroma_wh, int pal8)
165 {
166  int match = 1;
167  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
168 
169  if (desc->nb_components != components) {
170  return 0;
171  }
172 
173  switch (components) {
174  case 4:
175  match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
176  (log2_chroma_wh >> 14 & 3) == 0 &&
177  (log2_chroma_wh >> 12 & 3) == 0;
178  case 3:
179  match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
180  (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
181  (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
182  case 2:
183  match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
184  (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
185  (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
186 
187  case 1:
188  match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
189  (log2_chroma_wh >> 2 & 3) == 0 &&
190  (log2_chroma_wh & 3) == 0 &&
191  (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
192  }
193  return match;
194 }
195 
196 // pix_fmts with lower bpp have to be listed before
197 // similar pix_fmts with higher bpp.
198 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
199 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
200 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
201  AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
202  AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
203  AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
204  AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
205  AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
206  AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
207  AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
208  AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
209  AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
210  AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
211 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
212 
221 
222 /* marker segments */
223 /* get sizes and offsets of image, tiles; number of components */
225 {
226  int i;
227  int ncomponents;
228  uint32_t log2_chroma_wh = 0;
229  const enum AVPixelFormat *possible_fmts = NULL;
230  int possible_fmts_nb = 0;
231 
232  if (bytestream2_get_bytes_left(&s->g) < 36)
233  return AVERROR_INVALIDDATA;
234 
235  s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
236  s->width = bytestream2_get_be32u(&s->g); // Width
237  s->height = bytestream2_get_be32u(&s->g); // Height
238  s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
239  s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
240  s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
241  s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
242  s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
243  s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
244  ncomponents = bytestream2_get_be16u(&s->g); // CSiz
245 
246  if (s->image_offset_x || s->image_offset_y) {
247  avpriv_request_sample(s->avctx, "Support for image offsets");
248  return AVERROR_PATCHWELCOME;
249  }
250 
251  if (ncomponents <= 0) {
252  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
253  s->ncomponents);
254  return AVERROR_INVALIDDATA;
255  }
256 
257  if (ncomponents > 4) {
258  avpriv_request_sample(s->avctx, "Support for %d components",
259  s->ncomponents);
260  return AVERROR_PATCHWELCOME;
261  }
262 
263  s->ncomponents = ncomponents;
264 
265  if (s->tile_width <= 0 || s->tile_height <= 0) {
266  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
267  s->tile_width, s->tile_height);
268  return AVERROR_INVALIDDATA;
269  }
270 
271  if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
272  return AVERROR_INVALIDDATA;
273 
274  for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
275  uint8_t x = bytestream2_get_byteu(&s->g);
276  s->cbps[i] = (x & 0x7f) + 1;
277  s->precision = FFMAX(s->cbps[i], s->precision);
278  s->sgnd[i] = !!(x & 0x80);
279  s->cdx[i] = bytestream2_get_byteu(&s->g);
280  s->cdy[i] = bytestream2_get_byteu(&s->g);
281  if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
282  || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
283  av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
284  return AVERROR_INVALIDDATA;
285  }
286  log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
287  }
288 
291 
292  if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
293  s->numXtiles = s->numYtiles = 0;
294  return AVERROR(EINVAL);
295  }
296 
297  s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
298  if (!s->tile) {
299  s->numXtiles = s->numYtiles = 0;
300  return AVERROR(ENOMEM);
301  }
302 
303  for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
304  Jpeg2000Tile *tile = s->tile + i;
305 
306  tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
307  if (!tile->comp)
308  return AVERROR(ENOMEM);
309  }
310 
311  /* compute image size with reduction factor */
313  s->reduction_factor);
315  s->reduction_factor);
316 
319  possible_fmts = xyz_pix_fmts;
320  possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
321  } else {
322  switch (s->colour_space) {
323  case 16:
324  possible_fmts = rgb_pix_fmts;
325  possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
326  break;
327  case 17:
328  possible_fmts = gray_pix_fmts;
329  possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
330  break;
331  case 18:
332  possible_fmts = yuv_pix_fmts;
333  possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
334  break;
335  default:
336  possible_fmts = all_pix_fmts;
337  possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
338  break;
339  }
340  }
341  for (i = 0; i < possible_fmts_nb; ++i) {
342  if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
343  s->avctx->pix_fmt = possible_fmts[i];
344  break;
345  }
346  }
347  if (i == possible_fmts_nb) {
349  "Unknown pix_fmt, profile: %d, colour_space: %d, "
350  "components: %d, precision: %d, "
351  "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
352  s->avctx->profile, s->colour_space, ncomponents, s->precision,
353  ncomponents > 2 ? s->cdx[1] : 0,
354  ncomponents > 2 ? s->cdy[1] : 0,
355  ncomponents > 2 ? s->cdx[2] : 0,
356  ncomponents > 2 ? s->cdy[2] : 0);
357  return AVERROR_PATCHWELCOME;
358  }
360  return 0;
361 }
362 
363 /* get common part for COD and COC segments */
365 {
366  uint8_t byte;
367 
368  if (bytestream2_get_bytes_left(&s->g) < 5)
369  return AVERROR_INVALIDDATA;
370 
371  /* nreslevels = number of resolution levels
372  = number of decomposition level +1 */
373  c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
375  av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
376  return AVERROR_INVALIDDATA;
377  }
378 
379  if (c->nreslevels <= s->reduction_factor) {
380  /* we are forced to update reduction_factor as its requested value is
381  not compatible with this bitstream, and as we might have used it
382  already in setup earlier we have to fail this frame until
383  reinitialization is implemented */
384  av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
385  s->reduction_factor = c->nreslevels - 1;
386  return AVERROR(EINVAL);
387  }
388 
389  /* compute number of resolution levels to decode */
391 
392  c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
393  c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
394 
395  if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
396  c->log2_cblk_width + c->log2_cblk_height > 12) {
397  av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
398  return AVERROR_INVALIDDATA;
399  }
400 
401  if (c->log2_cblk_width > 6 || c->log2_cblk_height > 6) {
402  avpriv_request_sample(s->avctx, "cblk size > 64");
403  return AVERROR_PATCHWELCOME;
404  }
405 
406  c->cblk_style = bytestream2_get_byteu(&s->g);
407  if (c->cblk_style != 0) { // cblk style
408  av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
409  }
410  c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
411  /* set integer 9/7 DWT in case of BITEXACT flag */
412  if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
413  c->transform = FF_DWT97_INT;
414 
415  if (c->csty & JPEG2000_CSTY_PREC) {
416  int i;
417  for (i = 0; i < c->nreslevels; i++) {
418  byte = bytestream2_get_byte(&s->g);
419  c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
420  c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
421  }
422  } else {
423  memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
424  memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
425  }
426  return 0;
427 }
428 
429 /* get coding parameters for a particular tile or whole image*/
431  uint8_t *properties)
432 {
434  int compno, ret;
435 
436  if (bytestream2_get_bytes_left(&s->g) < 5)
437  return AVERROR_INVALIDDATA;
438 
439  tmp.csty = bytestream2_get_byteu(&s->g);
440 
441  // get progression order
442  tmp.prog_order = bytestream2_get_byteu(&s->g);
443 
444  tmp.nlayers = bytestream2_get_be16u(&s->g);
445  tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
446 
447  if (tmp.mct && s->ncomponents < 3) {
449  "MCT %d with too few components (%d)\n",
450  tmp.mct, s->ncomponents);
451  return AVERROR_INVALIDDATA;
452  }
453 
454  if ((ret = get_cox(s, &tmp)) < 0)
455  return ret;
456 
457  for (compno = 0; compno < s->ncomponents; compno++)
458  if (!(properties[compno] & HAD_COC))
459  memcpy(c + compno, &tmp, sizeof(tmp));
460  return 0;
461 }
462 
463 /* Get coding parameters for a component in the whole image or a
464  * particular tile. */
466  uint8_t *properties)
467 {
468  int compno, ret;
469 
470  if (bytestream2_get_bytes_left(&s->g) < 2)
471  return AVERROR_INVALIDDATA;
472 
473  compno = bytestream2_get_byteu(&s->g);
474 
475  if (compno >= s->ncomponents) {
477  "Invalid compno %d. There are %d components in the image.\n",
478  compno, s->ncomponents);
479  return AVERROR_INVALIDDATA;
480  }
481 
482  c += compno;
483  c->csty = bytestream2_get_byteu(&s->g);
484 
485  if ((ret = get_cox(s, c)) < 0)
486  return ret;
487 
488  properties[compno] |= HAD_COC;
489  return 0;
490 }
491 
492 /* Get common part for QCD and QCC segments. */
494 {
495  int i, x;
496 
497  if (bytestream2_get_bytes_left(&s->g) < 1)
498  return AVERROR_INVALIDDATA;
499 
500  x = bytestream2_get_byteu(&s->g); // Sqcd
501 
502  q->nguardbits = x >> 5;
503  q->quantsty = x & 0x1f;
504 
505  if (q->quantsty == JPEG2000_QSTY_NONE) {
506  n -= 3;
507  if (bytestream2_get_bytes_left(&s->g) < n ||
509  return AVERROR_INVALIDDATA;
510  for (i = 0; i < n; i++)
511  q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
512  } else if (q->quantsty == JPEG2000_QSTY_SI) {
513  if (bytestream2_get_bytes_left(&s->g) < 2)
514  return AVERROR_INVALIDDATA;
515  x = bytestream2_get_be16u(&s->g);
516  q->expn[0] = x >> 11;
517  q->mant[0] = x & 0x7ff;
518  for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
519  int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
520  q->expn[i] = curexpn;
521  q->mant[i] = q->mant[0];
522  }
523  } else {
524  n = (n - 3) >> 1;
525  if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
527  return AVERROR_INVALIDDATA;
528  for (i = 0; i < n; i++) {
529  x = bytestream2_get_be16u(&s->g);
530  q->expn[i] = x >> 11;
531  q->mant[i] = x & 0x7ff;
532  }
533  }
534  return 0;
535 }
536 
537 /* Get quantization parameters for a particular tile or a whole image. */
539  uint8_t *properties)
540 {
541  Jpeg2000QuantStyle tmp;
542  int compno, ret;
543 
544  if ((ret = get_qcx(s, n, &tmp)) < 0)
545  return ret;
546  for (compno = 0; compno < s->ncomponents; compno++)
547  if (!(properties[compno] & HAD_QCC))
548  memcpy(q + compno, &tmp, sizeof(tmp));
549  return 0;
550 }
551 
552 /* Get quantization parameters for a component in the whole image
553  * on in a particular tile. */
555  uint8_t *properties)
556 {
557  int compno;
558 
559  if (bytestream2_get_bytes_left(&s->g) < 1)
560  return AVERROR_INVALIDDATA;
561 
562  compno = bytestream2_get_byteu(&s->g);
563 
564  if (compno >= s->ncomponents) {
566  "Invalid compno %d. There are %d components in the image.\n",
567  compno, s->ncomponents);
568  return AVERROR_INVALIDDATA;
569  }
570 
571  properties[compno] |= HAD_QCC;
572  return get_qcx(s, n - 1, q + compno);
573 }
574 
575 /* Get start of tile segment. */
577 {
578  Jpeg2000TilePart *tp;
579  uint16_t Isot;
580  uint32_t Psot;
581  uint8_t TPsot;
582 
583  if (bytestream2_get_bytes_left(&s->g) < 8)
584  return AVERROR_INVALIDDATA;
585 
586  s->curtileno = 0;
587  Isot = bytestream2_get_be16u(&s->g); // Isot
588  if (Isot >= s->numXtiles * s->numYtiles)
589  return AVERROR_INVALIDDATA;
590 
591  s->curtileno = Isot;
592  Psot = bytestream2_get_be32u(&s->g); // Psot
593  TPsot = bytestream2_get_byteu(&s->g); // TPsot
594 
595  /* Read TNSot but not used */
596  bytestream2_get_byteu(&s->g); // TNsot
597 
598  if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
599  av_log(s->avctx, AV_LOG_ERROR, "Psot %d too big\n", Psot);
600  return AVERROR_INVALIDDATA;
601  }
602 
603  if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
604  avpriv_request_sample(s->avctx, "Support for %d components", TPsot);
605  return AVERROR_PATCHWELCOME;
606  }
607 
608  s->tile[Isot].tp_idx = TPsot;
609  tp = s->tile[Isot].tile_part + TPsot;
610  tp->tile_index = Isot;
611  tp->tp_end = s->g.buffer + Psot - n - 2;
612 
613  if (!TPsot) {
614  Jpeg2000Tile *tile = s->tile + s->curtileno;
615 
616  /* copy defaults */
617  memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
618  memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
619  }
620 
621  return 0;
622 }
623 
624 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
625  * Used to know the number of tile parts and lengths.
626  * There may be multiple TLMs in the header.
627  * TODO: The function is not used for tile-parts management, nor anywhere else.
628  * It can be useful to allocate memory for tile parts, before managing the SOT
629  * markers. Parsing the TLM header is needed to increment the input header
630  * buffer.
631  * This marker is mandatory for DCI. */
633 {
634  uint8_t Stlm, ST, SP, tile_tlm, i;
635  bytestream2_get_byte(&s->g); /* Ztlm: skipped */
636  Stlm = bytestream2_get_byte(&s->g);
637 
638  // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
639  ST = (Stlm >> 4) & 0x03;
640  // TODO: Manage case of ST = 0b11 --> raise error
641  SP = (Stlm >> 6) & 0x01;
642  tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
643  for (i = 0; i < tile_tlm; i++) {
644  switch (ST) {
645  case 0:
646  break;
647  case 1:
648  bytestream2_get_byte(&s->g);
649  break;
650  case 2:
651  bytestream2_get_be16(&s->g);
652  break;
653  case 3:
654  bytestream2_get_be32(&s->g);
655  break;
656  }
657  if (SP == 0) {
658  bytestream2_get_be16(&s->g);
659  } else {
660  bytestream2_get_be32(&s->g);
661  }
662  }
663  return 0;
664 }
665 
666 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
667 {
668  int compno;
669  int tilex = tileno % s->numXtiles;
670  int tiley = tileno / s->numXtiles;
671  Jpeg2000Tile *tile = s->tile + tileno;
672 
673  if (!tile->comp)
674  return AVERROR(ENOMEM);
675 
676  for (compno = 0; compno < s->ncomponents; compno++) {
677  Jpeg2000Component *comp = tile->comp + compno;
678  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
679  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
680  int ret; // global bandno
681 
682  comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
683  comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
684  comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
685  comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
686 
687  comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
688  comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
689  comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
690  comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
691 
692  if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
693  s->cbps[compno], s->cdx[compno],
694  s->cdy[compno], s->avctx))
695  return ret;
696  }
697  return 0;
698 }
699 
700 /* Read the number of coding passes. */
702 {
703  int num;
704  if (!get_bits(s, 1))
705  return 1;
706  if (!get_bits(s, 1))
707  return 2;
708  if ((num = get_bits(s, 2)) != 3)
709  return num < 0 ? num : 3 + num;
710  if ((num = get_bits(s, 5)) != 31)
711  return num < 0 ? num : 6 + num;
712  num = get_bits(s, 7);
713  return num < 0 ? num : 37 + num;
714 }
715 
717 {
718  int res = 0, ret;
719  while (ret = get_bits(s, 1)) {
720  if (ret < 0)
721  return ret;
722  res++;
723  }
724  return res;
725 }
726 
728  Jpeg2000CodingStyle *codsty,
729  Jpeg2000ResLevel *rlevel, int precno,
730  int layno, uint8_t *expn, int numgbits)
731 {
732  int bandno, cblkno, ret, nb_code_blocks;
733 
734  if (!(ret = get_bits(s, 1))) {
735  jpeg2000_flush(s);
736  return 0;
737  } else if (ret < 0)
738  return ret;
739 
740  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
741  Jpeg2000Band *band = rlevel->band + bandno;
742  Jpeg2000Prec *prec = band->prec + precno;
743 
744  if (band->coord[0][0] == band->coord[0][1] ||
745  band->coord[1][0] == band->coord[1][1])
746  continue;
747  nb_code_blocks = prec->nb_codeblocks_height *
748  prec->nb_codeblocks_width;
749  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
750  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
751  int incl, newpasses, llen;
752 
753  if (cblk->npasses)
754  incl = get_bits(s, 1);
755  else
756  incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
757  if (!incl)
758  continue;
759  else if (incl < 0)
760  return incl;
761 
762  if (!cblk->npasses) {
763  int v = expn[bandno] + numgbits - 1 -
764  tag_tree_decode(s, prec->zerobits + cblkno, 100);
765  if (v < 0) {
767  "nonzerobits %d invalid\n", v);
768  return AVERROR_INVALIDDATA;
769  }
770  cblk->nonzerobits = v;
771  }
772  if ((newpasses = getnpasses(s)) < 0)
773  return newpasses;
774  if ((llen = getlblockinc(s)) < 0)
775  return llen;
776  cblk->lblock += llen;
777  if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
778  return ret;
779  if (ret > sizeof(cblk->data)) {
781  "Block with lengthinc greater than %zu",
782  sizeof(cblk->data));
783  return AVERROR_PATCHWELCOME;
784  }
785  cblk->lengthinc = ret;
786  cblk->npasses += newpasses;
787  }
788  }
789  jpeg2000_flush(s);
790 
791  if (codsty->csty & JPEG2000_CSTY_EPH) {
792  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
793  bytestream2_skip(&s->g, 2);
794  else
795  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
796  }
797 
798  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
799  Jpeg2000Band *band = rlevel->band + bandno;
800  Jpeg2000Prec *prec = band->prec + precno;
801 
802  nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
803  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
804  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
805  if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
806  || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
807  ) {
809  "Block length %d or lengthinc %d is too large\n",
810  cblk->length, cblk->lengthinc);
811  return AVERROR_INVALIDDATA;
812  }
813 
814  bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
815  cblk->length += cblk->lengthinc;
816  cblk->lengthinc = 0;
817  }
818  }
819  return 0;
820 }
821 
823 {
824  int ret = 0;
825  int layno, reslevelno, compno, precno, ok_reslevel;
826  int x, y;
827 
828  s->bit_index = 8;
829  switch (tile->codsty[0].prog_order) {
830  case JPEG2000_PGOD_RLCP:
831  avpriv_request_sample(s->avctx, "Progression order RLCP");
832 
833  case JPEG2000_PGOD_LRCP:
834  for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
835  ok_reslevel = 1;
836  for (reslevelno = 0; ok_reslevel; reslevelno++) {
837  ok_reslevel = 0;
838  for (compno = 0; compno < s->ncomponents; compno++) {
839  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
840  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
841  if (reslevelno < codsty->nreslevels) {
842  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
843  reslevelno;
844  ok_reslevel = 1;
845  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
846  if ((ret = jpeg2000_decode_packet(s,
847  codsty, rlevel,
848  precno, layno,
849  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
850  qntsty->nguardbits)) < 0)
851  return ret;
852  }
853  }
854  }
855  }
856  break;
857 
858  case JPEG2000_PGOD_CPRL:
859  for (compno = 0; compno < s->ncomponents; compno++) {
860  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
861  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
862 
863  /* Set bit stream buffer address according to tile-part.
864  * For DCinema one tile-part per component, so can be
865  * indexed by component. */
866  s->g = tile->tile_part[compno].tpg;
867 
868  /* Position loop (y axis)
869  * TODO: Automate computing of step 256.
870  * Fixed here, but to be computed before entering here. */
871  for (y = 0; y < s->height; y += 256) {
872  /* Position loop (y axis)
873  * TODO: automate computing of step 256.
874  * Fixed here, but to be computed before entering here. */
875  for (x = 0; x < s->width; x += 256) {
876  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
877  uint16_t prcx, prcy;
878  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
879  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
880 
881  if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
882  (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
883  continue;
884 
885  if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
886  (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
887  continue;
888 
889  // check if a precinct exists
890  prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
891  prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
892  precno = prcx + rlevel->num_precincts_x * prcy;
893 
894  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y)
895  return AVERROR_PATCHWELCOME;
896 
897  for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
898  if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
899  precno, layno,
900  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
901  qntsty->nguardbits)) < 0)
902  return ret;
903  }
904  }
905  }
906  }
907  }
908  break;
909 
910  case JPEG2000_PGOD_RPCL:
911  avpriv_request_sample(s->avctx, "Progression order RPCL");
912  ret = AVERROR_PATCHWELCOME;
913  break;
914 
915  case JPEG2000_PGOD_PCRL:
916  avpriv_request_sample(s->avctx, "Progression order PCRL");
917  ret = AVERROR_PATCHWELCOME;
918  break;
919 
920  default:
921  break;
922  }
923 
924  /* EOC marker reached */
925  bytestream2_skip(&s->g, 2);
926 
927  return ret;
928 }
929 
930 /* TIER-1 routines */
932  int bpno, int bandno, int bpass_csty_symbol,
933  int vert_causal_ctx_csty_symbol)
934 {
935  int mask = 3 << (bpno - 1), y0, x, y;
936 
937  for (y0 = 0; y0 < height; y0 += 4)
938  for (x = 0; x < width; x++)
939  for (y = y0; y < height && y < y0 + 4; y++) {
940  if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
941  && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
942  int flags_mask = -1;
943  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
945  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
946  int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
947  if (bpass_csty_symbol)
948  t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
949  else
950  t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
951  -mask : mask;
952 
954  t1->data[y][x] < 0);
955  }
956  t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
957  }
958  }
959 }
960 
962  int bpno)
963 {
964  int phalf, nhalf;
965  int y0, x, y;
966 
967  phalf = 1 << (bpno - 1);
968  nhalf = -phalf;
969 
970  for (y0 = 0; y0 < height; y0 += 4)
971  for (x = 0; x < width; x++)
972  for (y = y0; y < height && y < y0 + 4; y++)
973  if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
974  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
975  int r = ff_mqc_decode(&t1->mqc,
976  t1->mqc.cx_states + ctxno)
977  ? phalf : nhalf;
978  t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
979  t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
980  }
981 }
982 
984  int width, int height, int bpno, int bandno,
985  int seg_symbols, int vert_causal_ctx_csty_symbol)
986 {
987  int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
988 
989  for (y0 = 0; y0 < height; y0 += 4) {
990  for (x = 0; x < width; x++) {
991  if (y0 + 3 < height &&
992  !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
993  (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
994  (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
995  (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
996  if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
997  continue;
998  runlen = ff_mqc_decode(&t1->mqc,
999  t1->mqc.cx_states + MQC_CX_UNI);
1000  runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1001  t1->mqc.cx_states +
1002  MQC_CX_UNI);
1003  dec = 1;
1004  } else {
1005  runlen = 0;
1006  dec = 0;
1007  }
1008 
1009  for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1010  if (!dec) {
1011  if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1012  int flags_mask = -1;
1013  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1015  dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
1016  bandno));
1017  }
1018  }
1019  if (dec) {
1020  int xorbit;
1021  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
1022  &xorbit);
1023  t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
1024  t1->mqc.cx_states + ctxno) ^
1025  xorbit)
1026  ? -mask : mask;
1027  ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
1028  }
1029  dec = 0;
1030  t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
1031  }
1032  }
1033  }
1034  if (seg_symbols) {
1035  int val;
1036  val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1037  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1038  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1039  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1040  if (val != 0xa)
1042  "Segmentation symbol value incorrect\n");
1043  }
1044 }
1045 
1048  int width, int height, int bandpos)
1049 {
1050  int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
1051  int clnpass_cnt = 0;
1052  int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
1053  int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1054 
1055  av_assert0(width <= JPEG2000_MAX_CBLKW);
1056  av_assert0(height <= JPEG2000_MAX_CBLKH);
1057 
1058  for (y = 0; y < height; y++)
1059  memset(t1->data[y], 0, width * sizeof(**t1->data));
1060 
1061  /* If code-block contains no compressed data: nothing to do. */
1062  if (!cblk->length)
1063  return 0;
1064 
1065  for (y = 0; y < height + 2; y++)
1066  memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
1067 
1068  cblk->data[cblk->length] = 0xff;
1069  cblk->data[cblk->length+1] = 0xff;
1070  ff_mqc_initdec(&t1->mqc, cblk->data);
1071 
1072  while (passno--) {
1073  switch(pass_t) {
1074  case 0:
1075  decode_sigpass(t1, width, height, bpno + 1, bandpos,
1076  bpass_csty_symbol && (clnpass_cnt >= 4),
1077  vert_causal_ctx_csty_symbol);
1078  break;
1079  case 1:
1080  decode_refpass(t1, width, height, bpno + 1);
1081  if (bpass_csty_symbol && clnpass_cnt >= 4)
1082  ff_mqc_initdec(&t1->mqc, cblk->data);
1083  break;
1084  case 2:
1085  decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1086  codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1087  vert_causal_ctx_csty_symbol);
1088  clnpass_cnt = clnpass_cnt + 1;
1089  if (bpass_csty_symbol && clnpass_cnt >= 4)
1090  ff_mqc_initdec(&t1->mqc, cblk->data);
1091  break;
1092  }
1093 
1094  pass_t++;
1095  if (pass_t == 3) {
1096  bpno--;
1097  pass_t = 0;
1098  }
1099  }
1100  return 0;
1101 }
1102 
1103 /* TODO: Verify dequantization for lossless case
1104  * comp->data can be float or int
1105  * band->stepsize can be float or int
1106  * depending on the type of DWT transformation.
1107  * see ISO/IEC 15444-1:2002 A.6.1 */
1108 
1109 /* Float dequantization of a codeblock.*/
1110 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1113 {
1114  int i, j;
1115  int w = cblk->coord[0][1] - cblk->coord[0][0];
1116  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1117  float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1118  int *src = t1->data[j];
1119  for (i = 0; i < w; ++i)
1120  datap[i] = src[i] * band->f_stepsize;
1121  }
1122 }
1123 
1124 /* Integer dequantization of a codeblock.*/
1125 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1128 {
1129  int i, j;
1130  int w = cblk->coord[0][1] - cblk->coord[0][0];
1131  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1132  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1133  int *src = t1->data[j];
1134  for (i = 0; i < w; ++i)
1135  datap[i] = (src[i] * band->i_stepsize + (1 << 14)) >> 15;
1136  }
1137 }
1138 
1139 /* Inverse ICT parameters in float and integer.
1140  * int value = (float value) * (1<<16) */
1141 static const float f_ict_params[4] = {
1142  1.402f,
1143  0.34413f,
1144  0.71414f,
1145  1.772f
1146 };
1147 static const int i_ict_params[4] = {
1148  91881,
1149  22553,
1150  46802,
1151  116130
1152 };
1153 
1155 {
1156  int i, csize = 1;
1157  int32_t *src[3], i0, i1, i2;
1158  float *srcf[3], i0f, i1f, i2f;
1159 
1160  for (i = 1; i < 3; i++)
1161  if (tile->codsty[0].transform != tile->codsty[i].transform) {
1162  av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1163  return;
1164  }
1165 
1166  for (i = 0; i < 3; i++)
1167  if (tile->codsty[0].transform == FF_DWT97)
1168  srcf[i] = tile->comp[i].f_data;
1169  else
1170  src [i] = tile->comp[i].i_data;
1171 
1172  for (i = 0; i < 2; i++)
1173  csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1174 
1175  switch (tile->codsty[0].transform) {
1176  case FF_DWT97:
1177  for (i = 0; i < csize; i++) {
1178  i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
1179  i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
1180  - (f_ict_params[2] * *srcf[2]);
1181  i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
1182  *srcf[0]++ = i0f;
1183  *srcf[1]++ = i1f;
1184  *srcf[2]++ = i2f;
1185  }
1186  break;
1187  case FF_DWT97_INT:
1188  for (i = 0; i < csize; i++) {
1189  i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1190  i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1191  - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1192  i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1193  *src[0]++ = i0;
1194  *src[1]++ = i1;
1195  *src[2]++ = i2;
1196  }
1197  break;
1198  case FF_DWT53:
1199  for (i = 0; i < csize; i++) {
1200  i1 = *src[0] - (*src[2] + *src[1] >> 2);
1201  i0 = i1 + *src[2];
1202  i2 = i1 + *src[1];
1203  *src[0]++ = i0;
1204  *src[1]++ = i1;
1205  *src[2]++ = i2;
1206  }
1207  break;
1208  }
1209 }
1210 
1212  AVFrame *picture)
1213 {
1214  int compno, reslevelno, bandno;
1215  int x, y;
1216 
1217  uint8_t *line;
1219 
1220  /* Loop on tile components */
1221  for (compno = 0; compno < s->ncomponents; compno++) {
1222  Jpeg2000Component *comp = tile->comp + compno;
1223  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1224 
1225  /* Loop on resolution levels */
1226  for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1227  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1228  /* Loop on bands */
1229  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1230  int nb_precincts, precno;
1231  Jpeg2000Band *band = rlevel->band + bandno;
1232  int cblkno = 0, bandpos;
1233 
1234  bandpos = bandno + (reslevelno > 0);
1235 
1236  if (band->coord[0][0] == band->coord[0][1] ||
1237  band->coord[1][0] == band->coord[1][1])
1238  continue;
1239 
1240  nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1241  /* Loop on precincts */
1242  for (precno = 0; precno < nb_precincts; precno++) {
1243  Jpeg2000Prec *prec = band->prec + precno;
1244 
1245  /* Loop on codeblocks */
1246  for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1247  int x, y;
1248  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1249  decode_cblk(s, codsty, &t1, cblk,
1250  cblk->coord[0][1] - cblk->coord[0][0],
1251  cblk->coord[1][1] - cblk->coord[1][0],
1252  bandpos);
1253 
1254  x = cblk->coord[0][0];
1255  y = cblk->coord[1][0];
1256 
1257  if (codsty->transform == FF_DWT97)
1258  dequantization_float(x, y, cblk, comp, &t1, band);
1259  else
1260  dequantization_int(x, y, cblk, comp, &t1, band);
1261  } /* end cblk */
1262  } /*end prec */
1263  } /* end band */
1264  } /* end reslevel */
1265 
1266  /* inverse DWT */
1267  ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1268  } /*end comp */
1269 
1270  /* inverse MCT transformation */
1271  if (tile->codsty[0].mct)
1272  mct_decode(s, tile);
1273 
1274  if (s->cdef[0] < 0) {
1275  for (x = 0; x < s->ncomponents; x++)
1276  s->cdef[x] = x + 1;
1277  if ((s->ncomponents & 1) == 0)
1278  s->cdef[s->ncomponents-1] = 0;
1279  }
1280 
1281  if (s->precision <= 8) {
1282  for (compno = 0; compno < s->ncomponents; compno++) {
1283  Jpeg2000Component *comp = tile->comp + compno;
1284  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1285  float *datap = comp->f_data;
1286  int32_t *i_datap = comp->i_data;
1287  int cbps = s->cbps[compno];
1288  int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1289  int planar = !!picture->data[2];
1290  int pixelsize = planar ? 1 : s->ncomponents;
1291  int plane = 0;
1292 
1293  if (planar)
1294  plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1295 
1296 
1297  y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1298  line = picture->data[plane] + y / s->cdy[compno] * picture->linesize[plane];
1299  for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1300  uint8_t *dst;
1301 
1302  x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1303  dst = line + x / s->cdx[compno] * pixelsize + compno*!planar;
1304 
1305  if (codsty->transform == FF_DWT97) {
1306  for (; x < w; x += s->cdx[compno]) {
1307  int val = lrintf(*datap) + (1 << (cbps - 1));
1308  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1309  val = av_clip(val, 0, (1 << cbps) - 1);
1310  *dst = val << (8 - cbps);
1311  datap++;
1312  dst += pixelsize;
1313  }
1314  } else {
1315  for (; x < w; x += s->cdx[compno]) {
1316  int val = *i_datap + (1 << (cbps - 1));
1317  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1318  val = av_clip(val, 0, (1 << cbps) - 1);
1319  *dst = val << (8 - cbps);
1320  i_datap++;
1321  dst += pixelsize;
1322  }
1323  }
1324  line += picture->linesize[plane];
1325  }
1326  }
1327  } else {
1328  for (compno = 0; compno < s->ncomponents; compno++) {
1329  Jpeg2000Component *comp = tile->comp + compno;
1330  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1331  float *datap = comp->f_data;
1332  int32_t *i_datap = comp->i_data;
1333  uint16_t *linel;
1334  int cbps = s->cbps[compno];
1335  int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1336  int planar = !!picture->data[2];
1337  int pixelsize = planar ? 1 : s->ncomponents;
1338  int plane = 0;
1339 
1340  if (planar)
1341  plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1342 
1343  y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1344  linel = (uint16_t *)picture->data[plane] + y / s->cdy[compno] * (picture->linesize[plane] >> 1);
1345  for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1346  uint16_t *dst;
1347 
1348  x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1349  dst = linel + (x / s->cdx[compno] * pixelsize + compno*!planar);
1350  if (codsty->transform == FF_DWT97) {
1351  for (; x < w; x += s-> cdx[compno]) {
1352  int val = lrintf(*datap) + (1 << (cbps - 1));
1353  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1354  val = av_clip(val, 0, (1 << cbps) - 1);
1355  /* align 12 bit values in little-endian mode */
1356  *dst = val << (16 - cbps);
1357  datap++;
1358  dst += pixelsize;
1359  }
1360  } else {
1361  for (; x < w; x += s-> cdx[compno]) {
1362  int val = *i_datap + (1 << (cbps - 1));
1363  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1364  val = av_clip(val, 0, (1 << cbps) - 1);
1365  /* align 12 bit values in little-endian mode */
1366  *dst = val << (16 - cbps);
1367  i_datap++;
1368  dst += pixelsize;
1369  }
1370  }
1371  linel += picture->linesize[plane] >> 1;
1372  }
1373  }
1374  }
1375 
1376  return 0;
1377 }
1378 
1380 {
1381  int tileno, compno;
1382  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1383  if (s->tile[tileno].comp) {
1384  for (compno = 0; compno < s->ncomponents; compno++) {
1385  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1386  Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1387 
1388  ff_jpeg2000_cleanup(comp, codsty);
1389  }
1390  av_freep(&s->tile[tileno].comp);
1391  }
1392  }
1393  av_freep(&s->tile);
1394  memset(s->codsty, 0, sizeof(s->codsty));
1395  memset(s->qntsty, 0, sizeof(s->qntsty));
1396  s->numXtiles = s->numYtiles = 0;
1397 }
1398 
1400 {
1401  Jpeg2000CodingStyle *codsty = s->codsty;
1402  Jpeg2000QuantStyle *qntsty = s->qntsty;
1403  uint8_t *properties = s->properties;
1404 
1405  for (;;) {
1406  int len, ret = 0;
1407  uint16_t marker;
1408  int oldpos;
1409 
1410  if (bytestream2_get_bytes_left(&s->g) < 2) {
1411  av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1412  break;
1413  }
1414 
1415  marker = bytestream2_get_be16u(&s->g);
1416  oldpos = bytestream2_tell(&s->g);
1417 
1418  if (marker == JPEG2000_SOD) {
1419  Jpeg2000Tile *tile;
1420  Jpeg2000TilePart *tp;
1421 
1422  if (!s->tile) {
1423  av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1424  return AVERROR_INVALIDDATA;
1425  }
1426  if (s->curtileno < 0) {
1427  av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1428  return AVERROR_INVALIDDATA;
1429  }
1430 
1431  tile = s->tile + s->curtileno;
1432  tp = tile->tile_part + tile->tp_idx;
1433  if (tp->tp_end < s->g.buffer) {
1434  av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1435  return AVERROR_INVALIDDATA;
1436  }
1437  bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1438  bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1439 
1440  continue;
1441  }
1442  if (marker == JPEG2000_EOC)
1443  break;
1444 
1445  len = bytestream2_get_be16(&s->g);
1446  if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1447  return AVERROR_INVALIDDATA;
1448 
1449  switch (marker) {
1450  case JPEG2000_SIZ:
1451  ret = get_siz(s);
1452  if (!s->tile)
1453  s->numXtiles = s->numYtiles = 0;
1454  break;
1455  case JPEG2000_COC:
1456  ret = get_coc(s, codsty, properties);
1457  break;
1458  case JPEG2000_COD:
1459  ret = get_cod(s, codsty, properties);
1460  break;
1461  case JPEG2000_QCC:
1462  ret = get_qcc(s, len, qntsty, properties);
1463  break;
1464  case JPEG2000_QCD:
1465  ret = get_qcd(s, len, qntsty, properties);
1466  break;
1467  case JPEG2000_SOT:
1468  if (!(ret = get_sot(s, len))) {
1469  av_assert1(s->curtileno >= 0);
1470  codsty = s->tile[s->curtileno].codsty;
1471  qntsty = s->tile[s->curtileno].qntsty;
1472  properties = s->tile[s->curtileno].properties;
1473  }
1474  break;
1475  case JPEG2000_COM:
1476  // the comment is ignored
1477  bytestream2_skip(&s->g, len - 2);
1478  break;
1479  case JPEG2000_TLM:
1480  // Tile-part lengths
1481  ret = get_tlm(s, len);
1482  break;
1483  default:
1485  "unsupported marker 0x%.4X at pos 0x%X\n",
1486  marker, bytestream2_tell(&s->g) - 4);
1487  bytestream2_skip(&s->g, len - 2);
1488  break;
1489  }
1490  if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1492  "error during processing marker segment %.4x\n", marker);
1493  return ret ? ret : -1;
1494  }
1495  }
1496  return 0;
1497 }
1498 
1499 /* Read bit stream packets --> T2 operation. */
1501 {
1502  int ret = 0;
1503  int tileno;
1504 
1505  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1506  Jpeg2000Tile *tile = s->tile + tileno;
1507 
1508  if (ret = init_tile(s, tileno))
1509  return ret;
1510 
1511  s->g = tile->tile_part[0].tpg;
1512  if (ret = jpeg2000_decode_packets(s, tile))
1513  return ret;
1514  }
1515 
1516  return 0;
1517 }
1518 
1520 {
1521  uint32_t atom_size, atom, atom_end;
1522  int search_range = 10;
1523 
1524  while (search_range
1525  &&
1526  bytestream2_get_bytes_left(&s->g) >= 8) {
1527  atom_size = bytestream2_get_be32u(&s->g);
1528  atom = bytestream2_get_be32u(&s->g);
1529  atom_end = bytestream2_tell(&s->g) + atom_size - 8;
1530 
1531  if (atom == JP2_CODESTREAM)
1532  return 1;
1533 
1534  if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1535  return 0;
1536 
1537  if (atom == JP2_HEADER &&
1538  atom_size >= 16) {
1539  uint32_t atom2_size, atom2, atom2_end;
1540  do {
1541  atom2_size = bytestream2_get_be32u(&s->g);
1542  atom2 = bytestream2_get_be32u(&s->g);
1543  atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
1544  if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1545  break;
1546  if (atom2 == JP2_CODESTREAM) {
1547  return 1;
1548  } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1549  int method = bytestream2_get_byteu(&s->g);
1550  bytestream2_skipu(&s->g, 2);
1551  if (method == 1) {
1552  s->colour_space = bytestream2_get_be32u(&s->g);
1553  }
1554  } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1555  int i, size, colour_count, colour_channels, colour_depth[3];
1556  uint32_t r, g, b;
1557  colour_count = bytestream2_get_be16u(&s->g);
1558  colour_channels = bytestream2_get_byteu(&s->g);
1559  // FIXME: Do not ignore channel_sign
1560  colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1561  colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1562  colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1563  size = (colour_depth[0] + 7 >> 3) * colour_count +
1564  (colour_depth[1] + 7 >> 3) * colour_count +
1565  (colour_depth[2] + 7 >> 3) * colour_count;
1566  if (colour_count > 256 ||
1567  colour_channels != 3 ||
1568  colour_depth[0] > 16 ||
1569  colour_depth[1] > 16 ||
1570  colour_depth[2] > 16 ||
1571  atom2_size < size) {
1572  avpriv_request_sample(s->avctx, "Unknown palette");
1573  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1574  continue;
1575  }
1576  s->pal8 = 1;
1577  for (i = 0; i < colour_count; i++) {
1578  if (colour_depth[0] <= 8) {
1579  r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
1580  r |= r >> colour_depth[0];
1581  } else {
1582  r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
1583  }
1584  if (colour_depth[1] <= 8) {
1585  g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
1586  r |= r >> colour_depth[1];
1587  } else {
1588  g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
1589  }
1590  if (colour_depth[2] <= 8) {
1591  b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
1592  r |= r >> colour_depth[2];
1593  } else {
1594  b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
1595  }
1596  s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
1597  }
1598  } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
1599  int n = bytestream2_get_be16u(&s->g);
1600  for (; n>0; n--) {
1601  int cn = bytestream2_get_be16(&s->g);
1602  int av_unused typ = bytestream2_get_be16(&s->g);
1603  int asoc = bytestream2_get_be16(&s->g);
1604  if (cn < 4 || asoc < 4)
1605  s->cdef[cn] = asoc;
1606  }
1607  }
1608  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1609  } while (atom_end - atom2_end >= 8);
1610  } else {
1611  search_range--;
1612  }
1613  bytestream2_seek(&s->g, atom_end, SEEK_SET);
1614  }
1615 
1616  return 0;
1617 }
1618 
1619 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1620  int *got_frame, AVPacket *avpkt)
1621 {
1623  ThreadFrame frame = { .f = data };
1624  AVFrame *picture = data;
1625  int tileno, ret;
1626 
1627  s->avctx = avctx;
1628  bytestream2_init(&s->g, avpkt->data, avpkt->size);
1629  s->curtileno = -1;
1630  memset(s->cdef, -1, sizeof(s->cdef));
1631 
1632  if (bytestream2_get_bytes_left(&s->g) < 2) {
1633  ret = AVERROR_INVALIDDATA;
1634  goto end;
1635  }
1636 
1637  // check if the image is in jp2 format
1638  if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1639  (bytestream2_get_be32u(&s->g) == 12) &&
1640  (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1641  (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1642  if (!jp2_find_codestream(s)) {
1643  av_log(avctx, AV_LOG_ERROR,
1644  "Could not find Jpeg2000 codestream atom.\n");
1645  ret = AVERROR_INVALIDDATA;
1646  goto end;
1647  }
1648  } else {
1649  bytestream2_seek(&s->g, 0, SEEK_SET);
1650  }
1651 
1652  while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
1653  bytestream2_skip(&s->g, 1);
1654 
1655  if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1656  av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1657  ret = AVERROR_INVALIDDATA;
1658  goto end;
1659  }
1660  if (ret = jpeg2000_read_main_headers(s))
1661  goto end;
1662 
1663  /* get picture buffer */
1664  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1665  goto end;
1666  picture->pict_type = AV_PICTURE_TYPE_I;
1667  picture->key_frame = 1;
1668 
1669  if (ret = jpeg2000_read_bitstream_packets(s))
1670  goto end;
1671 
1672  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1673  if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1674  goto end;
1675 
1677 
1678  *got_frame = 1;
1679 
1680  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
1681  memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
1682 
1683  return bytestream2_tell(&s->g);
1684 
1685 end:
1687  return ret;
1688 }
1689 
1691 {
1694 }
1695 
1696 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1697 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1698 
1699 static const AVOption options[] = {
1700  { "lowres", "Lower the decoding resolution by a power of two",
1701  OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1702  { NULL },
1703 };
1704 
1705 static const AVProfile profiles[] = {
1706  { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1707  { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1708  { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1709  { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1710  { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1711  { FF_PROFILE_UNKNOWN },
1712 };
1713 
1714 static const AVClass jpeg2000_class = {
1715  .class_name = "jpeg2000",
1716  .item_name = av_default_item_name,
1717  .option = options,
1718  .version = LIBAVUTIL_VERSION_INT,
1719 };
1720 
1722  .name = "jpeg2000",
1723  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1724  .type = AVMEDIA_TYPE_VIDEO,
1725  .id = AV_CODEC_ID_JPEG2000,
1726  .capabilities = CODEC_CAP_FRAME_THREADS,
1727  .priv_data_size = sizeof(Jpeg2000DecoderContext),
1728  .init_static_data = jpeg2000_init_static_data,
1730  .priv_class = &jpeg2000_class,
1731  .max_lowres = 5,
1732  .profiles = NULL_IF_CONFIG_SMALL(profiles)
1733 };