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