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