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 #if FF_API_CODEC_PROPS
546  else if (c->transform == FF_DWT53) {
548  s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
550  }
551 #endif
552 
553  if (c->csty & JPEG2000_CSTY_PREC) {
554  int i;
555  for (i = 0; i < c->nreslevels; i++) {
556  byte = bytestream2_get_byte(&s->g);
557  c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
558  c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
559  if (i)
560  if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
561  av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
562  c->log2_prec_widths[i], c->log2_prec_heights[i]);
563  c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
564  return AVERROR_INVALIDDATA;
565  }
566  }
567  } else {
568  memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
569  memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
570  }
571  return 0;
572 }
573 
574 /* get coding parameters for a particular tile or whole image*/
576  const uint8_t *properties)
577 {
579  int compno, ret;
580 
581  if (bytestream2_get_bytes_left(&s->g) < 5) {
582  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
583  return AVERROR_INVALIDDATA;
584  }
585 
586  tmp.csty = bytestream2_get_byteu(&s->g);
587 
588  // get progression order
589  tmp.prog_order = bytestream2_get_byteu(&s->g);
590 
591  tmp.nlayers = bytestream2_get_be16u(&s->g);
592  tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
593 
594  if (tmp.mct && s->ncomponents < 3) {
595  av_log(s->avctx, AV_LOG_ERROR,
596  "MCT %"PRIu8" with too few components (%d)\n",
597  tmp.mct, s->ncomponents);
598  return AVERROR_INVALIDDATA;
599  }
600 
601  if ((ret = get_cox(s, &tmp)) < 0)
602  return ret;
603  tmp.init = 1;
604  for (compno = 0; compno < s->ncomponents; compno++)
605  if (!(properties[compno] & HAD_COC))
606  memcpy(c + compno, &tmp, sizeof(tmp));
607  return 0;
608 }
609 
610 /* Get coding parameters for a component in the whole image or a
611  * particular tile. */
613  uint8_t *properties)
614 {
615  int compno, ret;
616  uint8_t has_eph, has_sop;
617 
618  if (bytestream2_get_bytes_left(&s->g) < 2) {
619  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
620  return AVERROR_INVALIDDATA;
621  }
622 
623  compno = bytestream2_get_byteu(&s->g);
624 
625  if (compno >= s->ncomponents) {
626  av_log(s->avctx, AV_LOG_ERROR,
627  "Invalid compno %d. There are %d components in the image.\n",
628  compno, s->ncomponents);
629  return AVERROR_INVALIDDATA;
630  }
631 
632  c += compno;
633  has_eph = c->csty & JPEG2000_CSTY_EPH;
634  has_sop = c->csty & JPEG2000_CSTY_SOP;
635  c->csty = bytestream2_get_byteu(&s->g);
636  c->csty |= has_eph; //do not override eph present bits from COD
637  c->csty |= has_sop; //do not override sop present bits from COD
638 
639  if ((ret = get_cox(s, c)) < 0)
640  return ret;
641 
642  properties[compno] |= HAD_COC;
643  c->init = 1;
644  return 0;
645 }
646 
647 static int get_rgn(Jpeg2000DecoderContext *s, int n)
648 {
649  uint16_t compno;
650  compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
651  bytestream2_get_be16u(&s->g);
652  if (bytestream2_get_byte(&s->g)) {
653  av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
654  return AVERROR_INVALIDDATA; // SRgn field value is 0
655  }
656  // SPrgn field
657  // Currently compno cannot be greater than 4.
658  // However, future implementation should support compno up to 65536
659  if (compno < s->ncomponents) {
660  int v;
661  if (s->curtileno == -1) {
662  v = bytestream2_get_byte(&s->g);
663  if (v > 30)
664  return AVERROR_PATCHWELCOME;
665  s->roi_shift[compno] = v;
666  } else {
667  if (s->tile[s->curtileno].tp_idx != 0)
668  return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
669  v = bytestream2_get_byte(&s->g);
670  if (v > 30)
671  return AVERROR_PATCHWELCOME;
672  s->tile[s->curtileno].comp[compno].roi_shift = v;
673  }
674  return 0;
675  }
676  return AVERROR_INVALIDDATA;
677 }
678 
679 /* Get common part for QCD and QCC segments. */
681 {
682  int i, x;
683 
684  if (bytestream2_get_bytes_left(&s->g) < 1)
685  return AVERROR_INVALIDDATA;
686 
687  x = bytestream2_get_byteu(&s->g); // Sqcd
688 
689  q->nguardbits = x >> 5;
690  q->quantsty = x & 0x1f;
691 
692  if (q->quantsty == JPEG2000_QSTY_NONE) {
693  n -= 3;
694  if (bytestream2_get_bytes_left(&s->g) < n ||
696  return AVERROR_INVALIDDATA;
697  for (i = 0; i < n; i++)
698  q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
699  } else if (q->quantsty == JPEG2000_QSTY_SI) {
700  if (bytestream2_get_bytes_left(&s->g) < 2)
701  return AVERROR_INVALIDDATA;
702  x = bytestream2_get_be16u(&s->g);
703  q->expn[0] = x >> 11;
704  q->mant[0] = x & 0x7ff;
705  for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
706  int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
707  q->expn[i] = curexpn;
708  q->mant[i] = q->mant[0];
709  }
710  } else {
711  n = (n - 3) >> 1;
712  if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
714  return AVERROR_INVALIDDATA;
715  for (i = 0; i < n; i++) {
716  x = bytestream2_get_be16u(&s->g);
717  q->expn[i] = x >> 11;
718  q->mant[i] = x & 0x7ff;
719  }
720  }
721  return 0;
722 }
723 
724 /* Get quantization parameters for a particular tile or a whole image. */
726  const uint8_t *properties)
727 {
729  int compno, ret;
730 
731  memset(&tmp, 0, sizeof(tmp));
732 
733  if ((ret = get_qcx(s, n, &tmp)) < 0)
734  return ret;
735  for (compno = 0; compno < s->ncomponents; compno++)
736  if (!(properties[compno] & HAD_QCC))
737  memcpy(q + compno, &tmp, sizeof(tmp));
738  return 0;
739 }
740 
741 /* Get quantization parameters for a component in the whole image
742  * on in a particular tile. */
744  uint8_t *properties)
745 {
746  int compno;
747 
748  if (bytestream2_get_bytes_left(&s->g) < 1)
749  return AVERROR_INVALIDDATA;
750 
751  compno = bytestream2_get_byteu(&s->g);
752 
753  if (compno >= s->ncomponents) {
754  av_log(s->avctx, AV_LOG_ERROR,
755  "Invalid compno %d. There are %d components in the image.\n",
756  compno, s->ncomponents);
757  return AVERROR_INVALIDDATA;
758  }
759 
760  properties[compno] |= HAD_QCC;
761  return get_qcx(s, n - 1, q + compno);
762 }
763 
765 {
766  int i;
767  int elem_size = s->ncomponents <= 257 ? 7 : 9;
768  Jpeg2000POC tmp = {{{0}}};
769 
770  if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
771  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
772  return AVERROR_INVALIDDATA;
773  }
774 
775  if (elem_size > 7) {
776  avpriv_request_sample(s->avctx, "Fat POC not supported");
777  return AVERROR_PATCHWELCOME;
778  }
779 
780  tmp.nb_poc = (size - 2) / elem_size;
781  if (tmp.nb_poc > MAX_POCS) {
782  avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
783  return AVERROR_PATCHWELCOME;
784  }
785 
786  for (i = 0; i<tmp.nb_poc; i++) {
787  Jpeg2000POCEntry *e = &tmp.poc[i];
788  e->RSpoc = bytestream2_get_byteu(&s->g);
789  e->CSpoc = bytestream2_get_byteu(&s->g);
790  e->LYEpoc = bytestream2_get_be16u(&s->g);
791  e->REpoc = bytestream2_get_byteu(&s->g);
792  e->CEpoc = bytestream2_get_byteu(&s->g);
793  e->Ppoc = bytestream2_get_byteu(&s->g);
794  if (!e->CEpoc)
795  e->CEpoc = 256;
796  if (e->CEpoc > s->ncomponents)
797  e->CEpoc = s->ncomponents;
798  if ( e->RSpoc >= e->REpoc || e->REpoc > 33
799  || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
800  || !e->LYEpoc) {
801  av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
802  e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
803  );
804  return AVERROR_INVALIDDATA;
805  }
806  }
807 
808  if (!p->nb_poc || p->is_default) {
809  *p = tmp;
810  } else {
811  if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
812  av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
813  return AVERROR_INVALIDDATA;
814  }
815  memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
816  p->nb_poc += tmp.nb_poc;
817  }
818 
819  p->is_default = 0;
820 
821  return 0;
822 }
823 
824 
825 /* Get start of tile segment. */
826 static int get_sot(Jpeg2000DecoderContext *s, int n)
827 {
828  Jpeg2000TilePart *tp;
829  uint16_t Isot;
830  uint32_t Psot;
831  unsigned TPsot;
832 
833  if (bytestream2_get_bytes_left(&s->g) < 8)
834  return AVERROR_INVALIDDATA;
835 
836  s->curtileno = 0;
837  Isot = bytestream2_get_be16u(&s->g); // Isot
838  if (Isot >= s->numXtiles * s->numYtiles)
839  return AVERROR_INVALIDDATA;
840 
841  s->curtileno = Isot;
842  Psot = bytestream2_get_be32u(&s->g); // Psot
843  TPsot = bytestream2_get_byteu(&s->g); // TPsot
844 
845  /* Read TNSot but not used */
846  bytestream2_get_byteu(&s->g); // TNsot
847 
848  if (!Psot)
849  Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
850 
851  if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
852  av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
853  return AVERROR_INVALIDDATA;
854  }
855 
856  if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
857  avpriv_request_sample(s->avctx, "Too many tile parts");
858  return AVERROR_PATCHWELCOME;
859  }
860 
861  s->tile[Isot].tp_idx = TPsot;
862  tp = s->tile[Isot].tile_part + TPsot;
863  tp->tile_index = Isot;
864  tp->tp_end = s->g.buffer + Psot - n - 2;
865 
866  if (!TPsot) {
867  Jpeg2000Tile *tile = s->tile + s->curtileno;
868 
869  /* copy defaults */
870  memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
871  memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
872  memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
873  tile->poc.is_default = 1;
874  }
875 
876  return 0;
877 }
878 
879 static int read_crg(Jpeg2000DecoderContext *s, int n)
880 {
881  if (s->ncomponents*4 != n - 2) {
882  av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
883  return AVERROR_INVALIDDATA;
884  }
885  bytestream2_skip(&s->g, n - 2);
886  return 0;
887 }
888 
889 static int read_cpf(Jpeg2000DecoderContext *s, int n)
890 {
891  if (bytestream2_get_bytes_left(&s->g) < (n - 2))
892  return AVERROR_INVALIDDATA;
893  bytestream2_skip(&s->g, n - 2);
894  return 0;
895 }
896 
897 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
898  * Used to know the number of tile parts and lengths.
899  * There may be multiple TLMs in the header.
900  * TODO: The function is not used for tile-parts management, nor anywhere else.
901  * It can be useful to allocate memory for tile parts, before managing the SOT
902  * markers. Parsing the TLM header is needed to increment the input header
903  * buffer.
904  * This marker is mandatory for DCI. */
905 static int get_tlm(Jpeg2000DecoderContext *s, int n)
906 {
907  uint8_t Stlm, ST, SP, tile_tlm, i;
908  bytestream2_get_byte(&s->g); /* Ztlm: skipped */
909  Stlm = bytestream2_get_byte(&s->g);
910 
911  // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
912  ST = (Stlm >> 4) & 0x03;
913  if (ST == 0x03) {
914  av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
915  return AVERROR_INVALIDDATA;
916  }
917 
918  SP = (Stlm >> 6) & 0x01;
919  tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
920  for (i = 0; i < tile_tlm; i++) {
921  switch (ST) {
922  case 0:
923  break;
924  case 1:
925  bytestream2_get_byte(&s->g);
926  break;
927  case 2:
928  bytestream2_get_be16(&s->g);
929  break;
930  }
931  if (SP == 0) {
932  bytestream2_get_be16(&s->g);
933  } else {
934  bytestream2_get_be32(&s->g);
935  }
936  }
937  return 0;
938 }
939 
940 static int get_plt(Jpeg2000DecoderContext *s, int n)
941 {
942  int i;
943  int v;
944 
945  av_log(s->avctx, AV_LOG_DEBUG,
946  "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
947 
948  if (n < 4)
949  return AVERROR_INVALIDDATA;
950 
951  /*Zplt =*/ bytestream2_get_byte(&s->g);
952 
953  for (i = 0; i < n - 3; i++) {
954  v = bytestream2_get_byte(&s->g);
955  }
956  if (v & 0x80)
957  return AVERROR_INVALIDDATA;
958 
959  return 0;
960 }
961 
962 static int get_ppm(Jpeg2000DecoderContext *s, int n)
963 {
964  void *new;
965 
966  if (n < 3) {
967  av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
968  return AVERROR_INVALIDDATA;
969  }
970  bytestream2_get_byte(&s->g); //Zppm is skipped and not used
971  new = av_realloc(s->packed_headers,
972  s->packed_headers_size + n - 3);
973  if (new) {
974  s->packed_headers = new;
975  } else
976  return AVERROR(ENOMEM);
977  s->has_ppm = 1;
978  memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
979  bytestream2_get_bufferu(&s->g, s->packed_headers + s->packed_headers_size,
980  n - 3);
981  s->packed_headers_size += n - 3;
982 
983  return 0;
984 }
985 
986 static int get_ppt(Jpeg2000DecoderContext *s, int n)
987 {
988  Jpeg2000Tile *tile;
989  void *new;
990 
991  if (n < 3) {
992  av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
993  return AVERROR_INVALIDDATA;
994  }
995  if (s->curtileno < 0)
996  return AVERROR_INVALIDDATA;
997 
998  tile = &s->tile[s->curtileno];
999  if (tile->tp_idx != 0) {
1000  av_log(s->avctx, AV_LOG_ERROR,
1001  "PPT marker can occur only on first tile part of a tile.\n");
1002  return AVERROR_INVALIDDATA;
1003  }
1004 
1005  tile->has_ppt = 1; // this tile has a ppt marker
1006  bytestream2_get_byte(&s->g); // Zppt is skipped and not used
1007  new = av_realloc(tile->packed_headers,
1008  tile->packed_headers_size + n - 3);
1009  if (new) {
1010  tile->packed_headers = new;
1011  } else
1012  return AVERROR(ENOMEM);
1013  memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
1014  bytestream2_get_bufferu(&s->g, tile->packed_headers + tile->packed_headers_size, n - 3);
1015  tile->packed_headers_size += n - 3;
1016 
1017  return 0;
1018 }
1019 
1020 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
1021 {
1022  int compno;
1023  int tilex = tileno % s->numXtiles;
1024  int tiley = tileno / s->numXtiles;
1025  Jpeg2000Tile *tile = s->tile + tileno;
1026 
1027  if (!tile->comp)
1028  return AVERROR(ENOMEM);
1029 
1030  tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
1031  tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
1032  tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
1033  tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
1034 
1035  for (compno = 0; compno < s->ncomponents; compno++) {
1036  Jpeg2000Component *comp = tile->comp + compno;
1037  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1038  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1039  int ret; // global bandno
1040 
1041  comp->coord_o[0][0] = tile->coord[0][0];
1042  comp->coord_o[0][1] = tile->coord[0][1];
1043  comp->coord_o[1][0] = tile->coord[1][0];
1044  comp->coord_o[1][1] = tile->coord[1][1];
1045 
1046  comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
1047  comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
1048  comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
1049  comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
1050 
1051  comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
1052  comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
1053  comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
1054  comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
1055 
1056  if (!comp->roi_shift)
1057  comp->roi_shift = s->roi_shift[compno];
1058  if (!codsty->init)
1059  return AVERROR_INVALIDDATA;
1060  if (s->isHT && (!s->Ccap15_b05) && (!codsty->transform)) {
1061  av_log(s->avctx, AV_LOG_ERROR, "Transformation = 0 (lossy DWT) is found in HTREV HT set\n");
1062  return AVERROR_INVALIDDATA;
1063  }
1064  if (s->isHT && s->Ccap15_b14_15 != (codsty->cblk_style >> 6) && s->Ccap15_b14_15 != HTJ2K_HTONLY) {
1065  av_log(s->avctx, AV_LOG_ERROR, "SPcod/SPcoc value does not match bit 14-15 values of Ccap15\n");
1066  return AVERROR_INVALIDDATA;
1067  }
1068  if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
1069  s->cbps[compno], s->cdx[compno],
1070  s->cdy[compno], s->avctx))
1071  return ret;
1072  }
1073  return 0;
1074 }
1075 
1076 /* Read the number of coding passes. */
1078 {
1079  int num;
1080  if (!get_bits(s, 1))
1081  return 1;
1082  if (!get_bits(s, 1))
1083  return 2;
1084  if ((num = get_bits(s, 2)) != 3)
1085  return num < 0 ? num : 3 + num;
1086  if ((num = get_bits(s, 5)) != 31)
1087  return num < 0 ? num : 6 + num;
1088  num = get_bits(s, 7);
1089  return num < 0 ? num : 37 + num;
1090 }
1091 
1093 {
1094  int res = 0, ret;
1095  while (ret = get_bits(s, 1)) {
1096  if (ret < 0)
1097  return ret;
1098  res++;
1099  }
1100  return res;
1101 }
1102 
1103 static inline void select_header(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile,
1104  int *tp_index)
1105 {
1106  s->g = tile->tile_part[*tp_index].header_tpg;
1107  if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1108  av_log(s->avctx, AV_LOG_WARNING, "Packet header bytes in PPM marker segment is too short.\n");
1109  if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1110  s->g = tile->tile_part[++(*tp_index)].tpg;
1111  }
1112  }
1113 }
1114 
1115 static inline void select_stream(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile,
1116  int *tp_index, const Jpeg2000CodingStyle *codsty)
1117 {
1118  int32_t is_endof_tp;
1119 
1120  s->g = tile->tile_part[*tp_index].tpg;
1121  is_endof_tp = bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8;
1122  // Following while loop is necessary because a tilepart may include only SOD marker.
1123  // Such a tilepart has neither packet header nor compressed data.
1124  while (is_endof_tp) {
1125  if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1126  s->g = tile->tile_part[++(*tp_index)].tpg;
1127  is_endof_tp = bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8;
1128  } else {
1129  is_endof_tp = 0;
1130  }
1131  }
1132  if (codsty->csty & JPEG2000_CSTY_SOP) {
1133  if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1135  else
1136  av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1137  }
1138 }
1139 
1141  const Jpeg2000CodingStyle *codsty,
1142  Jpeg2000ResLevel *rlevel, int precno,
1143  int layno, const uint8_t *expn, int numgbits)
1144 {
1145  int bandno, cblkno, ret, nb_code_blocks;
1146  int cwsno;
1147 
1148  if (layno < rlevel->band[0].prec[precno].decoded_layers)
1149  return 0;
1150  rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1151  // Select stream to read from
1152  if (s->has_ppm)
1153  select_header(s, tile, tp_index);
1154  else if (tile->has_ppt)
1155  s->g = tile->packed_headers_stream;
1156  else
1157  select_stream(s, tile, tp_index, codsty);
1158 
1159  if (!(ret = get_bits(s, 1))) {
1160  jpeg2000_flush(s);
1161  goto skip_data;
1162  } else if (ret < 0)
1163  return ret;
1164 
1165  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1166  Jpeg2000Band *band = rlevel->band + bandno;
1167  Jpeg2000Prec *prec = band->prec + precno;
1168 
1169  if (band->coord[0][0] == band->coord[0][1] ||
1170  band->coord[1][0] == band->coord[1][1])
1171  continue;
1172  nb_code_blocks = prec->nb_codeblocks_height *
1173  prec->nb_codeblocks_width;
1174  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1175  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1176  int incl, newpasses, llen;
1177  void *tmp;
1178 
1179  if (!cblk->incl) {
1180  incl = 0;
1181  cblk->modes = codsty->cblk_style;
1182  if (cblk->modes >= JPEG2000_CTSY_HTJ2K_F)
1183  cblk->ht_plhd = HT_PLHD_ON;
1184  if (layno > 0)
1185  incl = tag_tree_decode(s, prec->cblkincl + cblkno, 0 + 1) == 0;
1186  incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1187 
1188  if (incl) {
1189  int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100);
1190  int v = expn[bandno] + numgbits - 1 - (zbp - tile->comp->roi_shift);
1191  if (v < 0 || v > 30) {
1192  av_log(s->avctx, AV_LOG_ERROR,
1193  "nonzerobits %d invalid or unsupported\n", v);
1194  return AVERROR_INVALIDDATA;
1195  }
1196  cblk->incl = 1;
1197  cblk->nonzerobits = v;
1198  cblk->zbp = zbp;
1199  cblk->lblock = 3;
1200  }
1201  } else {
1202  incl = get_bits(s, 1);
1203  }
1204 
1205  if (incl) {
1206  uint8_t bypass_term_threshold = 0;
1207  uint8_t bits_to_read = 0;
1208  uint32_t segment_bytes = 0;
1209  int32_t segment_passes = 0;
1210  uint8_t next_segment_passes = 0;
1211  int32_t href_passes, pass_bound;
1212  uint32_t tmp_length = 0;
1213  int32_t newpasses_copy, npasses_copy;
1214 
1215  if ((newpasses = getnpasses(s)) <= 0)
1216  return newpasses;
1217  if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1218  avpriv_request_sample(s->avctx, "Too many passes");
1219  return AVERROR_PATCHWELCOME;
1220  }
1221  if ((llen = getlblockinc(s)) < 0)
1222  return llen;
1223  if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1224  avpriv_request_sample(s->avctx,
1225  "Block with length beyond 16 bits");
1226  return AVERROR_PATCHWELCOME;
1227  }
1228  cblk->nb_lengthinc = 0;
1229  cblk->nb_terminationsinc = 0;
1230  av_free(cblk->lengthinc);
1231  cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc));
1232  if (!cblk->lengthinc)
1233  return AVERROR(ENOMEM);
1234  tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1,
1235  sizeof(*cblk->data_start));
1236  if (!tmp)
1237  return AVERROR(ENOMEM);
1238  cblk->data_start = tmp;
1239  cblk->lblock += llen;
1240 
1241  // Count number of necessary terminations for non HT code block
1242  newpasses_copy = newpasses;
1243  npasses_copy = cblk->npasses;
1244  if (!(cblk->modes & JPEG2000_CTSY_HTJ2K_F)) {
1245  do {
1246  int newpasses1 = 0;
1247 
1248  while (newpasses1 < newpasses_copy) {
1249  newpasses1++;
1250  if (needs_termination(codsty->cblk_style, npasses_copy + newpasses1 - 1)) {
1251  cblk->nb_terminationsinc++;
1252  break;
1253  }
1254  }
1255  npasses_copy += newpasses1;
1256  newpasses_copy -= newpasses1;
1257  } while (newpasses_copy);
1258  }
1259 
1260  if (cblk->ht_plhd) {
1261  href_passes = (cblk->npasses + newpasses - 1) % 3;
1262  segment_passes = newpasses - href_passes;
1263  pass_bound = 2;
1264  bits_to_read = cblk->lblock;
1265  if (segment_passes < 1) {
1266  // No possible HT Cleanup pass here; may have placeholder passes
1267  // or an original J2K block bit-stream (in MIXED mode).
1268  segment_passes = newpasses;
1269  while (pass_bound <= segment_passes) {
1270  bits_to_read++;
1271  pass_bound += pass_bound;
1272  }
1273  segment_bytes = get_bits(s, bits_to_read);
1274  if (segment_bytes) {
1275  if (cblk->modes & HT_MIXED) {
1276  cblk->ht_plhd = HT_PLHD_OFF;
1277  cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1278  }
1279  else {
1280  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1281  }
1282  }
1283  } else {
1284  while (pass_bound <= segment_passes) {
1285  bits_to_read++;
1286  pass_bound += pass_bound;
1287  }
1288  segment_bytes = get_bits(s, bits_to_read);
1289  if (segment_bytes) {
1290  // No more placeholder passes
1291  if (!(cblk->modes & HT_MIXED)) {
1292  // Must be the first HT Cleanup pass
1293  if (segment_bytes < 2)
1294  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1295  next_segment_passes = 2;
1296  cblk->ht_plhd = HT_PLHD_OFF;
1297  // Write length information for HT CleanUp segment
1298  cblk->pass_lengths[0] = segment_bytes;
1299  } else if (cblk->lblock > 3 && segment_bytes > 1
1300  && (segment_bytes >> (bits_to_read - 1)) == 0) {
1301  // Must be the first HT Cleanup pass, since length MSB is 0
1302  next_segment_passes = 2;
1303  cblk->ht_plhd = HT_PLHD_OFF;
1304  // Write length information for HT CleanUp segment
1305  cblk->pass_lengths[0] = segment_bytes;
1306  } else {
1307  // Must have an original (non-HT) block coding pass
1308  cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1309  cblk->ht_plhd = HT_PLHD_OFF;
1310  segment_passes = newpasses;
1311  while (pass_bound <= segment_passes) {
1312  bits_to_read++;
1313  pass_bound += pass_bound;
1314  segment_bytes <<= 1;
1315  segment_bytes += get_bits(s, 1);
1316  }
1317  }
1318  } else {
1319  // Probably parsing placeholder passes, but we need to read an
1320  // extra length bit to verify this, since prior to the first
1321  // HT Cleanup pass, the number of length bits read for a
1322  // contributing code-block is dependent on the number of passes
1323  // being included, as if it were a non-HT code-block.
1324  segment_passes = newpasses;
1325  if (pass_bound <= segment_passes) {
1326  while (1) {
1327  bits_to_read++;
1328  pass_bound += pass_bound;
1329  segment_bytes <<= 1;
1330  segment_bytes += get_bits(s, 1);
1331  if (pass_bound > segment_passes)
1332  break;
1333  }
1334  if (segment_bytes) {
1335  if (cblk->modes & HT_MIXED) {
1336  cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1337  cblk->ht_plhd = HT_PLHD_OFF;
1338  } else {
1339  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1340  }
1341  }
1342  }
1343  }
1344  }
1345  } else if (cblk->modes & JPEG2000_CTSY_HTJ2K_F) {
1346  // Quality layer commences with a non-initial HT coding pass
1347  if(bits_to_read != 0)
1348  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1349  segment_passes = cblk->npasses % 3;
1350  if (segment_passes == 0) {
1351  // newpasses is a HT Cleanup pass; next segment has refinement passes
1352  segment_passes = 1;
1353  next_segment_passes = 2;
1354  if (segment_bytes == 1)
1355  av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1356  } else {
1357  // newpasses = 1 means npasses is HT SigProp; 2 means newpasses is
1358  // HT MagRef pass
1359  segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
1360  next_segment_passes = 1;
1361  bits_to_read = av_log2(segment_passes);
1362  }
1363  bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1364  segment_bytes = get_bits(s, bits_to_read);
1365  // Write length information for HT Refinment segment
1366  cblk->pass_lengths[1] += segment_bytes;
1367  } else if (!(cblk->modes & (JPEG2000_CBLK_TERMALL | JPEG2000_CBLK_BYPASS))) {
1368  // Common case for non-HT code-blocks; we have only one segment
1369  bits_to_read = (uint8_t) cblk->lblock + av_log2((uint8_t) newpasses);
1370  segment_bytes = get_bits(s, bits_to_read);
1371  segment_passes = newpasses;
1372  } else if (cblk->modes & JPEG2000_CBLK_TERMALL) {
1373  // RESTART MODE
1374  bits_to_read = cblk->lblock;
1375  segment_bytes = get_bits(s, bits_to_read);
1376  segment_passes = 1;
1377  next_segment_passes = 1;
1378  } else {
1379  // BYPASS MODE
1380  bypass_term_threshold = 10;
1381  if(bits_to_read != 0)
1382  av_log(s->avctx, AV_LOG_WARNING, "Length information for a codeblock is invalid\n");
1383  if (cblk->npasses < bypass_term_threshold) {
1384  // May have from 1 to 10 uninterrupted passes before 1st RAW SigProp
1385  segment_passes = bypass_term_threshold - cblk->npasses;
1386  if (segment_passes > newpasses)
1387  segment_passes = newpasses;
1388  while ((2 << bits_to_read) <= segment_passes)
1389  bits_to_read++;
1390  next_segment_passes = 2;
1391  } else if ((cblk->npasses - bypass_term_threshold) % 3 < 2) {
1392  // 0 means newpasses is a RAW SigProp; 1 means newpasses is a RAW MagRef pass
1393  segment_passes = newpasses > 1 ? 2 - (cblk->npasses - bypass_term_threshold) % 3 : 1;
1394  bits_to_read = av_log2(segment_passes);
1395  next_segment_passes = 1;
1396  } else {
1397  // newpasses is an isolated Cleanup pass that precedes a RAW SigProp pass
1398  segment_passes = 1;
1399  next_segment_passes = 2;
1400  }
1401  bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1402  segment_bytes = get_bits(s, bits_to_read);
1403  }
1404  // Update cblk->npasses and write length information
1405  cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1406  cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1407 
1408  if ((cblk->modes & JPEG2000_CTSY_HTJ2K_F) && cblk->ht_plhd == HT_PLHD_OFF) {
1409  newpasses -= (uint8_t) segment_passes;
1410  while (newpasses > 0) {
1411  segment_passes = newpasses > 1 ? next_segment_passes : 1;
1412  next_segment_passes = (uint8_t) (3 - next_segment_passes);
1413  bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1414  segment_bytes = get_bits(s, bits_to_read);
1415  newpasses -= (uint8_t) (segment_passes);
1416  // This is a FAST Refinement pass
1417  // Write length information for HT Refinement segment
1418  cblk->pass_lengths[1] += segment_bytes;
1419  // Update cblk->npasses and write length information
1420  cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1421  cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1422  }
1423  } else {
1424  newpasses -= (uint8_t) (segment_passes);
1425  while (newpasses > 0) {
1426  if (bypass_term_threshold != 0) {
1427  segment_passes = newpasses > 1 ? next_segment_passes : 1;
1428  next_segment_passes = (uint8_t) (3 - next_segment_passes);
1429  bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1430  } else {
1431  if ((cblk->modes & JPEG2000_CBLK_TERMALL) == 0)
1432  av_log(s->avctx, AV_LOG_WARNING, "Corrupted packet header is found.\n");
1433  segment_passes = 1;
1434  bits_to_read = cblk->lblock;
1435  }
1436  segment_bytes = get_bits(s, bits_to_read);
1437  newpasses -= (uint8_t) (segment_passes);
1438 
1439  // Update cblk->npasses and write length information
1440  cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1441  cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1442  }
1443  }
1444 
1445  for (int i = 0; i < cblk->nb_lengthinc; ++i)
1446  tmp_length = (tmp_length < cblk->lengthinc[i]) ? cblk->lengthinc[i] : tmp_length;
1447 
1448  if (tmp_length > cblk->data_allocated) {
1449  size_t new_size = FFMAX(2 * cblk->data_allocated, tmp_length);
1450  void *new = av_realloc(cblk->data, new_size);
1451  if (new) {
1452  cblk->data = new;
1453  cblk->data_allocated = new_size;
1454  }
1455  }
1456  if (tmp_length > cblk->data_allocated) {
1457  avpriv_request_sample(s->avctx,
1458  "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1459  cblk->data_allocated);
1460  return AVERROR_PATCHWELCOME;
1461  }
1462  } else {
1463  // This codeblock has no contribution to the current packet
1464  continue;
1465  }
1466  }
1467  }
1468  jpeg2000_flush(s);
1469 
1470  if (codsty->csty & JPEG2000_CSTY_EPH) {
1471  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1472  bytestream2_skip(&s->g, 2);
1473  else
1474  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1475  }
1476 
1477  // Save state of stream
1478  if (s->has_ppm) {
1479  tile->tile_part[*tp_index].header_tpg = s->g;
1480  select_stream(s, tile, tp_index, codsty);
1481  } else if (tile->has_ppt) {
1482  tile->packed_headers_stream = s->g;
1483  select_stream(s, tile, tp_index, codsty);
1484  }
1485  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1486  Jpeg2000Band *band = rlevel->band + bandno;
1487  Jpeg2000Prec *prec = band->prec + precno;
1488 
1489  nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1490  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1491  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1492  if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1493  continue;
1494  for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1495  if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1496  size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1497  void *new = av_realloc(cblk->data, new_size);
1498  if (new) {
1499  cblk->data = new;
1500  cblk->data_allocated = new_size;
1501  }
1502  }
1503  if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1504  || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1505  ) {
1506  av_log(s->avctx, AV_LOG_ERROR,
1507  "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1508  cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1509  return AVERROR_INVALIDDATA;
1510  }
1511 
1512  bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1513  cblk->length += cblk->lengthinc[cwsno];
1514  cblk->lengthinc[cwsno] = 0;
1515  if (cblk->nb_terminationsinc) {
1516  cblk->nb_terminationsinc--;
1517  cblk->nb_terminations++;
1518  cblk->data[cblk->length++] = 0xFF;
1519  cblk->data[cblk->length++] = 0xFF;
1520  cblk->data_start[cblk->nb_terminations] = cblk->length;
1521  }
1522  }
1523  av_freep(&cblk->lengthinc);
1524  cblk->nb_lengthinc = 0;
1525  }
1526  }
1527  // Save state of stream
1528  tile->tile_part[*tp_index].tpg = s->g;
1529  return 0;
1530 
1531 skip_data:
1532  if (codsty->csty & JPEG2000_CSTY_EPH) {
1533  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1534  bytestream2_skip(&s->g, 2);
1535  else
1536  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1537  }
1538  if (s->has_ppm) {
1539  tile->tile_part[*tp_index].header_tpg = s->g;
1540  select_stream(s, tile, tp_index, codsty);
1541  } else if (tile->has_ppt) {
1542  tile->packed_headers_stream = s->g;
1543  select_stream(s, tile, tp_index, codsty);
1544  }
1545  tile->tile_part[*tp_index].tpg = s->g;
1546  return 0;
1547 }
1548 
1550  int RSpoc, int CSpoc,
1551  int LYEpoc, int REpoc, int CEpoc,
1552  int Ppoc, int *tp_index)
1553 {
1554  int ret = 0;
1555  int layno, reslevelno, compno, precno, ok_reslevel;
1556  int x, y;
1557  int step_x, step_y;
1558 
1559  switch (Ppoc) {
1560  case JPEG2000_PGOD_RLCP:
1561  av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1562  ok_reslevel = 1;
1563  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1564  ok_reslevel = 0;
1565  for (layno = 0; layno < LYEpoc; layno++) {
1566  for (compno = CSpoc; compno < CEpoc; compno++) {
1567  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1568  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1569  if (reslevelno < codsty->nreslevels) {
1570  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1571  reslevelno;
1572  ok_reslevel = 1;
1573  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1574  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1575  codsty, rlevel,
1576  precno, layno,
1577  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1578  qntsty->nguardbits)) < 0)
1579  return ret;
1580  }
1581  }
1582  }
1583  }
1584  break;
1585 
1586  case JPEG2000_PGOD_LRCP:
1587  av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1588  for (layno = 0; layno < LYEpoc; layno++) {
1589  ok_reslevel = 1;
1590  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1591  ok_reslevel = 0;
1592  for (compno = CSpoc; compno < CEpoc; compno++) {
1593  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1594  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1595  if (reslevelno < codsty->nreslevels) {
1596  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1597  reslevelno;
1598  ok_reslevel = 1;
1599  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1600  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1601  codsty, rlevel,
1602  precno, layno,
1603  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1604  qntsty->nguardbits)) < 0)
1605  return ret;
1606  }
1607  }
1608  }
1609  }
1610  break;
1611 
1612  case JPEG2000_PGOD_CPRL:
1613  av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1614  for (compno = CSpoc; compno < CEpoc; compno++) {
1615  Jpeg2000Component *comp = tile->comp + compno;
1616  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1617  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1618  step_x = 32;
1619  step_y = 32;
1620 
1621  if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1622  continue;
1623 
1624  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1625  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1626  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1627  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1628  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1629  }
1630  if (step_x >= 31 || step_y >= 31){
1631  avpriv_request_sample(s->avctx, "CPRL with large step");
1632  return AVERROR_PATCHWELCOME;
1633  }
1634  step_x = 1<<step_x;
1635  step_y = 1<<step_y;
1636 
1637  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1638  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1639  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1640  unsigned prcx, prcy;
1641  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1642  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1643  int xc = x / s->cdx[compno];
1644  int yc = y / s->cdy[compno];
1645 
1646  if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1647  continue;
1648 
1649  if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1650  continue;
1651 
1652  // check if a precinct exists
1653  prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1654  prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1655  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1656  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1657 
1658  precno = prcx + rlevel->num_precincts_x * prcy;
1659 
1660  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1661  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1662  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1663  continue;
1664  }
1665 
1666  for (layno = 0; layno < LYEpoc; layno++) {
1667  if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1668  precno, layno,
1669  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1670  qntsty->nguardbits)) < 0)
1671  return ret;
1672  }
1673  }
1674  }
1675  }
1676  }
1677  break;
1678 
1679  case JPEG2000_PGOD_RPCL:
1680  av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1681  ok_reslevel = 1;
1682  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1683  ok_reslevel = 0;
1684  step_x = 30;
1685  step_y = 30;
1686  for (compno = CSpoc; compno < CEpoc; compno++) {
1687  Jpeg2000Component *comp = tile->comp + compno;
1688  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1689 
1690  if (reslevelno < codsty->nreslevels) {
1691  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1692  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1693  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1694  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1695  }
1696  }
1697  step_x = 1<<step_x;
1698  step_y = 1<<step_y;
1699 
1700  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1701  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1702  for (compno = CSpoc; compno < CEpoc; compno++) {
1703  Jpeg2000Component *comp = tile->comp + compno;
1704  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1705  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1706  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1707  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1708  unsigned prcx, prcy;
1709  int trx0, try0;
1710 
1711  if (!s->cdx[compno] || !s->cdy[compno])
1712  return AVERROR_INVALIDDATA;
1713 
1714  if (reslevelno >= codsty->nreslevels)
1715  continue;
1716 
1717  trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1718  try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1719 
1720  if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1721  (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1722  continue;
1723 
1724  if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1725  (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1726  continue;
1727 
1728  // check if a precinct exists
1729  prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1730  prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1731  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1732  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1733 
1734  precno = prcx + rlevel->num_precincts_x * prcy;
1735 
1736  ok_reslevel = 1;
1737  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1738  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1739  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1740  continue;
1741  }
1742 
1743  for (layno = 0; layno < LYEpoc; layno++) {
1744  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1745  codsty, rlevel,
1746  precno, layno,
1747  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1748  qntsty->nguardbits)) < 0)
1749  return ret;
1750  }
1751  }
1752  }
1753  }
1754  }
1755  break;
1756 
1757  case JPEG2000_PGOD_PCRL:
1758  av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1759  step_x = 32;
1760  step_y = 32;
1761  for (compno = CSpoc; compno < CEpoc; compno++) {
1762  Jpeg2000Component *comp = tile->comp + compno;
1763  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1764 
1765  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1766  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1767  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1768  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1769  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1770  }
1771  }
1772  if (step_x >= 31 || step_y >= 31){
1773  avpriv_request_sample(s->avctx, "PCRL with large step");
1774  return AVERROR_PATCHWELCOME;
1775  }
1776  step_x = 1<<step_x;
1777  step_y = 1<<step_y;
1778 
1779  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1780  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1781  for (compno = CSpoc; compno < CEpoc; compno++) {
1782  Jpeg2000Component *comp = tile->comp + compno;
1783  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1784  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1785 
1786  if (!s->cdx[compno] || !s->cdy[compno])
1787  return AVERROR_INVALIDDATA;
1788 
1789  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1790  unsigned prcx, prcy;
1791  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1792  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1793  int trx0, try0;
1794 
1795  trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1796  try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1797 
1798  if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1799  (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1800  continue;
1801 
1802  if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1803  (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1804  continue;
1805 
1806  // check if a precinct exists
1807  prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1808  prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1809  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1810  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1811 
1812  precno = prcx + rlevel->num_precincts_x * prcy;
1813 
1814  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1815  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1816  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1817  continue;
1818  }
1819 
1820  for (layno = 0; layno < LYEpoc; layno++) {
1821  if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1822  precno, layno,
1823  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1824  qntsty->nguardbits)) < 0)
1825  return ret;
1826  }
1827  }
1828  }
1829  }
1830  }
1831  break;
1832 
1833  default:
1834  break;
1835  }
1836 
1837  return ret;
1838 }
1839 
1841 {
1842  int ret = AVERROR_BUG;
1843  int i;
1844  int tp_index = 0;
1845 
1846  s->bit_index = 8;
1847  if (tile->poc.nb_poc) {
1848  for (i=0; i<tile->poc.nb_poc; i++) {
1849  Jpeg2000POCEntry *e = &tile->poc.poc[i];
1851  e->RSpoc, e->CSpoc,
1852  FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1853  e->REpoc,
1854  FFMIN(e->CEpoc, s->ncomponents),
1855  e->Ppoc, &tp_index
1856  );
1857  if (ret < 0)
1858  return ret;
1859  }
1860  } else {
1862  0, 0,
1863  tile->codsty[0].nlayers,
1864  33,
1865  s->ncomponents,
1866  tile->codsty[0].prog_order,
1867  &tp_index
1868  );
1869  }
1870  /* EOC marker reached */
1871  bytestream2_skip(&s->g, 2);
1872 
1873  return ret;
1874 }
1875 
1876 /* TIER-1 routines */
1877 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1878  int bpno, int bandno,
1879  int vert_causal_ctx_csty_symbol)
1880 {
1881  int mask = 3 << (bpno - 1), y0, x, y;
1882 
1883  for (y0 = 0; y0 < height; y0 += 4)
1884  for (x = 0; x < width; x++)
1885  for (y = y0; y < height && y < y0 + 4; y++) {
1886  int flags_mask = -1;
1887  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1889  if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1890  && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1891  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1892  int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1893  if (t1->mqc.raw) {
1894  t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno)) << 31;
1895  t1->data[(y) * t1->stride + x] |= mask;
1896  } else {
1897  t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) << 31;
1898  t1->data[(y) * t1->stride + x] |= mask;
1899  }
1901  t1->data[(y) * t1->stride + x] & INT32_MIN);
1902  }
1903  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1904  }
1905  }
1906 }
1907 
1908 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1909  int bpno, int vert_causal_ctx_csty_symbol)
1910 {
1911  int phalf;
1912  int y0, x, y;
1913 
1914  phalf = 1 << (bpno - 1);
1915 
1916  for (y0 = 0; y0 < height; y0 += 4)
1917  for (x = 0; x < width; x++)
1918  for (y = y0; y < height && y < y0 + 4; y++)
1919  if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1920  int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1922  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1923  t1->data[(y) * t1->stride + x] |= phalf;
1924  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno))
1925  t1->data[(y) * t1->stride + x] |= phalf << 1;
1926  else {
1927  t1->data[(y) * t1->stride + x] &= ~(phalf << 1);
1928 
1929  }
1930  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1931  }
1932 }
1933 
1935  int width, int height, int bpno, int bandno,
1936  int seg_symbols, int vert_causal_ctx_csty_symbol)
1937 {
1938  int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1939 
1940  for (y0 = 0; y0 < height; y0 += 4) {
1941  for (x = 0; x < width; x++) {
1942  int flags_mask = -1;
1943  if (vert_causal_ctx_csty_symbol)
1945  if (y0 + 3 < height &&
1946  !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1947  (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1948  (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1949  (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1950  if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1951  continue;
1952  runlen = ff_mqc_decode(&t1->mqc,
1953  t1->mqc.cx_states + MQC_CX_UNI);
1954  runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1955  t1->mqc.cx_states +
1956  MQC_CX_UNI);
1957  dec = 1;
1958  } else {
1959  runlen = 0;
1960  dec = 0;
1961  }
1962 
1963  for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1964  int flags_mask = -1;
1965  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1967  if (!dec) {
1968  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1969  dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1970  bandno));
1971  }
1972  }
1973  if (dec) {
1974  int xorbit;
1975  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1976  &xorbit);
1977  t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) << 31;
1978  t1->data[(y) * t1->stride + x] |= mask;
1979  ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] & INT32_MIN);
1980  }
1981  dec = 0;
1982  t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1983  }
1984  }
1985  }
1986  if (seg_symbols) {
1987  int val;
1988  val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1989  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1990  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1991  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1992  if (val != 0xa)
1993  av_log(s->avctx, AV_LOG_ERROR,
1994  "Segmentation symbol value incorrect\n");
1995  }
1996 }
1997 
1999  Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
2000  int width, int height, int bandpos, uint8_t roi_shift, const int M_b)
2001 {
2002  int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + 31 - M_b - 1 - roi_shift;
2003  int pass_cnt = 0;
2004  int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
2005  int term_cnt = 0;
2006  int coder_type;
2007 
2008  av_assert0(width <= 1024U && height <= 1024U);
2009  av_assert0(width*height <= 4096);
2010 
2011  memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
2012 
2013  /* If code-block contains no compressed data: nothing to do. */
2014  if (!cblk->length)
2015  return 0;
2016 
2017  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
2018 
2019  cblk->data[cblk->length] = 0xff;
2020  cblk->data[cblk->length+1] = 0xff;
2021  ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
2022 
2023  while (passno--) {
2024  if (bpno < 0 || bpno > 29) {
2025  av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
2026  return AVERROR_INVALIDDATA;
2027  }
2028  switch(pass_t) {
2029  case 0:
2030  decode_sigpass(t1, width, height, bpno + 1, bandpos,
2031  vert_causal_ctx_csty_symbol);
2032  break;
2033  case 1:
2034  decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
2035  break;
2036  case 2:
2037  av_assert2(!t1->mqc.raw);
2038  decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
2039  codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
2040  vert_causal_ctx_csty_symbol);
2041  break;
2042  }
2043  if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
2044  ff_mqc_init_contexts(&t1->mqc);
2045 
2046  if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
2047  if (term_cnt >= cblk->nb_terminations) {
2048  av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
2049  return AVERROR_INVALIDDATA;
2050  }
2051  if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
2052  av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
2053  cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
2054  pass_cnt, cblk->npasses);
2055  }
2056 
2057  ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
2058  }
2059 
2060  pass_t++;
2061  if (pass_t == 3) {
2062  bpno--;
2063  pass_t = 0;
2064  }
2065  pass_cnt ++;
2066  }
2067 
2068  if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
2069  av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
2070  cblk->data + cblk->length - 2 - t1->mqc.bp);
2071  }
2072 
2073  if (cblk->data + cblk->length < t1->mqc.bp) {
2074  av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
2075  }
2076 
2077  /* Reconstruct the sample values */
2078  for (int y = 0; y < height; y++) {
2079  for (int x = 0; x < width; x++) {
2080  int32_t sign, n, val;
2081  const uint32_t mask = UINT32_MAX >> (M_b + 1); // bit mask for ROI detection
2082 
2083  n = x + (y * t1->stride);
2084  val = t1->data[n];
2085  sign = val & INT32_MIN;
2086  val &= INT32_MAX;
2087  /* ROI shift, if necessary */
2088  if (roi_shift && (((uint32_t)val & ~mask) == 0))
2089  val <<= roi_shift;
2090  t1->data[n] = val | sign; /* NOTE: Binary point for reconstruction value is located in 31 - M_b */
2091  }
2092  }
2093  return 1;
2094 }
2095 
2096 /* TODO: Verify dequantization for lossless case
2097  * comp->data can be float or int
2098  * band->stepsize can be float or int
2099  * depending on the type of DWT transformation.
2100  * see ISO/IEC 15444-1:2002 A.6.1 */
2101 
2102 /* Float dequantization of a codeblock.*/
2103 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
2105  Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2106 {
2107  int i, j;
2108  int w = cblk->coord[0][1] - cblk->coord[0][0];
2109  const int downshift = 31 - M_b;
2110  float fscale = band->f_stepsize;
2111  fscale /= (float)(1 << downshift);
2112  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2113  float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2114  int *src = t1->data + j*t1->stride;
2115  for (i = 0; i < w; ++i) {
2116  int val = src[i];
2117  if (val < 0) // Convert sign-magnitude to two's complement
2118  val = -(val & INT32_MAX);
2119  datap[i] = (float)val * fscale;
2120  }
2121  }
2122 }
2123 
2124 /* Integer dequantization of a codeblock.*/
2125 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
2127  Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2128 {
2129  int i, j;
2130  const int downshift = 31 - M_b;
2131  int w = cblk->coord[0][1] - cblk->coord[0][0];
2132  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2133  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2134  int *src = t1->data + j*t1->stride;
2135  if (band->i_stepsize == 32768) {
2136  for (i = 0; i < w; ++i) {
2137  int val = src[i];
2138  if (val < 0) // Convert sign-magnitude to two's complement
2139  val = -((val & INT32_MAX) >> downshift);
2140  else
2141  val >>= downshift;
2142  datap[i] = val;
2143  }
2144  } else {
2145  // This should be VERY uncommon
2146  for (i = 0; i < w; ++i) {
2147  int val = src[i];
2148  if (val < 0) // Convert sign-magnitude to two's complement
2149  val = -((val & INT32_MAX) >> downshift);
2150  else
2151  val >>= downshift;
2152  datap[i] = (val * (int64_t)band->i_stepsize) / 65536;
2153  }
2154  }
2155  }
2156 }
2157 
2158 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
2160  Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2161 {
2162  int i, j;
2163  int w = cblk->coord[0][1] - cblk->coord[0][0];
2164  float fscale = band->f_stepsize;
2165  const int downshift = 31 - M_b;
2166  const int PRESCALE = 6; // At least 6 is required to pass the conformance tests in ISO/IEC 15444-4
2167  int scale;
2168 
2169  fscale /= (float)(1 << downshift);
2170  fscale *= (float)(1 << PRESCALE);
2171  fscale *= (float)(1 << (16 + I_PRESHIFT));
2172  scale = (int)(fscale + 0.5);
2173  band->i_stepsize = scale;
2174  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2175  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2176  int *src = t1->data + j*t1->stride;
2177  for (i = 0; i < w; ++i) {
2178  int val = src[i];
2179  if (val < 0) // Convert sign-magnitude to two's complement
2180  val = -(val & INT32_MAX);
2181  // Shifting down to prevent overflow in dequantization
2182  val = (val + (1 << (PRESCALE - 1))) >> PRESCALE;
2183  datap[i] = RSHIFT(val * (int64_t)band->i_stepsize, 16);
2184  }
2185  }
2186 }
2187 
2188 static inline void mct_decode(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
2189 {
2190  int i, csize = 1;
2191  void *src[3];
2192 
2193  for (i = 1; i < 3; i++) {
2194  if (tile->codsty[0].transform != tile->codsty[i].transform) {
2195  av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
2196  return;
2197  }
2198  if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
2199  av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
2200  return;
2201  }
2202  }
2203 
2204  for (i = 0; i < 3; i++)
2205  if (tile->codsty[0].transform == FF_DWT97)
2206  src[i] = tile->comp[i].f_data;
2207  else
2208  src[i] = tile->comp[i].i_data;
2209 
2210  for (i = 0; i < 2; i++)
2211  csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
2212 
2213  s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
2214 }
2215 
2216 
2217 static inline int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
2218 {
2219  Jpeg2000T1Context t1;
2220 
2221  int compno, reslevelno, bandno;
2222 
2223  /* Loop on tile components */
2224  for (compno = 0; compno < s->ncomponents; compno++) {
2225  Jpeg2000Component *comp = tile->comp + compno;
2226  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
2227  Jpeg2000QuantStyle *quantsty = tile->qntsty + compno;
2228 
2229  int coded = 0;
2230  int subbandno = 0;
2231 
2232  t1.stride = (1<<codsty->log2_cblk_width) + 2;
2233 
2234  /* Loop on resolution levels */
2235  for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
2236  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
2237  /* Loop on bands */
2238  for (bandno = 0; bandno < rlevel->nbands; bandno++, subbandno++) {
2239  int nb_precincts, precno;
2240  Jpeg2000Band *band = rlevel->band + bandno;
2241  int cblkno = 0, bandpos;
2242  /* See Rec. ITU-T T.800, Equation E-2 */
2243  int M_b = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
2244 
2245  bandpos = bandno + (reslevelno > 0);
2246 
2247  if (band->coord[0][0] == band->coord[0][1] ||
2248  band->coord[1][0] == band->coord[1][1])
2249  continue;
2250 
2251  if ((codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F) && M_b >= 31) {
2252  avpriv_request_sample(s->avctx, "JPEG2000_CTSY_HTJ2K_F and M_b >= 31");
2253  return AVERROR_PATCHWELCOME;
2254  }
2255 
2256  nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
2257  /* Loop on precincts */
2258  for (precno = 0; precno < nb_precincts; precno++) {
2259  Jpeg2000Prec *prec = band->prec + precno;
2260 
2261  /* Loop on codeblocks */
2262  for (cblkno = 0;
2263  cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
2264  cblkno++) {
2265  int x, y, ret;
2266 
2267  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
2268 
2269  if (cblk->modes & JPEG2000_CTSY_HTJ2K_F)
2270  ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
2271  cblk->coord[0][1] - cblk->coord[0][0],
2272  cblk->coord[1][1] - cblk->coord[1][0],
2273  M_b, comp->roi_shift);
2274  else
2275  ret = decode_cblk(s, codsty, &t1, cblk,
2276  cblk->coord[0][1] - cblk->coord[0][0],
2277  cblk->coord[1][1] - cblk->coord[1][0],
2278  bandpos, comp->roi_shift, M_b);
2279 
2280  if (ret)
2281  coded = 1;
2282  else
2283  continue;
2284  x = cblk->coord[0][0] - band->coord[0][0];
2285  y = cblk->coord[1][0] - band->coord[1][0];
2286 
2287  if (codsty->transform == FF_DWT97)
2288  dequantization_float(x, y, cblk, comp, &t1, band, M_b);
2289  else if (codsty->transform == FF_DWT97_INT)
2290  dequantization_int_97(x, y, cblk, comp, &t1, band, M_b);
2291  else
2292  dequantization_int(x, y, cblk, comp, &t1, band, M_b);
2293  } /* end cblk */
2294  } /*end prec */
2295  } /* end band */
2296  } /* end reslevel */
2297 
2298  /* inverse DWT */
2299  if (coded)
2300  ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
2301 
2302  } /*end comp */
2303  return 0;
2304 }
2305 
2306 #define WRITE_FRAME(D, PIXEL) \
2307  static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2308  AVFrame * picture, int precision) \
2309  { \
2310  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2311  int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2312  int pixelsize = planar ? 1 : pixdesc->nb_components; \
2313  \
2314  int compno; \
2315  int x, y; \
2316  \
2317  for (compno = 0; compno < s->ncomponents; compno++) { \
2318  Jpeg2000Component *comp = tile->comp + compno; \
2319  Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2320  PIXEL *line; \
2321  float *datap = comp->f_data; \
2322  int32_t *i_datap = comp->i_data; \
2323  int cbps = s->cbps[compno]; \
2324  int w = tile->comp[compno].coord[0][1] - \
2325  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2326  int h = tile->comp[compno].coord[1][1] - \
2327  ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2328  int plane = 0; \
2329  \
2330  if (planar) \
2331  plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2332  \
2333  y = tile->comp[compno].coord[1][0] - \
2334  ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2335  line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2336  for (; y < h; y++) { \
2337  PIXEL *dst; \
2338  \
2339  x = tile->comp[compno].coord[0][0] - \
2340  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2341  dst = line + x * pixelsize + compno*!planar; \
2342  \
2343  if (codsty->transform == FF_DWT97) { \
2344  for (; x < w; x++) { \
2345  int val = lrintf(*datap) + (1 << (cbps - 1)); \
2346  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2347  val = av_clip(val, 0, (1 << cbps) - 1); \
2348  *dst = val << (precision - cbps); \
2349  datap++; \
2350  dst += pixelsize; \
2351  } \
2352  } else { \
2353  for (; x < w; x++) { \
2354  int val = *i_datap + (1 << (cbps - 1)); \
2355  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2356  val = av_clip(val, 0, (1 << cbps) - 1); \
2357  *dst = val << (precision - cbps); \
2358  i_datap++; \
2359  dst += pixelsize; \
2360  } \
2361  } \
2362  line += picture->linesize[plane] / sizeof(PIXEL); \
2363  } \
2364  } \
2365  \
2366  }
2367 
2368 WRITE_FRAME(8, uint8_t)
2369 WRITE_FRAME(16, uint16_t)
2370 
2371 #undef WRITE_FRAME
2372 
2373 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2374  int jobnr, int threadnr)
2375 {
2376  const Jpeg2000DecoderContext *s = avctx->priv_data;
2377  AVFrame *picture = td;
2378  Jpeg2000Tile *tile = s->tile + jobnr;
2379 
2380  int ret = tile_codeblocks(s, tile);
2381  if (ret < 0)
2382  return ret;
2383 
2384  /* inverse MCT transformation */
2385  if (tile->codsty[0].mct)
2386  mct_decode(s, tile);
2387 
2388  if (s->precision <= 8) {
2389  write_frame_8(s, tile, picture, 8);
2390  } else {
2391  int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2392  picture->format == AV_PIX_FMT_RGB48 ||
2393  picture->format == AV_PIX_FMT_RGBA64 ||
2394  picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2395 
2396  write_frame_16(s, tile, picture, precision);
2397  }
2398 
2399  return 0;
2400 }
2401 
2403 {
2404  int tileno, compno;
2405  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2406  if (s->tile[tileno].comp) {
2407  for (compno = 0; compno < s->ncomponents; compno++) {
2408  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2409  Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2410 
2411  ff_jpeg2000_cleanup(comp, codsty);
2412  }
2413  av_freep(&s->tile[tileno].comp);
2414  av_freep(&s->tile[tileno].packed_headers);
2415  s->tile[tileno].packed_headers_size = 0;
2416  }
2417  }
2418  av_freep(&s->packed_headers);
2419  s->packed_headers_size = 0;
2420  memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
2421  av_freep(&s->tile);
2422  memset(s->codsty, 0, sizeof(s->codsty));
2423  memset(s->qntsty, 0, sizeof(s->qntsty));
2424  memset(s->properties, 0, sizeof(s->properties));
2425  memset(&s->poc , 0, sizeof(s->poc));
2426  s->numXtiles = s->numYtiles = 0;
2427  s->ncomponents = 0;
2428 }
2429 
2431 {
2432  Jpeg2000CodingStyle *codsty = s->codsty;
2433  Jpeg2000QuantStyle *qntsty = s->qntsty;
2434  Jpeg2000POC *poc = &s->poc;
2435  uint8_t *properties = s->properties;
2436  uint8_t in_tile_headers = 0;
2437 
2438  for (;;) {
2439  int len, ret = 0;
2440  uint16_t marker;
2441  int oldpos;
2442 
2443  if (bytestream2_get_bytes_left(&s->g) < 2) {
2444  av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2445  break;
2446  }
2447 
2448  marker = bytestream2_get_be16u(&s->g);
2449  oldpos = bytestream2_tell(&s->g);
2450  if (marker >= 0xFF30 && marker <= 0xFF3F)
2451  continue;
2452  if (marker == JPEG2000_SOD) {
2453  Jpeg2000Tile *tile;
2454  Jpeg2000TilePart *tp;
2455 
2456  if (!s->tile) {
2457  av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2458  return AVERROR_INVALIDDATA;
2459  }
2460  if (s->curtileno < 0) {
2461  av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2462  return AVERROR_INVALIDDATA;
2463  }
2464 
2465  tile = s->tile + s->curtileno;
2466  tp = tile->tile_part + tile->tp_idx;
2467  if (tp->tp_end < s->g.buffer) {
2468  av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2469  return AVERROR_INVALIDDATA;
2470  }
2471 
2472  if (s->has_ppm) {
2473  uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
2474  if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
2475  return AVERROR_INVALIDDATA;
2476  bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
2477  bytestream2_skip(&s->packed_headers_stream, tp_header_size);
2478  }
2479  if (tile->has_ppt && tile->tp_idx == 0) {
2481  }
2482 
2483  bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2484  bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2485 
2486  continue;
2487  }
2488  if (marker == JPEG2000_EOC)
2489  break;
2490 
2491  len = bytestream2_get_be16(&s->g);
2492  if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2493  if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2494  av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2495  return AVERROR_INVALIDDATA;
2496  }
2497  av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2498  break;
2499  }
2500 
2501  switch (marker) {
2502  case JPEG2000_SIZ:
2503  if (s->ncomponents) {
2504  av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2505  return AVERROR_INVALIDDATA;
2506  }
2507  ret = get_siz(s);
2508  if (!s->tile)
2509  s->numXtiles = s->numYtiles = 0;
2510  break;
2511  case JPEG2000_CAP:
2512  if (!s->ncomponents) {
2513  av_log(s->avctx, AV_LOG_ERROR, "CAP marker segment shall come after SIZ\n");
2514  return AVERROR_INVALIDDATA;
2515  }
2516  ret = get_cap(s, codsty);
2517  break;
2518  case JPEG2000_COC:
2519  if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2520  av_log(s->avctx, AV_LOG_ERROR,
2521  "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2522  return AVERROR_INVALIDDATA;
2523  }
2524  ret = get_coc(s, codsty, properties);
2525  break;
2526  case JPEG2000_COD:
2527  if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2528  av_log(s->avctx, AV_LOG_ERROR,
2529  "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2530  return AVERROR_INVALIDDATA;
2531  }
2532  ret = get_cod(s, codsty, properties);
2533  break;
2534  case JPEG2000_RGN:
2535  if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2536  av_log(s->avctx, AV_LOG_ERROR,
2537  "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2538  return AVERROR_INVALIDDATA;
2539  }
2540  ret = get_rgn(s, len);
2541  if ((!s->Ccap15_b12) && s->isHT) {
2542  av_log(s->avctx, AV_LOG_ERROR, "RGN marker found but the codestream belongs to the RGNFREE set\n");
2543  return AVERROR_INVALIDDATA;
2544  }
2545  break;
2546  case JPEG2000_QCC:
2547  if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2548  av_log(s->avctx, AV_LOG_ERROR,
2549  "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2550  return AVERROR_INVALIDDATA;
2551  }
2552  ret = get_qcc(s, len, qntsty, properties);
2553  break;
2554  case JPEG2000_QCD:
2555  if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2556  av_log(s->avctx, AV_LOG_ERROR,
2557  "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2558  return AVERROR_INVALIDDATA;
2559  }
2560  ret = get_qcd(s, len, qntsty, properties);
2561  break;
2562  case JPEG2000_POC:
2563  if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2564  av_log(s->avctx, AV_LOG_ERROR,
2565  "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2566  return AVERROR_INVALIDDATA;
2567  }
2568  ret = get_poc(s, len, poc);
2569  break;
2570  case JPEG2000_SOT:
2571  if (!in_tile_headers) {
2572  in_tile_headers = 1;
2573  if (s->has_ppm) {
2574  bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
2575  }
2576  }
2577  if (!(ret = get_sot(s, len))) {
2578  av_assert1(s->curtileno >= 0);
2579  codsty = s->tile[s->curtileno].codsty;
2580  qntsty = s->tile[s->curtileno].qntsty;
2581  poc = &s->tile[s->curtileno].poc;
2582  properties = s->tile[s->curtileno].properties;
2583  }
2584  break;
2585  case JPEG2000_PLM:
2586  // the PLM marker is ignored
2587  case JPEG2000_COM:
2588  // the comment is ignored
2589  bytestream2_skip(&s->g, len - 2);
2590  break;
2591  case JPEG2000_CRG:
2592  ret = read_crg(s, len);
2593  break;
2594  case JPEG2000_TLM:
2595  // Tile-part lengths
2596  ret = get_tlm(s, len);
2597  break;
2598  case JPEG2000_PLT:
2599  // Packet length, tile-part header
2600  ret = get_plt(s, len);
2601  break;
2602  case JPEG2000_PPM:
2603  // Packed headers, main header
2604  if (in_tile_headers) {
2605  av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
2606  return AVERROR_INVALIDDATA;
2607  }
2608  ret = get_ppm(s, len);
2609  break;
2610  case JPEG2000_PPT:
2611  // Packed headers, tile-part header
2612  if (s->has_ppm) {
2613  av_log(s->avctx, AV_LOG_ERROR,
2614  "Cannot have both PPT and PPM marker.\n");
2615  return AVERROR_INVALIDDATA;
2616  }
2617  if ((!s->Ccap15_b11) && s->isHT) {
2618  av_log(s->avctx, AV_LOG_ERROR, "PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
2619  return AVERROR_INVALIDDATA;
2620  }
2621  ret = get_ppt(s, len);
2622  break;
2623  case JPEG2000_CPF:
2624  // Corresponding profile marker
2625  ret = read_cpf(s, len);
2626  break;
2627  default:
2628  av_log(s->avctx, AV_LOG_ERROR,
2629  "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2630  marker, bytestream2_tell(&s->g) - 4);
2631  bytestream2_skip(&s->g, len - 2);
2632  break;
2633  }
2634  if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2635  av_log(s->avctx, AV_LOG_ERROR,
2636  "error during processing marker segment %.4"PRIx16"\n",
2637  marker);
2638  return ret ? ret : -1;
2639  }
2640  }
2641  return 0;
2642 }
2643 
2644 /* Read bit stream packets --> T2 operation. */
2646 {
2647  int ret = 0;
2648  int tileno;
2649 
2650  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2651  Jpeg2000Tile *tile = s->tile + tileno;
2652 
2653  if ((ret = init_tile(s, tileno)) < 0)
2654  return ret;
2655 
2656  if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2657  return ret;
2658  }
2659 
2660  return 0;
2661 }
2662 
2664 {
2665  uint32_t atom_size, atom, atom_end;
2666  int search_range = 10;
2667 
2668  while (search_range
2669  &&
2670  bytestream2_get_bytes_left(&s->g) >= 8) {
2671  atom_size = bytestream2_get_be32u(&s->g);
2672  atom = bytestream2_get_be32u(&s->g);
2673  if (atom_size == 1) {
2674  if (bytestream2_get_be32u(&s->g)) {
2675  avpriv_request_sample(s->avctx, "Huge atom");
2676  return 0;
2677  }
2678  atom_size = bytestream2_get_be32u(&s->g);
2679  if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
2680  return AVERROR_INVALIDDATA;
2681  atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2682  } else {
2683  if (atom_size < 8 || (int64_t)bytestream2_tell(&s->g) + atom_size - 8 > INT_MAX)
2684  return AVERROR_INVALIDDATA;
2685  atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2686  }
2687 
2688  if (atom == JP2_CODESTREAM)
2689  return 1;
2690 
2691  if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2692  return 0;
2693 
2694  if (atom == JP2_HEADER &&
2695  atom_size >= 16) {
2696  uint32_t atom2_size, atom2, atom2_end;
2697  do {
2698  if (bytestream2_get_bytes_left(&s->g) < 8)
2699  break;
2700  atom2_size = bytestream2_get_be32u(&s->g);
2701  atom2 = bytestream2_get_be32u(&s->g);
2702  atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2703  if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2704  break;
2705  atom2_size -= 8;
2706  if (atom2 == JP2_CODESTREAM) {
2707  return 1;
2708  } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2709  int method = bytestream2_get_byteu(&s->g);
2710  bytestream2_skipu(&s->g, 2);
2711  if (method == 1) {
2712  s->colour_space = bytestream2_get_be32u(&s->g);
2713  }
2714  } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2715  int i, size, colour_count, colour_channels, colour_depth[3];
2716  colour_count = bytestream2_get_be16u(&s->g);
2717  colour_channels = bytestream2_get_byteu(&s->g);
2718  // FIXME: Do not ignore channel_sign
2719  colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2720  colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2721  colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2722  size = (colour_depth[0] + 7 >> 3) * colour_count +
2723  (colour_depth[1] + 7 >> 3) * colour_count +
2724  (colour_depth[2] + 7 >> 3) * colour_count;
2725  if (colour_count > AVPALETTE_COUNT ||
2726  colour_channels != 3 ||
2727  colour_depth[0] > 16 ||
2728  colour_depth[1] > 16 ||
2729  colour_depth[2] > 16 ||
2730  atom2_size < size) {
2731  avpriv_request_sample(s->avctx, "Unknown palette");
2732  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2733  continue;
2734  }
2735  s->pal8 = 1;
2736  for (i = 0; i < colour_count; i++) {
2737  uint32_t r, g, b;
2738  if (colour_depth[0] <= 8) {
2739  r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2740  r |= r >> colour_depth[0];
2741  } else {
2742  r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2743  }
2744  if (colour_depth[1] <= 8) {
2745  g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2746  g |= g >> colour_depth[1];
2747  } else {
2748  g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2749  }
2750  if (colour_depth[2] <= 8) {
2751  b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2752  b |= b >> colour_depth[2];
2753  } else {
2754  b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2755  }
2756  s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2757  }
2758  } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2759  int n = bytestream2_get_be16u(&s->g);
2760  for (; n>0; n--) {
2761  int cn = bytestream2_get_be16(&s->g);
2762  int av_unused typ = bytestream2_get_be16(&s->g);
2763  int asoc = bytestream2_get_be16(&s->g);
2764  if (cn < 4 && asoc < 4)
2765  s->cdef[cn] = asoc;
2766  }
2767  } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2768  int64_t vnum, vden, hnum, hden, vexp, hexp;
2769  uint32_t resx;
2770  bytestream2_skip(&s->g, 4);
2771  resx = bytestream2_get_be32u(&s->g);
2772  if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2773  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2774  continue;
2775  }
2776  vnum = bytestream2_get_be16u(&s->g);
2777  vden = bytestream2_get_be16u(&s->g);
2778  hnum = bytestream2_get_be16u(&s->g);
2779  hden = bytestream2_get_be16u(&s->g);
2780  vexp = bytestream2_get_byteu(&s->g);
2781  hexp = bytestream2_get_byteu(&s->g);
2782  if (!vnum || !vden || !hnum || !hden) {
2783  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2784  av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2785  continue;
2786  }
2787  if (vexp > hexp) {
2788  vexp -= hexp;
2789  hexp = 0;
2790  } else {
2791  hexp -= vexp;
2792  vexp = 0;
2793  }
2794  if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2795  && INT64_MAX / (vnum * hden) > pow(10, vexp))
2796  av_reduce(&s->sar.den, &s->sar.num,
2797  hnum * vden * pow(10, hexp),
2798  vnum * hden * pow(10, vexp),
2799  INT32_MAX);
2800  }
2801  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2802  } while (atom_end - atom2_end >= 8);
2803  } else {
2804  search_range--;
2805  }
2806  bytestream2_seek(&s->g, atom_end, SEEK_SET);
2807  }
2808 
2809  return 0;
2810 }
2811 
2813 {
2815 
2816  if (avctx->lowres)
2817  av_log(avctx, AV_LOG_WARNING, "lowres is overriden by reduction_factor but set anyway\n");
2818  if (!s->reduction_factor && avctx->lowres < JPEG2000_MAX_RESLEVELS) {
2819  s->reduction_factor = avctx->lowres;
2820  }
2821  if (avctx->lowres != s->reduction_factor && avctx->lowres)
2822  return AVERROR(EINVAL);
2823 
2824  ff_jpeg2000dsp_init(&s->dsp);
2826 
2827  return 0;
2828 }
2829 
2830 static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
2831  int *got_frame, AVPacket *avpkt)
2832 {
2834  int ret;
2835 
2836  s->avctx = avctx;
2837  bytestream2_init(&s->g, avpkt->data, avpkt->size);
2838  s->curtileno = -1;
2839  memset(s->cdef, -1, sizeof(s->cdef));
2840 
2841  if (bytestream2_get_bytes_left(&s->g) < 2) {
2843  goto end;
2844  }
2845 
2846  // check if the image is in jp2 format
2847  if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2848  (bytestream2_get_be32u(&s->g) == 12) &&
2849  (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2850  (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2851  if (!jp2_find_codestream(s)) {
2852  av_log(avctx, AV_LOG_ERROR,
2853  "Could not find Jpeg2000 codestream atom.\n");
2855  goto end;
2856  }
2857  } else {
2858  bytestream2_seek(&s->g, 0, SEEK_SET);
2859  }
2860 
2861  while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2862  bytestream2_skip(&s->g, 1);
2863 
2864  if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2865  av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2867  goto end;
2868  }
2870  goto end;
2871 
2872  if (s->sar.num && s->sar.den)
2873  avctx->sample_aspect_ratio = s->sar;
2874  s->sar.num = s->sar.den = 0;
2875 
2876  if (avctx->skip_frame >= AVDISCARD_ALL) {
2878  return avpkt->size;
2879  }
2880 
2881  /* get picture buffer */
2882  if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
2883  goto end;
2884 
2886  goto end;
2887 
2888  for (int x = 0; x < s->ncomponents; x++) {
2889  if (s->cdef[x] < 0) {
2890  for (x = 0; x < s->ncomponents; x++) {
2891  s->cdef[x] = x + 1;
2892  }
2893  if ((s->ncomponents & 1) == 0)
2894  s->cdef[s->ncomponents-1] = 0;
2895  break;
2896  }
2897  }
2898 
2899  for (int x = 0; x < s->ncomponents && s->codsty[x].transform == FF_DWT53;)
2900  if (++x == s->ncomponents)
2901  picture->flags |= AV_FRAME_FLAG_LOSSLESS;
2902 
2903  avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2904 
2906 
2907  *got_frame = 1;
2908 
2909  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2910  memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2911 
2912  return bytestream2_tell(&s->g);
2913 
2914 end:
2916  return ret;
2917 }
2918 
2919 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2920 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2921 
2922 static const AVOption options[] = {
2923  { "lowres", "Lower the decoding resolution by a power of two",
2924  OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2925  { NULL },
2926 };
2927 
2928 static const AVClass jpeg2000_class = {
2929  .class_name = "jpeg2000",
2930  .item_name = av_default_item_name,
2931  .option = options,
2932  .version = LIBAVUTIL_VERSION_INT,
2933 };
2934 
2936  .p.name = "jpeg2000",
2937  CODEC_LONG_NAME("JPEG 2000"),
2938  .p.type = AVMEDIA_TYPE_VIDEO,
2939  .p.id = AV_CODEC_ID_JPEG2000,
2941  .priv_data_size = sizeof(Jpeg2000DecoderContext),
2944  .p.priv_class = &jpeg2000_class,
2945  .p.max_lowres = 5,
2947  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2948 };
tile_codeblocks
static int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:2217
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
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:2922
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:601
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:3170
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:1908
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:403
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:539
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:1092
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
dequantization_float
static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
Definition: jpeg2000dec.c:2103
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:38
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:683
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:2373
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:1077
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:424
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:2430
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:1841
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:764
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:1877
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:1549
MQC_CX_UNI
#define MQC_CX_UNI
Definition: mqc.h:33
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:486
dequantization_int
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
Definition: jpeg2000dec.c:2125
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:2645
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:2928
JPEG2000_CPF
@ JPEG2000_CPF
Definition: jpeg2000.h:47
read_crg
static int read_crg(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:879
avassert.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:235
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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:1817
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:2402
Jpeg2000CodingStyle::cblk_style
uint8_t cblk_style
Definition: jpeg2000.h:154
Jpeg2000QuantStyle::nguardbits
uint8_t nguardbits
Definition: jpeg2000.h:165
float
float
Definition: af_crystalizer.c:122
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:889
Jpeg2000CodingStyle::transform
uint8_t transform
Definition: jpeg2000.h:150
s
#define s(width, name)
Definition: cbs_vp9.c:198
I_PRESHIFT
#define I_PRESHIFT
Definition: jpeg2000dwt.h:35
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
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:1053
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:230
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
RSHIFT
#define RSHIFT(a, b)
Definition: common.h:56
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
dequantization_int_97
static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
Definition: jpeg2000dec.c:2158
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
if
if(ret)
Definition: filter_design.txt:179
init_tile
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
Definition: jpeg2000dec.c:1020
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:492
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:75
jpeg2000_decode_init
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
Definition: jpeg2000dec.c:2812
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:962
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
options
Definition: swscale.c:42
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:986
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
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, const int M_b)
Definition: jpeg2000dec.c:1998
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:1876
ff_jpeg2000_cleanup
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:600
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:469
get_qcd
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, const uint8_t *properties)
Definition: jpeg2000dec.c:725
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:368
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:2919
Jpeg2000Component::i_data
int * i_data
Definition: jpeg2000.h:237
AVPacket::size
int size
Definition: packet.h:540
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:743
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
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:488
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:490
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:2830
HT_PLHD_OFF
@ HT_PLHD_OFF
Definition: jpeg2000dec.c:59
get_rgn
static int get_rgn(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:647
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:220
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:1140
AV_PIX_FMT_XYZ12
#define AV_PIX_FMT_XYZ12
Definition: pixfmt.h:549
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:487
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:940
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:2663
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:612
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:2306
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:2920
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
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:80
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:451
FF_DWT53
@ FF_DWT53
Definition: jpeg2000dwt.h:39
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
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
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:1840
mct_decode
static void mct_decode(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:2188
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:2935
get_sot
static int get_sot(Jpeg2000DecoderContext *s, int n)
Definition: jpeg2000dec.c:826
Jpeg2000Cblk::npasses
uint8_t npasses
Definition: jpeg2000.h:184
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
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:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
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:1115
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:1934
Jpeg2000Cblk::nonzerobits
uint8_t nonzerobits
Definition: jpeg2000.h:186
get_qcx
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
Definition: jpeg2000dec.c:680
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:905
AV_FRAME_FLAG_LOSSLESS
#define AV_FRAME_FLAG_LOSSLESS
A decoder can use this flag to mark frames which were originally encoded losslessly.
Definition: frame.h:675
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:1650
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:656
JPEG2000_CBLK_SEGSYM
#define JPEG2000_CBLK_SEGSYM
Definition: jpeg2000.h:115
FF_DWT97_INT
@ FF_DWT97_INT
Definition: jpeg2000dwt.h:40
select_header
static void select_header(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile, int *tp_index)
Definition: jpeg2000dec.c:1103
src
#define src
Definition: vp8dsp.c:248
get_cod
static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, const uint8_t *properties)
Definition: jpeg2000dec.c:575
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