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