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