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