FFmpeg  4.4.7
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, dimy);
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  }
1317  }
1318  // Save state of stream
1319  tile->tile_part[*tp_index].tpg = s->g;
1320  return 0;
1321 
1322 skip_data:
1323  if (codsty->csty & JPEG2000_CSTY_EPH) {
1324  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1325  bytestream2_skip(&s->g, 2);
1326  else
1327  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1328  }
1329  if (s->has_ppm) {
1330  tile->tile_part[*tp_index].header_tpg = s->g;
1331  select_stream(s, tile, tp_index, codsty);
1332  } else if (tile->has_ppt) {
1333  tile->packed_headers_stream = s->g;
1334  select_stream(s, tile, tp_index, codsty);
1335  }
1336  tile->tile_part[*tp_index].tpg = s->g;
1337  return 0;
1338 }
1339 
1341  int RSpoc, int CSpoc,
1342  int LYEpoc, int REpoc, int CEpoc,
1343  int Ppoc, int *tp_index)
1344 {
1345  int ret = 0;
1346  int layno, reslevelno, compno, precno, ok_reslevel;
1347  int x, y;
1348  int step_x, step_y;
1349 
1350  switch (Ppoc) {
1351  case JPEG2000_PGOD_RLCP:
1352  av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1353  ok_reslevel = 1;
1354  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1355  ok_reslevel = 0;
1356  for (layno = 0; layno < LYEpoc; layno++) {
1357  for (compno = CSpoc; compno < CEpoc; compno++) {
1358  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1359  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1360  if (reslevelno < codsty->nreslevels) {
1361  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1362  reslevelno;
1363  ok_reslevel = 1;
1364  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1365  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1366  codsty, rlevel,
1367  precno, layno,
1368  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1369  qntsty->nguardbits)) < 0)
1370  return ret;
1371  }
1372  }
1373  }
1374  }
1375  break;
1376 
1377  case JPEG2000_PGOD_LRCP:
1378  av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1379  for (layno = 0; layno < LYEpoc; layno++) {
1380  ok_reslevel = 1;
1381  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1382  ok_reslevel = 0;
1383  for (compno = CSpoc; compno < CEpoc; compno++) {
1384  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1385  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1386  if (reslevelno < codsty->nreslevels) {
1387  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1388  reslevelno;
1389  ok_reslevel = 1;
1390  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1391  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1392  codsty, rlevel,
1393  precno, layno,
1394  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1395  qntsty->nguardbits)) < 0)
1396  return ret;
1397  }
1398  }
1399  }
1400  }
1401  break;
1402 
1403  case JPEG2000_PGOD_CPRL:
1404  av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1405  for (compno = CSpoc; compno < CEpoc; compno++) {
1406  Jpeg2000Component *comp = tile->comp + compno;
1407  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1408  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1409  step_x = 32;
1410  step_y = 32;
1411 
1412  if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1413  continue;
1414 
1415  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1416  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1417  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1418  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1419  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1420  }
1421  if (step_x >= 31 || step_y >= 31){
1422  avpriv_request_sample(s->avctx, "CPRL with large step");
1423  return AVERROR_PATCHWELCOME;
1424  }
1425  step_x = 1<<step_x;
1426  step_y = 1<<step_y;
1427 
1428  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1429  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1430  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1431  unsigned prcx, prcy;
1432  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1433  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1434  int xc = x / s->cdx[compno];
1435  int yc = y / s->cdy[compno];
1436 
1437  if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1438  continue;
1439 
1440  if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1441  continue;
1442 
1443  // check if a precinct exists
1444  prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1445  prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1446  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1447  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1448 
1449  precno = prcx + rlevel->num_precincts_x * prcy;
1450 
1451  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1452  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1453  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1454  continue;
1455  }
1456 
1457  for (layno = 0; layno < LYEpoc; layno++) {
1458  if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1459  precno, layno,
1460  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1461  qntsty->nguardbits)) < 0)
1462  return ret;
1463  }
1464  }
1465  }
1466  }
1467  }
1468  break;
1469 
1470  case JPEG2000_PGOD_RPCL:
1471  av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1472  ok_reslevel = 1;
1473  for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1474  ok_reslevel = 0;
1475  step_x = 30;
1476  step_y = 30;
1477  for (compno = CSpoc; compno < CEpoc; compno++) {
1478  Jpeg2000Component *comp = tile->comp + compno;
1479  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1480 
1481  if (reslevelno < codsty->nreslevels) {
1482  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1483  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1484  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1485  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1486  }
1487  }
1488  step_x = 1<<step_x;
1489  step_y = 1<<step_y;
1490 
1491  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1492  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1493  for (compno = CSpoc; compno < CEpoc; compno++) {
1494  Jpeg2000Component *comp = tile->comp + compno;
1495  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1496  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1497  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1498  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1499  unsigned prcx, prcy;
1500  int trx0, try0;
1501 
1502  if (!s->cdx[compno] || !s->cdy[compno])
1503  return AVERROR_INVALIDDATA;
1504 
1505  if (reslevelno >= codsty->nreslevels)
1506  continue;
1507 
1508  trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1509  try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1510 
1511  if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1512  (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1513  continue;
1514 
1515  if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1516  (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1517  continue;
1518 
1519  // check if a precinct exists
1520  prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1521  prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1522  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1523  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1524 
1525  precno = prcx + rlevel->num_precincts_x * prcy;
1526 
1527  ok_reslevel = 1;
1528  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1529  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1530  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1531  continue;
1532  }
1533 
1534  for (layno = 0; layno < LYEpoc; layno++) {
1535  if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1536  codsty, rlevel,
1537  precno, layno,
1538  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1539  qntsty->nguardbits)) < 0)
1540  return ret;
1541  }
1542  }
1543  }
1544  }
1545  }
1546  break;
1547 
1548  case JPEG2000_PGOD_PCRL:
1549  av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1550  step_x = 32;
1551  step_y = 32;
1552  for (compno = CSpoc; compno < CEpoc; compno++) {
1553  Jpeg2000Component *comp = tile->comp + compno;
1554  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1555 
1556  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1557  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1558  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1559  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1560  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1561  }
1562  }
1563  if (step_x >= 31 || step_y >= 31){
1564  avpriv_request_sample(s->avctx, "PCRL with large step");
1565  return AVERROR_PATCHWELCOME;
1566  }
1567  step_x = 1<<step_x;
1568  step_y = 1<<step_y;
1569 
1570  for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1571  for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1572  for (compno = CSpoc; compno < CEpoc; compno++) {
1573  Jpeg2000Component *comp = tile->comp + compno;
1574  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1575  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1576 
1577  if (!s->cdx[compno] || !s->cdy[compno])
1578  return AVERROR_INVALIDDATA;
1579 
1580  for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1581  unsigned prcx, prcy;
1582  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1583  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1584  int trx0, try0;
1585 
1586  trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1587  try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1588 
1589  if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1590  (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1591  continue;
1592 
1593  if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1594  (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1595  continue;
1596 
1597  // check if a precinct exists
1598  prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1599  prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1600  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1601  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1602 
1603  precno = prcx + rlevel->num_precincts_x * prcy;
1604 
1605  if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1606  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1607  prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1608  continue;
1609  }
1610 
1611  for (layno = 0; layno < LYEpoc; layno++) {
1612  if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1613  precno, layno,
1614  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1615  qntsty->nguardbits)) < 0)
1616  return ret;
1617  }
1618  }
1619  }
1620  }
1621  }
1622  break;
1623 
1624  default:
1625  break;
1626  }
1627 
1628  return ret;
1629 }
1630 
1632 {
1633  int ret = AVERROR_BUG;
1634  int i;
1635  int tp_index = 0;
1636 
1637  s->bit_index = 8;
1638  if (tile->poc.nb_poc) {
1639  for (i=0; i<tile->poc.nb_poc; i++) {
1640  Jpeg2000POCEntry *e = &tile->poc.poc[i];
1642  e->RSpoc, e->CSpoc,
1643  FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1644  e->REpoc,
1645  FFMIN(e->CEpoc, s->ncomponents),
1646  e->Ppoc, &tp_index
1647  );
1648  if (ret < 0)
1649  return ret;
1650  }
1651  } else {
1653  0, 0,
1654  tile->codsty[0].nlayers,
1655  33,
1656  s->ncomponents,
1657  tile->codsty[0].prog_order,
1658  &tp_index
1659  );
1660  }
1661  /* EOC marker reached */
1662  bytestream2_skip(&s->g, 2);
1663 
1664  return ret;
1665 }
1666 
1667 /* TIER-1 routines */
1669  int bpno, int bandno,
1670  int vert_causal_ctx_csty_symbol)
1671 {
1672  int mask = 3 << (bpno - 1), y0, x, y;
1673 
1674  for (y0 = 0; y0 < height; y0 += 4)
1675  for (x = 0; x < width; x++)
1676  for (y = y0; y < height && y < y0 + 4; y++) {
1677  int flags_mask = -1;
1678  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1680  if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1681  && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1682  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1683  int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1684  if (t1->mqc.raw)
1685  t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1686  else
1687  t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1688  -mask : mask;
1689 
1691  t1->data[(y) * t1->stride + x] < 0);
1692  }
1693  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1694  }
1695  }
1696 }
1697 
1699  int bpno, int vert_causal_ctx_csty_symbol)
1700 {
1701  int phalf, nhalf;
1702  int y0, x, y;
1703 
1704  phalf = 1 << (bpno - 1);
1705  nhalf = -phalf;
1706 
1707  for (y0 = 0; y0 < height; y0 += 4)
1708  for (x = 0; x < width; x++)
1709  for (y = y0; y < height && y < y0 + 4; y++)
1710  if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1711  int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1713  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1714  int r = ff_mqc_decode(&t1->mqc,
1715  t1->mqc.cx_states + ctxno)
1716  ? phalf : nhalf;
1717  t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1718  t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1719  }
1720 }
1721 
1723  int width, int height, int bpno, int bandno,
1724  int seg_symbols, int vert_causal_ctx_csty_symbol)
1725 {
1726  int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1727 
1728  for (y0 = 0; y0 < height; y0 += 4) {
1729  for (x = 0; x < width; x++) {
1730  int flags_mask = -1;
1731  if (vert_causal_ctx_csty_symbol)
1733  if (y0 + 3 < height &&
1734  !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1735  (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1736  (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1737  (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1738  if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1739  continue;
1740  runlen = ff_mqc_decode(&t1->mqc,
1741  t1->mqc.cx_states + MQC_CX_UNI);
1742  runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1743  t1->mqc.cx_states +
1744  MQC_CX_UNI);
1745  dec = 1;
1746  } else {
1747  runlen = 0;
1748  dec = 0;
1749  }
1750 
1751  for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1752  int flags_mask = -1;
1753  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1755  if (!dec) {
1756  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1757  dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1758  bandno));
1759  }
1760  }
1761  if (dec) {
1762  int xorbit;
1763  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1764  &xorbit);
1765  t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1766  t1->mqc.cx_states + ctxno) ^
1767  xorbit)
1768  ? -mask : mask;
1769  ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1770  }
1771  dec = 0;
1772  t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1773  }
1774  }
1775  }
1776  if (seg_symbols) {
1777  int val;
1778  val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1779  val = (val << 1) + 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  if (val != 0xa)
1783  av_log(s->avctx, AV_LOG_ERROR,
1784  "Segmentation symbol value incorrect\n");
1785  }
1786 }
1787 
1790  int width, int height, int bandpos, uint8_t roi_shift)
1791 {
1792  int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1793  int pass_cnt = 0;
1794  int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1795  int term_cnt = 0;
1796  int coder_type;
1797 
1798  av_assert0(width <= 1024U && height <= 1024U);
1799  av_assert0(width*height <= 4096);
1800 
1801  memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1802 
1803  /* If code-block contains no compressed data: nothing to do. */
1804  if (!cblk->length)
1805  return 0;
1806 
1807  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1808 
1809  cblk->data[cblk->length] = 0xff;
1810  cblk->data[cblk->length+1] = 0xff;
1811  ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1812 
1813  while (passno--) {
1814  if (bpno < 0 || bpno > 29) {
1815  av_log(s->avctx, AV_LOG_ERROR, "bpno (%d) became invalid\n", bpno);
1816  return AVERROR_INVALIDDATA;
1817  }
1818  switch(pass_t) {
1819  case 0:
1820  decode_sigpass(t1, width, height, bpno + 1, bandpos,
1821  vert_causal_ctx_csty_symbol);
1822  break;
1823  case 1:
1824  decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1825  break;
1826  case 2:
1827  av_assert2(!t1->mqc.raw);
1828  decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1829  codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1830  vert_causal_ctx_csty_symbol);
1831  break;
1832  }
1833  if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1834  ff_mqc_init_contexts(&t1->mqc);
1835 
1836  if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1837  if (term_cnt >= cblk->nb_terminations) {
1838  av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1839  return AVERROR_INVALIDDATA;
1840  }
1841  if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1842  av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1843  cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1844  pass_cnt, cblk->npasses);
1845  }
1846 
1847  ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1848  }
1849 
1850  pass_t++;
1851  if (pass_t == 3) {
1852  bpno--;
1853  pass_t = 0;
1854  }
1855  pass_cnt ++;
1856  }
1857 
1858  if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
1859  av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1860  cblk->data + cblk->length - 2 - t1->mqc.bp);
1861  }
1862 
1863  if (cblk->data + cblk->length < t1->mqc.bp) {
1864  av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
1865  }
1866 
1867  return 1;
1868 }
1869 
1871  int quan_parameter)
1872 {
1873  uint8_t roi_shift;
1874  int val;
1875  roi_shift = comp->roi_shift;
1876  val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1877 
1878  if (val > (1 << roi_shift))
1879  return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1880  return quan_parameter;
1881 }
1882 
1883 /* TODO: Verify dequantization for lossless case
1884  * comp->data can be float or int
1885  * band->stepsize can be float or int
1886  * depending on the type of DWT transformation.
1887  * see ISO/IEC 15444-1:2002 A.6.1 */
1888 
1889 /* Float dequantization of a codeblock.*/
1890 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1893 {
1894  int i, j;
1895  int w = cblk->coord[0][1] - cblk->coord[0][0];
1896  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1897  float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1898  int *src = t1->data + j*t1->stride;
1899  for (i = 0; i < w; ++i)
1900  datap[i] = src[i] * band->f_stepsize;
1901  }
1902 }
1903 
1904 /* Integer dequantization of a codeblock.*/
1905 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1908 {
1909  int i, j;
1910  int w = cblk->coord[0][1] - cblk->coord[0][0];
1911  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1912  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1913  int *src = t1->data + j*t1->stride;
1914  if (band->i_stepsize == 32768) {
1915  for (i = 0; i < w; ++i)
1916  datap[i] = src[i] / 2;
1917  } else {
1918  // This should be VERY uncommon
1919  for (i = 0; i < w; ++i)
1920  datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1921  }
1922  }
1923 }
1924 
1925 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1928 {
1929  int i, j;
1930  int w = cblk->coord[0][1] - cblk->coord[0][0];
1931  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1932  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1933  int *src = t1->data + j*t1->stride;
1934  for (i = 0; i < w; ++i)
1935  datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1936  }
1937 }
1938 
1940 {
1941  int i, csize = 1;
1942  void *src[3];
1943 
1944  for (i = 1; i < 3; i++) {
1945  if (tile->codsty[0].transform != tile->codsty[i].transform) {
1946  av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1947  return;
1948  }
1949  if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1950  av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1951  return;
1952  }
1953  }
1954 
1955  for (i = 0; i < 3; i++)
1956  if (tile->codsty[0].transform == FF_DWT97)
1957  src[i] = tile->comp[i].f_data;
1958  else
1959  src[i] = tile->comp[i].i_data;
1960 
1961  for (i = 0; i < 2; i++)
1962  csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1963 
1964  s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1965 }
1966 
1967 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1970 {
1971  int i, j;
1972  int w = cblk->coord[0][1] - cblk->coord[0][0];
1973  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1974  int *src = t1->data + j*t1->stride;
1975  for (i = 0; i < w; ++i)
1976  src[i] = roi_shift_param(comp, src[i]);
1977  }
1978 }
1979 
1981 {
1983 
1984  int compno, reslevelno, bandno;
1985 
1986  /* Loop on tile components */
1987  for (compno = 0; compno < s->ncomponents; compno++) {
1988  Jpeg2000Component *comp = tile->comp + compno;
1989  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1990  int coded = 0;
1991 
1992  t1.stride = (1<<codsty->log2_cblk_width) + 2;
1993 
1994  /* Loop on resolution levels */
1995  for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1996  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1997  /* Loop on bands */
1998  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1999  int nb_precincts, precno;
2000  Jpeg2000Band *band = rlevel->band + bandno;
2001  int cblkno = 0, bandpos;
2002 
2003  bandpos = bandno + (reslevelno > 0);
2004 
2005  if (band->coord[0][0] == band->coord[0][1] ||
2006  band->coord[1][0] == band->coord[1][1])
2007  continue;
2008 
2009  nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
2010  /* Loop on precincts */
2011  for (precno = 0; precno < nb_precincts; precno++) {
2012  Jpeg2000Prec *prec = band->prec + precno;
2013 
2014  /* Loop on codeblocks */
2015  for (cblkno = 0;
2016  cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
2017  cblkno++) {
2018  int x, y;
2019  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
2020  int ret = decode_cblk(s, codsty, &t1, cblk,
2021  cblk->coord[0][1] - cblk->coord[0][0],
2022  cblk->coord[1][1] - cblk->coord[1][0],
2023  bandpos, comp->roi_shift);
2024  if (ret)
2025  coded = 1;
2026  else
2027  continue;
2028  x = cblk->coord[0][0] - band->coord[0][0];
2029  y = cblk->coord[1][0] - band->coord[1][0];
2030 
2031  if (comp->roi_shift)
2032  roi_scale_cblk(cblk, comp, &t1);
2033  if (codsty->transform == FF_DWT97)
2034  dequantization_float(x, y, cblk, comp, &t1, band);
2035  else if (codsty->transform == FF_DWT97_INT)
2036  dequantization_int_97(x, y, cblk, comp, &t1, band);
2037  else
2038  dequantization_int(x, y, cblk, comp, &t1, band);
2039  } /* end cblk */
2040  } /*end prec */
2041  } /* end band */
2042  } /* end reslevel */
2043 
2044  /* inverse DWT */
2045  if (coded)
2046  ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
2047 
2048  } /*end comp */
2049 }
2050 
2051 #define WRITE_FRAME(D, PIXEL) \
2052  static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2053  AVFrame * picture, int precision) \
2054  { \
2055  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2056  int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2057  int pixelsize = planar ? 1 : pixdesc->nb_components; \
2058  \
2059  int compno; \
2060  int x, y; \
2061  \
2062  for (compno = 0; compno < s->ncomponents; compno++) { \
2063  Jpeg2000Component *comp = tile->comp + compno; \
2064  Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2065  PIXEL *line; \
2066  float *datap = comp->f_data; \
2067  int32_t *i_datap = comp->i_data; \
2068  int cbps = s->cbps[compno]; \
2069  int w = tile->comp[compno].coord[0][1] - \
2070  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2071  int h = tile->comp[compno].coord[1][1] - \
2072  ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2073  int plane = 0; \
2074  ptrdiff_t dstoffset = 0; \
2075  \
2076  if (planar) \
2077  plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2078  else \
2079  dstoffset = s->cdef[compno] ? s->cdef[compno] - 1 : compno; \
2080  \
2081  y = tile->comp[compno].coord[1][0] - \
2082  ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2083  line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2084  for (; y < h; y++) { \
2085  PIXEL *dst; \
2086  \
2087  x = tile->comp[compno].coord[0][0] - \
2088  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2089  dst = line + x * pixelsize + dstoffset; \
2090  \
2091  if (codsty->transform == FF_DWT97) { \
2092  for (; x < w; x++) { \
2093  int val = lrintf(*datap) + (1 << (cbps - 1)); \
2094  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2095  val = av_clip(val, 0, (1 << cbps) - 1); \
2096  *dst = val << (precision - cbps); \
2097  datap++; \
2098  dst += pixelsize; \
2099  } \
2100  } else { \
2101  for (; x < w; x++) { \
2102  int val = *i_datap + (1 << (cbps - 1)); \
2103  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2104  val = av_clip(val, 0, (1 << cbps) - 1); \
2105  *dst = val << (precision - cbps); \
2106  i_datap++; \
2107  dst += pixelsize; \
2108  } \
2109  } \
2110  line += picture->linesize[plane] / sizeof(PIXEL); \
2111  } \
2112  } \
2113  \
2114  }
2115 
2116 WRITE_FRAME(8, uint8_t)
2117 WRITE_FRAME(16, uint16_t)
2118 
2119 #undef WRITE_FRAME
2120 
2121 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2122  int jobnr, int threadnr)
2123 {
2125  AVFrame *picture = td;
2126  Jpeg2000Tile *tile = s->tile + jobnr;
2127  int x;
2128 
2129  tile_codeblocks(s, tile);
2130 
2131  /* inverse MCT transformation */
2132  if (tile->codsty[0].mct)
2133  mct_decode(s, tile);
2134 
2135  for (x = 0; x < s->ncomponents; x++) {
2136  if (s->cdef[x] < 0) {
2137  for (x = 0; x < s->ncomponents; x++) {
2138  s->cdef[x] = x + 1;
2139  }
2140  if ((s->ncomponents & 1) == 0)
2141  s->cdef[s->ncomponents-1] = 0;
2142  break;
2143  }
2144  }
2145 
2146  if (s->precision <= 8) {
2147  write_frame_8(s, tile, picture, 8);
2148  } else {
2149  int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2150  picture->format == AV_PIX_FMT_RGB48 ||
2151  picture->format == AV_PIX_FMT_RGBA64 ||
2152  picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2153 
2154  write_frame_16(s, tile, picture, precision);
2155  }
2156 
2157  return 0;
2158 }
2159 
2161 {
2162  int tileno, compno;
2163  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2164  if (s->tile[tileno].comp) {
2165  for (compno = 0; compno < s->ncomponents; compno++) {
2166  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2167  Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2168 
2169  ff_jpeg2000_cleanup(comp, codsty);
2170  }
2171  av_freep(&s->tile[tileno].comp);
2172  av_freep(&s->tile[tileno].packed_headers);
2173  s->tile[tileno].packed_headers_size = 0;
2174  }
2175  }
2176  av_freep(&s->packed_headers);
2177  s->packed_headers_size = 0;
2178  memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
2179  av_freep(&s->tile);
2180  memset(s->codsty, 0, sizeof(s->codsty));
2181  memset(s->qntsty, 0, sizeof(s->qntsty));
2182  memset(s->properties, 0, sizeof(s->properties));
2183  memset(&s->poc , 0, sizeof(s->poc));
2184  s->numXtiles = s->numYtiles = 0;
2185  s->ncomponents = 0;
2186 }
2187 
2189 {
2190  Jpeg2000CodingStyle *codsty = s->codsty;
2191  Jpeg2000QuantStyle *qntsty = s->qntsty;
2192  Jpeg2000POC *poc = &s->poc;
2193  uint8_t *properties = s->properties;
2194 
2195  for (;;) {
2196  int len, ret = 0;
2197  uint16_t marker;
2198  int oldpos;
2199 
2200  if (bytestream2_get_bytes_left(&s->g) < 2) {
2201  av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2202  break;
2203  }
2204 
2205  marker = bytestream2_get_be16u(&s->g);
2206  oldpos = bytestream2_tell(&s->g);
2207  if (marker >= 0xFF30 && marker <= 0xFF3F)
2208  continue;
2209  if (marker == JPEG2000_SOD) {
2210  Jpeg2000Tile *tile;
2211  Jpeg2000TilePart *tp;
2212 
2213  if (!s->tile) {
2214  av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2215  return AVERROR_INVALIDDATA;
2216  }
2217  if (s->curtileno < 0) {
2218  av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2219  return AVERROR_INVALIDDATA;
2220  }
2221 
2222  tile = s->tile + s->curtileno;
2223  tp = tile->tile_part + tile->tp_idx;
2224  if (tp->tp_end < s->g.buffer) {
2225  av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2226  return AVERROR_INVALIDDATA;
2227  }
2228 
2229  if (s->has_ppm) {
2230  uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
2231  if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
2232  return AVERROR_INVALIDDATA;
2233  bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
2234  bytestream2_skip(&s->packed_headers_stream, tp_header_size);
2235  }
2236  if (tile->has_ppt && tile->tp_idx == 0) {
2238  }
2239 
2240  bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2241  bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2242 
2243  continue;
2244  }
2245  if (marker == JPEG2000_EOC)
2246  break;
2247 
2248  len = bytestream2_get_be16(&s->g);
2249  if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2250  if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2251  av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2252  return AVERROR_INVALIDDATA;
2253  }
2254  av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2255  break;
2256  }
2257 
2258  switch (marker) {
2259  case JPEG2000_SIZ:
2260  if (s->ncomponents) {
2261  av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2262  return AVERROR_INVALIDDATA;
2263  }
2264  ret = get_siz(s);
2265  if (!s->tile)
2266  s->numXtiles = s->numYtiles = 0;
2267  break;
2268  case JPEG2000_COC:
2269  ret = get_coc(s, codsty, properties);
2270  break;
2271  case JPEG2000_COD:
2272  ret = get_cod(s, codsty, properties);
2273  break;
2274  case JPEG2000_RGN:
2275  ret = get_rgn(s, len);
2276  break;
2277  case JPEG2000_QCC:
2278  ret = get_qcc(s, len, qntsty, properties);
2279  break;
2280  case JPEG2000_QCD:
2281  ret = get_qcd(s, len, qntsty, properties);
2282  break;
2283  case JPEG2000_POC:
2284  ret = get_poc(s, len, poc);
2285  break;
2286  case JPEG2000_SOT:
2287  if (!s->in_tile_headers) {
2288  s->in_tile_headers = 1;
2289  if (s->has_ppm) {
2290  bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
2291  }
2292  }
2293  if (!(ret = get_sot(s, len))) {
2294  av_assert1(s->curtileno >= 0);
2295  codsty = s->tile[s->curtileno].codsty;
2296  qntsty = s->tile[s->curtileno].qntsty;
2297  poc = &s->tile[s->curtileno].poc;
2298  properties = s->tile[s->curtileno].properties;
2299  }
2300  break;
2301  case JPEG2000_PLM:
2302  // the PLM marker is ignored
2303  case JPEG2000_COM:
2304  // the comment is ignored
2305  bytestream2_skip(&s->g, len - 2);
2306  break;
2307  case JPEG2000_CRG:
2308  ret = read_crg(s, len);
2309  break;
2310  case JPEG2000_TLM:
2311  // Tile-part lengths
2312  ret = get_tlm(s, len);
2313  break;
2314  case JPEG2000_PLT:
2315  // Packet length, tile-part header
2316  ret = get_plt(s, len);
2317  break;
2318  case JPEG2000_PPM:
2319  // Packed headers, main header
2320  if (s->in_tile_headers) {
2321  av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
2322  return AVERROR_INVALIDDATA;
2323  }
2324  ret = get_ppm(s, len);
2325  break;
2326  case JPEG2000_PPT:
2327  // Packed headers, tile-part header
2328  if (s->has_ppm) {
2329  av_log(s->avctx, AV_LOG_ERROR,
2330  "Cannot have both PPT and PPM marker.\n");
2331  return AVERROR_INVALIDDATA;
2332  }
2333 
2334  ret = get_ppt(s, len);
2335  break;
2336  default:
2337  av_log(s->avctx, AV_LOG_ERROR,
2338  "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2339  marker, bytestream2_tell(&s->g) - 4);
2340  bytestream2_skip(&s->g, len - 2);
2341  break;
2342  }
2343  if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2344  av_log(s->avctx, AV_LOG_ERROR,
2345  "error during processing marker segment %.4"PRIx16"\n",
2346  marker);
2347  return ret ? ret : -1;
2348  }
2349  }
2350  return 0;
2351 }
2352 
2353 /* Read bit stream packets --> T2 operation. */
2355 {
2356  int ret = 0;
2357  int tileno;
2358 
2359  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2360  Jpeg2000Tile *tile = s->tile + tileno;
2361 
2362  if ((ret = init_tile(s, tileno)) < 0)
2363  return ret;
2364 
2365  if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2366  return ret;
2367  }
2368 
2369  return 0;
2370 }
2371 
2373 {
2374  uint32_t atom_size, atom, atom_end;
2375  int search_range = 10;
2376 
2377  while (search_range
2378  &&
2379  bytestream2_get_bytes_left(&s->g) >= 8) {
2380  atom_size = bytestream2_get_be32u(&s->g);
2381  atom = bytestream2_get_be32u(&s->g);
2382  if (atom_size == 1) {
2383  if (bytestream2_get_be32u(&s->g)) {
2384  avpriv_request_sample(s->avctx, "Huge atom");
2385  return 0;
2386  }
2387  atom_size = bytestream2_get_be32u(&s->g);
2388  if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
2389  return AVERROR_INVALIDDATA;
2390  atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2391  } else {
2392  if (atom_size < 8 || (int64_t)bytestream2_tell(&s->g) + atom_size - 8 > INT_MAX)
2393  return AVERROR_INVALIDDATA;
2394  atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2395  }
2396 
2397  if (atom == JP2_CODESTREAM)
2398  return 1;
2399 
2400  if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2401  return 0;
2402 
2403  if (atom == JP2_HEADER &&
2404  atom_size >= 16) {
2405  uint32_t atom2_size, atom2, atom2_end;
2406  do {
2407  if (bytestream2_get_bytes_left(&s->g) < 8)
2408  break;
2409  atom2_size = bytestream2_get_be32u(&s->g);
2410  atom2 = bytestream2_get_be32u(&s->g);
2411  atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2412  if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2413  break;
2414  atom2_size -= 8;
2415  if (atom2 == JP2_CODESTREAM) {
2416  return 1;
2417  } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2418  int method = bytestream2_get_byteu(&s->g);
2419  bytestream2_skipu(&s->g, 2);
2420  if (method == 1) {
2421  s->colour_space = bytestream2_get_be32u(&s->g);
2422  }
2423  } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2424  int i, size, colour_count, colour_channels, colour_depth[3];
2425  colour_count = bytestream2_get_be16u(&s->g);
2426  colour_channels = bytestream2_get_byteu(&s->g);
2427  // FIXME: Do not ignore channel_sign
2428  colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2429  colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2430  colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2431  size = (colour_depth[0] + 7 >> 3) * colour_count +
2432  (colour_depth[1] + 7 >> 3) * colour_count +
2433  (colour_depth[2] + 7 >> 3) * colour_count;
2434  if (colour_count > AVPALETTE_COUNT ||
2435  colour_channels != 3 ||
2436  colour_depth[0] > 16 ||
2437  colour_depth[1] > 16 ||
2438  colour_depth[2] > 16 ||
2439  atom2_size < size) {
2440  avpriv_request_sample(s->avctx, "Unknown palette");
2441  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2442  continue;
2443  }
2444  s->pal8 = 1;
2445  for (i = 0; i < colour_count; i++) {
2446  uint32_t r, g, b;
2447  if (colour_depth[0] <= 8) {
2448  r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2449  r |= r >> colour_depth[0];
2450  } else {
2451  r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2452  }
2453  if (colour_depth[1] <= 8) {
2454  g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2455  g |= g >> colour_depth[1];
2456  } else {
2457  g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2458  }
2459  if (colour_depth[2] <= 8) {
2460  b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2461  b |= b >> colour_depth[2];
2462  } else {
2463  b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2464  }
2465  s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2466  }
2467  } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2468  int n = bytestream2_get_be16u(&s->g);
2469  for (; n>0; n--) {
2470  int cn = bytestream2_get_be16(&s->g);
2471  int av_unused typ = bytestream2_get_be16(&s->g);
2472  int asoc = bytestream2_get_be16(&s->g);
2473  if (cn < 4 && asoc < 4)
2474  s->cdef[cn] = asoc;
2475  }
2476  } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2477  int64_t vnum, vden, hnum, hden, vexp, hexp;
2478  uint32_t resx;
2479  bytestream2_skip(&s->g, 4);
2480  resx = bytestream2_get_be32u(&s->g);
2481  if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2482  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2483  continue;
2484  }
2485  vnum = bytestream2_get_be16u(&s->g);
2486  vden = bytestream2_get_be16u(&s->g);
2487  hnum = bytestream2_get_be16u(&s->g);
2488  hden = bytestream2_get_be16u(&s->g);
2489  vexp = bytestream2_get_byteu(&s->g);
2490  hexp = bytestream2_get_byteu(&s->g);
2491  if (!vnum || !vden || !hnum || !hden) {
2492  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2493  av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2494  continue;
2495  }
2496  if (vexp > hexp) {
2497  vexp -= hexp;
2498  hexp = 0;
2499  } else {
2500  hexp -= vexp;
2501  vexp = 0;
2502  }
2503  if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2504  && INT64_MAX / (vnum * hden) > pow(10, vexp))
2505  av_reduce(&s->sar.den, &s->sar.num,
2506  hnum * vden * pow(10, hexp),
2507  vnum * hden * pow(10, vexp),
2508  INT32_MAX);
2509  }
2510  bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2511  } while (atom_end - atom2_end >= 8);
2512  } else {
2513  search_range--;
2514  }
2515  bytestream2_seek(&s->g, atom_end, SEEK_SET);
2516  }
2517 
2518  return 0;
2519 }
2520 
2522 {
2525 }
2526 
2528 {
2529  static AVOnce init_static_once = AV_ONCE_INIT;
2531 
2532  ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2533  ff_jpeg2000dsp_init(&s->dsp);
2534 
2535  return 0;
2536 }
2537 
2538 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2539  int *got_frame, AVPacket *avpkt)
2540 {
2542  ThreadFrame frame = { .f = data };
2543  AVFrame *picture = data;
2544  int ret;
2545 
2546  s->avctx = avctx;
2547  bytestream2_init(&s->g, avpkt->data, avpkt->size);
2548  s->curtileno = -1;
2549  memset(s->cdef, -1, sizeof(s->cdef));
2550 
2551  if (bytestream2_get_bytes_left(&s->g) < 2) {
2552  ret = AVERROR_INVALIDDATA;
2553  goto end;
2554  }
2555 
2556  // check if the image is in jp2 format
2557  if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2558  (bytestream2_get_be32u(&s->g) == 12) &&
2559  (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2560  (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2561  if (!jp2_find_codestream(s)) {
2562  av_log(avctx, AV_LOG_ERROR,
2563  "Could not find Jpeg2000 codestream atom.\n");
2564  ret = AVERROR_INVALIDDATA;
2565  goto end;
2566  }
2567  } else {
2568  bytestream2_seek(&s->g, 0, SEEK_SET);
2569  }
2570 
2571  while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2572  bytestream2_skip(&s->g, 1);
2573 
2574  if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2575  av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2576  ret = AVERROR_INVALIDDATA;
2577  goto end;
2578  }
2579  if (ret = jpeg2000_read_main_headers(s))
2580  goto end;
2581 
2582  /* get picture buffer */
2583  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2584  goto end;
2585  picture->pict_type = AV_PICTURE_TYPE_I;
2586  picture->key_frame = 1;
2587 
2589  goto end;
2590 
2591  avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2592 
2594 
2595  *got_frame = 1;
2596 
2597  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2598  memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2599  if (s->sar.num && s->sar.den)
2600  avctx->sample_aspect_ratio = s->sar;
2601  s->sar.num = s->sar.den = 0;
2602 
2603  return bytestream2_tell(&s->g);
2604 
2605 end:
2607  return ret;
2608 }
2609 
2610 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2611 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2612 
2613 static const AVOption options[] = {
2614  { "lowres", "Lower the decoding resolution by a power of two",
2615  OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2616  { NULL },
2617 };
2618 
2619 static const AVClass jpeg2000_class = {
2620  .class_name = "jpeg2000",
2621  .item_name = av_default_item_name,
2622  .option = options,
2623  .version = LIBAVUTIL_VERSION_INT,
2624 };
2625 
2627  .name = "jpeg2000",
2628  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2629  .type = AVMEDIA_TYPE_VIDEO,
2630  .id = AV_CODEC_ID_JPEG2000,
2632  .priv_data_size = sizeof(Jpeg2000DecoderContext),
2635  .priv_class = &jpeg2000_class,
2636  .max_lowres = 5,
2638 };
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:1939
static void roi_scale_cblk(Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1)
Definition: jpeg2000dec.c:1967
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:2051
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:1698
static const AVOption options[]
Definition: jpeg2000dec.c:2613
#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:1890
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:1668
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:2160
static av_cold void jpeg2000_init_static_data(void)
Definition: jpeg2000dec.c:2521
#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:2538
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:2121
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:1722
#define RGB_PIXEL_FORMATS
Definition: jpeg2000dec.c:245
static void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:1980
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
Definition: jpeg2000dec.c:1905
#define MAX_POCS
Definition: jpeg2000dec.c:54
static const AVClass jpeg2000_class
Definition: jpeg2000dec.c:2619
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
Definition: jpeg2000dec.c:2527
static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band)
Definition: jpeg2000dec.c:1925
#define VD
Definition: jpeg2000dec.c:2611
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:1340
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:1870
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:1788
static int jp2_find_codestream(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:2372
static enum AVPixelFormat gray_pix_fmts[]
Definition: jpeg2000dec.c:261
#define OFFSET(x)
Definition: jpeg2000dec.c:2610
static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
Definition: jpeg2000dec.c:2188
static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
Definition: jpeg2000dec.c:1631
#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:2354
AVCodec ff_jpeg2000_decoder
Definition: jpeg2000dec.c:2626
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
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Jpeg2000Prec * prec
Definition: jpeg2000.h:207
int coord[2][2]
Definition: jpeg2000.h:203
float f_stepsize
Definition: jpeg2000.h:206
int i_stepsize
Definition: jpeg2000.h:205
int nb_terminations
Definition: jpeg2000.h:184
uint16_t * lengthinc
Definition: jpeg2000.h:179
uint16_t length
Definition: jpeg2000.h:178
int nb_terminationsinc
Definition: jpeg2000.h:185
int coord[2][2]
Definition: jpeg2000.h:189
uint8_t * data
Definition: jpeg2000.h:182
uint8_t nonzerobits
Definition: jpeg2000.h:176
uint8_t nb_lengthinc
Definition: jpeg2000.h:180
uint8_t npasses
Definition: jpeg2000.h:174
int * data_start
Definition: jpeg2000.h:186
uint8_t lblock
Definition: jpeg2000.h:181
size_t data_allocated
Definition: jpeg2000.h:183
uint8_t log2_cblk_width
Definition: jpeg2000.h:138
uint8_t prog_order
Definition: jpeg2000.h:145
uint8_t cblk_style
Definition: jpeg2000.h:144
float * f_data
Definition: jpeg2000.h:221
int coord[2][2]
Definition: jpeg2000.h:223
Jpeg2000ResLevel * reslevel
Definition: jpeg2000.h:219
Jpeg2000Tile * tile
Definition: jpeg2000dec.c:134
AVCodecContext * avctx
Definition: jpeg2000dec.c:97
GetByteContext packed_headers_stream
Definition: jpeg2000dec.c:110
uint32_t palette[256]
Definition: jpeg2000dec.c:117
Jpeg2000QuantStyle qntsty[4]
Definition: jpeg2000dec.c:126
Jpeg2000DSPContext dsp
Definition: jpeg2000dec.c:135
Jpeg2000CodingStyle codsty[4]
Definition: jpeg2000dec.c:125
GetByteContext g
Definition: jpeg2000dec.c:98
uint16_t LYEpoc
Definition: jpeg2000dec.c:57
uint16_t CEpoc
Definition: jpeg2000dec.c:59
uint16_t CSpoc
Definition: jpeg2000dec.c:58
int is_default
Definition: jpeg2000dec.c:68
Jpeg2000POCEntry poc[MAX_POCS]
Definition: jpeg2000dec.c:66
int decoded_layers
Definition: jpeg2000.h:198
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:197
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:195
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:196
int nb_codeblocks_height
Definition: jpeg2000.h:194
int nb_codeblocks_width
Definition: jpeg2000.h:193
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:153
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:152
uint8_t nguardbits
Definition: jpeg2000.h:155
uint8_t quantsty
Definition: jpeg2000.h:154
uint8_t log2_prec_width
Definition: jpeg2000.h:214
uint8_t nbands
Definition: jpeg2000.h:211
Jpeg2000Band * band
Definition: jpeg2000.h:215
uint8_t log2_prec_height
Definition: jpeg2000.h:214
uint8_t vis
Definition: jpeg2000.h:131
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:132
uint8_t val
Definition: jpeg2000.h:129
uint8_t tile_index
Definition: jpeg2000dec.c:72
const uint8_t * tp_end
Definition: jpeg2000dec.c:73
GetByteContext header_tpg
Definition: jpeg2000dec.c:74
GetByteContext tpg
Definition: jpeg2000dec.c:75
Jpeg2000TilePart tile_part[32]
Definition: jpeg2000dec.c:86
int coord[2][2]
Definition: jpeg2000dec.c:92
Jpeg2000POC poc
Definition: jpeg2000dec.c:85
Jpeg2000QuantStyle qntsty[4]
Definition: jpeg2000dec.c:84
Jpeg2000CodingStyle codsty[4]
Definition: jpeg2000dec.c:83
GetByteContext packed_headers_stream
Definition: jpeg2000dec.c:90
uint8_t has_ppt
Definition: jpeg2000dec.c:87
uint8_t * packed_headers
Definition: jpeg2000dec.c:88
uint16_t tp_idx
Definition: jpeg2000dec.c:91
int packed_headers_size
Definition: jpeg2000dec.c:89
Jpeg2000Component * comp
Definition: j2kenc.c:104
uint8_t properties[4]
Definition: jpeg2000dec.c:82
#define av_free(p)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
#define height
#define width
int size
const char * b
Definition: vf_curves.c:118
const char * g
Definition: vf_curves.c:117
const char * r
Definition: vf_curves.c:116
if(ret< 0)
Definition: vf_mcdeint.c:282
int len
static double c[64]