FFmpeg
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 #include <math.h>
30 
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "avcodec.h"
39 #include "bytestream.h"
40 #include "codec_internal.h"
41 #include "decode.h"
42 #include "thread.h"
43 #include "jpeg2000.h"
44 #include "jpeg2000dsp.h"
45 #include "profiles.h"
46 #include "jpeg2000dec.h"
47 #include "jpeg2000htdec.h"
48 
49 #define JP2_SIG_TYPE 0x6A502020
50 #define JP2_SIG_VALUE 0x0D0A870A
51 #define JP2_CODESTREAM 0x6A703263
52 #define JP2_HEADER 0x6A703268
53 
54 #define HAD_COC 0x01
55 #define HAD_QCC 0x02
56 
57 // Values of flag for placeholder passes
61 };
62 
63 #define HT_MIXED 0x80 // bit 7 of SPcod/SPcoc
64 
65 
66 /* get_bits functions for JPEG2000 packet bitstream
67  * It is a get_bit function with a bit-stuffing routine. If the value of the
68  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
69  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
70 static int get_bits(Jpeg2000DecoderContext *s, int n)
71 {
72  int res = 0;
73 
74  while (--n >= 0) {
75  res <<= 1;
76  if (s->bit_index == 0) {
77  s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
78  }
79  s->bit_index--;
80  res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
81  }
82  return res;
83 }
84 
86 {
87  if (bytestream2_get_byte(&s->g) == 0xff)
88  bytestream2_skip(&s->g, 1);
89  s->bit_index = 8;
90 }
91 
92 /* decode the value stored in node */
94  int threshold)
95 {
96  Jpeg2000TgtNode *stack[30];
97  int sp = -1, curval = 0;
98 
99  if (!node) {
100  av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
101  return AVERROR_INVALIDDATA;
102  }
103 
104  while (node && !node->vis) {
105  stack[++sp] = node;
106  node = node->parent;
107  }
108 
109  if (node)
110  curval = node->val;
111  else
112  curval = stack[sp]->val;
113 
114  while (curval < threshold && sp >= 0) {
115  if (curval < stack[sp]->val)
116  curval = stack[sp]->val;
117  while (curval < threshold) {
118  int ret;
119  if ((ret = get_bits(s, 1)) > 0) {
120  stack[sp]->vis++;
121  break;
122  } else if (!ret)
123  curval++;
124  else
125  return ret;
126  }
127  stack[sp]->val = curval;
128  sp--;
129  }
130  return curval;
131 }
132 
133 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
134  int bpc, uint32_t log2_chroma_wh, int pal8)
135 {
136  int match = 1;
138 
139  av_assert2(desc);
140 
141  if (desc->nb_components != components) {
142  return 0;
143  }
144 
145  switch (components) {
146  case 4:
147  match = match && desc->comp[3].depth >= bpc &&
148  (log2_chroma_wh >> 14 & 3) == 0 &&
149  (log2_chroma_wh >> 12 & 3) == 0;
150  case 3:
151  match = match && desc->comp[2].depth >= bpc &&
152  (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
153  (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
154  case 2:
155  match = match && desc->comp[1].depth >= bpc &&
156  (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
157  (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
158 
159  case 1:
160  match = match && desc->comp[0].depth >= bpc &&
161  (log2_chroma_wh >> 2 & 3) == 0 &&
162  (log2_chroma_wh & 3) == 0 &&
163  (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
164  }
165  return match;
166 }
167 
168 // pix_fmts with lower bpp have to be listed before
169 // similar pix_fmts with higher bpp.
170 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
171 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
172 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
173  AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
174  AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
175  AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
176  AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
177  AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
178  AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
179  AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
180  AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
181  AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
182  AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
183 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
184 
194 
195 /* marker segments */
196 /* get sizes and offsets of image, tiles; number of components */
198 {
199  int i;
200  int ncomponents;
201  uint32_t log2_chroma_wh = 0;
202  const enum AVPixelFormat *possible_fmts = NULL;
203  int possible_fmts_nb = 0;
204  int ret;
205  int o_dimx, o_dimy; //original image dimensions.
206  int dimx, dimy;
207 
208  if (bytestream2_get_bytes_left(&s->g) < 36) {
209  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
210  return AVERROR_INVALIDDATA;
211  }
212 
213  s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
214  s->width = bytestream2_get_be32u(&s->g); // Width
215  s->height = bytestream2_get_be32u(&s->g); // Height
216  s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
217  s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
218  s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
219  s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
220  s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
221  s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
222  ncomponents = bytestream2_get_be16u(&s->g); // CSiz
223 
224  if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
225  avpriv_request_sample(s->avctx, "Large Dimensions");
226  return AVERROR_PATCHWELCOME;
227  }
228 
229  if (ncomponents <= 0) {
230  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
231  s->ncomponents);
232  return AVERROR_INVALIDDATA;
233  }
234 
235  if (ncomponents > 4) {
236  avpriv_request_sample(s->avctx, "Support for %d components",
237  ncomponents);
238  return AVERROR_PATCHWELCOME;
239  }
240 
241  if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
242  s->image_offset_x < s->tile_offset_x ||
243  s->image_offset_y < s->tile_offset_y ||
244  s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
245  s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
246  ) {
247  av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
248  return AVERROR_INVALIDDATA;
249  }
250 
251  if (s->image_offset_x >= s->width || s->image_offset_y >= s->height) {
252  av_log(s->avctx, AV_LOG_ERROR, "image offsets outside image");
253  return AVERROR_INVALIDDATA;
254  }
255 
256  if (s->reduction_factor && (s->image_offset_x || s->image_offset_y) ){
257  av_log(s->avctx, AV_LOG_ERROR, "reduction factor with image offsets is not fully implemented");
258  return AVERROR_PATCHWELCOME;
259  }
260 
261  s->ncomponents = ncomponents;
262 
263  if (s->tile_width <= 0 || s->tile_height <= 0) {
264  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
265  s->tile_width, s->tile_height);
266  return AVERROR_INVALIDDATA;
267  }
268 
269  if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
270  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
271  return AVERROR_INVALIDDATA;
272  }
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 
289  s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
290  s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
291 
292  // There must be at least a SOT and SOD per tile, their minimum size is 14
293  if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
294  s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
295  ) {
296  s->numXtiles = s->numYtiles = 0;
297  return AVERROR(EINVAL);
298  }
299 
300  s->tile = av_calloc(s->numXtiles * s->numYtiles, sizeof(*s->tile));
301  if (!s->tile) {
302  s->numXtiles = s->numYtiles = 0;
303  return AVERROR(ENOMEM);
304  }
305 
306  for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
307  Jpeg2000Tile *tile = s->tile + i;
308 
309  tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
310  if (!tile->comp)
311  return AVERROR(ENOMEM);
312  }
313 
314  /* compute image size with reduction factor */
315  o_dimx = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
316  s->reduction_factor);
317  o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
318  s->reduction_factor);
319  dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
320  dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
321  for (i = 1; i < s->ncomponents; i++) {
322  dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
323  dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
324  }
325 
326  ret = ff_set_dimensions(s->avctx, dimx << s->avctx->lowres, dimy << s->avctx->lowres);
327  if (ret < 0)
328  return ret;
329 
330  if (s->avctx->profile == AV_PROFILE_JPEG2000_DCINEMA_2K ||
331  s->avctx->profile == AV_PROFILE_JPEG2000_DCINEMA_4K) {
332  possible_fmts = xyz_pix_fmts;
333  possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
334  } else {
335  switch (s->colour_space) {
336  case 16:
337  possible_fmts = rgb_pix_fmts;
338  possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
339  break;
340  case 17:
341  possible_fmts = gray_pix_fmts;
342  possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
343  break;
344  case 18:
345  possible_fmts = yuv_pix_fmts;
346  possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
347  break;
348  default:
349  possible_fmts = all_pix_fmts;
350  possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
351  break;
352  }
353  }
354  if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
355  && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
356  s->avctx->pix_fmt = AV_PIX_FMT_NONE;
357  if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
358  for (i = 0; i < possible_fmts_nb; ++i) {
359  if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
360  s->avctx->pix_fmt = possible_fmts[i];
361  break;
362  }
363  }
364 
365  if (i == possible_fmts_nb) {
366  if (ncomponents == 4 &&
367  s->cdy[0] == 1 && s->cdx[0] == 1 &&
368  s->cdy[1] == 1 && s->cdx[1] == 1 &&
369  s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
370  if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
371  s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
372  s->cdef[0] = 0;
373  s->cdef[1] = 1;
374  s->cdef[2] = 2;
375  s->cdef[3] = 3;
376  i = 0;
377  }
378  } else if (ncomponents == 3 && s->precision == 8 &&
379  s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
380  s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
381  s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
382  i = 0;
383  } else if (ncomponents == 2 && s->precision == 8 &&
384  s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
385  s->avctx->pix_fmt = AV_PIX_FMT_YA8;
386  i = 0;
387  } else if (ncomponents == 2 && s->precision == 16 &&
388  s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
389  s->avctx->pix_fmt = AV_PIX_FMT_YA16;
390  i = 0;
391  } else if (ncomponents == 1 && s->precision == 8) {
392  s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
393  i = 0;
394  } else if (ncomponents == 1 && s->precision == 12) {
395  s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
396  i = 0;
397  }
398  }
399 
400 
401  if (i == possible_fmts_nb) {
402  av_log(s->avctx, AV_LOG_ERROR,
403  "Unknown pix_fmt, profile: %d, colour_space: %d, "
404  "components: %d, precision: %d\n"
405  "cdx[0]: %d, cdy[0]: %d\n"
406  "cdx[1]: %d, cdy[1]: %d\n"
407  "cdx[2]: %d, cdy[2]: %d\n"
408  "cdx[3]: %d, cdy[3]: %d\n",
409  s->avctx->profile, s->colour_space, ncomponents, s->precision,
410  s->cdx[0],
411  s->cdy[0],
412  ncomponents > 1 ? s->cdx[1] : 0,
413  ncomponents > 1 ? s->cdy[1] : 0,
414  ncomponents > 2 ? s->cdx[2] : 0,
415  ncomponents > 2 ? s->cdy[2] : 0,
416  ncomponents > 3 ? s->cdx[3] : 0,
417  ncomponents > 3 ? s->cdy[3] : 0);
418  return AVERROR_PATCHWELCOME;
419  }
420  s->avctx->bits_per_raw_sample = s->precision;
421  return 0;
422 }
423 /* get extended capabilities (CAP) marker segment */
425 {
426  uint32_t Pcap;
427  uint16_t Ccap_i[32] = { 0 };
428  uint16_t Ccap_15;
429  uint8_t P;
430 
431  if (bytestream2_get_bytes_left(&s->g) < 6) {
432  av_log(s->avctx, AV_LOG_ERROR, "Underflow while parsing the CAP marker\n");
433  return AVERROR_INVALIDDATA;
434  }
435 
436  Pcap = bytestream2_get_be32u(&s->g);
437  s->isHT = (Pcap >> (31 - (15 - 1))) & 1;
438  for (int i = 0; i < 32; i++) {
439  if ((Pcap >> (31 - i)) & 1)
440  Ccap_i[i] = bytestream2_get_be16u(&s->g);
441  }
442  Ccap_15 = Ccap_i[14];
443  if (s->isHT == 1) {
444  av_log(s->avctx, AV_LOG_INFO, "This codestream uses the HT block coder.\n");
445  // Bits 14-15
446  switch ((Ccap_15 >> 14) & 0x3) {
447  case 0x3:
448  s->Ccap15_b14_15 = HTJ2K_MIXED;
449  break;
450  case 0x1:
451  s->Ccap15_b14_15 = HTJ2K_HTDECLARED;
452  break;
453  case 0x0:
454  s->Ccap15_b14_15 = HTJ2K_HTONLY;
455  break;
456  default:
457  av_log(s->avctx, AV_LOG_ERROR, "Unknown CCap value.\n");
458  return AVERROR(EINVAL);
459  break;
460  }
461  // Bit 13
462  if ((Ccap_15 >> 13) & 1) {
463  av_log(s->avctx, AV_LOG_ERROR, "MULTIHT set is not supported.\n");
464  return AVERROR_PATCHWELCOME;
465  }
466  // Bit 12
467  s->Ccap15_b12 = (Ccap_15 >> 12) & 1;
468  // Bit 11
469  s->Ccap15_b11 = (Ccap_15 >> 11) & 1;
470  // Bit 5
471  s->Ccap15_b05 = (Ccap_15 >> 5) & 1;
472  // Bit 0-4
473  P = Ccap_15 & 0x1F;
474  if (!P)
475  s->HT_B = 8;
476  else if (P < 20)
477  s->HT_B = P + 8;
478  else if (P < 31)
479  s->HT_B = 4 * (P - 19) + 27;
480  else
481  s->HT_B = 74;
482 
483  if (s->HT_B > 31) {
484  av_log(s->avctx, AV_LOG_ERROR, "Codestream exceeds available precision (B > 31).\n");
485  return AVERROR_PATCHWELCOME;
486  }
487  }
488  return 0;
489 }
490 
491 /* get common part for COD and COC segments */
493 {
494  uint8_t byte;
495 
496  if (bytestream2_get_bytes_left(&s->g) < 5) {
497  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
498  return AVERROR_INVALIDDATA;
499  }
500 
501  /* nreslevels = number of resolution levels
502  = number of decomposition level +1 */
503  c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
504  if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
505  av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
506  return AVERROR_INVALIDDATA;
507  }
508 
509  if (c->nreslevels <= s->reduction_factor) {
510  /* we are forced to update reduction_factor as its requested value is
511  not compatible with this bitstream, and as we might have used it
512  already in setup earlier we have to fail this frame until
513  reinitialization is implemented */
514  av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
515  s->reduction_factor = c->nreslevels - 1;
516  return AVERROR(EINVAL);
517  }
518 
519  /* compute number of resolution levels to decode */
520  c->nreslevels2decode = c->nreslevels - s->reduction_factor;
521 
522  c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
523  c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
524 
525  if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
526  c->log2_cblk_width + c->log2_cblk_height > 12) {
527  av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
528  return AVERROR_INVALIDDATA;
529  }
530 
531  c->cblk_style = bytestream2_get_byteu(&s->g);
532  if (c->cblk_style != 0) { // cblk style
533  if (c->cblk_style & JPEG2000_CTSY_HTJ2K_M || c->cblk_style & JPEG2000_CTSY_HTJ2K_F) {
534  av_log(s->avctx,AV_LOG_TRACE,"High Throughput jpeg 2000 codestream.\n");
535  } else {
536  av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
537  if (c->cblk_style & JPEG2000_CBLK_BYPASS)
538  av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
539  }
540  }
541  c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
542  /* set integer 9/7 DWT in case of BITEXACT flag */
543  if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
544  c->transform = FF_DWT97_INT;
545  else if (c->transform == FF_DWT53) {
546  s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
547  }
548 
549  if (c->csty & JPEG2000_CSTY_PREC) {
550  int i;
551  for (i = 0; i < c->nreslevels; i++) {
552  byte = bytestream2_get_byte(&s->g);
553  c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
554  c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
555  if (i)
556  if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
557  av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
558  c->log2_prec_widths[i], c->log2_prec_heights[i]);
559  c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
560  return AVERROR_INVALIDDATA;
561  }
562  }
563  } else {
564  memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
565  memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
566  }
567  return 0;
568 }
569 
570 /* get coding parameters for a particular tile or whole image*/
572  const uint8_t *properties)
573 {
575  int compno, ret;
576 
577  if (bytestream2_get_bytes_left(&s->g) < 5) {
578  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
579  return AVERROR_INVALIDDATA;
580  }
581 
582  tmp.csty = bytestream2_get_byteu(&s->g);
583 
584  // get progression order
585  tmp.prog_order = bytestream2_get_byteu(&s->g);
586 
587  tmp.nlayers = bytestream2_get_be16u(&s->g);
588  tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
589 
590  if (tmp.mct && s->ncomponents < 3) {
591  av_log(s->avctx, AV_LOG_ERROR,
592  "MCT %"PRIu8" with too few components (%d)\n",
593  tmp.mct, s->ncomponents);
594  return AVERROR_INVALIDDATA;
595  }
596 
597  if ((ret = get_cox(s, &tmp)) < 0)
598  return ret;
599  tmp.init = 1;
600  for (compno = 0; compno < s->ncomponents; compno++)
601  if (!(properties[compno] & HAD_COC))
602  memcpy(c + compno, &tmp, sizeof(tmp));
603  return 0;
604 }
605 
606 /* Get coding parameters for a component in the whole image or a
607  * particular tile. */
609  uint8_t *properties)
610 {
611  int compno, ret;
612  uint8_t has_eph, has_sop;
613 
614  if (bytestream2_get_bytes_left(&s->g) < 2) {
615  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
616  return AVERROR_INVALIDDATA;
617  }
618 
619  compno = bytestream2_get_byteu(&s->g);
620 
621  if (compno >= s->ncomponents) {
622  av_log(s->avctx, AV_LOG_ERROR,
623  "Invalid compno %d. There are %d components in the image.\n",
624  compno, s->ncomponents);
625  return AVERROR_INVALIDDATA;
626  }
627 
628  c += compno;
629  has_eph = c->csty & JPEG2000_CSTY_EPH;
630  has_sop = c->csty & JPEG2000_CSTY_SOP;
631  c->csty = bytestream2_get_byteu(&s->g);
632  c->csty |= has_eph; //do not override eph present bits from COD
633  c->csty |= has_sop; //do not override sop present bits from COD
634 
635  if ((ret = get_cox(s, c)) < 0)
636  return ret;
637 
638  properties[compno] |= HAD_COC;
639  c->init = 1;
640  return 0;
641 }
642 
643 static int get_rgn(Jpeg2000DecoderContext *s, int n)
644 {
645  uint16_t compno;
646  compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
647  bytestream2_get_be16u(&s->g);
648  if (bytestream2_get_byte(&s->g)) {
649  av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
650  return AVERROR_INVALIDDATA; // SRgn field value is 0
651  }
652  // SPrgn field
653  // Currently compno cannot be greater than 4.
654  // However, future implementation should support compno up to 65536
655  if (compno < s->ncomponents) {
656  int v;
657  if (s->curtileno == -1) {
658  v = bytestream2_get_byte(&s->g);
659  if (v > 30)
660  return AVERROR_PATCHWELCOME;
661  s->roi_shift[compno] = v;
662  } else {
663  if (s->tile[s->curtileno].tp_idx != 0)
664  return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
665  v = bytestream2_get_byte(&s->g);
666  if (v > 30)
667  return AVERROR_PATCHWELCOME;
668  s->tile[s->curtileno].comp[compno].roi_shift = v;
669  }
670  return 0;
671  }
672  return AVERROR_INVALIDDATA;
673 }
674 
675 /* Get common part for QCD and QCC segments. */
677 {
678  int i, x;
679 
680  if (bytestream2_get_bytes_left(&s->g) < 1)
681  return AVERROR_INVALIDDATA;
682 
683  x = bytestream2_get_byteu(&s->g); // Sqcd
684 
685  q->nguardbits = x >> 5;
686  q->quantsty = x & 0x1f;
687 
688  if (q->quantsty == JPEG2000_QSTY_NONE) {
689  n -= 3;
690  if (bytestream2_get_bytes_left(&s->g) < n ||
692  return AVERROR_INVALIDDATA;
693  for (i = 0; i < n; i++)
694  q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
695  } else if (q->quantsty == JPEG2000_QSTY_SI) {
696  if (bytestream2_get_bytes_left(&s->g) < 2)
697  return AVERROR_INVALIDDATA;
698  x = bytestream2_get_be16u(&s->g);
699  q->expn[0] = x >> 11;
700  q->mant[0] = x & 0x7ff;
701  for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
702  int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
703  q->expn[i] = curexpn;
704  q->mant[i] = q->mant[0];
705  }
706  } else {
707  n = (n - 3) >> 1;
708  if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
710  return AVERROR_INVALIDDATA;
711  for (i = 0; i < n; i++) {
712  x = bytestream2_get_be16u(&s->g);
713  q->expn[i] = x >> 11;
714  q->mant[i] = x & 0x7ff;
715  }
716  }
717  return 0;
718 }
719 
720 /* Get quantization parameters for a particular tile or a whole image. */
722  const uint8_t *properties)
723 {
725  int compno, ret;
726 
727  memset(&tmp, 0, sizeof(tmp));
728 
729  if ((ret = get_qcx(s, n, &tmp)) < 0)
730  return ret;
731  for (compno = 0; compno < s->ncomponents; compno++)
732  if (!(properties[compno] & HAD_QCC))
733  memcpy(q + compno, &tmp, sizeof(tmp));
734  return 0;
735 }
736 
737 /* Get quantization parameters for a component in the whole image
738  * on in a particular tile. */
740  uint8_t *properties)
741 {
742  int compno;
743 
744  if (bytestream2_get_bytes_left(&s->g) < 1)
745  return AVERROR_INVALIDDATA;
746 
747  compno = bytestream2_get_byteu(&s->g);
748 
749  if (compno >= s->ncomponents) {
750  av_log(s->avctx, AV_LOG_ERROR,
751  "Invalid compno %d. There are %d components in the image.\n",
752  compno, s->ncomponents);
753  return AVERROR_INVALIDDATA;
754  }
755 
756  properties[compno] |= HAD_QCC;
757  return get_qcx(s, n - 1, q + compno);
758 }
759 
761 {
762  int i;
763  int elem_size = s->ncomponents <= 257 ? 7 : 9;
764  Jpeg2000POC tmp = {{{0}}};
765 
766  if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
767  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
768  return AVERROR_INVALIDDATA;
769  }
770 
771  if (elem_size > 7) {
772  avpriv_request_sample(s->avctx, "Fat POC not supported");
773  return AVERROR_PATCHWELCOME;
774  }
775 
776  tmp.nb_poc = (size - 2) / elem_size;
777  if (tmp.nb_poc > MAX_POCS) {
778  avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
779  return AVERROR_PATCHWELCOME;
780  }
781 
782  for (i = 0; i<tmp.nb_poc; i++) {
783  Jpeg2000POCEntry *e = &tmp.poc[i];
784  e->RSpoc = bytestream2_get_byteu(&s->g);
785  e->CSpoc = bytestream2_get_byteu(&s->g);
786  e->LYEpoc = bytestream2_get_be16u(&s->g);
787  e->REpoc = bytestream2_get_byteu(&s->g);
788  e->CEpoc = bytestream2_get_byteu(&s->g);
789  e->Ppoc = bytestream2_get_byteu(&s->g);
790  if (!e->CEpoc)
791  e->CEpoc = 256;
792  if (e->CEpoc > s->ncomponents)
793  e->CEpoc = s->ncomponents;
794  if ( e->RSpoc >= e->REpoc || e->REpoc > 33
795  || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
796  || !e->LYEpoc) {
797  av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
798  e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
799  );
800  return AVERROR_INVALIDDATA;
801  }
802  }
803 
804  if (!p->nb_poc || p->is_default) {
805  *p = tmp;
806  } else {
807  if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
808  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
809  return AVERROR_INVALIDDATA;
810  }
811  memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
812  p->nb_poc += tmp.nb_poc;
813  }
814 
815  p->is_default = 0;
816 
817  return 0;
818 }
819 
820 
821 /* Get start of tile segment. */
822 static int get_sot(Jpeg2000DecoderContext *s, int n)
823 {
824  Jpeg2000TilePart *tp;
825  uint16_t Isot;
826  uint32_t Psot;
827  unsigned TPsot;
828 
829  if (bytestream2_get_bytes_left(&s->g) < 8)
830  return AVERROR_INVALIDDATA;
831 
832  s->curtileno = 0;
833  Isot = bytestream2_get_be16u(&s->g); // Isot
834  if (Isot >= s->numXtiles * s->numYtiles)
835  return AVERROR_INVALIDDATA;
836 
837  s->curtileno = Isot;
838  Psot = bytestream2_get_be32u(&s->g); // Psot
839  TPsot = bytestream2_get_byteu(&s->g); // TPsot
840 
841  /* Read TNSot but not used */
842  bytestream2_get_byteu(&s->g); // TNsot
843 
844  if (!Psot)
845  Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
846 
847  if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
848  av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
849  return AVERROR_INVALIDDATA;
850  }
851 
852  if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
853  avpriv_request_sample(s->avctx, "Too many tile parts");
854  return AVERROR_PATCHWELCOME;
855  }
856 
857  s->tile[Isot].tp_idx = TPsot;
858  tp = s->tile[Isot].tile_part + TPsot;
859  tp->tile_index = Isot;
860  tp->tp_end = s->g.buffer + Psot - n - 2;
861 
862  if (!TPsot) {
863  Jpeg2000Tile *tile = s->tile + s->curtileno;
864 
865  /* copy defaults */
866  memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
867  memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
868  memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
869  tile->poc.is_default = 1;
870  }
871 
872  return 0;
873 }
874 
875 static int read_crg(Jpeg2000DecoderContext *s, int n)
876 {
877  if (s->ncomponents*4 != n - 2) {
878  av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
879  return AVERROR_INVALIDDATA;
880  }
881  bytestream2_skip(&s->g, n - 2);
882  return 0;
883 }
884 
885 static int read_cpf(Jpeg2000DecoderContext *s, int n)
886 {
887  if (bytestream2_get_bytes_left(&s->g) < (n - 2))
888  return AVERROR_INVALIDDATA;
889  bytestream2_skip(&s->g, n - 2);
890  return 0;
891 }
892 
893 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
894  * Used to know the number of tile parts and lengths.
895  * There may be multiple TLMs in the header.
896  * TODO: The function is not used for tile-parts management, nor anywhere else.
897  * It can be useful to allocate memory for tile parts, before managing the SOT
898  * markers. Parsing the TLM header is needed to increment the input header
899  * buffer.
900  * This marker is mandatory for DCI. */
901 static int get_tlm(Jpeg2000DecoderContext *s, int n)
902 {
903  uint8_t Stlm, ST, SP, tile_tlm, i;
904  bytestream2_get_byte(&s->g); /* Ztlm: skipped */
905  Stlm = bytestream2_get_byte(&s->g);
906 
907  // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
908  ST = (Stlm >> 4) & 0x03;
909  if (ST == 0x03) {
910  av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
911  return AVERROR_INVALIDDATA;
912  }
913 
914  SP = (Stlm >> 6) & 0x01;
915  tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
916  for (i = 0; i < tile_tlm; i++) {
917  switch (ST) {
918  case 0:
919  break;
920  case 1:
921  bytestream2_get_byte(&s->g);
922  break;
923  case 2:
924  bytestream2_get_be16(&s->g);
925  break;
926  }
927  if (SP == 0) {
928  bytestream2_get_be16(&s->g);
929  } else {
930  bytestream2_get_be32(&s->g);
931  }
932  }
933  return 0;
934 }
935 
936 static int get_plt(Jpeg2000DecoderContext *s, int n)
937 {
938  int i;
939  int v;
940 
941  av_log(s->avctx, AV_LOG_DEBUG,
942  "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
943 
944  if (n < 4)
945  return AVERROR_INVALIDDATA;
946 
947  /*Zplt =*/ bytestream2_get_byte(&s->g);
948 
949  for (i = 0; i < n - 3; i++) {
950  v = bytestream2_get_byte(&s->g);
951  }
952  if (v & 0x80)
953  return AVERROR_INVALIDDATA;
954 
955  return 0;
956 }
957 
958 static int get_ppm(Jpeg2000DecoderContext *s, int n)
959 {
960  void *new;
961 
962  if (n < 3) {
963  av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
964  return AVERROR_INVALIDDATA;
965  }
966  bytestream2_get_byte(&s->g); //Zppm is skipped and not used
967  new = av_realloc(s->packed_headers,
968  s->packed_headers_size + n - 3);
969  if (new) {
970  s->packed_headers = new;
971  } else
972  return AVERROR(ENOMEM);
973  s->has_ppm = 1;
974  memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
975  bytestream2_get_bufferu(&s->g, s->packed_headers + s->packed_headers_size,
976  n - 3);
977  s->packed_headers_size += n - 3;
978 
979  return 0;
980 }
981 
982 static int get_ppt(Jpeg2000DecoderContext *s, int n)
983 {
984  Jpeg2000Tile *tile;
985  void *new;
986 
987  if (n < 3) {
988  av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
989  return AVERROR_INVALIDDATA;
990  }
991  if (s->curtileno < 0)
992  return AVERROR_INVALIDDATA;
993 
994  tile = &s->tile[s->curtileno];
995  if (tile->tp_idx != 0) {
996  av_log(s->avctx, AV_LOG_ERROR,
997  "PPT marker can occur only on first tile part of a tile.\n");
998  return AVERROR_INVALIDDATA;
999  }
1000 
1001  tile->has_ppt = 1; // this tile has a ppt marker
1002  bytestream2_get_byte(&s->g); // Zppt is skipped and not used
1003  new = av_realloc(tile->packed_headers,
1004  tile->packed_headers_size + n - 3);
1005  if (new) {
1006  tile->packed_headers = new;
1007  } else
1008  return AVERROR(ENOMEM);
1009  memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
1010  bytestream2_get_bufferu(&s->g, tile->packed_headers + tile->packed_headers_size, n - 3);
1011  tile->packed_headers_size += n - 3;
1012 
1013  return 0;
1014 }
1015 
1016 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
1017 {
1018  int compno;
1019  int tilex = tileno % s->numXtiles;
1020  int tiley = tileno / s->numXtiles;
1021  Jpeg2000Tile *tile = s->tile + tileno;
1022 
1023  if (!tile->comp)
1024  return AVERROR(ENOMEM);
1025 
1026  tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
1027  tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
1028  tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
1029  tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
1030 
1031  for (compno = 0; compno < s->ncomponents; compno++) {
1032  Jpeg2000Component *comp = tile->comp + compno;
1033  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1034  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1035  int ret; // global bandno
1036 
1037  comp->coord_o[0][0] = tile->coord[0][0];
1038  comp->coord_o[0][1] = tile->coord[0][1];
1039  comp->coord_o[1][0] = tile->coord[1][0];
1040  comp->coord_o[1][1] = tile->coord[1][1];
1041 
1042  comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
1043  comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
1044  comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
1045  comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
1046 
1047  comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
1048  comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
1049  comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
1050  comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
1051 
1052  if (!comp->roi_shift)
1053  comp->roi_shift = s->roi_shift[compno];
1054  if (!codsty->init)
1055  return AVERROR_INVALIDDATA;
1056  if (s->isHT && (!s->Ccap15_b05) && (!codsty->transform)) {
1057  av_log(s->avctx, AV_LOG_ERROR, "Transformation = 0 (lossy DWT) is found in HTREV HT set\n");
1058  return AVERROR_INVALIDDATA;
1059  }
1060  if (s->isHT && s->Ccap15_b14_15 != (codsty->cblk_style >> 6) && s->Ccap15_b14_15 != HTJ2K_HTONLY) {
1061  av_log(s->avctx, AV_LOG_ERROR, "SPcod/SPcoc value does not match bit 14-15 values of Ccap15\n");
1062  return AVERROR_INVALIDDATA;
1063  }
1064  if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
1065  s->cbps[compno], s->cdx[compno],
1066  s->cdy[compno], s->avctx))
1067  return ret;
1068  }
1069  return 0;
1070 }
1071 
1072 /* Read the number of coding passes. */
1074 {
1075  int num;
1076  if (!get_bits(s, 1))
1077  return 1;
1078  if (!get_bits(s, 1))
1079  return 2;
1080  if ((num = get_bits(s, 2)) != 3)
1081  return num < 0 ? num : 3 + num;
1082  if ((num = get_bits(s, 5)) != 31)
1083  return num < 0 ? num : 6 + num;
1084  num = get_bits(s, 7);
1085  return num < 0 ? num : 37 + num;
1086 }
1087 
1089 {
1090  int res = 0, ret;
1091  while (ret = get_bits(s, 1)) {
1092  if (ret < 0)
1093  return ret;
1094  res++;
1095  }
1096  return res;
1097 }
1098 
1099 static inline void select_header(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile,
1100  int *tp_index)
1101 {
1102  s->g = tile->tile_part[*tp_index].header_tpg;
1103  if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1104  av_log(s->avctx, AV_LOG_WARNING, "Packet header bytes in PPM marker segment is too short.\n");
1105  if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1106  s->g = tile->tile_part[++(*tp_index)].tpg;
1107  }
1108  }
1109 }
1110 
1111 static inline void select_stream(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile,
1112  int *tp_index, const Jpeg2000CodingStyle *codsty)
1113 {
1114  int32_t is_endof_tp;
1115 
1116  s->g = tile->tile_part[*tp_index].tpg;
1117  is_endof_tp = bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8;
1118  // Following while loop is necessary because a tilepart may include only SOD marker.
1119  // Such a tilepart has neither packet header nor compressed data.
1120  while (is_endof_tp) {
1121  if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1122  s->g = tile->tile_part[++(*tp_index)].tpg;
1123  is_endof_tp = bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8;
1124  } else {
1125  is_endof_tp = 0;
1126  }
1127  }
1128  if (codsty->csty & JPEG2000_CSTY_SOP) {
1129  if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1131  else
1132  av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1133  }
1134 }
1135 
1137  const Jpeg2000CodingStyle *codsty,
1138  Jpeg2000ResLevel *rlevel, int precno,
1139  int layno, const uint8_t *expn, int numgbits)
1140 {
1141  int bandno, cblkno, ret, nb_code_blocks;
1142  int cwsno;
1143 
1144  if (layno < rlevel->band[0].prec[precno].decoded_layers)
1145  return 0;
1146  rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1147  // Select stream to read from
1148  if (s->has_ppm)
1149  select_header(s, tile, tp_index);
1150  else if (tile->has_ppt)
1151  s->g = tile->packed_headers_stream;
1152  else
1153  select_stream(s, tile, tp_index, codsty);
1154 
1155  if (!(ret = get_bits(s, 1))) {
1156  jpeg2000_flush(s);
1157  goto skip_data;
1158  } else if (ret < 0)
1159  return ret;
1160 
1161  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1162  Jpeg2000Band *band = rlevel->band + bandno;
1163  Jpeg2000Prec *prec = band->prec + precno;
1164 
1165  if (band->coord[0][0] == band->coord[0][1] ||
1166  band->coord[1][0] == band->coord[1][1])
1167  continue;
1168  nb_code_blocks = prec->nb_codeblocks_height *
1169  prec->nb_codeblocks_width;
1170  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1171  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1172  int incl, newpasses, llen;
1173  void *tmp;
1174 
1175  if (!cblk->incl) {
1176  incl = 0;
1177  cblk->modes = codsty->cblk_style;
1178  if (cblk->modes >= JPEG2000_CTSY_HTJ2K_F)
1179  cblk->ht_plhd = HT_PLHD_ON;
1180  if (layno > 0)
1181  incl = tag_tree_decode(s, prec->cblkincl + cblkno, 0 + 1) == 0;
1182  incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1183 
1184  if (incl) {
1185  int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100);
1186  int v = expn[bandno] + numgbits - 1 - (zbp - tile->comp->roi_shift);
1187  if (v < 0 || v > 30) {
1188  av_log(s->avctx, AV_LOG_ERROR,
1189  "nonzerobits %d invalid or unsupported\n", v);
1190  return AVERROR_INVALIDDATA;
1191  }
1192  cblk->incl = 1;
1193  cblk->nonzerobits = v;
1194  cblk->zbp = zbp;
1195  cblk->lblock = 3;
1196  }
1197  } else {
1198  incl = get_bits(s, 1);
1199  }
1200 
1201  if (incl) {
1202  uint8_t bypass_term_threshold = 0;
1203  uint8_t bits_to_read = 0;
1204  uint32_t segment_bytes = 0;
1205  int32_t segment_passes = 0;
1206  uint8_t next_segment_passes = 0;
1207  int32_t href_passes, pass_bound;
1208  uint32_t tmp_length = 0;
1209  int32_t newpasses_copy, npasses_copy;
1210 
1211  if ((newpasses = getnpasses(s)) <= 0)
1212  return newpasses;
1213  if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1214  avpriv_request_sample(s->avctx, "Too many passes");
1215  return AVERROR_PATCHWELCOME;
1216  }
1217  if ((llen = getlblockinc(s)) < 0)
1218  return llen;
1219  if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1220  avpriv_request_sample(s->avctx,
1221  "Block with length beyond 16 bits");
1222  return AVERROR_PATCHWELCOME;
1223  }
1224  cblk->nb_lengthinc = 0;
1225  cblk->nb_terminationsinc = 0;
1226  av_free(cblk->lengthinc);
1227  cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc));
1228  if (!cblk->lengthinc)
1229  return AVERROR(ENOMEM);
1230  tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1,
1231  sizeof(*cblk->data_start));
1232  if (!tmp)
1233  return AVERROR(ENOMEM);
1234  cblk->data_start = tmp;
1235  cblk->lblock += llen;
1236 
1237  // Count number of necessary terminations for non HT code block
1238  newpasses_copy = newpasses;
1239  npasses_copy = cblk->npasses;
1240  if (!(cblk->modes & JPEG2000_CTSY_HTJ2K_F)) {
1241  do {
1242  int newpasses1 = 0;
1243 
1244  while (newpasses1 < newpasses_copy) {
1245  newpasses1++;
1246  if (needs_termination(codsty->cblk_style, npasses_copy + newpasses1 - 1)) {
1247  cblk->nb_terminationsinc++;
1248  break;
1249  }
1250  }
1251  npasses_copy += newpasses1;
1252  newpasses_copy -= newpasses1;
1253  } while (newpasses_copy);
1254  }
1255 
1256  if (cblk->ht_plhd) {
1257  href_passes = (cblk->npasses + newpasses - 1) % 3;
1258  segment_passes = newpasses - href_passes;
1259  pass_bound = 2;
1260  bits_to_read = cblk->lblock;
1261  if (segment_passes < 1) {
1262  // No possible HT Cleanup pass here; may have placeholder passes
1263  // or an original J2K block bit-stream (in MIXED mode).
1264  segment_passes = newpasses;
1265  while (pass_bound <= segment_passes) {
1266  bits_to_read++;
1267  pass_bound += pass_bound;
1268  }
1269  segment_bytes = get_bits(s, bits_to_read);
1270  if (segment_bytes) {
1271  if (cblk->modes & HT_MIXED) {
1272  cblk->ht_plhd = HT_PLHD_OFF;
1273  cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1274  }
1275  else {
1276  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1277  }
1278  }
1279  } else {
1280  while (pass_bound <= segment_passes) {
1281  bits_to_read++;
1282  pass_bound += pass_bound;
1283  }
1284  segment_bytes = get_bits(s, bits_to_read);
1285  if (segment_bytes) {
1286  // No more placeholder passes
1287  if (!(cblk->modes & HT_MIXED)) {
1288  // Must be the first HT Cleanup pass
1289  if (segment_bytes < 2)
1290  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1291  next_segment_passes = 2;
1292  cblk->ht_plhd = HT_PLHD_OFF;
1293  // Write length information for HT CleanUp segment
1294  cblk->pass_lengths[0] = segment_bytes;
1295  } else if (cblk->lblock > 3 && segment_bytes > 1
1296  && (segment_bytes >> (bits_to_read - 1)) == 0) {
1297  // Must be the first HT Cleanup pass, since length MSB is 0
1298  next_segment_passes = 2;
1299  cblk->ht_plhd = HT_PLHD_OFF;
1300  // Write length information for HT CleanUp segment
1301  cblk->pass_lengths[0] = segment_bytes;
1302  } else {
1303  // Must have an original (non-HT) block coding pass
1304  cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1305  cblk->ht_plhd = HT_PLHD_OFF;
1306  segment_passes = newpasses;
1307  while (pass_bound <= segment_passes) {
1308  bits_to_read++;
1309  pass_bound += pass_bound;
1310  segment_bytes <<= 1;
1311  segment_bytes += get_bits(s, 1);
1312  }
1313  }
1314  } else {
1315  // Probably parsing placeholder passes, but we need to read an
1316  // extra length bit to verify this, since prior to the first
1317  // HT Cleanup pass, the number of length bits read for a
1318  // contributing code-block is dependent on the number of passes
1319  // being included, as if it were a non-HT code-block.
1320  segment_passes = newpasses;
1321  if (pass_bound <= segment_passes) {
1322  while (1) {
1323  bits_to_read++;
1324  pass_bound += pass_bound;
1325  segment_bytes <<= 1;
1326  segment_bytes += get_bits(s, 1);
1327  if (pass_bound > segment_passes)
1328  break;
1329  }
1330  if (segment_bytes) {
1331  if (cblk->modes & HT_MIXED) {
1332  cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1333  cblk->ht_plhd = HT_PLHD_OFF;
1334  } else {
1335  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1336  }
1337  }
1338  }
1339  }
1340  }
1341  } else if (cblk->modes & JPEG2000_CTSY_HTJ2K_F) {
1342  // Quality layer commences with a non-initial HT coding pass
1343  if(bits_to_read != 0)
1344  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1345  segment_passes = cblk->npasses % 3;
1346  if (segment_passes == 0) {
1347  // newpasses is a HT Cleanup pass; next segment has refinement passes
1348  segment_passes = 1;
1349  next_segment_passes = 2;
1350  if (segment_bytes == 1)
1351  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1352  } else {
1353  // newpasses = 1 means npasses is HT SigProp; 2 means newpasses is
1354  // HT MagRef pass
1355  segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
1356  next_segment_passes = 1;
1357  bits_to_read = av_log2(segment_passes);
1358  }
1359  bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1360  segment_bytes = get_bits(s, bits_to_read);
1361  // Write length information for HT Refinment segment
1362  cblk->pass_lengths[1] += segment_bytes;
1363  } else if (!(cblk->modes & (JPEG2000_CBLK_TERMALL | JPEG2000_CBLK_BYPASS))) {
1364  // Common case for non-HT code-blocks; we have only one segment
1365  bits_to_read = (uint8_t) cblk->lblock + av_log2((uint8_t) newpasses);
1366  segment_bytes = get_bits(s, bits_to_read);
1367  segment_passes = newpasses;
1368  } else if (cblk->modes & JPEG2000_CBLK_TERMALL) {
1369  // RESTART MODE
1370  bits_to_read = cblk->lblock;
1371  segment_bytes = get_bits(s, bits_to_read);
1372  segment_passes = 1;
1373  next_segment_passes = 1;
1374  } else {
1375  // BYPASS MODE
1376  bypass_term_threshold = 10;
1377  if(bits_to_read != 0)
1378  av_log(s->avctx, AV_LOG_WARNING, "Length information for a codeblock is invalid\n");
1379  if (cblk->npasses < bypass_term_threshold) {
1380  // May have from 1 to 10 uninterrupted passes before 1st RAW SigProp
1381  segment_passes = bypass_term_threshold - cblk->npasses;
1382  if (segment_passes > newpasses)
1383  segment_passes = newpasses;
1384  while ((2 << bits_to_read) <= segment_passes)
1385  bits_to_read++;
1386  next_segment_passes = 2;
1387  } else if ((cblk->npasses - bypass_term_threshold) % 3 < 2) {
1388  // 0 means newpasses is a RAW SigProp; 1 means newpasses is a RAW MagRef pass
1389  segment_passes = newpasses > 1 ? 2 - (cblk->npasses - bypass_term_threshold) % 3 : 1;
1390  bits_to_read = av_log2(segment_passes);
1391  next_segment_passes = 1;
1392  } else {
1393  // newpasses is an isolated Cleanup pass that precedes a RAW SigProp pass
1394  segment_passes = 1;
1395  next_segment_passes = 2;
1396  }
1397  bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1398  segment_bytes = get_bits(s, bits_to_read);
1399  }
1400  // Update cblk->npasses and write length information
1401  cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1402  cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1403 
1404  if ((cblk->modes & JPEG2000_CTSY_HTJ2K_F) && cblk->ht_plhd == HT_PLHD_OFF) {
1405  newpasses -= (uint8_t) segment_passes;
1406  while (newpasses > 0) {
1407  segment_passes = newpasses > 1 ? next_segment_passes : 1;
1408  next_segment_passes = (uint8_t) (3 - next_segment_passes);
1409  bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1410  segment_bytes = get_bits(s, bits_to_read);
1411  newpasses -= (uint8_t) (segment_passes);
1412  // This is a FAST Refinement pass
1413  // Write length information for HT Refinement segment
1414  cblk->pass_lengths[1] += segment_bytes;
1415  // Update cblk->npasses and write length information
1416  cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1417  cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1418  }
1419  } else {
1420  newpasses -= (uint8_t) (segment_passes);
1421  while (newpasses > 0) {
1422  if (bypass_term_threshold != 0) {
1423  segment_passes = newpasses > 1 ? next_segment_passes : 1;
1424  next_segment_passes = (uint8_t) (3 - next_segment_passes);
1425  bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1426  } else {
1427  if ((cblk->modes & JPEG2000_CBLK_TERMALL) == 0)
1428  av_log(s->avctx, AV_LOG_WARNING, "Corrupted packet header is found.\n");
1429  segment_passes = 1;
1430  bits_to_read = cblk->lblock;
1431  }
1432  segment_bytes = get_bits(s, bits_to_read);
1433  newpasses -= (uint8_t) (segment_passes);
1434 
1435  // Update cblk->npasses and write length information
1436  cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1437  cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1438  }
1439  }
1440 
1441  for (int i = 0; i < cblk->nb_lengthinc; ++i)
1442  tmp_length = (tmp_length < cblk->lengthinc[i]) ? cblk->lengthinc[i] : tmp_length;
1443 
1444  if (tmp_length > cblk->data_allocated) {
1445  size_t new_size = FFMAX(2 * cblk->data_allocated, tmp_length);
1446  void *new = av_realloc(cblk->data, new_size);
1447  if (new) {
1448  cblk->data = new;
1449  cblk->data_allocated = new_size;
1450  }
1451  }
1452  if (tmp_length > cblk->data_allocated) {
1453  avpriv_request_sample(s->avctx,
1454  "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1455  cblk->data_allocated);
1456  return AVERROR_PATCHWELCOME;
1457  }
1458  } else {
1459  // This codeblock has no contribution to the current packet
1460  continue;
1461  }
1462  }
1463  }
1464  jpeg2000_flush(s);
1465 
1466  if (codsty->csty & JPEG2000_CSTY_EPH) {
1467  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1468  bytestream2_skip(&s->g, 2);
1469  else
1470  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1471  }
1472 
1473  // Save state of stream
1474  if (s->has_ppm) {
1475  tile->tile_part[*tp_index].header_tpg = s->g;
1476  select_stream(s, tile, tp_index, codsty);
1477  } else if (tile->has_ppt) {
1478  tile->packed_headers_stream = s->g;
1479  select_stream(s, tile, tp_index, codsty);
1480  }
1481  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1482  Jpeg2000Band *band = rlevel->band + bandno;
1483  Jpeg2000Prec *prec = band->prec + precno;
1484 
1485  nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1486  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1487  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1488  if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1489  continue;
1490  for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1491  if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1492  size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1493  void *new = av_realloc(cblk->data, new_size);
1494  if (new) {
1495  cblk->data = new;
1496  cblk->data_allocated = new_size;
1497  }
1498  }
1499  if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1500  || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1501  ) {
1502  av_log(s->avctx, AV_LOG_ERROR,
1503  "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1504  cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1505  return AVERROR_INVALIDDATA;
1506  }
1507 
1508  bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1509  cblk->length += cblk->lengthinc[cwsno];
1510  cblk->lengthinc[cwsno] = 0;
1511  if (cblk->nb_terminationsinc) {
1512  cblk->nb_terminationsinc--;
1513  cblk->nb_terminations++;
1514  cblk->data[cblk->length++] = 0xFF;
1515  cblk->data[cblk->length++] = 0xFF;
1516  cblk->data_start[cblk->nb_terminations] = cblk->length;
1517  }
1518  }
1519  av_freep(&cblk->lengthinc);
1520  }
1521  }
1522  // Save state of stream
1523  tile->tile_part[*tp_index].tpg = s->g;
1524  return 0;
1525 
1526 skip_data:
1527  if (codsty->csty & JPEG2000_CSTY_EPH) {
1528  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1529  bytestream2_skip(&s->g, 2);
1530  else
1531  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1532  }
1533  if (s->has_ppm) {
1534  tile->tile_part[*tp_index].header_tpg = s->g;
1535  select_stream(s, tile, tp_index, codsty);
1536  } else if (tile->has_ppt) {
1537  tile->packed_headers_stream = s->g;
1538  select_stream(s, tile, tp_index, codsty);
1539  }
1540  tile->tile_part[*tp_index].tpg = s->g;
1541  return 0;
1542 }
1543 
1545  int RSpoc, int CSpoc,
1546  int LYEpoc, int REpoc, int CEpoc,
1547  int Ppoc, int *tp_index)
1548 {
1549  int ret = 0;
1550  int layno, reslevelno, compno, precno, ok_reslevel;
1551  int x, y;
1552  int step_x, step_y;
1553 
1554  switch (Ppoc) {
1555  case JPEG2000_PGOD_RLCP:
1556  av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1557  ok_reslevel = 1;
1558  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1559  ok_reslevel = 0;
1560  for (layno = 0; layno < LYEpoc; layno++) {
1561  for (compno = CSpoc; compno < CEpoc; compno++) {
1562  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1563  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1564  if (reslevelno < codsty->nreslevels) {
1565  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1566  reslevelno;
1567  ok_reslevel = 1;
1568  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1569  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1570  codsty, rlevel,
1571  precno, layno,
1572  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1573  qntsty->nguardbits)) < 0)
1574  return ret;
1575  }
1576  }
1577  }
1578  }
1579  break;
1580 
1581  case JPEG2000_PGOD_LRCP:
1582  av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1583  for (layno = 0; layno < LYEpoc; layno++) {
1584  ok_reslevel = 1;
1585  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1586  ok_reslevel = 0;
1587  for (compno = CSpoc; compno < CEpoc; compno++) {
1588  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1589  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1590  if (reslevelno < codsty->nreslevels) {
1591  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1592  reslevelno;
1593  ok_reslevel = 1;
1594  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1595  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1596  codsty, rlevel,
1597  precno, layno,
1598  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1599  qntsty->nguardbits)) < 0)
1600  return ret;
1601  }
1602  }
1603  }
1604  }
1605  break;
1606 
1607  case JPEG2000_PGOD_CPRL:
1608  av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1609  for (compno = CSpoc; compno < CEpoc; compno++) {
1610  Jpeg2000Component *comp = tile->comp + compno;
1611  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1612  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1613  step_x = 32;
1614  step_y = 32;
1615 
1616  if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1617  continue;
1618 
1619  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1620  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1621  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1622  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1623  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1624  }
1625  if (step_x >= 31 || step_y >= 31){
1626  avpriv_request_sample(s->avctx, "CPRL with large step");
1627  return AVERROR_PATCHWELCOME;
1628  }
1629  step_x = 1<<step_x;
1630  step_y = 1<<step_y;
1631 
1632  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1633  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1634  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1635  unsigned prcx, prcy;
1636  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1637  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1638  int xc = x / s->cdx[compno];
1639  int yc = y / s->cdy[compno];
1640 
1641  if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1642  continue;
1643 
1644  if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1645  continue;
1646 
1647  // check if a precinct exists
1648  prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1649  prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1650  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1651  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1652 
1653  precno = prcx + rlevel->num_precincts_x * prcy;
1654 
1655  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1656  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1657  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1658  continue;
1659  }
1660 
1661  for (layno = 0; layno < LYEpoc; layno++) {
1662  if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1663  precno, layno,
1664  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1665  qntsty->nguardbits)) < 0)
1666  return ret;
1667  }
1668  }
1669  }
1670  }
1671  }
1672  break;
1673 
1674  case JPEG2000_PGOD_RPCL:
1675  av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1676  ok_reslevel = 1;
1677  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1678  ok_reslevel = 0;
1679  step_x = 30;
1680  step_y = 30;
1681  for (compno = CSpoc; compno < CEpoc; compno++) {
1682  Jpeg2000Component *comp = tile->comp + compno;
1683  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1684 
1685  if (reslevelno < codsty->nreslevels) {
1686  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1687  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1688  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1689  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1690  }
1691  }
1692  step_x = 1<<step_x;
1693  step_y = 1<<step_y;
1694 
1695  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1696  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1697  for (compno = CSpoc; compno < CEpoc; compno++) {
1698  Jpeg2000Component *comp = tile->comp + compno;
1699  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1700  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1701  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1702  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1703  unsigned prcx, prcy;
1704  int trx0, try0;
1705 
1706  if (!s->cdx[compno] || !s->cdy[compno])
1707  return AVERROR_INVALIDDATA;
1708 
1709  if (reslevelno >= codsty->nreslevels)
1710  continue;
1711 
1712  trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1713  try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1714 
1715  if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1716  (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1717  continue;
1718 
1719  if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1720  (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1721  continue;
1722 
1723  // check if a precinct exists
1724  prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1725  prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1726  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1727  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1728 
1729  precno = prcx + rlevel->num_precincts_x * prcy;
1730 
1731  ok_reslevel = 1;
1732  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1733  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1734  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1735  continue;
1736  }
1737 
1738  for (layno = 0; layno < LYEpoc; layno++) {
1739  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1740  codsty, rlevel,
1741  precno, layno,
1742  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1743  qntsty->nguardbits)) < 0)
1744  return ret;
1745  }
1746  }
1747  }
1748  }
1749  }
1750  break;
1751 
1752  case JPEG2000_PGOD_PCRL:
1753  av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1754  step_x = 32;
1755  step_y = 32;
1756  for (compno = CSpoc; compno < CEpoc; compno++) {
1757  Jpeg2000Component *comp = tile->comp + compno;
1758  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1759 
1760  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1761  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1762  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1763  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1764  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1765  }
1766  }
1767  if (step_x >= 31 || step_y >= 31){
1768  avpriv_request_sample(s->avctx, "PCRL with large step");
1769  return AVERROR_PATCHWELCOME;
1770  }
1771  step_x = 1<<step_x;
1772  step_y = 1<<step_y;
1773 
1774  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1775  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1776  for (compno = CSpoc; compno < CEpoc; compno++) {
1777  Jpeg2000Component *comp = tile->comp + compno;
1778  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1779  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1780 
1781  if (!s->cdx[compno] || !s->cdy[compno])
1782  return AVERROR_INVALIDDATA;
1783 
1784  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1785  unsigned prcx, prcy;
1786  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1787  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1788  int trx0, try0;
1789 
1790  trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1791  try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1792 
1793  if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1794  (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1795  continue;
1796 
1797  if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1798  (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1799  continue;
1800 
1801  // check if a precinct exists
1802  prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1803  prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1804  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1805  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1806 
1807  precno = prcx + rlevel->num_precincts_x * prcy;
1808 
1809  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1810  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1811  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1812  continue;
1813  }
1814 
1815  for (layno = 0; layno < LYEpoc; layno++) {
1816  if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1817  precno, layno,
1818  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1819  qntsty->nguardbits)) < 0)
1820  return ret;
1821  }
1822  }
1823  }
1824  }
1825  }
1826  break;
1827 
1828  default:
1829  break;
1830  }
1831 
1832  return ret;
1833 }
1834 
1836 {
1837  int ret = AVERROR_BUG;
1838  int i;
1839  int tp_index = 0;
1840 
1841  s->bit_index = 8;
1842  if (tile->poc.nb_poc) {
1843  for (i=0; i<tile->poc.nb_poc; i++) {
1844  Jpeg2000POCEntry *e = &tile->poc.poc[i];
1846  e->RSpoc, e->CSpoc,
1847  FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1848  e->REpoc,
1849  FFMIN(e->CEpoc, s->ncomponents),
1850  e->Ppoc, &tp_index
1851  );
1852  if (ret < 0)
1853  return ret;
1854  }
1855  } else {
1857  0, 0,
1858  tile->codsty[0].nlayers,
1859  33,
1860  s->ncomponents,
1861  tile->codsty[0].prog_order,
1862  &tp_index
1863  );
1864  }
1865  /* EOC marker reached */
1866  bytestream2_skip(&s->g, 2);
1867 
1868  return ret;
1869 }
1870 
1871 /* TIER-1 routines */
1872 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1873  int bpno, int bandno,
1874  int vert_causal_ctx_csty_symbol)
1875 {
1876  int mask = 3 << (bpno - 1), y0, x, y;
1877 
1878  for (y0 = 0; y0 < height; y0 += 4)
1879  for (x = 0; x < width; x++)
1880  for (y = y0; y < height && y < y0 + 4; y++) {
1881  int flags_mask = -1;
1882  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1884  if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1885  && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1886  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1887  int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1888  if (t1->mqc.raw)
1889  t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1890  else
1891  t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1892  -mask : mask;
1893 
1895  t1->data[(y) * t1->stride + x] < 0);
1896  }
1897  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1898  }
1899  }
1900 }
1901 
1902 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1903  int bpno, int vert_causal_ctx_csty_symbol)
1904 {
1905  int phalf, nhalf;
1906  int y0, x, y;
1907 
1908  phalf = 1 << (bpno - 1);
1909  nhalf = -phalf;
1910 
1911  for (y0 = 0; y0 < height; y0 += 4)
1912  for (x = 0; x < width; x++)
1913  for (y = y0; y < height && y < y0 + 4; y++)
1914  if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1915  int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1917  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1918  int r = ff_mqc_decode(&t1->mqc,
1919  t1->mqc.cx_states + ctxno)
1920  ? phalf : nhalf;
1921  t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1922  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1923  }
1924 }
1925 
1927  int width, int height, int bpno, int bandno,
1928  int seg_symbols, int vert_causal_ctx_csty_symbol)
1929 {
1930  int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1931 
1932  for (y0 = 0; y0 < height; y0 += 4) {
1933  for (x = 0; x < width; x++) {
1934  int flags_mask = -1;
1935  if (vert_causal_ctx_csty_symbol)
1937  if (y0 + 3 < height &&
1938  !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1939  (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1940  (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1941  (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1942  if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1943  continue;
1944  runlen = ff_mqc_decode(&t1->mqc,
1945  t1->mqc.cx_states + MQC_CX_UNI);
1946  runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1947  t1->mqc.cx_states +
1948  MQC_CX_UNI);
1949  dec = 1;
1950  } else {
1951  runlen = 0;
1952  dec = 0;
1953  }
1954 
1955  for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1956  int flags_mask = -1;
1957  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1959  if (!dec) {
1960  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1961  dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1962  bandno));
1963  }
1964  }
1965  if (dec) {
1966  int xorbit;
1967  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1968  &xorbit);
1969  t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1970  t1->mqc.cx_states + ctxno) ^
1971  xorbit)
1972  ? -mask : mask;
1973  ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1974  }
1975  dec = 0;
1976  t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1977  }
1978  }
1979  }
1980  if (seg_symbols) {
1981  int val;
1982  val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1983  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1984  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1985  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1986  if (val != 0xa)
1987  av_log(s->avctx, AV_LOG_ERROR,
1988  "Segmentation symbol value incorrect\n");
1989  }
1990 }
1991 
1993  Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1994  int width, int height, int bandpos, uint8_t roi_shift)
1995 {
1996  int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
1997  int pass_cnt = 0;
1998  int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1999  int term_cnt = 0;
2000  int coder_type;
2001 
2002  av_assert0(width <= 1024U && height <= 1024U);
2003  av_assert0(width*height <= 4096);
2004 
2005  memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
2006 
2007  /* If code-block contains no compressed data: nothing to do. */
2008  if (!cblk->length)
2009  return 0;
2010 
2011  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
2012 
2013  cblk->data[cblk->length] = 0xff;
2014  cblk->data[cblk->length+1] = 0xff;
2015  ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
2016 
2017  while (passno--) {
2018  if (bpno < 0 || bpno > 29) {
2019  av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
2020  return AVERROR_INVALIDDATA;
2021  }
2022  switch(pass_t) {
2023  case 0:
2024  decode_sigpass(t1, width, height, bpno + 1, bandpos,
2025  vert_causal_ctx_csty_symbol);
2026  break;
2027  case 1:
2028  decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
2029  break;
2030  case 2:
2031  av_assert2(!t1->mqc.raw);
2032  decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
2033  codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
2034  vert_causal_ctx_csty_symbol);
2035  break;
2036  }
2037  if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
2038  ff_mqc_init_contexts(&t1->mqc);
2039 
2040  if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
2041  if (term_cnt >= cblk->nb_terminations) {
2042  av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
2043  return AVERROR_INVALIDDATA;
2044  }
2045  if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
2046  av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
2047  cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
2048  pass_cnt, cblk->npasses);
2049  }
2050 
2051  ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
2052  }
2053 
2054  pass_t++;
2055  if (pass_t == 3) {
2056  bpno--;
2057  pass_t = 0;
2058  }
2059  pass_cnt ++;
2060  }
2061 
2062  if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
2063  av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
2064  cblk->data + cblk->length - 2 - t1->mqc.bp);
2065  }
2066 
2067  if (cblk->data + cblk->length < t1->mqc.bp) {
2068  av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
2069  }
2070 
2071  return 1;
2072 }
2073 
2075  int quan_parameter)
2076 {
2077  uint8_t roi_shift;
2078  int val;
2079  roi_shift = comp->roi_shift;
2080  val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
2081 
2082  if (val > (1 << roi_shift))
2083  return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
2084  return quan_parameter;
2085 }
2086 
2087 /* TODO: Verify dequantization for lossless case
2088  * comp->data can be float or int
2089  * band->stepsize can be float or int
2090  * depending on the type of DWT transformation.
2091  * see ISO/IEC 15444-1:2002 A.6.1 */
2092 
2093 /* Float dequantization of a codeblock.*/
2094 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
2096  Jpeg2000T1Context *t1, Jpeg2000Band *band)
2097 {
2098  int i, j;
2099  int w = cblk->coord[0][1] - cblk->coord[0][0];
2100  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2101  float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2102  int *src = t1->data + j*t1->stride;
2103  for (i = 0; i < w; ++i)
2104  datap[i] = src[i] * band->f_stepsize;
2105  }
2106 }
2107 
2108 /* Integer dequantization of a codeblock.*/
2109 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
2111  Jpeg2000T1Context *t1, Jpeg2000Band *band)
2112 {
2113  int i, j;
2114  int w = cblk->coord[0][1] - cblk->coord[0][0];
2115  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2116  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2117  int *src = t1->data + j*t1->stride;
2118  if (band->i_stepsize == 32768) {
2119  for (i = 0; i < w; ++i)
2120  datap[i] = src[i] / 2;
2121  } else {
2122  // This should be VERY uncommon
2123  for (i = 0; i < w; ++i)
2124  datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
2125  }
2126  }
2127 }
2128 
2129 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
2131  Jpeg2000T1Context *t1, Jpeg2000Band *band)
2132 {
2133  int i, j;
2134  int w = cblk->coord[0][1] - cblk->coord[0][0];
2135  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2136  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2137  int *src = t1->data + j*t1->stride;
2138  for (i = 0; i < w; ++i)
2139  datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
2140  }
2141 }
2142 
2143 static inline void mct_decode(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
2144 {
2145  int i, csize = 1;
2146  void *src[3];
2147 
2148  for (i = 1; i < 3; i++) {
2149  if (tile->codsty[0].transform != tile->codsty[i].transform) {
2150  av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
2151  return;
2152  }
2153  if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
2154  av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
2155  return;
2156  }
2157  }
2158 
2159  for (i = 0; i < 3; i++)
2160  if (tile->codsty[0].transform == FF_DWT97)
2161  src[i] = tile->comp[i].f_data;
2162  else
2163  src[i] = tile->comp[i].i_data;
2164 
2165  for (i = 0; i < 2; i++)
2166  csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
2167 
2168  s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
2169 }
2170 
2171 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
2173  Jpeg2000T1Context *t1)
2174 {
2175  int i, j;
2176  int w = cblk->coord[0][1] - cblk->coord[0][0];
2177  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2178  int *src = t1->data + j*t1->stride;
2179  for (i = 0; i < w; ++i)
2180  src[i] = roi_shift_param(comp, src[i]);
2181  }
2182 }
2183 
2184 static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
2185 {
2186  Jpeg2000T1Context t1;
2187 
2188  int compno, reslevelno, bandno;
2189 
2190  /* Loop on tile components */
2191  for (compno = 0; compno < s->ncomponents; compno++) {
2192  Jpeg2000Component *comp = tile->comp + compno;
2193  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
2194  Jpeg2000QuantStyle *quantsty = tile->qntsty + compno;
2195 
2196  int coded = 0;
2197  int subbandno = 0;
2198 
2199  t1.stride = (1<<codsty->log2_cblk_width) + 2;
2200 
2201  /* Loop on resolution levels */
2202  for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
2203  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
2204  /* Loop on bands */
2205  for (bandno = 0; bandno < rlevel->nbands; bandno++, subbandno++) {
2206  int nb_precincts, precno;
2207  Jpeg2000Band *band = rlevel->band + bandno;
2208  int cblkno = 0, bandpos;
2209  /* See Rec. ITU-T T.800, Equation E-2 */
2210  int M_b = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
2211 
2212  bandpos = bandno + (reslevelno > 0);
2213 
2214  if (band->coord[0][0] == band->coord[0][1] ||
2215  band->coord[1][0] == band->coord[1][1])
2216  continue;
2217 
2218  if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && M_b >= 31) {
2219  avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and M_b >= 31");
2220  return AVERROR_PATCHWELCOME;
2221  }
2222 
2223  nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
2224  /* Loop on precincts */
2225  for (precno = 0; precno < nb_precincts; precno++) {
2226  Jpeg2000Prec *prec = band->prec + precno;
2227 
2228  /* Loop on codeblocks */
2229  for (cblkno = 0;
2230  cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
2231  cblkno++) {
2232  int x, y, ret;
2233 
2234  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
2235 
2236  if (cblk->modes & JPEG2000_CTSY_HTJ2K_F)
2237  ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
2238  cblk->coord[0][1] - cblk->coord[0][0],
2239  cblk->coord[1][1] - cblk->coord[1][0],
2240  M_b, comp->roi_shift);
2241  else
2242  ret = decode_cblk(s, codsty, &t1, cblk,
2243  cblk->coord[0][1] - cblk->coord[0][0],
2244  cblk->coord[1][1] - cblk->coord[1][0],
2245  bandpos, comp->roi_shift);
2246 
2247  if (ret)
2248  coded = 1;
2249  else
2250  continue;
2251  x = cblk->coord[0][0] - band->coord[0][0];
2252  y = cblk->coord[1][0] - band->coord[1][0];
2253 
2254  if (comp->roi_shift)
2255  roi_scale_cblk(cblk, comp, &t1);
2256  if (codsty->transform == FF_DWT97)
2257  dequantization_float(x, y, cblk, comp, &t1, band);
2258  else if (codsty->transform == FF_DWT97_INT)
2259  dequantization_int_97(x, y, cblk, comp, &t1, band);
2260  else
2261  dequantization_int(x, y, cblk, comp, &t1, band);
2262  } /* end cblk */
2263  } /*end prec */
2264  } /* end band */
2265  } /* end reslevel */
2266 
2267  /* inverse DWT */
2268  if (coded)
2269  ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
2270 
2271  } /*end comp */
2272  return 0;
2273 }
2274 
2275 #define WRITE_FRAME(D, PIXEL) \
2276  static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2277  AVFrame * picture, int precision) \
2278  { \
2279  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2280  int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2281  int pixelsize = planar ? 1 : pixdesc->nb_components; \
2282  \
2283  int compno; \
2284  int x, y; \
2285  \
2286  for (compno = 0; compno < s->ncomponents; compno++) { \
2287  Jpeg2000Component *comp = tile->comp + compno; \
2288  Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2289  PIXEL *line; \
2290  float *datap = comp->f_data; \
2291  int32_t *i_datap = comp->i_data; \
2292  int cbps = s->cbps[compno]; \
2293  int w = tile->comp[compno].coord[0][1] - \
2294  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2295  int h = tile->comp[compno].coord[1][1] - \
2296  ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2297  int plane = 0; \
2298  \
2299  if (planar) \
2300  plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2301  \
2302  y = tile->comp[compno].coord[1][0] - \
2303  ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2304  line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2305  for (; y < h; y++) { \
2306  PIXEL *dst; \
2307  \
2308  x = tile->comp[compno].coord[0][0] - \
2309  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2310  dst = line + x * pixelsize + compno*!planar; \
2311  \
2312  if (codsty->transform == FF_DWT97) { \
2313  for (; x < w; x++) { \
2314  int val = lrintf(*datap) + (1 << (cbps - 1)); \
2315  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2316  val = av_clip(val, 0, (1 << cbps) - 1); \
2317  *dst = val << (precision - cbps); \
2318  datap++; \
2319  dst += pixelsize; \
2320  } \
2321  } else { \
2322  for (; x < w; x++) { \
2323  int val = *i_datap + (1 << (cbps - 1)); \
2324  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2325  val = av_clip(val, 0, (1 << cbps) - 1); \
2326  *dst = val << (precision - cbps); \
2327  i_datap++; \
2328  dst += pixelsize; \
2329  } \
2330  } \
2331  line += picture->linesize[plane] / sizeof(PIXEL); \
2332  } \
2333  } \
2334  \
2335  }
2336 
2337 WRITE_FRAME(8, uint8_t)
2338 WRITE_FRAME(16, uint16_t)
2339 
2340 #undef WRITE_FRAME
2341 
2342 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2343  int jobnr, int threadnr)
2344 {
2345  const Jpeg2000DecoderContext *s = avctx->priv_data;
2346  AVFrame *picture = td;
2347  Jpeg2000Tile *tile = s->tile + jobnr;
2348 
2349  int ret = tile_codeblocks(s, tile);
2350  if (ret < 0)
2351  return ret;
2352 
2353  /* inverse MCT transformation */
2354  if (tile->codsty[0].mct)
2355  mct_decode(s, tile);
2356 
2357  if (s->precision <= 8) {
2358  write_frame_8(s, tile, picture, 8);
2359  } else {
2360  int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2361  picture->format == AV_PIX_FMT_RGB48 ||
2362  picture->format == AV_PIX_FMT_RGBA64 ||
2363  picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2364 
2365  write_frame_16(s, tile, picture, precision);
2366  }
2367 
2368  return 0;
2369 }
2370 
2372 {
2373  int tileno, compno;
2374  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2375  if (s->tile[tileno].comp) {
2376  for (compno = 0; compno < s->ncomponents; compno++) {
2377  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2378  Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2379 
2380  ff_jpeg2000_cleanup(comp, codsty);
2381  }
2382  av_freep(&s->tile[tileno].comp);
2383  av_freep(&s->tile[tileno].packed_headers);
2384  s->tile[tileno].packed_headers_size = 0;
2385  }
2386  }
2387  av_freep(&s->packed_headers);
2388  s->packed_headers_size = 0;
2389  memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
2390  av_freep(&s->tile);
2391  memset(s->codsty, 0, sizeof(s->codsty));
2392  memset(s->qntsty, 0, sizeof(s->qntsty));
2393  memset(s->properties, 0, sizeof(s->properties));
2394  memset(&s->poc , 0, sizeof(s->poc));
2395  s->numXtiles = s->numYtiles = 0;
2396  s->ncomponents = 0;
2397 }
2398 
2400 {
2401  Jpeg2000CodingStyle *codsty = s->codsty;
2402  Jpeg2000QuantStyle *qntsty = s->qntsty;
2403  Jpeg2000POC *poc = &s->poc;
2404  uint8_t *properties = s->properties;
2405 
2406  for (;;) {
2407  int len, ret = 0;
2408  uint16_t marker;
2409  int oldpos;
2410 
2411  if (bytestream2_get_bytes_left(&s->g) < 2) {
2412  av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2413  break;
2414  }
2415 
2416  marker = bytestream2_get_be16u(&s->g);
2417  oldpos = bytestream2_tell(&s->g);
2418  if (marker >= 0xFF30 && marker <= 0xFF3F)
2419  continue;
2420  if (marker == JPEG2000_SOD) {
2421  Jpeg2000Tile *tile;
2422  Jpeg2000TilePart *tp;
2423 
2424  if (!s->tile) {
2425  av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2426  return AVERROR_INVALIDDATA;
2427  }
2428  if (s->curtileno < 0) {
2429  av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2430  return AVERROR_INVALIDDATA;
2431  }
2432 
2433  tile = s->tile + s->curtileno;
2434  tp = tile->tile_part + tile->tp_idx;
2435  if (tp->tp_end < s->g.buffer) {
2436  av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2437  return AVERROR_INVALIDDATA;
2438  }
2439 
2440  if (s->has_ppm) {
2441  uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
2442  if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
2443  return AVERROR_INVALIDDATA;
2444  bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
2445  bytestream2_skip(&s->packed_headers_stream, tp_header_size);
2446  }
2447  if (tile->has_ppt && tile->tp_idx == 0) {
2449  }
2450 
2451  bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2452  bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2453 
2454  continue;
2455  }
2456  if (marker == JPEG2000_EOC)
2457  break;
2458 
2459  len = bytestream2_get_be16(&s->g);
2460  if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2461  if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2462  av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2463  return AVERROR_INVALIDDATA;
2464  }
2465  av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2466  break;
2467  }
2468 
2469  switch (marker) {
2470  case JPEG2000_SIZ:
2471  if (s->ncomponents) {
2472  av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2473  return AVERROR_INVALIDDATA;
2474  }
2475  ret = get_siz(s);
2476  if (!s->tile)
2477  s->numXtiles = s->numYtiles = 0;
2478  break;
2479  case JPEG2000_CAP:
2480  if (!s->ncomponents) {
2481  av_log(s->avctx, AV_LOG_ERROR, "CAP marker segment shall come after SIZ\n");
2482  return AVERROR_INVALIDDATA;
2483  }
2484  ret = get_cap(s, codsty);
2485  break;
2486  case JPEG2000_COC:
2487  if (s->in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2488  av_log(s->avctx, AV_LOG_ERROR,
2489  "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2490  return AVERROR_INVALIDDATA;
2491  }
2492  ret = get_coc(s, codsty, properties);
2493  break;
2494  case JPEG2000_COD:
2495  if (s->in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2496  av_log(s->avctx, AV_LOG_ERROR,
2497  "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2498  return AVERROR_INVALIDDATA;
2499  }
2500  ret = get_cod(s, codsty, properties);
2501  break;
2502  case JPEG2000_RGN:
2503  if (s->in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2504  av_log(s->avctx, AV_LOG_ERROR,
2505  "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2506  return AVERROR_INVALIDDATA;
2507  }
2508  ret = get_rgn(s, len);
2509  if ((!s->Ccap15_b12) && s->isHT) {
2510  av_log(s->avctx, AV_LOG_ERROR, "RGN marker found but the codestream belongs to the RGNFREE set\n");
2511  return AVERROR_INVALIDDATA;
2512  }
2513  break;
2514  case JPEG2000_QCC:
2515  if (s->in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2516  av_log(s->avctx, AV_LOG_ERROR,
2517  "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2518  return AVERROR_INVALIDDATA;
2519  }
2520  ret = get_qcc(s, len, qntsty, properties);
2521  break;
2522  case JPEG2000_QCD:
2523  if (s->in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2524  av_log(s->avctx, AV_LOG_ERROR,
2525  "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2526  return AVERROR_INVALIDDATA;
2527  }
2528  ret = get_qcd(s, len, qntsty, properties);
2529  break;
2530  case JPEG2000_POC:
2531  if (s->in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2532  av_log(s->avctx, AV_LOG_ERROR,
2533  "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2534  return AVERROR_INVALIDDATA;
2535  }
2536  ret = get_poc(s, len, poc);
2537  break;
2538  case JPEG2000_SOT:
2539  if (!s->in_tile_headers) {
2540  s->in_tile_headers = 1;
2541  if (s->has_ppm) {
2542  bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
2543  }
2544  }
2545  if (!(ret = get_sot(s, len))) {
2546  av_assert1(s->curtileno >= 0);
2547  codsty = s->tile[s->curtileno].codsty;
2548  qntsty = s->tile[s->curtileno].qntsty;
2549  poc = &s->tile[s->curtileno].poc;
2550  properties = s->tile[s->curtileno].properties;
2551  }
2552  break;
2553  case JPEG2000_PLM:
2554  // the PLM marker is ignored
2555  case JPEG2000_COM:
2556  // the comment is ignored
2557  bytestream2_skip(&s->g, len - 2);
2558  break;
2559  case JPEG2000_CRG:
2560  ret = read_crg(s, len);
2561  break;
2562  case JPEG2000_TLM:
2563  // Tile-part lengths
2564  ret = get_tlm(s, len);
2565  break;
2566  case JPEG2000_PLT:
2567  // Packet length, tile-part header
2568  ret = get_plt(s, len);
2569  break;
2570  case JPEG2000_PPM:
2571  // Packed headers, main header
2572  if (s->in_tile_headers) {
2573  av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
2574  return AVERROR_INVALIDDATA;
2575  }
2576  ret = get_ppm(s, len);
2577  break;
2578  case JPEG2000_PPT:
2579  // Packed headers, tile-part header
2580  if (s->has_ppm) {
2581  av_log(s->avctx, AV_LOG_ERROR,
2582  "Cannot have both PPT and PPM marker.\n");
2583  return AVERROR_INVALIDDATA;
2584  }
2585  if ((!s->Ccap15_b11) && s->isHT) {
2586  av_log(s->avctx, AV_LOG_ERROR, "PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
2587  return AVERROR_INVALIDDATA;
2588  }
2589  ret = get_ppt(s, len);
2590  break;
2591  case JPEG2000_CPF:
2592  // Corresponding profile marker
2593  ret = read_cpf(s, len);
2594  break;
2595  default:
2596  av_log(s->avctx, AV_LOG_ERROR,
2597  "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2598  marker, bytestream2_tell(&s->g) - 4);
2599  bytestream2_skip(&s->g, len - 2);
2600  break;
2601  }
2602  if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2603  av_log(s->avctx, AV_LOG_ERROR,
2604  "error during processing marker segment %.4"PRIx16"\n",
2605  marker);
2606  return ret ? ret : -1;
2607  }
2608  }
2609  return 0;
2610 }
2611 
2612 /* Read bit stream packets --> T2 operation. */
2614 {
2615  int ret = 0;
2616  int tileno;
2617 
2618  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2619  Jpeg2000Tile *tile = s->tile + tileno;
2620 
2621  if ((ret = init_tile(s, tileno)) < 0)
2622  return ret;
2623 
2624  if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2625  return ret;
2626  }
2627 
2628  return 0;
2629 }
2630 
2632 {
2633  uint32_t atom_size, atom, atom_end;
2634  int search_range = 10;
2635 
2636  while (search_range
2637  &&
2638  bytestream2_get_bytes_left(&s->g) >= 8) {
2639  atom_size = bytestream2_get_be32u(&s->g);
2640  atom = bytestream2_get_be32u(&s->g);
2641  if (atom_size == 1) {
2642  if (bytestream2_get_be32u(&s->g)) {
2643  avpriv_request_sample(s->avctx, "Huge atom");
2644  return 0;
2645  }
2646  atom_size = bytestream2_get_be32u(&s->g);
2647  if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
2648  return AVERROR_INVALIDDATA;
2649  atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2650  } else {
2651  if (atom_size < 8 || (int64_t)bytestream2_tell(&s->g) + atom_size - 8 > INT_MAX)
2652  return AVERROR_INVALIDDATA;
2653  atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2654  }
2655 
2656  if (atom == JP2_CODESTREAM)
2657  return 1;
2658 
2659  if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2660  return 0;
2661 
2662  if (atom == JP2_HEADER &&
2663  atom_size >= 16) {
2664  uint32_t atom2_size, atom2, atom2_end;
2665  do {
2666  if (bytestream2_get_bytes_left(&s->g) < 8)
2667  break;
2668  atom2_size = bytestream2_get_be32u(&s->g);
2669  atom2 = bytestream2_get_be32u(&s->g);
2670  atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2671  if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2672  break;
2673  atom2_size -= 8;
2674  if (atom2 == JP2_CODESTREAM) {
2675  return 1;
2676  } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2677  int method = bytestream2_get_byteu(&s->g);
2678  bytestream2_skipu(&s->g, 2);
2679  if (method == 1) {
2680  s->colour_space = bytestream2_get_be32u(&s->g);
2681  }
2682  } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2683  int i, size, colour_count, colour_channels, colour_depth[3];
2684  colour_count = bytestream2_get_be16u(&s->g);
2685  colour_channels = bytestream2_get_byteu(&s->g);
2686  // FIXME: Do not ignore channel_sign
2687  colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2688  colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2689  colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2690  size = (colour_depth[0] + 7 >> 3) * colour_count +
2691  (colour_depth[1] + 7 >> 3) * colour_count +
2692  (colour_depth[2] + 7 >> 3) * colour_count;
2693  if (colour_count > AVPALETTE_COUNT ||
2694  colour_channels != 3 ||
2695  colour_depth[0] > 16 ||
2696  colour_depth[1] > 16 ||
2697  colour_depth[2] > 16 ||
2698  atom2_size < size) {
2699  avpriv_request_sample(s->avctx, "Unknown palette");
2700  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2701  continue;
2702  }
2703  s->pal8 = 1;
2704  for (i = 0; i < colour_count; i++) {
2705  uint32_t r, g, b;
2706  if (colour_depth[0] <= 8) {
2707  r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2708  r |= r >> colour_depth[0];
2709  } else {
2710  r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2711  }
2712  if (colour_depth[1] <= 8) {
2713  g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2714  g |= g >> colour_depth[1];
2715  } else {
2716  g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2717  }
2718  if (colour_depth[2] <= 8) {
2719  b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2720  b |= b >> colour_depth[2];
2721  } else {
2722  b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2723  }
2724  s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2725  }
2726  } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2727  int n = bytestream2_get_be16u(&s->g);
2728  for (; n>0; n--) {
2729  int cn = bytestream2_get_be16(&s->g);
2730  int av_unused typ = bytestream2_get_be16(&s->g);
2731  int asoc = bytestream2_get_be16(&s->g);
2732  if (cn < 4 && asoc < 4)
2733  s->cdef[cn] = asoc;
2734  }
2735  } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2736  int64_t vnum, vden, hnum, hden, vexp, hexp;
2737  uint32_t resx;
2738  bytestream2_skip(&s->g, 4);
2739  resx = bytestream2_get_be32u(&s->g);
2740  if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2741  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2742  continue;
2743  }
2744  vnum = bytestream2_get_be16u(&s->g);
2745  vden = bytestream2_get_be16u(&s->g);
2746  hnum = bytestream2_get_be16u(&s->g);
2747  hden = bytestream2_get_be16u(&s->g);
2748  vexp = bytestream2_get_byteu(&s->g);
2749  hexp = bytestream2_get_byteu(&s->g);
2750  if (!vnum || !vden || !hnum || !hden) {
2751  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2752  av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2753  continue;
2754  }
2755  if (vexp > hexp) {
2756  vexp -= hexp;
2757  hexp = 0;
2758  } else {
2759  hexp -= vexp;
2760  vexp = 0;
2761  }
2762  if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2763  && INT64_MAX / (vnum * hden) > pow(10, vexp))
2764  av_reduce(&s->sar.den, &s->sar.num,
2765  hnum * vden * pow(10, hexp),
2766  vnum * hden * pow(10, vexp),
2767  INT32_MAX);
2768  }
2769  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2770  } while (atom_end - atom2_end >= 8);
2771  } else {
2772  search_range--;
2773  }
2774  bytestream2_seek(&s->g, atom_end, SEEK_SET);
2775  }
2776 
2777  return 0;
2778 }
2779 
2781 {
2783 
2784  if (avctx->lowres)
2785  av_log(avctx, AV_LOG_WARNING, "lowres is overriden by reduction_factor but set anyway\n");
2786  if (!s->reduction_factor && avctx->lowres < JPEG2000_MAX_RESLEVELS) {
2787  s->reduction_factor = avctx->lowres;
2788  }
2789  if (avctx->lowres != s->reduction_factor && avctx->lowres)
2790  return AVERROR(EINVAL);
2791 
2792  ff_jpeg2000dsp_init(&s->dsp);
2794 
2795  return 0;
2796 }
2797 
2798 static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
2799  int *got_frame, AVPacket *avpkt)
2800 {
2802  int ret;
2803 
2804  s->avctx = avctx;
2805  bytestream2_init(&s->g, avpkt->data, avpkt->size);
2806  s->curtileno = -1;
2807  memset(s->cdef, -1, sizeof(s->cdef));
2808 
2809  if (bytestream2_get_bytes_left(&s->g) < 2) {
2811  goto end;
2812  }
2813 
2814  // check if the image is in jp2 format
2815  if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2816  (bytestream2_get_be32u(&s->g) == 12) &&
2817  (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2818  (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2819  if (!jp2_find_codestream(s)) {
2820  av_log(avctx, AV_LOG_ERROR,
2821  "Could not find Jpeg2000 codestream atom.\n");
2823  goto end;
2824  }
2825  } else {
2826  bytestream2_seek(&s->g, 0, SEEK_SET);
2827  }
2828 
2829  while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2830  bytestream2_skip(&s->g, 1);
2831 
2832  if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2833  av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2835  goto end;
2836  }
2838  goto end;
2839 
2840  if (s->sar.num && s->sar.den)
2841  avctx->sample_aspect_ratio = s->sar;
2842  s->sar.num = s->sar.den = 0;
2843 
2844  if (avctx->skip_frame >= AVDISCARD_ALL) {
2846  return avpkt->size;
2847  }
2848 
2849  /* get picture buffer */
2850  if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
2851  goto end;
2852 
2854  goto end;
2855 
2856  for (int x = 0; x < s->ncomponents; x++) {
2857  if (s->cdef[x] < 0) {
2858  for (x = 0; x < s->ncomponents; x++) {
2859  s->cdef[x] = x + 1;
2860  }
2861  if ((s->ncomponents & 1) == 0)
2862  s->cdef[s->ncomponents-1] = 0;
2863  break;
2864  }
2865  }
2866 
2867  avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2868 
2870 
2871  *got_frame = 1;
2872 
2873  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2874  memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2875 
2876  return bytestream2_tell(&s->g);
2877 
2878 end:
2880  return ret;
2881 }
2882 
2883 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2884 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2885 
2886 static const AVOption options[] = {
2887  { "lowres", "Lower the decoding resolution by a power of two",
2888  OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2889  { NULL },
2890 };
2891 
2892 static const AVClass jpeg2000_class = {
2893  .class_name = "jpeg2000",
2894  .item_name = av_default_item_name,
2895  .option = options,
2896  .version = LIBAVUTIL_VERSION_INT,
2897 };
2898 
2900  .p.name = "jpeg2000",
2901  CODEC_LONG_NAME("JPEG 2000"),
2902  .p.type = AVMEDIA_TYPE_VIDEO,
2903  .p.id = AV_CODEC_ID_JPEG2000,
2905  .priv_data_size = sizeof(Jpeg2000DecoderContext),
2908  .p.priv_class = &jpeg2000_class,
2909  .p.max_lowres = 5,
2911  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2912 };
tile_codeblocks
static int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:2184
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
Jpeg2000POCEntry::CEpoc
uint16_t CEpoc
Definition: jpeg2000dec.h:37
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
options
static const AVOption options[]
Definition: jpeg2000dec.c:2886
Jpeg2000Cblk::nb_terminationsinc
int nb_terminationsinc
Definition: jpeg2000.h:195
av_clip
#define av_clip
Definition: common.h:100
ff_dwt_decode
int ff_dwt_decode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:598
r
const char * r
Definition: vf_curves.c:127
JPEG2000_POC
@ JPEG2000_POC
Definition: jpeg2000.h:51
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
HAD_COC
#define HAD_COC
Definition: jpeg2000dec.c:54
AV_PIX_FMT_YA8
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:140
JP2_HEADER
#define JP2_HEADER
Definition: jpeg2000dec.c:52
Jpeg2000Cblk::pass_lengths
int pass_lengths[2]
Definition: jpeg2000.h:202
Jpeg2000QuantStyle::quantsty
uint8_t quantsty
Definition: jpeg2000.h:164
Jpeg2000Prec::decoded_layers
int decoded_layers
Definition: jpeg2000.h:213
AV_PROFILE_JPEG2000_DCINEMA_4K
#define AV_PROFILE_JPEG2000_DCINEMA_4K
Definition: defs.h:152
JPEG2000_EOC
@ JPEG2000_EOC
Definition: jpeg2000.h:60
Jpeg2000POC::is_default
int is_default
Definition: jpeg2000dec.h:46
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:81
JPEG2000_MAX_RESLEVELS
#define JPEG2000_MAX_RESLEVELS
Definition: jpeg2000.h:79
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:251
JPEG2000_QSTY_NONE
@ JPEG2000_QSTY_NONE
Definition: jpeg2000.h:73
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
Jpeg2000CodingStyle::prog_order
uint8_t prog_order
Definition: jpeg2000.h:155
JPEG2000_QCD
@ JPEG2000_QCD
Definition: jpeg2000.h:48
Jpeg2000Prec::nb_codeblocks_height
int nb_codeblocks_height
Definition: jpeg2000.h:209
Jpeg2000Cblk::coord
int coord[2][2]
Definition: jpeg2000.h:199
Jpeg2000CodingStyle::mct
uint8_t mct
Definition: jpeg2000.h:153
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
int64_t
long long int64_t
Definition: coverity.c:34
av_unused
#define av_unused
Definition: attributes.h:131
mask
int mask
Definition: mediacodecdec_common.c:154
HTJ2K_HTONLY
@ HTJ2K_HTONLY
Definition: jpeg2000.h:64
Jpeg2000Band::i_stepsize
int i_stepsize
Definition: jpeg2000.h:220
needs_termination
static int needs_termination(int style, int passno)
Definition: jpeg2000.h:302
Jpeg2000Cblk::nb_lengthinc
uint8_t nb_lengthinc
Definition: jpeg2000.h:190
decode_refpass
static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int vert_causal_ctx_csty_symbol)
Definition: jpeg2000dec.c:1902
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
YUV_PIXEL_FORMATS
#define YUV_PIXEL_FORMATS
Definition: jpeg2000dec.c:172
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:533
Jpeg2000Prec::zerobits
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:210
AVOption
AVOption.
Definition: opt.h:429
Jpeg2000POCEntry::REpoc
uint8_t REpoc
Definition: jpeg2000dec.h:39
JPEG2000_SOD
@ JPEG2000_SOD
Definition: jpeg2000.h:59
b
#define b
Definition: input.c:41
JPEG2000_SOC
@ JPEG2000_SOC
Definition: jpeg2000.h:39
JPEG2000_CSTY_PREC
#define JPEG2000_CSTY_PREC
Definition: jpeg2000.h:118
getlblockinc
static int getlblockinc(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:1088
HTJ2K_MIXED
@ HTJ2K_MIXED
Definition: jpeg2000.h:66
JPEG2000_PPM
@ JPEG2000_PPM
Definition: jpeg2000.h:52
FF_COMPLIANCE_STRICT
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: defs.h:59
ff_jpeg2000_ceildiv
static int ff_jpeg2000_ceildiv(int a, int64_t b)
Definition: jpeg2000.h:249
FFCodec
Definition: codec_internal.h:127
ff_jpeg2000_profiles
const AVProfile ff_jpeg2000_profiles[]
Definition: profiles.c:107
Jpeg2000Prec
Definition: jpeg2000.h:207
JPEG2000_CBLK_TERMALL
#define JPEG2000_CBLK_TERMALL
Definition: jpeg2000.h:112
JPEG2000_SOT
@ JPEG2000_SOT
Definition: jpeg2000.h:56
Jpeg2000POCEntry
Definition: jpeg2000dec.h:34
Jpeg2000TgtNode::parent
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:142
Jpeg2000Band
Definition: jpeg2000.h:217
FF_DWT97
@ FF_DWT97
Definition: jpeg2000dwt.h:37
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
jpeg2000_decode_tile
static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td, int jobnr, int threadnr)
Definition: jpeg2000dec.c:2342
JPEG2000_CSTY_SOP
#define JPEG2000_CSTY_SOP
Definition: jpeg2000.h:119
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:94
Jpeg2000POCEntry::CSpoc
uint16_t CSpoc
Definition: jpeg2000dec.h:36
Jpeg2000Tile
Definition: j2kenc.c:107
ff_jpeg2000_getrefctxno
static int ff_jpeg2000_getrefctxno(int flag)
Definition: jpeg2000.h:277
thread.h
getnpasses
static int getnpasses(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:1073
Jpeg2000Tile::packed_headers
uint8_t * packed_headers
Definition: jpeg2000dec.h:66
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
Jpeg2000T1Context::mqc
MqcState mqc
Definition: jpeg2000.h:134
Jpeg2000Cblk::incl
uint8_t incl
Definition: jpeg2000.h:187
Jpeg2000CodingStyle::init
uint8_t init
Definition: jpeg2000.h:158
jpeg2000_read_main_headers
static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:2399
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
Jpeg2000CodingStyle::log2_cblk_width
uint8_t log2_cblk_width
Definition: jpeg2000.h:148
jpeg2000htdec.h
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1824
ff_mqc_initdec
void ff_mqc_initdec(MqcState *mqc, uint8_t *bp, int raw, int reset)
Initialize MQ-decoder.
Definition: mqcdec.c:71
get_poc
static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
Definition: jpeg2000dec.c:760
HT_MIXED
#define HT_MIXED
Definition: jpeg2000dec.c:63
decode_sigpass
static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int vert_causal_ctx_csty_symbol)
Definition: jpeg2000dec.c:1872
val
static double val(void *priv, double ch)
Definition: aeval.c:77
Jpeg2000Cblk::zbp
int zbp
Definition: jpeg2000.h:201
jpeg2000_decode_packets_po_iteration
static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int RSpoc, int CSpoc, int LYEpoc, int REpoc, int CEpoc, int Ppoc, int *tp_index)
Definition: jpeg2000dec.c:1544
MQC_CX_UNI
#define MQC_CX_UNI
Definition: mqc.h:33
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:462
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
Jpeg2000T1Context
Definition: jpeg2000.h:131
jpeg2000_read_bitstream_packets
static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:2613
av_image_check_size2
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:289
jpeg2000_class
static const AVClass jpeg2000_class
Definition: jpeg2000dec.c:2892
JPEG2000_CPF
@ JPEG2000_CPF
Definition: jpeg2000.h:47
read_crg
static int read_crg(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:875
avassert.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
Jpeg2000ResLevel
Definition: jpeg2000.h:225
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_PROPERTY_LOSSLESS
#define FF_CODEC_PROPERTY_LOSSLESS
Definition: avcodec.h:1801
get_siz
static int get_siz(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:197
jpeg2000_dec_cleanup
static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:2371
Jpeg2000CodingStyle::cblk_style
uint8_t cblk_style
Definition: jpeg2000.h:154
Jpeg2000QuantStyle::nguardbits
uint8_t nguardbits
Definition: jpeg2000.h:165
Jpeg2000Tile::comp
Jpeg2000Component * comp
Definition: j2kenc.c:108
Jpeg2000POCEntry::Ppoc
uint8_t Ppoc
Definition: jpeg2000dec.h:40
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
read_cpf
static int read_cpf(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:885
Jpeg2000CodingStyle::transform
uint8_t transform
Definition: jpeg2000.h:150
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
Jpeg2000ResLevel::band
Jpeg2000Band * band
Definition: jpeg2000.h:230
Jpeg2000Tile::packed_headers_stream
GetByteContext packed_headers_stream
Definition: jpeg2000dec.h:68
g
const char * g
Definition: vf_curves.c:128
decode_cblk
static int decode_cblk(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int bandpos, uint8_t roi_shift)
Definition: jpeg2000dec.c:1992
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
get_cox
static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
Definition: jpeg2000dec.c:492
jpeg2000.h
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:1046
roi_shift_param
static int roi_shift_param(Jpeg2000Component *comp, int quan_parameter)
Definition: jpeg2000dec.c:2074
JPEG2000_PGOD_RPCL
#define JPEG2000_PGOD_RPCL
Definition: jpeg2000.h:127
ff_jpeg2000dsp_init
av_cold void ff_jpeg2000dsp_init(Jpeg2000DSPContext *c)
Definition: jpeg2000dsp.c:93
Jpeg2000Cblk::data
uint8_t * data
Definition: jpeg2000.h:192
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
Jpeg2000Band::coord
int coord[2][2]
Definition: jpeg2000.h:218
decode.h
JP2_CODESTREAM
#define JP2_CODESTREAM
Definition: jpeg2000dec.c:51
Jpeg2000Band::f_stepsize
float f_stepsize
Definition: jpeg2000.h:221
get_cap
static int get_cap(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
Definition: jpeg2000dec.c:424
JPEG2000_COM
@ JPEG2000_COM
Definition: jpeg2000.h:55
JPEG2000_PGOD_CPRL
#define JPEG2000_PGOD_CPRL
Definition: jpeg2000.h:129
JPEG2000_QSTY_SI
@ JPEG2000_QSTY_SI
Definition: jpeg2000.h:74
JPEG2000_CRG
@ JPEG2000_CRG
Definition: jpeg2000.h:54
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
gray_pix_fmts
static enum AVPixelFormat gray_pix_fmts[]
Definition: jpeg2000dec.c:186
Jpeg2000Component::reslevel
Jpeg2000ResLevel * reslevel
Definition: jpeg2000.h:234
JPEG2000_CBLK_BYPASS
#define JPEG2000_CBLK_BYPASS
Definition: jpeg2000.h:110
Jpeg2000POC::nb_poc
int nb_poc
Definition: jpeg2000dec.h:45
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
init_tile
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
Definition: jpeg2000dec.c:1016
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:221
JPEG2000_T1_SIG_S
#define JPEG2000_T1_SIG_S
Definition: jpeg2000.h:88
Jpeg2000Cblk::lblock
uint8_t lblock
Definition: jpeg2000.h:191
Jpeg2000Tile::has_ppt
uint8_t has_ppt
Definition: jpeg2000dec.h:65
AV_PIX_FMT_RGBA64
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:468
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
Jpeg2000Cblk::length
uint16_t length
Definition: jpeg2000.h:188
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
jpeg2000_decode_init
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
Definition: jpeg2000dec.c:2780
PTRDIFF_SPECIFIER
#define PTRDIFF_SPECIFIER
Definition: internal.h:128
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
Jpeg2000POC
Definition: jpeg2000dec.h:43
get_ppm
static int get_ppm(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:958
JP2_SIG_TYPE
#define JP2_SIG_TYPE
Definition: jpeg2000dec.c:49
MqcState::bp
uint8_t * bp
Definition: mqc.h:41
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
JPEG2000_PLM
@ JPEG2000_PLM
Definition: jpeg2000.h:45
profiles.h
Jpeg2000Band::prec
Jpeg2000Prec * prec
Definition: jpeg2000.h:222
JPEG2000_T1_VIS
#define JPEG2000_T1_VIS
Definition: jpeg2000.h:103
Jpeg2000ResLevel::num_precincts_y
int num_precincts_y
Definition: jpeg2000.h:228
get_ppt
static int get_ppt(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:982
JPEG2000_EPH
@ JPEG2000_EPH
Definition: jpeg2000.h:58
JPEG2000_PPT
@ JPEG2000_PPT
Definition: jpeg2000.h:53
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
JPEG2000_CBLK_RESET
#define JPEG2000_CBLK_RESET
Definition: jpeg2000.h:111
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
JPEG2000_T1_SIG_NB
#define JPEG2000_T1_SIG_NB
Definition: jpeg2000.h:93
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
Jpeg2000Prec::nb_codeblocks_width
int nb_codeblocks_width
Definition: jpeg2000.h:208
ff_jpeg2000_decode_htj2k
int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int M_b, uint8_t roi_shift)
HT Block decoder as specified in Rec.
Definition: jpeg2000htdec.c:1189
tag_tree_decode
static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
Definition: jpeg2000dec.c:93
JPEG2000_T1_SIG_SE
#define JPEG2000_T1_SIG_SE
Definition: jpeg2000.h:91
Jpeg2000TilePart::tp_end
const uint8_t * tp_end
Definition: jpeg2000dec.h:51
Jpeg2000ResLevel::log2_prec_height
uint8_t log2_prec_height
Definition: jpeg2000.h:229
AVCodecContext::lowres
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition: avcodec.h:1859
ff_jpeg2000_cleanup
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:607
Jpeg2000Cblk::modes
uint8_t modes
Definition: jpeg2000.h:203
ff_jpeg2000_init_component
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, const int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:476
get_qcd
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, const uint8_t *properties)
Definition: jpeg2000dec.c:721
Jpeg2000Component
Definition: jpeg2000.h:233
JPEG2000_T1_SIG_SW
#define JPEG2000_T1_SIG_SW
Definition: jpeg2000.h:92
Jpeg2000Tile::codsty
Jpeg2000CodingStyle codsty[4]
Definition: jpeg2000dec.h:61
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
Jpeg2000Prec::cblkincl
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:211
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
OFFSET
#define OFFSET(x)
Definition: jpeg2000dec.c:2883
Jpeg2000Component::i_data
int * i_data
Definition: jpeg2000.h:237
AVPacket::size
int size
Definition: packet.h:534
JPEG2000_CTSY_HTJ2K_M
#define JPEG2000_CTSY_HTJ2K_M
Definition: jpeg2000.h:122
get_qcc
static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
Definition: jpeg2000dec.c:739
Jpeg2000Tile::tp_idx
uint16_t tp_idx
Definition: jpeg2000dec.h:69
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
byte
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:99
bytestream2_size
static av_always_inline int bytestream2_size(GetByteContext *g)
Definition: bytestream.h:202
height
#define height
Definition: dsp.h:85
codec_internal.h
P
#define P
Jpeg2000POC::poc
Jpeg2000POCEntry poc[MAX_POCS]
Definition: jpeg2000dec.h:44
Jpeg2000ResLevel::nbands
uint8_t nbands
Definition: jpeg2000.h:226
roi_scale_cblk
static void roi_scale_cblk(Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1)
Definition: jpeg2000dec.c:2171
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:464
size
int size
Definition: twinvq_data.h:10344
Jpeg2000Cblk
Definition: jpeg2000.h:183
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
Jpeg2000Component::f_data
float * f_data
Definition: jpeg2000.h:236
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: codec_internal.h:55
xyz_pix_fmts
static enum AVPixelFormat xyz_pix_fmts[]
Definition: jpeg2000dec.c:188
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:461
Jpeg2000Tile::coord
int coord[2][2]
Definition: jpeg2000dec.h:70
jpeg2000_decode_frame
static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition: jpeg2000dec.c:2798
HT_PLHD_OFF
@ HT_PLHD_OFF
Definition: jpeg2000dec.c:59
get_rgn
static int get_rgn(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:643
Jpeg2000TilePart::tile_index
uint8_t tile_index
Definition: jpeg2000dec.h:50
HT_PLHD_STATUS
HT_PLHD_STATUS
Definition: jpeg2000dec.c:58
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:114
JPEG2000_COD
@ JPEG2000_COD
Definition: jpeg2000.h:42
ff_jpeg2000_getsgnctxno
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition: jpeg2000.h:286
attributes.h
all_pix_fmts
static enum AVPixelFormat all_pix_fmts[]
Definition: jpeg2000dec.c:190
Jpeg2000T1Context::stride
int stride
Definition: jpeg2000.h:135
JPEG2000_PGOD_LRCP
#define JPEG2000_PGOD_LRCP
Definition: jpeg2000.h:125
Jpeg2000TgtNode
Definition: jpeg2000.h:138
HAD_QCC
#define HAD_QCC
Definition: jpeg2000dec.c:55
Jpeg2000Cblk::data_start
int * data_start
Definition: jpeg2000.h:196
Jpeg2000CodingStyle::csty
uint8_t csty
Definition: jpeg2000.h:151
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
Jpeg2000CodingStyle::nlayers
uint8_t nlayers
Definition: jpeg2000.h:152
Jpeg2000CodingStyle::nreslevels
int nreslevels
Definition: jpeg2000.h:146
jpeg2000_decode_packet
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index, const Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno, int layno, const uint8_t *expn, int numgbits)
Definition: jpeg2000dec.c:1136
AV_PIX_FMT_XYZ12
#define AV_PIX_FMT_XYZ12
Definition: pixfmt.h:525
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
jpeg2000dec.h
pix_fmt_match
static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components, int bpc, uint32_t log2_chroma_wh, int pal8)
Definition: jpeg2000dec.c:133
JPEG2000_PGOD_RLCP
#define JPEG2000_PGOD_RLCP
Definition: jpeg2000.h:126
AV_PIX_FMT_YA16
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:463
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
get_plt
static int get_plt(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:936
HTJ2K_HTDECLARED
@ HTJ2K_HTDECLARED
Definition: jpeg2000.h:65
Jpeg2000ResLevel::num_precincts_x
int num_precincts_x
Definition: jpeg2000.h:228
JPEG2000_RGN
@ JPEG2000_RGN
Definition: jpeg2000.h:50
jp2_find_codestream
static int jp2_find_codestream(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:2631
JPEG2000_T1_REF
#define JPEG2000_T1_REF
Definition: jpeg2000.h:105
Jpeg2000QuantStyle::expn
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:162
get_coc
static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
Definition: jpeg2000dec.c:608
common.h
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
JP2_SIG_VALUE
#define JP2_SIG_VALUE
Definition: jpeg2000dec.c:50
JPEG2000_CAP
@ JPEG2000_CAP
Definition: jpeg2000.h:40
JPEG2000_SOP_FIXED_BYTES
#define JPEG2000_SOP_FIXED_BYTES
Definition: jpeg2000.h:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
JPEG2000_SIZ
@ JPEG2000_SIZ
Definition: jpeg2000.h:41
Jpeg2000T1Context::data
int data[6144]
Definition: jpeg2000.h:132
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
Jpeg2000TilePart
Definition: jpeg2000dec.h:49
WRITE_FRAME
#define WRITE_FRAME(D, PIXEL)
Definition: jpeg2000dec.c:2275
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ff_jpeg2000_getsigctxno
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition: jpeg2000.h:268
VD
#define VD
Definition: jpeg2000dec.c:2884
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
dequantization_int
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
Definition: jpeg2000dec.c:2109
JPEG2000_MAX_PASSES
#define JPEG2000_MAX_PASSES
Definition: jpeg2000.h:81
GRAY_PIXEL_FORMATS
#define GRAY_PIXEL_FORMATS
Definition: jpeg2000dec.c:171
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
Jpeg2000QuantStyle::mant
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:163
avcodec.h
Jpeg2000Tile::poc
Jpeg2000POC poc
Definition: jpeg2000dec.h:63
JPEG2000_T1_SIG
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:104
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
JPEG2000_PLT
@ JPEG2000_PLT
Definition: jpeg2000.h:46
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
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:71
AV_PROFILE_JPEG2000_DCINEMA_2K
#define AV_PROFILE_JPEG2000_DCINEMA_2K
Definition: defs.h:151
U
#define U(x)
Definition: vpx_arith.h:37
MQC_CX_RL
#define MQC_CX_RL
Definition: mqc.h:34
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:129
Jpeg2000T1Context::flags
uint16_t flags[6156]
Definition: jpeg2000.h:133
Jpeg2000TilePart::tpg
GetByteContext tpg
Definition: jpeg2000dec.h:53
Jpeg2000Component::coord
int coord[2][2]
Definition: jpeg2000.h:238
AVCodecContext
main external API structure.
Definition: avcodec.h:445
FF_DWT53
@ FF_DWT53
Definition: jpeg2000dwt.h:38
JPEG2000_CBLK_VSC
#define JPEG2000_CBLK_VSC
Definition: jpeg2000.h:113
ff_jpeg2000_ceildivpow2
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:244
jpeg2000dsp.h
Jpeg2000ResLevel::log2_prec_width
uint8_t log2_prec_width
Definition: jpeg2000.h:229
jpeg2000_flush
static void jpeg2000_flush(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:85
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
RGB_PIXEL_FORMATS
#define RGB_PIXEL_FORMATS
Definition: jpeg2000dec.c:170
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
ff_jpeg2000_init_tier1_luts
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:172
Jpeg2000POCEntry::LYEpoc
uint16_t LYEpoc
Definition: jpeg2000dec.h:35
JPEG2000_T1_SGN_S
#define JPEG2000_T1_SGN_S
Definition: jpeg2000.h:99
JPEG2000_CSTY_EPH
#define JPEG2000_CSTY_EPH
Definition: jpeg2000.h:120
XYZ_PIXEL_FORMATS
#define XYZ_PIXEL_FORMATS
Definition: jpeg2000dec.c:183
Jpeg2000Cblk::nb_terminations
int nb_terminations
Definition: jpeg2000.h:194
Jpeg2000POCEntry::RSpoc
uint8_t RSpoc
Definition: jpeg2000dec.h:38
Jpeg2000Tile::packed_headers_size
int packed_headers_size
Definition: jpeg2000dec.h:67
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_GRAY16LE
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:105
Jpeg2000Tile::tile_part
Jpeg2000TilePart tile_part[32]
Definition: jpeg2000dec.h:64
mem.h
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
JPEG2000_COC
@ JPEG2000_COC
Definition: jpeg2000.h:43
JPEG2000_PGOD_PCRL
#define JPEG2000_PGOD_PCRL
Definition: jpeg2000.h:128
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:342
JPEG2000_MAX_DECLEVELS
#define JPEG2000_MAX_DECLEVELS
Definition: jpeg2000.h:78
ff_mqc_decode
int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
MQ decoder.
Definition: mqcdec.c:93
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
jpeg2000_decode_packets
static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:1835
mct_decode
static void mct_decode(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:2143
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
MqcState::cx_states
uint8_t cx_states[19]
Definition: mqc.h:45
ff_jpeg2000_decoder
const FFCodec ff_jpeg2000_decoder
Definition: jpeg2000dec.c:2899
get_sot
static int get_sot(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:822
Jpeg2000Cblk::npasses
uint8_t npasses
Definition: jpeg2000.h:184
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
yuv_pix_fmts
static enum AVPixelFormat yuv_pix_fmts[]
Definition: jpeg2000dec.c:187
Jpeg2000CodingStyle::nreslevels2decode
int nreslevels2decode
Definition: jpeg2000.h:147
AVPacket
This structure stores compressed data.
Definition: packet.h:510
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
JPEG2000_QCC
@ JPEG2000_QCC
Definition: jpeg2000.h:49
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
Jpeg2000Component::roi_shift
uint8_t roi_shift
Definition: jpeg2000.h:240
ff_jpeg2000_set_significance
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:178
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
Jpeg2000TgtNode::val
uint8_t val
Definition: jpeg2000.h:139
MqcState::raw
int raw
Definition: mqc.h:46
Jpeg2000TgtNode::vis
uint8_t vis
Definition: jpeg2000.h:141
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MAX_POCS
#define MAX_POCS
Definition: jpeg2000dec.h:32
Jpeg2000CodingStyle
Definition: jpeg2000.h:145
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
SP
static uint64_t SP[8][256]
Definition: camellia.c:44
Jpeg2000Cblk::lengthinc
uint16_t * lengthinc
Definition: jpeg2000.h:189
select_stream
static void select_stream(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile, int *tp_index, const Jpeg2000CodingStyle *codsty)
Definition: jpeg2000dec.c:1111
Jpeg2000QuantStyle
Definition: jpeg2000.h:161
JPEG2000_SOP_BYTE_LENGTH
#define JPEG2000_SOP_BYTE_LENGTH
Definition: jpeg2000.h:70
rgb_pix_fmts
static enum AVPixelFormat rgb_pix_fmts[]
Definition: jpeg2000dec.c:185
Jpeg2000DecoderContext
Definition: jpeg2000dec.h:73
width
#define width
Definition: dsp.h:85
Jpeg2000Cblk::ht_plhd
uint8_t ht_plhd
Definition: jpeg2000.h:204
decode_clnpass
static void decode_clnpass(const Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
Definition: jpeg2000dec.c:1926
Jpeg2000Cblk::nonzerobits
uint8_t nonzerobits
Definition: jpeg2000.h:186
get_qcx
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
Definition: jpeg2000dec.c:676
Jpeg2000Prec::cblk
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:212
JPEG2000_CTSY_HTJ2K_F
#define JPEG2000_CTSY_HTJ2K_F
Definition: jpeg2000.h:121
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
Jpeg2000Tile::qntsty
Jpeg2000QuantStyle qntsty[4]
Definition: jpeg2000dec.h:62
get_tlm
static int get_tlm(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:901
dequantization_float
static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
Definition: jpeg2000dec.c:2094
AVCodecContext::execute2
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:1636
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_mqc_init_contexts
void ff_mqc_init_contexts(MqcState *mqc)
MQ-coder context initialisations.
Definition: mqc.c:64
get_bits
static int get_bits(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:70
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:642
JPEG2000_CBLK_SEGSYM
#define JPEG2000_CBLK_SEGSYM
Definition: jpeg2000.h:115
FF_DWT97_INT
@ FF_DWT97_INT
Definition: jpeg2000dwt.h:39
select_header
static void select_header(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile, int *tp_index)
Definition: jpeg2000dec.c:1099
src
#define src
Definition: vp8dsp.c:248
dequantization_int_97
static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
Definition: jpeg2000dec.c:2129
get_cod
static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, const uint8_t *properties)
Definition: jpeg2000dec.c:571
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155
Jpeg2000Cblk::data_allocated
size_t data_allocated
Definition: jpeg2000.h:193
JPEG2000_TLM
@ JPEG2000_TLM
Definition: jpeg2000.h:44
Jpeg2000TilePart::header_tpg
GetByteContext header_tpg
Definition: jpeg2000dec.h:52
HT_PLHD_ON
@ HT_PLHD_ON
Definition: jpeg2000dec.c:60