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